Skip to content

Commit 7edd2c2

Browse files
lzehlPeyman-N
andauthored
switch mapping to openMINDS Python classes (#9)
* switch mapping to openMINDS Python classes * Update mapping.py * Update mapping.py * Update mapping.py * Update mapping.py * Update mapping.py * Update mapping.py * Update mapping.py set all undefined/non discussed to None * Update mapping.py * delete obsolete openminds instance func * using by_name for instances * correction for new detected instanses * deleting import numpy * Update mapping.py --------- Co-authored-by: Peyman-N <najafe.peyman@gmail.Com>
1 parent c79b2a3 commit 7edd2c2

4 files changed

Lines changed: 253 additions & 201 deletions

File tree

bids2openminds/main.py

Lines changed: 98 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from openminds import IRI
1212

1313
from .utility import table_filter, pd_table_value, file_hash, file_storage_size, detect_nifti_version
14-
from .mapping import bids2openminds_instance
14+
from . import mapping
1515

1616

1717
def create_openminds_person(full_name):
@@ -94,32 +94,66 @@ def create_behavioral_protocol(layout, collection):
9494
return behavioral_protocols, behavioral_protocols_dict
9595

9696

97+
def techniques_openminds(suffix):
98+
possible_types = ["Technique", "AnalysisTechnique", "StimulationApproach",
99+
"StimulationTechnique"]
100+
101+
if suffix in mapping.MAP_2_TECHNIQUES:
102+
items_openminds = mapping.MAP_2_TECHNIQUES[suffix]
103+
else:
104+
return []
105+
106+
if items_openminds is None:
107+
return []
108+
109+
openminds_techniques_list = []
110+
for item_openminds in items_openminds:
111+
for possible_type in possible_types:
112+
openminds_type = getattr(controlled_terms, possible_type)
113+
try:
114+
openminds_obj = openminds_type.by_name(item_openminds)
115+
break
116+
except KeyError:
117+
pass
118+
openminds_techniques_list.append(openminds_obj)
119+
return openminds_techniques_list
120+
121+
97122
def create_techniques(layout_df):
98123
suffixs = layout_df["suffix"].unique().tolist()
99124
techniques = []
100125
not_techniques_index = ["description", "participants", "events"]
101126
for suffix in suffixs:
102127
# excluding the None and non thechnique indexes
103-
if not (pd.isna(suffix) or (suffix in not_techniques_index)):
104-
openminds_techniques_cache = bids2openminds_instance(
105-
suffix, "MAP_2_TECHNIQUES")
106-
# Excluding the suffixs that are not in the library or flagged as non technique suffixes
107-
if not pd.isna(openminds_techniques_cache):
108-
techniques.extend(openminds_techniques_cache)
109-
else:
110-
warn(
111-
f"The {suffix} suffix is currently considered an auxiliary file for already existing techniques or a non technique file.")
128+
if not (pd.isnull(suffix) or (suffix in not_techniques_index)):
129+
openminds_techniques_cache = techniques_openminds(suffix)
130+
techniques.extend(openminds_techniques_cache)
112131

113-
return techniques or None
132+
techniques_set = set(techniques)
133+
techniques_list = list(techniques_set)
134+
return techniques_list or None
135+
136+
137+
def approaches_openminds(datatype):
138+
139+
if datatype in mapping.MAP_2_EXPERIMENTAL_APPROACHES:
140+
items_openminds = mapping.MAP_2_EXPERIMENTAL_APPROACHES[datatype]
141+
142+
approches_list = []
143+
144+
for item in items_openminds:
145+
approches_list.append(
146+
controlled_terms.ExperimentalApproach.by_name(item))
147+
148+
return approches_list
114149

115150

116151
def create_approaches(layout_df):
117152
datatypes = layout_df["datatype"].unique().tolist()
118153
approaches = set([])
119154
for datatype in datatypes:
120-
if not (pd.isna(datatype)):
121-
approaches.update(bids2openminds_instance(
122-
datatype, "MAP_2_EXPERIMENTAL_APPROACHES"))
155+
if not (pd.isnull(datatype)):
156+
approaches.update(approaches_openminds(datatype))
123157

124158
return list(approaches) or None
125159

@@ -233,6 +267,52 @@ def create_dataset(dataset_description, dataset_version, collection):
233267
return dataset
234268

235269

270+
def spices_openminds(data_subject: pd.DataFrame):
271+
bids_species = pd_table_value(data_subject, "species")
272+
if bids_species is None:
273+
# In BIDS the default species is homo sapiens.
274+
return controlled_terms.Species.homo_sapiens
275+
if bids_species in mapping.MAP_2_SPECIES:
276+
openminds_species = mapping.MAP_2_SPECIES[bids_species]
277+
return controlled_terms.Species.by_name(openminds_species[0])
278+
else:
279+
try:
280+
openminds_species = controlled_terms.Species.by_name(bids_species)
281+
warn(
282+
f"You have specified {bids_species} as species, we have autodetected {openminds_species.name}, please verify it.")
283+
return openminds_species
284+
except KeyError:
285+
warn(
286+
f"You have specified {bids_species} we currently don't support this species.")
287+
return None
288+
289+
290+
def handedness_openminds(data_subject: pd.DataFrame):
291+
bids_handedness = pd_table_value(data_subject, "handedness")
292+
if bids_handedness is None:
293+
return None
294+
if bids_handedness in mapping.MAP_2_HANDEDNESS:
295+
openminds_handedness = mapping.MAP_2_HANDEDNESS[bids_handedness]
296+
return controlled_terms.Handedness.by_name(openminds_handedness[0])
297+
else:
298+
warn(
299+
f"You have specified {bids_handedness} which is not a allowed value for handedness defined by BIDS standard.")
300+
return None
301+
302+
303+
def sex_openminds(data_subject: pd.DataFrame):
304+
bids_sex = pd_table_value(data_subject, "sex")
305+
if bids_sex is None:
306+
return None
307+
if bids_sex in mapping.MAP_2_BIOLOGICALSEX:
308+
bids_sex = mapping.MAP_2_BIOLOGICALSEX[bids_sex]
309+
return controlled_terms.BiologicalSex.by_name(bids_sex[0])
310+
else:
311+
warn(
312+
f"You have specified {bids_sex} which is not a allowed value for handedness defined by BIDS standard.")
313+
return None
314+
315+
236316
def create_subjects(subject_id, layout_df, layout, collection):
237317

238318
sessions = layout.get_sessions()
@@ -286,8 +366,6 @@ def create_subjects(subject_id, layout_df, layout, collection):
286366
# Select the tsv file of the table
287367
participants_path_tsv = pd_table_value(table_filter(
288368
participants_paths, ".tsv", "extension"), "path")
289-
participants_path_json = pd_table_value(table_filter(
290-
participants_paths, ".json", "extension"), "path")
291369

292370
participants_table = pd.read_csv(participants_path_tsv, sep="\t", header=0)
293371
for subject in subject_id:
@@ -299,8 +377,7 @@ def create_subjects(subject_id, layout_df, layout, collection):
299377
if not sessions:
300378
state = omcore.SubjectState(
301379
age=create_openminds_age(data_subject),
302-
handedness=bids2openminds_instance(pd_table_value(
303-
data_subject, "handedness"), "MAP_2_HANDEDNESS", is_list=False),
380+
handedness=handedness_openminds(data_subject),
304381
internal_identifier=f"Studied state {subject_name}".strip(),
305382
lookup_label=f"Studied state {subject_name}".strip()
306383
)
@@ -312,8 +389,7 @@ def create_subjects(subject_id, layout_df, layout, collection):
312389
if not (table_filter(table_filter(layout_df, session, "session"), subject, "subject").empty):
313390
state = omcore.SubjectState(
314391
age=create_openminds_age(data_subject),
315-
handedness=bids2openminds_instance(pd_table_value(
316-
data_subject, "handedness"), "MAP_2_HANDEDNESS", is_list=False),
392+
handedness=handedness_openminds(data_subject),
317393
internal_identifier=f"Studied state {subject_name} {session}".strip(
318394
),
319395
lookup_label=f"Studied state {subject_name} {session}".strip(
@@ -324,13 +400,11 @@ def create_subjects(subject_id, layout_df, layout, collection):
324400
state_cache.append(state)
325401
subject_state_dict[f"{subject}"] = state_cache_dict
326402
subject_cache = omcore.Subject(
327-
biological_sex=bids2openminds_instance(pd_table_value(
328-
data_subject, "sex"), "MAP_2_SEX", is_list=False),
403+
biological_sex=sex_openminds(data_subject),
329404
lookup_label=f"{subject_name}",
330405
internal_identifier=f"{subject_name}",
331406
# TODO species should default to homo sapiens
332-
species=bids2openminds_instance(pd_table_value(
333-
data_subject, "species"), "MAP_2_SPECIES", is_list=False),
407+
species=spices_openminds(data_subject),
334408
studied_states=state_cache
335409
)
336410
subjects_dict[f"{subject}"] = subject_cache

0 commit comments

Comments
 (0)