Skip to content

Commit 8cabb78

Browse files
authored
[REF] annotation analyses do not create id column (#739)
* change how annotation_analysis id is made * run black * add performance test
1 parent ce8f0b7 commit 8cabb78

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

store/neurostore/models/data.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Annotation(BaseMixin, db.Model):
115115
)
116116

117117

118-
class AnnotationAnalysis(BaseMixin, db.Model):
118+
class AnnotationAnalysis(db.Model):
119119
__tablename__ = "annotation_analyses"
120120
__table_args__ = (
121121
ForeignKeyConstraint(
@@ -126,17 +126,24 @@ class AnnotationAnalysis(BaseMixin, db.Model):
126126
)
127127
__mapper_args__ = {"confirm_deleted_rows": False}
128128

129+
created_at = db.Column(
130+
db.DateTime(timezone=True), index=True, server_default=func.now()
131+
)
132+
updated_at = db.Column(db.DateTime(timezone=True), index=True, onupdate=func.now())
133+
129134
user_id = db.Column(db.Text, db.ForeignKey("users.external_id"), index=True)
130135
study_id = db.Column(db.Text, nullable=False)
131136
studyset_id = db.Column(db.Text, nullable=False)
132137
annotation_id = db.Column(
133138
db.Text,
134139
db.ForeignKey("annotations.id", ondelete="CASCADE"),
140+
primary_key=True,
135141
index=True,
136142
)
137143
analysis_id = db.Column(
138144
db.Text,
139145
db.ForeignKey("analyses.id", ondelete="CASCADE"),
146+
primary_key=True,
140147
index=True,
141148
)
142149
note = db.Column(MutableDict.as_mutable(JSONB))
@@ -145,6 +152,14 @@ class AnnotationAnalysis(BaseMixin, db.Model):
145152
"User", backref=backref("annotation_analyses", passive_deletes=True)
146153
)
147154

155+
@hybrid_property
156+
def id(self):
157+
return f"{self.annotation_id}_{self.analysis_id}"
158+
159+
@id.expression
160+
def id(cls):
161+
return cls.annotation_id + "_" + cls.analysis_id
162+
148163

149164
class BaseStudy(BaseMixin, db.Model):
150165
__tablename__ = "base_studies"

store/neurostore/openapi

store/neurostore/resources/data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ def eager_load(self, q, args=None):
257257
.options(raiseload("*", sql_only=True)),
258258
selectinload(Annotation.annotation_analyses)
259259
.load_only(
260-
AnnotationAnalysis.id,
261260
AnnotationAnalysis.analysis_id,
262261
AnnotationAnalysis.created_at,
263262
AnnotationAnalysis.study_id,

store/neurostore/schemas/data.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,6 @@ def __init__(self, *args, **kwargs):
150150
OPTIONS_CLASS = BaseSchemaOpts
151151
# normal return key
152152

153-
created_at = fields.DateTime(dump_only=True, metadata={"info_field": True})
154-
updated_at = fields.DateTime(dump_only=True, metadata={"info_field": True})
155-
156153
id = fields.String(metadata={"info_field": True, "id_field": True})
157154

158155
def on_bind_field(self, field_name, field_obj):
@@ -171,6 +168,8 @@ class BaseDataSchema(BaseSchema):
171168
metadata={"info_field": True},
172169
default=None,
173170
)
171+
created_at = fields.DateTime(dump_only=True, metadata={"info_field": True})
172+
updated_at = fields.DateTime(dump_only=True, metadata={"info_field": True})
174173

175174

176175
class ConditionSchema(BaseDataSchema):

store/neurostore/tests/api/test_crud.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ def test_read(auth_client, user_data, endpoint, model, schema, session):
112112
resp_ids = set([res["id"] for res in resp.json()["results"]])
113113
assert query_ids == resp_ids
114114

115+
# get specific record
116+
record = expected_results[0]
117+
get_resp = auth_client.get(f"/api/{endpoint}/{record.id}")
118+
assert get_resp.status_code == 200
119+
115120

116121
@pytest.mark.parametrize(
117122
"endpoint,model,schema,update",

store/neurostore/tests/api/test_performance.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,23 @@ def test_updating_annotation(assign_neurosynth_to_user, auth_client, session):
108108
q = AnnotationsView().eager_load(q)
109109
annotation = q.one()
110110
annotation_dict = AnnotationSchema().dump(annotation)
111-
# with profiled_yappi("update_annotation_largs.prof"):
111+
# with profiled_yappi("update_annotation_large.prof"):
112112
for i in range(len(annotation_dict["notes"])):
113113
annotation_dict["notes"][i]["note"]["_5"] = 1.0
114114
auth_client.put(f"/api/annotations/{annotation.id}", data=annotation_dict)
115115

116+
@performance_test
117+
def test_updating_annotation_analysis(assign_neurosynth_to_user, auth_client, session):
118+
q = Annotation.query
119+
q = AnnotationsView().eager_load(q)
120+
annotation = q.one()
121+
annotation_dict = AnnotationSchema().dump(annotation)
122+
# with profiled_yappi("update_annotation_analysis_large.prof"):
123+
for i in range(len(annotation_dict["notes"])):
124+
annotation_analysis = annotation_dict["notes"][i]
125+
annotation_analysis["note"]["_5"] = 1.0
126+
aa_id = annotation_analysis["id"]
127+
auth_client.put(f"/api/annotation-analyses/{aa_id}", data=annotation_analysis)
116128

117129
@performance_test
118130
def test_updating_annotation_one(assign_neurosynth_to_user, auth_client, session):

0 commit comments

Comments
 (0)