Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
manage time in network
  • Loading branch information
AntSimi committed Jun 22, 2021
commit 700f91c57bef429f0af99a8eec448b3e8a7c48cf
4 changes: 2 additions & 2 deletions examples/16_network/pet_follow_particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ def update(frame):
shape = (n.obs.size, 2)
# Forward run
i_target_f, pct_target_f = -ones(shape, dtype="i4"), zeros(shape, dtype="i1")
for t in range(t_start, t_end - dt):
for t in arange(t_start, t_end - dt):
particle_candidate(c, n, step, t, i_target_f, pct_target_f, n_days=dt)

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

# %%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
},
"outputs": [],
"source": [
"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)"
"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)"
]
},
{
Expand Down
14 changes: 7 additions & 7 deletions notebooks/python_module/16_network/pet_segmentation_anim.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Network segmentation process\n"
"\nNetwork segmentation process\n============================\n"
]
},
{
Expand Down Expand Up @@ -62,7 +62,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load data\nLoad data where observations are put in same network but no segmentation\n\n"
"Load data\n---------\nLoad data where observations are put in same network but no segmentation\n\n"
]
},
{
Expand All @@ -80,7 +80,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Do segmentation\nSegmentation based on maximum overlap, temporal window for candidates = 5 days\n\n"
"Do segmentation\n---------------\nSegmentation based on maximum overlap, temporal window for candidates = 5 days\n\n"
]
},
{
Expand All @@ -98,7 +98,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Anim\n\n"
"Anim\n----\n\n"
]
},
{
Expand All @@ -109,14 +109,14 @@
},
"outputs": [],
"source": [
"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)"
"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)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Final Result\n\n"
"Final Result\n------------\n\n"
]
},
{
Expand Down Expand Up @@ -147,7 +147,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.7.7"
}
},
"nbformat": 4,
Expand Down
18 changes: 12 additions & 6 deletions src/py_eddy_tracker/observations/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
degrees,
empty,
histogram,
int_,
median,
nan,
ones,
Expand Down Expand Up @@ -601,6 +602,12 @@ def plot(self, ax, ref=None, **kwargs):

def split_network(self, intern=True, **kwargs):
"""Return each group (network) divided in segments"""
# Find timestep of dataset
# FIXME : how to know exact time sampling
t = unique(self.time)
dts = t[1:] - t[:-1]
timestep = median(dts)

track_s, track_e, track_ref = build_index(self.tracks)
ids = empty(
len(self),
Expand All @@ -614,7 +621,7 @@ def split_network(self, intern=True, **kwargs):
("next_obs", "i4"),
],
)
ids["group"], ids["time"] = self.tracks, self.time
ids["group"], ids["time"] = self.tracks, int_(self.time / timestep)
# Initialisation
# To store the id of the segments, the backward and forward cost associations
ids["track"], ids["previous_cost"], ids["next_cost"] = 0, 0, 0
Expand All @@ -641,6 +648,7 @@ def split_network(self, intern=True, **kwargs):
local_ids["next_obs"][m] += i_s
if display_iteration:
print()
ids["time"] *= timestep
return ids

def set_tracks(self, x, y, ids, window, **kwargs):
Expand All @@ -652,8 +660,7 @@ def set_tracks(self, x, y, ids, window, **kwargs):
:param ndarray ids: several fields like time, group, ...
:param int windows: number of days where observations could missed
"""

time_index = build_index(ids["time"])
time_index = build_index((ids["time"]).astype("i4"))
nb = x.shape[0]
used = zeros(nb, dtype="bool")
track_id = 1
Expand Down Expand Up @@ -698,8 +705,7 @@ def get_previous_obs(
i_current, ids, x, y, time_s, time_e, time_ref, window, **kwargs
):
"""Backward association of observations to the segments"""

time_cur = ids["time"][i_current]
time_cur = int_(ids["time"][i_current])
t0, t1 = time_cur - 1 - time_ref, max(time_cur - window - time_ref, 0)
for t_step in range(t0, t1 - 1, -1):
i0, i1 = time_s[t_step], time_e[t_step]
Expand Down Expand Up @@ -729,7 +735,7 @@ def get_previous_obs(
def get_next_obs(i_current, ids, x, y, time_s, time_e, time_ref, window, **kwargs):
"""Forward association of observations to the segments"""
time_max = time_e.shape[0] - 1
time_cur = ids["time"][i_current]
time_cur = int_(ids["time"][i_current])
t0, t1 = time_cur + 1 - time_ref, min(time_cur + window - time_ref, time_max)
if t0 > time_max:
return -1
Expand Down