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

Multi array fix #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion xarray_filters/multi_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def multi_index_to_coords(arr, axis=0):
return coords, (dim,)
if any(name is None for name in multi.names):
raise ValueError('Expected MultiIndex with named components (found {})'.format(multi.names))
np_arrs = (np.unique(x) for x in np.array(multi.tolist()).T)
cols = np.array(multi.tolist()).T
ascend = [(1 if c[0] < c[-1] else -1) for c in cols]
np_arrs = (np.unique(c)[::a] for c, a in zip(cols, ascend))
coords = OrderedDict(zip(multi.names, np_arrs))
dims = tuple(coords)
return coords, dims
12 changes: 8 additions & 4 deletions xarray_filters/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
'to_xy_arrays']


RAVEL_ORDER = 'C'

def has_features(dset, raise_err=True, features_layer=None):
'''Check if an MLDataset has a DataArray called "features"
with dimensions (space, layer)
Expand Down Expand Up @@ -174,7 +176,7 @@ def to_features(dset, layers=None, row_dim=None,
index = getattr(arr, row_dim)
else:
index = create_multi_index(arr)
val = val.ravel()[:, np.newaxis]
val = val.ravel(order=RAVEL_ORDER)[:, np.newaxis]
coords = OrderedDict([(row_dim, index),
(col_dim, [layer])])
new_dims = (row_dim, col_dim)
Expand Down Expand Up @@ -251,10 +253,11 @@ def from_features(arr, axis=0):
val = np.full(shp, np.nan)
feature_idx = arr.indexes[arr.dims[0]]
dim_coord_pairs = feature_idx.tolist()
row, col = coords[dims[0]], coords[dims[1]]
for idx, dim_coord_pair in enumerate(dim_coord_pairs):
val[dim_coord_pair] = arr_val[idx]
val[(dim_coord_pair[0] == row), (dim_coord_pair[1] == col)] = arr_val[idx]
else:
val = arr_val.reshape(shp)
val = arr_val.reshape(shp, order=RAVEL_ORDER)
layer = simple_np_arr[j]
dset[layer] = xr.DataArray(val, coords=coords, dims=dims)
return MLDataset(dset)
Expand Down Expand Up @@ -290,7 +293,8 @@ def to_xy_arrays(dset=None, y=None, features_layer=None,
ykw = {col_dim: yname}
X = arr.isel(**xkw)
if y is None:
if yname in getattr(arr, arr.dims[-1], pd.Series([]).values):
col_names = getattr(arr, arr.dims[-1])
if yname in col_names.values:
y = arr.isel(**ykw)
if as_np:
y = y.values
Expand Down