def _slicer(self, slices, isNavigation=None): array_slices = self._get_array_slices(slices, isNavigation) _obj = self._deepcopy_with_new_data(self.data[array_slices]) for slice_, axis in zip(array_slices, _obj.axes_manager._axes): if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis._slice_me(slice_) else: _obj._remove_axis(axis.index_in_axes_manager) if hasattr(self, "_additional_slicing_targets"): for ta in self._additional_slicing_targets: try: t = attrgetter(ta)(self) if hasattr(t, '_slicer'): attrsetter( _obj, ta, t._slicer( slices, isNavigation)) except AttributeError: pass _obj.get_dimensions_from_data() return _obj
def test_dummy(self): d = self.dummy d.multiply() attrsetter(d, 'another.name', 'New dummy') assert d.another.name == 'New dummy' d.another.multiply() attrsetter(d, 'another.another.name', 'super New dummy') assert d.another.another.name == 'super New dummy'
def test_dummy(self): d = self.dummy d.multiply() attrsetter(d, 'another.name', 'New dummy') nt.assert_equal(d.another.name, 'New dummy') d.another.multiply() attrsetter(d, 'another.another.name', 'super New dummy') nt.assert_equal(d.another.another.name, 'super New dummy')
def _slicer(self, slices, isNavigation=None, out=None): if self.axes_manager._ragged and not isNavigation: raise RuntimeError("`isig` is not supported for ragged signal.") array_slices = self._get_array_slices(slices, isNavigation) new_data = self.data[array_slices] if (self.ragged and new_data.dtype != np.dtype(object) and isinstance(new_data, np.ndarray)): # Numpy will convert the array to non-ragged, for consistency, # we make a ragged array with only one item data = new_data.copy() new_data = np.empty((1, ), dtype=object) new_data[0] = data if out is None: _obj = self._deepcopy_with_new_data(new_data, copy_variance=True) _to_remove = [] for slice_, axis in zip(array_slices, _obj.axes_manager._axes): if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis._slice_me(slice_) else: _to_remove.append(axis.index_in_axes_manager) _obj._remove_axis(_to_remove) else: out.data = new_data _obj = out i = 0 for slice_, axis_src in zip(array_slices, self.axes_manager._axes): axis_src = axis_src.copy() if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis_src._slice_me(slice_) axis_dst = out.axes_manager._axes[i] i += 1 axis_dst.update_from(axis_src, attributes=("scale", "offset", "size")) if hasattr(self, "_additional_slicing_targets"): for ta in self._additional_slicing_targets: try: t = attrgetter(ta)(self) if out is None: if hasattr(t, '_slicer'): attrsetter(_obj, ta, t._slicer(slices, isNavigation)) else: target = attrgetter(ta)(_obj) t._slicer(slices, isNavigation, out=target) except AttributeError: pass if out is None: return _obj else: out.events.data_changed.trigger(obj=out)
def _slicer(self, slices, isNavigation=None, out=None): array_slices = self._get_array_slices(slices, isNavigation) new_data = self.data[array_slices] if new_data.size == 1 and new_data.dtype is np.dtype('O'): if isinstance(new_data[0], (np.ndarray, dArray)): return self.__class__(new_data[0]).transpose(navigation_axes=0) else: return new_data[0] if out is None: _obj = self._deepcopy_with_new_data(new_data, copy_variance=True) _to_remove = [] for slice_, axis in zip(array_slices, _obj.axes_manager._axes): if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis._slice_me(slice_) else: _to_remove.append(axis.index_in_axes_manager) for _ind in reversed(sorted(_to_remove)): _obj._remove_axis(_ind) else: out.data = new_data _obj = out i = 0 for slice_, axis_src in zip(array_slices, self.axes_manager._axes): axis_src = axis_src.copy() if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis_src._slice_me(slice_) axis_dst = out.axes_manager._axes[i] i += 1 axis_dst.update_from(axis_src, attributes=("scale", "offset", "size")) if hasattr(self, "_additional_slicing_targets"): for ta in self._additional_slicing_targets: try: t = attrgetter(ta)(self) if out is None: if hasattr(t, '_slicer'): attrsetter(_obj, ta, t._slicer(slices, isNavigation)) else: target = attrgetter(ta)(_obj) t._slicer(slices, isNavigation, out=target) except AttributeError: pass # _obj.get_dimensions_from_data() # replots, so we do it manually: dc = _obj.data for axis in _obj.axes_manager._axes: axis.size = int(dc.shape[axis.index_in_array]) if out is None: return _obj else: out.events.data_changed.trigger(obj=out)
def _slicer(self, slices, isNavigation=None, out=None): array_slices = self._get_array_slices(slices, isNavigation) if out is None: _obj = self._deepcopy_with_new_data(self.data[array_slices], copy_variance=True) _to_remove = [] for slice_, axis in zip(array_slices, _obj.axes_manager._axes): if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis._slice_me(slice_) else: _to_remove.append(axis.index_in_axes_manager) for _ind in reversed(sorted(_to_remove)): _obj._remove_axis(_ind) else: out.data = self.data[array_slices] _obj = out i = 0 for slice_, axis_src in zip(array_slices, self.axes_manager._axes): axis_src = axis_src.copy() if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis_src._slice_me(slice_) axis_dst = out.axes_manager._axes[i] i += 1 axis_dst.update_from(axis_src, attributes=( "scale", "offset", "size")) if hasattr(self, "_additional_slicing_targets"): for ta in self._additional_slicing_targets: try: t = attrgetter(ta)(self) if out is None: if hasattr(t, '_slicer'): attrsetter( _obj, ta, t._slicer( slices, isNavigation)) else: target = attrgetter(ta)(_obj) t._slicer( slices, isNavigation, out=target) except AttributeError: pass # _obj.get_dimensions_from_data() # replots, so we do it manually: dc = _obj.data for axis in _obj.axes_manager._axes: axis.size = int(dc.shape[axis.index_in_array]) if out is None: return _obj else: out.events.data_changed.trigger(obj=out)
def _slicer(self, slices, isNavigation=None, out=None): array_slices = self._get_array_slices(slices, isNavigation) if out is None: _obj = self._deepcopy_with_new_data(self.data[array_slices]) for slice_, axis in zip(array_slices, _obj.axes_manager._axes): if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis._slice_me(slice_) else: _obj._remove_axis(axis.index_in_axes_manager) else: out.data = self.data[array_slices] _obj = out i = 0 for slice_, axis_src in zip(array_slices, self.axes_manager._axes): axis_src = axis_src.copy() if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis_src._slice_me(slice_) axis_dst = out.axes_manager._axes[i] i += 1 axis_dst.update_from(axis_src, attributes=( "scale", "offset", "size")) if hasattr(self, "_additional_slicing_targets"): for ta in self._additional_slicing_targets: try: t = attrgetter(ta)(self) if out is None: if hasattr(t, '_slicer'): attrsetter( _obj, ta, t._slicer( slices, isNavigation)) else: target = attrgetter(ta)(_obj) t._slicer( slices, isNavigation, out=target) except AttributeError: pass _obj.get_dimensions_from_data() if out is None: return _obj else: out.events.data_changed.trigger(obj=out)
def load_from_dictionary(target, dic): """ Loads attributes of target to dictionary dic The attribute list is read from dic['_whitelist'].keys() Parameters ---------- target : object must contain the (nested) attributes of the whitelist.keys() dic : dictionary A dictionary, containing field '_whitelist', which is a dictionary with all keys that were exported, with values being flag strings. The convention of the flags is as follows: * 'init': object used for initialization of the target. Will be copied to the _whitelist after loading * 'fn': the targeted attribute is a function, and may have been pickled (preferably with dill package). * 'id': the id of the original object was exported and the attribute will not be set. The key has to be '_id_' * 'sig': The targeted attribute was a signal, and may have been converted to a dictionary if fullcopy=True """ new_whitelist = {} for key, flags_str in dic['_whitelist'].items(): value = dic[key] flags = parse_flag_string(flags_str) if 'id' not in flags: value = reconstruct_object(flags, value) if 'init' in flags: new_whitelist[key] = (flags_str, value) else: attrsetter(target, key, value) if len(flags_str): new_whitelist[key] = (flags_str, None) else: new_whitelist[key] = None if hasattr(target, '_whitelist'): if isinstance(target._whitelist, dict): target._whitelist.update(new_whitelist) else: attrsetter(target, '_whitelist', new_whitelist)
def load_from_dictionary(target, dic): """ Loads attributes of target to dictionary dic The attribute list is read from dic['_whitelist'].keys() Parameters ---------- target : object must contain the (nested) attributes of the whitelist.keys() dic : dictionary A dictionary, containing field '_whitelist', which is a dictionary with all keys that were exported, with values being flag strings. The convention of the flags is as follows: * 'init': object used for initialization of the target. Will be copied to the _whitelist after loading * 'fn': the targeted attribute is a function, and may have been pickled (preferably with dill package). * 'id': the id of the original object was exported and the attribute will not be set. The key has to be '_id_' * 'sig': The targeted attribute was a signal, and may have been converted to a dictionary if fullcopy=True """ new_whitelist = {} for key, flags_str in dic['_whitelist'].iteritems(): value = dic[key] flags = parse_flag_string(flags_str) if 'id' not in flags: value = reconstruct_object(flags, value) if 'init' in flags: new_whitelist[key] = (flags_str, value) else: attrsetter(target, key, value) if len(flags_str): new_whitelist[key] = (flags_str, None) else: new_whitelist[key] = None if hasattr(target, '_whitelist'): if isinstance(target._whitelist, dict): target._whitelist.update(new_whitelist) else: attrsetter(target, '_whitelist', new_whitelist)
def _slicer(self, slices, isNavigation=None, out=None): array_slices = self._get_array_slices(slices, isNavigation) if out is None: _obj = self._deepcopy_with_new_data(self.data[array_slices]) for slice_, axis in zip(array_slices, _obj.axes_manager._axes): if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis._slice_me(slice_) else: _obj._remove_axis(axis.index_in_axes_manager) else: out.data = self.data[array_slices] _obj = out i = 0 for slice_, axis_src in zip(array_slices, self.axes_manager._axes): axis_src = axis_src.copy() if (isinstance(slice_, slice) or len(self.axes_manager._axes) < 2): axis_src._slice_me(slice_) axis_dst = out.axes_manager._axes[i] i += 1 axis_dst.update_from(axis_src, attributes=("scale", "offset", "size")) if hasattr(self, "_additional_slicing_targets"): for ta in self._additional_slicing_targets: try: t = attrgetter(ta)(self) if out is None: if hasattr(t, '_slicer'): attrsetter(_obj, ta, t._slicer(slices, isNavigation)) else: target = attrgetter(ta)(_obj) t._slicer(slices, isNavigation, out=target) except AttributeError: pass _obj.get_dimensions_from_data() if out is None: return _obj else: out.events.data_changed.trigger(obj=out)
def copy_slice_from_whitelist(_from, _to, dims, both_slices, isNav, order=None): """Copies things from one object to another, according to whitelist, slicing where required. Parameters ---------- _from : object Original object _to : object Target object dims : tuple (navigation_dimensions, signal_dimensions) of the original object that is sliced both_slices : tuple (original_slices, array_slices) of the operation that is performed isNav : bool if the slicing operation is performed on navigation dimensions of the object order : tuple, None if given, performs the copying in the order given. If not all attributes given, the rest is random (the order a whitelist.keys() returns them). If given in the object, _slicing_order is looked up. """ def make_slice_navigation_decision(flags, isnav): if isnav: if 'inav' in flags: return True return None if 'isig' in flags: return False return None swl = None if hasattr(_from, '_slicing_whitelist'): swl = _from._slicing_whitelist if order is not None and not isinstance(order, tuple): raise ValueError('order argument has to be None or a tuple of strings') if order is None: order = () if hasattr(_from, '_slicing_order'): order = order + \ tuple(k for k in _from._slicing_order if k not in order) keys = order + tuple(k for k in _from._whitelist.keys() if k not in order) for key in keys: val = _from._whitelist[key] if key == 'self': target = None else: target = attrgetter(key)(_from) if val is None: # attrsetter(_to, key, attrgetter(key)(_from)) # continue flags = [] else: flags_str = val[0] flags = parse_flag_string(flags_str) if swl is not None and key in swl: flags.extend(parse_flag_string(swl[key])) if 'init' in flags: continue if 'id' in flags: continue if 'inav' in flags or 'isig' in flags: slice_nav = make_slice_navigation_decision(flags, isNav) result = _slice_target(target, dims, both_slices, slice_nav, 'sig' in flags) attrsetter(_to, key, result) continue else: # 'fn' in flag or no flags at all attrsetter(_to, key, target) continue
def test_wrong_item(self): t = self.tree attrsetter(t, 'random.name.with.more.than.one', 13)
def test_dtb_settattr(self): t = self.tree attrsetter(t, 'Node1.leaf11', 119) assert t.Node1.leaf11 == 119 attrsetter(t, 'Leaf3', 39) assert t.Leaf3 == 39
def test_wrong_item(self): t = self.tree with pytest.raises(AttributeError): attrsetter(t, 'random.name.with.more.than.one', 13)
def test_dtb_settattr(self): t = self.tree attrsetter(t, 'Node1.leaf11', 119) nt.assert_equal(t.Node1.leaf11, 119) attrsetter(t, 'Leaf3', 39) nt.assert_equal(t.Leaf3, 39)
def copy_slice_from_whitelist(_from, _to, dims, both_slices, isNav): """Copies things from one object to another, according to whitelist, slicing where required. Parameters ---------- _from : object Original object _to : object Target object dims : tuple (navigation_dimensions, signal_dimensions) of the original object that is sliced both_slices : tuple (original_slices, array_slices) of the operation that is performed isNav : bool if the slicing operation is performed on navigation dimensions of the object """ def make_slice_navigation_decision(flags, isnav): if isnav: if 'inav' in flags: return True return None if 'isig' in flags: return False return None swl = None if hasattr(_from, '_slicing_whitelist'): swl = _from._slicing_whitelist for key, val in _from._whitelist.iteritems(): if key == 'self': target = None else: target = attrgetter(key)(_from) if val is None: # attrsetter(_to, key, attrgetter(key)(_from)) # continue flags = [] else: flags_str = val[0] flags = parse_flag_string(flags_str) if swl is not None and key in swl: flags.extend(parse_flag_string(swl[key])) if 'init' in flags: continue if 'id' in flags: continue if 'inav' in flags or 'isig' in flags: slice_nav = make_slice_navigation_decision(flags, isNav) result = _slice_target( target, dims, both_slices, slice_nav, 'sig' in flags) attrsetter(_to, key, result) continue else: # 'fn' in flag or no flags at all attrsetter(_to, key, target) continue
def copy_slice_from_whitelist(_from, _to, dims, both_slices, isNav): """Copies things from one object to another, according to whitelist, slicing where required. Parameters ---------- _from : object Original object _to : object Target object dims : tuple (navigation_dimensions, signal_dimensions) of the original object that is sliced both_slices : tuple (original_slices, array_slices) of the operation that is performed isNav : bool if the slicing operation is performed on navigation dimensions of the object """ def make_slice_navigation_decision(flags, isnav): if isnav: if 'inav' in flags: return True return None if 'isig' in flags: return False return None swl = None if hasattr(_from, '_slicing_whitelist'): swl = _from._slicing_whitelist for key, val in _from._whitelist.iteritems(): if key == 'self': target = None else: target = attrgetter(key)(_from) if val is None: # attrsetter(_to, key, attrgetter(key)(_from)) # continue flags = [] else: flags_str = val[0] flags = parse_flag_string(flags_str) if swl is not None and key in swl: flags.extend(parse_flag_string(swl[key])) if 'init' in flags: continue if 'id' in flags: continue if 'inav' in flags or 'isig' in flags: slice_nav = make_slice_navigation_decision(flags, isNav) result = _slice_target(target, dims, both_slices, slice_nav, 'sig' in flags) attrsetter(_to, key, result) continue else: # 'fn' in flag or no flags at all attrsetter(_to, key, target) continue
def copy_slice_from_whitelist( _from, _to, dims, both_slices, isNav, order=None): """Copies things from one object to another, according to whitelist, slicing where required. Parameters ---------- _from : object Original object _to : object Target object dims : tuple (navigation_dimensions, signal_dimensions) of the original object that is sliced both_slices : tuple (original_slices, array_slices) of the operation that is performed isNav : bool if the slicing operation is performed on navigation dimensions of the object order : tuple, None if given, performs the copying in the order given. If not all attributes given, the rest is random (the order a whitelist.keys() returns them). If given in the object, _slicing_order is looked up. """ def make_slice_navigation_decision(flags, isnav): if isnav: if 'inav' in flags: return True return None if 'isig' in flags: return False return None swl = None if hasattr(_from, '_slicing_whitelist'): swl = _from._slicing_whitelist if order is not None and not isinstance(order, tuple): raise ValueError('order argument has to be None or a tuple of strings') if order is None: order = () if hasattr(_from, '_slicing_order'): order = order + \ tuple(k for k in _from._slicing_order if k not in order) keys = order + tuple(k for k in _from._whitelist.keys() if k not in order) for key in keys: val = _from._whitelist[key] if val is None: # attrsetter(_to, key, attrgetter(key)(_from)) # continue flags = [] else: flags_str = val[0] flags = parse_flag_string(flags_str) if swl is not None and key in swl: flags.extend(parse_flag_string(swl[key])) if 'init' in flags: continue if 'id' in flags: continue if key == 'self': target = None else: target = attrgetter(key)(_from) if 'inav' in flags or 'isig' in flags: slice_nav = make_slice_navigation_decision(flags, isNav) result = _slice_target( target, dims, both_slices, slice_nav, 'sig' in flags) attrsetter(_to, key, result) continue else: # 'fn' in flag or no flags at all attrsetter(_to, key, target) continue