Skip to content

Commit

Permalink
context manager bug fix (#13)
Browse files Browse the repository at this point in the history
* - context manager bug fix
- removed string depth signals from tests

* - change max retries to 5
- added signals loading tutorial
- fixed bug in loading signals

* - improved signals loading test

* formatting
  • Loading branch information
romanmanasipov authored May 3, 2024
1 parent 17ecb7c commit 64acf5f
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 10,085 deletions.
10,146 changes: 159 additions & 9,987 deletions examples/entities_and_signals.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/petrovisor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.5"
__version__ = "0.1.6"

# api
from petrovisor.petrovisor import PetroVisor
Expand Down
1 change: 0 additions & 1 deletion src/petrovisor/api/methods/pivot_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
Dict,
)
import warnings
import time

from petrovisor.api.utils.helper import ApiHelper
from petrovisor.api.utils.requests import ApiRequests
Expand Down
70 changes: 49 additions & 21 deletions src/petrovisor/api/methods/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ def get_signal_and_unit(signal):
data_time_num = table_data.get("DataNumeric", [])
data_time_str = table_data.get("DataString", [])
data_depth_num = table_data.get("DataDepth", [])
data_depth_str = table_data.get("DataStringDepth", [])
data_depth_str = table_data.get("DataDepthString", [])

if data_time_num and data_time_str:
data_time = [*data_time_num, *data_time_str]
Expand Down Expand Up @@ -820,18 +820,26 @@ def get_signal_and_unit(signal):
)

# generate PivotTable
df = df_normalized.pivot(
index=["EntityName", "Date"], columns="ResultName", values="Value"
)
df.columns.name = None
df = df.rename(columns=signals_with_units_map)
df = df.reset_index()
df = df.rename(columns={"EntityName": "Entity"})
df["Date"] = pd.to_datetime(df["Date"])
if has_time_signals:
df_time = df
if "Date" not in df_normalized.columns:
warnings.warn(
"PetroVisor::load_signals_data():: Couldn't retrieve any 'time' data.",
RuntimeWarning,
)
else:
df_static = df.drop(columns=["Date"])
df = df_normalized.pivot(
index=["EntityName", "Date"],
columns="ResultName",
values="Value",
)
df.columns.name = None
df = df.rename(columns=signals_with_units_map)
df = df.reset_index()
df = df.rename(columns={"EntityName": "Entity"})
df["Date"] = pd.to_datetime(df["Date"])
if has_time_signals:
df_time = df
else:
df_static = df.drop(columns=["Date"])

if data_depth:
# create DataFrame by normalizing json
Expand All @@ -842,14 +850,22 @@ def get_signal_and_unit(signal):
)

# generate PivotTable
df = df_normalized.pivot(
index=["EntityName", "Depth"], columns="ResultName", values="Value"
)
df.columns.name = None
df = df.rename(columns=signals_with_units_map)
df = df.reset_index()
df = df.rename(columns={"EntityName": "Entity"})
df_depth = df
if "Depth" not in df_normalized.columns:
warnings.warn(
"PetroVisor::load_signals_data():: Couldn't retrieve any 'depth' data.",
RuntimeWarning,
)
else:
df = df_normalized.pivot(
index=["EntityName", "Depth"],
columns="ResultName",
values="Value",
)
df.columns.name = None
df = df.rename(columns=signals_with_units_map)
df = df.reset_index()
df = df.rename(columns={"EntityName": "Entity"})
df_depth = df
else:
for data_type, data_type_signals in signal_types.items():
if data_type == "static":
Expand Down Expand Up @@ -996,7 +1012,7 @@ def get_signal_and_unit(signal):

if not data:
warnings.warn(
f"PetroVisor::load_signals_data():: Couldn't retrieve any {data_type} data.",
f"PetroVisor::load_signals_data():: Couldn't retrieve any '{data_type}' data.",
RuntimeWarning,
)
continue
Expand All @@ -1008,6 +1024,12 @@ def get_signal_and_unit(signal):
)

# generate PivotTable
if "Date" not in df_normalized.columns:
warnings.warn(
f"PetroVisor::load_signals_data():: Couldn't retrieve any '{data_type}' data.",
RuntimeWarning,
)
continue
df = df_normalized.pivot(
index=["Entity", "Date"], columns="Signal", values="Value"
)
Expand All @@ -1023,6 +1045,12 @@ def get_signal_and_unit(signal):
)

# generate PivotTable
if "Depth" not in df_normalized.columns:
warnings.warn(
f"PetroVisor::load_signals_data():: Couldn't retrieve any '{data_type}' data.",
RuntimeWarning,
)
continue
df = df_normalized.pivot(
index=["Entity", "Depth"], columns="Signal", values="Value"
)
Expand Down
9 changes: 4 additions & 5 deletions src/petrovisor/api/utils/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,10 @@ def get_response(
if data and not isinstance(data, str):
data = json.dumps(data)

# request secs
# request specs
timeout = None # no timeout
max_retries = (
1 # increased to 3 times in case of 400 Bad Request or 4004 Not Found
)
max_retries_top = 5
max_retries = 1 # increased to max_retries_top times in case of 400 Bad Request or 404 Not Found
waiting_time = 5 # in seconds

# get response
Expand Down Expand Up @@ -426,7 +425,7 @@ def get_response(
requests.codes.bad_request,
requests.codes.not_found,
}:
max_retries = 3
max_retries = max_retries_top

# retry request
if attempt < max_retries:
Expand Down
2 changes: 1 addition & 1 deletion src/petrovisor/models/contexts_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(
if isinstance(entity_set, str) and api.item_exists(
ItemType.EntitySet, entity_set
):
entity_set = api.get_entity(entity_set)
entity_set = api.get_entity_set(entity_set)
entity_set = api.create_entity_set(entity_set)

# get Scope
Expand Down
6 changes: 3 additions & 3 deletions tests/test_entities_and_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def test_signals_by_entity(api: PetroVisor):
},
)
# rename entity
new_entity_name = entity_name.replace("_entity", "_renamed entity")
# pv_api.rename_entity(old_name=entity_name, new_name=new_entity_name)
# new_entity_name = entity_name.replace("_entity", "_renamed entity")
# api.rename_entity(old_name=entity_name, new_name=new_entity_name)
# delete entity
# pv_api.delete_entity(new_entity_name)
# api.delete_entity(new_entity_name)
# add signal
signal_name = "Time Signal"
signal_unit = " "
Expand Down
Loading

0 comments on commit 64acf5f

Please sign in to comment.