Skip to content

Commit

Permalink
Merge pull request #192 from ImperialCollegeLondon/start-model
Browse files Browse the repository at this point in the history
Model interactivity APIs
  • Loading branch information
AdrianDAlessandro authored Nov 30, 2023
2 parents a4a01fa + dc6e665 commit 3cafac0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 9 deletions.
12 changes: 12 additions & 0 deletions datahub/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,15 @@
opal_df: pd.DataFrame = create_opal_frame()
dsr_data: list[dict[str, NDArray | str]] = [] # type: ignore[type-arg]
wesim_data: dict[str, dict] = {} # type: ignore[type-arg]

model_running: bool = False
model_resetting: bool = False


def reset_data() -> None:
"""Reset the OPAL and DSR data to their initial (empty) values."""
global opal_df
global dsr_data

opal_df = create_opal_frame()
dsr_data = []
77 changes: 77 additions & 0 deletions datahub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,80 @@ def get_wesim_data() -> dict[str, dict[str, dict]]: # type: ignore[type-arg]
dt.wesim_data = get_wesim()

return {"data": dt.wesim_data}


@app.post("/set_model_signals")
def set_model_signals(start: bool) -> str:
"""POST method function for setting start and stop model signals.
It has the query parameter:
- `start`: A boolean to indicate the model should start running or stop running.
\f
Args:
start: A bool flag for if the model should start running or stop running.
True to start the model, False to stop the model.
Returns:
A confirmation message
""" # noqa: D301
message = "Start signal received" if start else "Stop signal received"
log.info(message)
dt.model_running = start

return message


@app.post("/model_ready")
def signal_model_ready(ready: bool) -> str:
"""POST method function for indicating when the model has reset and is ready to run.
It has the query parameter:
- `ready`: A boolean to indicate the model has completed setup and is ready.
\f
Args:
ready: A bool flag for if the model has completed setup and is ready.
Returns:
A confirmation message
""" # noqa: D301
message = "Ready signal received" if ready else "Not-Ready signal received"
log.info(message)
dt.model_resetting = not ready

return message


@app.get("/start")
def get_start_signal() -> bool:
"""GET method function for getting start model signal.
It returns a boolean: True for if the model should start running.
\f
Returns:
A bool flag for if the model should start
""" # noqa: D301
log.info("Start signal requested")

return dt.model_running and not dt.model_resetting


@app.get("/stop")
def get_stop_signal() -> bool:
"""GET method function for getting stop model signal.
It returns a boolean: True for if the model should stop running.
\f
Returns:
A bool flag for if the model should stop
""" # noqa: D301
log.info("Start signal requested")

return not dt.model_running
2 changes: 1 addition & 1 deletion datahub/opal.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,6 @@ def get_opal_row(data: dict[str, int | float] | list[int | float]) -> pd.DataFra
row = pd.DataFrame(
[data_array], index=[data_index], columns=list(opal_headers.keys())
)
row["Time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(row["Time"], unit="S")
row["Time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(row["Time"], unit="m")

return row
2 changes: 1 addition & 1 deletion tests/test_dsr_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@pytest.fixture(autouse=True)
def reset_dsr_data():
"""Pytest Fixture for resetting DSR data global variable."""
dt.dsr_data = []
dt.reset_data()


def test_post_dsr_api(dsr_data_path):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_opal.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def test_append_opal_data(opal_data):

# Checks that data appended to Dataframe matches data input.
data_1["time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(
data_1["time"], unit="S"
data_1["time"], unit="m"
)
data_2["time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(
data_2["time"], unit="S"
data_2["time"], unit="m"
)

assert (df.loc[1] == list(data_1.values())[1:]).all()
Expand All @@ -58,7 +58,7 @@ def test_append_opal_data(opal_data):
assert len(df.index) == 2

data_3["time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(
data_3["time"], unit="S"
data_3["time"], unit="m"
)

assert (df.loc[2] == list(data_3.values())[1:]).all()
Expand Down Expand Up @@ -100,6 +100,6 @@ def test_append_opal_data_array(opal_data_array):

assert df.shape == (1, len(opal_headers))

data_1[1] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(data_1[1], unit="S")
data_1[1] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(data_1[1], unit="m")

assert (df.loc[1] == data_1[1:]).all()
4 changes: 1 addition & 3 deletions tests/test_opal_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
@pytest.fixture(autouse=True)
def reset_opal_data():
"""Pytest Fixture for resetting Opal data global variable."""
from datahub.opal import create_opal_frame

dt.opal_df = create_opal_frame()
dt.reset_data()


def test_post_opal_api(client, opal_data):
Expand Down

0 comments on commit 3cafac0

Please sign in to comment.