Locations Module¶
API reference for the locations module.
locations ¶
Module for handling location data from OWI metadatabase.
Classes¶
LocationsAPI ¶
Bases: API
Class to connect to the location data API with methods to retrieve data.
A number of methods are provided to query the database via the
owimetadatabase API. In the majority of cases, the methods return a
dataframe based on the URL parameters provided. The methods are written
such that a number of mandatory URL parameters are required (see
documentation of the methods). The URL parameters can be expanded with
Django-style additional filtering arguments (e.g.
location__title__icontains="BB") as optional keyword arguments.
Knowledge of the Django models is required for this (see
owimetadatabase code).
Create an instance of the LocationsAPI class with required parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_subdir
|
str
|
Subdirectory of the API endpoint url for specific type of data. |
'/locations/'
|
**kwargs
|
Any
|
Additional parameters to pass to the API (see the base class). |
{}
|
Examples:
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> api.api_root.endswith("/locations/")
True
Source code in src/owi/metadatabase/locations/io.py
Functions¶
__eq__ ¶
Compare two instances of the API class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Another instance of the API class or a dictionary. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the instances are equal, False otherwise. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If comparison is not possible due to incompatible types. |
Examples:
>>> api_1 = API(api_root="https://example", token="test")
>>> api_2 = API(api_root="https://example", token="test")
>>> api_1 == api_2
True
Source code in src/owi/metadatabase/io.py
send_request ¶
Handle sending appropriate request.
Handles sending appropriate request according to the type of authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_data_type
|
str
|
Type of the data we want to request (according to database model). |
required |
url_params
|
Mapping
|
Parameters to send with the request to the database. |
required |
Returns:
| Type | Description |
|---|---|
Response
|
An instance of the Response object. |
Raises:
| Type | Description |
|---|---|
InvalidParameterError
|
If neither header nor username and password are defined. |
Examples:
>>> from types import SimpleNamespace
>>> from unittest import mock
>>> response = SimpleNamespace(status_code=200, reason="OK")
>>> with mock.patch("requests.get", return_value=response):
... api = API(api_root="https://example", token="test")
... resp = api.send_request("/projects", {})
>>> resp is response
True
Source code in src/owi/metadatabase/io.py
check_request_health
staticmethod
¶
Check status code of the response and provide details.
Checks status code of the response to request and provides details if unexpected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resp
|
Response
|
Instance of the Response object. |
required |
Raises:
| Type | Description |
|---|---|
APIConnectionError
|
If response status code is not 200. |
Examples:
>>> from types import SimpleNamespace
>>> ok = SimpleNamespace(status_code=200, reason="OK")
>>> API.check_request_health(ok)
Source code in src/owi/metadatabase/io.py
output_to_df
staticmethod
¶
Transform output to Pandas dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
Raw output of the sent request. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Pandas dataframe of the data from the output. |
Raises:
| Type | Description |
|---|---|
DataProcessingError
|
If failed to decode JSON from API response. |
Examples:
>>> from types import SimpleNamespace
>>> resp = SimpleNamespace(text='[{"a": 1}]')
>>> int(API.output_to_df(resp)["a"].iloc[0])
1
Source code in src/owi/metadatabase/io.py
postprocess_data
staticmethod
¶
Process dataframe information to extract additional data.
Processes dataframe information to extract the necessary additional data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe of the output data. |
required |
output_type
|
str
|
Expected type (amount) of the data extracted. |
required |
Returns:
| Type | Description |
|---|---|
PostprocessData
|
Dictionary of the additional data extracted. |
Raises:
| Type | Description |
|---|---|
InvalidParameterError
|
If more than one record was returned for 'single' output type, or if output type is not 'single' or 'list'. |
Examples:
Source code in src/owi/metadatabase/io.py
validate_data
staticmethod
¶
Validate the data extracted from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe of the output data. |
required |
data_type
|
str
|
Type of the data we want to request (according to database model). |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe with corrected data. |
Examples:
Source code in src/owi/metadatabase/io.py
process_data ¶
Process output data according to specified request parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_data_type
|
str
|
Type of the data we want to request (according to database model). |
required |
url_params
|
Mapping
|
Parameters to send with the request to the database. |
required |
output_type
|
str
|
Expected type (amount) of the data extracted. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple of dataframe with the requested data and additional data from postprocessing. |
Examples:
>>> from types import SimpleNamespace
>>> from unittest import mock
>>> response = SimpleNamespace(text="[]", status_code=200, reason="OK")
>>> api = API(api_root="https://example", token="test")
>>> with mock.patch.object(API, "send_request", return_value=response):
... df, info = api.process_data("projects", {}, "list")
>>> df.empty
True
>>> info["existance"]
False
Source code in src/owi/metadatabase/io.py
get_projectsites ¶
Get all available projects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional parameters to pass to the API. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the following keys:
|
Examples:
>>> from unittest import mock
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> df = pd.DataFrame({"id": [1]})
>>> with mock.patch.object(
... LocationsAPI,
... "process_data",
... return_value=(df, {"existance": True}),
... ):
... out = api.get_projectsites()
>>> out["exists"]
True
Source code in src/owi/metadatabase/locations/io.py
get_projectsite_detail ¶
Get details for a specific projectsite.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
projectsite
|
str
|
Title of the projectsite. |
required |
**kwargs
|
Any
|
Additional parameters to pass to the API. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the following keys:
|
Examples:
>>> from unittest import mock
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> df = pd.DataFrame({"id": [1]})
>>> with mock.patch.object(
... LocationsAPI,
... "process_data",
... return_value=(df, {"existance": True, "id": 1}),
... ):
... out = api.get_projectsite_detail("Site")
>>> out["id"]
1
Source code in src/owi/metadatabase/locations/io.py
get_assetlocations ¶
Get all available asset locations.
Get all available asset locations for all projectsites or a specific projectsite.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
projectsite
|
str
|
String with the projectsite title (e.g. "Nobelwind"). |
None
|
**kwargs
|
Any
|
Additional parameters to pass to the API. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the following keys:
|
Examples:
>>> from unittest import mock
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> df = pd.DataFrame({"id": [1]})
>>> with mock.patch.object(
... LocationsAPI,
... "process_data",
... return_value=(df, {"existance": True}),
... ):
... out = api.get_assetlocations(projectsite="Site")
>>> out["exists"]
True
Source code in src/owi/metadatabase/locations/io.py
get_assetlocation_detail ¶
Get a selected turbine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assetlocation
|
str
|
Title of the asset location (e.g. "BBK05"). |
required |
projectsite
|
str
|
Name of the projectsite (e.g. "Nobelwind"). |
None
|
**kwargs
|
Any
|
Additional parameters to pass to the API. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the following keys:
|
Examples:
>>> from unittest import mock
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> df = pd.DataFrame({"id": [1]})
>>> with mock.patch.object(
... LocationsAPI,
... "process_data",
... return_value=(df, {"existance": True, "id": 1}),
... ):
... out = api.get_assetlocation_detail("T01")
>>> out["id"]
1
Source code in src/owi/metadatabase/locations/io.py
plot_assetlocations ¶
Retrieve asset locations and generate a Plotly plot.
Retrieves asset locations and generates a Plotly plot to show them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
return_fig
|
bool
|
Boolean indicating whether to return the figure, default is False. |
False
|
**kwargs
|
Any
|
Keyword arguments for the search (see
|
{}
|
Returns:
| Type | Description |
|---|---|
Figure or None
|
Plotly figure object with selected asset locations plotted on OpenStreetMap tiles (if requested) or nothing. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no asset locations found for the given parameters. |
Examples:
>>> from unittest import mock
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> data = pd.DataFrame(
... {
... "northing": [51.5],
... "easting": [2.8],
... "title": ["T01"],
... "projectsite_name": ["Site"],
... "description": [""],
... }
... )
>>> with mock.patch.object(
... LocationsAPI,
... "get_assetlocations",
... return_value={"exists": True, "data": data},
... ):
... fig = api.plot_assetlocations(return_fig=True)
>>> fig is not None
True
Source code in src/owi/metadatabase/locations/io.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
locations.io — API Client¶
io ¶
Module to connect to the database API to retrieve and operate on locations data.
Classes¶
LocationsAPI ¶
Bases: API
Class to connect to the location data API with methods to retrieve data.
A number of methods are provided to query the database via the
owimetadatabase API. In the majority of cases, the methods return a
dataframe based on the URL parameters provided. The methods are written
such that a number of mandatory URL parameters are required (see
documentation of the methods). The URL parameters can be expanded with
Django-style additional filtering arguments (e.g.
location__title__icontains="BB") as optional keyword arguments.
Knowledge of the Django models is required for this (see
owimetadatabase code).
Create an instance of the LocationsAPI class with required parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_subdir
|
str
|
Subdirectory of the API endpoint url for specific type of data. |
'/locations/'
|
**kwargs
|
Any
|
Additional parameters to pass to the API (see the base class). |
{}
|
Examples:
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> api.api_root.endswith("/locations/")
True
Source code in src/owi/metadatabase/locations/io.py
Functions¶
Get all available projects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional parameters to pass to the API. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the following keys:
|
Examples:
>>> from unittest import mock
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> df = pd.DataFrame({"id": [1]})
>>> with mock.patch.object(
... LocationsAPI,
... "process_data",
... return_value=(df, {"existance": True}),
... ):
... out = api.get_projectsites()
>>> out["exists"]
True
Source code in src/owi/metadatabase/locations/io.py
Get details for a specific projectsite.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
projectsite
|
str
|
Title of the projectsite. |
required |
**kwargs
|
Any
|
Additional parameters to pass to the API. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the following keys:
|
Examples:
>>> from unittest import mock
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> df = pd.DataFrame({"id": [1]})
>>> with mock.patch.object(
... LocationsAPI,
... "process_data",
... return_value=(df, {"existance": True, "id": 1}),
... ):
... out = api.get_projectsite_detail("Site")
>>> out["id"]
1
Source code in src/owi/metadatabase/locations/io.py
Get all available asset locations.
Get all available asset locations for all projectsites or a specific projectsite.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
projectsite
|
str
|
String with the projectsite title (e.g. "Nobelwind"). |
None
|
**kwargs
|
Any
|
Additional parameters to pass to the API. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the following keys:
|
Examples:
>>> from unittest import mock
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> df = pd.DataFrame({"id": [1]})
>>> with mock.patch.object(
... LocationsAPI,
... "process_data",
... return_value=(df, {"existance": True}),
... ):
... out = api.get_assetlocations(projectsite="Site")
>>> out["exists"]
True
Source code in src/owi/metadatabase/locations/io.py
Get a selected turbine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assetlocation
|
str
|
Title of the asset location (e.g. "BBK05"). |
required |
projectsite
|
str
|
Name of the projectsite (e.g. "Nobelwind"). |
None
|
**kwargs
|
Any
|
Additional parameters to pass to the API. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the following keys:
|
Examples:
>>> from unittest import mock
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> df = pd.DataFrame({"id": [1]})
>>> with mock.patch.object(
... LocationsAPI,
... "process_data",
... return_value=(df, {"existance": True, "id": 1}),
... ):
... out = api.get_assetlocation_detail("T01")
>>> out["id"]
1
Source code in src/owi/metadatabase/locations/io.py
Retrieve asset locations and generate a Plotly plot.
Retrieves asset locations and generates a Plotly plot to show them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
return_fig
|
bool
|
Boolean indicating whether to return the figure, default is False. |
False
|
**kwargs
|
Any
|
Keyword arguments for the search (see
|
{}
|
Returns:
| Type | Description |
|---|---|
Figure or None
|
Plotly figure object with selected asset locations plotted on OpenStreetMap tiles (if requested) or nothing. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no asset locations found for the given parameters. |
Examples:
>>> from unittest import mock
>>> api = LocationsAPI(
... api_root="https://example",
... header={"Authorization": "Token test"},
... )
>>> data = pd.DataFrame(
... {
... "northing": [51.5],
... "easting": [2.8],
... "title": ["T01"],
... "projectsite_name": ["Site"],
... "description": [""],
... }
... )
>>> with mock.patch.object(
... LocationsAPI,
... "get_assetlocations",
... return_value={"exists": True, "data": data},
... ):
... fig = api.plot_assetlocations(return_fig=True)
>>> fig is not None
True
Source code in src/owi/metadatabase/locations/io.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
Compare two instances of the API class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Another instance of the API class or a dictionary. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the instances are equal, False otherwise. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If comparison is not possible due to incompatible types. |
Examples:
>>> api_1 = API(api_root="https://example", token="test")
>>> api_2 = API(api_root="https://example", token="test")
>>> api_1 == api_2
True
Source code in src/owi/metadatabase/io.py
Handle sending appropriate request.
Handles sending appropriate request according to the type of authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_data_type
|
str
|
Type of the data we want to request (according to database model). |
required |
url_params
|
Mapping
|
Parameters to send with the request to the database. |
required |
Returns:
| Type | Description |
|---|---|
Response
|
An instance of the Response object. |
Raises:
| Type | Description |
|---|---|
InvalidParameterError
|
If neither header nor username and password are defined. |
Examples:
>>> from types import SimpleNamespace
>>> from unittest import mock
>>> response = SimpleNamespace(status_code=200, reason="OK")
>>> with mock.patch("requests.get", return_value=response):
... api = API(api_root="https://example", token="test")
... resp = api.send_request("/projects", {})
>>> resp is response
True
Source code in src/owi/metadatabase/io.py
staticmethod
¶Check status code of the response and provide details.
Checks status code of the response to request and provides details if unexpected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resp
|
Response
|
Instance of the Response object. |
required |
Raises:
| Type | Description |
|---|---|
APIConnectionError
|
If response status code is not 200. |
Examples:
>>> from types import SimpleNamespace
>>> ok = SimpleNamespace(status_code=200, reason="OK")
>>> API.check_request_health(ok)
Source code in src/owi/metadatabase/io.py
staticmethod
¶Transform output to Pandas dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
Raw output of the sent request. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Pandas dataframe of the data from the output. |
Raises:
| Type | Description |
|---|---|
DataProcessingError
|
If failed to decode JSON from API response. |
Examples:
>>> from types import SimpleNamespace
>>> resp = SimpleNamespace(text='[{"a": 1}]')
>>> int(API.output_to_df(resp)["a"].iloc[0])
1
Source code in src/owi/metadatabase/io.py
staticmethod
¶Process dataframe information to extract additional data.
Processes dataframe information to extract the necessary additional data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe of the output data. |
required |
output_type
|
str
|
Expected type (amount) of the data extracted. |
required |
Returns:
| Type | Description |
|---|---|
PostprocessData
|
Dictionary of the additional data extracted. |
Raises:
| Type | Description |
|---|---|
InvalidParameterError
|
If more than one record was returned for 'single' output type, or if output type is not 'single' or 'list'. |
Examples:
Source code in src/owi/metadatabase/io.py
staticmethod
¶Validate the data extracted from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe of the output data. |
required |
data_type
|
str
|
Type of the data we want to request (according to database model). |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe with corrected data. |
Examples:
Source code in src/owi/metadatabase/io.py
Process output data according to specified request parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_data_type
|
str
|
Type of the data we want to request (according to database model). |
required |
url_params
|
Mapping
|
Parameters to send with the request to the database. |
required |
output_type
|
str
|
Expected type (amount) of the data extracted. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple of dataframe with the requested data and additional data from postprocessing. |
Examples:
>>> from types import SimpleNamespace
>>> from unittest import mock
>>> response = SimpleNamespace(text="[]", status_code=200, reason="OK")
>>> api = API(api_root="https://example", token="test")
>>> with mock.patch.object(API, "send_request", return_value=response):
... df, info = api.process_data("projects", {}, "list")
>>> df.empty
True
>>> info["existance"]
False