Utilities¶
API reference for internal utilities.
_utils ¶
Internal utilities for owi.metadatabase package.
Classes¶
APIConnectionError ¶
Bases: APIException
Exception raised when the API cannot be reached or returns a failure.
This exception is raised when there are network issues, server errors, or other connection-related problems during API communication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error message. |
required |
response
|
Response or None
|
The HTTP response object if available, default is None. |
None
|
Examples:
>>> exc = APIConnectionError("Network timeout")
>>> str(exc)
'Network timeout'
>>> exc.message
'Network timeout'
Source code in src/owi/metadatabase/_utils/exceptions.py
DataProcessingError ¶
Bases: APIException
Exception raised when there is a problem while processing the data.
This exception indicates that data was retrieved successfully from the API but could not be processed, transformed, or validated correctly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error message, default is "Error during data processing." |
'Error during data processing.'
|
Examples:
>>> exc = DataProcessingError("Cannot parse geometry coordinates")
>>> str(exc)
'Cannot parse geometry coordinates'
Source code in src/owi/metadatabase/_utils/exceptions.py
InvalidParameterError ¶
Bases: APIException
Exception raised when query parameters are invalid or missing.
This exception is raised before making API requests when the provided parameters fail validation checks or are inconsistent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error message, default is "Invalid or missing parameters for the request." |
'Invalid or missing parameters for the request.'
|
Examples:
Source code in src/owi/metadatabase/_utils/exceptions.py
Functions¶
deepcompare ¶
Compare two complicated objects recursively.
Compares two complicated (potentially nested) objects recursively and returns a result and a message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Any
|
First object to be compared. |
required |
b
|
Any
|
Second object to be compared. |
required |
tol
|
float
|
Tolerance for the comparison, default is 1e-5. |
1e-05
|
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple with a result of comparison and a message if different. |
Examples:
>>> deepcompare({"a": 1.0}, {"a": 1.0})[0]
True
>>> deepcompare([1, 2], [1, 3])[0]
False
>>> deepcompare(np.nan, np.nan)[0]
True
Source code in src/owi/metadatabase/_utils/utils.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
Exceptions¶
exceptions ¶
Custom exceptions for the API client. These exceptions encapsulate common errors that can occur during API calls and data post-processing.
Classes¶
APIException ¶
Bases: Exception
Base exception for all errors raised by API.
This is the parent class for all custom exceptions in the package. It provides a consistent interface for error handling across API operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error message describing what went wrong. |
required |
Examples:
>>> exc = APIException("Something went wrong")
>>> str(exc)
'Something went wrong'
>>> exc.message
'Something went wrong'
Source code in src/owi/metadatabase/_utils/exceptions.py
APIConnectionError ¶
Bases: APIException
Exception raised when the API cannot be reached or returns a failure.
This exception is raised when there are network issues, server errors, or other connection-related problems during API communication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error message. |
required |
response
|
Response or None
|
The HTTP response object if available, default is None. |
None
|
Examples:
>>> exc = APIConnectionError("Network timeout")
>>> str(exc)
'Network timeout'
>>> exc.message
'Network timeout'
Source code in src/owi/metadatabase/_utils/exceptions.py
DataNotFoundError ¶
Bases: APIException
Exception raised when no data is found for the given query parameters.
This exception indicates that the API request was successful but returned no results matching the search criteria.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error message, default is "No data found for the given search criteria." |
'No data found for the given search criteria.'
|
Examples:
Source code in src/owi/metadatabase/_utils/exceptions.py
DataProcessingError ¶
Bases: APIException
Exception raised when there is a problem while processing the data.
This exception indicates that data was retrieved successfully from the API but could not be processed, transformed, or validated correctly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error message, default is "Error during data processing." |
'Error during data processing.'
|
Examples:
>>> exc = DataProcessingError("Cannot parse geometry coordinates")
>>> str(exc)
'Cannot parse geometry coordinates'
Source code in src/owi/metadatabase/_utils/exceptions.py
InvalidParameterError ¶
Bases: APIException
Exception raised when query parameters are invalid or missing.
This exception is raised before making API requests when the provided parameters fail validation checks or are inconsistent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error message, default is "Invalid or missing parameters for the request." |
'Invalid or missing parameters for the request.'
|
Examples:
Source code in src/owi/metadatabase/_utils/exceptions.py
Helper Functions¶
utils ¶
Utility functions for the owimetadatabase_preprocessor package.
Functions¶
custom_formatwarning ¶
Return customized warning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Warning message. |
required |
category
|
type
|
Warning category. |
required |
filename
|
str
|
Filename where warning occurred. |
required |
lineno
|
int
|
Line number where warning occurred. |
required |
line
|
str
|
Line text where warning occurred. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted warning string. |
Examples:
Source code in src/owi/metadatabase/_utils/utils.py
dict_generator ¶
Generate a dictionary with the specified keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dict_
|
dict
|
Dictionary to be filtered. |
required |
keys_
|
Sequence of str
|
List of keys to be included or excluded. |
None
|
method_
|
str
|
Method to be used for filtering. Options are "exclude" and "include", default is "exclude". |
'exclude'
|
Returns:
| Type | Description |
|---|---|
dict
|
Filtered dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If method is not recognized. |
Examples:
>>> dict_generator({"a": 1, "b": 2}, keys_=["a"])
{'b': 2}
>>> dict_generator({"a": 1, "b": 2}, keys_=["a"], method_="include")
{'a': 1}
>>> dict_generator({"a": 1}, method_="unknown")
Traceback (most recent call last):
...
ValueError: Method not recognized!
Source code in src/owi/metadatabase/_utils/utils.py
compare_if_simple_close ¶
Compare two values and return a boolean and a message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Any
|
First value to be compared. |
required |
b
|
Any
|
Second value to be compared. |
required |
tol
|
float
|
Tolerance for the comparison, default is 1e-9. |
1e-09
|
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple with a result of comparison and a message if different. |
Examples:
>>> compare_if_simple_close(1.0, 1.0)
(True, None)
>>> compare_if_simple_close(1.0, 2.0)
(False, 'Values of 1.0 and 2.0 are different.')
>>> compare_if_simple_close(np.nan, np.nan)
(True, None)
Source code in src/owi/metadatabase/_utils/utils.py
check_df_eq ¶
Check if two dataframes are equal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df1
|
DataFrame
|
First dataframe to be compared. |
required |
df2
|
DataFrame
|
Second dataframe to be compared. |
required |
tol
|
float
|
Tolerance for the comparison, default is 1e-9. |
1e-09
|
Returns:
| Type | Description |
|---|---|
bool
|
Boolean indicating if the dataframes are equal. |
Examples:
>>> df1 = pd.DataFrame({"a": [1.0, 2.0], "b": ["x", "y"]})
>>> df2 = pd.DataFrame({"a": [1.0, 2.0], "b": ["x", "y"]})
>>> check_df_eq(df1, df2)
True
>>> check_df_eq(pd.DataFrame(), pd.DataFrame())
True
Source code in src/owi/metadatabase/_utils/utils.py
deepcompare ¶
Compare two complicated objects recursively.
Compares two complicated (potentially nested) objects recursively and returns a result and a message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Any
|
First object to be compared. |
required |
b
|
Any
|
Second object to be compared. |
required |
tol
|
float
|
Tolerance for the comparison, default is 1e-5. |
1e-05
|
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple with a result of comparison and a message if different. |
Examples:
>>> deepcompare({"a": 1.0}, {"a": 1.0})[0]
True
>>> deepcompare([1, 2], [1, 3])[0]
False
>>> deepcompare(np.nan, np.nan)[0]
True
Source code in src/owi/metadatabase/_utils/utils.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
fix_nan ¶
Replace "nan" strings with None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to be fixed. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Fixed object. |
Examples:
>>> fixed = fix_nan({"a": "NaN", "b": ["nan", "ok"]})
>>> bool(np.isnan(fixed["a"]))
True
>>> bool(np.isnan(fixed["b"][0]))
True
Source code in src/owi/metadatabase/_utils/utils.py
fix_outline ¶
Fix the outline attribute in the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Data to be fixed. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Fixed data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data type is not supported. |
Examples:
Source code in src/owi/metadatabase/_utils/utils.py
hex_to_dec ¶
Convert hex color(s) to normalized [r, g, b, a] floats.
Accepts 6-digit (#rrggbb) or 8-digit (#rrggbbaa) hex strings, with
or without leading '#'.
- If value is a string: returns [r, g, b, a]
- If value is a list of strings: returns [[r, g, b, a], ...]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str or Sequence of str
|
Hex color string or list of hex color strings. |
required |
Returns:
| Type | Description |
|---|---|
list of float or list of list of float
|
Normalized RGBA list or list of such lists. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the hex string length is not 6 or 8, or if the input type is not supported. |
Examples:
>>> hex_to_dec("#ff0000")
[1.0, 0.0, 0.0, 1.0]
>>> hex_to_dec(["#000000", "ffffff"])
[[0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0]]
Source code in src/owi/metadatabase/_utils/utils.py
load_token_from_env_file ¶
Load a token from an environment file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_file
|
Path
|
Path to the environment file. |
required |
env_var
|
str
|
Environment variable name, by default "OWI_METADATABASE_API_TOKEN" |
'OWI_METADATABASE_API_TOKEN'
|
Returns:
| Type | Description |
|---|---|
str | None
|
The token value if found, otherwise None. |