Skip to content

Commit

Permalink
ensure we handle empty data
Browse files Browse the repository at this point in the history
  • Loading branch information
atmorling committed Dec 17, 2024
1 parent 2ca3a4a commit c18bee9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
58 changes: 34 additions & 24 deletions ecoscope/io/earthranger.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ def get_subjectgroup_observations(
else:
subjects = self.get_subjects(subject_group_name=subject_group_name, include_inactive=include_inactive)

if subjects.empty:
return subjects

return self.get_subject_observations(subjects, **kwargs)

def get_event_types(self, include_inactive=False, **addl_kwargs):
Expand Down Expand Up @@ -641,7 +644,9 @@ def get_events(

def get_patrol_types(self):
df = pd.DataFrame(self._get("activity/patrols/types"))
return df.set_index("id")
if not df.empty:
df = df.set_index("id")
return df

def get_patrols(self, since=None, until=None, patrol_type=None, patrol_type_value=None, status=None, **addl_kwargs):
"""
Expand Down Expand Up @@ -872,38 +877,43 @@ def get_patrol_observations(self, patrols_df, include_patrol_details=False, **kw
until=patrol_end_time,
**kwargs,
)
if include_patrol_details:
observation["patrol_id"] = patrol["id"]
observation["patrol_title"] = patrol["title"]
observation["patrol_serial_number"] = patrol["serial_number"]
observation["patrol_start_time"] = patrol_start_time
observation["patrol_end_time"] = patrol_end_time
observation["patrol_type"] = patrol_type
observation = (
observation.reset_index()
.merge(
pd.DataFrame(df_pt).add_prefix("patrol_type__"),
left_on="patrol_type",
right_on="id",
)
.drop(
columns=[
"patrol_type__ordernum",
"patrol_type__icon_id",
"patrol_type__default_priority",
"patrol_type__is_active",
]
)
)
if len(observation) > 0:
observation["groupby_col"] = patrol["id"]

if include_patrol_details:
observation["patrol_id"] = patrol["id"]
observation["patrol_title"] = patrol["title"]
observation["patrol_serial_number"] = patrol["serial_number"]
observation["patrol_start_time"] = patrol_start_time
observation["patrol_end_time"] = patrol_end_time
observation["patrol_type"] = patrol_type
observation = (
observation.reset_index()
.merge(
pd.DataFrame(df_pt).add_prefix("patrol_type__"),
left_on="patrol_type",
right_on="id",
)
.drop(
columns=[
"patrol_type__ordernum",
"patrol_type__icon_id",
"patrol_type__default_priority",
"patrol_type__is_active",
]
)
)

observations.append(observation)
except Exception as e:
print(
f"Getting observations for subject_id={subject_id} start_time={patrol_start_time}"
f"end_time={patrol_end_time} failed for: {e}"
)

if not observations:
return pd.DataFrame()

df = pd.concat(observations)
df = clean_time_cols(df)
df = ecoscope.base.Relocations(df)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_earthranger_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ecoscope
from erclient import ERClientException

from ecoscope.io.earthranger import EarthRangerIO
from ecoscope.io.earthranger_utils import TIME_COLS

pytestmark = pytest.mark.io
Expand Down Expand Up @@ -427,3 +428,20 @@ def test_get_patrol_events_bad_geojson(get_objects_mock, sample_bad_patrol_event
assert not patrol_events.empty
# We're rejecting any geojson that's missing geometry or a timestamp
assert patrol_events.id.to_list() == ["ebf812f5-e616-40e4-8fcf-ebb3ef6a6364"]


@pytest.mark.parametrize(
"er_callable, er_kwargs",
[
(EarthRangerIO.get_patrols, {}),
(EarthRangerIO.get_subjectgroup_observations, {"subject_group_id": "12345"}),
(EarthRangerIO.get_patrol_observations_with_patrol_filter, {}),
(EarthRangerIO.get_patrol_events, {}),
(EarthRangerIO.get_events, {}),
],
)
@patch("erclient.client.ERClient._get")
def test_empty_responses(_get_mock, er_io, er_callable, er_kwargs):
_get_mock.return_value = {}
df = er_callable(er_io, **er_kwargs)
assert df.empty

0 comments on commit c18bee9

Please sign in to comment.