diff --git a/bids2openminds/main.py b/bids2openminds/main.py index f553293..5d03b60 100644 --- a/bids2openminds/main.py +++ b/bids2openminds/main.py @@ -102,6 +102,30 @@ def create_approaches(layout_df): return list(approaches) or None +def create_openminds_age(data_subject): + + try: + age = pd_table_value(data_subject, "age") + except: + return None + + if age is None or pd.isna(age): + return None + elif isinstance(age, float) or isinstance(age, int) or age.isnumeric(): + return omcore.QuantitativeValue( + value=age, + unit=controlled_terms.UnitOfMeasurement.year + ) + elif age == "89+": + return omcore.QuantitativeValueRange( + max_value=None, + min_value=89, + min_value_unit=controlled_terms.UnitOfMeasurement.year + ) + else: + return None + + def create_dataset_version(bids_layout, dataset_description, layout_df, studied_specimens, file_repository, collection): # Fetch the dataset type from dataset description file @@ -246,10 +270,7 @@ def create_subjects(subject_id, layout_df, layout, collection): state_cache = [] if not sessions: state = omcore.SubjectState( - age=omcore.QuantitativeValue( - value=pd_table_value(data_subject, "age"), - unit=controlled_terms.UnitOfMeasurement.year - ), + age=create_openminds_age(data_subject), handedness=bids2openminds_instance(pd_table_value( data_subject, "handedness"), "MAP_2_HANDEDNESS", is_list=False), internal_identifier=f"Studied state {subject_name}".strip(), @@ -262,10 +283,7 @@ def create_subjects(subject_id, layout_df, layout, collection): for session in sessions: if not (table_filter(table_filter(layout_df, session, "session"), subject, "subject").empty): state = omcore.SubjectState( - age=omcore.QuantitativeValue( - value=pd_table_value(data_subject, "age"), - unit=controlled_terms.UnitOfMeasurement.year - ), + age=create_openminds_age(data_subject), handedness=bids2openminds_instance(pd_table_value( data_subject, "handedness"), "MAP_2_HANDEDNESS", is_list=False), internal_identifier=f"Studied state {subject_name} {session}".strip( diff --git a/test/test_subject_age.py b/test/test_subject_age.py new file mode 100644 index 0000000..1a6c772 --- /dev/null +++ b/test/test_subject_age.py @@ -0,0 +1,22 @@ +import pandas as pd +import pytest +from bids2openminds.main import create_openminds_age + +example_ages = [("89+", "QuantitativeValueRange"), + (45, "QuantitativeValue"), ("XX", None)] + + +@ pytest.mark.parametrize("age,type", example_ages) +def test_subject_age(age, type): + data_subject_table = pd.DataFrame(data={'age': [age]}) + openminds_age = create_openminds_age(data_subject_table) + match type: + case "QuantitativeValueRange": + assert openminds_age.type_ == 'https://openminds.ebrains.eu/core/QuantitativeValueRange' + assert openminds_age.max_value is None + assert openminds_age.min_value == 89 + case "QuantitativeValue": + assert openminds_age.type_ == 'https://openminds.ebrains.eu/core/QuantitativeValue' + assert openminds_age.value == age + case None: + assert openminds_age is None