Skip to content
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

fix tests after model update with version and model version, and extend forbidding extra fields to nested nodes #135

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
],
"intrinsic_variable_description": [
"Test intrinsic variable 1\\nwith escaped character"
]
],
"version": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
],
"intrinsic_variable_description": [
"Test intrinsic variable 2"
]
],
"version": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
],
"submitted_in_study_uuid": "a2fdbd58-ee11-4cd9-bc6a-f3d3da7fff71",
"correlation_method": [],
"example_image_uri": []
"example_image_uri": [],
"version": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"protocol_description": "Test image acquisition parameters 1",
"imaging_instrument_description": "Test imaging instrument 1",
"fbbi_id": [],
"imaging_method_name": "confocal microscopy"
"imaging_method_name": "confocal microscopy",
"version": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"title_id": "Test specimen 1",
"uuid": "7199d730-29f1-4ad8-b599-e9089cbb2d7b",
"protocol_description": "Test sample preparation protocol 1",
"signal_channel_information": []
"signal_channel_information": [],
"version": 1
}
5 changes: 5 additions & 0 deletions bia-export/test/input_data/studies/S-BIADTEST.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"uuid": "a2fdbd58-ee11-4cd9-bc6a-f3d3da7fff71",
"version": 1,
"model": {
"type_name": "Study",
"version": 1
},
"accession_id": "S-BIADTEST",
"licence": "CC0",
"author": [
Expand Down
10 changes: 10 additions & 0 deletions bia-export/test/output_data/bia_export.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"uuid": "a2fdbd58-ee11-4cd9-bc6a-f3d3da7fff71",
"version": 1,
"model": {
"type_name": "Study",
"version": 1
},
"accession_id": "S-BIADTEST",
"licence": "CC0",
"author": [
Expand Down Expand Up @@ -75,6 +80,11 @@
{
"title_id": "Study Component 1",
"uuid": "47a4ab60-c76d-4424-bfaa-c2a024de720c",
"version": 1,
"model": {
"type_name": "ExperimentalImagingDataset",
"version": 1
},
"description": "Description of study component 1",
"attribute": {},
"analysis_method": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def extract_annotation_method_dicts(submission: Submission) -> List[Dict[str, An
model_dict["accno"] = section.__dict__.get("accno", "")
model_dict["accession_id"] = submission.accno
model_dict["uuid"] = generate_annotation_method_uuid(model_dict)
model_dict["version"] = 1
model_dict = filter_model_dictionary(model_dict, bia_data_model.AnnotationMethod)

model_dicts.append(model_dict)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def extract_biosample_dicts(submission: Submission) -> List[Dict[str, Any]]:

model_dict["accession_id"] = submission.accno
model_dict["uuid"] = generate_biosample_uuid(model_dict)
model_dict["version"] = 1
model_dict = filter_model_dictionary(model_dict, bia_data_model.BioSample)
model_dicts.append(model_dict)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def get_experimental_imaging_dataset(
"analysis_method": analysis_method_list,
"correlation_method": correlation_method_list,
"example_image_uri": [],
"version": 1
"attribute": {
"associations": associations
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def get_file_reference_by_study_component(
"accession_id": submission.accno,
"file_path": str(f.path),
"size_in_bytes": str(f.size),
"version": 1
}
fileref_uuid = dict_to_uuid(
file_dict, ["accession_id", "file_path", "size_in_bytes"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def extract_image_acquisition_dicts(submission: Submission) -> List[Dict[str, An
model_dict["accno"] = section.__dict__.get("accno", "")
model_dict["accession_id"] = submission.accno
model_dict["uuid"] = generate_image_acquisition_uuid(model_dict)
model_dict["version"] = 1
model_dict = filter_model_dictionary(model_dict, bia_data_model.ImageAcquisition)
model_dicts.append(model_dict)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def extract_specimen_growth_protocol_dicts(submission: Submission) -> List[Dict[
model_dict["accno"] = section.__dict__.get("accno", "")
model_dict["accession_id"] = submission.accno
model_dict["uuid"] = generate_specimen_growth_protocol_uuid(model_dict)
model_dict["version"] = 1
model_dict = filter_model_dictionary(model_dict, bia_data_model.SpecimenGrowthProtocol)

model_dicts.append(model_dict)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def extract_specimen_preparation_protocol_dicts(submission: Submission) -> List[
model_dict["accno"] = section.__dict__.get("accno", "")
model_dict["accession_id"] = submission.accno
model_dict["uuid"] = generate_specimen_imaging_preparation_uuid(model_dict)
model_dict["version"] = 1
model_dict = filter_model_dictionary(model_dict, bia_data_model.SpecimenImagingPrepartionProtocol)

model_dicts.append(model_dict)
Expand Down
1 change: 1 addition & 0 deletions bia-ingest-shared-models/bia_ingest_sm/conversion/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def get_study(
"author": [c.model_dump() for c in contributors],
"grant": [g.model_dump() for g in grants],
"attribute": study_attributes,
"version": 1
}
# study_uuid = dict_to_uuid(study_dict, ["accession_id",])
# study_dict["uuid"] = study_uuid
Expand Down
2 changes: 1 addition & 1 deletion bia-ingest-shared-models/bia_ingest_sm/conversion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,5 @@ def persist(object_list: List, object_path: str, sumbission_accno: str):

def filter_model_dictionary(dictionary: dict, target_model: Type[BaseModel]):
accepted_fields = target_model.model_fields.keys()
result_dict = {key: dictionary[key] for key in accepted_fields}
result_dict = {key: dictionary[key] for key in accepted_fields if key in dictionary}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix to cope with optional/missing fields in the original dictionary

return result_dict
15 changes: 15 additions & 0 deletions bia-ingest-shared-models/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def get_test_annotation_method() -> List[bia_data_model.AnnotationMethod]:
"annotation_coverage": "",
"method_type": "other",
"source_dataset": [],
"version": 1,
},
]

Expand Down Expand Up @@ -62,12 +63,14 @@ def get_test_specimen_growth_protocol() -> List[bia_data_model.SpecimenGrowthPro
"accession_id": "S-BIADTEST",
"title_id": "Test specimen 1",
"protocol_description": "Test growth protocol 1",
"version": 1,
},
{
"accno": "Specimen-2",
"accession_id": "S-BIADTEST",
"title_id": "Test specimen 2",
"protocol_description": "Test growth protocol 2",
"version": 1,
},
]

Expand Down Expand Up @@ -100,13 +103,15 @@ def get_test_specimen_imaging_preparation_protocol() -> (
"title_id": "Test specimen 1",
"protocol_description": "Test sample preparation protocol 1",
"signal_channel_information": [],
"version": 1,
},
{
"accno": "Specimen-2",
"accession_id": "S-BIADTEST",
"title_id": "Test specimen 2",
"protocol_description": "Test sample preparation protocol 2",
"signal_channel_information": [],
"version": 1,
},
]

Expand Down Expand Up @@ -168,6 +173,7 @@ def get_test_biosample() -> List[bia_data_model.BioSample]:
"intrinsic_variable_description": [
"Test intrinsic variable 1\nwith escaped character",
],
"version": 1,
},
{
"accno": "Biosample-2",
Expand All @@ -186,6 +192,7 @@ def get_test_biosample() -> List[bia_data_model.BioSample]:
"intrinsic_variable_description": [
"Test intrinsic variable 2",
],
"version": 1,
},
]

Expand Down Expand Up @@ -218,6 +225,7 @@ def get_test_image_acquisition() -> List[bia_data_model.ImageAcquisition]:
"imaging_instrument_description": "Test imaging instrument 1",
"imaging_method_name": "confocal microscopy",
"fbbi_id": [],
"version": 1,
},
{
"accno": "Image acquisition-7",
Expand All @@ -227,6 +235,7 @@ def get_test_image_acquisition() -> List[bia_data_model.ImageAcquisition]:
"imaging_instrument_description": "Test imaging instrument 2",
"imaging_method_name": "fluorescence microscopy",
"fbbi_id": [],
"version": 1,
},
]
image_acquisition = []
Expand Down Expand Up @@ -286,6 +295,7 @@ def get_test_experimental_imaging_dataset() -> (
],
"example_image_uri": [],
"description": "Description of study component 1",
"version": 1,
"attribute": {
"associations": [
{
Expand Down Expand Up @@ -328,16 +338,19 @@ def get_test_experimental_imaging_dataset() -> (
"accession_id": "S-BIADTEST",
"file_path": "study_component2/im06.png",
"size_in_bytes": 3,
"version": 1,
},
{
"accession_id": "S-BIADTEST",
"file_path": "study_component2/im08.png",
"size_in_bytes": 123,
"version": 1,
},
{
"accession_id": "S-BIADTEST",
"file_path": "study_component2/ann01-05",
"size_in_bytes": 11,
"version": 1,
},
]
experimental_imaging_dataset_dict = {
Expand All @@ -351,6 +364,7 @@ def get_test_experimental_imaging_dataset() -> (
],
"example_image_uri": [],
"description": "Description of study component 2",
"version": 1,
"attribute": {
"associations": [
{
Expand Down Expand Up @@ -540,6 +554,7 @@ def get_test_study() -> bia_data_model.Study:
"Test keyword3",
],
"grant": [g.model_dump() for g in grant],
"version": 1,
}
study_uuid = dict_to_uuid(
study_dict,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class DocumentMixin(BaseModel):
Optional because for some usecases (e.g. api) we want to accept objects without it because we have the info we need to set it."""
)

# Throw error if you try to validate/create model from a dictionary with keys that aren't a field in the model
model_config = ConfigDict(extra="forbid")

def __init__(self, *args, **data):
model_version_spec = self.model_config.get("model_version_latest")
if model_version_spec is None:
Expand All @@ -41,6 +38,7 @@ def __init__(self, *args, **data):
)
model_metadata_existing = data.get("model", None)
if model_metadata_existing:
model_metadata_existing = ModelMetadata(**model_metadata_existing)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix to the error checking - as it was currently trying to compare a dictionary with a model. This was in the original API models but was missed when copied over to here.

if model_metadata_existing != model_metadata_expected:
raise exceptions.UnexpectedDocumentType(
f"Document {str(data.get('uuid'))} has model metadata {model_metadata_existing}, expected : {model_metadata_expected}"
Expand Down
Loading
Loading