Skip to content

Commit

Permalink
[REF] annotation analyses do not create id column (#739)
Browse files Browse the repository at this point in the history
* change how annotation_analysis id is made

* run black

* add performance test
  • Loading branch information
jdkent authored Mar 13, 2024
1 parent ce8f0b7 commit 8cabb78
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
17 changes: 16 additions & 1 deletion store/neurostore/models/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Annotation(BaseMixin, db.Model):
)


class AnnotationAnalysis(BaseMixin, db.Model):
class AnnotationAnalysis(db.Model):
__tablename__ = "annotation_analyses"
__table_args__ = (
ForeignKeyConstraint(
Expand All @@ -126,17 +126,24 @@ class AnnotationAnalysis(BaseMixin, db.Model):
)
__mapper_args__ = {"confirm_deleted_rows": False}

created_at = db.Column(
db.DateTime(timezone=True), index=True, server_default=func.now()
)
updated_at = db.Column(db.DateTime(timezone=True), index=True, onupdate=func.now())

user_id = db.Column(db.Text, db.ForeignKey("users.external_id"), index=True)
study_id = db.Column(db.Text, nullable=False)
studyset_id = db.Column(db.Text, nullable=False)
annotation_id = db.Column(
db.Text,
db.ForeignKey("annotations.id", ondelete="CASCADE"),
primary_key=True,
index=True,
)
analysis_id = db.Column(
db.Text,
db.ForeignKey("analyses.id", ondelete="CASCADE"),
primary_key=True,
index=True,
)
note = db.Column(MutableDict.as_mutable(JSONB))
Expand All @@ -145,6 +152,14 @@ class AnnotationAnalysis(BaseMixin, db.Model):
"User", backref=backref("annotation_analyses", passive_deletes=True)
)

@hybrid_property
def id(self):
return f"{self.annotation_id}_{self.analysis_id}"

@id.expression
def id(cls):
return cls.annotation_id + "_" + cls.analysis_id


class BaseStudy(BaseMixin, db.Model):
__tablename__ = "base_studies"
Expand Down
2 changes: 1 addition & 1 deletion store/neurostore/openapi
1 change: 0 additions & 1 deletion store/neurostore/resources/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ def eager_load(self, q, args=None):
.options(raiseload("*", sql_only=True)),
selectinload(Annotation.annotation_analyses)
.load_only(
AnnotationAnalysis.id,
AnnotationAnalysis.analysis_id,
AnnotationAnalysis.created_at,
AnnotationAnalysis.study_id,
Expand Down
5 changes: 2 additions & 3 deletions store/neurostore/schemas/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ def __init__(self, *args, **kwargs):
OPTIONS_CLASS = BaseSchemaOpts
# normal return key

created_at = fields.DateTime(dump_only=True, metadata={"info_field": True})
updated_at = fields.DateTime(dump_only=True, metadata={"info_field": True})

id = fields.String(metadata={"info_field": True, "id_field": True})

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


class ConditionSchema(BaseDataSchema):
Expand Down
5 changes: 5 additions & 0 deletions store/neurostore/tests/api/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def test_read(auth_client, user_data, endpoint, model, schema, session):
resp_ids = set([res["id"] for res in resp.json()["results"]])
assert query_ids == resp_ids

# get specific record
record = expected_results[0]
get_resp = auth_client.get(f"/api/{endpoint}/{record.id}")
assert get_resp.status_code == 200


@pytest.mark.parametrize(
"endpoint,model,schema,update",
Expand Down
14 changes: 13 additions & 1 deletion store/neurostore/tests/api/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,23 @@ def test_updating_annotation(assign_neurosynth_to_user, auth_client, session):
q = AnnotationsView().eager_load(q)
annotation = q.one()
annotation_dict = AnnotationSchema().dump(annotation)
# with profiled_yappi("update_annotation_largs.prof"):
# with profiled_yappi("update_annotation_large.prof"):
for i in range(len(annotation_dict["notes"])):
annotation_dict["notes"][i]["note"]["_5"] = 1.0
auth_client.put(f"/api/annotations/{annotation.id}", data=annotation_dict)

@performance_test
def test_updating_annotation_analysis(assign_neurosynth_to_user, auth_client, session):
q = Annotation.query
q = AnnotationsView().eager_load(q)
annotation = q.one()
annotation_dict = AnnotationSchema().dump(annotation)
# with profiled_yappi("update_annotation_analysis_large.prof"):
for i in range(len(annotation_dict["notes"])):
annotation_analysis = annotation_dict["notes"][i]
annotation_analysis["note"]["_5"] = 1.0
aa_id = annotation_analysis["id"]
auth_client.put(f"/api/annotation-analyses/{aa_id}", data=annotation_analysis)

@performance_test
def test_updating_annotation_one(assign_neurosynth_to_user, auth_client, session):
Expand Down

0 comments on commit 8cabb78

Please sign in to comment.