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

remove write requests from ER notebook #346

Merged
merged 1 commit into from
Nov 29, 2024
Merged
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
307 changes: 0 additions & 307 deletions doc/source/notebooks/01. IO/EarthRanger_IO.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@
"import os\n",
"import sys\n",
"\n",
"import geopandas as gpd\n",
"import pandas as pd\n",
"from erclient.client import ERClientException\n",
"from shapely.geometry import Point\n",
"\n",
"import ecoscope\n",
"\n",
Expand Down Expand Up @@ -210,181 +207,6 @@
"events_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## POST `Event`\n",
"\n",
"First delete any existing events:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_event_types = pd.DataFrame(er_io.get_event_types())\n",
"carcass_id = df_event_types[df_event_types[\"value\"] == \"carcass_rep\"][\"id\"].values[0]\n",
"carcass_id"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" event_df = er_io.get_events(\n",
" event_type=[carcass_id], # Carcass\n",
" ).reset_index()\n",
" for i, value in event_df.iterrows():\n",
" er_io.delete_event(event_id=event_df.loc[i, \"id\"])\n",
"except AssertionError:\n",
" print(\"No events exist yet\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define events to be uploaded:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"new_event = [\n",
" {\n",
" \"event_details\": {\n",
" \"carcassrep_ageofanimal\": None,\n",
" \"carcassrep_ageofcarcass\": None,\n",
" \"carcassrep_causeofdeath\": None,\n",
" \"carcassrep_sex\": None,\n",
" \"carcassrep_species\": None,\n",
" \"carcassrep_trophystatus\": None,\n",
" },\n",
" \"event_type\": \"carcass_rep\",\n",
" \"icon_id\": \"carcass_rep\",\n",
" \"id\": \"e29f9078-ee0a-4f06-b685-92e9ff266e9b\",\n",
" \"location\": {\"latitude\": -27.12759, \"longitude\": -109.40804},\n",
" \"priority\": 100,\n",
" \"state\": \"resolved\",\n",
" \"time\": pd.Timestamp(\"2022-03-15 15:44:00-0700\"),\n",
" \"title\": \"Carcass\",\n",
" }\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read events as dataframe:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"new_event_df = pd.DataFrame.from_dict(new_event)\n",
"new_event_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Upload:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" er_io.post_event(events=new_event_df.iloc[[0]])\n",
"except ERClientException as e:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## PATCH `Event`\n",
"\n",
"Define updated values"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"updated_event = pd.DataFrame(\n",
" [\n",
" {\n",
" \"event_details\": {\n",
" \"carcassrep_ageofanimal\": \"adult\",\n",
" \"carcassrep_ageofcarcass\": \"less_than_a_day\",\n",
" \"carcassrep_causeofdeath\": \"unnaturalshot\",\n",
" \"carcassrep_sex\": \"female\",\n",
" \"carcassrep_species\": \"elephant\",\n",
" \"carcassrep_trophystatus\": \"removed\",\n",
" }\n",
" }\n",
" ]\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get and display current values"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"event_df = er_io.get_events(\n",
" event_type=[\"b0d66402-34cb-4e9a-9715-2ac4eeebb574\"], # Carcass\n",
").reset_index()\n",
"\n",
"event_df.loc[0, \"event_details\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Patch and confirm values have been updated"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"id_to_patch = event_df.loc[0, \"id\"]\n",
"er_io.patch_event(event_id=id_to_patch, events=updated_event).loc[0, \"event_details\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -688,135 +510,6 @@
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## POST `Observation`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First delete any existing observations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"elephants = er_io.get_subjectgroup_observations(\n",
" subject_group_name=\"Elephants\", since=pd.Timestamp(\"2022-01-01\").isoformat()\n",
")\n",
"\n",
"if not elephants.empty:\n",
" for observation_id in elephants[\"extra__id\"].unique():\n",
" er_io.delete_observation(observation_id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create new observations from existing sources"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_sources = er_io.get_sources()\n",
"habiba_source_id = df_sources[df_sources[\"manufacturer_id\"] == \"ST-Habiba\"][\"id\"].values[0]\n",
"salif_keita_source_id = df_sources[df_sources[\"manufacturer_id\"] == \"ST-SalifKeita\"][\"id\"].values[0]\n",
"habiba_source_id, salif_keita_source_id"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"observations = [\n",
" {\n",
" \"fixtime\": pd.Timestamp(\"2023-04-24 11:03:31\").isoformat(),\n",
" \"geometry\": Point(1.99, 2.99),\n",
" \"source_id\": habiba_source_id, # Habiba's source\n",
" },\n",
" {\n",
" \"fixtime\": pd.Timestamp(\"2023-02-01 14:02:05\").isoformat(),\n",
" \"geometry\": Point(1.345, 2.3303),\n",
" \"source_id\": habiba_source_id, # Habiba's source\n",
" },\n",
" {\n",
" \"fixtime\": pd.Timestamp(\"2023-03-21 06:03:27\").isoformat(),\n",
" \"geometry\": Point(3.4455, -3.4554),\n",
" \"source_id\": salif_keita_source_id, # Salif Keita's source\n",
" },\n",
"]\n",
"\n",
"gdf = gpd.GeoDataFrame.from_dict(observations)\n",
"gdf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Post observations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"er_io.post_observations(observations=gdf, source_id_col=\"source_id\", recorded_at_col=\"fixtime\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## POST `SubjectSource`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_subjects = er_io.get_subjects()\n",
"salif_keita_id = df_subjects[df_subjects[\"name\"] == \"Salif Keita\"][\"id\"].values[0]\n",
"\n",
"df_sources = er_io.get_sources()\n",
"salif_keita_source_id = df_sources[df_sources[\"manufacturer_id\"] == \"ST-SalifKeita\"][\"id\"].values[0]\n",
"\n",
"salif_keita_id, salif_keita_source_id"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"er_io.post_subjectsource(\n",
" salif_keita_id, # Salif Keita subject ID\n",
" source_id=salif_keita_source_id, # Salif Keita's source\n",
" lower_bound_assigned_range=pd.Timestamp.utcnow().isoformat(),\n",
" upper_bound_assigned_range=(pd.Timestamp.utcnow() + pd.Timedelta(days=9000)).isoformat(),\n",
" additional={},\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading