Skip to content

Commit bd045c9

Browse files
authored
Merge pull request #420 from djarecka/bids_update
Bids update: BIDS URI format
2 parents e5fc96b + 43abcd1 commit bd045c9

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/nidm/experiment/tools/bidsmri2nidm.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,22 @@
3636
)
3737

3838

39-
def getRelPathToBIDS(filepath, bids_root):
39+
def getRelPathToBIDS(filepath, bids_root, bidsuri_format=False):
4040
"""
4141
This function returns a relative file link that is relative to the BIDS root directory.
4242
4343
:param filename: absolute path + file
4444
:param bids_root: absolute path to BIDS directory
45+
:param bidsuri_format: if True, BIDS URI format is created with bids:: prefix
4546
:return: relative path to file, relative to BIDS root
4647
"""
4748
path, file = os.path.split(filepath)
4849

4950
relpath = path.replace(bids_root, "")
50-
return os.path.join(relpath, file)
51+
file_relpath = os.path.join(relpath, file)
52+
if bidsuri_format:
53+
file_relpath = f'bids::{file_relpath.lstrip("/")}'
54+
return file_relpath
5155

5256

5357
def getsha512(filename):
@@ -311,7 +315,9 @@ def addimagingsessions(
311315
acq_obj.add_attributes(
312316
{
313317
Constants.NIDM_FILENAME: getRelPathToBIDS(
314-
join(file_tpl.dirname, file_tpl.filename), directory
318+
join(file_tpl.dirname, file_tpl.filename),
319+
directory,
320+
bidsuri_format=True,
315321
)
316322
}
317323
)
@@ -458,7 +464,9 @@ def addimagingsessions(
458464
acq_obj.add_attributes(
459465
{
460466
Constants.NIDM_FILENAME: getRelPathToBIDS(
461-
join(file_tpl.dirname, file_tpl.filename), directory
467+
join(file_tpl.dirname, file_tpl.filename),
468+
directory,
469+
bidsuri_format=True,
462470
)
463471
}
464472
)
@@ -554,7 +562,7 @@ def addimagingsessions(
554562
PROV_TYPE: Constants.NIDM_MRI_BOLD_EVENTS,
555563
BIDS_Constants.json_keys["TaskName"]: json_data["TaskName"],
556564
Constants.NIDM_FILENAME: getRelPathToBIDS(
557-
events_file[0].filename, directory
565+
events_file[0].filename, directory, bidsuri_format=True
558566
),
559567
}
560568
)
@@ -661,7 +669,9 @@ def addimagingsessions(
661669
acq_obj.add_attributes(
662670
{
663671
Constants.NIDM_FILENAME: getRelPathToBIDS(
664-
join(file_tpl.dirname, file_tpl.filename), directory
672+
join(file_tpl.dirname, file_tpl.filename),
673+
directory,
674+
bidsuri_format=True,
665675
)
666676
}
667677
)
@@ -765,7 +775,9 @@ def addimagingsessions(
765775
acq_obj.add_attributes(
766776
{
767777
Constants.NIDM_FILENAME: getRelPathToBIDS(
768-
join(file_tpl.dirname, file_tpl.filename), directory
778+
join(file_tpl.dirname, file_tpl.filename),
779+
directory,
780+
bidsuri_format=True,
769781
)
770782
}
771783
)
@@ -846,6 +858,7 @@ def addimagingsessions(
846858
),
847859
),
848860
directory,
861+
bidsuri_format=True,
849862
)
850863
}
851864
)
@@ -905,6 +918,7 @@ def addimagingsessions(
905918
),
906919
),
907920
directory,
921+
bidsuri_format=True,
908922
)
909923
}
910924
)
@@ -1114,7 +1128,9 @@ def bidsmri2project(directory, args):
11141128
acq_entity.add_attributes(
11151129
{
11161130
Constants.NIDM_FILENAME: getRelPathToBIDS(
1117-
os.path.join(directory, "participants.tsv"), directory
1131+
os.path.join(directory, "participants.tsv"),
1132+
directory,
1133+
bidsuri_format=True,
11181134
)
11191135
}
11201136
)
@@ -1153,7 +1169,9 @@ def bidsmri2project(directory, args):
11531169
Namespace("bids", Constants.BIDS), "sidecar_file"
11541170
),
11551171
Constants.NIDM_FILENAME: getRelPathToBIDS(
1156-
os.path.join(directory, "participants.json"), directory
1172+
os.path.join(directory, "participants.json"),
1173+
directory,
1174+
bidsuri_format=True,
11571175
),
11581176
}
11591177
)
@@ -1376,7 +1394,11 @@ def bidsmri2project(directory, args):
13761394

13771395
# link TSV file
13781396
acq_entity.add_attributes(
1379-
{Constants.NIDM_FILENAME: getRelPathToBIDS(tsv_file, directory)}
1397+
{
1398+
Constants.NIDM_FILENAME: getRelPathToBIDS(
1399+
tsv_file, directory, bidsuri_format=True
1400+
)
1401+
}
13801402
)
13811403

13821404
# if there are git annex sources for participants.tsv file then add them
@@ -1408,7 +1430,7 @@ def bidsmri2project(directory, args):
14081430
{
14091431
PROV_TYPE: Constants.BIDS["sidecar_file"],
14101432
Constants.NIDM_FILENAME: getRelPathToBIDS(
1411-
data_dict, directory
1433+
data_dict, directory, bidsuri_format=True
14121434
),
14131435
}
14141436
)

0 commit comments

Comments
 (0)