Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Daniel Boeckenhoff
tfields
Commits
3dc433b0
Commit
3dc433b0
authored
May 08, 2019
by
Daniel Boeckenhoff
Committed by
Daniel Boeckenhoff
Mar 18, 2020
Browse files
introduced name attribute
parent
4258ec5d
Changes
1
Hide whitespace changes
Inline
Side-by-side
tfields/core.py
View file @
3dc433b0
...
...
@@ -283,9 +283,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])]
...
...
@@ -306,6 +306,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
)
...
...
@@ -477,7 +482,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
]
...
...
@@ -494,6 +499,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
:
...
...
@@ -1419,7 +1425,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
)
...
...
@@ -1442,6 +1448,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
):
...
...
@@ -1544,6 +1551,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:
...
...
@@ -1653,7 +1686,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'
,
[])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment