Skip to content

Commit

Permalink
Add moving window in iter_on, prepare tag 3.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
AntSimi committed Oct 14, 2022
1 parent 3359eda commit 9d408e5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 39 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,25 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
Changed
^^^^^^^

Fixed
^^^^^

Added
^^^^^


[3.6.1] - 2022-10-14
--------------------
Changed
^^^^^^^

- Rewrite particle candidate to be easily parallelize

Fixed
^^^^^

- Check strictly increasing coordinates for RegularGridDataset.
- Grid mask is check to replace mask monovalue by 2D mask with fixed value

Added
^^^^^
Expand Down
4 changes: 2 additions & 2 deletions src/py_eddy_tracker/observations/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ def longer_than(self, nb_day_min=-1, nb_day_max=-1):
nb_day_max = 1000000000000
mask = zeros(self.shape, dtype="bool")
t = self.time
for i, b0, b1 in self.iter_on(self.track):
for i, _, _ in self.iter_on(self.track):
nb = i.stop - i.start
if nb == 0:
continue
if nb_day_min <= ptp(t[i]) <= nb_day_max:
if nb_day_min <= (ptp(t[i]) + 1) <= nb_day_max:
mask[i] = True
return self.extract_with_mask(mask)

Expand Down
79 changes: 45 additions & 34 deletions src/py_eddy_tracker/observations/observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
local_to_coordinates,
reverse_index,
wrap_longitude,
window_index,
)
from ..poly import (
bbox_intersection,
Expand Down Expand Up @@ -574,51 +575,58 @@ def __iter__(self):
for obs in self.obs:
yield obs

def iter_on(self, xname, bins=None):
def iter_on(self, xname, window=None, bins=None):
"""
Yield observation group for each bin.
:param str,array xname:
:param float,None window: if defined we use a moving window with value like half window
:param array bins: bounds of each bin
:yield array,float,float: index in self, lower bound, upper bound
.. minigallery:: py_eddy_tracker.EddiesObservations.iter_on
"""
x = self.parse_varname(xname)
d = x[1:] - x[:-1]
if bins is None:
bins = arange(x.min(), x.max() + 2)
elif not isinstance(bins, ndarray):
bins = array(bins)
nb_bins = len(bins) - 1

# Not monotonous
if (d < 0).any():
# If bins cover a small part of value
test, translate, x = iter_mode_reduce(x, bins)
# convert value in bins number
i = numba_digitize(x, bins) - 1
# Order by bins
i_sort = i.argsort()
# If in reduced mode we will translate i_sort in full array index
i_sort_ = translate[i_sort] if test else i_sort
# Bound for each bins in sorting view
i0, i1, _ = build_index(i[i_sort])
m = ~(i0 == i1)
i0, i1 = i0[m], i1[m]
for i0_, i1_ in zip(i0, i1):
i_bins = i[i_sort[i0_]]
if i_bins == -1 or i_bins == nb_bins:
continue
yield i_sort_[i0_:i1_], bins[i_bins], bins[i_bins + 1]
if window is not None:
x0 = arange(x.min(), x.max()) if bins is None else array(bins)
i_ordered, first_index, last_index = window_index(x, x0, window)
for x_, i0, i1 in zip(x0, first_index, last_index):
yield i_ordered[i0: i1], x_ - window, x_ + window
else:
i = numba_digitize(x, bins) - 1
i0, i1, _ = build_index(i)
m = ~(i0 == i1)
i0, i1 = i0[m], i1[m]
for i0_, i1_ in zip(i0, i1):
i_bins = i[i0_]
yield slice(i0_, i1_), bins[i_bins], bins[i_bins + 1]
d = x[1:] - x[:-1]
if bins is None:
bins = arange(x.min(), x.max() + 2)
elif not isinstance(bins, ndarray):
bins = array(bins)
nb_bins = len(bins) - 1

# Not monotonous
if (d < 0).any():
# If bins cover a small part of value
test, translate, x = iter_mode_reduce(x, bins)
# convert value in bins number
i = numba_digitize(x, bins) - 1
# Order by bins
i_sort = i.argsort()
# If in reduced mode we will translate i_sort in full array index
i_sort_ = translate[i_sort] if test else i_sort
# Bound for each bins in sorting view
i0, i1, _ = build_index(i[i_sort])
m = ~(i0 == i1)
i0, i1 = i0[m], i1[m]
for i0_, i1_ in zip(i0, i1):
i_bins = i[i_sort[i0_]]
if i_bins == -1 or i_bins == nb_bins:
continue
yield i_sort_[i0_:i1_], bins[i_bins], bins[i_bins + 1]
else:
i = numba_digitize(x, bins) - 1
i0, i1, _ = build_index(i)
m = ~(i0 == i1)
i0, i1 = i0[m], i1[m]
for i0_, i1_ in zip(i0, i1):
i_bins = i[i0_]
yield slice(i0_, i1_), bins[i_bins], bins[i_bins + 1]

def align_on(self, other, var_name="time", all_ref=False, **kwargs):
"""
Expand Down Expand Up @@ -2420,6 +2428,9 @@ def create_particles(self, step, intern=True):
xname, yname = self.intern(intern)
return create_meshed_particles(self[xname], self[yname], step)

def empty_dataset(self):
return self.new_like(self, 0)


@njit(cache=True)
def grid_count_(grid, i, j):
Expand Down
3 changes: 0 additions & 3 deletions src/py_eddy_tracker/observations/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,6 @@ def extract_with_length(self, bounds):
raise Exception("One bound must be positive")
return self.extract_with_mask(track_mask.repeat(self.nb_obs_by_track))

def empty_dataset(self):
return self.new_like(self, 0)

def loess_filter(self, half_window, xfield, yfield, inplace=True):
track = self.track
x = self.obs[xfield]
Expand Down

0 comments on commit 9d408e5

Please sign in to comment.