Skip to content

Commit

Permalink
Merge pull request #3005 from MRtrix3/python_generators
Browse files Browse the repository at this point in the history
Python: Pylint fix consider-using-generator
  • Loading branch information
Lestropie authored Nov 20, 2024
2 parents 50a5e04 + 53a84e3 commit 5c45774
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions bin/for_each
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions bin/population_template
Original file line number Diff line number Diff line change
Expand Up @@ -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 [])
Expand Down
2 changes: 1 addition & 1 deletion bin/responsemean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mrtrix3/_5ttgen/hsvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) ]) + ' | '
Expand Down
2 changes: 1 addition & 1 deletion lib/mrtrix3/fsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion lib/mrtrix3/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))



Expand Down

0 comments on commit 5c45774

Please sign in to comment.