Spaces:
Sleeping
Sleeping
fourth commit
Browse files- app.py +37 -1
- requirements.txt +3 -1
- scripts/markets.py +5 -3
- scripts/profitability.py +9 -7
- scripts/{weekly_analysis.py → pull_data.py} +5 -1
- scripts/tools.py +7 -3
- tabs/__pycache__/error.cpython-310.pyc +0 -0
- tabs/__pycache__/tool_win.cpython-310.pyc +0 -0
- tabs/__pycache__/trades.cpython-310.pyc +0 -0
- tabs/error.py +17 -11
- tabs/tool_win.py +12 -9
- tabs/trades.py +14 -7
app.py
CHANGED
@@ -1,5 +1,10 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
|
|
|
|
|
|
3 |
from tabs.trades import (
|
4 |
prepare_trades,
|
5 |
get_overall_trades,
|
@@ -22,7 +27,34 @@ from tabs.error import (
|
|
22 |
plot_week_error_data
|
23 |
)
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
trades_df = pd.read_csv("./data/all_trades_profitability.csv")
|
27 |
trades_df = prepare_trades(trades_df)
|
28 |
|
@@ -267,4 +299,8 @@ with demo:
|
|
267 |
with gr.Accordion("About the Benchmark"):
|
268 |
gr.Markdown("This app shows the actual performance of Olas Predict tools on the live market.")
|
269 |
|
|
|
|
|
|
|
|
|
270 |
demo.queue(default_concurrency_limit=40).launch()
|
|
|
1 |
+
import subprocess
|
2 |
+
import logging
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
6 |
+
from apscheduler.triggers.cron import CronTrigger
|
7 |
+
from pytz import utc
|
8 |
from tabs.trades import (
|
9 |
prepare_trades,
|
10 |
get_overall_trades,
|
|
|
27 |
plot_week_error_data
|
28 |
)
|
29 |
|
30 |
+
|
31 |
+
def refresh_data():
|
32 |
+
# Run the pull_data.py script and wait for it to finish
|
33 |
+
try:
|
34 |
+
result = subprocess.run(["python", "./scripts/pull_data.py"], check=True)
|
35 |
+
logging.info("Script executed successfully: %s", result)
|
36 |
+
except subprocess.CalledProcessError as e:
|
37 |
+
logging.error("Failed to run script: %s", e)
|
38 |
+
return # Stop execution if the script fails
|
39 |
+
|
40 |
+
# Reload dataframes
|
41 |
+
try:
|
42 |
+
global tools_df, trades_df, error_df, error_overall_df, winning_rate_df, winning_rate_overall_df, trades_count_df, trades_winning_rate_df
|
43 |
+
logging.info("Refreshing data...")
|
44 |
+
tools_df = pd.read_csv("./data/tools.csv", low_memory=False)
|
45 |
+
trades_df = pd.read_csv("./data/all_trades_profitability.csv")
|
46 |
+
trades_df = prepare_trades(trades_df)
|
47 |
+
error_df = get_error_data(tools_df=tools_df, inc_tools=INC_TOOLS)
|
48 |
+
error_overall_df = get_error_data_overall(error_df=error_df)
|
49 |
+
winning_rate_df = get_tool_winning_rate(tools_df=tools_df, inc_tools=INC_TOOLS)
|
50 |
+
winning_rate_overall_df = get_overall_winning_rate(wins_df=winning_rate_df)
|
51 |
+
trades_count_df = get_overall_trades(trades_df=trades_df)
|
52 |
+
trades_winning_rate_df = get_overall_winning_trades(trades_df=trades_df)
|
53 |
+
logging.info("Data refreshed.")
|
54 |
+
except Exception as e:
|
55 |
+
logging.error("Failed to refresh data: %s", e)
|
56 |
+
|
57 |
+
tools_df = pd.read_csv("./data/tools.csv", low_memory=False)
|
58 |
trades_df = pd.read_csv("./data/all_trades_profitability.csv")
|
59 |
trades_df = prepare_trades(trades_df)
|
60 |
|
|
|
299 |
with gr.Accordion("About the Benchmark"):
|
300 |
gr.Markdown("This app shows the actual performance of Olas Predict tools on the live market.")
|
301 |
|
302 |
+
# Create the scheduler
|
303 |
+
scheduler = BackgroundScheduler(timezone=utc)
|
304 |
+
scheduler.add_job(refresh_data, CronTrigger(hour=0, minute=0)) # Runs daily at 12 AM UTC
|
305 |
+
scheduler.start()
|
306 |
demo.queue(default_concurrency_limit=40).launch()
|
requirements.txt
CHANGED
@@ -4,4 +4,6 @@ huggingface-hub
|
|
4 |
pyarrow
|
5 |
web3
|
6 |
requests
|
7 |
-
gradio
|
|
|
|
|
|
4 |
pyarrow
|
5 |
web3
|
6 |
requests
|
7 |
+
gradio
|
8 |
+
apscheduler
|
9 |
+
pytz
|
scripts/markets.py
CHANGED
@@ -27,7 +27,7 @@ import requests
|
|
27 |
from tqdm import tqdm
|
28 |
|
29 |
from typing import List, Dict
|
30 |
-
|
31 |
|
32 |
ResponseItemType = List[Dict[str, str]]
|
33 |
SubgraphResponseType = Dict[str, ResponseItemType]
|
@@ -47,7 +47,9 @@ OUTCOMES_FIELD = "outcomes"
|
|
47 |
TITLE_FIELD = "title"
|
48 |
MAX_UINT_HEX = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
49 |
DEFAULT_FILENAME = "fpmms.csv"
|
50 |
-
|
|
|
|
|
51 |
FPMMS_QUERY = Template(
|
52 |
"""
|
53 |
{
|
@@ -216,7 +218,7 @@ def etl(filename: Optional[str] = None) -> pd.DataFrame:
|
|
216 |
fpmms = transform_fpmms(fpmms)
|
217 |
|
218 |
if filename:
|
219 |
-
fpmms.to_csv(filename, index=False)
|
220 |
|
221 |
return fpmms
|
222 |
|
|
|
27 |
from tqdm import tqdm
|
28 |
|
29 |
from typing import List, Dict
|
30 |
+
from pathlib import Path
|
31 |
|
32 |
ResponseItemType = List[Dict[str, str]]
|
33 |
SubgraphResponseType = Dict[str, ResponseItemType]
|
|
|
47 |
TITLE_FIELD = "title"
|
48 |
MAX_UINT_HEX = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
49 |
DEFAULT_FILENAME = "fpmms.csv"
|
50 |
+
SCRIPTS_DIR = Path(__file__).parent
|
51 |
+
ROOT_DIR = SCRIPTS_DIR.parent
|
52 |
+
DATA_DIR = ROOT_DIR / "data"
|
53 |
FPMMS_QUERY = Template(
|
54 |
"""
|
55 |
{
|
|
|
218 |
fpmms = transform_fpmms(fpmms)
|
219 |
|
220 |
if filename:
|
221 |
+
fpmms.to_csv(DATA_DIR / filename, index=False)
|
222 |
|
223 |
return fpmms
|
224 |
|
scripts/profitability.py
CHANGED
@@ -27,7 +27,7 @@ from string import Template
|
|
27 |
from enum import Enum
|
28 |
from tqdm import tqdm
|
29 |
import numpy as np
|
30 |
-
|
31 |
|
32 |
IRRELEVANT_TOOLS = [
|
33 |
"openai-text-davinci-002",
|
@@ -55,7 +55,9 @@ DEFAULT_TO_TIMESTAMP = 2147483647
|
|
55 |
WXDAI_CONTRACT_ADDRESS = "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d"
|
56 |
DEFAULT_MECH_FEE = 0.01
|
57 |
DUST_THRESHOLD = 10000000000000
|
58 |
-
|
|
|
|
|
59 |
|
60 |
class MarketState(Enum):
|
61 |
"""Market state"""
|
@@ -383,7 +385,7 @@ def create_fpmmTrades(rpc: str):
|
|
383 |
df.rename(columns={"creator": "trader_address"}, inplace=True)
|
384 |
|
385 |
# save to csv
|
386 |
-
df.to_csv("fpmmTrades.csv", index=False)
|
387 |
|
388 |
return df
|
389 |
|
@@ -418,8 +420,8 @@ def prepare_profitalibity_data(rpc: str):
|
|
418 |
except FileNotFoundError:
|
419 |
print("fpmmTrades.csv not found. Creating fpmmTrades.csv...")
|
420 |
fpmmTrades = create_fpmmTrades(rpc)
|
421 |
-
fpmmTrades.to_csv("fpmmTrades.csv", index=False)
|
422 |
-
fpmmTrades = pd.read_csv("fpmmTrades.csv")
|
423 |
|
424 |
# make sure trader_address is in the columns
|
425 |
assert "trader_address" in fpmmTrades.columns, "trader_address column not found"
|
@@ -618,8 +620,8 @@ def run_profitability_analysis(rpc):
|
|
618 |
summary_df = summary_analyse(all_trades_df)
|
619 |
|
620 |
# save to csv
|
621 |
-
all_trades_df.to_csv("all_trades_profitability.csv", index=False)
|
622 |
-
summary_df.to_csv("summary_profitability.csv", index=False)
|
623 |
|
624 |
print("Done!")
|
625 |
|
|
|
27 |
from enum import Enum
|
28 |
from tqdm import tqdm
|
29 |
import numpy as np
|
30 |
+
from pathlib import Path
|
31 |
|
32 |
IRRELEVANT_TOOLS = [
|
33 |
"openai-text-davinci-002",
|
|
|
55 |
WXDAI_CONTRACT_ADDRESS = "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d"
|
56 |
DEFAULT_MECH_FEE = 0.01
|
57 |
DUST_THRESHOLD = 10000000000000
|
58 |
+
SCRIPTS_DIR = Path(__file__).parent
|
59 |
+
ROOT_DIR = SCRIPTS_DIR.parent
|
60 |
+
DATA_DIR = ROOT_DIR / "data"
|
61 |
|
62 |
class MarketState(Enum):
|
63 |
"""Market state"""
|
|
|
385 |
df.rename(columns={"creator": "trader_address"}, inplace=True)
|
386 |
|
387 |
# save to csv
|
388 |
+
df.to_csv(DATA_DIR / "fpmmTrades.csv", index=False)
|
389 |
|
390 |
return df
|
391 |
|
|
|
420 |
except FileNotFoundError:
|
421 |
print("fpmmTrades.csv not found. Creating fpmmTrades.csv...")
|
422 |
fpmmTrades = create_fpmmTrades(rpc)
|
423 |
+
fpmmTrades.to_csv(DATA_DIR / "fpmmTrades.csv", index=False)
|
424 |
+
fpmmTrades = pd.read_csv(DATA_DIR / "fpmmTrades.csv")
|
425 |
|
426 |
# make sure trader_address is in the columns
|
427 |
assert "trader_address" in fpmmTrades.columns, "trader_address column not found"
|
|
|
620 |
summary_df = summary_analyse(all_trades_df)
|
621 |
|
622 |
# save to csv
|
623 |
+
all_trades_df.to_csv(DATA_DIR / "all_trades_profitability.csv", index=False)
|
624 |
+
summary_df.to_csv(DATA_DIR / "summary_profitability.csv", index=False)
|
625 |
|
626 |
print("Done!")
|
627 |
|
scripts/{weekly_analysis.py → pull_data.py}
RENAMED
@@ -7,6 +7,7 @@ from tqdm import tqdm
|
|
7 |
from web3 import Web3
|
8 |
from typing import Optional
|
9 |
import pandas as pd
|
|
|
10 |
from functools import partial
|
11 |
from markets import (
|
12 |
etl as mkt_etl,
|
@@ -21,6 +22,9 @@ from profitability import run_profitability_analysis
|
|
21 |
|
22 |
logging.basicConfig(level=logging.INFO)
|
23 |
|
|
|
|
|
|
|
24 |
|
25 |
def get_question(text: str) -> str:
|
26 |
"""Get the question from a text."""
|
@@ -109,7 +113,7 @@ def weekly_analysis():
|
|
109 |
tools['request_month_year_week'] = pd.to_datetime(tools['request_time']).dt.to_period('W').astype(str)
|
110 |
|
111 |
# Save the tools
|
112 |
-
tools.to_csv(TOOLS_FILENAME, index=False)
|
113 |
|
114 |
logging.info("Weekly analysis files generated and saved")
|
115 |
|
|
|
7 |
from web3 import Web3
|
8 |
from typing import Optional
|
9 |
import pandas as pd
|
10 |
+
from pathlib import Path
|
11 |
from functools import partial
|
12 |
from markets import (
|
13 |
etl as mkt_etl,
|
|
|
22 |
|
23 |
logging.basicConfig(level=logging.INFO)
|
24 |
|
25 |
+
SCRIPTS_DIR = Path(__file__).parent
|
26 |
+
ROOT_DIR = SCRIPTS_DIR.parent
|
27 |
+
DATA_DIR = ROOT_DIR / "data"
|
28 |
|
29 |
def get_question(text: str) -> str:
|
30 |
"""Get the question from a text."""
|
|
|
113 |
tools['request_month_year_week'] = pd.to_datetime(tools['request_time']).dt.to_period('W').astype(str)
|
114 |
|
115 |
# Save the tools
|
116 |
+
tools.to_csv(DATA_DIR / TOOLS_FILENAME, index=False)
|
117 |
|
118 |
logging.info("Weekly analysis files generated and saved")
|
119 |
|
scripts/tools.py
CHANGED
@@ -56,6 +56,8 @@ from web3 import Web3, HTTPProvider
|
|
56 |
from web3.exceptions import MismatchedABI
|
57 |
from web3.types import BlockParams
|
58 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
|
|
|
59 |
|
60 |
CONTRACTS_PATH = "contracts"
|
61 |
MECH_TO_INFO = {
|
@@ -111,7 +113,9 @@ IRRELEVANT_TOOLS = [
|
|
111 |
SNAPSHOT_RATE = 10
|
112 |
NUM_WORKERS = 10
|
113 |
GET_CONTENTS_BATCH_SIZE = 1000
|
114 |
-
|
|
|
|
|
115 |
|
116 |
class MechEventName(Enum):
|
117 |
"""The mech's event names."""
|
@@ -621,13 +625,13 @@ def store_progress(
|
|
621 |
if "result" in content.columns:
|
622 |
content.drop(columns=["result"], inplace=True)
|
623 |
|
624 |
-
content.to_csv(event_filename, index=False, escapechar="\\")
|
625 |
|
626 |
# drop result and error columns
|
627 |
if "result" in tools.columns:
|
628 |
tools.drop(columns=["result"], inplace=True)
|
629 |
|
630 |
-
tools.to_csv(filename, index=False, escapechar="\\")
|
631 |
|
632 |
|
633 |
def etl(
|
|
|
56 |
from web3.exceptions import MismatchedABI
|
57 |
from web3.types import BlockParams
|
58 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
59 |
+
from pathlib import Path
|
60 |
+
|
61 |
|
62 |
CONTRACTS_PATH = "contracts"
|
63 |
MECH_TO_INFO = {
|
|
|
113 |
SNAPSHOT_RATE = 10
|
114 |
NUM_WORKERS = 10
|
115 |
GET_CONTENTS_BATCH_SIZE = 1000
|
116 |
+
SCRIPTS_DIR = Path(__file__).parent
|
117 |
+
ROOT_DIR = SCRIPTS_DIR.parent
|
118 |
+
DATA_DIR = ROOT_DIR / "data"
|
119 |
|
120 |
class MechEventName(Enum):
|
121 |
"""The mech's event names."""
|
|
|
625 |
if "result" in content.columns:
|
626 |
content.drop(columns=["result"], inplace=True)
|
627 |
|
628 |
+
content.to_csv(DATA_DIR / event_filename, index=False, escapechar="\\")
|
629 |
|
630 |
# drop result and error columns
|
631 |
if "result" in tools.columns:
|
632 |
tools.drop(columns=["result"], inplace=True)
|
633 |
|
634 |
+
tools.to_csv(DATA_DIR / filename, index=False, escapechar="\\")
|
635 |
|
636 |
|
637 |
def etl(
|
tabs/__pycache__/error.cpython-310.pyc
CHANGED
Binary files a/tabs/__pycache__/error.cpython-310.pyc and b/tabs/__pycache__/error.cpython-310.pyc differ
|
|
tabs/__pycache__/tool_win.cpython-310.pyc
CHANGED
Binary files a/tabs/__pycache__/tool_win.cpython-310.pyc and b/tabs/__pycache__/tool_win.cpython-310.pyc differ
|
|
tabs/__pycache__/trades.cpython-310.pyc
CHANGED
Binary files a/tabs/__pycache__/trades.cpython-310.pyc and b/tabs/__pycache__/trades.cpython-310.pyc differ
|
|
tabs/error.py
CHANGED
@@ -1,33 +1,38 @@
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
|
|
3 |
|
4 |
|
5 |
HEIGHT=600
|
6 |
WIDTH=1000
|
7 |
|
8 |
-
def set_error(row):
|
|
|
9 |
if row.error not in [True, False]:
|
10 |
if not row.prompt_response:
|
11 |
return True
|
12 |
return False
|
13 |
return row.error
|
14 |
|
15 |
-
def get_error_data(tools_df, inc_tools):
|
16 |
-
|
|
|
17 |
tools_inc['error'] = tools_inc.apply(set_error, axis=1)
|
18 |
error = tools_inc.groupby(['tool', 'request_month_year_week', 'error']).size().unstack().fillna(0).reset_index()
|
19 |
error['error_perc'] = (error[True] / (error[False] + error[True])) * 100
|
20 |
error['total_requests'] = error[False] + error[True]
|
21 |
return error
|
22 |
|
23 |
-
def get_error_data_overall(error_df):
|
|
|
24 |
error_total = error_df.groupby('request_month_year_week').agg({'total_requests': 'sum', False: 'sum', True: 'sum'}).reset_index()
|
25 |
error_total['error_perc'] = (error_total[True] / error_total['total_requests']) * 100
|
26 |
error_total.columns = error_total.columns.astype(str)
|
27 |
error_total['error_perc'] = error_total['error_perc'].apply(lambda x: round(x, 4))
|
28 |
return error_total
|
29 |
|
30 |
-
def plot_error_data(error_all_df):
|
|
|
31 |
return gr.BarPlot(
|
32 |
value=error_all_df,
|
33 |
x="request_month_year_week",
|
@@ -43,8 +48,9 @@ def plot_error_data(error_all_df):
|
|
43 |
width=WIDTH
|
44 |
)
|
45 |
|
46 |
-
def plot_tool_error_data(error_df, tool):
|
47 |
-
|
|
|
48 |
error_tool.columns = error_tool.columns.astype(str)
|
49 |
error_tool['error_perc'] = error_tool['error_perc'].apply(lambda x: round(x, 4))
|
50 |
|
@@ -63,8 +69,9 @@ def plot_tool_error_data(error_df, tool):
|
|
63 |
width=WIDTH
|
64 |
)
|
65 |
|
66 |
-
def plot_week_error_data(error_df, week):
|
67 |
-
|
|
|
68 |
error_week.columns = error_week.columns.astype(str)
|
69 |
error_week['error_perc'] = error_week['error_perc'].apply(lambda x: round(x, 4))
|
70 |
return gr.BarPlot(
|
@@ -80,5 +87,4 @@ def plot_week_error_data(error_df, week):
|
|
80 |
tooltip=["tool", "error_perc"],
|
81 |
height=HEIGHT,
|
82 |
width=WIDTH
|
83 |
-
)
|
84 |
-
|
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
+
from typing import List
|
4 |
|
5 |
|
6 |
HEIGHT=600
|
7 |
WIDTH=1000
|
8 |
|
9 |
+
def set_error(row: pd.Series) -> bool:
|
10 |
+
"""Sets the error for the given row."""
|
11 |
if row.error not in [True, False]:
|
12 |
if not row.prompt_response:
|
13 |
return True
|
14 |
return False
|
15 |
return row.error
|
16 |
|
17 |
+
def get_error_data(tools_df: pd.DataFrame, inc_tools: List[str]) -> pd.DataFrame:
|
18 |
+
"""Gets the error data for the given tools and calculates the error percentage."""
|
19 |
+
tools_inc = tools_df[tools_df['tool'].isin(inc_tools)].copy()
|
20 |
tools_inc['error'] = tools_inc.apply(set_error, axis=1)
|
21 |
error = tools_inc.groupby(['tool', 'request_month_year_week', 'error']).size().unstack().fillna(0).reset_index()
|
22 |
error['error_perc'] = (error[True] / (error[False] + error[True])) * 100
|
23 |
error['total_requests'] = error[False] + error[True]
|
24 |
return error
|
25 |
|
26 |
+
def get_error_data_overall(error_df: pd.DataFrame) -> pd.DataFrame:
|
27 |
+
"""Gets the error data for the given tools and calculates the error percentage."""
|
28 |
error_total = error_df.groupby('request_month_year_week').agg({'total_requests': 'sum', False: 'sum', True: 'sum'}).reset_index()
|
29 |
error_total['error_perc'] = (error_total[True] / error_total['total_requests']) * 100
|
30 |
error_total.columns = error_total.columns.astype(str)
|
31 |
error_total['error_perc'] = error_total['error_perc'].apply(lambda x: round(x, 4))
|
32 |
return error_total
|
33 |
|
34 |
+
def plot_error_data(error_all_df: pd.DataFrame) -> gr.BarPlot:
|
35 |
+
"""Plots the error data for the given tools and calculates the error percentage."""
|
36 |
return gr.BarPlot(
|
37 |
value=error_all_df,
|
38 |
x="request_month_year_week",
|
|
|
48 |
width=WIDTH
|
49 |
)
|
50 |
|
51 |
+
def plot_tool_error_data(error_df: pd.DataFrame, tool: str) -> gr.BarPlot:
|
52 |
+
"""Plots the error data for the given tool."""
|
53 |
+
error_tool = error_df[error_df['tool'] == tool].copy()
|
54 |
error_tool.columns = error_tool.columns.astype(str)
|
55 |
error_tool['error_perc'] = error_tool['error_perc'].apply(lambda x: round(x, 4))
|
56 |
|
|
|
69 |
width=WIDTH
|
70 |
)
|
71 |
|
72 |
+
def plot_week_error_data(error_df: pd.DataFrame, week: str) -> gr.BarPlot:
|
73 |
+
"""Plots the error data for the given week."""
|
74 |
+
error_week = error_df[error_df['request_month_year_week'] == week].copy()
|
75 |
error_week.columns = error_week.columns.astype(str)
|
76 |
error_week['error_perc'] = error_week['error_perc'].apply(lambda x: round(x, 4))
|
77 |
return gr.BarPlot(
|
|
|
87 |
tooltip=["tool", "error_perc"],
|
88 |
height=HEIGHT,
|
89 |
width=WIDTH
|
90 |
+
)
|
|
tabs/tool_win.py
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
|
|
3 |
|
4 |
|
5 |
HEIGHT=600
|
6 |
WIDTH=1000
|
7 |
|
8 |
|
9 |
-
def set_error(row):
|
|
|
10 |
if row.error not in [True, False]:
|
11 |
if not row.prompt_response:
|
12 |
return True
|
@@ -14,7 +16,8 @@ def set_error(row):
|
|
14 |
return row.error
|
15 |
|
16 |
|
17 |
-
def get_tool_winning_rate(tools_df, inc_tools):
|
|
|
18 |
tools_inc = tools_df[tools_df['tool'].isin(inc_tools)].copy()
|
19 |
tools_inc['error'] = tools_inc.apply(set_error, axis=1)
|
20 |
tools_non_error = tools_inc[tools_inc['error'] != True]
|
@@ -33,7 +36,8 @@ def get_tool_winning_rate(tools_df, inc_tools):
|
|
33 |
return wins
|
34 |
|
35 |
|
36 |
-
def get_overall_winning_rate(wins_df):
|
|
|
37 |
overall_wins = wins_df.groupby('request_month_year_week').agg({
|
38 |
"0": 'sum',
|
39 |
"1": 'sum',
|
@@ -43,10 +47,8 @@ def get_overall_winning_rate(wins_df):
|
|
43 |
return overall_wins
|
44 |
|
45 |
|
46 |
-
def plot_tool_winnings_overall(
|
47 |
-
|
48 |
-
winning_selector="win_perc"
|
49 |
-
):
|
50 |
return gr.BarPlot(
|
51 |
title="Winning Rate",
|
52 |
x_title="Date",
|
@@ -63,7 +65,8 @@ def plot_tool_winnings_overall(
|
|
63 |
)
|
64 |
|
65 |
|
66 |
-
def plot_tool_winnings_by_tool(wins_df, tool):
|
|
|
67 |
return gr.BarPlot(
|
68 |
title="Winning Rate",
|
69 |
x_title="Week",
|
@@ -77,4 +80,4 @@ def plot_tool_winnings_by_tool(wins_df, tool):
|
|
77 |
tooltip=["request_month_year_week", "win_perc"],
|
78 |
height=HEIGHT,
|
79 |
width=WIDTH
|
80 |
-
)
|
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
+
from typing import List
|
4 |
|
5 |
|
6 |
HEIGHT=600
|
7 |
WIDTH=1000
|
8 |
|
9 |
|
10 |
+
def set_error(row: pd.Series) -> bool:
|
11 |
+
"""Sets the error for the given row."""
|
12 |
if row.error not in [True, False]:
|
13 |
if not row.prompt_response:
|
14 |
return True
|
|
|
16 |
return row.error
|
17 |
|
18 |
|
19 |
+
def get_tool_winning_rate(tools_df: pd.DataFrame, inc_tools: List[str]) -> pd.DataFrame:
|
20 |
+
"""Gets the tool winning rate data for the given tools and calculates the winning percentage."""
|
21 |
tools_inc = tools_df[tools_df['tool'].isin(inc_tools)].copy()
|
22 |
tools_inc['error'] = tools_inc.apply(set_error, axis=1)
|
23 |
tools_non_error = tools_inc[tools_inc['error'] != True]
|
|
|
36 |
return wins
|
37 |
|
38 |
|
39 |
+
def get_overall_winning_rate(wins_df: pd.DataFrame) -> pd.DataFrame:
|
40 |
+
"""Gets the overall winning rate data for the given tools and calculates the winning percentage."""
|
41 |
overall_wins = wins_df.groupby('request_month_year_week').agg({
|
42 |
"0": 'sum',
|
43 |
"1": 'sum',
|
|
|
47 |
return overall_wins
|
48 |
|
49 |
|
50 |
+
def plot_tool_winnings_overall(wins_df: pd.DataFrame, winning_selector: str = "win_perc") -> gr.BarPlot:
|
51 |
+
"""Plots the overall winning rate data for the given tools and calculates the winning percentage."""
|
|
|
|
|
52 |
return gr.BarPlot(
|
53 |
title="Winning Rate",
|
54 |
x_title="Date",
|
|
|
65 |
)
|
66 |
|
67 |
|
68 |
+
def plot_tool_winnings_by_tool(wins_df: pd.DataFrame, tool: str) -> gr.BarPlot:
|
69 |
+
"""Plots the winning rate data for the given tool."""
|
70 |
return gr.BarPlot(
|
71 |
title="Winning Rate",
|
72 |
x_title="Week",
|
|
|
80 |
tooltip=["request_month_year_week", "win_perc"],
|
81 |
height=HEIGHT,
|
82 |
width=WIDTH
|
83 |
+
)
|
tabs/trades.py
CHANGED
@@ -5,21 +5,25 @@ import pandas as pd
|
|
5 |
HEIGHT=600
|
6 |
WIDTH=1000
|
7 |
|
8 |
-
def prepare_trades(trades_df):
|
|
|
9 |
trades_df['creation_timestamp'] = pd.to_datetime(trades_df['creation_timestamp'])
|
|
|
10 |
trades_df['month_year'] = trades_df['creation_timestamp'].dt.to_period('M').astype(str)
|
11 |
trades_df['month_year_week'] = trades_df['creation_timestamp'].dt.to_period('W').astype(str)
|
12 |
trades_df['winning_trade'] = trades_df['winning_trade'].astype(int)
|
13 |
return trades_df
|
14 |
|
15 |
|
16 |
-
def get_overall_trades(trades_df):
|
|
|
17 |
trades_count = trades_df.groupby('month_year_week').size().reset_index()
|
18 |
trades_count.columns = trades_count.columns.astype(str)
|
19 |
trades_count.rename(columns={'0': 'trades'}, inplace=True)
|
20 |
return trades_count
|
21 |
|
22 |
-
def get_overall_winning_trades(trades_df):
|
|
|
23 |
winning_trades = trades_df.groupby(['month_year_week'])['winning_trade'].sum() / trades_df.groupby(['month_year_week'])['winning_trade'].count() * 100
|
24 |
# winning_trades is a series, give it a dataframe
|
25 |
winning_trades = winning_trades.reset_index()
|
@@ -27,7 +31,8 @@ def get_overall_winning_trades(trades_df):
|
|
27 |
winning_trades.columns = ['month_year_week', 'winning_trade']
|
28 |
return winning_trades
|
29 |
|
30 |
-
def plot_trade_details(trade_detail, trades_df):
|
|
|
31 |
if trade_detail == "mech calls":
|
32 |
# this is to filter out the data before 2023-09-01
|
33 |
trades_filtered = trades_df[trades_df["creation_timestamp"] >"2023-09-01"]
|
@@ -164,7 +169,8 @@ def plot_trade_details(trade_detail, trades_df):
|
|
164 |
width=WIDTH
|
165 |
)
|
166 |
|
167 |
-
def plot_trades_by_week(trades_df):
|
|
|
168 |
return gr.BarPlot(
|
169 |
value=trades_df,
|
170 |
x="month_year_week",
|
@@ -177,7 +183,8 @@ def plot_trades_by_week(trades_df):
|
|
177 |
width=WIDTH
|
178 |
)
|
179 |
|
180 |
-
def plot_winning_trades_by_week(trades_df):
|
|
|
181 |
return gr.BarPlot(
|
182 |
value=trades_df,
|
183 |
x="month_year_week",
|
@@ -188,4 +195,4 @@ def plot_winning_trades_by_week(trades_df):
|
|
188 |
tooltip=["month_year_week", "winning_trade"],
|
189 |
height=HEIGHT,
|
190 |
width=WIDTH
|
191 |
-
)
|
|
|
5 |
HEIGHT=600
|
6 |
WIDTH=1000
|
7 |
|
8 |
+
def prepare_trades(trades_df: pd.DataFrame) -> pd.DataFrame:
|
9 |
+
"""Prepares the trades data for analysis."""
|
10 |
trades_df['creation_timestamp'] = pd.to_datetime(trades_df['creation_timestamp'])
|
11 |
+
trades_df['creation_timestamp'] = trades_df['creation_timestamp'].dt.tz_convert('UTC')
|
12 |
trades_df['month_year'] = trades_df['creation_timestamp'].dt.to_period('M').astype(str)
|
13 |
trades_df['month_year_week'] = trades_df['creation_timestamp'].dt.to_period('W').astype(str)
|
14 |
trades_df['winning_trade'] = trades_df['winning_trade'].astype(int)
|
15 |
return trades_df
|
16 |
|
17 |
|
18 |
+
def get_overall_trades(trades_df: pd.DataFrame) -> pd.DataFrame:
|
19 |
+
"""Gets the overall trades data for the given tools and calculates the winning percentage."""
|
20 |
trades_count = trades_df.groupby('month_year_week').size().reset_index()
|
21 |
trades_count.columns = trades_count.columns.astype(str)
|
22 |
trades_count.rename(columns={'0': 'trades'}, inplace=True)
|
23 |
return trades_count
|
24 |
|
25 |
+
def get_overall_winning_trades(trades_df: pd.DataFrame) -> pd.DataFrame:
|
26 |
+
"""Gets the overall winning trades data for the given tools and calculates the winning percentage."""
|
27 |
winning_trades = trades_df.groupby(['month_year_week'])['winning_trade'].sum() / trades_df.groupby(['month_year_week'])['winning_trade'].count() * 100
|
28 |
# winning_trades is a series, give it a dataframe
|
29 |
winning_trades = winning_trades.reset_index()
|
|
|
31 |
winning_trades.columns = ['month_year_week', 'winning_trade']
|
32 |
return winning_trades
|
33 |
|
34 |
+
def plot_trade_details(trade_detail: str, trades_df: pd.DataFrame) -> gr.LinePlot:
|
35 |
+
"""Plots the trade details for the given trade detail."""
|
36 |
if trade_detail == "mech calls":
|
37 |
# this is to filter out the data before 2023-09-01
|
38 |
trades_filtered = trades_df[trades_df["creation_timestamp"] >"2023-09-01"]
|
|
|
169 |
width=WIDTH
|
170 |
)
|
171 |
|
172 |
+
def plot_trades_by_week(trades_df: pd.DataFrame) -> gr.BarPlot:
|
173 |
+
"""Plots the trades data for the given tools and calculates the winning percentage."""
|
174 |
return gr.BarPlot(
|
175 |
value=trades_df,
|
176 |
x="month_year_week",
|
|
|
183 |
width=WIDTH
|
184 |
)
|
185 |
|
186 |
+
def plot_winning_trades_by_week(trades_df: pd.DataFrame) -> gr.BarPlot:
|
187 |
+
"""Plots the winning trades data for the given tools and calculates the winning percentage."""
|
188 |
return gr.BarPlot(
|
189 |
value=trades_df,
|
190 |
x="month_year_week",
|
|
|
195 |
tooltip=["month_year_week", "winning_trade"],
|
196 |
height=HEIGHT,
|
197 |
width=WIDTH
|
198 |
+
)
|