Skip to content

Commit 820190b

Browse files
Thykofdavidevdt
andauthored
Fix gold score on weekends (#188)
* Revert "fix tests" This reverts commit 20a2fac. * _transform_data adds nan * new data transform * fix unit tests * remove full_fill_real_prices * fix compare isnan to store real path in db --------- Co-authored-by: davidevdt <[email protected]>
1 parent 2bb7eb6 commit 820190b

File tree

5 files changed

+153
-134
lines changed

5 files changed

+153
-134
lines changed

synth/utils/helpers.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
from typing import Optional, Any
1+
from typing import Optional
22
from datetime import datetime, timedelta, timezone
33

44

5-
import numpy as np
6-
7-
85
def get_current_time() -> datetime:
96
# Get current date and time
107
return datetime.now(timezone.utc).replace(microsecond=0)
@@ -71,39 +68,6 @@ def adjust_predictions(predictions: list) -> list:
7168
return predictions[2:]
7269

7370

74-
def full_fill_real_prices(
75-
prediction: list[dict[str, Any]], real_prices: list[dict[str, Any]]
76-
) -> list[dict[str, Any]]:
77-
"""
78-
Fills missing real prices in the prediction with None.
79-
80-
:param prediction: List of dictionaries with 'time' and 'price' keys.
81-
:param real_prices: List of dictionaries with 'time' and 'price' keys.
82-
:return: List of dictionaries with filled prices.
83-
"""
84-
# transform real_prices into a dictionary for fast lookup
85-
real_prices_dict = {}
86-
for idx, entry in enumerate(real_prices):
87-
real_prices_dict[idx] = entry
88-
89-
# fill missing times and prices in the real_prices_dict
90-
for idx, entry in enumerate(prediction):
91-
if (
92-
idx not in real_prices_dict
93-
or real_prices_dict[idx] is None
94-
or np.isnan(real_prices_dict[idx])
95-
or not np.isfinite(real_prices_dict[idx])
96-
):
97-
real_prices_dict[idx] = np.nan
98-
99-
real_prices_filled = []
100-
# recreate the real_prices list of dict sorted by time
101-
for time in sorted(real_prices_dict.keys()):
102-
real_prices_filled.append(real_prices_dict[time])
103-
104-
return real_prices_filled
105-
106-
10771
def get_intersecting_arrays(array1, array2):
10872
"""
10973
Filters two arrays of dictionaries, keeping only entries that intersect by 'time'.

synth/validator/miner_data_handler.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import typing
55
import logging
6+
import math
67

78

89
import bittensor as bt
@@ -178,10 +179,14 @@ def set_miner_scores(
178179
with connection.begin():
179180
# update validator request with the real paths
180181
if real_prices is not None and len(real_prices) > 0:
181-
# replace np.nan with None
182-
for price in real_prices:
183-
if price is not None and pd.isna(price):
184-
price = None
182+
real_prices = [
183+
(
184+
None
185+
if (isinstance(x, float) and math.isnan(x))
186+
else x
187+
)
188+
for x in real_prices
189+
]
185190
update_stmt_validator = (
186191
update(ValidatorRequest)
187192
.where(

synth/validator/price_data_provider.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
stop_after_attempt,
99
wait_random_exponential,
1010
)
11+
import numpy as np
1112
import bittensor as bt
1213

1314

@@ -62,32 +63,45 @@ def fetch_data(self, validator_request: ValidatorRequest) -> list[dict]:
6263
data = response.json()
6364

6465
transformed_data = self._transform_data(
65-
data, start_time_int, validator_request.time_increment
66+
data,
67+
start_time_int,
68+
validator_request.time_increment,
69+
validator_request.time_length,
6670
)
6771

6872
return transformed_data
6973

7074
@staticmethod
7175
def _transform_data(
72-
data, start_time_int: int, time_increment: int
76+
data, start_time_int: int, time_increment: int, time_length: int
7377
) -> list:
74-
if data is None or len(data) == 0:
78+
if data is None or len(data) == 0 or len(data["t"]) == 0:
7579
return []
7680

77-
timestamps = data["t"]
78-
close_prices = data["c"]
79-
80-
transformed_data = []
81-
82-
if len(timestamps) == 0:
83-
return []
84-
85-
for t, c in zip(timestamps, close_prices):
86-
if (
87-
t >= start_time_int
88-
and (t - start_time_int) % time_increment == 0
89-
):
90-
transformed_data.append(float(c))
81+
time_end_int = start_time_int + time_length
82+
timestamps = [
83+
t
84+
for t in range(
85+
start_time_int, time_end_int + time_increment, time_increment
86+
)
87+
]
88+
89+
if len(timestamps) != int(time_length / time_increment) + 1:
90+
# Note: this part of code should never be activated; just included for precaution
91+
if len(timestamps) == int(time_length / time_increment) + 2:
92+
if data["t"][-1] < timestamps[1]:
93+
timestamps = timestamps[:-1]
94+
elif data["t"][0] > timestamps[0]:
95+
timestamps = timestamps[1:]
96+
else:
97+
return []
98+
99+
close_prices_dict = {t: c for t, c in zip(data["t"], data["c"])}
100+
transformed_data = [np.nan for _ in range(len(timestamps))]
101+
102+
for idx, t in enumerate(timestamps):
103+
if t in close_prices_dict:
104+
transformed_data[idx] = close_prices_dict[t]
91105

92106
return transformed_data
93107

synth/validator/reward.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
from synth.db.models import ValidatorRequest
29-
from synth.utils.helpers import adjust_predictions, full_fill_real_prices
29+
from synth.utils.helpers import adjust_predictions
3030
from synth.validator.crps_calculation import calculate_crps_for_miner
3131
from synth.validator.miner_data_handler import MinerDataHandler
3232
from synth.validator.price_data_provider import PriceDataProvider
@@ -64,14 +64,11 @@ def reward(
6464

6565
predictions_path = adjust_predictions(miner_prediction.prediction)
6666
simulation_runs = np.array(predictions_path).astype(float)
67-
full_filled_real_prices = full_fill_real_prices(
68-
miner_prediction.prediction[2], real_prices
69-
)
7067

7168
try:
7269
score, detailed_crps_data = calculate_crps_for_miner(
7370
simulation_runs,
74-
np.array(full_filled_real_prices),
71+
np.array(real_prices),
7572
time_increment,
7673
)
7774
except Exception as e:

0 commit comments

Comments
 (0)