From 38936c9b8297506d42502f073d7ad1de06420061 Mon Sep 17 00:00:00 2001 From: SimonKonar Date: Mon, 4 Nov 2024 13:44:52 +0100 Subject: [PATCH 1/2] fix: check if storage temperature is not None when updating collection characteristics --- blaze_client/blaze_client.py | 38 ++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/blaze_client/blaze_client.py b/blaze_client/blaze_client.py index 2ec28ce..12b6cfe 100644 --- a/blaze_client/blaze_client.py +++ b/blaze_client/blaze_client.py @@ -1,3 +1,5 @@ +from typing import Generator, Any + import requests from fhirclient.models.bundle import Bundle, BundleEntry, BundleEntryRequest from requests import Response @@ -146,6 +148,14 @@ def get_diagnosis_report_fhir_ids_belonging_to_patient(self, donor_fhir_id: str) diagnosis_report_fhir_ids.append(diagnosis_report_fhir_id) return diagnosis_report_fhir_ids + def get_condition_by_patient_fhir_id(self, patient_fhir_id: str): + response = self._session.get(f"{self._blaze_url}/Condition?subject={patient_fhir_id}") + self.__raise_for_status_extract_diagnostics_message(response) + response_json = response.json() + if response_json["total"] == 0: + return None + return get_nested_value(response_json, ["entry", 0, "resource", "id"]) + def update_fhir_resource(self, resource_type: str, resource_fhir_id: str, resource_json: dict) -> bool: """Update a FHIR resource in blaze. :param resource_type: the type of the resource @@ -596,12 +606,15 @@ def add_already_present_samples_to_existing_collection(self, sample_fhir_ids: li :raises NonExistentResourceException: if the resource cannot be found :return: Bool indicating outcome of this operation""" collection = self.build_collection_from_json(collection_fhir_id) - samples = [self.build_sample_from_json(sample_fhir_id) for sample_fhir_id in sample_fhir_ids] - collection = self.__update_collection_characteristics_from_samples(samples, collection) + sample_generator_for_characteristics = (self.build_sample_from_json(sample_fhir_id) for sample_fhir_id in + sample_fhir_ids) + + collection = self.__update_collection_characteristics_from_samples(sample_generator_for_characteristics, + collection) already_present_samples_set = set(collection.sample_fhir_ids) - for sample in samples: - if sample.sample_fhir_id not in already_present_samples_set: - already_present_samples_set.add(sample.sample_fhir_id) + for sample_fhir_id in sample_fhir_ids: + if sample_fhir_id not in already_present_samples_set: + already_present_samples_set.add(sample_fhir_id) collection._sample_fhir_ids = list(already_present_samples_set) collection = collection.add_fhir_id_to_collection(collection.to_fhir()) return self.update_fhir_resource("Group", collection_fhir_id, collection.as_json()) @@ -615,7 +628,7 @@ def update_collection_values(self, collection_fhir_id) -> bool: :return: Bool indicating if the collection was updated or not""" collection = self.build_collection_from_json(collection_fhir_id) sample_fhir_ids = collection.sample_fhir_ids - present_samples = [self.build_sample_from_json(sample_fhir_id) for sample_fhir_id in collection.sample_fhir_ids] + present_samples = (self.build_sample_from_json(sample_fhir_id) for sample_fhir_id in collection.sample_fhir_ids) collection._sample_fhir_ids = [] collection.age_range_low = 0 collection.age_range_high = 0 @@ -629,7 +642,7 @@ def update_collection_values(self, collection_fhir_id) -> bool: collection_fhir = collection.add_fhir_id_to_collection(collection.to_fhir()) return self.update_fhir_resource("Group", collection.collection_fhir_id, collection_fhir.as_json()) - def __update_collection_characteristics_from_samples(self, samples: list[Sample], + def __update_collection_characteristics_from_samples(self, samples: Generator[Sample, Any, None], collection: Collection) -> Collection: """update the characteristics for collection with new values from the samples. :param samples: the samples to calculate the characteristics from @@ -638,8 +651,8 @@ def __update_collection_characteristics_from_samples(self, samples: list[Sample] :raises HTTPError: if the request to blaze fails :raises NonExistentResourceException: if the resource cannot be found """ - already_present_samples = [self.build_sample_from_json(sample_fhir_id) for sample_fhir_id in - collection.sample_fhir_ids] + already_present_samples = (self.build_sample_from_json(sample_fhir_id) for sample_fhir_id in + collection.sample_fhir_ids) donor_fhir_ids = set([sample.subject_fhir_id for sample in already_present_samples]) count_of_new_subjects = 0 @@ -652,13 +665,14 @@ def __update_collection_characteristics_from_samples(self, samples: list[Sample] sample_material_type = get_material_type_from_detailed_material_type(sample.material_type) if donor.gender not in collection.genders: collection.genders.append(donor.gender) - if sample.storage_temperature not in collection.storage_temperatures: + if sample.storage_temperature is not None and \ + sample.storage_temperature not in collection.storage_temperatures: collection.storage_temperatures.append(sample.storage_temperature) - if sample_material_type not in collection.material_types: + if sample_material_type is not None and sample_material_type not in collection.material_types: collection.material_types.append(sample_material_type) sample_diagnoses = self.__get_diagnoses_of_sample(sample_fhir_id) for diagnosis in sample_diagnoses: - if diagnosis not in collection.diagnoses: + if diagnosis is not None and diagnosis not in collection.diagnoses: collection.diagnoses.append(diagnosis) ages_at_diagnosis = self.__get_age_at_the_time_of_diagnosis(sample_fhir_id, donor.donor_fhir_id) for age in ages_at_diagnosis: From 032e57e823ef48a3e9e91d1c02787100eb482237 Mon Sep 17 00:00:00 2001 From: SimonKonar Date: Mon, 4 Nov 2024 13:45:33 +0100 Subject: [PATCH 2/2] refactor: delete comments --- test/service/test_blaze_client.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/service/test_blaze_client.py b/test/service/test_blaze_client.py index d1da61e..d43d52a 100644 --- a/test/service/test_blaze_client.py +++ b/test/service/test_blaze_client.py @@ -673,9 +673,6 @@ def test_add_existing_sample_to_collection(self): self.blaze_service.upload_collection_organization(self.example_collection_org) collection_fhir_id = self.blaze_service.upload_collection(self.example_collection) self.blaze_service.upload_network(self.example_network) - # sample_fhir_id = self.blaze_service.get_fhir_id("Specimen", "sampleId") - # sample_json = self.blaze_service.get_fhir_resource_as_json("Specimen", sample_fhir_id) - # sample = Sample.from_json(sample_json, "donorId") updated = self.blaze_service.add_already_present_samples_to_existing_collection( [sample_fhir_id1, sample_fhir_id2], collection_fhir_id) self.assertTrue(updated)