From 53a84e3dfd60997f2427721384c9bd8cadb33271 Mon Sep 17 00:00:00 2001 From: Robert Smith Date: Wed, 18 Sep 2024 11:32:41 +1000 Subject: [PATCH] Python: Pylint fix consider-using-generator --- bin/for_each | 6 +++--- bin/population_template | 4 ++-- bin/responsemean | 2 +- lib/mrtrix3/_5ttgen/hsvs.py | 2 +- lib/mrtrix3/fsl.py | 2 +- lib/mrtrix3/matrix.py | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bin/for_each b/bin/for_each index 7bc111badd..fb41d7b3f7 100755 --- a/bin/for_each +++ b/bin/for_each @@ -204,12 +204,12 @@ def execute(): #pylint: disable=unused-variable parallel = app.NUM_THREADS is not None and app.NUM_THREADS > 1 def progress_string(): - text = str(sum([ 1 if job.returncode is not None else 0 for job in jobs ])) + \ + text = str(sum(1 if job.returncode is not None else 0 for job in jobs)) + \ '/' + \ str(len(jobs)) + \ ' jobs completed ' + \ ('across ' + str(app.NUM_THREADS) + ' threads' if parallel else 'sequentially') - fail_count = sum([ 1 if job.returncode else 0 for job in jobs ]) + fail_count = sum(bool(job.returncode) for job in jobs) if fail_count: text += ' (' + str(fail_count) + ' errors)' return text @@ -260,7 +260,7 @@ def execute(): #pylint: disable=unused-variable progress.done() assert all(job.returncode is not None for job in jobs) - fail_count = sum([ 1 if job.returncode else 0 for job in jobs ]) + fail_count = sum(bool(job.returncode) for job in jobs) if fail_count: app.warn(str(fail_count) + ' of ' + str(len(jobs)) + ' jobs did not complete successfully') if fail_count > 1: diff --git a/bin/population_template b/bin/population_template index 6842c88e81..468607cee5 100755 --- a/bin/population_template +++ b/bin/population_template @@ -195,10 +195,10 @@ def aggregate(inputs, output, contrast_idx, mode, force=True): elif mode == 'weighted_mean': weights = [inp.aggregation_weight for inp in inputs] assert not any(w is None for w in weights), weights - wsum = sum([float(w) for w in weights]) - cmd = ['mrcalc'] + wsum = sum(map(float, weights)) if wsum <= 0: raise MRtrixError("the sum of aggregetion weights has to be positive") + cmd = ['mrcalc'] for weight, image in zip(weights, images): if float(weight) != 0: cmd += [image, weight, '-mult'] + (['-add'] if len(cmd) > 1 else []) diff --git a/bin/responsemean b/bin/responsemean index 0fcca853f6..5f5c3c06b4 100755 --- a/bin/responsemean +++ b/bin/responsemean @@ -56,7 +56,7 @@ def execute(): #pylint: disable=unused-variable # New approach: Calculate a multiplier to use for each subject, based on the geometric mean # scaling factor required to bring the subject toward the group mean l=0 terms (across shells) - mean_lzero_terms = [ sum([ subject[row][0] for subject in data ])/len(data) for row in range(len(data[0])) ] + mean_lzero_terms = [ sum(subject[row][0] for subject in data)/len(data) for row in range(len(data[0])) ] app.debug('Mean l=0 terms: ' + str(mean_lzero_terms)) weighted_sum_coeffs = [[0.0] * len(data[0][0]) for _ in range(len(data[0]))] #pylint: disable=unused-variable diff --git a/lib/mrtrix3/_5ttgen/hsvs.py b/lib/mrtrix3/_5ttgen/hsvs.py index 5c1ae3d285..8b0407cabc 100644 --- a/lib/mrtrix3/_5ttgen/hsvs.py +++ b/lib/mrtrix3/_5ttgen/hsvs.py @@ -437,7 +437,7 @@ def execute(): #pylint: disable=unused-variable run.command('mesh2voxel ' + bs_smoothed_mesh_path + ' ' + template_image + ' brain_stem.mif') app.cleanup(bs_smoothed_mesh_path) progress.increment() - fourthventricle_zmin = min([ int(line.split()[2]) for line in run.command('maskdump 4th-Ventricle.mif')[0].splitlines() ]) + fourthventricle_zmin = min(int(line.split()[2]) for line in run.command('maskdump 4th-Ventricle.mif')[0].splitlines()) if fourthventricle_zmin: bs_cropmask_path = 'brain_stem_crop.mif' run.command('mredit brain_stem.mif - ' + ' '.join([ '-plane 2 ' + str(index) + ' 0' for index in range(0, fourthventricle_zmin) ]) + ' | ' diff --git a/lib/mrtrix3/fsl.py b/lib/mrtrix3/fsl.py index d0330085c2..f7a0739f52 100644 --- a/lib/mrtrix3/fsl.py +++ b/lib/mrtrix3/fsl.py @@ -32,7 +32,7 @@ def check_first(prefix, structures): #pylint: disable=unused-variable from mrtrix3 import app, path #pylint: disable=import-outside-toplevel vtk_files = [ prefix + '-' + struct + '_first.vtk' for struct in structures ] - existing_file_count = sum([ os.path.exists(filename) for filename in vtk_files ]) + existing_file_count = sum(os.path.exists(filename) for filename in vtk_files) if existing_file_count != len(vtk_files): if 'SGE_ROOT' in os.environ and os.environ['SGE_ROOT']: app.console('FSL FIRST job may have been run via SGE; awaiting completion') diff --git a/lib/mrtrix3/matrix.py b/lib/mrtrix3/matrix.py index 64dc1f8ef2..0aa648d6ba 100644 --- a/lib/mrtrix3/matrix.py +++ b/lib/mrtrix3/matrix.py @@ -44,7 +44,7 @@ def dot(input_a, input_b): #pylint: disable=unused-variable raise MRtrixError('Both inputs must be either 1D vectors or 2D matrices') if len(input_a) != len(input_b): raise MRtrixError('Dimension mismatch (' + str(len(input_a)) + ' vs. ' + str(len(input_b)) + ')') - return sum([ x*y for x,y in zip(input_a, input_b) ]) + return sum(x*y for x,y in zip(input_a, input_b))