np-struct 0.1.7__tar.gz → 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {np_struct-0.1.7 → np_struct-0.2.1}/PKG-INFO +1 -1
- {np_struct-0.1.7 → np_struct-0.2.1}/np_struct/ldarray.py +512 -71
- np_struct-0.2.1/np_struct/utils.py +74 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/np_struct.egg-info/PKG-INFO +1 -1
- {np_struct-0.1.7 → np_struct-0.2.1}/np_struct.egg-info/SOURCES.txt +1 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/pyproject.toml +3 -3
- {np_struct-0.1.7 → np_struct-0.2.1}/tests/test_ldarray.py +100 -2
- {np_struct-0.1.7 → np_struct-0.2.1}/README.md +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/np_struct/__init__.py +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/np_struct/bitfields.py +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/np_struct/structures.py +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/np_struct/transfer.py +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/np_struct.egg-info/dependency_links.txt +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/np_struct.egg-info/requires.txt +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/np_struct.egg-info/top_level.txt +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/setup.cfg +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/setup.py +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/tests/test_ldarray_math.py +0 -0
- {np_struct-0.1.7 → np_struct-0.2.1}/tests/test_transfers.py +0 -0
|
@@ -1,24 +1,17 @@
|
|
|
1
1
|
import numpy as np
|
|
2
2
|
import datetime as dt
|
|
3
|
-
from scipy import
|
|
4
|
-
from scipy
|
|
3
|
+
from scipy import ndimage
|
|
4
|
+
from scipy import interpolate
|
|
5
5
|
from collections import OrderedDict
|
|
6
6
|
from copy import deepcopy as dcopy
|
|
7
7
|
import datetime
|
|
8
|
-
from
|
|
9
|
-
from
|
|
8
|
+
from typing import TYPE_CHECKING, Callable
|
|
9
|
+
from itertools import product
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
"""
|
|
13
|
-
Check that the shape tuples a and b match
|
|
14
|
-
"""
|
|
15
|
-
|
|
16
|
-
if len(a) != len(b):
|
|
17
|
-
return False
|
|
18
|
-
|
|
19
|
-
# check that the length of each dimension matches
|
|
20
|
-
return all([a[i] == b[i] for i in range(len(a))])
|
|
11
|
+
from np_struct import utils
|
|
21
12
|
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from matplotlib import axes
|
|
22
15
|
|
|
23
16
|
def datetime_idx_handler(v: datetime.datetime, coords: np.ndarray):
|
|
24
17
|
"""
|
|
@@ -350,12 +343,19 @@ class ldarray(np.ndarray):
|
|
|
350
343
|
|
|
351
344
|
def __new__(cls, data=None, coords=None, attrs= dict(), dtype=None):
|
|
352
345
|
|
|
346
|
+
# if coords is not provided, attempt to use coords from data
|
|
347
|
+
if coords is None and isinstance(data, ldarray):
|
|
348
|
+
coords = data.coords
|
|
349
|
+
|
|
353
350
|
# cast coords as a OrderedDictionary type
|
|
354
|
-
if not isinstance(coords, Coords):
|
|
351
|
+
if coords is not None and not isinstance(coords, Coords):
|
|
355
352
|
coords = Coords(**coords)
|
|
356
353
|
|
|
357
354
|
# create 0 filled array if no data is given in the constructor
|
|
358
355
|
if data is None:
|
|
356
|
+
if coords is None:
|
|
357
|
+
raise ValueError("Coords must be provided.")
|
|
358
|
+
|
|
359
359
|
obj = np.zeros(coords.shape, dtype=dtype).view(cls)
|
|
360
360
|
|
|
361
361
|
# cast input data to ldarray type
|
|
@@ -368,8 +368,8 @@ class ldarray(np.ndarray):
|
|
|
368
368
|
obj = obj.view(cls)
|
|
369
369
|
|
|
370
370
|
# If dim is not compatible with the data shape return a standard numpy array
|
|
371
|
-
if (coords is None)
|
|
372
|
-
raise
|
|
371
|
+
if (coords is not None) and (not utils.check_shapes(obj.shape, coords.shape)):
|
|
372
|
+
raise ValueError(
|
|
373
373
|
"Coordinates of shape {} are not compatible with data of shape {}.".format(coords.shape, obj.shape)
|
|
374
374
|
)
|
|
375
375
|
|
|
@@ -419,8 +419,8 @@ class ldarray(np.ndarray):
|
|
|
419
419
|
obj = super().__array_function__(func, types, args, kwargs)
|
|
420
420
|
|
|
421
421
|
# recast as ldarray if return type is ndarray
|
|
422
|
-
if isinstance(obj, np.ndarray) and not isinstance(obj, ldarray) and check_shapes(obj.shape, self.shape):
|
|
423
|
-
obj = ldarray(obj, coords=self.coords)
|
|
422
|
+
if isinstance(obj, np.ndarray) and not isinstance(obj, ldarray) and utils.check_shapes(obj.shape, self.shape):
|
|
423
|
+
obj = ldarray(obj, coords=self.coords, attrs=self.attrs)
|
|
424
424
|
|
|
425
425
|
# remove axis from coords if it was indexed out by np.sum, np.average, etc...
|
|
426
426
|
elif isinstance(obj, np.ndarray) and axis is not None and len(obj.shape) == (len(self.shape) - len(axis)):
|
|
@@ -429,7 +429,7 @@ class ldarray(np.ndarray):
|
|
|
429
429
|
keys = tuple(self.coords.keys())
|
|
430
430
|
[coords.pop(keys[i]) for i in axis]
|
|
431
431
|
# recast as labeled array
|
|
432
|
-
obj = ldarray(obj, coords=coords)
|
|
432
|
+
obj = ldarray(obj, coords=coords, attrs=self.attrs)
|
|
433
433
|
|
|
434
434
|
# invalidate coords for functions capable of leaving the shape intact but permuting the axis
|
|
435
435
|
# order. transpose is subclassed separately and not included here.
|
|
@@ -448,8 +448,9 @@ class ldarray(np.ndarray):
|
|
|
448
448
|
# array finalize is called when array is cast to a new type, indexed, or whenever a new array with a different
|
|
449
449
|
# shape is created (i.e. transpose). By default, drop the coordinates which are most likely out of date now.
|
|
450
450
|
# Coordinates will be added back by lower level functions if the shape stayed the same.
|
|
451
|
-
if isinstance(obj, ldarray) and getattr(obj, "coords", None) and check_shapes(self.shape, obj.coords.shape):
|
|
451
|
+
if isinstance(obj, ldarray) and getattr(obj, "coords", None) and utils.check_shapes(self.shape, obj.coords.shape):
|
|
452
452
|
self.coords = dcopy(obj.coords)
|
|
453
|
+
self.attrs = dcopy(obj.attrs)
|
|
453
454
|
else:
|
|
454
455
|
self.coords = None
|
|
455
456
|
|
|
@@ -533,13 +534,14 @@ class ldarray(np.ndarray):
|
|
|
533
534
|
|
|
534
535
|
# if the shapes of the inputs were expanded, restore the full expanded coordinates if the shape
|
|
535
536
|
# is still consistent.
|
|
536
|
-
elif len(result_coords) and check_shapes(results.shape, Coords(**result_coords).shape):
|
|
537
|
-
results = ldarray(results, coords=result_coords)
|
|
537
|
+
elif len(result_coords) and utils.check_shapes(results.shape, Coords(**result_coords).shape):
|
|
538
|
+
results = ldarray(results, coords=result_coords, attrs=self.attrs)
|
|
538
539
|
|
|
539
540
|
# if the shape is the same after the math operation, restore the coordinates
|
|
540
|
-
elif self.coords and check_shapes(results.shape, self.coords.shape):
|
|
541
|
+
elif self.coords and utils.check_shapes(results.shape, self.coords.shape):
|
|
541
542
|
results = results.view(ldarray)
|
|
542
543
|
results.coords = dcopy(self.coords)
|
|
544
|
+
results.attrs = dcopy(self.attrs)
|
|
543
545
|
|
|
544
546
|
else:
|
|
545
547
|
results = results.view(np.ndarray)
|
|
@@ -606,7 +608,16 @@ class ldarray(np.ndarray):
|
|
|
606
608
|
# index is a standard index of slices or integers so pass key to the numpy indexing routine.
|
|
607
609
|
# this object will have the coords set to None by __array_finalize___
|
|
608
610
|
obj = super(ldarray, self).__getitem__(key)
|
|
611
|
+
|
|
612
|
+
# Cast index key as a tuple if it's a single value
|
|
613
|
+
nkey = tuple(key) if isinstance(key, (tuple, list)) else (key,)
|
|
609
614
|
|
|
615
|
+
# use coords from advanced index
|
|
616
|
+
labeled_idx = [k for k in nkey if isinstance(k, ldarray) and utils.check_shapes(k.shape, obj.shape)]
|
|
617
|
+
if len(labeled_idx):
|
|
618
|
+
obj.coords = dcopy(labeled_idx[0].coords)
|
|
619
|
+
return obj
|
|
620
|
+
|
|
610
621
|
# shape length can be greater after indexing if np.newaxis was used. In this case just
|
|
611
622
|
# return a standard numpy array and make the user responsible for adding dimensional labels.
|
|
612
623
|
if len(obj.shape) > len(self.shape):
|
|
@@ -623,13 +634,11 @@ class ldarray(np.ndarray):
|
|
|
623
634
|
# At this point, we need to index the dimension dictionary so it matches the obj data,
|
|
624
635
|
# and remove axis that were indexed out completely.
|
|
625
636
|
try:
|
|
626
|
-
# Cast index key as a tuple if it's a single value
|
|
627
|
-
nkey = tuple(key) if isinstance(key, (tuple, list)) else (key,)
|
|
628
637
|
|
|
629
638
|
# Initialize list of indices for each dimension that will be used to index the label arrays in dim.
|
|
630
639
|
# Length is the original array shape length so it matches ndim.
|
|
631
640
|
idx = [slice(None,None) for i in range(len(self.shape))]
|
|
632
|
-
|
|
641
|
+
|
|
633
642
|
# step through index keys and update idx with the appropriate keys.
|
|
634
643
|
# Keys are always in order of the array dimensions, but axis can be skipped with the Ellipsis operator.
|
|
635
644
|
idx_i = 0
|
|
@@ -656,14 +665,16 @@ class ldarray(np.ndarray):
|
|
|
656
665
|
# idx has a value for every dimension so we can use i to get the correct index key
|
|
657
666
|
ncoords[k] = np.array(v)[idx[i]].squeeze()
|
|
658
667
|
|
|
659
|
-
# revert to standard numpy array if we weren't able to keep coords consistent with the numpy array data
|
|
660
|
-
|
|
668
|
+
# revert to standard numpy array if we weren't able to keep coords consistent with the numpy array data.
|
|
669
|
+
# This commonly happens with advanced indexing (or pair-wise indexing)
|
|
670
|
+
if not utils.check_shapes(obj.shape, ncoords.shape):
|
|
661
671
|
return obj.view(np.ndarray)
|
|
662
672
|
|
|
663
673
|
# if dim and the obj shape match, update the dim member of the indexed obj and return
|
|
664
674
|
obj.coords = ncoords
|
|
675
|
+
obj.attrs = dcopy(self.attrs)
|
|
665
676
|
return obj
|
|
666
|
-
|
|
677
|
+
|
|
667
678
|
# if the coords were unable to be indexed, clear the coords and return a unlabeled numpy array.
|
|
668
679
|
except Exception:
|
|
669
680
|
obj.coords = None
|
|
@@ -796,9 +807,17 @@ class ldarray(np.ndarray):
|
|
|
796
807
|
|
|
797
808
|
# convert coordinate to standard index
|
|
798
809
|
if isinstance(v, (list, tuple, np.ndarray)):
|
|
810
|
+
v = np.atleast_1d(v)
|
|
799
811
|
# get standard indices for each value in list
|
|
800
|
-
np_index[np_i] = [handler(vv, coords_k, **handler_kwargs) for vv in v]
|
|
801
|
-
|
|
812
|
+
np_index[np_i] = np.reshape([handler(vv, coords_k, **handler_kwargs) for vv in v.flatten()], v.shape)
|
|
813
|
+
# recast as labeled array
|
|
814
|
+
if isinstance(v, ldarray):
|
|
815
|
+
np_index[np_i] = ldarray(np_index[np_i], v.coords)
|
|
816
|
+
# cast single valued arrays as slices, this preserves the dimension
|
|
817
|
+
if np_index[np_i].size == 1:
|
|
818
|
+
idx_v = np_index[np_i].item()
|
|
819
|
+
np_index[np_i] = slice(idx_v, idx_v+1)
|
|
820
|
+
|
|
802
821
|
elif isinstance(v, slice):
|
|
803
822
|
# call handler for each start, stop and step value
|
|
804
823
|
s_start, s_stop = [handler(vv, coords_k, **handler_kwargs) if vv is not None else None for vv in [v.start, v.stop]]
|
|
@@ -812,27 +831,67 @@ class ldarray(np.ndarray):
|
|
|
812
831
|
|
|
813
832
|
# if more than one index is a list or array, numpy does pair-wise indexing. Otherwise, we can return the
|
|
814
833
|
# indices as is.
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
# create pairwise indices.
|
|
819
|
-
for i, idx in enumerate(np_index):
|
|
820
|
-
# convert slice indices to a range of indices
|
|
821
|
-
if isinstance(np_index[i], slice):
|
|
834
|
+
# shape of each index
|
|
835
|
+
is_idx_2d = [len(idx.shape) > 1 if isinstance(idx, (np.ndarray)) else False for idx in np_index]
|
|
836
|
+
is_idx_vector = [len(idx) > 1 if isinstance(idx, (list, tuple, np.ndarray)) else False for idx in np_index]
|
|
822
837
|
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
838
|
+
if np.any(is_idx_2d):
|
|
839
|
+
# create advanced pairwise indices (or advanced indices). Every index is an array of the same shape,
|
|
840
|
+
# or will be broadcast together so they are the same shape. The indexing arrays must be labeled
|
|
841
|
+
# and have all the dimensions present, minus the dimension it is selecting.
|
|
826
842
|
|
|
827
|
-
|
|
843
|
+
# get the first 2D matrix index
|
|
844
|
+
idx_2d = np_index[is_idx_2d.index(1)]
|
|
845
|
+
result_shape = idx_2d.shape
|
|
828
846
|
|
|
829
|
-
|
|
830
|
-
|
|
847
|
+
# check that it is labeled
|
|
848
|
+
if not isinstance(idx_2d, ldarray):
|
|
849
|
+
raise ValueError("Matrix indices must be labeled numpy arrays with all dimensions present.")
|
|
850
|
+
|
|
851
|
+
for i, idx in enumerate(np_index):
|
|
852
|
+
|
|
853
|
+
# create matrix indices for dimensions left with full slices (:)
|
|
854
|
+
if isinstance(np_index[i], slice):
|
|
855
|
+
|
|
856
|
+
key = dim_keys[i]
|
|
857
|
+
|
|
858
|
+
start = 0 if idx.step is None else idx.start
|
|
859
|
+
stop = self.shape[i] if idx.stop is None else idx.stop + 1
|
|
860
|
+
step = 1 if idx.step is None else idx.step
|
|
861
|
+
|
|
862
|
+
idx_b = [None] * len(result_shape)
|
|
863
|
+
# place of index in the result array
|
|
864
|
+
result_place = list(idx_2d.coords.keys()).index(key)
|
|
865
|
+
idx_b[result_place] = slice(None)
|
|
866
|
+
# add extra dimensions
|
|
867
|
+
np_index[i] = np.array(np.arange(start, stop, step))[tuple(idx_b)]
|
|
868
|
+
|
|
869
|
+
# return a meshgrid of index values, the resulting array when this index is used will have the same
|
|
870
|
+
# shape as each array in the axis positions. np.ix_ doesn't perform a full meshgrid broadcast, but ensures
|
|
871
|
+
# the shapes are compatible.
|
|
872
|
+
return tuple([np.broadcast_to(m, result_shape) for m in np_index])
|
|
873
|
+
|
|
874
|
+
# if more than one index is a vector, advanced indexing is used. Broadcast the indices together.
|
|
875
|
+
elif np.count_nonzero(is_idx_vector) > 1:
|
|
876
|
+
|
|
877
|
+
for i, idx in enumerate(np_index):
|
|
878
|
+
# convert slice indices to a range of indices
|
|
879
|
+
if isinstance(np_index[i], slice):
|
|
880
|
+
|
|
881
|
+
start = 0 if idx.step is None else idx.start
|
|
882
|
+
stop = self.shape[i] if idx.stop is None else idx.stop + 1
|
|
883
|
+
step = 1 if idx.step is None else idx.step
|
|
884
|
+
|
|
885
|
+
np_index[i] = np.arange(start, stop, step)
|
|
886
|
+
|
|
887
|
+
else:
|
|
888
|
+
np_index[i] = np.atleast_1d(idx)
|
|
889
|
+
|
|
890
|
+
return np.ix_(*np_index)
|
|
891
|
+
|
|
892
|
+
else:
|
|
893
|
+
return tuple(np_index)
|
|
831
894
|
|
|
832
|
-
# return a meshgrid of index values, the resulting array when this index is used will have the same
|
|
833
|
-
# shape as each array in the axis positions. np.ix_ doesn't perform a full meshgrid broadcast, but ensures
|
|
834
|
-
# the shapes are compatible.
|
|
835
|
-
return np.ix_(*np_index)
|
|
836
895
|
|
|
837
896
|
def save(self, filepath: str):
|
|
838
897
|
"""
|
|
@@ -886,6 +945,7 @@ class ldarray(np.ndarray):
|
|
|
886
945
|
prefilter: bool = True,
|
|
887
946
|
dtype: np.dtype = None,
|
|
888
947
|
precision: int = 6,
|
|
948
|
+
flat: bool = False,
|
|
889
949
|
**coords,
|
|
890
950
|
):
|
|
891
951
|
"""
|
|
@@ -909,6 +969,9 @@ class ldarray(np.ndarray):
|
|
|
909
969
|
unexpected results if interpolating an integer array.
|
|
910
970
|
precision : int, optional
|
|
911
971
|
decimal precision of interpolation, default is 6 decimal places.
|
|
972
|
+
flat : bool, default: False
|
|
973
|
+
flattens all coords into a pairwise interpolation if True. Default is False, which creates
|
|
974
|
+
a grid interpolation across all coords.
|
|
912
975
|
**coords
|
|
913
976
|
coordinate values to interpolate at. Each value is typically a 1D vector of coordinate values, but
|
|
914
977
|
multi-dimensional arrays are also supported if they are provided as an ldarray. The interpolated
|
|
@@ -964,11 +1027,18 @@ class ldarray(np.ndarray):
|
|
|
964
1027
|
data = np.nan_to_num(self)
|
|
965
1028
|
|
|
966
1029
|
coords = {k: np.atleast_1d(v) for k, v in coords.items()}
|
|
1030
|
+
v0 = list(coords.values())[0]
|
|
967
1031
|
|
|
1032
|
+
if flat:
|
|
1033
|
+
if not np.all([len(v) == len(v0) for v in coords.values()]):
|
|
1034
|
+
raise ValueError("All pair-wise interpolation coords must be equal length.")
|
|
1035
|
+
|
|
968
1036
|
# coordinate keys that are specified as meshgrids
|
|
969
1037
|
mg_keys = [k for k in self.coords.keys() if k in coords.keys() and len(coords[k].shape) > 1]
|
|
970
1038
|
# dimension indices for all coordinates that are single vectors and not meshgrids
|
|
971
1039
|
vector_idx = [i for i, k in enumerate(self.coords.keys()) if k not in mg_keys]
|
|
1040
|
+
# dimensions that do not have interp coords
|
|
1041
|
+
missing_idx = [i for i, k in enumerate(self.coords.keys()) if k not in coords.keys()]
|
|
972
1042
|
|
|
973
1043
|
# check that all meshgrid indices have the same shape
|
|
974
1044
|
if len(mg_keys):
|
|
@@ -977,17 +1047,8 @@ class ldarray(np.ndarray):
|
|
|
977
1047
|
raise ValueError("All meshgrid indices must be the same shape.")
|
|
978
1048
|
|
|
979
1049
|
# all meshgrids must be labeled with the same coordinates
|
|
980
|
-
if not
|
|
981
|
-
raise ValueError("
|
|
982
|
-
|
|
983
|
-
# interpolated shape is the length of each data coordinates that are given as vectors (or not included),
|
|
984
|
-
# followed by the meshgrid shape.
|
|
985
|
-
dim_keys = list(self.coords.keys())
|
|
986
|
-
interp_shape = tuple(
|
|
987
|
-
[self.shape[i] if dim_keys[i] not in coords.keys() else len(coords[dim_keys[i]]) for i in vector_idx]
|
|
988
|
-
)
|
|
989
|
-
if len(mg_keys):
|
|
990
|
-
interp_shape += m0.shape
|
|
1050
|
+
if not isinstance(m0, ldarray):
|
|
1051
|
+
raise ValueError("Meshgrid indices must labeled arrays with identical coordinates.")
|
|
991
1052
|
|
|
992
1053
|
# Start with list of slices that index the full range of each dimension.
|
|
993
1054
|
# dimensions that are not included in coords will be left as a full vector of all indices in
|
|
@@ -1019,20 +1080,37 @@ class ldarray(np.ndarray):
|
|
|
1019
1080
|
|
|
1020
1081
|
# get the floating point "index" by interpolation for each coordinate value.
|
|
1021
1082
|
else:
|
|
1022
|
-
coord_interp = interp1d(
|
|
1083
|
+
coord_interp = interpolate.interp1d(
|
|
1084
|
+
coords_k, np.arange(0, self.shape[np_i]), assume_sorted=False, kind="linear"
|
|
1085
|
+
)
|
|
1023
1086
|
interp_index[np_i] = coord_interp(np.around(v, decimals=precision))
|
|
1024
1087
|
|
|
1025
1088
|
# map_coordinates work similarly as numpy advanced indexing, where the index for each dimension can
|
|
1026
1089
|
# be an matrix. The matrices must all be the same shape, so broadcast the matrices/vectors in interp_index
|
|
1027
1090
|
# across each other. The number of interpolated dimensions does not need to be the same as the array dimensions.
|
|
1028
1091
|
interp_index_b = [None] * self.ndim
|
|
1029
|
-
v_i = 0
|
|
1030
1092
|
|
|
1093
|
+
if flat:
|
|
1094
|
+
# interpolated shape is the shape of the dimensions that are not in the interp coordinates,
|
|
1095
|
+
# plus the flat interpolated vector.
|
|
1096
|
+
interp_shape = [len(v) for k, v in self.coords.items() if k not in coords.keys()] + [len(v0)]
|
|
1097
|
+
|
|
1098
|
+
|
|
1099
|
+
else:
|
|
1100
|
+
# interpolated shape is the length of each data coordinates that are given as vectors (or not included),
|
|
1101
|
+
# followed by the meshgrid shape.
|
|
1102
|
+
|
|
1103
|
+
interp_shape = tuple(
|
|
1104
|
+
[self.shape[i] if dim_keys[i] not in coords.keys() else len(coords[dim_keys[i]]) for i in vector_idx]
|
|
1105
|
+
)
|
|
1106
|
+
if len(mg_keys):
|
|
1107
|
+
interp_shape += m0.shape
|
|
1108
|
+
|
|
1109
|
+
v_i = 0
|
|
1031
1110
|
for i in range(self.ndim):
|
|
1032
1111
|
|
|
1033
|
-
# for vector indices, add dimensions for all the other
|
|
1034
|
-
|
|
1035
|
-
if i in vector_idx:
|
|
1112
|
+
# for vector indices, add dimensions for all the other dimensions
|
|
1113
|
+
if (flat and i in missing_idx) or (not flat and i in vector_idx):
|
|
1036
1114
|
# select current dimension in the interpolated shape by adding a ":" in the dimension list.
|
|
1037
1115
|
# the vector indices are stacked at the front of the interpolated shape, regardless of where
|
|
1038
1116
|
# they appear in the array dimensions (use v_i instead of i to select dimension)
|
|
@@ -1041,9 +1119,12 @@ class ldarray(np.ndarray):
|
|
|
1041
1119
|
# add extra dimensions
|
|
1042
1120
|
interp_index_b[i] = np.array(interp_index[i])[tuple(idx_b)]
|
|
1043
1121
|
v_i += 1
|
|
1044
|
-
|
|
1122
|
+
|
|
1123
|
+
# for meshgrid indices or flattened dimensions, add extra dimensions for the vector dimensions
|
|
1124
|
+
# at the beginning of the array.
|
|
1045
1125
|
else:
|
|
1046
|
-
interp_index_b[i] = interp_index[i][tuple([None] * v_i)]
|
|
1126
|
+
interp_index_b[i] = interp_index[i][tuple([None] * v_i)] if v_i else interp_index[i]
|
|
1127
|
+
|
|
1047
1128
|
|
|
1048
1129
|
# map_coordinates doesn't broadcast the indices like numpy does for advanced indexing. Broadcast
|
|
1049
1130
|
# index array to the same shape for each dimension.
|
|
@@ -1052,24 +1133,130 @@ class ldarray(np.ndarray):
|
|
|
1052
1133
|
if dtype is None:
|
|
1053
1134
|
dtype = self.dtype
|
|
1054
1135
|
|
|
1055
|
-
|
|
1136
|
+
data_interp = ndimage.map_coordinates(
|
|
1056
1137
|
data.astype(dtype), map_idx, output=output, order=order, mode=mode, cval=cval, prefilter=prefilter
|
|
1057
1138
|
)
|
|
1058
1139
|
|
|
1059
1140
|
data_coords = {}
|
|
1141
|
+
attrs = dict()
|
|
1060
1142
|
# add coordinates from vector indices
|
|
1061
1143
|
for i, k in enumerate(self.coords.keys()):
|
|
1062
|
-
if i in vector_idx:
|
|
1144
|
+
if (flat and i in missing_idx) or (not flat and i in vector_idx):
|
|
1063
1145
|
data_coords[k] = coords[k] if k in coords.keys() else self.coords[k]
|
|
1064
1146
|
|
|
1065
1147
|
# add the coordinates from the meshgrid
|
|
1066
1148
|
if len(mg_keys):
|
|
1067
1149
|
data_coords.update(m0.coords)
|
|
1068
1150
|
|
|
1151
|
+
# add the coordinates for the flattened dimensions
|
|
1152
|
+
if flat:
|
|
1153
|
+
flat_key = "".join(coords.keys())
|
|
1154
|
+
data_coords[flat_key] = np.arange(len(v0))
|
|
1155
|
+
attrs = {k: v for k, v in coords.items()}
|
|
1156
|
+
|
|
1069
1157
|
return ldarray(
|
|
1070
|
-
|
|
1158
|
+
data_interp, coords=data_coords, attrs=attrs
|
|
1159
|
+
)
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
def interpolate_from_flat(self, flat: bool = False, **coords):
|
|
1163
|
+
"""
|
|
1164
|
+
Interpolate pairwise, flattened dimensions. Two (and only two) interpolation dimensions are supported.
|
|
1165
|
+
The pairwise coordinates must be present in the attributes.
|
|
1166
|
+
|
|
1167
|
+
Parameters
|
|
1168
|
+
----------
|
|
1169
|
+
**coords
|
|
1170
|
+
coordinate values to interpolate at. If dimension is "uv", interpolated coords must be
|
|
1171
|
+
"u" and "v".
|
|
1172
|
+
|
|
1173
|
+
flat: bool, default: False
|
|
1174
|
+
if False (default), the data is returned as a meshgrid of the two interpolation coordinates.
|
|
1175
|
+
If True, the data is returned as pairwise points of the interpolation coordinate.
|
|
1176
|
+
|
|
1177
|
+
Examples
|
|
1178
|
+
--------
|
|
1179
|
+
"""
|
|
1180
|
+
|
|
1181
|
+
interp_keys = list(coords.keys())
|
|
1182
|
+
interp_v1, interp_v2 = [np.atleast_1d(v) for v in coords.values()]
|
|
1183
|
+
|
|
1184
|
+
if len(interp_keys) != 2:
|
|
1185
|
+
raise ValueError("Requires a pair of interpolation coordinates.")
|
|
1186
|
+
|
|
1187
|
+
# ensure shape of coords matches if flat
|
|
1188
|
+
if (len(interp_v1.shape) > 1) and (interp_v1.shape != interp_v2.shape):
|
|
1189
|
+
raise ValueError("Interpolation coordinates must have equal shapes.")
|
|
1190
|
+
|
|
1191
|
+
# coords must be labeled if more than 1D
|
|
1192
|
+
if len(interp_v1.shape) > 1 and not isinstance(interp_v1, ldarray):
|
|
1193
|
+
raise ValueError("Interpolation coordinates must be labeled numpy arrays if greater than 1D.")
|
|
1194
|
+
|
|
1195
|
+
if flat:
|
|
1196
|
+
if not len(interp_v1) == len(interp_v1):
|
|
1197
|
+
raise ValueError("All pair-wise interpolation coords must be equal length.")
|
|
1198
|
+
|
|
1199
|
+
# get data coordinates for both interpolated dimensions
|
|
1200
|
+
if all([k in self.coords.keys() for k in interp_keys]):
|
|
1201
|
+
data = self.transpose((*interp_keys, ...))
|
|
1202
|
+
data_coords_m = np.meshgrid(*[self.coords[k] for k in interp_keys], indexing="ij")
|
|
1203
|
+
# flatten and stack mesh so coords are Nx2
|
|
1204
|
+
data_coords = np.stack(data_coords_m, axis=-1).reshape((-1, 2))
|
|
1205
|
+
# flatten interpolated coords in data
|
|
1206
|
+
data = np.reshape(data, (len(data_coords), *data.shape[2:]))
|
|
1207
|
+
|
|
1208
|
+
# if data coordinates are flattened into one dimensions, use the attributes to create Nx2 positions
|
|
1209
|
+
elif "".join(interp_keys) in self.coords.keys():
|
|
1210
|
+
data = self.transpose(("".join(interp_keys), ...))
|
|
1211
|
+
data_coords = np.stack([self.attrs[k] for k in interp_keys], axis=-1)
|
|
1212
|
+
|
|
1213
|
+
else:
|
|
1214
|
+
raise ValueError(f"Unable to interpolate coordinates {list(self.coords.keys())}")
|
|
1215
|
+
|
|
1216
|
+
# create interpolator, this does handle complex data but performs better if interpolation is done
|
|
1217
|
+
# on magnitude and angle separately.
|
|
1218
|
+
# set any nan values to 0
|
|
1219
|
+
data = np.nan_to_num(data)
|
|
1220
|
+
# leave extrapolated values at nan
|
|
1221
|
+
abs_data = np.abs(data)
|
|
1222
|
+
interp_func_mag = interpolate.CloughTocher2DInterpolator(data_coords, abs_data, fill_value=np.nan)
|
|
1223
|
+
interp_func_phasor = interpolate.CloughTocher2DInterpolator(data_coords, data / abs_data, fill_value=np.nan)
|
|
1224
|
+
|
|
1225
|
+
# stack coordinates so shape is ..., 2
|
|
1226
|
+
if len(interp_v1.shape) > 1 or flat:
|
|
1227
|
+
interp_pos = np.stack((interp_v1, interp_v2), axis=-1).reshape((-1, 2))
|
|
1228
|
+
else:
|
|
1229
|
+
interp_pos_m = np.meshgrid(interp_v1, interp_v2, indexing="ij")
|
|
1230
|
+
interp_pos = np.stack(interp_pos_m, axis=-1).reshape((-1, 2))
|
|
1231
|
+
|
|
1232
|
+
# create result coords
|
|
1233
|
+
if flat:
|
|
1234
|
+
interp_coords = {"".join(coords.keys()): np.arange(0, len(interp_v1))}
|
|
1235
|
+
# interpolated coords are the same as the argument coords if a meshgrid was passed in
|
|
1236
|
+
elif len(interp_v1.shape) > 1:
|
|
1237
|
+
interp_coords = interp_v1.coords
|
|
1238
|
+
# otherwise use the argument values as coords
|
|
1239
|
+
else:
|
|
1240
|
+
interp_coords = coords
|
|
1241
|
+
|
|
1242
|
+
# evaluate interpolation
|
|
1243
|
+
with np.errstate(all="ignore"):
|
|
1244
|
+
phasor = interp_func_phasor(interp_pos)
|
|
1245
|
+
data_interp = interp_func_mag(interp_pos) * (phasor / np.abs(phasor))
|
|
1246
|
+
|
|
1247
|
+
interp_data = ldarray(
|
|
1248
|
+
data_interp.reshape(*[len(v) for v in interp_coords.values()], *data.shape[1:]),
|
|
1249
|
+
coords = dict(
|
|
1250
|
+
**interp_coords, **{k: v for k, v in self.coords.items() if k not in (*interp_keys, "".join(interp_keys))}
|
|
1251
|
+
)
|
|
1071
1252
|
)
|
|
1072
1253
|
|
|
1254
|
+
# add flattened coordinates as attributes
|
|
1255
|
+
if flat:
|
|
1256
|
+
interp_data.attrs = {k: v for k, v in coords.items()}
|
|
1257
|
+
|
|
1258
|
+
return interp_data
|
|
1259
|
+
|
|
1073
1260
|
@classmethod
|
|
1074
1261
|
def load(cls, filepath: str, **kwargs):
|
|
1075
1262
|
"""
|
|
@@ -1153,6 +1340,260 @@ class ldarray(np.ndarray):
|
|
|
1153
1340
|
|
|
1154
1341
|
return ldarray(super().transpose(order_idx), coords=coords)
|
|
1155
1342
|
|
|
1343
|
+
def plot(
|
|
1344
|
+
self,
|
|
1345
|
+
xaxis: str = None,
|
|
1346
|
+
xfmt: str = "real",
|
|
1347
|
+
yfmt: str = "real",
|
|
1348
|
+
legend: bool = True,
|
|
1349
|
+
ax = None,
|
|
1350
|
+
lines = None,
|
|
1351
|
+
ymin: float = None,
|
|
1352
|
+
ymax: float = None,
|
|
1353
|
+
format_axes: bool = True,
|
|
1354
|
+
**kwargs
|
|
1355
|
+
):
|
|
1356
|
+
"""
|
|
1357
|
+
Create line plot for labeled numpy array.
|
|
1358
|
+
|
|
1359
|
+
Parameters
|
|
1360
|
+
----------
|
|
1361
|
+
xaxis : str, optional
|
|
1362
|
+
dimension to plot along the x-axis, chooses the first dimension if not provided.
|
|
1363
|
+
|
|
1364
|
+
ax : plt.Axes, optional
|
|
1365
|
+
matplotlib axes object
|
|
1366
|
+
|
|
1367
|
+
xfmt : (np.ndarray) -> np.ndarray, optional
|
|
1368
|
+
String value that determines how to format the x-axis data before plotting.
|
|
1369
|
+
An arbitrary function is also supported that accepts a 1D numpy array and returns a formatted array.
|
|
1370
|
+
|
|
1371
|
+
The following string values are supported for the xmft or yfmt arguments:
|
|
1372
|
+
- "db20" : `20 * np.log10(...)`
|
|
1373
|
+
- "db10" : `10 * np.log10(...)`
|
|
1374
|
+
- "abs" : `np.abs(...)`
|
|
1375
|
+
- "mag" : `np.abs(...)`
|
|
1376
|
+
- "deg" : `np.angle(..., deg=True)`
|
|
1377
|
+
- "rad" : `np.angle(..., deg=False)`
|
|
1378
|
+
- "angle": `np.angle(..., deg=False)`
|
|
1379
|
+
- "real" : `np.real(...)`
|
|
1380
|
+
- "imag" : `np.imag(...)`
|
|
1381
|
+
- "deg2rad" : `np.deg2rad(...)`
|
|
1382
|
+
- "rad2deg" : `np.rad2deg(...)`
|
|
1383
|
+
|
|
1384
|
+
yfmt : (np.ndarray) -> np.ndarray, optional
|
|
1385
|
+
String value that determines how to format the y-axis data before plotting.
|
|
1386
|
+
An arbitrary function is also supported that accepts a 1D numpy array and returns a formatted array.
|
|
1387
|
+
|
|
1388
|
+
**kwargs
|
|
1389
|
+
keys that are in coordinates are passed to .sel(). Remaining kwargs are passed to ax.plot()
|
|
1390
|
+
|
|
1391
|
+
"""
|
|
1392
|
+
|
|
1393
|
+
# create axes if one is not provided
|
|
1394
|
+
if ax is None:
|
|
1395
|
+
import matplotlib.pyplot as plt
|
|
1396
|
+
ax = plt.gca()
|
|
1397
|
+
|
|
1398
|
+
# plot along first dimension by default
|
|
1399
|
+
if xaxis is None:
|
|
1400
|
+
xaxis = list(self.coords.keys())[0]
|
|
1401
|
+
|
|
1402
|
+
# select a format function from one of the defaults if provided as a string
|
|
1403
|
+
ylabel = ""
|
|
1404
|
+
yfmt_str = yfmt
|
|
1405
|
+
if isinstance(yfmt, str):
|
|
1406
|
+
ylabel = yfmt
|
|
1407
|
+
yfmt = utils.DATA_FMT_FUNC[yfmt]
|
|
1408
|
+
|
|
1409
|
+
if isinstance(xfmt, str):
|
|
1410
|
+
xfmt = utils.DATA_FMT_FUNC[xfmt]
|
|
1411
|
+
|
|
1412
|
+
# xaxis coords
|
|
1413
|
+
xaxis_coords = xfmt(self.coords[xaxis])
|
|
1414
|
+
|
|
1415
|
+
# select data
|
|
1416
|
+
sel_coords = {k: np.atleast_1d(kwargs.pop(k)) for k in self.coords.keys() if k in kwargs.keys()}
|
|
1417
|
+
data = self.sel(**sel_coords)
|
|
1418
|
+
|
|
1419
|
+
# coords with more than one value (other than the x-axis)
|
|
1420
|
+
other_coords = {k: v for k, v in data.coords.items() if k != xaxis and len(v) > 1}
|
|
1421
|
+
# coords with only one value, these will not be included in legend since they're the same for all lines
|
|
1422
|
+
unitary_coords = {k: v for k, v in data.coords.items() if k != xaxis and len(v) == 1}
|
|
1423
|
+
# label for title with all unitary coords
|
|
1424
|
+
unitary_label = ", ".join([utils.format_label(k, v.item()) for k, v in unitary_coords.items()])
|
|
1425
|
+
|
|
1426
|
+
# all combinations of coordinates
|
|
1427
|
+
combinations = list(product(*other_coords.values()))
|
|
1428
|
+
|
|
1429
|
+
lines_new = []
|
|
1430
|
+
|
|
1431
|
+
for i, comb_i in enumerate(combinations):
|
|
1432
|
+
# get single combination, only dimension should be x-axis
|
|
1433
|
+
coords_dict = {k: comb_i[i] for (i, k) in enumerate(other_coords.keys())}
|
|
1434
|
+
ln_data = data.sel(**coords_dict).squeeze()
|
|
1435
|
+
|
|
1436
|
+
# update line data if lines were provided
|
|
1437
|
+
if lines is not None:
|
|
1438
|
+
lines[i].set_ydata(yfmt(ln_data))
|
|
1439
|
+
# add lines to plot
|
|
1440
|
+
else:
|
|
1441
|
+
# build legend label
|
|
1442
|
+
label = ", ".join([utils.format_label(k, v) for k, v in coords_dict.items()])
|
|
1443
|
+
lines_new += ax.plot(xaxis_coords, yfmt(ln_data), label=label, **kwargs)
|
|
1444
|
+
|
|
1445
|
+
if lines is None and format_axes:
|
|
1446
|
+
|
|
1447
|
+
if legend and len(combinations) > 1 and len(combinations) < 7:
|
|
1448
|
+
ax.legend()
|
|
1449
|
+
|
|
1450
|
+
ax.set_xlabel(xaxis)
|
|
1451
|
+
ax.set_title(f"{unitary_label}", fontsize="medium")
|
|
1452
|
+
ax.grid(True)
|
|
1453
|
+
|
|
1454
|
+
# set upper/lower limit to a multiple of 5 for dB plot.
|
|
1455
|
+
if yfmt_str in ("db20", "db10"):
|
|
1456
|
+
if ymax is None:
|
|
1457
|
+
ymax = np.ceil(np.nanmax(yfmt(data)) / 5) * 5
|
|
1458
|
+
if ymin is None:
|
|
1459
|
+
ymin = np.floor(np.nanmin(yfmt(data)) / 5) * 5
|
|
1460
|
+
# clip to - 40dB range
|
|
1461
|
+
ymin = np.clip(ymin, ymax - 40, None)
|
|
1462
|
+
|
|
1463
|
+
ymin = ax.get_ylim()[0] if ymin is None else ymin
|
|
1464
|
+
ymax = ax.get_ylim()[1] if ymax is None else ymax
|
|
1465
|
+
|
|
1466
|
+
try:
|
|
1467
|
+
ax.set_ylim((ymin, ymax))
|
|
1468
|
+
except:
|
|
1469
|
+
pass
|
|
1156
1470
|
|
|
1157
|
-
|
|
1471
|
+
# if polar axes, add the ylabel to the last tick marker
|
|
1472
|
+
if ax.name == "polar":
|
|
1473
|
+
ax.set_theta_zero_location('N')
|
|
1474
|
+
ax.set_theta_direction(-1)
|
|
1475
|
+
|
|
1476
|
+
# polar always interprets the data in radians, set the plot range to be from -180° to 180°.
|
|
1477
|
+
ax.set_thetalim(-np.pi, np.pi)
|
|
1478
|
+
ax.set_thetagrids(range(-180, 180, 45))
|
|
1479
|
+
ax.tick_params(labelsize='small')
|
|
1480
|
+
|
|
1481
|
+
# add label to last tick marker
|
|
1482
|
+
labels = [f"{t:.0f}" for t in ax.get_yticks()]
|
|
1483
|
+
labels[-1] += ("dB" if yfmt_str in ("db20", "db10") else ylabel[:3])
|
|
1484
|
+
ax.set_yticks(ax.get_yticks(), labels)
|
|
1485
|
+
|
|
1486
|
+
# setup cartesian axes limits
|
|
1487
|
+
else:
|
|
1488
|
+
ax.set_ylabel(ylabel)
|
|
1489
|
+
ax.set_xlim([np.nanmin(xaxis_coords), np.nanmax(xaxis_coords)])
|
|
1490
|
+
|
|
1491
|
+
return lines_new
|
|
1492
|
+
else:
|
|
1493
|
+
return lines
|
|
1494
|
+
|
|
1495
|
+
def pcolormesh(
|
|
1496
|
+
self,
|
|
1497
|
+
xaxis: str,
|
|
1498
|
+
yaxis: str,
|
|
1499
|
+
xfmt: str = "real",
|
|
1500
|
+
yfmt: str = "real",
|
|
1501
|
+
zfmt: str = "real",
|
|
1502
|
+
ax = None,
|
|
1503
|
+
mesh = None,
|
|
1504
|
+
colorbar : dict = True,
|
|
1505
|
+
**kwargs
|
|
1506
|
+
):
|
|
1507
|
+
"""
|
|
1508
|
+
Create pcolormesh plot for labeled numpy array.
|
|
1509
|
+
|
|
1510
|
+
Parameters
|
|
1511
|
+
----------
|
|
1512
|
+
xaxis : str, optional
|
|
1513
|
+
dimension to plot along the x-axis
|
|
1514
|
+
|
|
1515
|
+
yaxis : str, optional
|
|
1516
|
+
dimension to plot along the y-axis
|
|
1517
|
+
|
|
1518
|
+
xfmt : (np.ndarray) -> np.ndarray, optional
|
|
1519
|
+
String value that determines how to format the x-axis data before plotting.
|
|
1520
|
+
An arbitrary function is also supported that accepts a 1D numpy array and returns a formatted array.
|
|
1521
|
+
|
|
1522
|
+
The following string values are supported for the xfmt or yfmt arguments:
|
|
1523
|
+
- "db20" : `20 * np.log10(...)`
|
|
1524
|
+
- "db10" : `10 * np.log10(...)`
|
|
1525
|
+
- "abs" : `np.abs(...)`
|
|
1526
|
+
- "deg" : `np.angle(..., deg=True)`
|
|
1527
|
+
- "rad" : `np.angle(..., deg=False)`
|
|
1528
|
+
- "angle": `np.angle(..., deg=False)`
|
|
1529
|
+
- "real" : `np.real(...)`
|
|
1530
|
+
- "imag" : `np.imag(...)`
|
|
1531
|
+
- "deg2rad" : `np.deg2rad(...)`
|
|
1532
|
+
- "rad2deg" : `np.rad2deg(...)`
|
|
1533
|
+
|
|
1534
|
+
yfmt : (np.ndarray) -> np.ndarray, optional
|
|
1535
|
+
String value that determines how to format the y-axis data before plotting.
|
|
1536
|
+
An arbitrary function is also supported that accepts a 1D numpy array and returns a formatted array.
|
|
1537
|
+
|
|
1538
|
+
zfmt : (np.ndarray) -> np.ndarray, optional
|
|
1539
|
+
String value that determines how to format the z-axis data before plotting.
|
|
1540
|
+
An arbitrary function is also supported that accepts a 2D numpy array and returns a formatted array.
|
|
1541
|
+
|
|
1542
|
+
|
|
1543
|
+
**kwargs
|
|
1544
|
+
keys that are in coordinates are passed to .sel(). Remaining kwargs are passed to ax.pcolormesh()
|
|
1545
|
+
|
|
1546
|
+
"""
|
|
1547
|
+
|
|
1548
|
+
# create axes if one is not provided
|
|
1549
|
+
if ax is None:
|
|
1550
|
+
import matplotlib.pyplot as plt
|
|
1551
|
+
ax = plt.gca()
|
|
1552
|
+
|
|
1553
|
+
if colorbar is True:
|
|
1554
|
+
colorbar = dict()
|
|
1555
|
+
|
|
1556
|
+
# select a format function from one of the defaults if provided as a string
|
|
1557
|
+
zlabel = ""
|
|
1558
|
+
if isinstance(zfmt, str):
|
|
1559
|
+
zlabel = zfmt
|
|
1560
|
+
zfmt = utils.DATA_FMT_FUNC[zfmt]
|
|
1561
|
+
|
|
1562
|
+
if isinstance(xfmt, str):
|
|
1563
|
+
xfmt = utils.DATA_FMT_FUNC[xfmt]
|
|
1564
|
+
|
|
1565
|
+
if isinstance(yfmt, str):
|
|
1566
|
+
yfmt = utils.DATA_FMT_FUNC[yfmt]
|
|
1567
|
+
|
|
1568
|
+
# select data
|
|
1569
|
+
sel_coords = {k: np.atleast_1d(kwargs.pop(k)) for k in self.coords.keys() if k in kwargs.keys()}
|
|
1570
|
+
data = self.sel(**sel_coords)
|
|
1571
|
+
|
|
1572
|
+
# data must have only 2 dimensions at this point
|
|
1573
|
+
if data.squeeze().ndim != 2:
|
|
1574
|
+
raise ValueError("Data must have only 2 dimensions.")
|
|
1575
|
+
|
|
1576
|
+
# coords with only one value, these will not be included in legend since they're the same for all lines
|
|
1577
|
+
unitary_coords = {k: v for k, v in data.coords.items() if k != xaxis and len(v) == 1}
|
|
1578
|
+
# label for title with all unitary coords
|
|
1579
|
+
unitary_label = ", ".join([utils.format_label(k, v.item()) for k, v in unitary_coords.items()])
|
|
1580
|
+
|
|
1581
|
+
data = data.squeeze().transpose((yaxis, xaxis))
|
|
1582
|
+
|
|
1583
|
+
# add new colormesh object to plot
|
|
1584
|
+
if mesh is None:
|
|
1585
|
+
mesh = ax.pcolormesh(xfmt(data.coords[xaxis]), yfmt(data.coords[yaxis]), zfmt(data), **kwargs)
|
|
1586
|
+
|
|
1587
|
+
if isinstance(colorbar, dict):
|
|
1588
|
+
c_kwargs = dict(label=zlabel, **colorbar) if "label" not in colorbar.keys() else colorbar
|
|
1589
|
+
ax.figure.colorbar(mesh, **c_kwargs)
|
|
1590
|
+
|
|
1591
|
+
# update existing colormesh
|
|
1592
|
+
else:
|
|
1593
|
+
mesh.set_array(zfmt(data))
|
|
1594
|
+
|
|
1595
|
+
ax.set_xlabel(xaxis)
|
|
1596
|
+
ax.set_title(f"{unitary_label}", fontsize="medium")
|
|
1597
|
+
ax.set_ylabel(yaxis)
|
|
1158
1598
|
|
|
1599
|
+
return mesh
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
DATA_FMT_FUNC = {
|
|
4
|
+
"mag": np.abs,
|
|
5
|
+
"abs": np.abs,
|
|
6
|
+
"db20": lambda x: 20 * np.log10(np.abs(x)),
|
|
7
|
+
"db10": lambda x: 10 * np.log10(np.abs(x)),
|
|
8
|
+
"deg": lambda x: np.angle(x, deg=True),
|
|
9
|
+
"rad": lambda x: np.angle(x, deg=False),
|
|
10
|
+
"angle": lambda x: np.angle(x, deg=False),
|
|
11
|
+
"deg_unwrap": lambda x: np.rad2deg(np.unwrap(np.angle(x))),
|
|
12
|
+
"real": np.real,
|
|
13
|
+
"imag": np.imag,
|
|
14
|
+
"deg2rad" : np.deg2rad,
|
|
15
|
+
"rad2deg" : np.rad2deg
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
LABEL_FMT_FUNC = dict(
|
|
19
|
+
frequency = lambda x: f"{x/1e9:.3f}GHz"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def check_shapes(a: tuple, b: tuple):
|
|
24
|
+
"""
|
|
25
|
+
Check that the shape tuples a and b match
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
if len(a) != len(b):
|
|
29
|
+
return False
|
|
30
|
+
|
|
31
|
+
# check that the length of each dimension matches
|
|
32
|
+
return all([a[i] == b[i] for i in range(len(a))])
|
|
33
|
+
|
|
34
|
+
def check_coords(c1: dict, c2: dict, tolerance=1e-6):
|
|
35
|
+
"""
|
|
36
|
+
Check that two coordinates are identical
|
|
37
|
+
"""
|
|
38
|
+
assert tuple(c1.keys()) == tuple(c2.keys()), f"Coord keys are different: {c1.keys()} vs {c2.keys()}"
|
|
39
|
+
|
|
40
|
+
for k in c1.keys():
|
|
41
|
+
if np.any(np.abs(c1[k] - c2[k]) > tolerance):
|
|
42
|
+
return False
|
|
43
|
+
|
|
44
|
+
return True
|
|
45
|
+
|
|
46
|
+
def add_data_formatter(key: str, func):
|
|
47
|
+
"""
|
|
48
|
+
Add a custom data format function. Function must accept a single numpy array argument and return a
|
|
49
|
+
formatted array of the same shape.
|
|
50
|
+
"""
|
|
51
|
+
DATA_FMT_FUNC[key] = func
|
|
52
|
+
|
|
53
|
+
def add_label_formatter(key: str, func):
|
|
54
|
+
"""
|
|
55
|
+
Add a custom data format function. Function must accept a single scalar argument and return a
|
|
56
|
+
formatted string
|
|
57
|
+
"""
|
|
58
|
+
LABEL_FMT_FUNC[key] = func
|
|
59
|
+
|
|
60
|
+
def format_label(
|
|
61
|
+
key: str, value
|
|
62
|
+
) -> str:
|
|
63
|
+
|
|
64
|
+
if key in LABEL_FMT_FUNC.keys():
|
|
65
|
+
return LABEL_FMT_FUNC[key](value)
|
|
66
|
+
|
|
67
|
+
# create default label formatters if not included in look up table
|
|
68
|
+
if isinstance(value, (float, np.floating)):
|
|
69
|
+
return f"{key}={value:.3f}"
|
|
70
|
+
elif isinstance(value, (int, np.integer)):
|
|
71
|
+
return f"{key}={value}"
|
|
72
|
+
# don't include key in label for string coordinates
|
|
73
|
+
else:
|
|
74
|
+
return "{}".format(value)
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "np-struct"
|
|
7
|
-
version = "0.1
|
|
7
|
+
version = "0.2.1"
|
|
8
8
|
description = "Interface for NumPy structured arrays"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
authors = [{ name = "Rick Lyon", email = "rlyon14@gmail.com" }]
|
|
@@ -16,7 +16,7 @@ classifiers = [
|
|
|
16
16
|
]
|
|
17
17
|
keywords = ["numpy", "structured array", "struct"]
|
|
18
18
|
dependencies = [
|
|
19
|
-
"numpy", "scipy"
|
|
19
|
+
"numpy", "scipy",
|
|
20
20
|
]
|
|
21
21
|
requires-python = ">=3.7"
|
|
22
22
|
|
|
@@ -30,7 +30,7 @@ include = ["np_struct"]
|
|
|
30
30
|
repository = "https://github.com/ricklyon/np_struct"
|
|
31
31
|
|
|
32
32
|
[tool.bumpversion]
|
|
33
|
-
current_version = "0.1
|
|
33
|
+
current_version = "0.2.1"
|
|
34
34
|
commit = true
|
|
35
35
|
tag = true
|
|
36
36
|
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import unittest
|
|
2
|
-
from np_struct import ldarray, Coords
|
|
2
|
+
from np_struct import ldarray, Coords, utils
|
|
3
3
|
import numpy as np
|
|
4
4
|
from numpy import testing as npt
|
|
5
5
|
import datetime as dt
|
|
6
6
|
from dateutil import relativedelta as rdt
|
|
7
7
|
import os
|
|
8
8
|
|
|
9
|
+
from scipy import ndimage
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
9
12
|
|
|
10
13
|
class TestLdArray(unittest.TestCase):
|
|
11
14
|
|
|
@@ -39,6 +42,7 @@ class TestLdArray(unittest.TestCase):
|
|
|
39
42
|
npt.assert_array_equal(ld_2, data[np.array([1, 0]), 0])
|
|
40
43
|
npt.assert_array_equal(ld_2.coords["a"], [1, 0])
|
|
41
44
|
|
|
45
|
+
|
|
42
46
|
def test_float_index(self):
|
|
43
47
|
|
|
44
48
|
coords = dict(a=['data1', 'data2'], b=np.arange(0, 20, 0.2))
|
|
@@ -146,7 +150,7 @@ class TestLdArray(unittest.TestCase):
|
|
|
146
150
|
np.testing.assert_array_almost_equal(ld_int.sel(a="exp(t)"), np.exp(1j * t_int), decimal=2)
|
|
147
151
|
np.testing.assert_array_almost_equal(ld_int.sel(a="exp(0.5t)"), np.exp(0.5 * 1j * t_int), decimal=2)
|
|
148
152
|
|
|
149
|
-
def
|
|
153
|
+
def test_ldarray_indexing(self):
|
|
150
154
|
coords = dict(a=[1, 2], b=['data1', 'data2', 'data3'])
|
|
151
155
|
ld = ldarray([[10, 8, 6], [0, 2, 4]], coords=coords)
|
|
152
156
|
|
|
@@ -167,6 +171,42 @@ class TestLdArray(unittest.TestCase):
|
|
|
167
171
|
np.testing.assert_array_equal(data.coords["x"], [0, 1])
|
|
168
172
|
np.testing.assert_array_equal(data.coords["y"], [0, 1])
|
|
169
173
|
|
|
174
|
+
def test_ldarray_indexing2(self):
|
|
175
|
+
# test index values that are ldarrays
|
|
176
|
+
|
|
177
|
+
coords = dict(a=np.arange(3), b=np.arange(10, 16), c=np.arange(3))
|
|
178
|
+
data = np.arange(54).reshape(3, 6, 3)
|
|
179
|
+
ld = ldarray(data, coords=coords)
|
|
180
|
+
|
|
181
|
+
# index must include all coordinates and have the same shape as the data,
|
|
182
|
+
# except the indexing dimension (in this case "b")
|
|
183
|
+
idx_v = np.ones((3, 2, 2, 3)) * 10
|
|
184
|
+
idx_v[0, 0, :] = 12
|
|
185
|
+
idx_v[2, 1, :] = 11
|
|
186
|
+
|
|
187
|
+
idx = ldarray(idx_v, coords=dict(a=np.arange(3), new1=[12, 11], new2=[6, 7], c=np.arange(3)))
|
|
188
|
+
result = ld.sel(b=idx)
|
|
189
|
+
|
|
190
|
+
# both values of new2 axis should be at b=12 for the a=0, new1=12 dimension
|
|
191
|
+
np.testing.assert_array_almost_equal(result.sel(a=0, new1=12)[0], ld.sel(a=0, b=12))
|
|
192
|
+
np.testing.assert_array_almost_equal(result.sel(a=0, new1=12)[1], ld.sel(a=0, b=12))
|
|
193
|
+
np.testing.assert_array_almost_equal(result.sel(a=0, new1=11)[0], ld.sel(a=0, b=10))
|
|
194
|
+
np.testing.assert_array_almost_equal(result.sel(a=0, new1=11)[1], ld.sel(a=0, b=10))
|
|
195
|
+
|
|
196
|
+
# both new1 and new2 should be at b=10 for the a=1 dimension
|
|
197
|
+
np.testing.assert_array_almost_equal(result.sel(a=1)[0, 0], ld.sel(a=1, b=10))
|
|
198
|
+
np.testing.assert_array_almost_equal(result.sel(a=1)[1, 0], ld.sel(a=1, b=10))
|
|
199
|
+
np.testing.assert_array_almost_equal(result.sel(a=1)[0, 1], ld.sel(a=1, b=10))
|
|
200
|
+
np.testing.assert_array_almost_equal(result.sel(a=1)[1, 1], ld.sel(a=1, b=10))
|
|
201
|
+
|
|
202
|
+
# both values of new 2 should be at b=11 for the a=2, new1=11 dimension
|
|
203
|
+
np.testing.assert_array_almost_equal(result.sel(a=2, new1=12)[0], ld.sel(a=2, b=10))
|
|
204
|
+
np.testing.assert_array_almost_equal(result.sel(a=2, new1=12)[1], ld.sel(a=2, b=10))
|
|
205
|
+
np.testing.assert_array_almost_equal(result.sel(a=2, new1=11)[0], ld.sel(a=2, b=11))
|
|
206
|
+
np.testing.assert_array_almost_equal(result.sel(a=2, new1=11)[1], ld.sel(a=2, b=11))
|
|
207
|
+
|
|
208
|
+
self.assertTrue(utils.check_coords(result.coords, idx.coords), "coords are not equal.")
|
|
209
|
+
|
|
170
210
|
def test_interpolation_nan(self):
|
|
171
211
|
t = np.linspace(0, 2 * np.pi, 21)
|
|
172
212
|
t_int = np.linspace(1, 2, 61)
|
|
@@ -191,6 +231,46 @@ class TestLdArray(unittest.TestCase):
|
|
|
191
231
|
|
|
192
232
|
np.testing.assert_array_almost_equal(ld.interpolate(a=[-1e-7], t=t_int)[0], np.sin(t_int), decimal=2)
|
|
193
233
|
|
|
234
|
+
def test_interpolation_flat(self):
|
|
235
|
+
# avoid interpolating at the endpoints, it's close to the right value but hard to test exactly
|
|
236
|
+
t = np.linspace(0, 2 * np.pi, 21)
|
|
237
|
+
|
|
238
|
+
data = np.array([np.sin(t), np.cos(t)])
|
|
239
|
+
ld = ldarray(data, coords = dict(a=["sin", "cos"], t=t))
|
|
240
|
+
|
|
241
|
+
coords = dict(t = [0, 3, 6], a=["sin", "sin", "cos"])
|
|
242
|
+
ld.interpolate(**coords, flat=True)
|
|
243
|
+
|
|
244
|
+
def test_interpolate_2d(self):
|
|
245
|
+
# test 2d interpolator on flattened data
|
|
246
|
+
u = np.linspace(-1, 1, 21)
|
|
247
|
+
v = np.linspace(-1, 1, 21)
|
|
248
|
+
|
|
249
|
+
u_m, v_m = np.meshgrid(u, v)
|
|
250
|
+
z = np.cos(u_m) * np.cos(v_m)
|
|
251
|
+
|
|
252
|
+
data = ldarray(z, coords=dict(u=u, v=v))
|
|
253
|
+
|
|
254
|
+
u_int = np.linspace(-1, 1, 141)
|
|
255
|
+
v_int = np.linspace(-1, 1, 141)
|
|
256
|
+
|
|
257
|
+
# interpolate_from_flat should work on meshgrid data as well. It is slower and not as accurate, but
|
|
258
|
+
# should be reasonably close
|
|
259
|
+
d1 = data.interpolate(u=u_int, v=v_int)
|
|
260
|
+
d2 = data.interpolate_from_flat(u=u_int, v=v_int)
|
|
261
|
+
np.testing.assert_array_less(np.mean(np.abs(d1 - d2)), 0.002)
|
|
262
|
+
|
|
263
|
+
# interpolate with pairwise points
|
|
264
|
+
data_flat = data.interpolate(u=u_m.flatten(), v=v_m.flatten(), flat=True)
|
|
265
|
+
|
|
266
|
+
np.testing.assert_array_almost_equal(np.reshape(data_flat, (21, 21)), data)
|
|
267
|
+
# flattened coords should be labeled 0-N, where N is the number of pairwise points
|
|
268
|
+
np.testing.assert_array_almost_equal(data_flat.uv, np.arange(21*21))
|
|
269
|
+
|
|
270
|
+
# interpolate the flattened data to get back to the original data
|
|
271
|
+
data_round_trip = data_flat.interpolate_from_flat(u=u, v=v)
|
|
272
|
+
np.testing.assert_array_almost_equal(data_round_trip, data)
|
|
273
|
+
|
|
194
274
|
def test_save(self):
|
|
195
275
|
|
|
196
276
|
coords = dict(a=['data1', 'data2'], b=np.arange(0, 20, 0.2))
|
|
@@ -246,5 +326,23 @@ class TestLdArray(unittest.TestCase):
|
|
|
246
326
|
|
|
247
327
|
npt.assert_array_equal(new_ld[dict(j=1.201)], [10, 11])
|
|
248
328
|
|
|
329
|
+
def test_attribute_selection(self):
|
|
330
|
+
|
|
331
|
+
coords = dict(a=['data1', 'data2'], b=np.arange(0, 20, 0.2))
|
|
332
|
+
data = np.arange(200).reshape(2, 100)
|
|
333
|
+
attrs = dict(attr1="test1", attr2="...test2", attr3=np.array([9, 8]))
|
|
334
|
+
ld = ldarray(data, coords=coords, attrs=attrs)
|
|
335
|
+
|
|
336
|
+
# check that attributes are passed to the indexed copy of the array
|
|
337
|
+
ld_sel = ld.sel(a="data1")
|
|
338
|
+
|
|
339
|
+
# change the attribute on the original array and verify the copy isn't affected
|
|
340
|
+
ld.attrs["attr3"] = np.array([[8, 7]])
|
|
341
|
+
|
|
342
|
+
self.assertEqual(ld_sel.attr1, attrs["attr1"])
|
|
343
|
+
self.assertEqual(ld_sel.attr2, attrs["attr2"])
|
|
344
|
+
np.testing.assert_array_equal(ld_sel.attr3, attrs["attr3"])
|
|
345
|
+
|
|
346
|
+
|
|
249
347
|
if __name__ == '__main__':
|
|
250
348
|
unittest.main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|