Skip to content

Commit 700f91c

Browse files
committed
manage time in network
1 parent 7e72eff commit 700f91c

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

examples/16_network/pet_follow_particle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ def update(frame):
133133
shape = (n.obs.size, 2)
134134
# Forward run
135135
i_target_f, pct_target_f = -ones(shape, dtype="i4"), zeros(shape, dtype="i1")
136-
for t in range(t_start, t_end - dt):
136+
for t in arange(t_start, t_end - dt):
137137
particle_candidate(c, n, step, t, i_target_f, pct_target_f, n_days=dt)
138138

139139
# Backward run
140140
i_target_b, pct_target_b = -ones(shape, dtype="i4"), zeros(shape, dtype="i1")
141-
for t in range(t_start + dt, t_end):
141+
for t in arange(t_start + dt, t_end):
142142
particle_candidate(c, n, step, t, i_target_b, pct_target_b, n_days=-dt)
143143

144144
# %%

notebooks/python_module/16_network/pet_follow_particle.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
},
121121
"outputs": [],
122122
"source": [
123-
"step = 1 / 60.0\n\nx, y = meshgrid(arange(24, 36, step), arange(31, 36, step))\nx0, y0 = x.reshape(-1), y.reshape(-1)\n# Pre-order to speed up\n_, i = group_obs(x0, y0, 1, 360)\nx0, y0 = x0[i], y0[i]\n\nt_start, t_end = n.period\ndt = 14\n\nshape = (n.obs.size, 2)\n# Forward run\ni_target_f, pct_target_f = -ones(shape, dtype=\"i4\"), zeros(shape, dtype=\"i1\")\nfor t in range(t_start, t_end - dt):\n particle_candidate(x0, y0, c, n, t, i_target_f, pct_target_f, n_days=dt)\n\n# Backward run\ni_target_b, pct_target_b = -ones(shape, dtype=\"i4\"), zeros(shape, dtype=\"i1\")\nfor t in range(t_start + dt, t_end):\n particle_candidate(x0, y0, c, n, t, i_target_b, pct_target_b, n_days=-dt)"
123+
"step = 1 / 60.0\n\nx, y = meshgrid(arange(24, 36, step), arange(31, 36, step))\nx0, y0 = x.reshape(-1), y.reshape(-1)\n# Pre-order to speed up\n_, i = group_obs(x0, y0, 1, 360)\nx0, y0 = x0[i], y0[i]\n\nt_start, t_end = n.period\ndt = 14\n\nshape = (n.obs.size, 2)\n# Forward run\ni_target_f, pct_target_f = -ones(shape, dtype=\"i4\"), zeros(shape, dtype=\"i1\")\nfor t in arange(t_start, t_end - dt):\n particle_candidate(x0, y0, c, n, t, i_target_f, pct_target_f, n_days=dt)\n\n# Backward run\ni_target_b, pct_target_b = -ones(shape, dtype=\"i4\"), zeros(shape, dtype=\"i1\")\nfor t in arange(t_start + dt, t_end):\n particle_candidate(x0, y0, c, n, t, i_target_b, pct_target_b, n_days=-dt)"
124124
]
125125
},
126126
{

notebooks/python_module/16_network/pet_segmentation_anim.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"cell_type": "markdown",
1616
"metadata": {},
1717
"source": [
18-
"\n# Network segmentation process\n"
18+
"\nNetwork segmentation process\n============================\n"
1919
]
2020
},
2121
{
@@ -62,7 +62,7 @@
6262
"cell_type": "markdown",
6363
"metadata": {},
6464
"source": [
65-
"## Load data\nLoad data where observations are put in same network but no segmentation\n\n"
65+
"Load data\n---------\nLoad data where observations are put in same network but no segmentation\n\n"
6666
]
6767
},
6868
{
@@ -80,7 +80,7 @@
8080
"cell_type": "markdown",
8181
"metadata": {},
8282
"source": [
83-
"## Do segmentation\nSegmentation based on maximum overlap, temporal window for candidates = 5 days\n\n"
83+
"Do segmentation\n---------------\nSegmentation based on maximum overlap, temporal window for candidates = 5 days\n\n"
8484
]
8585
},
8686
{
@@ -98,7 +98,7 @@
9898
"cell_type": "markdown",
9999
"metadata": {},
100100
"source": [
101-
"## Anim\n\n"
101+
"Anim\n----\n\n"
102102
]
103103
},
104104
{
@@ -109,14 +109,14 @@
109109
},
110110
"outputs": [],
111111
"source": [
112-
"def update(i_frame):\n tr = TRACKS[i_frame]\n mappable_tracks.set_array(tr)\n s = 40 * ones(tr.shape)\n s[tr == 0] = 4\n mappable_tracks.set_sizes(s)\n\n indices_frames = INDICES[i_frame]\n mappable_CONTOUR.set_data(\n e.contour_lon_e[indices_frames],\n e.contour_lat_e[indices_frames],\n )\n mappable_CONTOUR.set_color(cmap.colors[tr[indices_frames] % len(cmap.colors)])\n return (mappable_tracks,)\n\n\nfig = plt.figure(figsize=(16, 9), dpi=60)\nax = fig.add_axes([0.04, 0.06, 0.94, 0.88], projection=GUI_AXES)\nax.set_title(f\"{len(e)} observations to segment\")\nax.set_xlim(19, 29), ax.set_ylim(31, 35.5), ax.grid()\nvmax = TRACKS[-1].max()\ncmap = ListedColormap([\"gray\", *e.COLORS[:-1]], name=\"from_list\", N=vmax)\nmappable_tracks = ax.scatter(\n e.lon, e.lat, c=TRACKS[0], cmap=cmap, vmin=0, vmax=vmax, s=20\n)\nmappable_CONTOUR = ax.plot(\n e.contour_lon_e[INDICES[0]], e.contour_lat_e[INDICES[0]], color=cmap.colors[0]\n)[0]\nani = VideoAnimation(fig, update, frames=range(1, len(TRACKS), 4), interval=125)"
112+
"def update(i_frame):\n tr = TRACKS[i_frame]\n mappable_tracks.set_array(tr)\n s = 40 * ones(tr.shape)\n s[tr == 0] = 4\n mappable_tracks.set_sizes(s)\n\n indices_frames = INDICES[i_frame]\n mappable_CONTOUR.set_data(\n e.contour_lon_e[indices_frames], e.contour_lat_e[indices_frames],\n )\n mappable_CONTOUR.set_color(cmap.colors[tr[indices_frames] % len(cmap.colors)])\n return (mappable_tracks,)\n\n\nfig = plt.figure(figsize=(16, 9), dpi=60)\nax = fig.add_axes([0.04, 0.06, 0.94, 0.88], projection=GUI_AXES)\nax.set_title(f\"{len(e)} observations to segment\")\nax.set_xlim(19, 29), ax.set_ylim(31, 35.5), ax.grid()\nvmax = TRACKS[-1].max()\ncmap = ListedColormap([\"gray\", *e.COLORS[:-1]], name=\"from_list\", N=vmax)\nmappable_tracks = ax.scatter(\n e.lon, e.lat, c=TRACKS[0], cmap=cmap, vmin=0, vmax=vmax, s=20\n)\nmappable_CONTOUR = ax.plot(\n e.contour_lon_e[INDICES[0]], e.contour_lat_e[INDICES[0]], color=cmap.colors[0]\n)[0]\nani = VideoAnimation(fig, update, frames=range(1, len(TRACKS), 4), interval=125)"
113113
]
114114
},
115115
{
116116
"cell_type": "markdown",
117117
"metadata": {},
118118
"source": [
119-
"## Final Result\n\n"
119+
"Final Result\n------------\n\n"
120120
]
121121
},
122122
{
@@ -147,7 +147,7 @@
147147
"name": "python",
148148
"nbconvert_exporter": "python",
149149
"pygments_lexer": "ipython3",
150-
"version": "3.7.9"
150+
"version": "3.7.7"
151151
}
152152
},
153153
"nbformat": 4,

src/py_eddy_tracker/observations/tracking.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
degrees,
1717
empty,
1818
histogram,
19+
int_,
1920
median,
2021
nan,
2122
ones,
@@ -601,6 +602,12 @@ def plot(self, ax, ref=None, **kwargs):
601602

602603
def split_network(self, intern=True, **kwargs):
603604
"""Return each group (network) divided in segments"""
605+
# Find timestep of dataset
606+
# FIXME : how to know exact time sampling
607+
t = unique(self.time)
608+
dts = t[1:] - t[:-1]
609+
timestep = median(dts)
610+
604611
track_s, track_e, track_ref = build_index(self.tracks)
605612
ids = empty(
606613
len(self),
@@ -614,7 +621,7 @@ def split_network(self, intern=True, **kwargs):
614621
("next_obs", "i4"),
615622
],
616623
)
617-
ids["group"], ids["time"] = self.tracks, self.time
624+
ids["group"], ids["time"] = self.tracks, int_(self.time / timestep)
618625
# Initialisation
619626
# To store the id of the segments, the backward and forward cost associations
620627
ids["track"], ids["previous_cost"], ids["next_cost"] = 0, 0, 0
@@ -641,6 +648,7 @@ def split_network(self, intern=True, **kwargs):
641648
local_ids["next_obs"][m] += i_s
642649
if display_iteration:
643650
print()
651+
ids["time"] *= timestep
644652
return ids
645653

646654
def set_tracks(self, x, y, ids, window, **kwargs):
@@ -652,8 +660,7 @@ def set_tracks(self, x, y, ids, window, **kwargs):
652660
:param ndarray ids: several fields like time, group, ...
653661
:param int windows: number of days where observations could missed
654662
"""
655-
656-
time_index = build_index(ids["time"])
663+
time_index = build_index((ids["time"]).astype("i4"))
657664
nb = x.shape[0]
658665
used = zeros(nb, dtype="bool")
659666
track_id = 1
@@ -698,8 +705,7 @@ def get_previous_obs(
698705
i_current, ids, x, y, time_s, time_e, time_ref, window, **kwargs
699706
):
700707
"""Backward association of observations to the segments"""
701-
702-
time_cur = ids["time"][i_current]
708+
time_cur = int_(ids["time"][i_current])
703709
t0, t1 = time_cur - 1 - time_ref, max(time_cur - window - time_ref, 0)
704710
for t_step in range(t0, t1 - 1, -1):
705711
i0, i1 = time_s[t_step], time_e[t_step]
@@ -729,7 +735,7 @@ def get_previous_obs(
729735
def get_next_obs(i_current, ids, x, y, time_s, time_e, time_ref, window, **kwargs):
730736
"""Forward association of observations to the segments"""
731737
time_max = time_e.shape[0] - 1
732-
time_cur = ids["time"][i_current]
738+
time_cur = int_(ids["time"][i_current])
733739
t0, t1 = time_cur + 1 - time_ref, min(time_cur + window - time_ref, time_max)
734740
if t0 > time_max:
735741
return -1

0 commit comments

Comments
 (0)