Skip to content

Commit

Permalink
Merge pull request #162 from BIH-CEI/120-rename-phenopacketelement-to…
Browse files Browse the repository at this point in the history
…-phenopacketbuildingblock

120 rename phenopacketelement to phenopacketbuildingblock
  • Loading branch information
frehburg authored Oct 8, 2024
2 parents 92f79c1 + 3946f9b commit 5263a6b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
16 changes: 8 additions & 8 deletions notebooks/erdri_cds_definition_in_code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@
"import phenopackets\n",
"\n",
"from phenopacket_mapper import PhenopacketMapper\n",
"from phenopacket_mapper.mapping import PhenopacketElement"
"from phenopacket_mapper.mapping import PhenopacketBuildingBlock"
],
"metadata": {
"collapsed": false,
Expand All @@ -1437,35 +1437,35 @@
"mapper = PhenopacketMapper(\n",
" data_model=erdri_cds_data_model,\n",
" id=erdri_cds_data_model.pseudonym,\n",
" subject=PhenopacketElement(\n",
" subject=PhenopacketBuildingBlock(\n",
" phenopacket_element=phenopackets.Individual,\n",
" id=erdri_cds_data_model.pseudonym,\n",
" date_of_birth=erdri_cds_data_model.date_of_birth,\n",
" vital_status=PhenopacketElement(\n",
" vital_status=PhenopacketBuildingBlock(\n",
" phenopacket_element=phenopackets.VitalStatus,\n",
" status=erdri_cds_data_model.patient_s_status,\n",
" time_of_death=PhenopacketElement(\n",
" time_of_death=PhenopacketBuildingBlock(\n",
" phenopacket_element=phenopackets.TimeElement,\n",
" timestamp=erdri_cds_data_model.date_of_death\n",
" )\n",
" ),\n",
" sex=erdri_cds_data_model.sex,\n",
" ),\n",
" diseases=[\n",
" PhenopacketElement(\n",
" PhenopacketBuildingBlock(\n",
" phenopacket_element=phenopackets.Disease,\n",
" term=erdri_cds_data_model.diagnosis_of_the_rare_disease,\n",
" # TODO: preprocess to remove the strings\n",
" # onset=PhenopacketElement(\n",
" # onset=PhenopacketBuildingBlock(\n",
" # phenopacket_element=phenopackets.TimeElement,\n",
" # timestamp=erdri_cds_data_model.age_at_diagnosis\n",
" # )\n",
" )\n",
" ],\n",
" interpretations=[\n",
" PhenopacketElement(\n",
" PhenopacketBuildingBlock(\n",
" phenopacket_element=phenopackets.Interpretation,\n",
" diagnosis=PhenopacketElement(\n",
" diagnosis=PhenopacketBuildingBlock(\n",
" phenopacket_element=phenopackets.Diagnosis,\n",
" disease=erdri_cds_data_model.diagnosis_of_the_rare_disease,\n",
" )\n",
Expand Down
4 changes: 2 additions & 2 deletions src/phenopacket_mapper/mapping/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""This module facilitates the mapping from a local data model to the phenopacket schema"""

from .phenopacket_element import PhenopacketElement, map_single
from .phenopacket_building_block import PhenopacketBuildingBlock, map_single
from .mapper import PhenopacketMapper

__all__ = [
'map_single',
'PhenopacketElement',
'PhenopacketBuildingBlock',
'PhenopacketMapper',

]
8 changes: 4 additions & 4 deletions src/phenopacket_mapper/mapping/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from phenopackets import Phenopacket

from phenopacket_mapper.data_standards.data_model import DataModel, DataSet, DataField
from phenopacket_mapper.mapping import PhenopacketElement, map_single
from phenopacket_mapper.mapping import PhenopacketBuildingBlock, map_single


class PhenopacketMapper:
Expand All @@ -17,7 +17,7 @@ def __init__(self, data_model: DataModel, **kwargs):
:param kwargs: The elements to map the data to Phenopackets
"""
self.data_model = data_model
self.elements: Dict[str, Union[PhenopacketElement, DataField]] = {}
self.elements: Dict[str, Union[PhenopacketBuildingBlock, DataField]] = {}
self.resources = data_model.resources
for k, v in kwargs.items():
setattr(self, k, v)
Expand All @@ -30,14 +30,14 @@ def __post_init__(self):
for e in self.elements.values():
self.check_data_fields_in_model(e)

def check_data_fields_in_model(self, element: Union[PhenopacketElement, DataField]):
def check_data_fields_in_model(self, element: Union[PhenopacketBuildingBlock, DataField]):
if isinstance(element, DataField):
field = element
if field not in self.data_model:
raise AttributeError(f"The mapping definition contains an invalid field. "
f"{field} is not in the data model underlying the passed data set."
f" (The data model includes the fields: {self.data_model.get_field_ids()})")
elif isinstance(element, PhenopacketElement):
elif isinstance(element, PhenopacketBuildingBlock):
for key, ee in element.elements.items():
self.check_data_fields_in_model(ee)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from phenopacket_mapper.data_standards import DataModelInstance, DataField, DataFieldValue, Coding


class PhenopacketElement:
class PhenopacketBuildingBlock:

def __init__(self, phenopacket_element, **kwargs):
"""Mapping equivalent to the constructor of a Phenopacket element (e.g., Individual) for the mapping
Expand All @@ -17,7 +17,7 @@ def __init__(self, phenopacket_element, **kwargs):
:param kwargs: The elements to map the data to Phenopackets
"""
self.phenopacket_element = phenopacket_element
self.elements: Dict[str, Union[PhenopacketElement, DataField]] = {}
self.elements: Dict[str, Union[PhenopacketBuildingBlock, DataField]] = {}
for k, v in kwargs.items():
setattr(self, k, v)
self.elements[k] = v
Expand All @@ -30,7 +30,7 @@ def map(self, instance: DataModelInstance):
>>> data_field = DataField("pseudonym", str)
>>> data_model = DataModel("Example data model", [data_field], [])
>>> inst = DataModelInstance(0, data_model, [DataFieldValue(0, data_field, "example_pseudonym")])
>>> individual = PhenopacketElement(phenopackets.Individual, id=data_field).map(inst)
>>> individual = PhenopacketBuildingBlock(phenopackets.Individual, id=data_field).map(inst)
>>> individual.id
'example_pseudonym'
Expand Down Expand Up @@ -64,6 +64,6 @@ def map_single(key, e, instance, kwargs):
pass
elif isinstance(e, list):
kwargs[key] = [v.map(instance) for v in e]
elif isinstance(e, PhenopacketElement):
elif isinstance(e, PhenopacketBuildingBlock):
phenopacket_element = e
kwargs[key] = phenopacket_element.map(instance)

0 comments on commit 5263a6b

Please sign in to comment.