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

Dev #355

Merged
merged 4 commits into from
Mar 11, 2025
Merged

Dev #355

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
58 changes: 32 additions & 26 deletions ldcpy/calcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,40 +345,46 @@ def magnitude_range(self) -> xr.DataArray:
The range of the dataset
"""
if not self._is_memoized('_magnitude_range'):
mag_min = self.min_abs_nonzero
mag_max = self.max_abs
mag_range = np.log10(mag_max) - np.log10(mag_min)
# print('RANGE = ', mag_range)
self._magnitude_range = mag_range

# Get the range in exponent space
def max_agg(ds):
return ds.max(skipna=True)
# def max_agg(ds):
# return ds.max(skipna=True)

def min_agg(ds):
return ds.min(skipna=True)
# def min_agg(ds):
# return ds.min(skipna=True)

# avoid divde by zero warning
a_d = abs(self._ds.copy())
# a_d = abs(self._ds.copy())
# print('a_d = ', a_d.data)
log_ds = np.log10(a_d, where=a_d.data > 0)
# log_ds = np.log10(a_d, where=a_d.data > 0)
# print('log_ds = ', log_ds.data)
# print('data: min abs nonzero = ', self.min_abs_nonzero)
# print('data: max value = ', self.max_val)
if len(self._not_agg_dims) == 0:
my_max = max_agg(log_ds)
my_min = min_agg(log_ds)
else:
stack = log_ds.stack(multi_index=tuple(self._not_agg_dims))
my_max = stack.groupby('multi_index').map(max_agg)
my_min = stack.groupby('multi_index').map(min_agg)
# if len(self._not_agg_dims) == 0:
# my_max = max_agg(log_ds)
# my_min = min_agg(log_ds)
# else:
# stack = log_ds.stack(multi_index=tuple(self._not_agg_dims))
# my_max = stack.groupby('multi_index').map(max_agg)
# my_min = stack.groupby('multi_index').map(min_agg)
# print('max exp= ', my_max)
# print('min exp = ', my_min)
if (
np.isinf(my_max).any()
or np.isinf(my_min).any()
or np.isnan(my_max).any()
or np.isnan(my_min).any()
):
self._magnitude_range = -1
return self._magnitude_range
else:
# if (
# np.isinf(my_max).any()
# or np.isinf(my_min).any()
# or np.isnan(my_max).any()
# or np.isnan(my_min).any()
# ):
# self._magnitude_range = -1
# return self._magnitude_range
# else:

self._magnitude_range = my_max - my_min
# self._magnitude_range = my_max - my_min

return self._magnitude_range

Expand Down Expand Up @@ -974,7 +980,7 @@ def quantile_value(self) -> xr.DataArray:
@property
def max_abs(self) -> xr.DataArray:
if not self._is_memoized('_max_abs'):
self._max_abs = abs(self._ds).max(dim=self._agg_dims)
self._max_abs = abs(self._ds).max(dim=self._agg_dims, skipna=True)
self._max_abs.attrs = self._ds.attrs
if hasattr(self._ds, 'units'):
self._max_abs.attrs['units'] = f'{self._ds.units}'
Expand Down Expand Up @@ -1005,7 +1011,7 @@ def min_abs_nonzero(self) -> xr.DataArray:
@property
def max_val(self) -> xr.DataArray:
if not self._is_memoized('_max_val'):
self._max_val = self._ds.max(dim=self._agg_dims)
self._max_val = self._ds.max(dim=self._agg_dims, skipna=True)
self._max_val.attrs = self._ds.attrs
if hasattr(self._ds, 'units'):
self._max_val.attrs['units'] = f'{self._ds.units}'
Expand All @@ -1015,7 +1021,7 @@ def max_val(self) -> xr.DataArray:
@property
def min_val(self) -> xr.DataArray:
if not self._is_memoized('_min_val'):
self._min_val = self._ds.min(dim=self._agg_dims)
self._min_val = self._ds.min(dim=self._agg_dims, skipna=True)
self._min_val.attrs = self._ds.attrs
if hasattr(self._ds, 'units'):
self._min_val.attrs['units'] = f'{self._ds.units}'
Expand Down
2 changes: 1 addition & 1 deletion tests/test_calcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_mean(self):

def test_magnitude_range(self):
value = float(test_overall_calcs.magnitude_range.values)
print('VALUE = ', value)
# print('VALUE = ', value)
self.assertTrue(np.isclose(value, 2, rtol=1e-04))

def test_mean_abs(self):
Expand Down
Loading