Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
elif type_name == PARAMETER_GROUP:
if type(instance) in (NNGroupNode,
ResultGroup,
ConfigGroup,
DerivedParameterGroup):
raise TypeError('You cannot add a `%s` type of group under parameters' %
str(type(instance)))
elif type_name == CONFIG_GROUP:
if type(instance) in (NNGroupNode,
ParameterGroup,
ResultGroup,
DerivedParameterGroup):
raise TypeError('You cannot add a `%s` type of group under config' %
str(type(instance)))
elif type_name == DERIVED_PARAMETER_GROUP:
if type(instance) in (NNGroupNode,
ParameterGroup,
ConfigGroup,
ResultGroup):
raise TypeError('You cannot add a `%s` type of group under derived '
'parameters' % str(type(instance)))
elif type_name == GROUP:
if type(instance) in (ResultGroup,
ParameterGroup,
ConfigGroup,
DerivedParameterGroup):
raise TypeError('You cannot add a `%s` type of group under other data' %
str(type(instance)))
else:
raise RuntimeError('You shall not pass!')
self._set_details_tree_node(parent_node, name, instance)
def __setattr__(self, key, value):
if key.startswith('_'):
# We set a private attribute
super(NNGroupNode, self).__setattr__(key, value)
elif hasattr(self.__class__, key):
# If we set a property we need this work around here:
python_property = getattr(self.__class__, key)
if python_property.fset is None:
raise AttributeError('%s is read only!' % key)
else:
python_property.fset(self, value)
elif isinstance(value, (NNGroupNode, NNLeafNode)):
old_name = value.v_full_name
try:
if self.v_root.f_contains(value, shortcuts=False, with_links=False):
self.f_add_link(key, value)
else:
name = value.v_full_name
if name == '':
value._rename(key)
if python_property.fset is None:
raise AttributeError('%s is read only!' % key)
else:
python_property.fset(self, value)
elif isinstance(value, (NNGroupNode, NNLeafNode)):
old_name = value.v_full_name
try:
if self.v_root.f_contains(value, shortcuts=False, with_links=False):
self.f_add_link(key, value)
else:
name = value.v_full_name
if name == '':
value._rename(key)
elif name != key:
value._rename(key + '.' + name)
if isinstance(value, NNGroupNode):
self.f_add_group(value)
else:
self.f_add_leaf(value)
except:
value._set_details(old_name)
raise
elif self.v_root.v_lazy_adding:
if value is new_group:
self.f_add_group(key)
else:
if not isinstance(value, tuple):
value = (value,)
self.f_add_leaf(key, *value)
else:
instance = self.f_get(key)
instance.f_set(value)
onto the constructor of the result.
Adds the full name of the current node as prefix to the name of the result.
If current node is a single run (root) adds the prefix `'results.runs.run_08%d%'` to the
full name where `'08%d'` is replaced by the index of the current run.
"""
return self._nn_interface._add_generic(self, type_name=RESULT,
group_type_name=RESULT_GROUP,
args=args, kwargs=kwargs)
f_ares = f_add_result # Abbreviation of the function
class DerivedParameterGroup(NNGroupNode):
"""Group node in your trajectory, hanging below `traj.derived_parameters`.
You can add other groups or parameters to it.
"""
__slots__ = ()
def f_add_derived_parameter_group(self, *args, **kwargs):
"""Adds an empty derived parameter group under the current node.
Adds the full name of the current node as prefix to the name of the group.
If current node is a single run (root) adds the prefix `'derived_parameters.runs.run_08%d%'`
to the full name where `'08%d'` is replaced by the index of the current run.
The `name` can also contain subgroups separated via colons, for example:
def __setattr__(self, key, value):
if key.startswith('_'):
# We set a private attribute
super(NNGroupNode, self).__setattr__(key, value)
elif hasattr(self.__class__, key):
# If we set a property we need this work around here:
python_property = getattr(self.__class__, key)
if python_property.fset is None:
raise AttributeError('%s is read only!' % key)
else:
python_property.fset(self, value)
elif isinstance(value, (NNGroupNode, NNLeafNode)):
old_name = value.v_full_name
try:
if self.v_root.f_contains(value, shortcuts=False, with_links=False):
self.f_add_link(key, value)
else:
name = value.v_full_name
if name == '':
value._rename(key)
elif name != key:
value._rename(key + '.' + name)
if isinstance(value, NNGroupNode):
self.f_add_group(value)
else:
self.f_add_leaf(value)
except:
value._set_details(old_name)
kwargs = {}
full_name = self._make_full_name(parent_node.v_full_name, name)
if instance is None:
if constructor is None:
if type_name == RESULT_GROUP:
constructor = ResultGroup
elif type_name == PARAMETER_GROUP:
constructor = ParameterGroup
elif type_name == CONFIG_GROUP:
constructor = ConfigGroup
elif type_name == DERIVED_PARAMETER_GROUP:
constructor = DerivedParameterGroup
elif type_name == GROUP:
constructor = NNGroupNode
else:
raise RuntimeError('You shall not pass!')
instance = self._root_instance._construct_instance(constructor, full_name,
*args, **kwargs)
else:
instance._rename(full_name)
# Check if someone tries to add a particular standard group to a branch where
# it does not belong:
if type_name == RESULT_GROUP:
if type(instance) in (NNGroupNode,
ParameterGroup,
ConfigGroup,
DerivedParameterGroup):
raise TypeError('You cannot add a `%s` type of group under results' %
str(type(instance)))
elif type_name == PARAMETER_GROUP:
(non-keyword!):
>>> traj.f_add_parameter(PickleParameter,'group1.group2.myparam', data=42, comment='Example!')
The full name of the current node is added as a prefix to the given parameter name.
If the current node is the trajectory the prefix `'parameters'` is added to the name.
"""
return self._nn_interface._add_generic(self, type_name=PARAMETER,
group_type_name=PARAMETER_GROUP,
args=args, kwargs=kwargs)
f_apar = f_add_parameter # Abbreviation of the function
class ResultGroup(NNGroupNode):
"""Group node in your trajectory, hanging below `traj.results`.
You can add other groups or results to it.
"""
__slots__ = ()
def f_add_result_group(self, *args, **kwargs):
"""Adds an empty result group under the current node.
Adds the full name of the current node as prefix to the name of the group.
If current node is a single run (root) adds the prefix `'results.runs.run_08%d%'` to the
full name where `'08%d'` is replaced by the index of the current run.
The `name` can also contain subgroups separated via colons, for example:
else:
raise RuntimeError('You shall not pass!')
instance = self._root_instance._construct_instance(constructor, full_name,
*args, **kwargs)
else:
# Check if someone tries to add a particular standard group to a branch where
# it does not belong:
if type_name == RESULT_GROUP:
if type(instance) in (NNGroupNode,
ParameterGroup,
ConfigGroup,
DerivedParameterGroup):
raise TypeError('You cannot add a `%s` type of group under results' %
str(type(instance)))
elif type_name == PARAMETER_GROUP:
if type(instance) in (NNGroupNode,
ResultGroup,
ConfigGroup,
DerivedParameterGroup):
raise TypeError('You cannot add a `%s` type of group under parameters' %
str(type(instance)))
elif type_name == CONFIG_GROUP:
if type(instance) in (NNGroupNode,
ParameterGroup,
ResultGroup,
DerivedParameterGroup):
raise TypeError('You cannot add a `%s` type of group under config' %
str(type(instance)))
elif type_name == DERIVED_PARAMETER_GROUP:
if type(instance) in (NNGroupNode,
ParameterGroup,
ConfigGroup,
if kwargs is None:
kwargs = {}
if instance is None:
if constructor is None:
if type_name == RESULT_GROUP:
constructor = ResultGroup
elif type_name == PARAMETER_GROUP:
constructor = ParameterGroup
elif type_name == CONFIG_GROUP:
constructor = ConfigGroup
elif type_name == DERIVED_PARAMETER_GROUP:
constructor = DerivedParameterGroup
elif type_name == GROUP:
constructor = NNGroupNode
else:
raise RuntimeError('You shall not pass!')
instance = self._root_instance._construct_instance(constructor, full_name,
*args, **kwargs)
else:
# Check if someone tries to add a particular standard group to a branch where
# it does not belong:
if type_name == RESULT_GROUP:
if type(instance) in (NNGroupNode,
ParameterGroup,
ConfigGroup,
DerivedParameterGroup):
raise TypeError('You cannot add a `%s` type of group under results' %
str(type(instance)))
elif type_name == PARAMETER_GROUP:
if type(instance) in (NNGroupNode,