From 5c86f0293b53c15e1e8c0c15cd236d0d16998887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Tourbier?= Date: Mon, 31 Jan 2022 10:45:46 +0100 Subject: [PATCH] STY: Fix a number of code style issues reported by Codacy --- cmtklib/carbonfootprint.py | 2 +- cmtklib/config.py | 1 - cmtklib/connectome.py | 1 - cmtklib/diffusion.py | 66 +++++++++++++++----------------------- cmtklib/interfaces/afni.py | 2 +- cmtklib/interfaces/dipy.py | 49 ++++++++++++++-------------- cmtklib/interfaces/fsl.py | 2 +- cmtklib/parcellation.py | 57 ++++++++++++++++---------------- 8 files changed, 81 insertions(+), 99 deletions(-) diff --git a/cmtklib/carbonfootprint.py b/cmtklib/carbonfootprint.py index bf4e8c61b..68b9be5a0 100644 --- a/cmtklib/carbonfootprint.py +++ b/cmtklib/carbonfootprint.py @@ -140,7 +140,7 @@ def create_carbon_footprint_message(bids_dir, emissions_csv_file, nb_of_subjects ---------- bids_dir : string Path to root directory of the BIDS dataset - + emissions_csv_file : string Path to the `emissions.csv` file generated by `Codecarbon`. diff --git a/cmtklib/config.py b/cmtklib/config.py index 23399a32f..aaf94e397 100644 --- a/cmtklib/config.py +++ b/cmtklib/config.py @@ -180,7 +180,6 @@ def save_configparser_as_json(config, config_json_path, ini_mode=False, debug=Fa print_error( f" .. EXCEPTION: String {value} COULD NOT BE evaluated" ) - pass if isinstance(value, dict): if debug: # pragma: no cover diff --git a/cmtklib/connectome.py b/cmtklib/connectome.py index 99913ace9..ba7ba845c 100644 --- a/cmtklib/connectome.py +++ b/cmtklib/connectome.py @@ -30,7 +30,6 @@ InputMultiPath, OutputMultiPath, ) -from nipype.interfaces import cmtk from nipype.utils.filemanip import split_filename from .util import mean_curvature, length diff --git a/cmtklib/diffusion.py b/cmtklib/diffusion.py index 2f4a1df98..6e8c1e2f0 100644 --- a/cmtklib/diffusion.py +++ b/cmtklib/diffusion.py @@ -21,7 +21,6 @@ OutputMultiPath, InputMultiPath, ) -from nipype.utils.filemanip import split_filename from traits.trait_types import List, Str, Int, Enum @@ -44,32 +43,32 @@ def compute_length_array(trkfile=None, streams=None, savefname="lengths.npy"): Returns ------- - leng : numpy.array + fibers_length : numpy.array Array of fiber lengths """ if streams is None and trkfile is not None: - print("Compute length array for fibers in %s" % trkfile) + print(f'Compute length array for fibers in {trkfile}') streams, hdr = tv.read(trkfile, as_generator=True) n_fibers = hdr["n_count"] if n_fibers == 0: msg = ( - "Header field n_count of trackfile %s is set to 0. No track seem to exist in this file." - % trkfile + f'Header field n_count of trackfile {trkfile} is set to 0. ' + "No track seem to exist in this file." ) print(msg) raise Exception(msg) else: n_fibers = len(streams) - leng = np.zeros(n_fibers, dtype=np.float) + fibers_length = np.zeros(n_fibers, dtype=np.float) for i, fib in enumerate(streams): - leng[i] = length(fib[0]) + fibers_length[i] = length(fib[0]) # store length array - np.save(savefname, leng) - print("Store lengths array to: %s" % savefname) + np.save(savefname, fibers_length) + print(f'Store lengths array to: {savefname}') - return leng + return fibers_length def filter_fibers(intrk, outtrk="", fiber_cutoff_lower=20, fiber_cutoff_upper=500): @@ -92,7 +91,7 @@ def filter_fibers(intrk, outtrk="", fiber_cutoff_lower=20, fiber_cutoff_upper=50 print("Cut Fiber Filtering") print("===================") - print("Input file for fiber cutting is: %s" % intrk) + print(f'Input file for fiber cutting is: {intrk}') if outtrk == "": _, filename = os.path.split(intrk) @@ -119,10 +118,10 @@ def filter_fibers(intrk, outtrk="", fiber_cutoff_lower=20, fiber_cutoff_upper=50 # print("Compute length array for cutted fibers") # le = compute_length_array(streams=outstreams) - print("Write out file: %s" % outtrk) - print("Number of fibers out : %d" % hdrnew["n_count"]) + print(f'Write out file: {outtrk}') + print(f'Number of fibers out : {hdrnew["n_count"]}') tv.write(outtrk, outstreams, hdrnew) - print("File wrote : %d" % os.path.exists(outtrk)) + print(f'File wrote : {os.path.exists(outtrk)}') # ---- # extension idea @@ -174,7 +173,7 @@ def _run_interface(self, runtime): axis_dict = {"x": 0, "y": 1, "z": 2} f = open(self.inputs.table, "r") header = "" - for h in range(self.inputs.header_lines): + for _ in range(self.inputs.header_lines): header += f.readline() if self.inputs.delimiter == " ": table = np.loadtxt(f) @@ -303,7 +302,7 @@ def _run_interface(self, runtime): process = subprocess.Popen( fslmaths_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) - proc_stdout = process.communicate()[0].strip() + _ = process.communicate()[0].strip() fslmaths_cmd = "fslmaths %s -kernel sphere %s -dilD %s" % ( os.path.abspath(self.inputs.pve_wm_file), @@ -315,7 +314,7 @@ def _run_interface(self, runtime): process = subprocess.Popen( fslmaths_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) - proc_stdout = process.communicate()[0].strip() + _ = process.communicate()[0].strip() fslmaths_cmd = "fslmaths %s -kernel sphere %s -dilD %s" % ( os.path.abspath(self.inputs.pve_gm_file), @@ -327,7 +326,7 @@ def _run_interface(self, runtime): process = subprocess.Popen( fslmaths_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) - proc_stdout = process.communicate()[0].strip() + _ = process.communicate()[0].strip() fslmaths_cmd = "fslmaths %s -kernel gauss %s -fmean %s" % ( os.path.abspath(self.inputs.pve_csf_file), @@ -339,7 +338,7 @@ def _run_interface(self, runtime): process = subprocess.Popen( fslmaths_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) - proc_stdout = process.communicate()[0].strip() + _ = process.communicate()[0].strip() fslmaths_cmd = "fslmaths %s -kernel gauss %s -fmean %s" % ( os.path.abspath(self.inputs.pve_wm_file), @@ -351,7 +350,7 @@ def _run_interface(self, runtime): process = subprocess.Popen( fslmaths_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) - proc_stdout = process.communicate()[0].strip() + _ = process.communicate()[0].strip() fslmaths_cmd = "fslmaths %s -kernel gauss %s -fmean %s" % ( os.path.abspath(self.inputs.pve_gm_file), @@ -363,7 +362,7 @@ def _run_interface(self, runtime): process = subprocess.Popen( fslmaths_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) - proc_stdout = process.communicate()[0].strip() + _ = process.communicate()[0].strip() pve_csf = nib.load(os.path.abspath(self.inputs.pve_csf_file)).get_data() pve_wm = nib.load(os.path.abspath(self.inputs.pve_wm_file)).get_data() @@ -387,14 +386,11 @@ def _run_interface(self, runtime): def _list_outputs(self): outputs = self._outputs().get() - - pve_files = [] - pve_files.append(os.path.abspath(self.inputs.pve_csf_file)) - pve_files.append(os.path.abspath(self.inputs.pve_gm_file)) - pve_files.append(os.path.abspath(self.inputs.pve_wm_file)) - - outputs["partial_volume_files"] = pve_files - + outputs["partial_volume_files"] = [ + os.path.abspath(self.inputs.pve_csf_file), + os.path.abspath(self.inputs.pve_gm_file), + os.path.abspath(self.inputs.pve_wm_file) + ] return outputs @@ -505,7 +501,7 @@ def _run_interface(self, runtime): f = open(self.inputs.bvecs, "r") header = "" - for h in range(self.inputs.header_lines): + for _ in range(self.inputs.header_lines): header += f.readline() if self.inputs.delimiter == " ": table = np.loadtxt(f) @@ -526,14 +522,6 @@ def _run_interface(self, runtime): else: np.savetxt(out_f, table, delimiter=self.inputs.delimiter) - # with open(os.path.abspath('flipped_bvecs.bvec'), 'w') as out_f: - # np.savetxt(out_f, table, header=header, delimiter=self.inputs.delimiter) - - # out_f = file(os.path.abspath('flipped_bvecs.bvec'), 'w') - # if self.inputs.header_lines > 0: - # out_f.write(header) - # np.savetxt(out_f, table, header=header, delimiter=self.inputs.delimiter) - # out_f.close() return runtime def _list_outputs(self): @@ -657,7 +645,5 @@ def _run_interface(self, runtime): def _list_outputs(self): outputs = self._outputs().get() - outputs["out_gmwmi_file"] = os.path.abspath(self.inputs.out_gmwmi_file) - return outputs diff --git a/cmtklib/interfaces/afni.py b/cmtklib/interfaces/afni.py index 351b39759..48aad1d19 100644 --- a/cmtklib/interfaces/afni.py +++ b/cmtklib/interfaces/afni.py @@ -198,7 +198,7 @@ def _list_outputs(self): if isdefined(self.inputs.out_file): if outputs[name]: print('out_file: {}'.format(outputs[name])) - _, base, ext = split_filename(outputs[name]) + _, _, ext = split_filename(outputs[name]) if ext == "": outputs[name] = outputs[name] + "+orig.BRIK" else: diff --git a/cmtklib/interfaces/dipy.py b/cmtklib/interfaces/dipy.py index cf6df8fc3..676cdb772 100644 --- a/cmtklib/interfaces/dipy.py +++ b/cmtklib/interfaces/dipy.py @@ -1153,35 +1153,35 @@ def _run_interface(self, runtime): hdr = imref.header.copy() gtab = self._get_gradient_table() - gtab = gradient_table(bvals=gtab.bvals, bvecs=gtab.bvecs, - small_delta=self.inputs.small_delta, - big_delta=self.inputs.big_delta) - - map_model_both_aniso = mapmri.MapmriModel(gtab, - radial_order=self.inputs.radial_order, - anisotropic_scaling=True, - laplacian_regularization=self.inputs.laplacian_regularization, - laplacian_weighting=self.inputs.laplacian_weighting, - positivity_constraint=self.inputs.positivity_constraint - ) + gtab = gradient_table( + bvals=gtab.bvals, bvecs=gtab.bvecs, + small_delta=self.inputs.small_delta, + big_delta=self.inputs.big_delta + ) + + map_model_both_aniso = mapmri.MapmriModel( + gtab, + radial_order=self.inputs.radial_order, + anisotropic_scaling=True, + laplacian_regularization=self.inputs.laplacian_regularization, + laplacian_weighting=self.inputs.laplacian_weighting, + positivity_constraint=self.inputs.positivity_constraint + ) IFLOGGER.info('Fitting MAP-MRI model') mapfit_both_aniso = map_model_both_aniso.fit(data) '''maps''' - maps = {} - maps["rtop"] = mapfit_both_aniso.rtop() # '''1/Volume of pore''' - maps["rtap"] = mapfit_both_aniso.rtap() # '''1/AREA ...''' - maps["rtpp"] = mapfit_both_aniso.rtpp() # '''1/length ...''' - maps["msd"] = mapfit_both_aniso.msd( - ) # '''similar to mean diffusivity''' - maps["qiv"] = mapfit_both_aniso.qiv( - ) # '''almost reciprocal of rtop''' - maps["ng"] = mapfit_both_aniso.ng() # '''general non Gaussianity''' - maps[ - "ng_perp"] = mapfit_both_aniso.ng_perpendicular() # '''perpendicular to main direction (likely to be non gaussian in white matter)''' - maps["ng_para"] = mapfit_both_aniso.ng_parallel( - ) # '''along main direction (likely to be gaussian)''' + maps = { + "rtop": mapfit_both_aniso.rtop(), + "rtap": mapfit_both_aniso.rtap(), + "rtpp": mapfit_both_aniso.rtpp(), + "msd": mapfit_both_aniso.msd(), + "qiv": mapfit_both_aniso.qiv(), + "ng": mapfit_both_aniso.ng(), + "ng_perp": mapfit_both_aniso.ng_perpendicular(), + "ng_para": mapfit_both_aniso.ng_parallel() + } ''' The most related to white matter anisotropy are: rtpp, for anisotropy @@ -1208,7 +1208,6 @@ def _run_interface(self, runtime): def _list_outputs(self): outputs = self._outputs().get() outputs['model'] = self._gen_filename('mapmri', ext='.pklz') - for metric in ["rtop", "rtap", "rtpp", "msd", "qiv", "ng", "ng_perp", "ng_para"]: outputs["{}_file".format(metric)] = self._gen_filename(metric) return outputs diff --git a/cmtklib/interfaces/fsl.py b/cmtklib/interfaces/fsl.py index 32c46397a..5babb05ea 100644 --- a/cmtklib/interfaces/fsl.py +++ b/cmtklib/interfaces/fsl.py @@ -355,7 +355,7 @@ class Eddy(FSLCommand): output_spec = EddyOutputSpec def __init__(self, **inputs): - return super(Eddy, self).__init__(**inputs) + super(Eddy, self).__init__(**inputs) def _run_interface(self, runtime): if not isdefined(self.inputs.out_file): diff --git a/cmtklib/parcellation.py b/cmtklib/parcellation.py index 1570db841..95919585d 100644 --- a/cmtklib/parcellation.py +++ b/cmtklib/parcellation.py @@ -2373,7 +2373,7 @@ def create_roi(subject_id, subjects_dir, v=True): if not (os.access(freesurfer_subj, os.F_OK)): print('ERROR: FreeSurfer subjects directory ($SUBJECTS_DIR) does not exist') else: - if v: + if v: # pragma: no cover print( '- FreeSurfer subjects directory ($SUBJECTS_DIR):\n {}\n'.format(freesurfer_subj)) @@ -2390,14 +2390,14 @@ def create_roi(subject_id, subjects_dir, v=True): print(' -> Copy fsaverage') shutil.copytree(src, dst) else: - if v: + if v: # pragma: no cover print( '- FreeSurfer subjects directory ($SUBJECTS_DIR) DOES contain \'fsaverage\'\n') if not (os.access(subject_dir, os.F_OK)): print('ERROR: No input subject directory was found in FreeSurfer $SUBJECTS_DIR') else: - if v: + if v: # pragma: no cover print('- Freesurfer subject id:\n {}\n'.format(subject_id)) print('- Freesurfer subject directory:\n {}\n'.format(subject_dir)) @@ -2434,7 +2434,7 @@ def create_roi(subject_id, subjects_dir, v=True): os.makedirs(this_dir) # Loop over parcellation scales - if v: + if v: # pragma: no cover print('Generate MULTISCALE PARCELLATION for input subject') fs_string = 'export SUBJECTS_DIR=' + freesurfer_subj @@ -2456,12 +2456,12 @@ def create_roi(subject_id, subjects_dir, v=True): for i in reversed(list(range(0, nscales))): - if v: + if v: # pragma: no cover print(' ... working on multiscale parcellation, SCALE {}'.format(i + 1)) # 1. Resample fsaverage CorticalSurface onto SUBJECT_ID CorticalSurface and map annotation for current scale # Left hemisphere - if v: + if v: # pragma: no cover print( ' > resample fsaverage CorticalSurface to individual CorticalSurface') mri_cmd = fs_string + '; mri_surf2surf --srcsubject fsaverage --trgsubject %s --hemi lh --sval-annot %s --tval %s' % ( @@ -2471,10 +2471,10 @@ def create_roi(subject_id, subjects_dir, v=True): os.path.join(subject_dir, 'label', lh_annot_files[i]) ) if v == 2: - _ = subprocess.call(mri_cmd, shell=True) + subprocess.call(mri_cmd, shell=True) else: - _ = subprocess.call( - mri_cmd, shell=True, stdout=FNULL, stderr=subprocess.STDOUT) + subprocess.call(mri_cmd, shell=True, stdout=FNULL, stderr=subprocess.STDOUT) + # Right hemisphere mri_cmd = fs_string + '; mri_surf2surf --srcsubject fsaverage --trgsubject %s --hemi rh --sval-annot %s --tval %s' % ( subject_id, @@ -2483,14 +2483,13 @@ def create_roi(subject_id, subjects_dir, v=True): os.path.join(subject_dir, 'label', rh_annot_files[i]) ) if v == 2: - status = subprocess.call(mri_cmd, shell=True) + subprocess.call(mri_cmd, shell=True) else: - status = subprocess.call( - mri_cmd, shell=True, stdout=FNULL, stderr=subprocess.STDOUT) + subprocess.call(mri_cmd, shell=True, stdout=FNULL, stderr=subprocess.STDOUT) # 2. Generate Nifti volume from annotation # Note: change here --wmparc-dmax (FS default 5mm) to dilate cortical regions toward the WM - if v: + if v: # pragma: no cover print(' > generate Nifti volume from annotation') mri_cmd = fs_string + '; mri_aparc2aseg --s %s --annot %s --wmparc-dmax 0 --labelwm --hypo-as-wm --new-ribbon --o %s' % ( subject_id, @@ -2504,7 +2503,7 @@ def create_roi(subject_id, subjects_dir, v=True): # 3. Update numerical IDs of cortical and subcortical regions # Load Nifti volume - if v: + if v: # pragma: no cover print(' > relabel cortical and subcortical regions' ' for consistency between resolutions') this_nifti = ni.load(os.path.join(subject_dir, 'tmp', rois_output[i])) @@ -2551,7 +2550,7 @@ def create_roi(subject_id, subjects_dir, v=True): value = np.argmax(counts) newrois[xxMax[j], yyMax[j], zzMax[j]] = value # print("Cortical ROIs adaptation took %s seconds to process." % (time()-adaptstart)) - if v: + if v: # pragma: no cover print(' ... save output volumes') this_out = os.path.join(subject_dir, 'mri', rois_output[i]) img = ni.Nifti1Image(newrois, this_nifti.affine, hdr2) @@ -2559,7 +2558,7 @@ def create_roi(subject_id, subjects_dir, v=True): del img # 4. Dilate cortical regions - if v: + if v: # pragma: no cover print(" > dilating cortical regions") # loop throughout all the voxels belonging to the aseg GM volume for j in range(xx.size): @@ -2578,7 +2577,7 @@ def create_roi(subject_id, subjects_dir, v=True): newrois[xx[j], yy[j], zz[j]] = value # 5. Save Nifti and mgz volumes - if v: + if v: # pragma: no cover print(' ... save output volumes ') this_out = os.path.join(subject_dir, 'mri', roivs_output[i]) img = ni.Nifti1Image(newrois, this_nifti.affine, hdr2) @@ -2589,9 +2588,9 @@ def create_roi(subject_id, subjects_dir, v=True): this_out, os.path.join(subject_dir, 'mri', roivs_output[i][0:-4] + '.mgz')) if v == 2: - status = subprocess.call(mri_cmd, shell=True) + _ = subprocess.call(mri_cmd, shell=True) else: - status = subprocess.call( + _ = subprocess.call( mri_cmd, shell=True, stdout=FNULL, stderr=subprocess.STDOUT) # Create Gray Matter mask @@ -2628,16 +2627,16 @@ def create_wm_mask(subject_id, subjects_dir, v=True): v : Boolean Verbose mode """ - if v: + if v: # pragma: no cover iflogger.info(" > Create white matter mask") fs_dir = op.join(subjects_dir, subject_id) - if v: + if v: # pragma: no cover iflogger.info(" ... FreeSurfer dir: %s" % fs_dir) # load ribbon as basis for white matter mask - if v: + if v: # pragma: no cover iflogger.info(" > load ribbon") fsmask = ni.load(op.join(fs_dir, 'mri', 'ribbon.nii.gz')) fsmaskd = fsmask.get_data() @@ -2648,7 +2647,7 @@ def create_wm_mask(subject_id, subjects_dir, v=True): # FIXME understand when ribbon file has default value or has "aseg" value # extract right and left white matter - if v: + if v: # pragma: no cover iflogger.info(" > Extract right and left wm") # Ribbon labels by default if fsmaskd.max() == 120: @@ -2664,7 +2663,7 @@ def create_wm_mask(subject_id, subjects_dir, v=True): wmmask[idx_rh] = 1 # remove subcortical nuclei from white matter mask - if v: + if v: # pragma: no cover iflogger.info(" > Load aseg") aseg = ni.load(op.join(fs_dir, 'mri', 'aseg.nii.gz')) asegd = aseg.get_data() @@ -2705,7 +2704,7 @@ def create_wm_mask(subject_id, subjects_dir, v=True): (asegd == 49)) csfA[idx] = 1 - if v: + if v: # pragma: no cover iflogger.info(" > Save CSF mask") img = ni.Nifti1Image(csfA, aseg.get_affine(), aseg.get_header()) ni.save(img, op.join(fs_dir, 'mri', 'csf_mask.nii.gz')) @@ -2744,7 +2743,7 @@ def create_wm_mask(subject_id, subjects_dir, v=True): # would stop the fiber going to the segmented "brainstem" # grey nuclei, either with or without erosion - if v: + if v: # pragma: no cover iflogger.info(" > Grey nuclei, either with or without erosion") gr_ncl = np.zeros(asegd.shape) @@ -2764,7 +2763,7 @@ def create_wm_mask(subject_id, subjects_dir, v=True): gr_ncl[idx] = 1 # remove remaining structure, e.g. brainstem - if v: + if v: # pragma: no cover iflogger.info(" > Remove remaining structure, e.g. brainstem") remaining = np.zeros(asegd.shape) idx = np.where(asegd == 16) @@ -2774,7 +2773,7 @@ def create_wm_mask(subject_id, subjects_dir, v=True): idx = np.where((csfA != 0) | (csfB != 0) | (gr_ncl != 0) | (remaining != 0)) wmmask[idx] = 0 - if v: + if v: # pragma: no cover iflogger.info( " > Removing lateral ventricles and eroded grey nuclei and brainstem from white matter mask") @@ -2797,7 +2796,7 @@ def create_wm_mask(subject_id, subjects_dir, v=True): # output white matter mask. crop and move it afterwards wm_out = op.join(fs_dir, 'mri', 'fsmask_1mm.nii.gz') img = ni.Nifti1Image(wmmask, fsmask.get_affine(), fsmask.get_header()) - if v: + if v: # pragma: no cover iflogger.info(" > Save white matter mask: %s" % wm_out) ni.save(img, wm_out) del img