-
Notifications
You must be signed in to change notification settings - Fork 0
me model calibration result proposal #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
09ab372
me model calibration result proposal
19dc825
make rin optional, remove holding/threshold current from MEModel
ed14f12
MEModelCalibrationResult schema
b456274
add filter and router
6ec4c69
fixing test
f170314
fix ruff
2646fac
Initial implementation of `brain-atlas` endpoints (#195)
mgeplf 86362be
recreate migration after rebase
49769b9
fix tests due to rebase
1efe91c
fix merge
9c094e0
Remove unique constraint from Person (#199)
eleftherioszisis a9564d7
new migration file
efc04bd
move calibrated_entity_id to base class
42f4c3a
import calibration results
92b446d
adding memodel to calibrationresult relationship
3e9f707
Add joinedload
g-bar a72f42c
Add nested created_by, updated_by loader
g-bar 6c39b76
add test for the memodel to calibration relationship
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
alembic/versions/20250526_173512_8d1610d7c882_default_migration_message.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Default migration message | ||
|
||
Revision ID: 8d1610d7c882 | ||
Revises: 634224e88212 | ||
Create Date: 2025-05-26 17:35:12.743948 | ||
|
||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
from sqlalchemy import Text | ||
import app.db.types | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "8d1610d7c882" | ||
down_revision: Union[str, None] = "634224e88212" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint("unique_person_name_1", "person", type_="unique") | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_unique_constraint("unique_person_name_1", "person", ["given_name", "family_name"]) | ||
# ### end Alembic commands ### |
138 changes: 138 additions & 0 deletions
138
alembic/versions/20250526_222351_1589bff44728_default_migration_message.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
"""Default migration message | ||
|
||
Revision ID: 1589bff44728 | ||
Revises: 8d1610d7c882 | ||
Create Date: 2025-05-26 22:23:51.082630 | ||
|
||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
from alembic_postgresql_enum import TableReference | ||
|
||
from sqlalchemy import Text | ||
import app.db.types | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "1589bff44728" | ||
down_revision: Union[str, None] = "8d1610d7c882" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"memodel_calibration_result", | ||
sa.Column("id", sa.Uuid(), nullable=False), | ||
sa.Column("holding_current", sa.Float(), nullable=False), | ||
sa.Column("threshold_current", sa.Float(), nullable=False), | ||
sa.Column("rin", sa.Float(), nullable=True), | ||
sa.Column("calibrated_entity_id", sa.Uuid(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["calibrated_entity_id"], | ||
["memodel.id"], | ||
name=op.f("fk_memodel_calibration_result_calibrated_entity_id_memodel"), | ||
), | ||
sa.ForeignKeyConstraint( | ||
["id"], ["entity.id"], name=op.f("fk_memodel_calibration_result_id_entity") | ||
), | ||
sa.PrimaryKeyConstraint("id", name=op.f("pk_memodel_calibration_result")), | ||
) | ||
op.create_index( | ||
op.f("ix_memodel_calibration_result_calibrated_entity_id"), | ||
"memodel_calibration_result", | ||
["calibrated_entity_id"], | ||
unique=False, | ||
) | ||
op.drop_column("memodel", "holding_current") | ||
op.drop_column("memodel", "threshold_current") | ||
op.sync_enum_values( | ||
enum_schema="public", | ||
enum_name="entitytype", | ||
new_values=[ | ||
"analysis_software_source_code", | ||
"brain_atlas", | ||
"brain_atlas_region", | ||
"emodel", | ||
"cell_composition", | ||
"memodel_calibration_result", | ||
"experimental_bouton_density", | ||
"experimental_neuron_density", | ||
"experimental_synapses_per_connection", | ||
"memodel", | ||
"mesh", | ||
"me_type_density", | ||
"reconstruction_morphology", | ||
"electrical_cell_recording", | ||
"electrical_recording_stimulus", | ||
"single_neuron_simulation", | ||
"single_neuron_synaptome", | ||
"single_neuron_synaptome_simulation", | ||
"ion_channel_model", | ||
"subject", | ||
"validation_result", | ||
], | ||
affected_columns=[ | ||
TableReference(table_schema="public", table_name="entity", column_name="type") | ||
], | ||
enum_values_to_rename=[], | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.sync_enum_values( | ||
enum_schema="public", | ||
enum_name="entitytype", | ||
new_values=[ | ||
"analysis_software_source_code", | ||
"brain_atlas", | ||
"brain_atlas_region", | ||
"emodel", | ||
"cell_composition", | ||
"experimental_bouton_density", | ||
"experimental_neuron_density", | ||
"experimental_synapses_per_connection", | ||
"memodel", | ||
"mesh", | ||
"me_type_density", | ||
"reconstruction_morphology", | ||
"electrical_cell_recording", | ||
"electrical_recording_stimulus", | ||
"single_neuron_simulation", | ||
"single_neuron_synaptome", | ||
"single_neuron_synaptome_simulation", | ||
"ion_channel_model", | ||
"subject", | ||
"validation_result", | ||
], | ||
affected_columns=[ | ||
TableReference(table_schema="public", table_name="entity", column_name="type") | ||
], | ||
enum_values_to_rename=[], | ||
) | ||
op.add_column( | ||
"memodel", | ||
sa.Column( | ||
"threshold_current", | ||
sa.DOUBLE_PRECISION(precision=53), | ||
autoincrement=False, | ||
nullable=True, | ||
), | ||
) | ||
op.add_column( | ||
"memodel", | ||
sa.Column( | ||
"holding_current", sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True | ||
), | ||
) | ||
op.drop_index( | ||
op.f("ix_memodel_calibration_result_calibrated_entity_id"), | ||
table_name="memodel_calibration_result", | ||
) | ||
op.drop_table("memodel_calibration_result") | ||
# ### end Alembic commands ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import uuid | ||
from typing import Annotated | ||
|
||
from fastapi_filter import FilterDepends | ||
|
||
from app.db.model import MEModelCalibrationResult | ||
from app.filters.base import CustomFilter | ||
from app.filters.common import EntityFilterMixin | ||
|
||
|
||
class MEModelCalibrationResultFilter( | ||
CustomFilter, | ||
EntityFilterMixin, | ||
): | ||
passed: bool | None = None | ||
calibrated_entity_id: uuid.UUID | None = None | ||
|
||
order_by: list[str] = ["calibrated_entity_id"] # noqa: RUF012 | ||
|
||
class Constants(CustomFilter.Constants): | ||
model = MEModelCalibrationResult | ||
ordering_model_fields = ["calibrated_entity_id"] # noqa: RUF012 | ||
|
||
|
||
MEModelCalibrationResultFilterDep = Annotated[ | ||
MEModelCalibrationResultFilter, FilterDepends(MEModelCalibrationResultFilter) | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from fastapi import APIRouter | ||
|
||
import app.service.memodel_calibration_result | ||
|
||
router = APIRouter( | ||
prefix="/memodel-calibration-result", | ||
tags=["memodel-calibration-result"], | ||
) | ||
read_many = router.get("")(app.service.memodel_calibration_result.read_many) | ||
read_one = router.get("/{id_}")(app.service.memodel_calibration_result.read_one) | ||
create_one = router.post("")(app.service.memodel_calibration_result.create_one) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import uuid | ||
|
||
from pydantic import BaseModel | ||
|
||
from app.schemas.agent import CreatedByUpdatedByMixin | ||
from app.schemas.base import ( | ||
CreationMixin, | ||
IdentifiableMixin, | ||
) | ||
|
||
|
||
class MEModelCalibrationResultBase(BaseModel): | ||
"""Base model for MEModel calibration results.""" | ||
|
||
holding_current: float | ||
threshold_current: float | ||
rin: float | None = None | ||
calibrated_entity_id: uuid.UUID | ||
|
||
|
||
class MEModelCalibrationResultRead( | ||
MEModelCalibrationResultBase, CreationMixin, IdentifiableMixin, CreatedByUpdatedByMixin | ||
): | ||
"""Read model for MEModel calibration results, including entity metadata.""" | ||
|
||
|
||
class MEModelCalibrationResultCreate(MEModelCalibrationResultBase): | ||
"""Create model for MEModel calibration results.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implication of this is that now a SingleNeuronSimulation should have a reference to both an MEModal and an MEModelCalibrationResult.
Not sure how it is for
SingleNeuronSynaptome
which also reference MEModelThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hum, @AurelienJaquier should we have multiple calibration result per memodel or just one ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have just one.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If users create an MEModel, and run a simulation without running validation, then the resulting simulations use different currents (the dummy values in bluenaas). That's a different model than then one with a calibration result.
Is it possible to re-run validation? Is Calibration result itself immutable? If i re-run the validation service do I update the values in the existing CalibrationResult?, is it guaranteed not to result in different values and thus not 'invalidate' simulations ran using the previous values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calibration result is immutable. However, we can replace it with a new calibration result. (recomputing the calibration)
Which means that it would be a different calibration result id. Therefore the simulation result would likely be different.
Provenance should track the memodel id + calibration result id used.
Wrt invalidation, we don't have that concept so far.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if a change in a field of calibration result means the model will behave differently, then it should be a different entity.
Therefore, we would replace the previous calibration result entity, with a new one, with a different id.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@g-bar , I am trying to add an memodel to calibrationresult relationship:
calibration_result = relationship( "MEModelCalibrationResult", uselist=False, foreign_keys="MEModelCalibrationResult.calibrated_entity_id", lazy="joined", )
However this thing is giving me the following error:
from return response_schema_class.model_validate(row) in "router_read_one":
"Error extracting attribute: InvalidRequestError: 'MEModel.calibration_result' is not available due to lazy='raise' [type=get_attribute_error, input_value=<app.db.model.MEModel object at 0x12cbaaa80>, input_type=MEModel]"
I think you worked on that , would you have any clue ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we just had to add a
joinedload
to the MEModel response query. (See my latest commit).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is calibration_result part of the immutability constraints of the MEModel based on the immutability proposal the is currently pending? i.e. will we need to create a new MEModel id if a new calibration result is created?
If the MEModel results differ when a calibration result is present, then it should be part of its immutability constraints. Otherwise, it will be quite difficult to track when a config (that points to entities like memodels for example), which is an input to an activity, has changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no it will be a new calibration result. With respect to the current proposal, the previous calibration result would be "deprecated". The ME Model would stay the same.
Provenance should log the usage of the MEModel and the CalibrationResult.