Skip to content

Commit

Permalink
Checking the age of subject for anomalies (openMetadataInitiative#33)
Browse files Browse the repository at this point in the history
* creating a function for age

* create_openminds_age

* checking for nan

* test for create_openminds_age
  • Loading branch information
Peyman-N authored May 17, 2024
1 parent 458ea7e commit a6f82b5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
34 changes: 26 additions & 8 deletions bids2openminds/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -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(
Expand Down
22 changes: 22 additions & 0 deletions test/test_subject_age.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a6f82b5

Please sign in to comment.