Example #1
0
    def test_matching_named_fields(self):
        # Test combination of arrays w/ matching field names
        (_, x, _, z) = self.data
        zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)],
                      dtype=[('A', '|S3'), ('B', float), ('C', float)])
        test = stack_arrays((z, zz))
        control = ma.array([('A', 1, -1), ('B', 2, -1), ('a', 10., 100.),
                            ('b', 20., 200.), ('c', 30., 300.)],
                           dtype=[('A', '|S3'), ('B', float), ('C', float)],
                           mask=[(0, 0, 1), (0, 0, 1), (0, 0, 0), (0, 0, 0),
                                 (0, 0, 0)])
        assert_equal(test, control)
        assert_equal(test.mask, control.mask)

        test = stack_arrays((z, zz, x))
        ndtype = [('A', '|S3'), ('B', float), ('C', float), ('f3', int)]
        control = ma.array([('A', 1, -1, -1), ('B', 2, -1, -1),
                            ('a', 10., 100., -1), ('b', 20., 200., -1),
                            ('c', 30., 300., -1), (-1, -1, -1, 1),
                            (-1, -1, -1, 2)],
                           dtype=ndtype,
                           mask=[(0, 0, 1, 1), (0, 0, 1, 1), (0, 0, 0, 1),
                                 (0, 0, 0, 1), (0, 0, 0, 1), (1, 1, 1, 0),
                                 (1, 1, 1, 0)])
        assert_equal(test, control)
        assert_equal(test.mask, control.mask)
Example #2
0
    def test_solo(self):
        # Test stack_arrays on single arrays
        (_, x, _, _) = self.data
        test = stack_arrays((x, ))
        assert_equal(test, x)
        assert_(test is x)

        test = stack_arrays(x)
        assert_equal(test, x)
        assert_(test is x)
Example #3
0
    def test_unnamed_fields(self):
        # Tests combinations of arrays w/o named fields
        (_, x, y, _) = self.data

        test = stack_arrays((x, x), usemask=False)
        control = np.array([1, 2, 1, 2])
        assert_equal(test, control)

        test = stack_arrays((x, y), usemask=False)
        control = np.array([1, 2, 10, 20, 30])
        assert_equal(test, control)

        test = stack_arrays((y, x), usemask=False)
        control = np.array([10, 20, 30, 1, 2])
        assert_equal(test, control)
Example #4
0
 def test_autoconversion(self):
     # Tests autoconversion
     adtype = [('A', int), ('B', bool), ('C', float)]
     a = ma.array([(1, 2, 3)], mask=[(0, 1, 0)], dtype=adtype)
     bdtype = [('A', int), ('B', float), ('C', float)]
     b = ma.array([(4, 5, 6)], dtype=bdtype)
     control = ma.array([(1, 2, 3), (4, 5, 6)],
                        mask=[(0, 1, 0), (0, 0, 0)],
                        dtype=bdtype)
     test = stack_arrays((a, b), autoconvert=True)
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
     try:
         test = stack_arrays((a, b), autoconvert=False)
     except TypeError:
         pass
     else:
         raise AssertionError
Example #5
0
 def test_checktitles(self):
     # Test using titles in the field names
     adtype = [(('a', 'A'), int), (('b', 'B'), bool), (('c', 'C'), float)]
     a = ma.array([(1, 2, 3)], mask=[(0, 1, 0)], dtype=adtype)
     bdtype = [(('a', 'A'), int), (('b', 'B'), bool), (('c', 'C'), float)]
     b = ma.array([(4, 5, 6)], dtype=bdtype)
     test = stack_arrays((a, b))
     control = ma.array([(1, 2, 3), (4, 5, 6)],
                        mask=[(0, 1, 0), (0, 0, 0)],
                        dtype=bdtype)
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
Example #6
0
    def test_unnamed_and_named_fields(self):
        # Test combination of arrays w/ & w/o named fields
        (_, x, _, z) = self.data

        test = stack_arrays((x, z))
        control = ma.array([(1, -1, -1), (2, -1, -1), (-1, 'A', 1),
                            (-1, 'B', 2)],
                           mask=[(0, 1, 1), (0, 1, 1), (1, 0, 0), (1, 0, 0)],
                           dtype=[('f0', int), ('A', '|S3'), ('B', float)])
        assert_equal(test, control)
        assert_equal(test.mask, control.mask)

        test = stack_arrays((z, x))
        control = ma.array([
            ('A', 1, -1),
            ('B', 2, -1),
            (-1, -1, 1),
            (-1, -1, 2),
        ],
                           mask=[(0, 0, 1), (0, 0, 1), (1, 1, 0), (1, 1, 0)],
                           dtype=[('A', '|S3'), ('B', float), ('f2', int)])
        assert_equal(test, control)
        assert_equal(test.mask, control.mask)

        test = stack_arrays((z, z, x))
        control = ma.array([
            ('A', 1, -1),
            ('B', 2, -1),
            ('A', 1, -1),
            ('B', 2, -1),
            (-1, -1, 1),
            (-1, -1, 2),
        ],
                           mask=[(0, 0, 1), (0, 0, 1), (0, 0, 1), (0, 0, 1),
                                 (1, 1, 0), (1, 1, 0)],
                           dtype=[('A', '|S3'), ('B', float), ('f2', int)])
        assert_equal(test, control)
Example #7
0
 def test_defaults(self):
     # Test defaults: no exception raised if keys of defaults are not fields.
     (_, _, _, z) = self.data
     zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)],
                   dtype=[('A', '|S3'), ('B', float), ('C', float)])
     defaults = {'A': '???', 'B': -999., 'C': -9999., 'D': -99999.}
     test = stack_arrays((z, zz), defaults=defaults)
     control = ma.array([('A', 1, -9999.), ('B', 2, -9999.),
                         ('a', 10., 100.), ('b', 20., 200.),
                         ('c', 30., 300.)],
                        dtype=[('A', '|S3'), ('B', float), ('C', float)],
                        mask=[(0, 0, 1), (0, 0, 1), (0, 0, 0), (0, 0, 0),
                              (0, 0, 0)])
     assert_equal(test, control)
     assert_equal(test.data, control.data)
     assert_equal(test.mask, control.mask)
Example #8
0
    def test_subdtype(self):
        z = np.array([('A', 1), ('B', 2)],
                     dtype=[('A', '|S3'), ('B', float, (1, ))])
        zz = np.array([('a', [10.], 100.), ('b', [20.], 200.),
                       ('c', [30.], 300.)],
                      dtype=[('A', '|S3'), ('B', float, (1, )), ('C', float)])

        res = stack_arrays((z, zz))
        expected = ma.array(data=[(b'A', [1.0], 0), (b'B', [2.0], 0),
                                  (b'a', [10.0], 100.0), (b'b', [20.0], 200.0),
                                  (b'c', [30.0], 300.0)],
                            mask=[(False, [False], True),
                                  (False, [False], True),
                                  (False, [False], False),
                                  (False, [False], False),
                                  (False, [False], False)],
                            dtype=zz.dtype)
        assert_equal(res.dtype, expected.dtype)
        assert_equal(res, expected)
        assert_equal(res.mask, expected.mask)