|
122 | 122 | "execution_count": null, |
123 | 123 | "metadata": {}, |
124 | 124 | "outputs": [], |
125 | | - "source": [] |
| 125 | + "source": [ |
| 126 | + "#tc = mpd.TrajectoryCollection(df, traj_id_col='AgentID', t='t', x='x', y='y', crs=31287)\n", |
| 127 | + "tc" |
| 128 | + ] |
126 | 129 | }, |
127 | 130 | { |
128 | 131 | "cell_type": "code", |
|
171 | 174 | "metadata": {}, |
172 | 175 | "outputs": [], |
173 | 176 | "source": [ |
174 | | - "tc.add_speed(overwrite=True)\n", |
| 177 | + "import os\n", |
| 178 | + "\n", |
| 179 | + "tc.add_speed(overwrite=True, n_threads=os.cpu_count())\n", |
175 | 180 | "tc.trajectories[0].df#.hvplot(c='speed')" |
176 | 181 | ] |
177 | 182 | }, |
|
216 | 221 | "metadata": {}, |
217 | 222 | "outputs": [], |
218 | 223 | "source": [ |
219 | | - "sim_out_file_name = 'sim_20250517_100643.csv'\n", |
| 224 | + "sim_out_file_name = 'sim_20250517_164810.csv'\n", |
220 | 225 | "PERCENTAGE_OF_ANOMALIES = 0.03" |
221 | 226 | ] |
222 | 227 | }, |
|
236 | 241 | "metadata": {}, |
237 | 242 | "outputs": [], |
238 | 243 | "source": [ |
239 | | - "unique_ids = sims.AgentID.unique()\n", |
240 | | - "unique_ids" |
| 244 | + "#sims = sims[sims.AgentID<29].copy()\n", |
| 245 | + "sims['is_anomaly'] = False\n", |
| 246 | + "sims['anomaly_type'] = 'n/a'\n" |
241 | 247 | ] |
242 | 248 | }, |
243 | 249 | { |
|
246 | 252 | "metadata": {}, |
247 | 253 | "outputs": [], |
248 | 254 | "source": [ |
249 | | - "sample_ids_pd = pd.Series(unique_ids).sample(frac=PERCENTAGE_OF_ANOMALIES).tolist()\n", |
250 | | - "print(\"Subset we'll insert anomalies in: \", sample_ids_pd)" |
| 255 | + "tc = mpd.TrajectoryCollection(sims, t='t', traj_id_col='AgentID', x='x', y='y', crs=31287)\n", |
| 256 | + "tc" |
251 | 257 | ] |
252 | 258 | }, |
253 | 259 | { |
|
256 | 262 | "metadata": {}, |
257 | 263 | "outputs": [], |
258 | 264 | "source": [ |
259 | | - "#sims = sims[sims.AgentID<29].copy()\n", |
260 | | - "sims['is_anomaly'] = False\n", |
261 | | - "sims['anomaly_type'] = 'n/a'\n", |
262 | | - "tc = mpd.TrajectoryCollection(sims, t='t', traj_id_col='AgentID', x='x', y='y', crs=31287)\n", |
| 265 | + "# Remove stopped segments (with speed < 0.0001)\n", |
| 266 | + "tc = mpd.SpeedSplitter(tc).split(speed=0.0001, duration=timedelta(seconds=5))\n", |
263 | 267 | "tc" |
264 | 268 | ] |
265 | 269 | }, |
266 | 270 | { |
267 | | - "cell_type": "code", |
268 | | - "execution_count": null, |
| 271 | + "cell_type": "markdown", |
269 | 272 | "metadata": {}, |
270 | | - "outputs": [], |
271 | 273 | "source": [ |
272 | | - "tc.add_speed(overwrite=True)\n", |
273 | | - "tc.plot(column='speed', legend=True)" |
| 274 | + "### Speed\n", |
| 275 | + "\n", |
| 276 | + "Change the timestamps to make a subsection of the trajectory faster" |
274 | 277 | ] |
275 | 278 | }, |
276 | 279 | { |
|
279 | 282 | "metadata": {}, |
280 | 283 | "outputs": [], |
281 | 284 | "source": [ |
282 | | - "# Remove stopped segments (with speed < 0.01)\n", |
283 | | - "tc = mpd.SpeedSplitter(tc).split(speed=0.01, duration=timedelta(seconds=5))" |
| 285 | + "unique_ids = sims.AgentID.unique()\n", |
| 286 | + "unique_ids" |
284 | 287 | ] |
285 | 288 | }, |
286 | 289 | { |
287 | | - "cell_type": "markdown", |
| 290 | + "cell_type": "code", |
| 291 | + "execution_count": null, |
288 | 292 | "metadata": {}, |
| 293 | + "outputs": [], |
289 | 294 | "source": [ |
290 | | - "### Speed\n", |
291 | | - "\n", |
292 | | - "Change the timestamps to make a subsection of the trajectory faster" |
| 295 | + "sample_ids_pd = pd.Series(unique_ids).sample(frac=PERCENTAGE_OF_ANOMALIES).tolist()\n", |
| 296 | + "print(\"Subset we'll insert anomalies in: \", sample_ids_pd)" |
293 | 297 | ] |
294 | 298 | }, |
295 | 299 | { |
|
327 | 331 | "outputs": [], |
328 | 332 | "source": [ |
329 | 333 | "for i, traj in enumerate(tc.trajectories):\n", |
330 | | - " traj = inject_speed_anomaly(traj)\n", |
331 | | - " tc.trajectories[i] = traj\n", |
| 334 | + " if i in sample_ids_pd: \n", |
| 335 | + " traj = inject_speed_anomaly(traj)\n", |
| 336 | + " tc.trajectories[i] = traj\n", |
332 | 337 | " \n", |
333 | 338 | "tc.add_speed(overwrite=True)\n", |
334 | 339 | "tc.plot(column='speed', legend=True)" |
|
376 | 381 | "source": [ |
377 | 382 | "### Location\n", |
378 | 383 | "\n", |
379 | | - "#### Generate trajectories that pass through obstacles" |
| 384 | + "#### Generate trajectories that pass through obstacles\n", |
| 385 | + "\n", |
| 386 | + "Note that is not guaranteed that each resulting trajectory will pass through an obtacle! Ships simply ignore the obstacles." |
380 | 387 | ] |
381 | 388 | }, |
382 | 389 | { |
|
408 | 415 | "model = ShipModel(\n", |
409 | 416 | " width=config[\"width\"],\n", |
410 | 417 | " height=config[\"height\"],\n", |
411 | | - " num_ships=config[\"num_ships\"]*PERCENTAGE_OF_ANOMALIES, \n", |
| 418 | + " num_ships=int(config[\"num_ships\"]*2*PERCENTAGE_OF_ANOMALIES), # doubling the number, assuming that 50% will violate obstacles\n", |
412 | 419 | " max_speed_range=config[\"max_speed_range\"],\n", |
413 | 420 | " ports=config[\"ports\"],\n", |
414 | 421 | " speed_limit_zones=config.get(\"speed_limit_zones\", []),\n", |
|
607 | 614 | { |
608 | 615 | "cell_type": "markdown", |
609 | 616 | "metadata": {}, |
| 617 | + "source": [ |
| 618 | + "## Merge files" |
| 619 | + ] |
| 620 | + }, |
| 621 | + { |
| 622 | + "cell_type": "markdown", |
| 623 | + "metadata": {}, |
| 624 | + "source": [] |
| 625 | + }, |
| 626 | + { |
| 627 | + "cell_type": "code", |
| 628 | + "execution_count": null, |
| 629 | + "metadata": {}, |
| 630 | + "outputs": [], |
| 631 | + "source": [ |
| 632 | + "df1 = pd.read_csv(\"anomalies_speed_20250517_172830.csv\")\n", |
| 633 | + "df2 = pd.read_csv(\"anomalies_loc_20250517_184842.csv\")\n", |
| 634 | + "out = pd.concat([df1,df2])[['t','AgentID','speed','is_anomaly','anomaly_type','x','y']]\n", |
| 635 | + "out.to_csv(\"synthetic_vessel_tracks_with_anomalies_20250517.csv\", index=False)" |
| 636 | + ] |
| 637 | + }, |
| 638 | + { |
| 639 | + "cell_type": "code", |
| 640 | + "execution_count": null, |
| 641 | + "metadata": {}, |
| 642 | + "outputs": [], |
| 643 | + "source": [ |
| 644 | + "tmp = pd.read_csv(\"synthetic_vessel_tracks_with_anomalies_20250517.csv\")\n", |
| 645 | + "tmp" |
| 646 | + ] |
| 647 | + }, |
| 648 | + { |
| 649 | + "cell_type": "code", |
| 650 | + "execution_count": null, |
| 651 | + "metadata": {}, |
| 652 | + "outputs": [], |
| 653 | + "source": [ |
| 654 | + "tmp[tmp.anomaly_type==\"location\"]" |
| 655 | + ] |
| 656 | + }, |
| 657 | + { |
| 658 | + "cell_type": "code", |
| 659 | + "execution_count": null, |
| 660 | + "metadata": {}, |
| 661 | + "outputs": [], |
| 662 | + "source": [ |
| 663 | + "tmp[tmp.anomaly_type==\"location\"].AgentID.unique()" |
| 664 | + ] |
| 665 | + }, |
| 666 | + { |
| 667 | + "cell_type": "code", |
| 668 | + "execution_count": null, |
| 669 | + "metadata": {}, |
| 670 | + "outputs": [], |
610 | 671 | "source": [] |
611 | 672 | } |
612 | 673 | ], |
|
0 commit comments