Skip to content

Commit 82609db

Browse files
committed
add Table table and endpoint
1 parent 232a0c2 commit 82609db

File tree

11 files changed

+352
-19
lines changed

11 files changed

+352
-19
lines changed

store/backend/neurostore/ingest/__init__.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from neurostore.database import db
1717
from 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)

store/backend/neurostore/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
BaseStudy,
66
Study,
77
Analysis,
8+
Table,
89
Condition,
910
Point,
1011
Image,
@@ -26,6 +27,7 @@
2627
"BaseStudy",
2728
"Study",
2829
"Analysis",
30+
"Table",
2931
"Condition",
3032
"Point",
3133
"Image",

store/backend/neurostore/models/data.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,14 @@ class Study(BaseMixin, db.Model):
473473
passive_deletes=True,
474474
cascade="all, delete-orphan",
475475
)
476+
tables = relationship(
477+
"Table",
478+
back_populates="study",
479+
passive_deletes=True,
480+
cascade="all, delete-orphan",
481+
cascade_backrefs=False,
482+
lazy="selectin",
483+
)
476484

477485
__table_args__ = (
478486
db.CheckConstraint(level.in_(["group", "meta"])),
@@ -510,18 +518,45 @@ class StudysetStudy(db.Model):
510518
)
511519

512520

521+
class Table(BaseMixin, db.Model):
522+
__tablename__ = "tables"
523+
524+
study_id = db.Column(
525+
db.Text, db.ForeignKey("studies.id", ondelete="CASCADE"), index=True
526+
)
527+
t_id = db.Column(db.Text)
528+
name = db.Column(db.Text)
529+
footer = db.Column(db.Text)
530+
caption = db.Column(db.Text)
531+
user_id = db.Column(db.Text, db.ForeignKey("users.external_id"), index=True)
532+
533+
study = relationship(
534+
"Study",
535+
back_populates="tables",
536+
passive_deletes=True,
537+
)
538+
user = relationship(
539+
"User", backref=backref("tables", cascade_backrefs=False, passive_deletes=True)
540+
)
541+
542+
__table_args__ = (
543+
db.UniqueConstraint("study_id", "t_id", name="uq_tables_study_t_id"),
544+
)
545+
546+
513547
class Analysis(BaseMixin, db.Model):
514548
__tablename__ = "analyses"
515549

516550
study_id = db.Column(
517551
db.Text, db.ForeignKey("studies.id", ondelete="CASCADE"), index=True
518552
)
553+
table_id = db.Column(
554+
db.Text, db.ForeignKey("tables.id", ondelete="SET NULL"), index=True
555+
)
519556
name = db.Column(db.String)
520557
description = db.Column(db.String)
521558
metadata_ = db.Column(JSONB)
522559
order = db.Column(db.Integer)
523-
# used to keep track of neurosynth analyses (in case of neurosynth/ace updates)
524-
table_id = db.Column(db.String)
525560
points = relationship(
526561
"Point",
527562
backref=backref("analysis", passive_deletes=True),
@@ -553,6 +588,12 @@ class Analysis(BaseMixin, db.Model):
553588
cascade="all, delete-orphan",
554589
cascade_backrefs=False,
555590
)
591+
table = relationship(
592+
"Table",
593+
backref=backref("analyses", cascade_backrefs=False, passive_deletes=True),
594+
foreign_keys=[table_id],
595+
passive_deletes=True,
596+
)
556597

557598

558599
class Condition(BaseMixin, db.Model):

store/backend/neurostore/openapi

store/backend/neurostore/resources/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
BaseStudiesView,
66
StudiesView,
77
AnalysesView,
8+
TablesView,
89
ConditionsView,
910
ImagesView,
1011
PointsView,
@@ -29,6 +30,7 @@
2930
"BaseStudiesView",
3031
"StudiesView",
3132
"AnalysesView",
33+
"TablesView",
3234
"ConditionsView",
3335
"ImagesView",
3436
"PointsView",

0 commit comments

Comments
 (0)