Skip to content

Commit 53c32cf

Browse files
IngeborgGjerdeIngeborg Gjerde
andauthored
Add/hierarchical models (#3)
* remove example script * update * Bump version * small fixes * allow python 3.11 * remove unused import * remove old token --------- Co-authored-by: Ingeborg Gjerde <ingeborg.gjerde@ngi.no>
1 parent daa99b0 commit 53c32cf

11 files changed

Lines changed: 1992 additions & 377 deletions

File tree

Demo.ipynb

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "4a9e8552",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from field_manager_api.field_manager import FieldManagerAPI"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"id": "fbc48aa3",
16+
"metadata": {},
17+
"source": [
18+
"### Adding your user token\n",
19+
"\n",
20+
"Access to the Field Manager API is controlled using tokens. To retrieve your token, go to [the Field Manager Developer Portal](https://app.test.fieldmanager.io/developer). This page shows the documentation for the API, i.e., all the endpoints and their inputs and outputs.\n",
21+
"\n",
22+
"For now, just click `get_token` in the upper right corner:\n",
23+
"\n",
24+
"<img src=\"get_token.png\" width=\"40\">\n",
25+
"\n",
26+
"Copy the token you get into the string below.\n"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": null,
32+
"id": "03b57afc",
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"token = \"YOUR_TOKEN_HERE\"\n",
37+
"fm = FieldManagerAPI()\n",
38+
"fm.set_token(token)"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"id": "3ebe8b65",
44+
"metadata": {},
45+
"source": [
46+
"The token expires after 30 minutes, at which pointy you should make a new one.\n",
47+
"\n",
48+
"## Importing data\n",
49+
"\n",
50+
"The `Field Manager API` is a python package that simplifies using the API.\n",
51+
"\n",
52+
"If you know the name of the project, you can look up all projects with names containing some substring:\n"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"id": "dc3246df",
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"project_names = fm.search_for_project_name(substring=\"TellefTest\")\n",
63+
"print(project_names)\n",
64+
"project = fm.get_project_by_name(project_names[0])"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"id": "2928f0df",
70+
"metadata": {},
71+
"source": [
72+
"We take the first match.\n",
73+
"\n",
74+
"Data is structured hierarchically within the `project`:\n",
75+
"\n",
76+
"- `project`s have `locations`, i.e., a list of `Location` classes\n",
77+
"- `locations`s have `methods`, i.e., a list of `Method` classes\n",
78+
"- `method`s have `method_data`, which is a pandas dataframe:\n"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"id": "48dc9b1b",
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"df = project.locations[3].methods[0].data\n",
89+
"df"
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"id": "80886a4e",
95+
"metadata": {},
96+
"source": [
97+
"You can easily drop columns you're not interested in\n"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": null,
103+
"id": "980d175f",
104+
"metadata": {},
105+
"outputs": [],
106+
"source": [
107+
"df = project.locations[3].methods[0].data\n",
108+
"df.drop(\n",
109+
" columns=[\"updated_at\", \"created_at\", \"comment_code\", \"remarks\"],\n",
110+
" errors=\"ignore\",\n",
111+
" inplace=True,\n",
112+
")\n",
113+
"df"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": null,
119+
"id": "e94b80bc",
120+
"metadata": {},
121+
"outputs": [],
122+
"source": [
123+
"from field_manager_api.utils.folium_plotter import MapHandler\n",
124+
"\n",
125+
"mh = MapHandler(project.locations, tiles=\"OpenTopoMap\", height=\"300px\", width=\"80%\")\n",
126+
"mh.plot_locations()"
127+
]
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"id": "5f455cb4",
132+
"metadata": {},
133+
"source": [
134+
"If you hover of the locations, a tooltip will display the _location index_ and a list of methods performed at the location. E.g.,If you want to examine the method data associated with e.g. the 0th method on the 2nd location, write\n"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": null,
140+
"id": "c426f7aa",
141+
"metadata": {},
142+
"outputs": [],
143+
"source": [
144+
"df_full = project.locations[2].methods[0].data\n",
145+
"df_full.head()"
146+
]
147+
},
148+
{
149+
"cell_type": "markdown",
150+
"id": "b716e9f2",
151+
"metadata": {},
152+
"source": [
153+
"You can easily drop columns you're not interested in,\n"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": null,
159+
"id": "8ce891b1",
160+
"metadata": {},
161+
"outputs": [],
162+
"source": [
163+
"df = df_full.drop(\n",
164+
" columns=[\"updated_at\", \"remarks\", \"comment_code\", \"conductivity\", \"created_at\"]\n",
165+
")\n",
166+
"df.head()"
167+
]
168+
},
169+
{
170+
"cell_type": "markdown",
171+
"id": "2d79e6a0",
172+
"metadata": {},
173+
"source": [
174+
"or only grab the data you want\n"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"id": "0cc17db8",
181+
"metadata": {},
182+
"outputs": [],
183+
"source": [
184+
"df = df_full[[\"depth\", \"fs\", \"qc\", \"u2\"]]"
185+
]
186+
}
187+
],
188+
"metadata": {
189+
"kernelspec": {
190+
"display_name": ".venv",
191+
"language": "python",
192+
"name": "python3"
193+
},
194+
"language_info": {
195+
"codemirror_mode": {
196+
"name": "ipython",
197+
"version": 3
198+
},
199+
"file_extension": ".py",
200+
"mimetype": "text/x-python",
201+
"name": "python",
202+
"nbconvert_exporter": "python",
203+
"pygments_lexer": "ipython3",
204+
"version": "3.12.0"
205+
}
206+
},
207+
"nbformat": 4,
208+
"nbformat_minor": 5
209+
}

get_token.png

91.6 KB
Loading

0 commit comments

Comments
 (0)