Skip to content

Commit 8bd1576

Browse files
authored
Merge pull request #19 from harvard-nrg/b0vols_fmaps
B0vols fmaps
2 parents 5ce1fd4 + 67cd1ee commit 8bd1576

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

dwiqc/tasks/prequal.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ def create_bfiles(self, inputs_dir):
6969
if not fmap_files:
7070
self._layout.get(subject=self._sub, session=self._ses, suffix='epi', extension='.nii', return_type='filename')
7171

72+
# truncate down the fmap files to include just b0 volumes
73+
74+
truncated_fmaps = self.truncate_fmaps(fmap_files)
75+
7276
# get the basename of the file and then remove the extension
73-
for fmap in fmap_files:
77+
for fmap in truncated_fmaps:
7478

7579
basename = os.path.basename(fmap)
7680

@@ -116,6 +120,56 @@ def create_bfiles(self, inputs_dir):
116120
# this method serves to create the accompanying spec file for prequal
117121
# the contents of the file depends on the SeriesDescription stored in the metadata
118122

123+
def truncate_fmaps(self, fmap_files):
124+
"""
125+
method that will extract all the b0 volumes from the scans designated as BIDS fieldmaps
126+
"""
127+
self.get_fsl_sif()
128+
129+
truncated_fmaps = []
130+
131+
for fmap in fmap_files:
132+
fmap_basename = os.path.basename(fmap)
133+
fmap_dir = os.path.dirname(fmap)
134+
cmd = [
135+
'singularity',
136+
'exec',
137+
'--pwd', fmap_dir,
138+
self._fsl_sif,
139+
'/APPS/fsl/bin/fslselectvols',
140+
'-i', fmap_basename,
141+
'-o', fmap_basename,
142+
'--vols=0'
143+
]
144+
#logger.info(f'running {json.dumps(cmd, indent=2)} on {fmap}')
145+
146+
cmdline = subprocess.list2cmdline(cmd)
147+
logger.info(f'running {cmdline}')
148+
proc = subprocess.Popen(cmdline, shell=True, stdout=subprocess.PIPE)
149+
proc.communicate()
150+
if proc.returncode > 0:
151+
logger.critical(f'fslselectvols command failed')
152+
raise subprocess.CalledProcessError(returncode=proc.returncode, cmd=cmdline)
153+
154+
truncated_fmaps.append(fmap_dir + f'/{fmap_basename}')
155+
156+
157+
return truncated_fmaps
158+
159+
160+
def get_fsl_sif(self):
161+
162+
if self._container_dir:
163+
try:
164+
self._fsl_sif = f'{self._container_dir}/fsl_6.0.4.sif'
165+
except FileNotFoundError:
166+
logger.error(f'{self._container_dir}/fsl_6.0.4.sif does not exist. Verify the path and file name.')
167+
sys.exit(1)
168+
else:
169+
home_dir = os.path.expanduser("~")
170+
self._fsl_sif = os.path.join(home_dir, '.config/dwiqc/containers/fsl_6.0.4.sif')
171+
172+
119173
def create_spec(self, inputs_dir):
120174
dwi_file = self._layout.get(subject=self._sub, session=self._ses, run=self._run, suffix='dwi', extension='.nii.gz')
121175
#dwi_file = layout.get()[10]

0 commit comments

Comments
 (0)