Skip to content

Commit

Permalink
v0.0.12
Browse files Browse the repository at this point in the history
v0.0.12
  • Loading branch information
nkarasiak authored Mar 6, 2024
2 parents aa6648d + ceaff93 commit 46f8e62
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.12] - 2024-03-06

### Added

- `_typer` supports custom

### Changed

- `zonal_stats` outputs only valid geometries. Set `True`
to `raise_missing_geometry` to have the old behavior.
- `zonal_stats` with geocube now manages `all_touched`.

## [0.0.11] - 2024-03-06

### Fixed

- some issues with `_typer` from accessor.
- `zonal_stats` manages index independently from row.

## [0.0.10] - 2024-03-05
Expand Down
2 changes: 1 addition & 1 deletion earthdaily/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# to hide warnings from rioxarray or nano seconds conversion
warnings.filterwarnings("ignore")

__version__ = "0.0.11"
__version__ = "0.0.12"
77 changes: 54 additions & 23 deletions earthdaily/accessor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,70 @@ class MisType(Warning):
pass


_SUPPORTED_DTYPE = [int, float, list, bool, str]


def _typer(raise_mistype=False, custom_types={}):
"""
Parameters
----------
raise_mistype : TYPE, optional
DESCRIPTION. The default is False.
custom_types : TYPE, optional
DESCRIPTION. The default is {}.
Example : {
np.ndarray:{"func":np.asarray},
xr.Dataset:{"func":xr.DataArray.to_dataset,"kwargs":{"dim":"band"}}
}
Raises
------
MisType
DESCRIPTION.
Returns
-------
TYPE
DESCRIPTION.
"""

def decorator(func):
def force(*args, **kwargs):
_args = list(args)
func_arg = func.__code__.co_varnames
for key, val in func.__annotations__.items():
if not isinstance(val, (list, tuple)):
val = [val]
idx = [i for i in range(len(func_arg)) if func_arg[i] == key][0]
for key, vals in func.__annotations__.items():
if not isinstance(vals, (list, tuple)):
vals = [vals]
val = vals[0]
idx = func.__code__.co_varnames.index(key)
is_kwargs = key in kwargs.keys()
if not is_kwargs and idx >= len(args):
continue
input_value = kwargs.get(key, None) if is_kwargs else args[idx]
if type(input_value) in val:
if not is_kwargs and idx > len(_args):
break
value = kwargs.get(key, None) if is_kwargs else args[idx]
if type(value) in vals:
continue
if (
type(kwargs.get(key)) not in val
type(kwargs.get(key)) not in vals
if is_kwargs
else type(args[idx]) not in val
else type(args[idx]) not in vals
):
if raise_mistype:
if is_kwargs:
expected = f"{type(kwargs[key]).__name__} ({kwargs[key]})"
else:
expected = f"{type(args[idx]).__name__} ({args[idx]})"
raise MisType(f"{key} expected {val.__name__}, not {expected}.")
if is_kwargs:
kwargs[key] = val[0](kwargs[key])
if any(val == k for k in custom_types.keys()):
exp = custom_types[val]
var = args[idx]
res = exp["func"](var, **exp.get("kwargs", {}))
if is_kwargs:
kwargs[key] = res
else:
_args[idx] = res
elif is_kwargs:
kwargs[key] = (
var(kwargs[key]) if var is not list else [kwargs[key]]
)
else:
_args[idx] = val[0](args[idx])
_args[idx] = var(args[idx]) if var is not list else [args[idx]]
args = tuple(_args)
return func(*args, **kwargs)

Expand Down Expand Up @@ -258,7 +289,7 @@ def available_indices(self, details=False):
return available_indices

@_typer()
def add_indices(self, index: list, **kwargs):
def add_indices(self, indices: list, **kwargs):
"""
Uses spyndex to compute and add index.
Expand All @@ -267,7 +298,7 @@ def add_indices(self, index: list, **kwargs):
Parameters
----------
index : list
indices : list
['NDVI'].
Returns
-------
Expand All @@ -279,11 +310,11 @@ def add_indices(self, index: list, **kwargs):
params = {}
params = self._auto_mapper()
params.update(**kwargs)
idx = spyndex.computeIndex(index=index, params=params, **kwargs)
idx = spyndex.computeIndex(index=indices, params=params, **kwargs)

if len(index) == 1:
idx = idx.expand_dims(index=index)
idx = idx.to_dataset(dim="index")
if len(indices) == 1:
idx = idx.expand_dims(indices=indices)
idx = idx.to_dataset(dim="indices")

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

Expand Down

0 comments on commit 46f8e62

Please sign in to comment.