Skip to content

Commit

Permalink
Linter fixes after merge and P/C architecture fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
calina-c committed Mar 27, 2024
1 parent 058aaa5 commit e055aef
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 77 deletions.
1 change: 0 additions & 1 deletion pdr_backend/cli/cli_module.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import os
import subprocess
import sys

from enforce_typing import enforce_types
Expand Down
10 changes: 1 addition & 9 deletions pdr_backend/cli/test/test_cli_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,7 @@ def test_do_sim(monkeypatch):
mock_f = Mock()
monkeypatch.setattr(f"{_CLI_PATH}.SimEngine.run", mock_f)

with patch("pdr_backend.cli.cli_module.subprocess.run") as mock_subprocess:
with patch("sys.argv", ["pdr", "sim", "ppss.yaml"]):
do_sim(MockArgParser_PPSS().parse_args())

mock_subprocess.assert_called()
mock_f.assert_not_called()

with patch("sys.argv", ["streamlit_entrypoint.py", "sim", "ppss.yaml"]):
do_sim(MockArgParser_PPSS().parse_args())
do_sim(MockArgParser_PPSS().parse_args())

mock_f.assert_called()

Expand Down
10 changes: 0 additions & 10 deletions pdr_backend/ppss/sim_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ class SimSS(StrMixin, CCXTExchangeMixin):
def __init__(self, d: dict):
self.d = d # yaml_dict["sim_ss"]

# check do_plot
if not isinstance(d["do_plot"], bool):
raise TypeError

# handle log_dir; self.log_dir is supposed to be path-expanded version
assert self.log_dir == os.path.abspath(self.log_dir)
if not os.path.exists(self.log_dir):
Expand All @@ -47,10 +43,6 @@ def __init__(self, d: dict):

# --------------------------------
# properties direct from yaml dict
@property
def do_plot(self) -> bool:
return self.d["do_plot"]

@property
def log_dir(self) -> str:
s = self.d["log_dir"]
Expand Down Expand Up @@ -82,13 +74,11 @@ def is_final_iter(self, iter_i: int) -> bool:

@enforce_types
def sim_ss_test_dict(
do_plot: bool,
log_dir: str,
test_n: Optional[int] = None,
tradetype: Optional[str] = None,
) -> dict:
d = {
"do_plot": do_plot,
"log_dir": log_dir,
"test_n": test_n or 10,
"tradetype": tradetype or "histmock",
Expand Down
1 change: 0 additions & 1 deletion pdr_backend/ppss/test/test_ppss.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def _test_ppss(yaml_filename=None, yaml_str=None, network=None):
assert ppss.predictoor_ss.aimodel_ss.approach == "LinearLogistic"
assert ppss.payout_ss.batch_size >= 0
assert 1 <= ppss.predictoor_ss.s_until_epoch_end <= 120
assert isinstance(ppss.sim_ss.do_plot, bool)
assert 0.0 <= ppss.trader_ss.fee_percent <= 0.99
assert "USD" in ppss.trader_ss.buy_amt_str
assert ppss.trueval_ss.batch_size >= 0
Expand Down
29 changes: 9 additions & 20 deletions pdr_backend/ppss/test/test_sim_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

@enforce_types
def test_sim_ss_defaults(tmpdir):
d = sim_ss_test_dict(True, _logdir(tmpdir))
d = sim_ss_test_dict(_logdir(tmpdir))
ss = SimSS(d)

# yaml properties
assert isinstance(ss.do_plot, bool)
assert isinstance(ss.log_dir, str)
assert isinstance(ss.test_n, int)
assert 1 <= ss.test_n <= 10000
Expand All @@ -29,15 +28,13 @@ def test_sim_ss_defaults(tmpdir):
@enforce_types
def test_sim_ss_give_values(tmpdir):
d = sim_ss_test_dict(
do_plot=False,
log_dir=os.path.join(tmpdir, "mylogs"),
test_n=13,
tradetype="livereal",
)
ss = SimSS(d)

# yaml properties
assert not ss.do_plot
assert ss.log_dir == os.path.join(tmpdir, "mylogs")
assert ss.test_n == 13
assert ss.tradetype == "livereal"
Expand All @@ -46,20 +43,12 @@ def test_sim_ss_give_values(tmpdir):
assert "SimSS" in str(ss)


@enforce_types
def test_sim_ss_do_plot_badpaths(tmpdir):
d = sim_ss_test_dict(True, _logdir(tmpdir))
d["do_plot"] = "not_a_bool"
with pytest.raises(TypeError):
_ = SimSS(d)


@enforce_types
def test_sim_ss_log_dir_relative_path():
# it will work with the relative path
expanded_path = os.path.abspath("mylogs")
had_before = os.path.exists(expanded_path)
d = sim_ss_test_dict(True, "mylogs")
d = sim_ss_test_dict("mylogs")
ss = SimSS(d)
assert ss.log_dir == expanded_path
if not had_before:
Expand All @@ -68,16 +57,16 @@ def test_sim_ss_log_dir_relative_path():

@enforce_types
def test_sim_ss_test_n_badpaths(tmpdir):
d = sim_ss_test_dict(True, _logdir(tmpdir), test_n=-3)
d = sim_ss_test_dict(_logdir(tmpdir), test_n=-3)
with pytest.raises(ValueError):
_ = SimSS(d)

d = sim_ss_test_dict(True, _logdir(tmpdir))
d = sim_ss_test_dict(_logdir(tmpdir))
d["test_n"] = "not_an_int"
with pytest.raises(TypeError):
_ = SimSS(d)

d = sim_ss_test_dict(True, _logdir(tmpdir))
d = sim_ss_test_dict(_logdir(tmpdir))
d["test_n"] = 3.2
with pytest.raises(TypeError):
_ = SimSS(d)
Expand All @@ -86,26 +75,26 @@ def test_sim_ss_test_n_badpaths(tmpdir):
@enforce_types
def test_sim_ss_tradetype_happypaths(tmpdir):
for tradetype in TRADETYPE_OPTIONS:
d = sim_ss_test_dict(True, _logdir(tmpdir), tradetype=tradetype)
d = sim_ss_test_dict(_logdir(tmpdir), tradetype=tradetype)
ss = SimSS(d)
assert ss.tradetype == tradetype


@enforce_types
def test_sim_ss_tradetype_badpaths(tmpdir):
d = sim_ss_test_dict(True, _logdir(tmpdir))
d = sim_ss_test_dict(_logdir(tmpdir))
d["tradetype"] = 3.2
with pytest.raises(TypeError):
_ = SimSS(d)

d = sim_ss_test_dict(True, _logdir(tmpdir), tradetype="not a tradetype")
d = sim_ss_test_dict(_logdir(tmpdir), tradetype="not a tradetype")
with pytest.raises(ValueError):
_ = SimSS(d)


@enforce_types
def test_sim_ss_is_final_iter(tmpdir):
d = sim_ss_test_dict(True, _logdir(tmpdir), test_n=10)
d = sim_ss_test_dict(_logdir(tmpdir), test_n=10)
ss = SimSS(d)
with pytest.raises(ValueError):
_ = ss.is_final_iter(-5)
Expand Down
1 change: 0 additions & 1 deletion pdr_backend/sim/multisim_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def ppss_from_point(self, point_i: Point) -> PPSS:
d = copy.deepcopy(self.d)
recursive_update(d, nested_args)
ppss = PPSS(d=d, network=self.network)
assert not ppss.sim_ss.do_plot # type: ignore[attr-defined]
return ppss

@enforce_types
Expand Down
2 changes: 0 additions & 2 deletions pdr_backend/sim/sim_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,6 @@ def save_state(self, i: int, N: int):
if self.ppss.sim_ss.is_final_iter(i):
return True, True

# TODO: remove do_plot in sim_ss -> if the user wants to plot, they start the streamlit app

# don't save first 5 iters -> not interesting
# then save the next 5 -> "stuff's happening!"
# then save every 5th iter, to balance "stuff's happening" w/ speed
Expand Down
46 changes: 28 additions & 18 deletions pdr_backend/sim/sim_plotter.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import glob
import os
import pickle
import time
from datetime import datetime

import altair as alt
import numpy as np
import pandas as pd
from enforce_typing import enforce_types

from pdr_backend.aimodel.aimodel_plotdata import AimodelPlotdata
from datetime import datetime
import pickle
import os
import glob
import time


HEIGHT = 7.5
WIDTH = int(HEIGHT * 3.2)
Expand All @@ -26,16 +26,17 @@ def __init__(

def load_state(self):
if not os.path.exists("sim_state"):
raise Exception("sim_state folder does not exist. Please run the simulation first.")
raise Exception(
"sim_state folder does not exist. Please run the simulation first."
)

all_state_files = glob.glob('sim_state/st_*.pkl')
all_state_files = glob.glob("sim_state/st_*.pkl")
if not all_state_files:
raise Exception("No state files found. Please run the simulation first.")

if not os.path.exists("sim_state/st_final.pkl"):
# TODO logger.info("Simulation still running. Plot will update in real time")
# plot previous state to avoid using a pickle that hasn't finished
all_state_files = glob.glob('sim_state/st_*.pkl')
all_state_files = glob.glob("sim_state/st_*.pkl")
all_state_files.sort()
latest_file = all_state_files[-1]
with open(latest_file, "rb") as f:
Expand All @@ -46,23 +47,32 @@ def load_state(self):

return self.st, latest_file.replace("sim_state/st_", "").replace(".pkl", "")

# TODO: logger.info("Simulation finished. Plotting final results")
# TODO: make sure the final state is written to disk!
# make sure the final state is written to disk before unpickling
# avoid race conditions with the pickling itself
if file_age_in_seconds("sim_state/st_final.pkl") < 3:
time.sleep(3)

self.st = pickle.load(open("sim_state/st_final.pkl", "rb"))
self.aimodel_plotdata = pickle.load(open("sim_state/aimodel_plotdata_final.pkl", "rb"))
with open("sim_state/st_final.pkl", "rb") as f:
self.st = pickle.load(f)

with open("sim_state/aimodel_plotdata_final.pkl", "rb") as f:
self.aimodel_plotdata = pickle.load(f)

return self.st, "final"

def init_state(self):
files = glob.glob('sim_state/*')
files = glob.glob("sim_state/*")
for f in files:
os.remove(f)

def save_state(self, sim_state, aimodel_plotdata: AimodelPlotdata, is_final: bool = False):
# TODO: for multisim, we could add a unique identifier to the filename and separate states
ts = datetime.now().strftime("%Y%m%d_%H%M%S.%f")[:-3] if not is_final else "final"
def save_state(
self, sim_state, aimodel_plotdata: AimodelPlotdata, is_final: bool = False
):
ts = (
datetime.now().strftime("%Y%m%d_%H%M%S.%f")[:-3]
if not is_final
else "final"
)
with open(f"sim_state/st_{ts}.pkl", "wb") as f:
pickle.dump(sim_state, f)

Expand Down
2 changes: 1 addition & 1 deletion pdr_backend/sim/sim_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, init_holdings: Dict[str, Eth]):
tok: float(amt.amt_eth) for tok, amt in init_holdings.items()
}
self.init_loop_attributes()
self.iter_number = None
self.iter_number = 0

def init_loop_attributes(self):
# 'i' is iteration number i
Expand Down
2 changes: 1 addition & 1 deletion pdr_backend/sim/test/test_multisim_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _constructor_d_with_fast_runtime(tmpdir):

# sim ss
log_dir = os.path.join(tmpdir, "logs")
d = sim_ss_test_dict(do_plot=False, log_dir=log_dir, test_n=10)
d = sim_ss_test_dict(log_dir=log_dir, test_n=10)
constructor_d["sim_ss"] = d

return constructor_d
3 changes: 1 addition & 2 deletions pdr_backend/sim/test/test_sim_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ def test_sim_engine(tmpdir):
ppss.predictoor_ss = PredictoorSS(d)

# sim ss
do_plot = True
log_dir = os.path.join(tmpdir, "logs")
d = sim_ss_test_dict(do_plot, log_dir, test_n=5)
d = sim_ss_test_dict(log_dir, test_n=5)
ppss.sim_ss = SimSS(d)

# go
Expand Down
1 change: 0 additions & 1 deletion ppss.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ trader_ss:
defaultType: spot

sim_ss: # sim only
do_plot: True
log_dir: logs
test_n: 5000 # number of epochs to simulate
tradetype: histmock # histmock | livemock | livereal
Expand Down
36 changes: 26 additions & 10 deletions sim_plots.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from pdr_backend.sim.sim_plotter import SimPlotter
import time

import streamlit

from pdr_backend.aimodel.aimodel_plotter import (
plot_aimodel_response,
plot_aimodel_varimps,
)
import streamlit
import time
from pdr_backend.sim.sim_plotter import SimPlotter

streamlit.set_page_config(layout="wide")

Expand Down Expand Up @@ -51,35 +53,49 @@
continue

canvas["pdr_profit_vs_time"].altair_chart(
sim_plotter.plot_pdr_profit_vs_time(), use_container_width=True, theme="streamlit"
sim_plotter.plot_pdr_profit_vs_time(),
use_container_width=True,
theme="streamlit",
)

canvas["trader_profit_vs_time"].altair_chart(
sim_plotter.plot_trader_profit_vs_time(), use_container_width=True, theme="streamlit"
sim_plotter.plot_trader_profit_vs_time(),
use_container_width=True,
theme="streamlit",
)

canvas["accuracy_vs_time"].altair_chart(
sim_plotter.plot_accuracy_vs_time(), use_container_width=True, theme="streamlit"
)

canvas["f1_precision_recall_vs_time"].altair_chart(
sim_plotter.plot_f1_precision_recall_vs_time(), use_container_width=True, theme="streamlit"
sim_plotter.plot_f1_precision_recall_vs_time(),
use_container_width=True,
theme="streamlit",
)

canvas["pdr_profit_vs_ptrue"].altair_chart(
sim_plotter.plot_pdr_profit_vs_ptrue(), use_container_width=True, theme="streamlit"
sim_plotter.plot_pdr_profit_vs_ptrue(),
use_container_width=True,
theme="streamlit",
)

canvas["trader_profit_vs_ptrue"].altair_chart(
sim_plotter.plot_trader_profit_vs_ptrue(), use_container_width=True, theme="streamlit"
sim_plotter.plot_trader_profit_vs_ptrue(),
use_container_width=True,
theme="streamlit",
)

canvas["aimodel_varimps"].altair_chart(
plot_aimodel_varimps(sim_plotter.aimodel_plotdata), use_container_width=True, theme="streamlit"
plot_aimodel_varimps(sim_plotter.aimodel_plotdata),
use_container_width=True,
theme="streamlit",
)

canvas["aimodel_response"].plotly_chart(
plot_aimodel_response(sim_plotter.aimodel_plotdata), use_container_width=True, theme="streamlit"
plot_aimodel_response(sim_plotter.aimodel_plotdata),
use_container_width=True,
theme="streamlit",
)

last_ts = new_ts
Expand Down

0 comments on commit e055aef

Please sign in to comment.