def _slice_me(self, slice_): """Returns a slice to slice the corresponding data axis and change the offset and scale of the DataAxis acordingly. Parameters ---------- slice_ : {float, int, slice} Returns ------- my_slice : slice """ i2v = self.index2value v2i = self.value2index if isinstance(slice_, slice): start = slice_.start stop = slice_.stop step = slice_.step else: if isfloat(slice_): start = v2i(slice_) else: start = self._get_positive_index(slice_) stop = start + 1 step = None if isfloat(step): step = int(round(step / self.scale)) if isfloat(start): try: start = v2i(start) except ValueError: # The value is below the axis limits # we slice from the start. start = None if isfloat(stop): try: stop = v2i(stop) except ValueError: # The value is above the axes limits # we slice up to the end. stop = None if step == 0: raise ValueError("slice step cannot be zero") my_slice = slice(start, stop, step) if start is None: if step > 0 or step is None: start = 0 else: start = self.size - 1 self.offset = i2v(start) if step is not None: self.scale *= step return my_slice
def __getitem__(self, y): """x.__getitem__(y) <==> x[y] """ if isinstance(y, basestring): axes = list(self._get_axes_in_natural_order()) while axes: axis = axes.pop() if y == axis.name: return axis raise ValueError("There is no DataAxis named %s" % y) elif (isfloat(y.real) and not y.real.is_integer() or isfloat(y.imag) and not y.imag.is_integer()): raise TypeError("axesmanager indices must be integers, " "complex intergers or strings") if y.imag == 0: # Natural order return self._get_axes_in_natural_order()[y] elif y.imag == 3: # Array order # Array order return self._axes[int(y.real)] elif y.imag == 1: # Navigation natural order # return self.navigation_axes[int(y.real)] elif y.imag == 2: # Signal natural order return self.signal_axes[int(y.real)] else: raise IndexError("axesmanager imaginary part of complex indices " "must be 0, 1 or 2")
def _axes_getter(self, y): if y in self._axes: return y if isinstance(y, str): axes = list(self._get_axes_in_natural_order()) while axes: axis = axes.pop() if y == axis.name: return axis raise ValueError("There is no DataAxis named %s" % y) elif (isfloat(y.real) and not y.real.is_integer() or isfloat(y.imag) and not y.imag.is_integer()): raise TypeError("axesmanager indices must be integers, " "complex intergers or strings") if y.imag == 0: # Natural order return self._get_axes_in_natural_order()[y] elif y.imag == 3: # Array order # Array order return self._axes[int(y.real)] elif y.imag == 1: # Navigation natural order # return self.navigation_axes[int(y.real)] elif y.imag == 2: # Signal natural order return self.signal_axes[int(y.real)] else: raise IndexError("axesmanager imaginary part of complex indices " "must be 0, 1 or 2")
def _get_array_slices(self, slice_): """Returns a slice to slice the corresponding data axis without changing the offset and scale of the DataAxis. Parameters ---------- slice_ : {float, int, slice} Returns ------- my_slice : slice """ v2i = self.value2index if isinstance(slice_, slice): start = slice_.start stop = slice_.stop step = slice_.step else: if isfloat(slice_): start = v2i(slice_) else: start = self._get_positive_index(slice_) stop = start + 1 step = None if isfloat(step): step = int(round(step / self.scale)) if isfloat(start): try: start = v2i(start) except ValueError: # The value is below the axis limits # we slice from the start. start = None if isfloat(stop): try: stop = v2i(stop) except ValueError: # The value is above the axes limits # we slice up to the end. stop = None if step == 0: raise ValueError("slice step cannot be zero") return slice(start, stop, step)
def _get_array_slices(self, slice_): """Returns a slice to slice the corresponding data axis without changing the offset and scale of the DataAxis. Parameters ---------- slice_ : {float, int, slice} Returns ------- my_slice : slice """ v2i = self.value2index if isinstance(slice_, slice): start = slice_.start stop = slice_.stop step = slice_.step else: if isfloat(slice_): start = v2i(slice_) else: start = self._get_positive_index(slice_) stop = start + 1 step = None if isfloat(step): step = int(round(step / self.scale)) if isfloat(start): try: start = v2i(start) except ValueError: # The value is below the axis limits # we slice from the start. start = None if isfloat(stop): try: stop = v2i(stop) except ValueError: # The value is above the axes limits # we slice up to the end. stop = None if step == 0: raise ValueError("slice step cannot be zero") return slice(start, stop, step)
def test_isfloat_float(): assert math_tools.isfloat(3.)
def _get_index(self, value): if isfloat(value): return self.value2index(value) else: return value
def test_isfloat_npint(): assert not math_tools.isfloat(np.int16(3))
def test_isfloat_npfloat(): assert math_tools.isfloat(np.float32(3.))
def test_isfloat_int(): assert not math_tools.isfloat(3)
def test_isfloat_float(): assert math_tools.isfloat(3.)
def test_isfloat_npfloat(): nose.tools.assert_true(math_tools.isfloat(np.float32(3.)))
def test_isfloat_npint(): nose.tools.assert_false(math_tools.isfloat(np.int16(3)))
def test_isfloat_npint(): assert not math_tools.isfloat(np.int16(3))
def test_isfloat_npfloat(): assert math_tools.isfloat(np.float32(3.))
def test_isfloat_int(): assert not math_tools.isfloat(3)
def test_isfloat_float(): nose.tools.assert_true(math_tools.isfloat(3.))
def _get_index(self, value): if isfloat(value): return self.value2index(value) else: return value
def _get_array_slices(self, slice_): """Returns a slice to slice the corresponding data axis without changing the offset and scale of the DataAxis. Parameters ---------- slice_ : {float, int, slice} Returns ------- my_slice : slice """ v2i = self.value2index if isinstance(slice_, slice): start = slice_.start stop = slice_.stop step = slice_.step else: if isfloat(slice_): start = v2i(slice_) else: start = self._get_positive_index(slice_) stop = start + 1 step = None if isfloat(step): step = int(round(step / self.scale)) if isfloat(start): try: start = v2i(start) except ValueError: if start > self.high_value: # The start value is above the axis limit raise IndexError( "Start value above axis high bound for axis %s." "value: %f high_bound: %f" % (repr(self), start, self.high_value)) else: # The start value is below the axis limit, # we slice from the start. start = None if isfloat(stop): try: stop = v2i(stop) except ValueError: if stop < self.low_value: # The stop value is below the axis limits raise IndexError( "Stop value below axis low bound for axis %s." "value: %f low_bound: %f" % (repr(self), stop, self.low_value)) else: # The stop value is below the axis limit, # we slice until the end. stop = None if step == 0: raise ValueError("slice step cannot be zero") return slice(start, stop, step)
def _get_array_slices(self, slice_): """Returns a slice to slice the corresponding data axis without changing the offset and scale of the DataAxis. Parameters ---------- slice_ : {float, int, slice} Returns ------- my_slice : slice """ v2i = self.value2index if isinstance(slice_, slice): start = slice_.start stop = slice_.stop step = slice_.step else: if isfloat(slice_): start = v2i(slice_) else: start = self._get_positive_index(slice_) stop = start + 1 step = None if isfloat(step): step = int(round(step / self.scale)) if isfloat(start): try: start = v2i(start) except ValueError: if start > self.high_value: # The start value is above the axis limit raise IndexError( "Start value above axis high bound for axis %s." "value: %f high_bound: %f" % (repr(self), start, self.high_value) ) else: # The start value is below the axis limit, # we slice from the start. start = None if isfloat(stop): try: stop = v2i(stop) except ValueError: if stop < self.low_value: # The stop value is below the axis limits raise IndexError( "Stop value below axis low bound for axis %s." "value: %f low_bound: %f" % (repr(self), stop, self.low_value) ) else: # The stop value is below the axis limit, # we slice until the end. stop = None if step == 0: raise ValueError("slice step cannot be zero") return slice(start, stop, step)
def test_isfloat_int(): nose.tools.assert_false(math_tools.isfloat(3))