Commit 25701a93 authored by Daniel Boeckenhoff's avatar Daniel Boeckenhoff
Browse files

introduced name attribute

parent 3f7e9998
......@@ -282,9 +282,9 @@ class AbstractNdarray(np.ndarray):
>>> import tfields
>>> from tempfile import NamedTemporaryFile
>>> out_file = NamedTemporaryFile(suffix='.npz')
>>> p = tfields.Points3D([[1., 2., 3.], [4., 5., 6.], [1, 2, -6]])
>>> scalars = tfields.Tensors([0, 1, 2])
>>> p = tfields.Points3D([[1., 2., 3.], [4., 5., 6.], [1, 2, -6]],
... name='my_points')
>>> scalars = tfields.Tensors([0, 1, 2], name=42)
>>> vectors = tfields.Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> maps = [tfields.TensorFields([[0, 1, 2], [0, 1, 2]], [42, 21]),
... tfields.TensorFields([[1], [2]], [-42, -21])]
......@@ -305,6 +305,11 @@ class AbstractNdarray(np.ndarray):
>>> assert m.equal(m1)
>>> assert m.maps[0].dtype == m1.maps[0].dtype
Names are preserved
>>> assert p.name == 'my_points'
>>> m.names
[42]
"""
content_dict = self._as_dict()
np.savez(path, **content_dict)
......@@ -406,6 +411,8 @@ class Tensors(AbstractNdarray):
Args:
tensors: np.ndarray or AbstractNdarray subclass
**kwargs:
name: optional - custom name, can be anything
Examples:
>>> import numpy as np
......@@ -476,7 +483,7 @@ class Tensors(AbstractNdarray):
Tensors([], shape=(0, 7), dtype=float64)
"""
__slots__ = ['coord_sys']
__slots__ = ['coord_sys', 'name']
__slot_defaults__ = ['cartesian']
__slot_setters__ = [tfields.bases.get_coord_system_name]
......@@ -493,6 +500,7 @@ class Tensors(AbstractNdarray):
tensors = tensors.copy()
tensors.transform(coord_sys)
kwargs['coord_sys'] = coord_sys
kwargs['name'] = kwargs.pop('name', tensors.name)
if dtype is None:
dtype = tensors.dtype
else:
......@@ -1418,7 +1426,7 @@ class TensorFields(Tensors):
>>> assert len(loose) != 1
"""
__slots__ = ['coord_sys', 'fields']
__slots__ = ['coord_sys', 'name', 'fields']
def __new__(cls, tensors, *fields, **kwargs):
rigid = kwargs.pop('rigid', True)
......@@ -1441,6 +1449,7 @@ class TensorFields(Tensors):
raise ValueError("Length of base ({olen}) should be the same as"
" the length of all fields ({field_lengths})."
.format(**locals()))
return obj
def __getitem__(self, index):
......@@ -1541,6 +1550,32 @@ class TensorFields(Tensors):
inst = cls.__new__(cls, inst, *fields)
return inst
@property
def names(self):
"""
Retrive the names of the fields as a list
Examples:
>>> import numpy as np
>>> import tfields
>>> s = tfields.Tensors([1,2,3], name=1.)
>>> tf = tfields.TensorFields(s, *[s]*10)
>>> assert len(tf.names) == 10
>>> assert set(tf.names) == {1.}
>>> tf.names = range(10)
>>> tf.names
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
return [f.name for f in self.fields]
@names.setter
def names(self, names):
if not len(names) == len(self.fields):
raise ValueError("len(names) != len(fields)")
for i, name in enumerate(names):
self.fields[i].name = name
# def transform(self, coord_sys):
# super(TensorFields, self).transform(coord_sys)
# # for field in self.fields:
......@@ -1650,7 +1685,7 @@ class TensorMaps(TensorFields):
ValueError: Incorrect map rank 0
"""
__slots__ = ['coord_sys', 'fields', 'maps']
__slots__ = ['coord_sys', 'name', 'fields', 'maps']
def __new__(cls, tensors, *fields, **kwargs):
maps = kwargs.pop('maps', [])
......
Supports Markdown
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