Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include patrol details in get_patrol_observations() #47

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 8 additions & 38 deletions ecoscope/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
__initialized = False


def init(silent=False, selenium=False, force=False):
def init(silent=False, pyppeteer=False, force=False):
"""
Initializes the environment with ecoscope-specific customizations.

Parameters
----------
silent : bool, optional
Removes console output
selenium : bool, optional
Installs selenium webdriver in a colab environment
pyppeteer : bool, optional
Installs Pyppeteer and Chrome in a colab environment
force : bool, optional
Ignores `__initialized`

Expand Down Expand Up @@ -76,46 +76,16 @@ def explore(data, *args, **kwargs):

import sys

if "google.colab" in sys.modules and selenium:
if "google.colab" in sys.modules and pyppeteer:
from IPython import get_ipython

shell_text = """\
cat > /etc/apt/sources.list.d/debian.list <<'EOF'
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-bookworm.gpg] http://deb.debian.org/debian bookworm main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-bookworm-updates.gpg]\
http://deb.debian.org/debian bookworm-updates main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-bookworm.gpg]\
http://deb.debian.org/debian-security bookworm/updates main
EOF

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A

apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-bookworm.gpg
apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-bookworm-updates.gpg
apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-bookworm.gpg

cat > /etc/apt/preferences.d/chromium.pref << 'EOF'
Package: *
Pin: release a=eoan
Pin-Priority: 500

import nest_asyncio

Package: *
Pin: origin "deb.debian.org"
Pin-Priority: 300


Package: chromium*
Pin: origin "deb.debian.org"
Pin-Priority: 700
EOF
nest_asyncio.apply()

shell_text = """\
apt-get update
apt-get install chromium chromium-driver

pip install selenium
apt-get install libxtst6
"""

if silent:
Expand Down
66 changes: 47 additions & 19 deletions ecoscope/io/earthranger.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,8 @@ def get_events(
return df

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

def get_patrols(self, since=None, until=None, patrol_type=None, status=None, **addl_kwargs):
"""
Expand Down Expand Up @@ -677,15 +678,17 @@ def get_patrol_segments(self):
self.get_objects_multithreaded(object=object, threads=self.tcp_limit, page_size=self.sub_page_size)
)

def get_observations_for_patrols(self, patrols_df, **kwargs):
def get_patrol_observations(self, patrols_df, include_patrol_details=False, **kwargs):
"""
Download observations for provided `patrols_df`.
Parameters
----------
patrols_df :
Data returned from a call to `get_patrols`.
patrols_df : pd.DataFrame
Data returned from a call to `get_patrols`.
include_patrol_details : bool, optional
Whether to merge patrol details into dataframe
kwargs
Additional parameters to pass to `get_subject_observations`.
Additional parameters to pass to `get_subject_observations`.
Returns
-------
relocations : ecoscope.base.Relocations
Expand All @@ -695,27 +698,52 @@ def get_observations_for_patrols(self, patrols_df, **kwargs):
for _, patrol in patrols_df.iterrows():
for patrol_segment in patrol["patrol_segments"]:
subject_id = (patrol_segment.get("leader") or {}).get("id")
start_time = (patrol_segment.get("time_range") or {}).get("start_time")
end_time = (patrol_segment.get("time_range") or {}).get("end_time")
patrol_start_time = (patrol_segment.get("time_range") or {}).get("start_time")
patrol_end_time = (patrol_segment.get("time_range") or {}).get("end_time")

if None in {subject_id, start_time}:
df_pt = self.get_patrol_types()
patrol_type = df_pt[df_pt["value"] == patrol_segment.get("patrol_type")].reset_index()["id"][0]

if None in {subject_id, patrol_start_time}:
continue

try:
observations.append(
self.get_subject_observations(
subject_ids=[subject_id],
since=start_time,
until=end_time,
**kwargs,
)
observation = self.get_subject_observations(
subject_ids=[subject_id], since=patrol_start_time, until=patrol_end_time
)
if include_patrol_details:
observation["patrol_id"] = patrol["id"]
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(self.get_patrol_types()).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=} {start_time=} {end_time=} failed for: {e}')
print(
f"Getting observations for subject_id={subject_id} start_time={start_time} end_time={end_time}"
f"failed for: {e}"
f"Getting observations for subject_id={subject_id} start_time={patrol_start_time}"
f"end_time={patrol_end_time} failed for: {e}"
)
return ecoscope.base.Relocations(pd.concat(observations))

df = ecoscope.base.Relocations(pd.concat(observations))
if include_patrol_details:
return df.set_index("id")
return df

def get_patrol_segment_events(
self,
Expand Down
16 changes: 8 additions & 8 deletions ecoscope/mapping/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,20 @@ def to_png(self, outfile, sleep_time=10, **kwargs):
outfile : str, Pathlike
Output destination
sleep_time : int, optional
Additional seconds to wait before taking screenshot. Should be increased if map tiles in the output haven't
fully loaded but can also be decreased in most cases.

Additional seconds to wait before taking screenshot. Should be increased
if map tiles in the output haven't fully loaded but can also be decreased
in most cases.
"""

html_string=self.get_root().render()
html_string = self.get_root().render()

async def capture_screenshot(html_string,outfile):
browser = await launch()
async def capture_screenshot(html_string, outfile):
browser = await launch(options={"args": ["--no-sandbox"]})
page = await browser.newPage()
await page.setViewport({'width': self.px_width, 'height': self.px_height}) # Set the viewport size as needed
await page.setViewport({"width": int(self.px_width), "height": int(self.px_height)})
await page.setContent(html_string)
await asyncio.sleep(sleep_time)
await page.screenshot({'path': outfile})
await page.screenshot({"path": outfile})
await browser.close()

loop = asyncio.get_event_loop()
Expand Down
2 changes: 1 addition & 1 deletion ecoscope/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.4.0"
__version__ = "1.4.2"
6 changes: 3 additions & 3 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: ecoscope
channels:
- conda-forge
dependencies:
- python==3.7.12 # Fixed to mirror version on Google Colab
- python==3.10.12 # Fixed to mirror version on Google Colab
- pip==22.3
- git==2.38.1
- affine==2.3.1
Expand All @@ -11,6 +11,7 @@ dependencies:
- backoff==2.2.1
- branca==0.5.0
- earthengine-api==0.1.328
- earthranger-client
- geopandas==0.10.2
- ipywidgets==8.0.2
- jinja2==3.1.2
Expand All @@ -27,6 +28,7 @@ dependencies:
- pyarrow==9.0.0
- pydata-sphinx-theme==0.11.0
- pypdf2==2.10.8
- pyppeteer
- pyproj==3.2.1
- pytest-cov==4.0.0
- pytest-mock==3.10.0
Expand All @@ -37,12 +39,10 @@ dependencies:
- scikit-image==0.19.2
- scikit-learn==1.0.2
- scipy==1.7.3
- selenium==4.5.0
- shapely==1.8.2
- sphinx==5.3.0
- tqdm==4.64.1
- xyzservices==2022.9.0
- earthranger-client
- pip:
- coverage[toml]
- nbsphinx
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"backoff",
"branca",
"earthengine-api",
"earthranger-client",
"folium>=0.11.0",
"geopandas",
"igraph",
Expand All @@ -31,17 +32,16 @@
"pandas",
"plotly",
"pyarrow",
"pyppeteer",
"pyproj",
"pytest-mock",
"rasterio",
"scikit-image",
"scikit-learn",
"scipy",
"selenium",
"shapely",
"tqdm",
"xyzservices",
"earthranger-client",
]

setup(
Expand Down Expand Up @@ -76,6 +76,7 @@
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
include_package_data=True,
)
Binary file modified tests/test_output/png_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions tests/test_png.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sys
import os
sys.path.append('../ecoscope')
from ecoscope.mapping.map import EcoMap

m=EcoMap(draw_control=False)
sys.path.append("../ecoscope")

relative_path=os.path.join('tests/test_output/png_map.png')
m.to_png(relative_path)

m = EcoMap(draw_control=False)

relative_path = os.path.join("tests/test_output/png_map.png")
m.to_png(relative_path)
Loading