Skip to content

Commit

Permalink
Single reloc fix (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
atmorling authored Nov 18, 2024
1 parent 7a06d57 commit f937fd1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ecoscope/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ def get_tortuosity(self):

@staticmethod
def _create_multitraj(df):
if len(df) == 1:
warnings.warn(
f"Subject id {df['extra__subject_id']} has only one relocation "
"and will be excluded from trajectory creation"
)
return pd.DataFrame()
with warnings.catch_warnings():
"""
Note : This warning can be removed once the version of Geopandas is updated
Expand Down
4 changes: 2 additions & 2 deletions ecoscope/io/earthranger_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def to_gdf(df):


def clean_time_cols(df):
time_cols = ["time", "created_at", "updated_at", "end_time", "last_position_date", "recorded_at"]
time_cols = ["time", "created_at", "updated_at", "end_time", "last_position_date", "recorded_at", "fixtime"]
for col in time_cols:
if col in df.columns:
if col in df.columns and not pd.api.types.is_datetime64_ns_dtype(df[col]):
# convert x is not None to pd.isna(x) is False
df[col] = df[col].apply(lambda x: pd.to_datetime(parser.parse(x), utc=True) if not pd.isna(x) else None)
return df
Expand Down
Binary file added tests/sample_data/vector/sample_relocs.parquet
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,18 @@ def test_apply_traj_filter(movebank_relocations):

assert filtered["speed_kmhr"].min() >= min_speed
assert filtered["speed_kmhr"].max() <= max_speed


@pytest.fixture
def sample_relocs():
gdf = gpd.read_parquet("tests/sample_data/vector/sample_relocs.parquet")
gdf = ecoscope.io.earthranger_utils.clean_time_cols(gdf)

return ecoscope.base.Relocations.from_gdf(gdf)


def test_trajectory_with_single_relocation(sample_relocs):
assert len(sample_relocs["extra__subject_id"].unique()) == 3
trajectory = ecoscope.base.Trajectory.from_relocations(sample_relocs)
assert not trajectory.empty
assert len(trajectory["extra__subject_id"].unique()) == 2

0 comments on commit f937fd1

Please sign in to comment.