Skip to content

Commit e5fc96b

Browse files
authored
Merge pull request #419 from djarecka/ref_bids2nidm
bids2nidm refactoring
2 parents 26026de + ae213d8 commit e5fc96b

File tree

2 files changed

+59
-170
lines changed

2 files changed

+59
-170
lines changed

src/nidm/experiment/Utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,9 +3071,9 @@ def addGitAnnexSources(obj, bids_root, filepath=None):
30713071
obj.add_attributes({Constants.PROV["Location"]: URIRef(source)})
30723072

30733073
return len(sources)
3074-
except Exception:
3075-
# if "No annex found at" not in str(e):
3076-
# print("Warning, error with AnnexRepo (Utils.py, addGitAnnexSources):", e)
3074+
except Exception as e:
3075+
if "No annex found at" not in str(e):
3076+
print("Warning, error with AnnexRepo (Utils.py, addGitAnnexSources):", e)
30773077
return 0
30783078

30793079

src/nidm/experiment/tools/bidsmri2nidm.py

Lines changed: 56 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -167,36 +167,20 @@ def main():
167167

168168
# if args.outputfile was defined by user then use it else use default which is args.directory/nidm.ttl
169169
if args.outputfile == "nidm.ttl":
170-
# if we're choosing json-ld, make sure file extension is .json
171-
# if args.jsonld:
172-
# outputfile=os.path.join(directory,os.path.splitext(args.outputfile)[0]+".json")
173-
# if flag set to add to .bidsignore then add
174-
# if (args.bidsignore):
175-
# addbidsignore(directory,os.path.splitext(args.outputfile)[0]+".json")
176-
177170
outputfile = os.path.join(directory, args.outputfile)
178-
if args.bidsignore:
179-
addbidsignore(directory, args.outputfile)
180-
rdf_graph.serialize(destination=outputfile, format="turtle")
181-
182-
# else:
183-
# outputfile=os.path.join(directory,args.outputfile)
184-
# if (args.bidsignore):
185-
# addbidsignore(directory,args.outputfile)
186171
else:
187-
# if we're choosing json-ld, make sure file extension is .json
188-
# if args.jsonld:
189-
# outputfile = os.path.splitext(args.outputfile)[0]+".json"
190-
# if (args.bidsignore):
191-
# addbidsignore(directory,os.path.splitext(args.outputfile)[0]+".json")
192-
# else:
193-
# outputfile = args.outputfile
194-
# if (args.bidsignore):
195-
# addbidsignore(directory,args.outputfile)
196172
outputfile = args.outputfile
197-
if args.bidsignore:
198-
addbidsignore(directory, args.outputfile)
199-
rdf_graph.serialize(destination=outputfile, format="turtle")
173+
174+
# if we're choosing json-ld, make sure file extension is .json
175+
# if args.jsonld:
176+
# outputfile=os.path.join(directory,os.path.splitext(args.outputfile)[0]+".json")
177+
# if flag set to add to .bidsignore then add
178+
# if (args.bidsignore):
179+
# addbidsignore(directory,os.path.splitext(args.outputfile)[0]+".json")
180+
181+
if args.bidsignore:
182+
addbidsignore(directory, args.outputfile)
183+
rdf_graph.serialize(destination=outputfile, format="turtle")
200184

201185
# serialize NIDM file
202186
# with open(outputfile,'w', encoding="utf-8") as f:
@@ -1072,82 +1056,30 @@ def bidsmri2project(directory, args):
10721056
# defaults to participants.json because here we're mapping the participants.tsv file variables to terms
10731057
# if participants.json file doesn't exist then run without json mapping file
10741058
if not os.path.isfile(os.path.join(directory, "participants.json")):
1075-
# temporary data frame of variables we need to create data dictionaries for
1076-
temp = DataFrame(columns=mapping_list)
1077-
# create data dictionary without concept mapping
1078-
if args.no_concepts:
1079-
column_to_terms, cde = map_variables_to_terms(
1080-
directory=directory,
1081-
assessment_name="participants.tsv",
1082-
df=temp,
1083-
output_file=os.path.join(directory, "participants.json"),
1084-
bids=True,
1085-
associate_concepts=False,
1086-
dataset_identifier=dataset_doi,
1087-
)
1088-
# create data dictionary with concept mapping
1089-
else:
1090-
column_to_terms, cde = map_variables_to_terms(
1091-
directory=directory,
1092-
assessment_name="participants.tsv",
1093-
df=temp,
1094-
output_file=os.path.join(directory, "participants.json"),
1095-
bids=True,
1096-
dataset_identifier=dataset_doi,
1097-
)
1059+
json_source = None
10981060
else:
1099-
# temporary data frame of variables we need to create data dictionaries for
1100-
temp = DataFrame(columns=mapping_list)
1101-
# create data dictionary without concept mapping
1102-
if args.no_concepts:
1103-
column_to_terms, cde = map_variables_to_terms(
1104-
directory=directory,
1105-
assessment_name="participants.tsv",
1106-
df=temp,
1107-
output_file=os.path.join(directory, "participants.json"),
1108-
json_source=os.path.join(directory, "participants.json"),
1109-
bids=True,
1110-
associate_concepts=False,
1111-
dataset_identifier=dataset_doi,
1112-
)
1113-
# create data dictionary with concept mapping
1114-
else:
1115-
column_to_terms, cde = map_variables_to_terms(
1116-
directory=directory,
1117-
assessment_name="participants.tsv",
1118-
df=temp,
1119-
output_file=os.path.join(directory, "participants.json"),
1120-
json_source=os.path.join(directory, "participants.json"),
1121-
bids=True,
1122-
dataset_identifier=dataset_doi,
1123-
)
1124-
# if user supplied a JSON data dictionary then use it
1125-
else:
1126-
# temporary data frame of variables we need to create data dictionaries for
1127-
temp = DataFrame(columns=mapping_list)
1128-
# create data dictionary without concept mapping
1129-
if args.no_concepts:
1130-
column_to_terms, cde = map_variables_to_terms(
1131-
directory=directory,
1132-
assessment_name="participants.tsv",
1133-
df=temp,
1134-
output_file=os.path.join(directory, "participants.json"),
1135-
json_source=args.json_map,
1136-
bids=True,
1137-
associate_concepts=False,
1138-
dataset_identifier=dataset_doi,
1139-
)
1140-
# create data dictionary with concept mapping
1141-
else:
1142-
column_to_terms, cde = map_variables_to_terms(
1143-
directory=directory,
1144-
assessment_name="participants.tsv",
1145-
df=temp,
1146-
output_file=os.path.join(directory, "participants.json"),
1147-
json_source=args.json_map,
1148-
bids=True,
1149-
dataset_identifier=dataset_doi,
1150-
)
1061+
json_source = os.path.join(directory, "participants.json")
1062+
else: # if user supplied a JSON data dictionary then use it
1063+
json_source = args.json_map
1064+
# create data dictionary without concept mapping
1065+
if args.no_concepts:
1066+
associate_concepts = False
1067+
else: # create data dictionary with concept mapping
1068+
associate_concepts = True
1069+
1070+
# temporary data frame of variables we need to create data dictionaries for
1071+
temp = DataFrame(columns=mapping_list)
1072+
1073+
column_to_terms, cde = map_variables_to_terms(
1074+
directory=directory,
1075+
assessment_name="participants.tsv",
1076+
df=temp,
1077+
output_file=os.path.join(directory, "participants.json"),
1078+
json_source=json_source,
1079+
bids=True,
1080+
associate_concepts=associate_concepts,
1081+
dataset_identifier=dataset_doi,
1082+
)
11511083

11521084
# iterate over rows in participants.tsv file and create NIDM objects for sessions and acquisitions
11531085
for row in participants_data:
@@ -1386,76 +1318,33 @@ def bidsmri2project(directory, args):
13861318
# add column to list for column_to_terms mapping
13871319
mapping_list.append(field)
13881320

1389-
# if user didn't supply a json data dictionary file
1390-
# create an empty one for column_to_terms to use
1321+
# if user didn't supply a json data dictionary file but we're doing some variable-term mapping create an empty one
1322+
# for column_to_terms to use
13911323
if args.json_map is False:
13921324
# defaults to participants.json because here we're mapping the participants.tsv file variables to terms
13931325
# if participants.json file doesn't exist then run without json mapping file
13941326
if not os.path.isfile(os.path.splitext(tsv_file)[0] + ".json"):
1395-
# maps variables in CSV file to terms
1396-
temp = DataFrame(columns=mapping_list)
1397-
if args.no_concepts:
1398-
column_to_terms_pheno, cde_tmp = map_variables_to_terms(
1399-
directory=directory,
1400-
assessment_name=tsv_file,
1401-
df=temp,
1402-
output_file=os.path.splitext(tsv_file)[0] + ".json",
1403-
bids=True,
1404-
associate_concepts=False,
1405-
)
1406-
else:
1407-
column_to_terms_pheno, cde_tmp = map_variables_to_terms(
1408-
directory=directory,
1409-
assessment_name=tsv_file,
1410-
df=temp,
1411-
output_file=os.path.splitext(tsv_file)[0] + ".json",
1412-
bids=True,
1413-
)
1414-
else:
1415-
# maps variables in CSV file to terms
1416-
temp = DataFrame(columns=mapping_list)
1417-
if args.no_concepts:
1418-
column_to_terms_pheno, cde_tmp = map_variables_to_terms(
1419-
directory=directory,
1420-
assessment_name=tsv_file,
1421-
df=temp,
1422-
output_file=os.path.splitext(tsv_file)[0] + ".json",
1423-
json_source=os.path.splitext(tsv_file)[0] + ".json",
1424-
bids=True,
1425-
associate_concepts=False,
1426-
)
1427-
else:
1428-
column_to_terms_pheno, cde_tmp = map_variables_to_terms(
1429-
directory=directory,
1430-
assessment_name=tsv_file,
1431-
df=temp,
1432-
output_file=os.path.splitext(tsv_file)[0] + ".json",
1433-
json_source=os.path.splitext(tsv_file)[0] + ".json",
1434-
bids=True,
1435-
)
1436-
# else user did supply a json data dictionary so use it
1437-
else:
1438-
# maps variables in CSV file to terms
1439-
temp = DataFrame(columns=mapping_list)
1440-
if args.no_concepts:
1441-
column_to_terms_pheno, cde_tmp = map_variables_to_terms(
1442-
directory=directory,
1443-
assessment_name=tsv_file,
1444-
df=temp,
1445-
output_file=os.path.splitext(tsv_file)[0] + ".json",
1446-
json_source=args.json_map,
1447-
bids=True,
1448-
associate_concepts=False,
1449-
)
1327+
json_source = None
14501328
else:
1451-
column_to_terms_pheno, cde_tmp = map_variables_to_terms(
1452-
directory=directory,
1453-
assessment_name=tsv_file,
1454-
df=temp,
1455-
output_file=os.path.splitext(tsv_file)[0] + ".json",
1456-
json_source=args.json_map,
1457-
bids=True,
1458-
)
1329+
json_source = os.path.splitext(tsv_file)[0] + ".json"
1330+
else: # if user supplied a JSON data dictionary then use it
1331+
json_source = args.json_map
1332+
# create data dictionary without concept mapping
1333+
if args.no_concepts:
1334+
associate_concepts = False
1335+
else: # create data dictionary with concept mapping
1336+
associate_concepts = True
1337+
# maps variables in CSV file to terms
1338+
temp = DataFrame(columns=mapping_list)
1339+
column_to_terms_pheno, cde_tmp = map_variables_to_terms(
1340+
directory=directory,
1341+
assessment_name=tsv_file,
1342+
df=temp,
1343+
output_file=os.path.splitext(tsv_file)[0] + ".json",
1344+
json_source=json_source,
1345+
bids=True,
1346+
associate_concepts=associate_concepts,
1347+
)
14591348

14601349
for row in pheno_data:
14611350
subjid = row["participant_id"].split("-")

0 commit comments

Comments
 (0)