Commit 35c7faed authored by dboe's avatar dboe
Browse files

tests unified with tensor_grid

parent ed4818cd
Pipeline #104530 passed with stages
in 56 seconds
......@@ -17,9 +17,8 @@ class Base_Check(object):
self.assertIsNot(self._inst, other)
def test_pickle(self):
with NamedTemporaryFile(suffix='.pickle') as out_file:
pickle.dump(self._inst,
out_file)
with NamedTemporaryFile(suffix=".pickle") as out_file:
pickle.dump(self._inst, out_file)
out_file.flush()
out_file.seek(0)
reloaded = pickle.load(out_file)
......@@ -28,6 +27,7 @@ class Base_Check(object):
def test_deep_copy(self):
from copy import deepcopy
other = deepcopy(self._inst)
self.demand_deep_copy(other)
......@@ -40,7 +40,7 @@ class Base_Check(object):
self.demand_deep_copy(other)
def test_save_npz(self):
out_file = NamedTemporaryFile(suffix='.npz')
out_file = NamedTemporaryFile(suffix=".npz")
self._inst.save(out_file.name)
_ = out_file.seek(0) # this is only necessary in the test
load_inst = type(self._inst).load(out_file.name)
......@@ -65,6 +65,7 @@ class Tensors_Check(AbstractNdarray_Check):
"""
Testing derivatives of Tensors
"""
_inst = None
def demand_equal(self, other, atol=False, transformed=False):
......@@ -78,15 +79,14 @@ class Tensors_Check(AbstractNdarray_Check):
self.assertEqual(self._inst.name, other.name)
def test_slice_indexing(self):
self.demand_index_equal(slice(0, None, None), 'type')
self.demand_index_equal(slice(0, None, None), "type")
def test_pick_indexing(self):
self.demand_index_equal(0, (np.ndarray, np.integer, np.float))
self.demand_index_equal(0, (np.ndarray, np.integer, float))
def test_mask_indexing(self):
mask = np.array([True if i % 2 == 0 else False
for i in range(len(self._inst))])
self.demand_index_equal(mask, 'type')
mask = np.array([True if i % 2 == 0 else False for i in range(len(self._inst))])
self.demand_index_equal(mask, "type")
def test_iteration(self):
# iteration
......@@ -95,7 +95,7 @@ class Tensors_Check(AbstractNdarray_Check):
next(iterator)
def demand_index_equal(self, index, check_type):
if check_type == 'type':
if check_type == "type":
check_type = type(self._inst)
tensors = np.array(self._inst)
if len(self._inst) > 0:
......@@ -148,19 +148,20 @@ class Tensors_Check(AbstractNdarray_Check):
# check that all copies are the same also with new coord_sys
for i in range(len(merge_list)):
value = np.allclose(merge_list[0],
obj[i * len(self._inst): (i + 1) *
len(self._inst)],
atol=ATOL)
value = np.allclose(
merge_list[0],
obj[i * len(self._inst) : (i + 1) * len(self._inst)],
atol=ATOL,
)
self.assertTrue(value)
obj_cs = type(self._inst).merged(*merge_list,
coord_sys=tfields.bases.CYLINDER)
obj_cs = type(self._inst).merged(*merge_list, coord_sys=tfields.bases.CYLINDER)
for i in range(len(merge_list)):
value = np.allclose(merge_list[1],
obj_cs[i * len(self._inst): (i + 1) *
len(self._inst)],
atol=ATOL)
value = np.allclose(
merge_list[1],
obj_cs[i * len(self._inst) : (i + 1) * len(self._inst)],
atol=ATOL,
)
self.assertTrue(value)
......@@ -183,10 +184,13 @@ class TensorFields_Check(Tensors_Check):
if len(self._inst) > 0:
item = self._inst[index]
for i, field in enumerate(self._inst.fields):
if check_type == 'type':
if check_type == "type":
check_type = type(self._inst.fields[i])
self.assertTrue(np.array_equal(
item.fields[i], np.array(self._inst.fields[i])[index]))
self.assertTrue(
np.array_equal(
item.fields[i], np.array(self._inst.fields[i])[index]
)
)
self.assertIsInstance(item.fields[i], check_type)
def demand_deep_copy(self, other):
......@@ -204,8 +208,7 @@ class TensorMaps_Check(TensorFields_Check):
clean = self._inst.cleaned()
# no faces are removed
for map_dim in self._inst.maps:
self.assertEqual(len(self._inst.maps[map_dim]),
len(clean.maps[map_dim]))
self.assertEqual(len(self._inst.maps[map_dim]), len(clean.maps[map_dim]))
def demand_index_equal(self, index, check_type):
super().demand_index_equal(index, check_type)
......@@ -245,39 +248,43 @@ class TensorMaps_Empty_Test(TensorMaps_Check, unittest.TestCase):
class TensorFields_Test(Tensors_Check, unittest.TestCase):
def setUp(self):
base = [(-5, 5, 11)] * 3
self._fields = [tfields.Tensors.grid(*base, coord_sys='cylinder'),
tfields.Tensors(range(11**3))]
self._fields = [
tfields.Tensors.grid(*base, coord_sys="cylinder"),
tfields.Tensors(range(11 ** 3)),
]
tensors = tfields.Tensors.grid(*base)
self._inst = tfields.TensorFields(tensors, *self._fields)
self.assertTrue(self._fields[0].coord_sys, 'cylinder')
self.assertTrue(self._fields[1].coord_sys, 'cartesian')
self.assertTrue(self._fields[0].coord_sys, "cylinder")
self.assertTrue(self._fields[1].coord_sys, "cartesian")
class TensorMaps_Test(Tensors_Check, unittest.TestCase):
def setUp(self):
base = [(-1, 1, 3)] * 3
tensors = tfields.Tensors.grid(*base)
self._fields = [tfields.Tensors.grid(*base, coord_sys='cylinder'),
tfields.Tensors(range(len(tensors)))]
self._maps_tensors = [[[0, 0, 0],
[1, 2, 3],
[1, 5, 9]],
[[0, 4],
[1, 3]],
[[42]]]
self._maps_fields = [[[42., 21., 11]],
[[3, 25]],
[[111]]]
self._maps = [tfields.TensorFields(map_tensors,
*map_fields) for map_tensors,
map_fields in zip(self._maps_tensors, self._maps_fields)]
self._inst = tfields.TensorMaps(tensors, *self._fields,
maps=self._maps)
self._fields = [
tfields.Tensors.grid(*base, coord_sys="cylinder"),
tfields.Tensors(range(len(tensors))),
]
self._maps_tensors = [
[[0, 0, 0], [1, 2, 3], [1, 5, 9]],
[[0, 4], [1, 3]],
[[42]],
]
self._maps_fields = [[[42.0, 21.0, 11]], [[3, 25]], [[111]]]
self._maps = [
tfields.TensorFields(map_tensors, *map_fields)
for map_tensors, map_fields in zip(self._maps_tensors, self._maps_fields)
]
self._inst = tfields.TensorMaps(tensors, *self._fields, maps=self._maps)
def test_legacy(self):
this_dir = pathlib.Path(__file__).parent
legacy_file = this_dir / 'resources/TensorMaps_0.2.1_ce3ea1fb69058dc39815be65f485abebb487a6bd.npz' # NOQA
legacy_file = (
this_dir
/ "resources/TensorMaps_0.2.1_ce3ea1fb69058dc39815be65f485abebb487a6bd.npz"
) # NOQA
tm = tfields.TensorMaps.load(legacy_file)
self.assertTrue(self._inst.equal(tm))
......@@ -285,14 +292,12 @@ class TensorMaps_Test(Tensors_Check, unittest.TestCase):
class TensorMaps_Indexing_Test(unittest.TestCase):
def setUp(self):
tensors = np.arange(10).reshape((-1, 1))
self._maps_tensors = [[[0, 0, 0],
[1, 2, 3],
[3, 5, 9]],
[[6, 4],
[7, 8]],
[[7]]]
self._inst = tfields.TensorMaps(tensors,
maps=self._maps_tensors)
self._maps_tensors = [
[[0, 0, 0], [1, 2, 3], [3, 5, 9]],
[[6, 4], [7, 8]],
[[7]],
]
self._inst = tfields.TensorMaps(tensors, maps=self._maps_tensors)
def test_pick_indexing(self):
pick = self._inst[7]
......@@ -313,8 +318,9 @@ class TensorMaps_Indexing_Test(unittest.TestCase):
self.assertTrue(len(slce.maps), 2)
def test_mask_indexing(self):
mask = self._inst[np.array([False, True, True, True, True,
True, True, False, False, False])]
mask = self._inst[
np.array([False, True, True, True, True, True, True, False, False, False])
]
self.assertTrue(mask.equal([[1], [2], [3], [4], [5], [6]]))
self.assertTrue(np.array_equal(mask.maps[3], [[0, 1, 2]]))
self.assertTrue(np.array_equal(mask.maps[2], [[5, 3]]))
......@@ -325,7 +331,7 @@ class TensorMaps_NoFields_Test(Tensors_Check, unittest.TestCase):
def setUp(self):
self._inst = tfields.TensorMaps(
[[1, 2, 3], [3, 3, 3], [0, 0, 0], [5, 6, 7]],
maps=[[[0, 1, 2], [1, 2, 3]], [[1]], [[0, 1, 2, 3]]]
maps=[[[0, 1, 2], [1, 2, 3]], [[1]], [[0, 1, 2, 3]]],
)
......@@ -336,12 +342,8 @@ class Maps_Test(Base_Check, unittest.TestCase):
def setUp(self):
self._inst = tfields.Maps(
[[[0, 0, 0],
[1, 2, 3],
[1, 5, 9]],
[[0, 4],
[1, 3]],
[[42]]])
[[[0, 0, 0], [1, 2, 3], [1, 5, 9]], [[0, 4], [1, 3]], [[42]]]
)
class Maps_Init_Test(Maps_Test):
......@@ -388,10 +390,11 @@ class Container_Test(Container_Check, unittest.TestCase):
(1, 1, 1),
(-np.pi, np.pi, 3),
(-np.pi / 2, np.pi / 2, 3),
coord_sys='spherical')
coord_sys="spherical",
)
sphere2 = sphere.copy() * 3
self._inst = tfields.Container([sphere, sphere2], labels=['test'])
self._inst = tfields.Container([sphere, sphere2], labels=["test"])
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
......@@ -4,9 +4,10 @@ import numpy as np
import tfields
from tfields.lib import grid
from tfields.tensor_grid import TensorGrid
from tests.test_core import TensorFields_Check
class TensorGrid_Test(unittest.TestCase):
class TensorGrid_Test(unittest.TestCase, TensorFields_Check):
def setUp(self):
self.bv = [(0, 1, 2j), (2, 4, 3j), (5, 8, 4j)]
self.iter_order = [0, 1, 2]
......@@ -38,10 +39,7 @@ class TensorGrid_Test(unittest.TestCase):
[1.0, 4.0, 8.0],
]
)
def get_tg(self):
tg = TensorGrid.from_base_vectors(*self.bv, iter_order=self.iter_order)
return tg
self._inst = TensorGrid.from_base_vectors(*self.bv, iter_order=self.iter_order)
def check_filled(self, tg):
self.assertTrue(tg.equal(self.res))
......@@ -51,8 +49,7 @@ class TensorGrid_Test(unittest.TestCase):
self.assertTrue(tg.is_empty())
def test_from_base_vectors(self):
tg = self.get_tg()
self.check_filled(tg)
self.check_filled(self._inst)
def test_empty(self):
tg = TensorGrid.empty(*self.bv, iter_order=self.iter_order)
......@@ -93,6 +90,7 @@ class TensorGrid_Test_Permutation1(TensorGrid_Test):
[1.0, 4.0, 8.0],
]
)
self._inst = TensorGrid.from_base_vectors(*self.bv, iter_order=self.iter_order)
class TensorGrid_Test_IO_Change(unittest.TestCase):
......@@ -102,10 +100,10 @@ class TensorGrid_Test_IO_Change(unittest.TestCase):
t2 = TensorGrid_Test_Permutation1()
t2.setUp()
tg1 = t1.get_tg()
tg1 = t1._inst
tg1.fields.append(tfields.Tensors(tg1))
tg1_original = tg1.copy()
tg2 = t2.get_tg()
tg2 = t2._inst
tg2.fields.append(tfields.Tensors(tg2))
tg1.change_iter_order(t2.iter_order)
......
......@@ -988,7 +988,7 @@ class Tensors(AbstractNdarray): # pylint: disable=too-many-public-methods
>>> assert t_cyl[0, 0] == 3
"""
if self.rank == 0 or self.shape[0] == 0:
if self.rank == 0 or any(s == 0 for s in self.shape):
# scalar or empty
self.coord_sys = coord_sys # pylint: disable=attribute-defined-outside-init
return
......
......@@ -26,7 +26,7 @@ class TensorGrid(TensorFields):
default_base_vectors = tensors.base_vectors
default_iter_order = tensors.iter_order
else:
default_base_vectors = kwargs.pop("base_vectors")
default_base_vectors = kwargs.pop("base_vectors", None)
default_iter_order = np.arange(len(default_base_vectors))
base_vectors = kwargs.pop("base_vectors", default_base_vectors)
iter_order = kwargs.pop("iter_order", default_iter_order)
......@@ -54,6 +54,22 @@ class TensorGrid(TensorFields):
return cls.from_base_vectors(*base_vectors, tensors=tensors, **kwargs)
@classmethod
def merged(cls, *objects, **kwargs):
if "base_vectors" not in kwargs:
base_vectors = None
for obj in objects:
if base_vectors is None:
base_vectors = obj.base_vectors
else:
if not all(
[a == b for a, b in zip(base_vectors, obj.base_vectors)]
):
raise NotImplementedError("Non-alligned base vectors")
kwargs.setdefault("base_vectors", base_vectors)
merge = super().merged(*objects, **kwargs)
return merge
@property
def rank(self):
if self.is_empty():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment