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
66c0f6f3
Commit
66c0f6f3
authored
May 10, 2019
by
Daniel Boeckenhoff
Browse files
setter for fields implemented
parent
25701a93
Changes
1
Hide whitespace changes
Inline
Side-by-side
tfields/core.py
View file @
66c0f6f3
...
...
@@ -166,12 +166,31 @@ class AbstractNdarray(np.ndarray):
Counterpart to __reduce__. Important for unpickling.
"""
# Call the parent's __setstate__ with the other tuple elements.
super
(
AbstractNdarray
,
self
).
__setstate__
(
state
[
0
:
-
len
(
self
.
_iter_slots
())])
# numpy ndarray state has 5 entries
super
(
AbstractNdarray
,
self
).
__setstate__
(
state
[:
5
])
# set the __slot__ attributes
for
i
,
slot
in
enumerate
(
reversed
(
self
.
_iter_slots
())):
index
=
-
(
i
+
1
)
setattr
(
self
,
slot
,
state
[
index
])
valid_slot_attrs
=
list
(
self
.
_iter_slots
())
added_slot_attrs
=
[
'name'
]
# attributes that have been added later
# have not been pickled with the full
# information and thus need to be
# excluded from the __setstate__
# need to be in the same order as they have
# been added to __slots__
n_old
=
len
(
valid_slot_attrs
)
-
len
(
state
[
5
:])
if
n_old
>
0
:
for
latest_index
in
range
(
n_old
):
new_slot
=
added_slot_attrs
[
-
latest_index
]
warnings
.
warn
(
"Slots with names '{new_slot}' appears to have been"
"added after the creation of the reduced state. "
"No corresponding state found in __setstate__."
.
format
(
**
locals
()))
valid_slot_attrs
.
pop
(
valid_slot_attrs
.
index
(
new_slot
))
setattr
(
self
,
new_slot
,
None
)
for
slot_index
,
slot
in
enumerate
(
valid_slot_attrs
):
state_index
=
5
+
slot_index
setattr
(
self
,
slot
,
state
[
state_index
])
@
property
def
bulk
(
self
):
...
...
@@ -181,6 +200,25 @@ class AbstractNdarray(np.ndarray):
"""
return
np
.
array
(
self
)
@
classmethod
@
contextmanager
def
_bypass_setter
(
cls
,
slot
,
demand_existence
=
False
):
"""
Temporarily remove the setter in __slot_setters__ corresponding to slot
position in __slot__. You should know what you do, when using this.
"""
slot_index
=
cls
.
__slots__
.
index
(
slot
)
if
slot
in
cls
.
__slots__
else
None
if
slot_index
is
None
:
if
demand_existence
:
raise
ValueError
(
"Slot {slot} not existing"
.
format
(
**
locals
()))
else
:
yield
return
setter
=
cls
.
__slot_setters__
[
slot_index
]
cls
.
__slot_setters__
[
slot_index
]
=
None
yield
cls
.
__slot_setters__
[
slot_index
]
=
setter
def
copy
(
self
,
*
args
,
**
kwargs
):
"""
The standard ndarray copy does not copy slots. Correct for this.
...
...
@@ -387,18 +425,19 @@ class AbstractNdarray(np.ndarray):
bulk_type
=
getattr
(
tfields
,
bulk_type
)
list_dict
[
key
].
append
(
bulk_type
.
_from_dict
(
**
sub_dict
[
index
]))
'''
Build the normal way
'''
bulk
=
kwargs
.
pop
(
'bulk'
)
bulk_type
=
kwargs
.
pop
(
'bulk_type'
)
obj
=
cls
.
__new__
(
cls
,
bulk
,
**
kwargs
)
'''
Set list attributes
'''
for
attr
,
list_value
in
list_dict
.
items
():
setattr
(
obj
,
attr
,
list_value
)
with
cls
.
_bypass_setter
(
'fields'
):
'''
Build the normal way
'''
bulk
=
kwargs
.
pop
(
'bulk'
)
bulk_type
=
kwargs
.
pop
(
'bulk_type'
)
obj
=
cls
.
__new__
(
cls
,
bulk
,
**
kwargs
)
'''
Set list attributes
'''
for
attr
,
list_value
in
list_dict
.
items
():
setattr
(
obj
,
attr
,
list_value
)
return
obj
...
...
@@ -1350,6 +1389,33 @@ class Tensors(AbstractNdarray):
return
artist
def
as_tensors_list
(
tensors_list
):
"""
Setter for TensorFields.fields
Copies input
Examples:
>>> import tfields
>>> import numpy as np
>>> scalars = tfields.Tensors([0, 1, 2])
>>> vectors = tfields.Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> maps = [tfields.TensorFields([[0, 1, 2], [0, 1, 2]]),
... tfields.TensorFields([[1], [2]], [-42, -21])]
>>> mesh = tfields.TensorMaps(vectors, scalars,
... maps=maps)
>>> mesh.maps[0].fields = [[42, 21]]
>>> assert len(mesh.maps[0].fields) == 1
>>> assert mesh.maps[0].fields[0].equal([42, 21])
"""
if
tensors_list
is
not
None
:
new_list
=
[]
for
tensors
in
tensors_list
:
tensors_list
=
Tensors
(
tensors
)
new_list
.
append
(
tensors_list
)
tensors_list
=
new_list
return
tensors_list
class
TensorFields
(
Tensors
):
"""
Discrete Tensor Field
...
...
@@ -1427,6 +1493,9 @@ class TensorFields(Tensors):
"""
__slots__
=
[
'coord_sys'
,
'name'
,
'fields'
]
__slot_setters__
=
[
tfields
.
bases
.
get_coord_system_name
,
None
,
as_tensors_list
]
def
__new__
(
cls
,
tensors
,
*
fields
,
**
kwargs
):
rigid
=
kwargs
.
pop
(
'rigid'
,
True
)
...
...
@@ -1435,12 +1504,12 @@ class TensorFields(Tensors):
if
issubclass
(
type
(
tensors
),
TensorFields
):
if
tensors
.
fields
is
None
:
raise
ValueError
(
"Tensor fields were None"
)
obj
.
fields
=
[
Tensors
(
field
)
for
field
in
tensors
.
fields
]
obj
.
fields
=
tensors
.
fields
elif
not
fields
:
obj
.
fields
=
[]
if
fields
:
# (over)write fields
obj
.
fields
=
[
Tensors
(
field
)
for
field
in
fields
]
obj
.
fields
=
fields
if
rigid
:
olen
=
len
(
obj
)
...
...
@@ -1459,7 +1528,10 @@ class TensorFields(Tensors):
>>> import tfields
>>> import numpy as np
>>> vectors = tfields.Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> scalar_field = tfields.TensorFields(vectors, [42, 21, 10.5], [1, 2, 3])
>>> scalar_field = tfields.TensorFields(vectors,
... [42, 21, 10.5],
... [1, 2, 3],
... [[0, 0], [-1, -1], [-2, -2]])
Slicing
...
...
@@ -1472,6 +1544,7 @@ class TensorFields(Tensors):
>>> picked = scalar_field[1]
>>> assert np.array_equal(picked, [0, 0, 1])
>>> assert np.array_equal(picked.fields[0], 21)
Masking
...
...
@@ -1491,7 +1564,9 @@ class TensorFields(Tensors):
if
isinstance
(
index
,
tuple
):
index
=
index
[
0
]
if
item
.
fields
:
item
.
fields
=
[
field
.
__getitem__
(
index
)
for
field
in
item
.
fields
]
# circumvent the setter here.
with
self
.
_bypass_setter
(
'fields'
):
item
.
fields
=
[
field
.
__getitem__
(
index
)
for
field
in
item
.
fields
]
except
IndexError
as
err
:
warnings
.
warn
(
"Index error occured for field.__getitem__. Error "
"message: {err}"
.
format
(
**
locals
()))
...
...
@@ -1572,7 +1647,8 @@ class TensorFields(Tensors):
@
names
.
setter
def
names
(
self
,
names
):
if
not
len
(
names
)
==
len
(
self
.
fields
):
raise
ValueError
(
"len(names) != len(fields)"
)
raise
ValueError
(
"len(names) ({0}) != len(fields) ({1})"
.
format
(
len
(
names
),
len
(
self
.
fields
)))
for
i
,
name
in
enumerate
(
names
):
self
.
fields
[
i
].
name
=
name
...
...
@@ -2129,6 +2205,7 @@ class Container(AbstractNdarray):
if
__name__
==
'__main__'
:
# pragma: no cover
import
doctest
doctest
.
testmod
()
# doctest.run_docstring_examples(as_tensors_list, globals())
# doctest.run_docstring_examples(Tensors._save_npz, globals())
# doctest.run_docstring_examples(TensorMaps.cut, globals())
# doctest.run_docstring_examples(AbstractNdarray._save_npz, globals())
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