Ejemplo n.º 1
0
    def _new_dims(self, index, new_arr):
        dims_pos = 0
        shape_pos = 0
        not_ellipsis_or_none = len(
            filter(lambda x: x is not Ellipsis and x is not None, index))
        for sl in index:
            if sl is None:
                # additional dimension via np.newaxis
                yield IdentityDimension()
            elif isinstance(sl, int):
                # burn a dimension
                dims_pos += 1
            elif isinstance(sl, list):
                dims_pos += 1
                shape_pos += 1
                yield IdentityDimension()
            elif sl is Ellipsis:
                ellipsis_size = len(self.dimensions) - not_ellipsis_or_none
                for i in xrange(ellipsis_size):
                    yield self.dimensions[dims_pos]
                    dims_pos += 1
                    shape_pos += 1
            else:
                try:
                    dim = self.dimensions[dims_pos]
                    shape = new_arr.shape[shape_pos]
                    yield dim.metaslice(sl, shape)
                except IndexError:
                    yield dim
                dims_pos += 1
                shape_pos += 1

        # Return any leftover dimensions
        for dim in self.dimensions[dims_pos:]:
            yield dim
Ejemplo n.º 2
0
 def test_max_supports_multiple_axes(self):
     raw = np.random.random_sample((10, 9, 8, 7))
     arr = ArrayWithUnits(raw,
                          dimensions=[
                              ContrivedDimension(10),
                              IdentityDimension(),
                              IdentityDimension(),
                              ContrivedDimension2(10)
                          ])
     result = arr.max(axis=(1, 2))
     self.assertEqual((10, 7), result.shape)
     self.assertIsInstance(result, ArrayWithUnits)
     self.assertEqual(2, len(result.dimensions))
     self.assertIsInstance(result.dimensions[0], ContrivedDimension)
     self.assertIsInstance(result.dimensions[1], ContrivedDimension2)
Ejemplo n.º 3
0
 def test_correct_result_of_indexing_using_boolean_array(self):
     raw = np.random.random_sample((8, 9))
     arr = ContrivedArray(raw,
                          (ContrivedDimension(10), IdentityDimension()))
     result = arr[arr > 0.5]
     self.assertIsInstance(result, np.ndarray)
     self.assertNotIsInstance(result, ArrayWithUnits)
Ejemplo n.º 4
0
 def test_zeros_like(self):
     arr = ArrayWithUnits(np.zeros((3, 2, 10)), [
         ContrivedDimension(10),
         IdentityDimension(),
         ContrivedDimension2(10)
     ])
     zeros = arr.zeros_like()
     self.assertEqual(zeros.shape, arr.shape)
     self.assertEqual(zeros.dimensions, arr.dimensions)
     np.testing.assert_allclose(0, zeros)
Ejemplo n.º 5
0
 def test_can_maintain_array_with_units_when_squeezing_3d(self):
     arr = ArrayWithUnits(np.zeros((3, 1, 10)), [
         ContrivedDimension(10),
         IdentityDimension(),
         ContrivedDimension2(10)
     ])
     squeezed = arr.squeeze()
     self.assertEqual((3, 10), squeezed.shape)
     self.assertIsInstance(squeezed, ArrayWithUnits)
     self.assertIsInstance(squeezed.dimensions[0], ContrivedDimension)
     self.assertIsInstance(squeezed.dimensions[1], ContrivedDimension2)
Ejemplo n.º 6
0
    def reshape(self, shape, order='C'):
        non_one = lambda x: abs(x) != 1

        if shape == filter(non_one, self.shape):
            # the new shape is this array's shape will all ones removed
            return self.squeeze()
        elif self.shape == filter(non_one, shape):
            # the new shape just adds some single dimension axes
            dims = iter(self.dimensions)
            new_dims = [
                IdentityDimension() if abs(size) == 1 else dims.next()
                for size in shape
            ]
            raw = np.asarray(self)
            return ArrayWithUnits(raw.reshape(shape), new_dims)

        # treat every axis as an IdentityDimension
        raw = np.asarray(self)
        return ArrayWithUnits(np.reshape(raw, shape, order),
                              [IdentityDimension() for _ in shape])
Ejemplo n.º 7
0
    def sliding_window(self, windowsize, stepsize=None):
        ws = tuple(self._compute_span(windowsize))
        ss = tuple(self._compute_span(stepsize)) if stepsize else ws
        result = sliding_window(self, ws, ss)

        try:
            new_dims = tuple(self._compute_new_dims(result, ws, ss))
        except ValueError:
            new_dims = [IdentityDimension()] * result.ndim

        return ArrayWithUnits(result, new_dims)
Ejemplo n.º 8
0
 def test_squeeze_with_no_single_dimension_is_unchanged(self):
     arr = ArrayWithUnits(np.zeros((3, 2, 10)), [
         ContrivedDimension(10),
         IdentityDimension(),
         ContrivedDimension2(10)
     ])
     squeezed = arr.squeeze()
     self.assertEqual((3, 2, 10), squeezed.shape)
     self.assertIsInstance(squeezed, ArrayWithUnits)
     self.assertIsInstance(squeezed.dimensions[0], ContrivedDimension)
     self.assertIsInstance(squeezed.dimensions[1], IdentityDimension)
     self.assertIsInstance(squeezed.dimensions[2], ContrivedDimension2)
Ejemplo n.º 9
0
    def _apply_reduction_to_dimensions(self, result, axis, keepdims):
        if axis is None:
            return result

        ndims = len(self.dimensions)
        reduced_axes = set([ndims + a if a < 0 else a for a in tuplify(axis)])
        all_axes = set(range(ndims))

        if keepdims:
            new_dims = [
                IdentityDimension() if i in reduced_axes else dim
                for i, dim in enumerate(self.dimensions)
            ]
        else:
            remaining_axes = sorted(all_axes - reduced_axes)
            new_dims = [self.dimensions[i] for i in remaining_axes]

        return ArrayWithUnits(result, new_dims)
Ejemplo n.º 10
0
    def sliding_window_with_leftovers(self,
                                      windowsize,
                                      stepsize=None,
                                      dopad=False):

        ws, ss = \
            self._sliding_window_integer_slices(windowsize, stepsize)

        leftovers, result = windowed(self, ws[0], ss[0], dopad)

        if not result.size:
            return self, ArrayWithUnits(result, self.dimensions)

        try:
            new_dims = tuple(self._compute_new_dims(result, ws, ss))
        except ValueError:
            new_dims = [IdentityDimension()] * result.ndim

        return leftovers, ArrayWithUnits(result, new_dims)
Ejemplo n.º 11
0
    def __new__(cls, arr, dimensions):
        if arr.ndim != len(dimensions):
            raise ValueError(
                'arr.ndim and len(dimensions) must match.  '
                'They were {arr.shape} and {dimensions}'.format(**locals()))

        obj = np.asarray(arr).view(cls)
        obj.dimensions = tuple(
            map(lambda d: IdentityDimension()
                if d is None else d.copy(), dimensions))

        for dim, size in zip(obj.dimensions, obj.shape):
            try:
                dim.size = size
            except AttributeError:
                pass
            try:
                dim.validate(size)
            except AttributeError:
                pass

        return obj
Ejemplo n.º 12
0
 def test_equal(self):
     d1 = IdentityDimension()
     d2 = IdentityDimension()
     self.assertEqual(d1, d2)