Skip to content

FIX: Replace deprecated HasTraits.get with trait_get #2534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions nipype/interfaces/base/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ def _list_outputs(self):
metadata = dict(name_source=lambda t: t is not None)
traits = self.inputs.traits(**metadata)
if traits:
outputs = self.output_spec().get()
outputs = self.output_spec().trait_get()
for name, trait_spec in list(traits.items()):
out_name = name
if trait_spec.output_name is not None:
Expand Down Expand Up @@ -1237,7 +1237,7 @@ class SEMLikeCommandLine(CommandLine):
"""

def _list_outputs(self):
outputs = self.output_spec().get()
outputs = self.output_spec().trait_get()
return self._outputs_from_inputs(outputs)

def _outputs_from_inputs(self, outputs):
Expand Down
25 changes: 13 additions & 12 deletions nipype/interfaces/base/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from builtins import str, bytes
from packaging.version import Version

from ...utils.misc import is_container
from ...utils.filemanip import md5, hash_infile, hash_timestamp, to_str
from .traits_extension import (
traits,
Expand Down Expand Up @@ -151,24 +150,26 @@ def _deprecated_warn(self, obj, name, old, new):
'%s' % trait_spec.new_name: new
})

def get(self, **kwargs):
def trait_get(self, **kwargs):
""" Returns traited class as a dict

Augments the trait get function to return a dictionary without
notification handles
"""
out = super(BaseTraitedSpec, self).get(**kwargs)
out = super(BaseTraitedSpec, self).trait_get(**kwargs)
out = self._clean_container(out, Undefined)
return out

get = trait_get

def get_traitsfree(self, **kwargs):
""" Returns traited class as a dict

Augments the trait get function to return a dictionary without
any traits. The dictionary does not contain any attributes that
were Undefined
"""
out = super(BaseTraitedSpec, self).get(**kwargs)
out = super(BaseTraitedSpec, self).trait_get(**kwargs)
out = self._clean_container(out, skipundefined=True)
return out

Expand All @@ -183,8 +184,8 @@ def _clean_container(self, objekt, undefinedval=None, skipundefined=False):
else:
if not skipundefined:
out[key] = undefinedval
elif (isinstance(objekt, TraitListObject) or isinstance(objekt, list)
or isinstance(objekt, tuple)):
elif (isinstance(objekt, TraitListObject) or isinstance(objekt, list) or
isinstance(objekt, tuple)):
out = []
for val in objekt:
if isdefined(val):
Expand Down Expand Up @@ -236,13 +237,13 @@ def get_hashval(self, hash_method=None):

list_withhash = []
list_nofilename = []
for name, val in sorted(self.get().items()):
for name, val in sorted(self.trait_get().items()):
if not isdefined(val) or self.has_metadata(name, "nohash", True):
# skip undefined traits and traits with nohash=True
continue

hash_files = (not self.has_metadata(name, "hash_files", False)
and not self.has_metadata(name, "name_source"))
hash_files = (not self.has_metadata(name, "hash_files", False) and
not self.has_metadata(name, "name_source"))
list_nofilename.append((name,
self._get_sorteddict(
val,
Expand Down Expand Up @@ -286,8 +287,8 @@ def _get_sorteddict(self,
else:
out = None
if isdefined(objekt):
if (hash_files and isinstance(objekt, (str, bytes))
and os.path.isfile(objekt)):
if (hash_files and isinstance(objekt, (str, bytes)) and
os.path.isfile(objekt)):
if hash_method is None:
hash_method = config.get('execution', 'hash_method')

Expand Down Expand Up @@ -341,7 +342,7 @@ def __deepcopy__(self, memo):
id_self = id(self)
if id_self in memo:
return memo[id_self]
dup_dict = deepcopy(self.get(), memo)
dup_dict = deepcopy(self.trait_get(), memo)
# access all keys
for key in self.copyable_trait_names():
if key in self.__dict__.keys():
Expand Down
14 changes: 7 additions & 7 deletions nipype/pipeline/engine/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@ def _get_inputs(self):
else:
output_name = info[1]
try:
output_value = results.outputs.get()[output_name]
except TypeError:
output_value = results.outputs.trait_get()[output_name]
except AttributeError:
output_value = results.outputs.dictcopy()[output_name]
logger.debug('output: %s', output_name)
try:
Expand Down Expand Up @@ -678,7 +678,7 @@ def _copyfiles_to_wd(self, execute=True, linksonly=False):
makedirs(outdir, exist_ok=True)

for info in self._interface._get_filecopy_info():
files = self.inputs.get().get(info['key'])
files = self.inputs.trait_get().get(info['key'])
if not isdefined(files) or not files:
continue

Expand Down Expand Up @@ -1084,7 +1084,7 @@ def inputs(self):
@property
def outputs(self):
if self._interface._outputs():
return Bunch(self._interface._outputs().get())
return Bunch(self._interface._outputs().trait_get())

def _make_nodes(self, cwd=None):
if cwd is None:
Expand All @@ -1109,7 +1109,7 @@ def _make_nodes(self, cwd=None):
name=nodename)
node.plugin_args = self.plugin_args
node.interface.inputs.trait_set(
**deepcopy(self._interface.inputs.get()))
**deepcopy(self._interface.inputs.trait_get()))
node.interface.resource_monitor = self._interface.resource_monitor
for field in self.iterfield:
if self.nested:
Expand Down Expand Up @@ -1153,7 +1153,7 @@ def _collate_results(self, nodes):
if not isdefined(values):
values = []
if nresult and nresult.outputs:
values.insert(i, nresult.outputs.get()[key])
values.insert(i, nresult.outputs.trait_get()[key])
else:
values.insert(i, None)
defined_vals = [isdefined(val) for val in values]
Expand Down Expand Up @@ -1201,7 +1201,7 @@ def num_subnodes(self):
return len(filename_to_list(getattr(self.inputs, self.iterfield[0])))

def _get_inputs(self):
old_inputs = self._inputs.get()
old_inputs = self._inputs.trait_get()
self._inputs = self._create_dynamic_traits(
self._interface.inputs, fields=self.iterfield)
self._inputs.trait_set(**old_inputs)
Expand Down
22 changes: 11 additions & 11 deletions nipype/pipeline/engine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ def write_report(node, report_type=None, is_mapnode=False):
['Hierarchy : %s' % node.fullname,
'Exec ID : %s' % node._id]),
write_rst_header('Original Inputs', level=1),
write_rst_dict(node.inputs.get()),
write_rst_dict(node.inputs.trait_get()),
]
with open(report_file, 'wt') as fp:
fp.write('\n'.join(lines))
return

lines = [
write_rst_header('Execution Inputs', level=1),
write_rst_dict(node.inputs.get()),
write_rst_dict(node.inputs.trait_get()),
]

result = node.result # Locally cache result
Expand All @@ -166,7 +166,7 @@ def write_report(node, report_type=None, is_mapnode=False):
if isinstance(outputs, Bunch):
lines.append(write_rst_dict(outputs.dictcopy()))
elif outputs:
lines.append(write_rst_dict(outputs.get()))
lines.append(write_rst_dict(outputs.trait_get()))

if is_mapnode:
lines.append(write_rst_header('Subnode reports', level=1))
Expand Down Expand Up @@ -238,8 +238,8 @@ def save_resultfile(result, cwd, name):
resultsfile = os.path.join(cwd, 'result_%s.pklz' % name)
if result.outputs:
try:
outputs = result.outputs.get()
except TypeError:
outputs = result.outputs.trait_get()
except AttributeError:
outputs = result.outputs.dictcopy() # outputs was a bunch
result.outputs.set(**modify_paths(outputs, relative=True, basedir=cwd))

Expand Down Expand Up @@ -293,8 +293,8 @@ def load_resultfile(path, name):
else:
if result.outputs:
try:
outputs = result.outputs.get()
except TypeError:
outputs = result.outputs.trait_get()
except AttributeError:
outputs = result.outputs.dictcopy() # outputs == Bunch
try:
result.outputs.set(
Expand Down Expand Up @@ -1391,19 +1391,19 @@ def clean_working_directory(outputs,
"""
if not outputs:
return
outputs_to_keep = list(outputs.get().keys())
outputs_to_keep = list(outputs.trait_get().keys())
if needed_outputs and \
str2bool(config['execution']['remove_unnecessary_outputs']):
outputs_to_keep = needed_outputs
# build a list of needed files
output_files = []
outputdict = outputs.get()
outputdict = outputs.trait_get()
for output in outputs_to_keep:
output_files.extend(walk_outputs(outputdict[output]))
needed_files = [path for path, type in output_files if type == 'f']
if str2bool(config['execution']['keep_inputs']):
input_files = []
inputdict = inputs.get()
inputdict = inputs.trait_get()
input_files.extend(walk_outputs(inputdict))
needed_files += [path for path, type in input_files if type == 'f']
for extra in [
Expand Down Expand Up @@ -1435,7 +1435,7 @@ def clean_working_directory(outputs,
else:
if not str2bool(config['execution']['keep_inputs']):
input_files = []
inputdict = inputs.get()
inputdict = inputs.trait_get()
input_files.extend(walk_outputs(inputdict))
input_files = [path for path, type in input_files if type == 'f']
for f in walk_files(cwd):
Expand Down