-
Notifications
You must be signed in to change notification settings - Fork 13
/
flattenedstorage.py
1095 lines (912 loc) · 41.7 KB
/
flattenedstorage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Efficient storage of ragged arrays in a flattened format.
"""
# coding: utf-8
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
# Distributed under the terms of "New BSD License", see the LICENSE file.
__author__ = ("Marvin Poul", "Niklas Leimeroth")
__copyright__ = (
"Copyright 2021, Max-Planck-Institut für Eisenforschung GmbH - "
"Computational Materials Design (CM) Department"
)
__version__ = "1.0"
__maintainer__ = "Marvin Poul"
__email__ = "poul@mpie.de"
__status__ = "production"
__date__ = "Jul 16, 2020"
import copy
import warnings
from typing import Callable, Iterable, List, Tuple, Any
import numpy as np
import h5py
import pandas as pd
from pyiron_base.interfaces.has_hdf import HasHDF
def _ensure_str_array_size(array, strlen):
"""
Ensures that the given array can store at least string of length `strlen`.
Args:
array (ndarray): array of dtype <U
strlen (int): maximum length that should fit in it
Returns:
ndarray: either `array` or resized copy
"""
current_length = array.itemsize // np.dtype("1U").itemsize
if current_length < strlen:
return array.astype(f"{2 * strlen}U")
else:
return array
class FlattenedStorage(HasHDF):
"""
Efficient storage of ragged arrays in flattened arrays.
This class stores multiple arrays at the same time. Storage is organized in "chunks" that may be of any size, but
all arrays within chunk are of the same size, e.g.
>>> a = [ [1], [2, 3], [4, 5, 6] ]
>>> b = [ [2], [4, 6], [8, 10, 12] ]
are stored as in three chunks like
>>> a_flat = [ 1, 2, 3, 4, 5, 6 ]
>>> b_flat = [ 2, 4, 6, 8, 10, 12 ]
with additional metadata to indicate where the boundaries of each chunk are.
First add arrays and chunks like this
>>> store = FlattenedStorage()
>>> store.add_array("even", dtype=np.int64)
>>> store.add_chunk(1, even=[2])
>>> store.add_chunk(2, even=[4, 6])
>>> store.add_chunk(3, even=[8, 10, 12])
where the first argument indicates the length of each chunk. You may retrieve stored values like this
>>> store.get_array("even", 1)
array([4, 6])
>>> store.get_array("even", 0)
array([2])
where the second arguments are integer indices in the order of insertion. After intial storage you may modify
arrays.
>>> store.set_array("even", 0, [0])
>>> store.get_array("even", 0)
array([0])
As a shorthand you can use regular index syntax
>>> store["even", 0] = [2]
>>> store["even", 0]
array([2])
>>> store["even", 1]
array([4, 6])
>>> store["even"]
array([2, 4, 6, 8, 10, 12])
>>> store["even", 0] = [0]
You can add arrays to the storage even after you added already other arrays and chunks.
>>> store.add_array("odd", dtype=np.int64, fill=0)
>>> store.get_array("odd", 1)
array([0, 0])
>>> store.set_array("odd", 0, [1])
>>> store.set_array("odd", 1, [3, 5])
>>> store.set_array("odd", 2, [7, 9, 11])
>>> store.get_array("odd", 2)
array([ 7, 9, 11])
Because the second chunk is already known to be of length two and `fill` was specified the 'odd' array has been
appropriatly allocated.
Additionally arrays may also only have one value per chunk ("per chunk", previous examples are "per element").
>>> store.add_array("sum", dtype=np.int64, per="chunk")
>>> for i in range(len(store)):
... store.set_array("sum", i, sum(store.get_array("even", i) + store.get_array("odd", i)))
>>> store.get_array("sum", 0)
1
>>> store.get_array("sum", 1)
18
>>> store.get_array("sum", 2)
57
Finally you may add multiple arrays in one call to :meth:`.add_chunk` by using keyword arguments
>>> store.add_chunk(4, even=[14, 16, 18, 20], odd=[13, 15, 17, 19], sum=119)
>>> store.get_array("sum", 3)
119
>>> store.get_array("even", 3)
array([14, 16, 18, 20])
It is usually not necessary to call :meth:`.add_array` before :meth:`.add_chunk`, the type of the array will be
inferred in this case.
If you skip the `frame` argument to :meth:`.get_array` it will return a flat array of all the values for that array
in storage.
>>> store.get_array("sum")
array([ 1, 18, 57, 119])
>>> store.get_array("even")
array([ 0, 4, 6, 8, 10, 12, 14, 16, 18, 20])
Arrays may be of more complicated shape, too, see :meth:`.add_array` for details.
Use :meth:`.copy` to obtain a deep copy of the storage, for shallow copies using the builting `copy.copy` is
sufficient.
>>> copy = store.copy()
>>> copy["even", 0]
array([0])
>>> copy["even", 1]
array([4, 6])
>>> copy["even"]
array([0, 4, 6, 8, 10, 12])
Storages can be :meth:`.split` and :meth:`.join` again as long as their internal chunk structure is consistent,
i.e. same number of chunks and same chunk lengths. If this is not the case a `ValueError` is raised.
>>> even = store.split(["even"])
>>> bool(even.has_array("even"))
True
>>> bool(even.has_array("odd"))
False
>>> odd = store.split(["odd"])
:meth:`.join` adds new arrays to the storage it is called on in-place. To leave it unchanged, simply call copy
before join.
>>> both = even.copy().join(odd)
Chunks may be given string names, either by passing `identifier` to :meth:`.add_chunk` or by setting to the
special per chunk array "identifier"
>>> store.set_array("identifier", 1, "second")
>>> all(store.get_array("even", "second") == store.get_array("even", 1))
True
When adding new arrays follow the convention that per-structure arrays should be named in singular and per-atom
arrays should be named in plural.
You may initialize flattened storage objects with a ragged lists or numpy arrays of dtype object
>>> even = [ list(range(0, 2, 2)), list(range(2, 6, 2)), list(range(6, 12, 2)) ]
>>> even
[[0], [2, 4], [6, 8, 10]]
>>> import numpy as np
>>> odd = np.array([ np.arange(1, 2, 2), np.arange(3, 6, 2), np.arange(7, 12, 2) ], dtype=object)
>>> odd
array([array([1]), array([3, 5]), array([ 7, 9, 11])], dtype=object)
>>> store = FlattenedStorage(even=even, odd=odd)
>>> store.get_array("even", 1)
array([2, 4])
>>> store.get_array("odd", 2)
array([ 7, 9, 11])
>>> len(store)
3
"""
__version__ = "0.2.0"
__hdf_version__ = "0.3.0"
_default_fill_values = {
np.dtype("int8"): -1,
np.dtype("int16"): -1,
np.dtype("int32"): -1,
np.dtype("int64"): -1,
np.dtype("float16"): np.nan,
np.dtype("float32"): np.nan,
np.dtype("float64"): np.nan,
np.dtype("object"): None,
np.dtype("uint8"): 0,
np.dtype("uint16"): 0,
np.dtype("uint32"): 0,
np.dtype("uint64"): 0,
str: "_default",
}
def __init__(self, num_chunks=1, num_elements=1, **kwargs):
"""
Create new flattened storage.
Args:
num_chunks (int): pre-allocation for per chunk arrays
num_elements (int): pre-allocation for per elements arrays
"""
# tracks allocated versed as yet used number of chunks/elements
self._num_chunks_alloc = num_chunks
self._num_elements_alloc = num_elements
self.num_chunks = 0
self.num_elements = 0
# store the starting index for properties with unknown length
self.current_element_index = 0
# store the index for properties of known size, stored at the same index as the chunk
self.current_chunk_index = 0
# Also store indices of chunk recently added
self.prev_chunk_index = 0
self.prev_element_index = 0
self._fill_values = {}
self._init_arrays()
if len(kwargs) == 0:
return
if len(set(len(chunks) for chunks in kwargs.values())) != 1:
raise ValueError("Not all initializers provide the same number of chunks!")
keys = kwargs.keys()
for chunk_list in zip(*kwargs.values()):
chunk_length = len(chunk_list[0])
# values in chunk_list may either be a sequence of chunk_length, scalars (see hasattr check) or a sequence of
# length 1
if any(
hasattr(c, "__len__") and len(c) != chunk_length and len(c) != 1
for c in chunk_list
):
raise ValueError("Inconsistent chunk length in initializer!")
self.add_chunk(chunk_length, **{k: c for k, c in zip(keys, chunk_list)})
def _init_arrays(self):
self._per_element_arrays = {}
self._per_chunk_arrays = {
"start_index": np.full(
self._num_chunks_alloc, dtype=np.int32, fill_value=0
),
"length": np.full(self._num_chunks_alloc, dtype=np.int32, fill_value=0),
"identifier": np.empty(self._num_chunks_alloc, dtype=np.dtype("U20")),
}
def __len__(self):
return self.current_chunk_index
def _internal_arrays(self) -> Tuple[str, ...]:
"""
Names of "internal" arrays, i.e. arrays needed for the correct inner
working of the flattened storage and that not are not added by the
user via :meth:`.add_array`.
Subclasses can override this tuple, by calling `super()` and appending
to it.
This exists mostly to support :meth:`.to_pandas()`.
"""
return (
"start_index",
"length",
)
def copy(self):
"""
Return a deep copy of the storage.
Returns:
:class:`.FlattenedStorage`: copy of self
"""
return copy.deepcopy(self)
def find_chunk(self, identifier):
"""
Return integer index for given identifier.
Args:
identifier (str): name of chunk previously passed to :meth:`.add_chunk`
Returns:
int: integer index for chunk
Raises:
KeyError: if identifier is not found in storage
"""
for i, name in enumerate(self._per_chunk_arrays["identifier"]):
if name == identifier:
return i
raise KeyError(f"No chunk named {identifier}")
def _get_per_element_slice(self, frame):
start = self._per_chunk_arrays["start_index"][frame]
end = start + self._per_chunk_arrays["length"][frame]
return slice(start, end, 1)
def _resize_elements(self, new):
old_max = self._num_elements_alloc
self._num_elements_alloc = new
for k, a in self._per_element_arrays.items():
new_shape = (new,) + a.shape[1:]
try:
a.resize(new_shape)
except ValueError:
self._per_element_arrays[k] = np.resize(a, new_shape)
if old_max < new:
for k in self._per_element_arrays.keys():
if k in self._fill_values.keys():
self._per_element_arrays[k][old_max:] = self._fill_values[k]
def _resize_chunks(self, new):
old_max = self._num_chunks_alloc
self._num_chunks_alloc = new
for k, a in self._per_chunk_arrays.items():
new_shape = (new,) + a.shape[1:]
try:
a.resize(new_shape)
except ValueError:
self._per_chunk_arrays[k] = np.resize(a, new_shape)
if old_max < new:
for k in self._per_chunk_arrays.keys():
if k in self._fill_values.keys():
self._per_chunk_arrays[k][old_max:] = self._fill_values[k]
def add_array(self, name, shape=(), dtype=np.float64, fill=None, per="element"):
"""
Add a custom array to the container.
When adding an array after some chunks have been added, specifying `fill` will be used as a default value
for the value of the array for those chunks.
Adding an array with the same name twice is ignored, if dtype and shape match, otherwise raises an exception.
>>> store = FlattenedStorage()
>>> store.add_chunk(1, "foo")
>>> store.add_array("energy", shape=(), dtype=np.float64, fill=42, per="chunk")
>>> store.get_array("energy", 0)
42.0
Args:
name (str): name of the new array
shape (tuple of int): shape of the new array per element or chunk; scalars can pass ()
dtype (type): data type of the new array, string arrays can pass 'U$n' where $n is the length of the string
fill (object): populate the new array with this value for existing chunk, if given; default `None`
per (str): either "element" or "chunk"; denotes whether the new array should exist for every element in a
chunk or only once for every chunk; case-insensitive
Raises:
ValueError: if wrong value for `per` is given
ValueError: if array with same name but different parameters exists already
"""
if per == "structure":
per = "chunk"
warnings.warn(
'per="structure" is deprecated, use pr="chunk"',
category=DeprecationWarning,
stacklevel=2,
)
if per == "atom":
per = "element"
warnings.warn(
'per="atom" is deprecated, use pr="element"',
category=DeprecationWarning,
stacklevel=2,
)
if name in self._per_element_arrays:
a = self._per_element_arrays[name]
if (
a.shape[1:] != shape
or not np.can_cast(dtype, a.dtype)
or per != "element"
):
raise ValueError(
f"Array with name '{name}' exists with shape {a.shape[1:]} and dtype {a.dtype}."
)
else:
return
if name in self._per_chunk_arrays:
a = self._per_chunk_arrays[name]
if (
a.shape[1:] != shape
or not np.can_cast(dtype, a.dtype)
or per != "chunk"
):
raise ValueError(
f"Array with name '{name}' exists with shape {a.shape[1:]} and dtype {a.dtype}."
)
else:
return
per = per.lower()
if per == "element":
shape = (self._num_elements_alloc,) + shape
store = self._per_element_arrays
elif per == "chunk":
shape = (self._num_chunks_alloc,) + shape
store = self._per_chunk_arrays
else:
raise ValueError(f'per must "element" or "chunk", not {per}')
if fill is None:
store[name] = np.empty(shape=shape, dtype=dtype)
else:
store[name] = np.full(shape=shape, fill_value=fill, dtype=dtype)
if fill is None and store[name].dtype in self._default_fill_values:
fill = self._default_fill_values[store[name].dtype]
if fill is not None:
self._fill_values[name] = fill
def get_array(self, name, frame=None):
"""
Fetch array for given structure.
Works for per atom and per arrays.
Args:
name (str): name of the array to fetch
frame (int, str, optional): selects structure to fetch, as in :meth:`.get_structure()`, if not given
return a flat array of all values for either all chunks or elements
Returns:
:class:`numpy.ndarray`: requested array
Raises:
`KeyError`: if array with name does not exists
"""
if isinstance(frame, str):
frame = self.find_chunk(frame)
if name in self._per_element_arrays:
if frame is not None:
return self._per_element_arrays[name][
self._get_per_element_slice(frame)
]
else:
return self._per_element_arrays[name][: self.num_elements]
elif name in self._per_chunk_arrays:
if frame is not None:
return self._per_chunk_arrays[name][frame]
else:
return self._per_chunk_arrays[name][: self.num_chunks]
else:
raise KeyError(f"no array named {name}")
def get_array_ragged(self, name: str) -> np.ndarray:
"""
Return elements of array `name` in all chunks. Values are returned in a ragged array of dtype=object.
If `name` specifies a per chunk array, there's nothing to pad and this method is equivalent to
:meth:`.get_array`.
Args:
name (str): name of array to fetch
Returns:
numpy.ndarray, dtype=object: ragged arrray of all elements in all chunks
"""
if name in self._per_chunk_arrays:
return self.get_array(name)
# 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:
"""
Return elements of array `name` in all chunks. Arrays are padded to be all of the same length.
The padding value depends on the datatpye of the array or can be configured via the `fill` parameter of
:meth:`.add_array`.
If `name` specifies a per chunk array, there's nothing to pad and this method is equivalent to
:meth:`.get_array`.
Args:
name (str): name of array to fetch
Returns:
numpy.ndarray: padded arrray of all elements in all chunks
"""
if name in self._per_chunk_arrays:
return self.get_array(name)
values = self.get_array_ragged(name)
max_len = self._per_chunk_arrays["length"].max()
def resize_and_pad(v):
l = len(v)
per_shape = self._per_element_arrays[name].shape[1:]
v = np.resize(v, max_len * np.prod(per_shape, dtype=int))
v = v.reshape((max_len,) + per_shape)
if name in self._fill_values:
fill = self._fill_values[name]
else:
fill = np.zeros(1, dtype=self._per_element_arrays[name].dtype)[0]
v[l:] = fill
return v
return np.array([resize_and_pad(v) for v in values])
def set_array(self, name, frame, value):
"""
Add array for given structure.
Works for per chunk and per element arrays.
Args:
name (str): name of array to set
frame (int, str): selects structure to set, as in :meth:`.get_strucure()`
value: value (for per chunk) or array of values (for per element); type and shape as per :meth:`.hasarray()`.
Raises:
`KeyError`: if array with name does not exists
"""
if isinstance(frame, str):
frame = self.find_chunk(frame)
if name in self._per_element_arrays:
if self._per_element_arrays[name].dtype.char == "U":
self._per_element_arrays[name] = _ensure_str_array_size(
self._per_element_arrays[name], max(map(len, value))
)
self._per_element_arrays[name][self._get_per_element_slice(frame)] = value
elif name in self._per_chunk_arrays:
if self._per_chunk_arrays[name].dtype.char == "U":
if isinstance(value, np.ndarray) and value.ndim == 0:
strlen = len(value.item())
else:
strlen = len(value)
self._per_chunk_arrays[name] = _ensure_str_array_size(
self._per_chunk_arrays[name], strlen
)
self._per_chunk_arrays[name][frame] = value
else:
raise KeyError(f"no array named {name}")
def del_array(self, name: str, ignore_missing: bool = False):
"""
Remove an array.
Works with both per chunk and per element arrays.
Args:
name (str): name of the array
ignore_missing (bool): if given do not raise an error if no array
of the given `name` exists
Raises:
KeyError: if no array with given `name` exists and `ignore_missing` is not given
"""
if name in self._per_element_arrays:
del self._per_element_arrays[name]
elif name in self._per_chunk_arrays:
del self._per_chunk_arrays[name]
elif not ignore_missing:
raise KeyError(name)
def __getitem__(self, index):
if isinstance(index, tuple) and len(index) == 2:
return self.get_array(index[0], index[1])
else:
return self.get_array(index)
def __setitem__(self, index, value):
if isinstance(index, tuple) and len(index) == 2:
self.set_array(index[0], index[1], value)
else:
raise IndexError("Must specify chunk index.")
def __delitem__(self, index):
self.del_array(index)
def has_array(self, name):
"""
Checks whether an array of the given name exists and returns meta data given to :meth:`.add_array()`.
>>> container.has_array("energy")
{'shape': (), 'dtype': np.float64, 'per': 'chunk'}
>>> container.has_array("fnorble")
None
Args:
name (str): name of the array to check
Returns:
None: if array does not exist
dict: if array exists, keys corresponds to the shape, dtype and per arguments of :meth:`.add_array`
"""
if name in self._per_element_arrays:
a = self._per_element_arrays[name]
per = "element"
elif name in self._per_chunk_arrays:
a = self._per_chunk_arrays[name]
per = "chunk"
else:
return None
return {"shape": a.shape[1:], "dtype": a.dtype, "per": per}
def list_arrays(self, only_user=False) -> List[str]:
"""
Return a list of names of arrays inside the storage.
Args:
only_user (bool): If `True` include only array names added by the
user via :meth:`.add_array` and the `identifier` array.
Returns:
list of str: array names
"""
arrays = list(self._per_chunk_arrays) + list(self._per_element_arrays)
if only_user:
arrays = [a for a in arrays if a not in self._internal_arrays()]
return arrays
def sample(
self, selector: Callable[["FlattenedStorage", int], bool]
) -> "FlattenedStorage":
"""
Create a new storage with chunks selected by given function.
If called on a subclass this correctly returns an instance of that subclass instead.
Args:
select (callable): function that takes this storage as the first argument and the chunk index to sample as
the second argument; if it returns True it will be part of the new storage.
Returns:
:class:`.FlattenedStorage` or subclass: storage with the selected chunks
"""
new = type(self)()
for k, a in self._per_chunk_arrays.items():
if k not in ("start_index", "length", "identifier"):
new.add_array(k, shape=a.shape[1:], dtype=a.dtype, per="chunk")
for k, a in self._per_element_arrays.items():
new.add_array(k, shape=a.shape[1:], dtype=a.dtype, per="element")
for i in range(len(self)):
if selector(self, i):
new.add_chunk(
self.get_array("length", i),
identifier=self.get_array("identifier", i),
)
for k in self._per_chunk_arrays:
if k not in ("start_index", "length", "identifier"):
new.set_array(k, len(new) - 1, self.get_array(k, i))
for k in self._per_element_arrays:
new.set_array(k, len(new) - 1, self.get_array(k, i))
return new
def split(self, array_names: Iterable[str]) -> "FlattenedStorage":
"""
Return a new storage with only the selected arrays present.
Arrays are deep-copied from `self`.
Args:
array_names (list of str): names of the arrays to present in new storage
Returns:
:class:`.FlattenedStorage`: storage with split arrays
"""
for k in array_names:
if k not in self._per_element_arrays and k not in self._per_chunk_arrays:
raise ValueError(f"Array name {k} not present in FlattenedStorage!")
split = copy.copy(self)
for k in list(split._per_element_arrays):
if k not in array_names:
del split._per_element_arrays[k]
else:
split._per_element_arrays[k] = np.copy(split._per_element_arrays[k])
for k in list(split._per_chunk_arrays):
if k not in array_names and k not in (
"start_index",
"length",
"identifier",
):
del split._per_chunk_arrays[k]
else:
split._per_chunk_arrays[k] = np.copy(split._per_chunk_arrays[k])
return split
def join(
self, store: "FlattenedStorage", lsuffix: str = "", rsuffix: str = ""
) -> "FlattenedStorage":
"""
Merge given storage into this one.
`self` and `store` may not share any arrays. Arrays defined on `stores` are copied and then added to `self`.
Args:
store (:class:`.FlattenedStorage`): storage to join
lsuffix, rsuffix (str, optional): if either are given rename *all* arrays by appending the suffices to the
array name; `lsuffix` for arrays in this storage, `rsuffix` for arrays in
the added storage; in this case arrays are no longer available under the
old name
Returns:
:class:`.FlattenedStorage`: self
Raise:
ValueError: if the two stores do not have the same number of chunks
ValueError: if the two stores do not have equal chunk lengths
ValueError: if lsuffix and rsuffix are equal and different from ""
ValueError: if the stores share array names but `lsuffix` and `rsuffix` are not given
"""
if len(self) != len(store):
raise ValueError(
"FlattenedStorages to be joined have to be of the same length!"
)
if (self["length"] != store["length"]).any():
raise ValueError(
"FlattenedStorages to be joined have to have same length chunks everywhere!"
)
if lsuffix == rsuffix != "":
raise ValueError("lsuffix and rsuffix may not be equal!")
rename = lsuffix != "" or rsuffix != ""
if not rename:
shared_elements = set(self._per_element_arrays).intersection(
store._per_element_arrays
)
shared_chunks = set(self._per_chunk_arrays).intersection(
store._per_chunk_arrays
)
shared_chunks.remove("start_index")
shared_chunks.remove("length")
shared_chunks.remove("identifier")
if len(shared_elements) > 0 or len(shared_chunks) > 0:
raise ValueError(
"FlattenedStorages to be joined may have common arrays only if lsuffix or rsuffix are given!"
)
for k, a in store._per_element_arrays.items():
if k in self._per_element_arrays and rename:
self._per_element_arrays[k + lsuffix] = self._per_element_arrays[k]
if lsuffix != "":
del self._per_element_arrays[k]
k += rsuffix
self._per_element_arrays[k] = a
for k, a in store._per_chunk_arrays.items():
if k not in ("start_index", "length", "identifier"):
if k in self._per_chunk_arrays and rename:
self._per_chunk_arrays[k + lsuffix] = self._per_chunk_arrays[k]
if lsuffix != "":
del self._per_chunk_arrays[k]
k += rsuffix
self._per_chunk_arrays[k] = a
self._resize_elements(self._num_elements_alloc)
self._resize_chunks(self._num_chunks_alloc)
return self
def add_chunk(self, chunk_length, identifier=None, **arrays):
"""
Add a new chunk to the storeage.
Additional keyword arguments given specify arrays to store for the chunk. If an array with the given keyword
name does not exist yet, it will be added to the container.
>>> container = FlattenedStorage()
>>> container.add_chunk(2, identifier="A", energy=3.14)
>>> container.get_array("energy", 0)
3.14
If the first axis of the extra array matches the length of the chunk, it will be added as an per element array,
otherwise as an per chunk array.
>>> container.add_chunk(2, identifier="B", forces=2 * [[0,0,0]])
>>> len(container.get_array("forces", 1)) == 2
True
Reshaping the array to have the first axis be length 1 forces the array to be set as per chunk array. That axis
will then be stripped.
>>> container.add_chunk(2, identifier="C", pressure=np.eye(3)[np.newaxis, :, :])
>>> container.get_array("pressure", 2).shape
(3, 3)
.. attention:: Edge-case!
This will not work when the chunk length is also 1 and the array does not exist yet! In this case the array
will be assumed to be per element and there is no way around explicitly calling :meth:`.add_array()`.
Args:
chunk_length (int): length of the new chunk
identifier (str, optional): human-readable name for the chunk, if None use current chunk index as string
**kwargs: additional arrays to store for the chunk
"""
if identifier is None:
identifier = str(self.num_chunks)
n = chunk_length
new_elements = self.current_element_index + n
if new_elements > self._num_elements_alloc:
self._resize_elements(max(new_elements, self._num_elements_alloc * 2))
if self.current_chunk_index + 1 > self._num_chunks_alloc:
self._resize_chunks(max(1, self._num_chunks_alloc * 2))
if new_elements > self.num_elements:
self.num_elements = new_elements
if self.current_chunk_index + 1 > self.num_chunks:
self.num_chunks += 1
# len of chunk to index into the initialized arrays
i = self.current_element_index + n
self._per_chunk_arrays["start_index"][
self.current_chunk_index
] = self.current_element_index
self._per_chunk_arrays["length"][self.current_chunk_index] = n
self._per_chunk_arrays["identifier"] = _ensure_str_array_size(
self._per_chunk_arrays["identifier"], len(identifier)
)
self._per_chunk_arrays["identifier"][self.current_chunk_index] = identifier
for k, a in arrays.items():
a = np.asarray(a)
if k not in self._per_element_arrays and k not in self._per_chunk_arrays:
if len(a.shape) > 0 and a.shape[0] == n:
self.add_array(k, shape=a.shape[1:], dtype=a.dtype, per="element")
else:
shape = a.shape
# if the first axis was added by the caller to force to add a per chunk array, remove it again here
if len(shape) > 0 and a.shape[0] == 1:
shape = shape[1:]
self.add_array(k, shape=shape, dtype=a.dtype, per="chunk")
# same as above: if the first axis was added by the caller to force to add a per chunk array, remove it
# again here
if k in self._per_chunk_arrays and len(a.shape) > 0 and a.shape[0] == 1:
a = a[0]
self.set_array(k, self.current_chunk_index, a)
self.prev_chunk_index = self.current_chunk_index
self.prev_element_index = self.current_element_index
# Set new current_element_index and increase current_chunk_index
self.current_chunk_index += 1
self.current_element_index = i
# return last_chunk_index, last_element_index
def extend(self, other: "FlattenedStorage"):
"""
Add chunks from `other` to this storage.
Afterwards the number of chunks and elements are the sum of the respective previous values.
If `other` defines new arrays or doesn't define some of the arrays they are padded by the fill values.
Args:
other (:class:`.FlattenedStorage`): other storage to add
Raises:
ValueError: if fill values between both storages are not compatible
Returns:
FlattenedStorage: return this storage
"""
self._check_compatible_fill_values(other=other)
combined_num_chunks = self.num_chunks + other.num_chunks
combined_num_elements = self.num_elements + other.num_elements
if combined_num_chunks > self._num_chunks_alloc:
self._resize_chunks(combined_num_chunks)
if combined_num_elements > self._num_elements_alloc:
self._resize_elements(combined_num_elements)
for k, a in other._per_chunk_arrays.items():
# add start_index of last chunk to start_index of other for correct mapping
if (
k == "start_index" and len(self) > 0
): # Check if len > 0 to ensure that no random values are accessed for length and start_index after empty init
last = self.num_chunks - 1
len_last = self._per_chunk_arrays["length"][last]
a = (
a + self._per_chunk_arrays[k][last] + len_last
) # no += to prevent inplace mutation
if k not in self._per_chunk_arrays.keys():
dtype, fill = get_dtype_and_fill(storage=other, name=k)
self.add_array(
name=k, dtype=dtype, shape=a.shape[1:], fill=fill, per="chunk"
)
self._per_chunk_arrays[k][self.num_chunks : combined_num_chunks] = a[
0 : other.num_chunks
]
for k, a in other._per_element_arrays.items():
if k not in self._per_element_arrays.keys():
dtype, fill = get_dtype_and_fill(storage=other, name=k)
self.add_array(
name=k, shape=a.shape[1:], dtype=dtype, fill=fill, per="element"
)
self._per_element_arrays[k][self.num_elements : combined_num_elements] = a[
0 : other.num_elements
]
self.num_elements = combined_num_elements
self.num_chunks = combined_num_chunks
self.current_chunk_index = self.num_chunks
self.current_element_index = self.num_elements
return self
def _check_compatible_fill_values(self, other: "FlattenedStorage"):
"""
Check if fill values of 2 FlattenedStorages match to prevent errors due to wrong fill values,
f.e. after extending to the storage.
Args:
other (FlattenedStorage): Another FlattenedStorage instance
Raises:
ValueError: Raises when the storages have different fill values for a key
"""
for k in set(self._fill_values).intersection(other._fill_values):
if np.isnan(self._fill_values[k]) and np.isnan(other._fill_values[k]):
continue
else:
if self._fill_values[k] != other._fill_values[k]:
raise ValueError(
"Fill values for arrays in storages don't match, can't perform requested operation"
)
def _get_hdf_group_name(self):
return "flat_storage"
def _to_hdf(self, hdf):
def write_array(name, array, hdf):
if array.dtype.char == "U":
# numpy stores unicode data in UTF-32/UCS-4, but h5py wants UTF-8, so we manually encode them here
# TODO: string arrays with shape != () not handled
hdf[name] = np.array(
[s.encode("utf8") for s in array],
# each character in a utf8 string might be encoded in up to 4 bytes, so to
# make sure we can store any string of length n we tell h5py that the
# string will be 4 * n bytes; numpy's dtype does this calculation already
# in itemsize, so we don't need to repeat it here
# see also https://docs.h5py.org/en/stable/strings.html
dtype=h5py.string_dtype("utf8", array.dtype.itemsize),
)
else:
hdf[name] = array
# truncate arrays to necessary size before writing
self._resize_elements(self.num_elements)
self._resize_chunks(self.num_chunks)
hdf["num_elements"] = self._num_elements_alloc
hdf["num_chunks"] = self._num_chunks_alloc
hdf_arrays = hdf.open("element_arrays")
for k, a in self._per_element_arrays.items():
write_array(k, a, hdf_arrays)
hdf_arrays = hdf.open("chunk_arrays")
for k, a in self._per_chunk_arrays.items():
write_array(k, a, hdf_arrays)
hdf["_fill_values"] = self._fill_values
def _from_hdf(self, hdf, version=None):
def read_array(name, hdf):
a = np.asarray(hdf[name])
if a.dtype.char == "S":
# if saved as bytes, we wrote this as an encoded unicode string, so manually decode here
# TODO: string arrays with shape != () not handled
a = np.fromiter(
(s.decode("utf8") for s in a),
# itemsize of original a is four bytes per character, so divide by four to get
# length of the orignal stored unicode string; np.dtype('U1').itemsize is just a