Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not lose track of dtypes in get_array_ragged #494

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pyiron_base/generic/flattenedstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,12 @@ def get_array_ragged(self, name: str) -> np.ndarray:
"""
if name in self._per_chunk_arrays:
return self.get_array(name)
return np.array([self.get_array(name, i) for i in range(len(self))],
dtype=object)
# pre-allocated as dtype=object, then setting individual elements makes sure that element arrays retain their
# dtype
result = np.empty(len(self), dtype=object)
for i in range(len(self)):
result[i] = self.get_array(name, i)
return result

def get_array_filled(self, name: str) -> np.ndarray:
"""
Expand Down
14 changes: 14 additions & 0 deletions tests/generic/test_flattenedstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ def test_get_array_ragged(self):
store.get_array_ragged("chunk"),
), "get_array_ragged does not give same result as get_array for per chunk array")

def test_get_array_ragged_dtype_stability(self):
"""get_array_ragged should (only!) convert top-most dimension to dtype=object and be of shape (n,) """
# regression test
store = FlattenedStorage(elem=[ [1, 2], [3, 4], [5, 6] ])
ragged = store.get_array_ragged("elem")
self.assertEqual(ragged.dtype, np.dtype("O"),
"Top most dtype not object!")
self.assertEqual(len(ragged.shape), 1,
"Shape not (n,)!")
for array in store._per_element_arrays:
for a in ragged:
self.assertEqual(a.dtype, store._per_element_arrays[array].dtype,
"Nested array returned from get_array_ragged has wrong dtype!")

def test_has_array(self):
"""hasarray should return correct information for added array; None otherwise."""

Expand Down