Example #1
0
class TestStandard(tests.IrisTest):
    def setUp(self):
        # Crete an instance, with non-default arguments to allow testing of
        # correct property setting.
        self.kwargs = {
            "indices": np.linspace(1, 9, 9, dtype=int).reshape((3, -1)),
            "cf_role": "face_node_connectivity",
            "long_name": "my_face_nodes",
            "var_name": "face_nodes",
            "attributes": {"notes": "this is a test"},
            "start_index": 1,
            "src_dim": 1,
        }
        self.connectivity = Connectivity(**self.kwargs)

    def test_cf_role(self):
        self.assertEqual(self.kwargs["cf_role"], self.connectivity.cf_role)

    def test_src_location(self):
        expected = self.kwargs["cf_role"].split("_")[0]
        self.assertEqual(expected, self.connectivity.src_location)

    def test_tgt_location(self):
        expected = self.kwargs["cf_role"].split("_")[1]
        self.assertEqual(expected, self.connectivity.tgt_location)

    def test_start_index(self):
        self.assertEqual(
            self.kwargs["start_index"], self.connectivity.start_index
        )

    def test_src_dim(self):
        self.assertEqual(self.kwargs["src_dim"], self.connectivity.src_dim)

    def test_indices(self):
        self.assertArrayEqual(
            self.kwargs["indices"], self.connectivity.indices
        )

    def test_read_only(self):
        attributes = ("indices", "cf_role", "start_index", "src_dim")
        for attribute in attributes:
            self.assertRaisesRegex(
                AttributeError,
                "can't set attribute",
                setattr,
                self.connectivity,
                attribute,
                1,
            )

    def test_transpose(self):
        expected_dim = 1 - self.kwargs["src_dim"]
        expected_indices = self.kwargs["indices"].transpose()
        new_connectivity = self.connectivity.transpose()
        self.assertEqual(expected_dim, new_connectivity.src_dim)
        self.assertArrayEqual(expected_indices, new_connectivity.indices)

    def test_lazy_indices(self):
        self.assertTrue(is_lazy_data(self.connectivity.lazy_indices()))

    def test_core_indices(self):
        self.assertArrayEqual(
            self.kwargs["indices"], self.connectivity.core_indices()
        )

    def test_has_lazy_indices(self):
        self.assertFalse(self.connectivity.has_lazy_indices())

    def test_lazy_src_lengths(self):
        self.assertTrue(is_lazy_data(self.connectivity.lazy_src_lengths()))

    def test_src_lengths(self):
        expected = [3, 3, 3]
        self.assertArrayEqual(expected, self.connectivity.src_lengths())

    def test___str__(self):
        expected = (
            "Connectivity(cf_role='face_node_connectivity', start_index=1)"
        )
        self.assertEqual(expected, self.connectivity.__str__())

    def test___repr__(self):
        expected = (
            "Connectivity(array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), "
            "cf_role='face_node_connectivity', long_name='my_face_nodes', "
            "var_name='face_nodes', attributes={'notes': 'this is a test'}, "
            "start_index=1, src_dim=1)"
        )
        self.assertEqual(expected, self.connectivity.__repr__())

    def test_xml_element(self):
        doc = minidom.Document()
        connectivity_element = self.connectivity.xml_element(doc)
        self.assertEqual(connectivity_element.tagName, "connectivity")
        for attribute in ("cf_role", "start_index", "src_dim"):
            self.assertIn(attribute, connectivity_element.attributes)

    def test___eq__(self):
        equivalent_kwargs = self.kwargs
        equivalent_kwargs["indices"] = self.kwargs["indices"].transpose()
        equivalent_kwargs["src_dim"] = 1 - self.kwargs["src_dim"]
        equivalent = Connectivity(**equivalent_kwargs)
        self.assertFalse(
            (equivalent.indices == self.connectivity.indices).all()
        )
        self.assertEqual(equivalent, self.connectivity)

    def test_different(self):
        different_kwargs = self.kwargs
        different_kwargs["indices"] = self.kwargs["indices"].transpose()
        different = Connectivity(**different_kwargs)
        self.assertNotEqual(different, self.connectivity)

    def test_no_cube_dims(self):
        self.assertRaises(NotImplementedError, self.connectivity.cube_dims, 1)

    def test_shape(self):
        self.assertEqual(self.kwargs["indices"].shape, self.connectivity.shape)

    def test_ndim(self):
        self.assertEqual(self.kwargs["indices"].ndim, self.connectivity.ndim)

    def test___getitem_(self):
        subset = self.connectivity[:, 0:1]
        self.assertArrayEqual(self.kwargs["indices"][:, 0:1], subset.indices)

    def test_copy(self):
        new_indices = np.linspace(11, 16, 6, dtype=int).reshape((3, -1))
        copy_connectivity = self.connectivity.copy(new_indices)
        self.assertArrayEqual(new_indices, copy_connectivity.indices)

    def test_indices_by_src(self):
        expected = self.kwargs["indices"].transpose()
        self.assertArrayEqual(expected, self.connectivity.indices_by_src())

    def test_indices_by_src_input(self):
        expected = as_lazy_data(self.kwargs["indices"].transpose())
        by_src = self.connectivity.indices_by_src(
            self.connectivity.lazy_indices()
        )
        self.assertArrayEqual(expected, by_src)
Example #2
0
class TestStandard(tests.IrisTest):
    def setUp(self):
        # Crete an instance, with non-default arguments to allow testing of
        # correct property setting.
        self.kwargs = {
            "indices": np.linspace(1, 12, 12, dtype=int).reshape((4, -1)),
            "cf_role": "face_node_connectivity",
            "long_name": "my_face_nodes",
            "var_name": "face_nodes",
            "attributes": {
                "notes": "this is a test"
            },
            "start_index": 1,
            "location_axis": 1,
        }
        self.connectivity = Connectivity(**self.kwargs)

    def test_cf_role(self):
        self.assertEqual(self.kwargs["cf_role"], self.connectivity.cf_role)

    def test_location(self):
        expected = self.kwargs["cf_role"].split("_")[0]
        self.assertEqual(expected, self.connectivity.location)

    def test_connected(self):
        expected = self.kwargs["cf_role"].split("_")[1]
        self.assertEqual(expected, self.connectivity.connected)

    def test_start_index(self):
        self.assertEqual(self.kwargs["start_index"],
                         self.connectivity.start_index)

    def test_location_axis(self):
        self.assertEqual(self.kwargs["location_axis"],
                         self.connectivity.location_axis)

    def test_indices(self):
        self.assertArrayEqual(self.kwargs["indices"],
                              self.connectivity.indices)

    def test_read_only(self):
        attributes = ("indices", "cf_role", "start_index", "location_axis")
        for attribute in attributes:
            self.assertRaisesRegex(
                AttributeError,
                "can't set attribute",
                setattr,
                self.connectivity,
                attribute,
                1,
            )

    def test_transpose(self):
        expected_dim = 1 - self.kwargs["location_axis"]
        expected_indices = self.kwargs["indices"].transpose()
        new_connectivity = self.connectivity.transpose()
        self.assertEqual(expected_dim, new_connectivity.location_axis)
        self.assertArrayEqual(expected_indices, new_connectivity.indices)

    def test_lazy_indices(self):
        self.assertTrue(is_lazy_data(self.connectivity.lazy_indices()))

    def test_core_indices(self):
        self.assertArrayEqual(self.kwargs["indices"],
                              self.connectivity.core_indices())

    def test_has_lazy_indices(self):
        self.assertFalse(self.connectivity.has_lazy_indices())

    def test_lazy_location_lengths(self):
        self.assertTrue(is_lazy_data(
            self.connectivity.lazy_location_lengths()))

    def test_location_lengths(self):
        expected = [4, 4, 4]
        self.assertArrayEqual(expected, self.connectivity.location_lengths())

    def test___str__(self):
        expected = "\n".join([
            "Connectivity :  my_face_nodes / (unknown)",
            "    data: [",
            "        [ 1,  2,  3],",
            "        [ 4,  5,  6],",
            "        [ 7,  8,  9],",
            "        [10, 11, 12]]",
            "    shape: (4, 3)",
            "    dtype: int64",
            "    long_name: 'my_face_nodes'",
            "    var_name: 'face_nodes'",
            "    attributes:",
            "        notes  'this is a test'",
            "    cf_role: 'face_node_connectivity'",
            "    start_index: 1",
            "    location_axis: 1",
        ])
        self.assertEqual(expected, self.connectivity.__str__())

    def test___repr__(self):
        expected = "<Connectivity: my_face_nodes / (unknown)  [[1, 2, 3], ...]  shape(4, 3)>"
        self.assertEqual(expected, self.connectivity.__repr__())

    def test_xml_element(self):
        doc = minidom.Document()
        connectivity_element = self.connectivity.xml_element(doc)
        self.assertEqual(connectivity_element.tagName, "connectivity")
        for attribute in ("cf_role", "start_index", "location_axis"):
            self.assertIn(attribute, connectivity_element.attributes)

    def test___eq__(self):
        equivalent_kwargs = self.kwargs
        equivalent_kwargs["indices"] = self.kwargs["indices"].transpose()
        equivalent_kwargs["location_axis"] = 1 - self.kwargs["location_axis"]
        equivalent = Connectivity(**equivalent_kwargs)
        self.assertFalse(
            np.array_equal(equivalent.indices, self.connectivity.indices))
        self.assertEqual(equivalent, self.connectivity)

    def test_different(self):
        different_kwargs = self.kwargs
        different_kwargs["indices"] = self.kwargs["indices"].transpose()
        different = Connectivity(**different_kwargs)
        self.assertNotEqual(different, self.connectivity)

    def test_no_cube_dims(self):
        self.assertRaises(NotImplementedError, self.connectivity.cube_dims, 1)

    def test_shape(self):
        self.assertEqual(self.kwargs["indices"].shape, self.connectivity.shape)

    def test_ndim(self):
        self.assertEqual(self.kwargs["indices"].ndim, self.connectivity.ndim)

    def test___getitem_(self):
        subset = self.connectivity[:, 0:1]
        self.assertArrayEqual(self.kwargs["indices"][:, 0:1], subset.indices)

    def test_copy(self):
        new_indices = np.linspace(11, 16, 6, dtype=int).reshape((3, -1))
        copy_connectivity = self.connectivity.copy(new_indices)
        self.assertArrayEqual(new_indices, copy_connectivity.indices)

    def test_indices_by_location(self):
        expected = self.kwargs["indices"].transpose()
        self.assertArrayEqual(expected,
                              self.connectivity.indices_by_location())

    def test_indices_by_location_input(self):
        expected = as_lazy_data(self.kwargs["indices"].transpose())
        by_location = self.connectivity.indices_by_location(
            self.connectivity.lazy_indices())
        self.assertArrayEqual(expected, by_location)