Skip to content

Commit 46f8e62

Browse files
authored
v0.0.12
v0.0.12
2 parents aa6648d + ceaff93 commit 46f8e62

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.0.12] - 2024-03-06
8+
9+
### Added
10+
11+
- `_typer` supports custom
12+
13+
### Changed
14+
15+
- `zonal_stats` outputs only valid geometries. Set `True`
16+
to `raise_missing_geometry` to have the old behavior.
17+
- `zonal_stats` with geocube now manages `all_touched`.
718

819
## [0.0.11] - 2024-03-06
920

1021
### Fixed
1122

23+
- some issues with `_typer` from accessor.
1224
- `zonal_stats` manages index independently from row.
1325

1426
## [0.0.10] - 2024-03-05

earthdaily/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
# to hide warnings from rioxarray or nano seconds conversion
66
warnings.filterwarnings("ignore")
77

8-
__version__ = "0.0.11"
8+
__version__ = "0.0.12"

earthdaily/accessor/__init__.py

+54-23
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,70 @@ class MisType(Warning):
1919
pass
2020

2121

22-
_SUPPORTED_DTYPE = [int, float, list, bool, str]
23-
24-
2522
def _typer(raise_mistype=False, custom_types={}):
23+
"""
24+
25+
Parameters
26+
----------
27+
raise_mistype : TYPE, optional
28+
DESCRIPTION. The default is False.
29+
custom_types : TYPE, optional
30+
DESCRIPTION. The default is {}.
31+
Example : {
32+
np.ndarray:{"func":np.asarray},
33+
xr.Dataset:{"func":xr.DataArray.to_dataset,"kwargs":{"dim":"band"}}
34+
}
35+
Raises
36+
------
37+
MisType
38+
DESCRIPTION.
39+
40+
Returns
41+
-------
42+
TYPE
43+
DESCRIPTION.
44+
45+
"""
46+
2647
def decorator(func):
2748
def force(*args, **kwargs):
2849
_args = list(args)
29-
func_arg = func.__code__.co_varnames
30-
for key, val in func.__annotations__.items():
31-
if not isinstance(val, (list, tuple)):
32-
val = [val]
33-
idx = [i for i in range(len(func_arg)) if func_arg[i] == key][0]
50+
for key, vals in func.__annotations__.items():
51+
if not isinstance(vals, (list, tuple)):
52+
vals = [vals]
53+
val = vals[0]
54+
idx = func.__code__.co_varnames.index(key)
3455
is_kwargs = key in kwargs.keys()
35-
if not is_kwargs and idx >= len(args):
36-
continue
37-
input_value = kwargs.get(key, None) if is_kwargs else args[idx]
38-
if type(input_value) in val:
56+
if not is_kwargs and idx > len(_args):
57+
break
58+
value = kwargs.get(key, None) if is_kwargs else args[idx]
59+
if type(value) in vals:
3960
continue
4061
if (
41-
type(kwargs.get(key)) not in val
62+
type(kwargs.get(key)) not in vals
4263
if is_kwargs
43-
else type(args[idx]) not in val
64+
else type(args[idx]) not in vals
4465
):
4566
if raise_mistype:
4667
if is_kwargs:
4768
expected = f"{type(kwargs[key]).__name__} ({kwargs[key]})"
4869
else:
4970
expected = f"{type(args[idx]).__name__} ({args[idx]})"
5071
raise MisType(f"{key} expected {val.__name__}, not {expected}.")
51-
if is_kwargs:
52-
kwargs[key] = val[0](kwargs[key])
72+
if any(val == k for k in custom_types.keys()):
73+
exp = custom_types[val]
74+
var = args[idx]
75+
res = exp["func"](var, **exp.get("kwargs", {}))
76+
if is_kwargs:
77+
kwargs[key] = res
78+
else:
79+
_args[idx] = res
80+
elif is_kwargs:
81+
kwargs[key] = (
82+
var(kwargs[key]) if var is not list else [kwargs[key]]
83+
)
5384
else:
54-
_args[idx] = val[0](args[idx])
85+
_args[idx] = var(args[idx]) if var is not list else [args[idx]]
5586
args = tuple(_args)
5687
return func(*args, **kwargs)
5788

@@ -258,7 +289,7 @@ def available_indices(self, details=False):
258289
return available_indices
259290

260291
@_typer()
261-
def add_indices(self, index: list, **kwargs):
292+
def add_indices(self, indices: list, **kwargs):
262293
"""
263294
Uses spyndex to compute and add index.
264295
@@ -267,7 +298,7 @@ def add_indices(self, index: list, **kwargs):
267298
268299
Parameters
269300
----------
270-
index : list
301+
indices : list
271302
['NDVI'].
272303
Returns
273304
-------
@@ -279,11 +310,11 @@ def add_indices(self, index: list, **kwargs):
279310
params = {}
280311
params = self._auto_mapper()
281312
params.update(**kwargs)
282-
idx = spyndex.computeIndex(index=index, params=params, **kwargs)
313+
idx = spyndex.computeIndex(index=indices, params=params, **kwargs)
283314

284-
if len(index) == 1:
285-
idx = idx.expand_dims(index=index)
286-
idx = idx.to_dataset(dim="index")
315+
if len(indices) == 1:
316+
idx = idx.expand_dims(indices=indices)
317+
idx = idx.to_dataset(dim="indices")
287318

288319
return xr.merge((self._obj, idx))
289320

0 commit comments

Comments
 (0)