diff --git a/nipype/algorithms/modelgen.py b/nipype/algorithms/modelgen.py index 17e2d2f65f..38b5e47549 100644 --- a/nipype/algorithms/modelgen.py +++ b/nipype/algorithms/modelgen.py @@ -161,7 +161,7 @@ def bids_gen_info( for bids_event_file in bids_event_files: with open(bids_event_file) as f: f_events = csv.DictReader(f, skipinitialspace=True, delimiter="\t") - events = [{k: v for k, v in row.items()} for row in f_events] + events = list(f_events) if not condition_column: condition_column = "_trial_type" for i in events: diff --git a/nipype/algorithms/tests/test_CompCor.py b/nipype/algorithms/tests/test_CompCor.py index 60e1ba34b2..f506ded628 100644 --- a/nipype/algorithms/tests/test_CompCor.py +++ b/nipype/algorithms/tests/test_CompCor.py @@ -287,9 +287,9 @@ def run_cc( components_metadata = [ line.rstrip().split("\t") for line in metadata_file ] - components_metadata = { - i: j for i, j in zip(components_metadata[0], components_metadata[1]) - } + components_metadata = dict( + zip(components_metadata[0], components_metadata[1]) + ) assert components_metadata == expected_metadata return ccresult diff --git a/nipype/interfaces/ants/segmentation.py b/nipype/interfaces/ants/segmentation.py index f4b60ec11c..3c87b71975 100644 --- a/nipype/interfaces/ants/segmentation.py +++ b/nipype/interfaces/ants/segmentation.py @@ -194,7 +194,7 @@ def _format_arg(self, opt, spec, val): priors_paths[0] % i for i in range(1, n_classes + 1) ] - if not all([os.path.exists(p) for p in priors_paths]): + if not all(os.path.exists(p) for p in priors_paths): raise FileNotFoundError( "One or more prior images do not exist: " "%s." % ", ".join(priors_paths) diff --git a/nipype/interfaces/base/core.py b/nipype/interfaces/base/core.py index 02c4a1ab7e..35764d5397 100644 --- a/nipype/interfaces/base/core.py +++ b/nipype/interfaces/base/core.py @@ -486,7 +486,7 @@ def load_inputs_from_json(self, json_file, overwrite=True): if not overwrite: def_inputs = list(self.inputs.get_traitsfree().keys()) - new_inputs = list(set(list(inputs_dict.keys())) - set(def_inputs)) + new_inputs = set(inputs_dict) - set(def_inputs) for key in new_inputs: if hasattr(self.inputs, key): setattr(self.inputs, key, inputs_dict[key]) diff --git a/nipype/interfaces/cmtk/cmtk.py b/nipype/interfaces/cmtk/cmtk.py index baf1bccaae..342856cb18 100644 --- a/nipype/interfaces/cmtk/cmtk.py +++ b/nipype/interfaces/cmtk/cmtk.py @@ -248,7 +248,7 @@ def cmat( axis=1, ) ) - G.nodes[int(u)]["dn_position"] = tuple([xyz[0], xyz[2], -xyz[1]]) + G.nodes[int(u)]["dn_position"] = (xyz[0], xyz[2], -xyz[1]) if intersections: iflogger.info("Filtering tractography from intersections") @@ -1070,7 +1070,7 @@ def create_nodes(roi_file, resolution_network_file, out_filename): np.where(np.flipud(roiData) == int(d["dn_correspondence_id"])), axis=1 ) ) - G.nodes[int(u)]["dn_position"] = tuple([xyz[0], xyz[2], -xyz[1]]) + G.nodes[int(u)]["dn_position"] = (xyz[0], xyz[2], -xyz[1]) with open(out_filename, 'wb') as f: pickle.dump(G, f, pickle.HIGHEST_PROTOCOL) return out_filename diff --git a/nipype/interfaces/diffusion_toolkit/dti.py b/nipype/interfaces/diffusion_toolkit/dti.py index 37a76ab6f0..fa031799e3 100644 --- a/nipype/interfaces/diffusion_toolkit/dti.py +++ b/nipype/interfaces/diffusion_toolkit/dti.py @@ -97,7 +97,7 @@ class DTIRecon(CommandLine): def _create_gradient_matrix(self, bvecs_file, bvals_file): _gradient_matrix_file = "gradient_matrix.txt" with open(bvals_file) as fbvals: - bvals = [val for val in re.split(r"\s+", fbvals.readline().strip())] + bvals = fbvals.readline().strip().split() with open(bvecs_file) as fbvecs: bvecs_x = fbvecs.readline().split() bvecs_y = fbvecs.readline().split() diff --git a/nipype/interfaces/diffusion_toolkit/odf.py b/nipype/interfaces/diffusion_toolkit/odf.py index 3b30cc7c69..00f86a322c 100644 --- a/nipype/interfaces/diffusion_toolkit/odf.py +++ b/nipype/interfaces/diffusion_toolkit/odf.py @@ -98,12 +98,12 @@ class HARDIMat(CommandLine): def _create_gradient_matrix(self, bvecs_file, bvals_file): _gradient_matrix_file = "gradient_matrix.txt" - bvals = [val for val in re.split(r"\s+", open(bvals_file).readline().strip())] - bvecs_f = open(bvecs_file) - bvecs_x = [val for val in re.split(r"\s+", bvecs_f.readline().strip())] - bvecs_y = [val for val in re.split(r"\s+", bvecs_f.readline().strip())] - bvecs_z = [val for val in re.split(r"\s+", bvecs_f.readline().strip())] - bvecs_f.close() + with open(bvals_file) as bvals_f: + bvals = bvals_f.readline().strip().split() + with open(bvecs_file) as bvecs_f: + bvecs_x = bvecs_f.readline().strip().split() + bvecs_y = bvecs_f.readline().strip().split() + bvecs_z = bvecs_f.readline().strip().split() gradient_matrix_f = open(_gradient_matrix_file, "w") for i in range(len(bvals)): if int(bvals[i]) == 0: diff --git a/nipype/interfaces/freesurfer/preprocess.py b/nipype/interfaces/freesurfer/preprocess.py index 80b8703c31..5b2fd19a0b 100644 --- a/nipype/interfaces/freesurfer/preprocess.py +++ b/nipype/interfaces/freesurfer/preprocess.py @@ -692,11 +692,11 @@ def _get_runs(self): if self.inputs.seq_list: if self.inputs.ignore_single_slice: if (int(s[8]) > 1) and any( - [s[12].startswith(sn) for sn in self.inputs.seq_list] + s[12].startswith(sn) for sn in self.inputs.seq_list ): runs.append(int(s[2])) else: - if any([s[12].startswith(sn) for sn in self.inputs.seq_list]): + if any(s[12].startswith(sn) for sn in self.inputs.seq_list): runs.append(int(s[2])) else: runs.append(int(s[2])) diff --git a/nipype/interfaces/fsl/model.py b/nipype/interfaces/fsl/model.py index 70ce3d7439..4326178f41 100644 --- a/nipype/interfaces/fsl/model.py +++ b/nipype/interfaces/fsl/model.py @@ -1503,8 +1503,8 @@ def _run_interface(self, runtime): regs = sorted(self.inputs.regressors.keys()) nwaves = len(regs) npoints = len(self.inputs.regressors[regs[0]]) - ntcons = sum([1 for con in self.inputs.contrasts if con[1] == "T"]) - nfcons = sum([1 for con in self.inputs.contrasts if con[1] == "F"]) + ntcons = sum(1 for con in self.inputs.contrasts if con[1] == "T") + nfcons = sum(1 for con in self.inputs.contrasts if con[1] == "F") # write mat file mat_txt = ["/NumWaves %d" % nwaves, "/NumPoints %d" % npoints] ppheights = [] @@ -1591,7 +1591,7 @@ def _run_interface(self, runtime): def _list_outputs(self): outputs = self._outputs().get() - nfcons = sum([1 for con in self.inputs.contrasts if con[1] == "F"]) + nfcons = sum(1 for con in self.inputs.contrasts if con[1] == "F") for field in list(outputs.keys()): if ("fts" in field) and (nfcons == 0): continue diff --git a/nipype/interfaces/io.py b/nipype/interfaces/io.py index 34dbc1b461..ce1e73ca05 100644 --- a/nipype/interfaces/io.py +++ b/nipype/interfaces/io.py @@ -942,7 +942,7 @@ def _list_outputs(self): # get list of all files in s3 bucket conn = boto.connect_s3(anon=self.inputs.anon) bkt = conn.get_bucket(self.inputs.bucket) - bkt_files = list(k.key for k in bkt.list(prefix=self.inputs.bucket_path)) + bkt_files = [k.key for k in bkt.list(prefix=self.inputs.bucket_path)] # keys are outfields, args are template args for the outfield for key, args in list(self.inputs.template_args.items()): @@ -1022,7 +1022,7 @@ def _list_outputs(self): if self.inputs.sort_filelist: outfiles = human_order_sorted(outfiles) outputs[key].append(simplify_list(outfiles)) - if any([val is None for val in outputs[key]]): + if None in outputs[key]: outputs[key] = [] if len(outputs[key]) == 0: outputs[key] = None @@ -1297,7 +1297,7 @@ def _list_outputs(self): if self.inputs.drop_blank_outputs: outputs[key] = [x for x in outputs[key] if x is not None] else: - if any([val is None for val in outputs[key]]): + if None in outputs[key]: outputs[key] = [] if len(outputs[key]) == 0: outputs[key] = None @@ -2302,7 +2302,7 @@ def __init__(self, input_names, **inputs): super().__init__(**inputs) self._input_names = ensure_list(input_names) - add_traits(self.inputs, [name for name in self._input_names]) + add_traits(self.inputs, self._input_names) def _list_outputs(self): """Execute this module.""" @@ -2364,7 +2364,7 @@ def __init__(self, input_names, **inputs): super().__init__(**inputs) self._input_names = ensure_list(input_names) - add_traits(self.inputs, [name for name in self._input_names]) + add_traits(self.inputs, self._input_names) def _list_outputs(self): """Execute this module.""" @@ -2642,7 +2642,7 @@ def _list_outputs(self): outputs[key].append(self._get_files_over_ssh(filledtemplate)) # disclude where there was any invalid matches - if any([val is None for val in outputs[key]]): + if None in outputs[key]: outputs[key] = [] # no outputs is None, not empty list diff --git a/nipype/interfaces/spm/model.py b/nipype/interfaces/spm/model.py index 62bf4447ea..d81937dc9d 100644 --- a/nipype/interfaces/spm/model.py +++ b/nipype/interfaces/spm/model.py @@ -159,7 +159,7 @@ def _parse_inputs(self): """validate spm realign options if set to None ignore""" einputs = super()._parse_inputs(skip=("mask_threshold", "flags")) if isdefined(self.inputs.flags): - einputs[0].update({flag: val for (flag, val) in self.inputs.flags.items()}) + einputs[0].update(self.inputs.flags) for sessinfo in einputs[0]["sess"]: sessinfo["scans"] = scans_for_fnames( ensure_list(sessinfo["scans"]), keep4d=False @@ -309,7 +309,7 @@ def _parse_inputs(self): """validate spm realign options if set to None ignore""" einputs = super()._parse_inputs(skip=("flags")) if isdefined(self.inputs.flags): - einputs[0].update({flag: val for (flag, val) in self.inputs.flags.items()}) + einputs[0].update(self.inputs.flags) return einputs def _list_outputs(self): diff --git a/nipype/interfaces/utility/wrappers.py b/nipype/interfaces/utility/wrappers.py index dffe98c862..e521f14a76 100644 --- a/nipype/interfaces/utility/wrappers.py +++ b/nipype/interfaces/utility/wrappers.py @@ -95,11 +95,9 @@ def __init__( self.inputs.on_trait_change(self._set_function_string, "function_str") self._input_names = ensure_list(input_names) self._output_names = ensure_list(output_names) - add_traits(self.inputs, [name for name in self._input_names]) + add_traits(self.inputs, self._input_names) self.imports = imports - self._out = {} - for name in self._output_names: - self._out[name] = None + self._out = {name: None for name in self._output_names} def _set_function_string(self, obj, name, old, new): if name == "function_str": diff --git a/nipype/pipeline/engine/nodes.py b/nipype/pipeline/engine/nodes.py index 3756d00ce8..5f357abb00 100644 --- a/nipype/pipeline/engine/nodes.py +++ b/nipype/pipeline/engine/nodes.py @@ -240,7 +240,7 @@ def needed_outputs(self): @needed_outputs.setter def needed_outputs(self, new_outputs): """Needed outputs changes the hash, refresh if changed""" - new_outputs = sorted(list(set(new_outputs or []))) + new_outputs = sorted(set(new_outputs or [])) if new_outputs != self._needed_outputs: # Reset hash self._hashvalue = None @@ -1283,7 +1283,7 @@ def _collate_results(self, nodes): ) setattr(finalresult.outputs, key, values) - if returncode and any([code is not None for code in returncode]): + if returncode and any(code is not None for code in returncode): msg = [] for i, code in enumerate(returncode): if code is not None: diff --git a/nipype/pipeline/engine/utils.py b/nipype/pipeline/engine/utils.py index 3601290cd9..e0018e607d 100644 --- a/nipype/pipeline/engine/utils.py +++ b/nipype/pipeline/engine/utils.py @@ -1046,7 +1046,7 @@ def make_field_func(*pair): logger.debug("node: %s iterables: %s", inode, iterables) # collect the subnodes to expand - subnodes = [s for s in dfs_preorder(graph_in, inode)] + subnodes = list(dfs_preorder(graph_in, inode)) prior_prefix = [re.findall(r"\.(.)I", s._id) for s in subnodes if s._id] prior_prefix = sorted([l for item in prior_prefix for l in item]) if not prior_prefix: @@ -1482,11 +1482,8 @@ def clean_working_directory( files2remove = [] if str2bool(config["execution"]["remove_unnecessary_outputs"]): for f in walk_files(cwd): - if f not in needed_files: - if not needed_dirs: - files2remove.append(f) - elif not any([f.startswith(dname) for dname in needed_dirs]): - files2remove.append(f) + if f not in needed_files and not f.startswith(tuple(needed_dirs)): + files2remove.append(f) else: if not str2bool(config["execution"]["keep_inputs"]): input_files = { diff --git a/nipype/pipeline/engine/workflows.py b/nipype/pipeline/engine/workflows.py index 97bd8cce9e..dc0dc0b6cc 100644 --- a/nipype/pipeline/engine/workflows.py +++ b/nipype/pipeline/engine/workflows.py @@ -191,10 +191,8 @@ def connect(self, *args, **kwargs): and ( ".io" in str(destnode._interface.__class__) or any( - [ - ".io" in str(val) - for val in destnode._interface.__class__.__bases__ - ] + ".io" in str(val) + for val in destnode._interface.__class__.__bases__ ) ) ): @@ -205,10 +203,8 @@ def connect(self, *args, **kwargs): and ( ".io" in str(srcnode._interface.__class__) or any( - [ - ".io" in str(val) - for val in srcnode._interface.__class__.__bases__ - ] + ".io" in str(val) + for val in srcnode._interface.__class__.__bases__ ) ) ): diff --git a/nipype/pipeline/plugins/base.py b/nipype/pipeline/plugins/base.py index 1e2106304d..1ea62c890e 100644 --- a/nipype/pipeline/plugins/base.py +++ b/nipype/pipeline/plugins/base.py @@ -455,7 +455,7 @@ def _remove_node_deps(self, jobid, crashfile, graph): dfs_preorder = nx.dfs_preorder except AttributeError: dfs_preorder = nx.dfs_preorder_nodes - subnodes = [s for s in dfs_preorder(graph, self.procs[jobid])] + subnodes = list(dfs_preorder(graph, self.procs[jobid])) for node in subnodes: idx = self.procs.index(node) self.proc_done[idx] = True diff --git a/nipype/pipeline/plugins/linear.py b/nipype/pipeline/plugins/linear.py index 226a25dbd4..93029ee1b9 100644 --- a/nipype/pipeline/plugins/linear.py +++ b/nipype/pipeline/plugins/linear.py @@ -50,7 +50,7 @@ def run(self, graph, config, updatehash=False): # node might fail crashfile = report_crash(node) # remove dependencies from queue - subnodes = [s for s in dfs_preorder(graph, node)] + subnodes = list(dfs_preorder(graph, node)) notrun.append( {"node": node, "dependents": subnodes, "crashfile": crashfile} ) diff --git a/nipype/scripts/instance.py b/nipype/scripts/instance.py index f8712cf367..d9cc425e9a 100644 --- a/nipype/scripts/instance.py +++ b/nipype/scripts/instance.py @@ -39,7 +39,7 @@ def list_interfaces(module): the given module. """ iface_names = [] - for k, v in sorted(list(module.__dict__.items())): + for k, v in sorted(module.__dict__.items()): if inspect.isclass(v) and issubclass(v, Interface): iface_names.append(k) return iface_names diff --git a/nipype/utils/docparse.py b/nipype/utils/docparse.py index c56d847738..7731329265 100644 --- a/nipype/utils/docparse.py +++ b/nipype/utils/docparse.py @@ -283,7 +283,7 @@ def _parse_doc(doc, style=["--"]): flag = [ item for i, item in enumerate(linelist) - if i < 2 and any([item.startswith(s) for s in style]) and len(item) > 1 + if i < 2 and item.startswith(tuple(style)) and len(item) > 1 ] if flag: if len(flag) == 1: diff --git a/nipype/utils/filemanip.py b/nipype/utils/filemanip.py index e97b93c3f9..6aa0c5e74c 100644 --- a/nipype/utils/filemanip.py +++ b/nipype/utils/filemanip.py @@ -499,7 +499,7 @@ def ensure_list(filename): elif isinstance(filename, list): return filename elif is_container(filename): - return [x for x in filename] + return list(filename) else: return None diff --git a/nipype/utils/misc.py b/nipype/utils/misc.py index 95f4b408c2..15b3fb6b7a 100644 --- a/nipype/utils/misc.py +++ b/nipype/utils/misc.py @@ -53,7 +53,7 @@ def trim(docstring, marker=None): if ( marker is not None and stripped - and all([s == stripped[0] for s in stripped]) + and all(s == stripped[0] for s in stripped) and stripped[0] not in [":"] ): line = line.replace(stripped[0], marker) diff --git a/nipype/utils/nipype_cmd.py b/nipype/utils/nipype_cmd.py index 38ae1a101b..eabe84d4c3 100644 --- a/nipype/utils/nipype_cmd.py +++ b/nipype/utils/nipype_cmd.py @@ -13,7 +13,7 @@ def listClasses(module=None): __import__(module) pkg = sys.modules[module] print("Available Interfaces:") - for k, v in sorted(list(pkg.__dict__.items())): + for k, v in sorted(pkg.__dict__.items()): if inspect.isclass(v) and issubclass(v, Interface): print("\t%s" % k)