Skip to content

Commit 486e895

Browse files
committed
Add condition to avoid error when dataset is empty
1 parent d71913b commit 486e895

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/py_eddy_tracker/observations/tracking.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ def nb_tracks(self):
9393
Will count and send number of track
9494
"""
9595
if self.__nb_track is None:
96-
self.__nb_track = (self.nb_obs_by_track != 0).sum()
96+
if len(self) == 0:
97+
self.__nb_track = 0
98+
else:
99+
self.__nb_track = (self.nb_obs_by_track != 0).sum()
97100
return self.__nb_track
98101

99102
def __repr__(self):
@@ -326,18 +329,23 @@ def extract_with_length(self, bounds):
326329
327330
.. minigallery:: py_eddy_tracker.TrackEddiesObservations.extract_with_length
328331
"""
332+
if len(self) == 0:
333+
return self.empty_dataset()
329334
b0, b1 = bounds
330-
if b0 >= 0 and b1 >= 0:
335+
if b0 >= 0 and b1 != -1:
331336
track_mask = (self.nb_obs_by_track >= b0) * (self.nb_obs_by_track <= b1)
332-
elif b0 < 0 and b1 >= 0:
337+
elif b0 == -1 and b1 >= 0:
333338
track_mask = self.nb_obs_by_track <= b1
334-
elif b0 >= 0 and b1 < 0:
339+
elif b0 >= 0 and b1 == -1:
335340
track_mask = self.nb_obs_by_track > b0
336341
else:
337342
logger.warning("No valid value for bounds")
338343
raise Exception("One bounds must be positiv")
339344
return self.extract_with_mask(track_mask.repeat(self.nb_obs_by_track))
340345

346+
def empty_dataset(self):
347+
return self.new_like(self, 0)
348+
341349
def loess_filter(self, half_window, xfield, yfield, inplace=True):
342350
track = self.obs["track"]
343351
x = self.obs[xfield]
@@ -427,6 +435,8 @@ def plot(self, ax, ref=None, **kwargs):
427435
"""
428436
if "label" in kwargs:
429437
kwargs["label"] += " (%s eddies)" % (self.nb_obs_by_track != 0).sum()
438+
if len(self) == 0:
439+
return ax.plot([], [], **kwargs)
430440
x, y = split_line(self.longitude, self.latitude, self.tracks)
431441
if ref is not None:
432442
x, y = wrap_longitude(x, y, ref, cut=True)

src/py_eddy_tracker/poly.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def get_wrap_vertice(x0, y0, x1, y1, i):
269269

270270

271271
def vertice_overlap(x0, y0, x1, y1, minimal_area=False):
272-
"""
272+
r"""
273273
Return percent of overlap for each item.
274274
275275
:param array x0: x for polygon list 0
@@ -279,6 +279,14 @@ def vertice_overlap(x0, y0, x1, y1, minimal_area=False):
279279
:param bool minimal_area: If True, function will compute intersection/little polygon, else intersection/union
280280
:return: Result of cost function
281281
:rtype: array
282+
283+
By default
284+
285+
.. math:: Score = \frac{Intersection(P_0,P_1)_{area}}{Union(P_0,P_1)_{area}}
286+
287+
If minimal area:
288+
289+
.. math:: Score = \frac{Intersection(P_0,P_1)_{area}}{min(P_{0 area},P_{1 area})}
282290
"""
283291
nb = x0.shape[0]
284292
cost = empty(nb)

0 commit comments

Comments
 (0)