Skip to content

Commit

Permalink
Fix some style issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed Oct 31, 2024
1 parent 1d4eaaf commit ecc4901
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 22 deletions.
27 changes: 22 additions & 5 deletions aslprep/interfaces/cbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ def _run_interface(self, runtime):
config.loggers.interface.info(
'Calculating deltaM from label-control pairs in ASL file.'
)
assert len(label_volume_idx) == len(control_volume_idx)
if len(label_volume_idx) != len(control_volume_idx):
raise ValueError(
f'Number of label volumes ({len(label_volume_idx)}) != '
f'number of control volumes ({len(control_volume_idx)})'
)
metadata_idx = control_volume_idx
control_data = asl_data[:, :, :, control_volume_idx]
label_data = asl_data[:, :, :, label_volume_idx]
Expand Down Expand Up @@ -435,7 +439,8 @@ def _run_interface(self, runtime):
# NiftiMasker.transform, until 0.12.0, so the arrays will currently be 2D no matter what.
masker = maskers.NiftiMasker(mask_img=mask_file)
deltam_arr = masker.fit_transform(deltam_file).T # Transpose to SxT
assert deltam_arr.ndim == 2, f'deltam is {deltam_arr.ndim}'
if deltam_arr.ndim != 2:
raise ValueError(f'deltam is {deltam_arr.ndim}')

# Load the M0 map and average over time, in case there's more than one map in the file.
m0data = masker.transform(m0_file)
Expand Down Expand Up @@ -551,21 +556,33 @@ def _run_interface(self, runtime):
elif metadata['BolusCutOffTechnique'] == 'QUIPSS':
# PASL + QUIPSS
# Only one BolusCutOffDelayTime allowed.
assert isinstance(metadata['BolusCutOffDelayTime'], Number)
if not isinstance(metadata['BolusCutOffDelayTime'], Number):
raise ValueError(
f'Expected a single BolusCutOffDelayTime, but got '
f'{metadata["BolusCutOffDelayTime"]}'
)
denom_factor = plds - metadata['BolusCutOffDelayTime'] # delta_TI, per Wong 1998

elif metadata['BolusCutOffTechnique'] == 'QUIPSSII':
# PASL + QUIPSSII
# Per SD, use PLD as TI for PASL, so we will just use 'plds' in the numerator when
# calculating the perfusion factor.
# Only one BolusCutOffDelayTime allowed.
assert isinstance(metadata['BolusCutOffDelayTime'], Number)
if not isinstance(metadata['BolusCutOffDelayTime'], Number):
raise ValueError(
f'Expected a single BolusCutOffDelayTime, but got '
f'{metadata["BolusCutOffDelayTime"]}'
)
denom_factor = metadata['BolusCutOffDelayTime'] # called TI1 in Alsop 2015

elif metadata['BolusCutOffTechnique'] == 'Q2TIPS':
# PASL + Q2TIPS
# Q2TIPS should have two BolusCutOffDelayTimes.
assert len(metadata['BolusCutOffDelayTime']) == 2
if len(metadata['BolusCutOffDelayTime']) != 2:
raise ValueError(
f'Expected two BolusCutOffDelayTimes, but got '
f'{metadata["BolusCutOffDelayTime"]}'
)
denom_factor = metadata['BolusCutOffDelayTime'][0] # called TI1 in Noguchi 2015

else:
Expand Down
5 changes: 4 additions & 1 deletion aslprep/interfaces/parcellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ def _run_interface(self, runtime):

# Use nilearn to parcellate the file
timeseries_arr = masker.fit_transform(self.inputs.in_file)
assert timeseries_arr.shape[1] == n_found_nodes
if timeseries_arr.shape[1] != n_found_nodes:
raise ValueError(
f'Expected {n_found_nodes} parcels, but found {timeseries_arr.shape[1]}.'
)
masker_labels = masker.labels_[:]
del masker

Expand Down
7 changes: 6 additions & 1 deletion aslprep/interfaces/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ class SelectHighestContrastVolumes(SimpleInterface):
def _run_interface(self, runtime):
asl_img = nb.load(self.inputs.asl_file)
aslcontext_df = pd.read_table(self.inputs.aslcontext)
assert aslcontext_df.shape[0] == asl_img.shape[3]
if aslcontext_df.shape[0] != asl_img.shape[3]:
raise ValueError(
f'Number of volumes in {self.inputs.asl_file} ({asl_img.shape[3]}) '
f'does not match the number of rows in {self.inputs.aslcontext} '
f'({aslcontext_df.shape[0]})'
)

if 'm0scan' in aslcontext_df['volume_type'].tolist() and self.inputs.prioritize_m0:
target_type = 'm0scan'
Expand Down
13 changes: 10 additions & 3 deletions aslprep/interfaces/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ def _run_interface(self, runtime):
# run rmsdiff
rmsdiff = RMSDiff(matrixfile1=file1, matrixfile2=file2, ref_vol=self.inputs.ref_file)
res = rmsdiff.run()
assert isinstance(res.outputs.rmsd, float)
if not isinstance(res.outputs.rmsd, float):
raise ValueError(f'Expected a float, got {res.outputs.rmsd}')
rmsd.append(str(res.outputs.rmsd))

self._results['out_file'] = fname_presuffix(
Expand Down Expand Up @@ -225,8 +226,14 @@ def _run_interface(self, runtime):
out_par = [None] * aslcontext.shape[0]
out_mat_files = [None] * aslcontext.shape[0]

assert len(self.inputs.volume_types) == len(self.inputs.mat_files)
assert len(self.inputs.volume_types) == len(self.inputs.par_files)
if len(self.inputs.volume_types) != len(self.inputs.mat_files):
raise ValueError(
'Number of volume types and number of mat files must be the same.'
)
if len(self.inputs.volume_types) != len(self.inputs.par_files):
raise ValueError(
'Number of volume types and number of par files must be the same.'
)

for i_type, volume_type in enumerate(self.inputs.volume_types):
type_mat_files = self.inputs.mat_files[i_type]
Expand Down
4 changes: 2 additions & 2 deletions aslprep/utils/asl.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def select_processing_target(aslcontext):

try:
aslcontext_df = pd.read_table(aslcontext)
except:
raise FileNotFoundError(aslcontext)
except Exception:
raise FileNotFoundError(aslcontext) from None

voltypes = aslcontext_df['volume_type'].tolist()

Expand Down
10 changes: 7 additions & 3 deletions aslprep/utils/cbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,12 @@ def estimate_att_pcasl(deltam_arr, plds, lds, t1blood, t1tissue):
.. footbibliography::
"""
n_voxels, n_plds = plds.shape
assert deltam_arr.shape == plds.shape, f'{deltam_arr.shape} != {plds.shape}'
if deltam_arr.shape != plds.shape:
raise ValueError(f'{deltam_arr.shape} != {plds.shape}')

# Beginning of auxil_asl_gen_wsum
assert lds.size == n_plds, f'{lds.size} != {n_plds}'
if lds.size != n_plds:
raise ValueError(f'{lds.size} != {n_plds}')

att_arr = np.empty(n_voxels)
for i_voxel in range(n_voxels):
Expand Down Expand Up @@ -694,7 +696,9 @@ def estimate_cbf_pcasl_multipld(
"""
n_voxels, n_volumes = deltam_arr.shape
n_voxels_pld, n_plds = plds.shape
assert n_voxels_pld == n_voxels
if n_voxels_pld != n_voxels:
raise ValueError(f'{n_voxels_pld} != {n_voxels}')

first_voxel_plds = plds[0, :]

if n_plds != n_volumes:
Expand Down
4 changes: 2 additions & 2 deletions aslprep/utils/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ def plot(self, figure=None):
confoundplot(
tseries, grid[grid_id], tr=self.tr, color=palette[i], name=name, **kwargs
)
except:
raise ValueError(name)
except Exception:
raise ValueError(name) from None
grid_id += 1

plt_carpet(
Expand Down
2 changes: 1 addition & 1 deletion aslprep/workflows/asl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
def init_asl_wf(
*,
asl_file: str,
precomputed: dict = {},
precomputed: dict = None,
fieldmap_id: str | None = None,
):
"""Perform the functional preprocessing stages of ASLPrep.
Expand Down
2 changes: 1 addition & 1 deletion aslprep/workflows/asl/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def init_asl_fit_wf(
asl_file: str,
m0scan: str | None,
use_ge: bool,
precomputed: dict = {},
precomputed: dict = None,
fieldmap_id: str | None = None,
omp_nthreads: int = 1,
name: str = 'asl_fit_wf',
Expand Down
3 changes: 1 addition & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import os
import sys
from datetime import datetime

from packaging import version as pver
from sphinx import __version__ as sphinxversion
Expand Down Expand Up @@ -133,7 +132,7 @@
# General information about the project.
project = 'aslprep'
author = 'ASLPrep developers'
copyright = f'2020-{datetime.now().year}, {author}'
copyright = f'2020-, {author}'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down
2 changes: 1 addition & 1 deletion docs/sphinxext/github_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _linkcode_resolve(domain, info, package, url_fmt, revision):
return

class_name = info['fullname'].split('.')[0]
if type(class_name) != str:
if not isinstance(class_name, str):
# Python 2 only
class_name = class_name.encode('utf-8')
module = __import__(info['module'], fromlist=[class_name])
Expand Down

0 comments on commit ecc4901

Please sign in to comment.