Skip to content

Commit

Permalink
Fix monotic time parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jmorgadov committed Dec 3, 2022
1 parent 215bf78 commit bb75365
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
15 changes: 13 additions & 2 deletions recipes/animals.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,27 @@ def _get_datetime(date_str: str, time_str: str) -> datetime:


def _process_animal(animal_rows) -> Tuple[Trajectory, str]:
for row in animal_rows:
row["time"] = _get_datetime(row[" LocDate"], row[" LocTime"])

sorted_rows = sorted(animal_rows, key=lambda x: x["time"])

lat, long, time = [], [], []
start_time = None
label = animal_rows[0][" Species"]
for row in animal_rows:
for i, row in enumerate(sorted_rows):
if start_time is None:
start_time = _get_datetime(row[" LocDate"], row[" LocTime"])
time.append(0)
else:
_time = _get_datetime(row[" LocDate"], row[" LocTime"])
time.append((_time - start_time).total_seconds())
new_time = (_time - start_time).total_seconds()
if new_time == time[-1]:
continue
assert (
new_time > time[-1]
), f"Time is not increasing: {new_time} <= {time[-1]}. {i}: {_time}"
time.append(new_time)

assert row[" Species"] == label

Expand Down
36 changes: 22 additions & 14 deletions recipes/uci_gotrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Tuple
from typing import Any, Dict, List, Tuple, Union

from yupi import Trajectory

Expand All @@ -28,15 +28,20 @@ def _fetch_raw_data() -> Path:
return raw_trajs_filepath.parent


def _get_traj(rows: List[dict]) -> Trajectory:
lat = [float(row["latitude"]) for row in rows]
lon = [float(row["longitude"]) for row in rows]
def _get_traj(rows: List[dict]) -> Union[Trajectory, None]:
lat, lon, time = [], [], []
_t0 = datetime.strptime(rows[0]["time"], "%Y-%m-%d %H:%M:%S")
time = [
(datetime.strptime(row["time"], "%Y-%m-%d %H:%M:%S") - _t0).total_seconds()
for row in rows
]
return Trajectory(x=lon, y=lat, t=time)
_last_t = None
for row in rows:
_t = datetime.strptime(row["time"], "%Y-%m-%d %H:%M:%S")
if _last_t is not None and _t <= _last_t:
_last_t = _t
continue
lat.append(float(row["latitude"]))
lon.append(float(row["longitude"]))
time.append((_t - _t0).total_seconds())
_last_t = _t
return Trajectory(x=lon, y=lat, t=time) if len(time) > 3 else None


def _yupify(raw_dir) -> Tuple[List[Trajectory], List[str]]:
Expand Down Expand Up @@ -74,9 +79,12 @@ def _yupify(raw_dir) -> Tuple[List[Trajectory], List[str]]:
_track_id = track_id
_rows.append(row)

# filter out tracks with less than 3 points
trajs_rows = {k: v for k, v in trajs_rows.items() if len(v) > 2}

labels = [labels_dict[_id] for _id in trajs_rows]
trajs = [_get_traj(trajs_rows[_id]) for _id in trajs_rows]
trajs, labels = [], []
for rows, lbl in zip(trajs_rows.values(), labels_dict.values()):
if len(rows) < 3:
continue
traj = _get_traj(rows)
if traj is not None:
trajs.append(traj)
labels.append(lbl)
return trajs, labels

0 comments on commit bb75365

Please sign in to comment.