1616from neurostore .database import db
1717from neurostore .models import (
1818 Analysis ,
19+ Table ,
1920 AnalysisConditions ,
2021 AnnotationAnalysis ,
2122 Annotation ,
@@ -296,9 +297,14 @@ def ingest_neurosynth(max_rows=None):
296297 )
297298 analyses = []
298299 points = []
300+ tables = {}
299301
300302 for order , (t_id , df ) in enumerate (study_coord_data .groupby ("table_id" )):
301- a = Analysis (name = str (t_id ), study = s , order = order , table_id = str (t_id ))
303+ table = tables .get (t_id ) or Table (
304+ t_id = str (t_id ), name = str (t_id ), study = s , user_id = s .user_id
305+ )
306+ tables [t_id ] = table
307+ a = Analysis (name = str (t_id ), study = s , order = order , table = table )
302308 analyses .append (a )
303309 point_idx = 0
304310 for _ , p in df .iterrows ():
@@ -314,6 +320,7 @@ def ingest_neurosynth(max_rows=None):
314320 )
315321 points .append (point )
316322 point_idx += 1
323+ to_commit .extend (tables .values ())
317324 to_commit .extend (points )
318325 to_commit .extend (analyses )
319326 studies .append (s )
@@ -413,9 +420,14 @@ def ingest_neuroquery(max_rows=None):
413420 )
414421 analyses = []
415422 points = []
423+ tables = {}
416424
417425 for order , (t_id , df ) in enumerate (study_coord_data .groupby ("table_id" )):
418- a = Analysis (name = str (t_id ), table_id = str (t_id ), order = order , study = s )
426+ table = tables .get (t_id ) or Table (
427+ t_id = str (t_id ), name = str (t_id ), study = s , user_id = s .user_id
428+ )
429+ tables [t_id ] = table
430+ a = Analysis (name = str (t_id ), table = table , order = order , study = s )
419431 analyses .append (a )
420432 point_idx = 0
421433 for _ , p in df .iterrows ():
@@ -432,7 +444,9 @@ def ingest_neuroquery(max_rows=None):
432444 points .append (point )
433445 point_idx += 1
434446
435- db .session .add_all ([s ] + analyses + points + [base_study ])
447+ db .session .add_all (
448+ [s ] + analyses + points + list (tables .values ()) + [base_study ]
449+ )
436450 # db.session.commit()
437451
438452 # make a neuroquery studyset
@@ -556,20 +570,34 @@ def update_study_info(study, metadata_row, text_row, doi, pmcid, year, level):
556570 def process_coordinates (id_ , s , metadata_row ):
557571 analyses = []
558572 points = []
573+ tables = []
559574 try :
560575 study_coord_data = coordinates_df .loc [[id_ ]]
561576 except KeyError :
562577 print (f"pmid: { id_ } has no coordinates" )
563578 return analyses , points
564579 for order , (t_id , df ) in enumerate (study_coord_data .groupby ("table_id" )):
565- a = (
566- Analysis .query .filter_by (
567- table_id = str (t_id ), study_id = s .id
568- ).one_or_none ()
569- or Analysis ()
580+ table = Table .query .filter_by (
581+ t_id = str (t_id ), study_id = s .id
582+ ).one_or_none () or Table (t_id = str (t_id ), study = s , user_id = s .user_id )
583+ if table not in tables :
584+ tables .append (table )
585+ if not table .name :
586+ table .name = df ["table_label" ][0 ] or str (t_id )
587+ if table .caption is None :
588+ table .caption = (
589+ df ["table_caption" ][0 ]
590+ if not df ["table_caption" ].isna ()[0 ]
591+ else None
592+ )
593+ existing_analysis = (
594+ Analysis .query .filter_by (table_id = table .id , study_id = s .id ).one_or_none ()
595+ if table .id
596+ else None
570597 )
598+ a = existing_analysis or Analysis ()
571599 a .name = df ["table_label" ][0 ] or str (t_id )
572- a .table_id = str ( t_id )
600+ a .table = table
573601 a .order = a .order or order
574602 a .description = (
575603 df ["table_caption" ][0 ] if not df ["table_caption" ].isna ()[0 ] else None
@@ -594,7 +622,7 @@ def process_coordinates(id_, s, metadata_row):
594622 )
595623 points .append (point )
596624 point_idx += 1
597- return analyses , points
625+ return analyses , points , tables
598626
599627 to_commit = []
600628 all_base_studies = []
@@ -663,9 +691,10 @@ def process_coordinates(id_, s, metadata_row):
663691 s = all_studies .get (pmid , Study ())
664692 update_study_info (s , metadata_row , text_row , doi , pmcid , year , level )
665693
666- analyses , points = process_coordinates (pmid , s , metadata_row )
694+ analyses , points , tables = process_coordinates (pmid , s , metadata_row )
667695 to_commit .extend (points )
668696 to_commit .extend (analyses )
697+ to_commit .extend (tables )
669698 base_study .versions .append (s )
670699
671700 db .session .add_all (to_commit )
0 commit comments