コード例 #1
0
ファイル: test_Data.py プロジェクト: bradyrx/cfdm
    def test_Data_dtype_mask(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        a = numpy.ma.array(
            [[280.0, -99, -99, -99], [281.0, 279.0, 278.0, 279.0]],
            dtype=float,
            mask=[[0, 1, 1, 1], [0, 0, 0, 0]])

        d = cfdm.Data([[280, -99, -99, -99], [281, 279, 278, 279]])
        self.assertEqual(d.dtype, numpy.dtype(int))

        d = cfdm.Data([[280, -99, -99, -99], [281, 279, 278, 279]],
                      dtype=float,
                      mask=[[0, 1, 1, 1], [0, 0, 0, 0]])

        self.assertEqual(d.dtype, a.dtype)
        self.assertTrue((d.array == a).all())
        self.assertEqual(d.mask.shape, a.mask.shape)
        self.assertTrue((d.mask.array == numpy.ma.getmaskarray(a)).all())

        a = numpy.array([[280.0, -99, -99, -99], [281.0, 279.0, 278.0, 279.0]],
                        dtype=float)
        mask = numpy.ma.masked_all(a.shape).mask

        d = cfdm.Data([[280, -99, -99, -99], [281, 279, 278, 279]],
                      dtype=float)

        self.assertEqual(d.dtype, a.dtype)
        self.assertTrue((d.array == a).all())
        self.assertEqual(d.mask.shape, mask.shape)
        self.assertTrue((d.mask.array == numpy.ma.getmaskarray(a)).all())
コード例 #2
0
ファイル: test_Data.py プロジェクト: bradyrx/cfdm
    def test_Data_filled(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        d = cfdm.Data([[1, 2, 3]])
        self.assertTrue((d.filled().array == [[1, 2, 3]]).all())

        d[0, 0] = cfdm.masked
        self.assertTrue((d.filled().array == [[
            -9223372036854775806,
            2,
            3,
        ]]).all())

        d.set_fill_value(-99)
        self.assertTrue((d.filled().array == [[
            -99,
            2,
            3,
        ]]).all())

        self.assertTrue((d.filled(1e10).array == [[
            1e10,
            2,
            3,
        ]]).all())

        d = cfdm.Data(['a', 'b', 'c'], mask=[1, 0, 0])
        self.assertTrue((d.filled().array == ['', 'b', 'c']).all())
コード例 #3
0
ファイル: test_Data.py プロジェクト: bradyrx/cfdm
    def test_Data_transpose(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        a = numpy.arange(2 * 3 * 5).reshape(2, 1, 3, 5)
        d = cfdm.Data(a.copy())

        for indices in (list(range(a.ndim)), list(range(-a.ndim, 0))):
            for axes in itertools.permutations(indices):
                a = numpy.transpose(a, axes)
                d = d.transpose(axes)
                message = ("cfdm.Data.transpose({}) failed: d.shape={}, "
                           "a.shape={}".format(axes, d.shape, a.shape))
                self.assertEqual(d.shape, a.shape, message)
                self.assertTrue((d.array == a).all(), message)
        # --- End: for

        with self.assertRaises(ValueError):
            d.transpose(axes=99)

        with self.assertRaises(ValueError):
            d.transpose(axes=[1, 2, 3, 4, 5])

        d = cfdm.Data(9)
        self.assertTrue(d.equals(d.transpose()))
コード例 #4
0
    def test_GATHERING_create(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        # Define the gathered values
        gathered_array = numpy.array([[280, 282.5, 281], [279, 278, 277.5]],
                                     dtype='float32')
        # Define the list array values
        list_array = [1, 4, 5]

        # Initialise the list variable
        list_variable = cfdm.List(data=cfdm.Data(list_array))

        # Initialise the gathered array object
        array = cfdm.GatheredArray(
            compressed_array=cfdm.Data(gathered_array),
            compressed_dimension=1,
            shape=(2, 3, 2), size=12, ndim=3,
            list_variable=list_variable
        )

        # Create the field construct with the domain axes and the
        # gathered array
        tas = cfdm.Field(properties={'standard_name': 'air_temperature',
                                     'units': 'K'})

        # Create the domain axis constructs for the uncompressed array
        T = tas.set_construct(cfdm.DomainAxis(2))
        Y = tas.set_construct(cfdm.DomainAxis(3))
        X = tas.set_construct(cfdm.DomainAxis(2))

        # Set the data for the field
        tas.set_data(cfdm.Data(array), axes=[T, Y, X])

        self.assertTrue((tas.data.array == numpy.ma.masked_array(
            data=[[[1, 280.0],
                   [1, 1],
                   [282.5, 281.0]],

                  [[1, 279.0],
                   [1, 1],
                   [278.0, 277.5]]],
            mask=[[[True, False],
                   [True, True],
                   [False, False]],

                  [[True, False],
                   [True, True],
                   [False, False]]],
            fill_value=1e+20,
            dtype='float32')).all())

        self.assertEqual(tas.data.get_compression_type(), 'gathered')

        self.assertTrue((tas.data.compressed_array == numpy.array(
            [[280., 282.5, 281.],
             [279., 278., 277.5]], dtype='float32')).all())

        self.assertTrue((tas.data.get_list().data.array == numpy.array(
            [1, 4, 5])).all())
コード例 #5
0
    def test_Data_dtype_mask(self):
        """Test the dtype and mask Data methods."""
        a = numpy.ma.array(
            [[280.0, -99, -99, -99], [281.0, 279.0, 278.0, 279.0]],
            dtype=float,
            mask=[[0, 1, 1, 1], [0, 0, 0, 0]],
        )

        d = cfdm.Data([[280, -99, -99, -99], [281, 279, 278, 279]])
        self.assertEqual(d.dtype, numpy.dtype(int))

        d = cfdm.Data(
            [[280, -99, -99, -99], [281, 279, 278, 279]],
            dtype=float,
            mask=[[0, 1, 1, 1], [0, 0, 0, 0]],
        )

        self.assertEqual(d.dtype, a.dtype)
        self.assertTrue((d.array == a).all())
        self.assertEqual(d.mask.shape, a.mask.shape)
        self.assertTrue((d.mask.array == numpy.ma.getmaskarray(a)).all())

        a = numpy.array([[280.0, -99, -99, -99], [281.0, 279.0, 278.0, 279.0]],
                        dtype=float)
        mask = numpy.ma.masked_all(a.shape).mask

        d = cfdm.Data([[280, -99, -99, -99], [281, 279, 278, 279]],
                      dtype=float)

        self.assertEqual(d.dtype, a.dtype)
        self.assertTrue((d.array == a).all())
        self.assertEqual(d.mask.shape, mask.shape)
        self.assertTrue((d.mask.array == numpy.ma.getmaskarray(a)).all())
コード例 #6
0
    def test_Data_filled(self):
        """Test the filled Data method."""
        d = cfdm.Data([[1, 2, 3]])
        self.assertTrue((d.filled().array == [[1, 2, 3]]).all())

        d[0, 0] = cfdm.masked
        self.assertTrue((d.filled().array == [[
            -9223372036854775806,
            2,
            3,
        ]]).all())

        d.set_fill_value(-99)
        self.assertTrue((d.filled().array == [[
            -99,
            2,
            3,
        ]]).all())

        self.assertTrue((d.filled(1e10).array == [[
            1e10,
            2,
            3,
        ]]).all())

        d = cfdm.Data(["a", "b", "c"], mask=[1, 0, 0])
        self.assertTrue((d.filled().array == ["", "b", "c"]).all())
コード例 #7
0
ファイル: test_dsg.py プロジェクト: bradyrx/cfdm
    def test_DSG_contiguous(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = self.c.copy()

        self.assertEqual(len(f), 2)

        # Select the specific humidity field
        q = [g for g in f
             if g.get_property('standard_name') == 'specific_humidity'][0]

        self.assertTrue(q._equals(self.a, q.data.array))

        cfdm.write(f, tempfile)
        g = cfdm.read(tempfile)

        self.assertEqual(len(g), len(f))

        for i in range(len(f)):
            self.assertTrue(g[i].equals(f[i], verbose=3))

        # ------------------------------------------------------------
        # Test creation
        # ------------------------------------------------------------
        # Define the ragged array values
        ragged_array = numpy.array([280, 282.5, 281, 279, 278, 279.5],
                                   dtype='float32')

        # Define the count array values
        count_array = [2, 4]

        # Create the count variable
        count_variable = cfdm.Count(data=cfdm.Data(count_array))
        count_variable.set_property('long_name',
                                    'number of obs for this timeseries')

        # Create the contiguous ragged array object
        array = cfdm.RaggedContiguousArray(
                         compressed_array=cfdm.NumpyArray(ragged_array),
                         shape=(2, 4), size=8, ndim=2,
                         count_variable=count_variable)

        # Create the field construct with the domain axes and the ragged
        # array
        tas = cfdm.Field()
        tas.set_properties({'standard_name': 'air_temperature',
                            'units': 'K',
                            'featureType': 'timeSeries'})

        # Create the domain axis constructs for the uncompressed array
        X = tas.set_construct(cfdm.DomainAxis(4))
        Y = tas.set_construct(cfdm.DomainAxis(2))

        # Set the data for the field
        tas.set_data(cfdm.Data(array), axes=[Y, X])

        cfdm.write(tas, tempfile)
コード例 #8
0
    def test_GATHERING_create(self):
        """Test the creation of a construct with a gathered array."""
        # Define the gathered values
        gathered_array = numpy.array([[280, 282.5, 281], [279, 278, 277.5]],
                                     dtype="float32")
        # Define the list array values
        list_array = [1, 4, 5]

        # Initialise the list variable
        list_variable = cfdm.List(data=cfdm.Data(list_array))

        # Initialise the gathered array object
        array = cfdm.GatheredArray(
            compressed_array=cfdm.Data(gathered_array),
            compressed_dimension=1,
            shape=(2, 3, 2),
            size=12,
            ndim=3,
            list_variable=list_variable,
        )

        # Create the field construct with the domain axes and the
        # gathered array
        tas = cfdm.Field(properties={
            "standard_name": "air_temperature",
            "units": "K"
        })

        # Create the domain axis constructs for the uncompressed array
        T = tas.set_construct(cfdm.DomainAxis(2))
        Y = tas.set_construct(cfdm.DomainAxis(3))
        X = tas.set_construct(cfdm.DomainAxis(2))

        # Set the data for the field
        tas.set_data(cfdm.Data(array), axes=[T, Y, X])

        self.assertTrue((tas.data.array == numpy.ma.masked_array(
            data=[
                [[1, 280.0], [1, 1], [282.5, 281.0]],
                [[1, 279.0], [1, 1], [278.0, 277.5]],
            ],
            mask=[
                [[True, False], [True, True], [False, False]],
                [[True, False], [True, True], [False, False]],
            ],
            fill_value=1e20,
            dtype="float32",
        )).all())

        self.assertEqual(tas.data.get_compression_type(), "gathered")

        self.assertTrue((tas.data.compressed_array == numpy.array(
            [[280.0, 282.5, 281.0], [279.0, 278.0, 277.5]],
            dtype="float32",
        )).all())

        self.assertTrue(
            (tas.data.get_list().data.array == numpy.array([1, 4, 5])).all())
コード例 #9
0
    def test_DSG_create_contiguous(self):
        """Test the creation of a contiguous ragged array."""
        # Define the ragged array values
        ragged_array = numpy.array([1, 3, 4, 3, 6], dtype="float32")
        # Define the count array values
        count_array = [2, 3]

        # Initialise the count variable
        count_variable = cfdm.Count(data=cfdm.Data(count_array))
        count_variable.set_property(
            "long_name", "number of obs for this timeseries"
        )

        # Initialise the contiguous ragged array object
        array = cfdm.RaggedContiguousArray(
            compressed_array=cfdm.Data(ragged_array),
            shape=(2, 3),
            size=6,
            ndim=2,
            count_variable=count_variable,
        )

        # Initialize the auxiliary coordinate construct with the
        # ragged array and set some properties
        z = cfdm.AuxiliaryCoordinate(
            data=cfdm.Data(array),
            properties={
                "standard_name": "height",
                "units": "km",
                "positive": "up",
            },
        )

        self.assertTrue(
            (
                z.data.array
                == numpy.ma.masked_array(
                    data=[[1.0, 3.0, 99], [4.0, 3.0, 6.0]],
                    mask=[[False, False, True], [False, False, False]],
                    fill_value=1e20,
                    dtype="float32",
                )
            ).all()
        )

        self.assertEqual(z.data.get_compression_type(), "ragged contiguous")

        self.assertTrue(
            (
                z.data.compressed_array
                == numpy.array([1.0, 3.0, 4.0, 3.0, 6.0], dtype="float32")
            ).all()
        )

        self.assertTrue(
            (z.data.get_count().data.array == numpy.array([2, 3])).all()
        )
コード例 #10
0
    def test_Data__setitem__(self):
        """Test the assignment of data items on Data."""
        a = numpy.ma.arange(3000).reshape(50, 60)

        d = cfdm.Data(a.filled(), units="m")

        for n, (j, i) in enumerate((
            (34, 23),
            (0, 0),
            (-1, -1),
            (slice(40, 50), slice(58, 60)),
            (Ellipsis, slice(None)),
            (slice(None), Ellipsis),
        )):
            n = -n - 1
            for dvalue, avalue in (
                (n, n),
                (cfdm.masked, numpy.ma.masked),
                (n, n),
            ):
                message = "cfdm.Data[{}, {}]={}={} failed".format(
                    j, i, dvalue, avalue)
                d[j, i] = dvalue
                a[j, i] = avalue
                x = d.array
                self.assertTrue((x == a).all() in (True, numpy.ma.masked),
                                message)
                m = numpy.ma.getmaskarray(x)
                self.assertTrue(
                    (m == numpy.ma.getmaskarray(a)).all(),
                    "d.mask.array=" + repr(m) + "\nnumpy.ma.getmaskarray(a)=" +
                    repr(numpy.ma.getmaskarray(a)),
                )

        a = numpy.ma.arange(3000).reshape(50, 60)

        d = cfdm.Data(a.filled(), "m")

        (j, i) = (slice(0, 2), slice(0, 3))
        array = numpy.array([[1, 2, 6], [3, 4, 5]]) * -1

        for dvalue in (array, numpy.ma.masked_where(array < -2, array), array):
            message = "cfdm.Data[%s, %s]=%s failed" % (j, i, dvalue)
            d[j, i] = dvalue
            a[j, i] = dvalue
            x = d.array
            self.assertTrue((x == a).all() in (True, numpy.ma.masked), message)
            m = numpy.ma.getmaskarray(x)
            self.assertTrue((m == numpy.ma.getmaskarray(a)).all(), message)

        # Scalar numeric array
        d = cfdm.Data(9, units="km")
        d[...] = cfdm.masked
        a = d.array
        self.assertEqual(a.shape, ())
        self.assertIs(a[()], numpy.ma.masked)
コード例 #11
0
    def test_Data__setitem__(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        a = numpy.ma.arange(3000).reshape(50, 60)

        d = cfdm.Data(a.filled(), units='m')

        for n, (j, i) in enumerate((
            (34, 23),
            (0, 0),
            (-1, -1),
            (slice(40, 50), slice(58, 60)),
            (Ellipsis, slice(None)),
            (slice(None), Ellipsis),
        )):
            n = -n - 1
            for dvalue, avalue in ((n, n), (cfdm.masked, numpy.ma.masked),
                                   (n, n)):
                message = "cfdm.Data[{}, {}]={}={} failed".format(
                    j, i, dvalue, avalue)
                d[j, i] = dvalue
                a[j, i] = avalue
                x = d.array
                self.assertTrue((x == a).all() in (True, numpy.ma.masked),
                                message)
                m = numpy.ma.getmaskarray(x)
                self.assertTrue(
                    (m == numpy.ma.getmaskarray(a)).all(),
                    'd.mask.array=' + repr(m) + '\nnumpy.ma.getmaskarray(a)=' +
                    repr(numpy.ma.getmaskarray(a)))
        #--- End: for

        a = numpy.ma.arange(3000).reshape(50, 60)

        d = cfdm.Data(a.filled(), 'm')

        (j, i) = (slice(0, 2), slice(0, 3))
        array = numpy.array([[1, 2, 6], [3, 4, 5]]) * -1

        for dvalue in (array, numpy.ma.masked_where(array < -2, array), array):
            message = 'cfdm.Data[%s, %s]=%s failed' % (j, i, dvalue)
            d[j, i] = dvalue
            a[j, i] = dvalue
            x = d.array
            self.assertTrue((x == a).all() in (True, numpy.ma.masked), message)
            m = numpy.ma.getmaskarray(x)
            self.assertTrue((m == numpy.ma.getmaskarray(a)).all(), message)
        #--- End: for

        # Scalar numeric array
        d = cfdm.Data(9, units='km')
        d[...] = cfdm.masked
        a = d.array
        self.assertTrue(a.shape == ())
        self.assertTrue(a[()] is numpy.ma.masked)
コード例 #12
0
    def test_Data_unique(self):
        """Test the unique Data method."""
        d = cfdm.Data([[4, 2, 1], [1, 2, 3]], units="metre")
        u = d.unique()
        self.assertEqual(u.shape, (4, ))
        self.assertTrue((u.array == cfdm.Data([1, 2, 3, 4],
                                              "metre").array).all())

        d[1, -1] = cfdm.masked
        u = d.unique()
        self.assertEqual(u.shape, (3, ))
        self.assertTrue((u.array == cfdm.Data([1, 2, 4], "metre").array).all())
コード例 #13
0
ファイル: test_Data.py プロジェクト: bradyrx/cfdm
    def test_Data_flatten(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        ma = numpy.ma.arange(24).reshape(1, 2, 3, 4)
        ma[0, 1, 1, 2] = cfdm.masked
        ma[0, 0, 2, 1] = cfdm.masked

        d = cfdm.Data(ma.copy())
        self.assertTrue(d.equals(d.flatten([]), verbose=3))
        self.assertIsNone(d.flatten(inplace=True))

        d = cfdm.Data(ma.copy())

        b = ma.flatten()
        for axes in (None, list(range(d.ndim))):
            e = d.flatten(axes)
            self.assertEqual(e.ndim, 1)
            self.assertEqual(e.shape, b.shape)
            self.assertTrue(e.equals(cfdm.Data(b), verbose=3))

        for axes in axes_combinations(d.ndim):
            e = d.flatten(axes)

            if len(axes) <= 1:
                shape = d.shape
            else:
                shape = [n for i, n in enumerate(d.shape) if i not in axes]
                shape.insert(
                    sorted(axes)[0],
                    numpy.prod([n for i, n in enumerate(d.shape)
                                if i in axes]))

            self.assertEqual(e.shape, tuple(shape), axes)
            self.assertEqual(e.ndim, d.ndim - len(axes) + 1)
            self.assertEqual(e.size, d.size)

        for n in range(4):
            e = d.flatten(n)
            f = d.flatten([n])
            self.assertTrue(e.equals(f))

        with self.assertRaises(ValueError):
            d.flatten(99)

        d = cfdm.Data(9)
        self.assertTrue(d.equals(d.flatten()))
        self.assertTrue(d.equals(d.flatten([])))

        with self.assertRaises(ValueError):
            d.flatten(0)
コード例 #14
0
    def test_Data_unique(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        d = cfdm.Data([[4, 2, 1], [1, 2, 3]], units='metre')
        u = d.unique()
        self.assertTrue(u.shape == (4, ))
        self.assertTrue((u.array == cfdm.Data([1, 2, 3, 4],
                                              'metre').array).all())

        d[1, -1] = cfdm.masked
        u = d.unique()
        self.assertTrue(u.shape == (3, ))
        self.assertTrue((u.array == cfdm.Data([1, 2, 4], 'metre').array).all())
コード例 #15
0
    def test_Data_insert_dimension(self):
        """Test the `insert_dimension` Data method."""
        d = cfdm.Data([list(range(12))])
        self.assertEqual(d.shape, (1, 12))
        e = d.squeeze()
        self.assertEqual(e.shape, (12, ))
        self.assertIsNone(d.squeeze(inplace=True))
        self.assertEqual(d.shape, (12, ))

        d = cfdm.Data([list(range(12))])
        d.transpose(inplace=True)
        self.assertEqual(d.shape, (12, 1))
        e = d.squeeze()
        self.assertEqual(e.shape, (12, ))
        self.assertIsNone(d.squeeze(inplace=True))
        self.assertEqual(d.shape, (12, ))

        d.insert_dimension(0, inplace=True)
        d.insert_dimension(-1, inplace=True)
        self.assertEqual(d.shape, (1, 12, 1))
        e = d.squeeze()
        self.assertEqual(e.shape, (12, ))
        e = d.squeeze(-1)
        self.assertEqual(
            e.shape,
            (
                1,
                12,
            ),
        )
        self.assertIsNone(e.squeeze(0, inplace=True))
        self.assertEqual(e.shape, (12, ))

        d = e
        d.insert_dimension(0, inplace=True)
        d.insert_dimension(-1, inplace=True)
        d.insert_dimension(-1, inplace=True)
        self.assertEqual(d.shape, (1, 12, 1, 1))
        e = d.squeeze([0, 2])
        self.assertEqual(e.shape, (12, 1))

        array = numpy.arange(12).reshape(1, 4, 3)
        d = cfdm.Data(array)
        e = d.squeeze()
        f = e.insert_dimension(0)
        a = f.array
        self.assertTrue(numpy.allclose(a, array))

        with self.assertRaises(ValueError):
            d.insert_dimension(1000)
コード例 #16
0
ファイル: test_Data.py プロジェクト: bradyrx/cfdm
    def test_Data_insert_dimension(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        d = cfdm.Data([list(range(12))])
        self.assertEqual(d.shape, (1, 12))
        e = d.squeeze()
        self.assertEqual(e.shape, (12, ))
        self.assertIsNone(d.squeeze(inplace=True))
        self.assertEqual(d.shape, (12, ))

        d = cfdm.Data([list(range(12))])
        d.transpose(inplace=True)
        self.assertEqual(d.shape, (12, 1))
        e = d.squeeze()
        self.assertEqual(e.shape, (12, ))
        self.assertIsNone(d.squeeze(inplace=True))
        self.assertEqual(d.shape, (12, ))

        d.insert_dimension(0, inplace=True)
        d.insert_dimension(-1, inplace=True)
        self.assertEqual(d.shape, (1, 12, 1))
        e = d.squeeze()
        self.assertEqual(e.shape, (12, ))
        e = d.squeeze(-1)
        self.assertEqual(e.shape, (
            1,
            12,
        ))
        self.assertIsNone(e.squeeze(0, inplace=True))
        self.assertEqual(e.shape, (12, ))

        d = e
        d.insert_dimension(0, inplace=True)
        d.insert_dimension(-1, inplace=True)
        d.insert_dimension(-1, inplace=True)
        self.assertEqual(d.shape, (1, 12, 1, 1))
        e = d.squeeze([0, 2])
        self.assertEqual(e.shape, (12, 1))

        array = numpy.arange(12).reshape(1, 4, 3)
        d = cfdm.Data(array)
        e = d.squeeze()
        f = e.insert_dimension(0)
        a = f.array
        self.assertTrue(numpy.allclose(a, array))

        with self.assertRaises(ValueError):
            d.insert_dimension(1000)
コード例 #17
0
    def test_Data_datetime_array(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        d = cfdm.Data(11292.5, units='days since 1970-1-1')
        dt = d.datetime_array[()]
        self.assertTrue(dt == datetime.datetime(2000, 12, 1, 12, 0))
コード例 #18
0
    def test_RaggedIndexedContiguousArray_to_memory(self):
        """TODO DOCS."""
        compressed_data = cfdm.Data([
            280.0,
            281.0,
            279.0,
            278.0,
            279.5,
            281.0,
            282.0,
            278.0,
            279.0,
            277.5,
        ])

        index = cfdm.Index(data=[0, 0, 0, 0, 1, 1, 1, 1])

        cfdm.Count(data=[1, 3, 2, 2])

        r = cfdm.RaggedIndexedContiguousArray(
            compressed_data,
            shape=(2, 2, 3),
            size=12,
            ndim=3,
            index_variable=index,
            count_variable=index,
        )

        r.to_memory()
コード例 #19
0
    def test_Data_datetime_array(self):
        """Test the `datetime_array` Data method."""
        d = cfdm.Data([11292.5, 11293], units="days since 1970-1-1")
        dt = d.datetime_array
        self.assertEqual(dt[0], datetime.datetime(2000, 12, 1, 12, 0))
        self.assertEqual(dt[1], datetime.datetime(2000, 12, 2, 0, 0))

        d[0] = cfdm.masked
        dt = d.datetime_array
        self.assertIs(dt[0], numpy.ma.masked)
        self.assertEqual(dt[1], datetime.datetime(2000, 12, 2, 0, 0))

        d = cfdm.Data(11292.5, units="days since 1970-1-1")
        dt = d.datetime_array
        self.assertEqual(dt[()], datetime.datetime(2000, 12, 1, 12, 0))

        d[()] = cfdm.masked
        dt = d.datetime_array
        self.assertIs(dt[()], numpy.ma.masked)
コード例 #20
0
 def test_Data_any(self):
     """Test the any Data method."""
     d = cfdm.Data([[0, 0, 0]])
     self.assertFalse(d.any())
     d[0, 0] = numpy.ma.masked
     self.assertFalse(d.any())
     d[0, 1] = 3
     self.assertTrue(d.any())
     d[...] = numpy.ma.masked
     self.assertFalse(d.any())
コード例 #21
0
    def test_Data_array(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        # ------------------------------------------------------------
        # Numpy array interface (__array__)
        # ------------------------------------------------------------
        a = numpy.arange(12, dtype='int32').reshape(3, 4)

        d = cfdm.Data(a, units='km')

        b = numpy.array(d)

        self.assertTrue(b.dtype == numpy.dtype('int32'))
        self.assertTrue(a.shape == b.shape)
        self.assertTrue((a == b).all())

        b = numpy.array(d, dtype='float32')

        self.assertTrue(b.dtype == numpy.dtype('float32'))
        self.assertTrue(a.shape == b.shape)
        self.assertTrue((a == b).all())

        # Scalar numeric array
        d = cfdm.Data(9, units='km')
        a = d.array
        self.assertTrue(a.shape == ())
        self.assertTrue(a == numpy.array(9))
        d[...] = cfdm.masked
        a = d.array
        self.assertTrue(a.shape == ())
        self.assertTrue(a[()] is numpy.ma.masked)

        # Non-scalar numeric array
        b = numpy.arange(10 * 15 * 19).reshape(10, 1, 15, 19)
        d = cfdm.Data(b, 'km')
        a = d.array
        a[0, 0, 0, 0] = -999
        a2 = d.array
        self.assertTrue(a2[0, 0, 0, 0] == 0)
        self.assertTrue(a2.shape == b.shape)
        self.assertTrue((a2 == b).all())
        self.assertFalse((a2 == a).all())
コード例 #22
0
ファイル: test_dsg.py プロジェクト: bradyrx/cfdm
    def test_DSG_create_contiguous(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        # Define the ragged array values
        ragged_array = numpy.array([1, 3, 4, 3, 6], dtype='float32')
        # Define the count array values
        count_array = [2, 3]

        # Initialise the count variable
        count_variable = cfdm.Count(data=cfdm.Data(count_array))
        count_variable.set_property('long_name',
                                    'number of obs for this timeseries')

        # Initialise the contiguous ragged array object
        array = cfdm.RaggedContiguousArray(
            compressed_array=cfdm.Data(ragged_array),
            shape=(2, 3), size=6, ndim=2,
            count_variable=count_variable)

        # Initialize the auxiliary coordinate construct with the
        # ragged array and set some properties
        z = cfdm.AuxiliaryCoordinate(
            data=cfdm.Data(array),
            properties={'standard_name': 'height',
                        'units': 'km',
                        'positive': 'up'})

        self.assertTrue((z.data.array == numpy.ma.masked_array(
            data=[[1.0, 3.0, 99],
                  [4.0, 3.0, 6.0]],
            mask=[[False, False,  True],
                  [False, False, False]],
            fill_value=1e+20,
            dtype='float32')).all())

        self.assertEqual(z.data.get_compression_type(), 'ragged contiguous')

        self.assertTrue((z.data.compressed_array == numpy.array(
            [1., 3., 4., 3., 6.], dtype='float32')).all())

        self.assertTrue((z.data.get_count().data.array == numpy.array(
            [2, 3])).all())
コード例 #23
0
    def test_AuxiliaryCoordinate_interior_ring(self):
        c = cfdm.AuxiliaryCoordinate()

        i = cfdm.InteriorRing(data=cfdm.Data(numpy.arange(10).reshape(5, 2)))

        c.set_interior_ring(i)
        self.assertTrue(c.has_interior_ring())
        self.assertIsInstance(c.get_interior_ring(), cfdm.InteriorRing)
        self.assertIsInstance(c.del_interior_ring(None), cfdm.InteriorRing)
        self.assertFalse(c.has_interior_ring())
        self.assertIsNone(c.del_interior_ring(None))
コード例 #24
0
    def test_Data_equals(self):
        """Test the equality-testing Data method."""
        a = numpy.ma.arange(10 * 15 * 19).reshape(10, 1, 15, 19)
        a[0, 0, 2, 3] = numpy.ma.masked

        d = cfdm.Data(a, units="days since 2000-2-2", calendar="noleap")
        e = copy.deepcopy(d)

        self.assertTrue(d.equals(d, verbose=3))
        self.assertTrue(d.equals(e, verbose=3))
        self.assertTrue(e.equals(d, verbose=3))
コード例 #25
0
    def test_DimensionCoordinate_set_data(self):
        x = cfdm.DimensionCoordinate()

        y = x.set_data(cfdm.Data([1, 2, 3]))
        self.assertIsNone(y)
        self.assertTrue(x.has_data())

        # Test inplace
        x.del_data()
        y = x.set_data(cfdm.Data([1, 2, 3]), inplace=False)
        self.assertIsInstance(y, cfdm.DimensionCoordinate)
        self.assertFalse(x.has_data())
        self.assertTrue(y.has_data())

        # Exceptions should be raised for 0-d and N-d (N>=2) data
        with self.assertRaises(Exception):
            y = x.set_data(cfdm.Data([[1, 2, 3]]))

        with self.assertRaises(Exception):
            y = x.set_data(cfdm.Data(1))
コード例 #26
0
ファイル: test_Data.py プロジェクト: davidhassell/cfdm
    def test_Data__format__(self):
        """Test the `__format__` Data method."""
        d = cfdm.Data(9, "metres")
        self.assertEqual(f"{d}", "9 metres")
        self.assertEqual(f"{d!s}", "9 metres")
        self.assertEqual(f"{d!r}", "<Data(): 9 metres>")
        self.assertEqual(f"{d:.3f}", "9.000")

        d = cfdm.Data([[9]], "metres")
        self.assertEqual(f"{d}", "[[9]] metres")
        self.assertEqual(f"{d!s}", "[[9]] metres")
        self.assertEqual(f"{d!r}", "<Data(1, 1): [[9]] metres>")
        self.assertEqual(f"{d:.3f}", "9.000")

        d = cfdm.Data([9, 10], "metres")
        self.assertEqual(f"{d}", "[9, 10] metres")
        self.assertEqual(f"{d!s}", "[9, 10] metres")
        self.assertEqual(f"{d!r}", "<Data(2): [9, 10] metres>")
        with self.assertRaises(ValueError):
            f"{d:.3f}"
コード例 #27
0
    def test_Data_array(self):
        """Test the array Data method."""
        # ------------------------------------------------------------
        # Numpy array interface (__array__)
        # ------------------------------------------------------------
        a = numpy.arange(12, dtype="int32").reshape(3, 4)

        d = cfdm.Data(a, units="km")

        b = numpy.array(d)

        self.assertEqual(b.dtype, numpy.dtype("int32"))
        self.assertEqual(a.shape, b.shape)
        self.assertTrue((a == b).all())

        b = numpy.array(d, dtype="float32")

        self.assertEqual(b.dtype, numpy.dtype("float32"))
        self.assertEqual(a.shape, b.shape)
        self.assertTrue((a == b).all())

        # Scalar numeric array
        d = cfdm.Data(9, units="km")
        a = d.array
        self.assertEqual(a.shape, ())
        self.assertEqual(a, numpy.array(9))
        d[...] = cfdm.masked
        a = d.array
        self.assertEqual(a.shape, ())
        self.assertIs(a[()], numpy.ma.masked)

        # Non-scalar numeric array
        b = numpy.arange(10 * 15 * 19).reshape(10, 1, 15, 19)
        d = cfdm.Data(b, "km")
        a = d.array
        a[0, 0, 0, 0] = -999
        a2 = d.array
        self.assertEqual(a2[0, 0, 0, 0], 0)
        self.assertEqual(a2.shape, b.shape)
        self.assertTrue((a2 == b).all())
        self.assertFalse((a2 == a).all())
コード例 #28
0
ファイル: test_Data.py プロジェクト: bradyrx/cfdm
    def test_Data_any(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        d = cfdm.Data([[0, 0, 0]])
        self.assertFalse(d.any())
        d[0, 0] = numpy.ma.masked
        self.assertFalse(d.any())
        d[0, 1] = 3
        self.assertTrue(d.any())
        d[...] = numpy.ma.masked
        self.assertFalse(d.any())
コード例 #29
0
ファイル: test_Data.py プロジェクト: bradyrx/cfdm
    def test_Data_apply_masking(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        a = numpy.ma.arange(12).reshape(3, 4)
        a[1, 1] = numpy.ma.masked

        d = cfdm.Data(a, units='m')

        self.assertTrue((a == d.array).all())
        self.assertTrue((a.mask == d.mask.array).all())

        b = a.copy()
        e = d.apply_masking()
        self.assertTrue((b == e.array).all())
        self.assertTrue((b.mask == e.mask.array).all())

        b = numpy.ma.where(a == 0, numpy.ma.masked, a)
        e = d.apply_masking(fill_values=[0])
        self.assertTrue((b == e.array).all())
        self.assertTrue((b.mask == e.mask.array).all())

        b = numpy.ma.where((a == 0) | (a == 11), numpy.ma.masked, a)
        e = d.apply_masking(fill_values=[0, 11])
        self.assertTrue((b == e.array).all())
        self.assertTrue((b.mask == e.mask.array).all())

        b = numpy.ma.where(a < 3, numpy.ma.masked, a)
        e = d.apply_masking(valid_min=3)
        self.assertTrue((b == e.array).all())
        self.assertTrue((b.mask == e.mask.array).all())

        b = numpy.ma.where(a > 6, numpy.ma.masked, a)
        e = d.apply_masking(valid_max=6)
        self.assertTrue((b == e.array).all())
        self.assertTrue((b.mask == e.mask.array).all())

        b = numpy.ma.where((a < 2) | (a > 8), numpy.ma.masked, a)
        e = d.apply_masking(valid_range=[2, 8])
        self.assertTrue((b == e.array).all())
        self.assertTrue((b.mask == e.mask.array).all())

        d.set_fill_value(7)

        b = numpy.ma.where(a == 7, numpy.ma.masked, a)
        e = d.apply_masking(fill_values=True)
        self.assertTrue((b == e.array).all())
        self.assertTrue((b.mask == e.mask.array).all())

        b = numpy.ma.where((a == 7) | (a < 2) | (a > 8), numpy.ma.masked, a)
        e = d.apply_masking(fill_values=True, valid_range=[2, 8])
        self.assertTrue((b == e.array).all())
        self.assertTrue((b.mask == e.mask.array).all())
コード例 #30
0
ファイル: test_Data.py プロジェクト: bradyrx/cfdm
    def test_Data_datetime_array(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        d = cfdm.Data([11292.5, 11293], units='days since 1970-1-1')
        dt = d.datetime_array
        self.assertEqual(dt[0], datetime.datetime(2000, 12, 1, 12, 0))
        self.assertEqual(dt[1], datetime.datetime(2000, 12, 2, 0, 0))

        d[0] = cfdm.masked
        dt = d.datetime_array
        self.assertIs(dt[0], numpy.ma.masked)
        self.assertEqual(dt[1], datetime.datetime(2000, 12, 2, 0, 0))

        d = cfdm.Data(11292.5, units='days since 1970-1-1')
        dt = d.datetime_array
        self.assertEqual(dt[()], datetime.datetime(2000, 12, 1, 12, 0))

        d[()] = cfdm.masked
        dt = d.datetime_array
        self.assertIs(dt[()], numpy.ma.masked)