Skip to content

Commit

Permalink
add custom-rules for dicom2specs
Browse files Browse the repository at this point in the history
  • Loading branch information
pvavra committed Dec 9, 2019
1 parent 2177904 commit d63d3a7
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .datalad/config
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
[datalad "procedures.repack-studyspec"]
call-format = "bash {script} {args}"
help = "helper to convert valid-json into studyspec.json format"

[datalad "procedures.add_customrules"]
help = "add hirni's custom_rules.py script to dataset"
118 changes: 118 additions & 0 deletions custom_rules/custom_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""custom rules for dicom2spec for sfb_cuetarget study"""


class MyDICOM2SpecRules(object):

def __init__(self, dicommetadata):
"""
Parameter
----------
dicommetadata: list of dict
dicom metadata as extracted by datalad; one dict per image series
"""
self._dicom_series = dicommetadata

def __call__(self, subject=None, anon_subject=None, session=None):
"""
Parameters
----------
Returns
-------
list of tuple (dict, bool)
"""
spec_dicts = []

# check whether any "active task" present in thte current series_dict
# we can use that to infer the session as 1 or 4 based on that
if session == None and any(s['SeriesDescription'].startswith("fMRI_task") for s in self._dicom_series):
session = "004"
else:
session = "001"

# print("session == ")
# print(session)
# print("\n")

for dicom_dict in self._dicom_series:
spec_dicts.append((self._rules(dicom_dict,
subject=subject,
anon_subject=anon_subject,
session=session),
self.series_is_valid(dicom_dict)
)
)
return spec_dicts

def _rules(self, series_dict, subject=None, anon_subject=None,
session=None):

# use this to figure out which fields you have available in the dict.
# print('\n-----------------------\n')
# print('\nnext series dict:\n')
# print(series_dict)

# figure out modality:
task = None
modality = None
comment = ''
run = None
aquisition = None
if series_dict['SeriesDescription'].startswith("t1"):
modality = 'T1w'
aquisition = 'mprage'
if series_dict['SeriesDescription'].startswith("gre_field_mapping"):
modality = 'fieldmap'
if series_dict['SeriesDescription'].startswith("IR-EPI"):
# c.f. https://www.ncbi.nlm.nih.gov/pubmed/14635150
modality = 'T1w'
aquisition = 'mprage'
if series_dict['SeriesDescription'].startswith("DTI"):
modality = 'dwi'
if series_dict['SeriesDescription'].startswith("fMRI_task"):
modality = 'bold'
task = 'active'
# number afterwards indicates run number (range: 1 to 4)
run = series_dict['SeriesDescription'][len("fMRI_task")]
if series_dict['SeriesDescription'].startswith("fMRI_withouttask"):
modality = 'bold'
task = 'passive'
# number afterwards indicates run number (range: 1 to 4)
run = series_dict['SeriesDescription'][len("fMRI_withouttask")]
if series_dict['SeriesDescription'].startswith("fMRI_resting"):
modality = 'bold'
task = 'rest'
# for any single-band reference image, overwrite to `sbref` instead of bold
if series_dict['SeriesDescription'].endswith("SBRef"):
modality = 'sbref'

if series_dict['SeriesDescription'].startswith("AAHead_Scout"):
comment = "Localizer - will be ignored for BIDS"


return {'description': series_dict['SeriesDescription']
if "SeriesDescription" in series_dict else '',
'comment': comment,
'subject': series_dict['PatientID'] if not subject else subject,
'anon-subject': anon_subject if anon_subject else None,
'bids-session': session if session else None,
'bids-modality' : modality,
'bids-task': task,
'bids-run': run,
'bids-acquisition': acquisition
}

def series_is_valid(self, series_dict):
# For all series which this returns `false`, the hirni-spec2bids will
# skip the conversion to niftis.

# Skip all localizers
valid = not series_dict['SeriesDescription'].startswith("AAHead_Scout")
# skip DTI for now
valid = valid and not series_dict['SeriesDescription'].startswith("DTI")
return valid


__datalad_hirni_rules = MyDICOM2SpecRules
22 changes: 22 additions & 0 deletions procedures/add_customrules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Procedure to configure custom rules for dicom2spec"""

import sys
import os.path as op

from datalad.distribution.dataset import require_dataset

ds = require_dataset(
sys.argv[1],
check_installed=True,
purpose='adding custom rule')

# grab config
cfg = ds.config

# TODO: what to do when a rules is already present? abort? overwrite?
cfg.add('datalad.hirni.dicom2spec.rules',
# we assume that hirni_addons have been installed into `sourcedata/code/.`
op.join("code","hirni_addons", "custom_rules", "custom_rules.py"),
where='dataset')

ds.save(message="add custom_rules.py to dataset configuration")

0 comments on commit d63d3a7

Please sign in to comment.