Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ax/storage/json_store/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def object_to_json(
"value": datetime.datetime.strftime(obj, "%Y-%m-%d %H:%M:%S.%f"),
}
elif _type is pd.DataFrame:
return {"__type": _type.__name__, "value": obj.to_json()}
# pandas' default double_precision is 10 decimal places, which turns
# small floats (e.g. a metric mean of 1e-12) into 0.0; 15 is its maximum.
return {"__type": _type.__name__, "value": obj.to_json(double_precision=15)}
elif issubclass(_type, enum.Enum):
return {"__type": _type.__name__, "name": obj.name}
elif _type is np.ndarray or issubclass(_type, np.ndarray):
Expand Down
34 changes: 34 additions & 0 deletions ax/storage/json_store/tests/test_json_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,40 @@ def test_decode_data_backward_compatible(self) -> None:
)
self.assertIsInstance(data, Data)

def test_encode_decode_data_keeps_small_metric_values(self) -> None:
# pandas' default ``double_precision`` of 10 decimal places turned
# small means / SEMs into 0.0 and dropped digits of larger ones.
df = pd.DataFrame(
{
"trial_index": [0, 0, 0],
"arm_name": ["0_0", "0_0", "0_0"],
"metric_name": ["tiny", "regular", "nano"],
"metric_signature": ["tiny", "regular", "nano"],
"mean": [1.23456789e-12, 0.123456789012, 3.456789e-9],
"sem": [3.3e-13, 0.5, 0.5],
}
)
data_json = object_to_json(
Data(df=df),
encoder_registry=CORE_ENCODER_REGISTRY,
class_encoder_registry=CORE_CLASS_ENCODER_REGISTRY,
)
loaded = object_from_json(
data_json,
decoder_registry=CORE_DECODER_REGISTRY,
class_decoder_registry=CORE_CLASS_DECODER_REGISTRY,
)
self.assertIsInstance(loaded, Data)
loaded_df = loaded.df.set_index("metric_name")
self.assertAlmostEqual(
loaded_df.loc["tiny", "mean"], 1.23456789e-12, delta=1e-15
)
self.assertAlmostEqual(loaded_df.loc["nano", "mean"], 3.456789e-9, delta=1e-15)
self.assertAlmostEqual(loaded_df.loc["tiny", "sem"], 3.3e-13, delta=1e-15)
self.assertAlmostEqual(
loaded_df.loc["regular", "mean"], 0.123456789012, delta=1e-14
)

def test_EncodeDecodeNumpy(self) -> None:
arr = np.array([[1, 2, 3], [4, 5, 6]])
self.assertTrue(
Expand Down
4 changes: 3 additions & 1 deletion ax/storage/sqa_store/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,9 @@ def data_to_sqa(
# pyre-fixme[29]: `SQAData` is not a function.
return data_class(
id=data.db_id,
data_json=data.full_df.to_json(),
# pandas' default double_precision (10 decimals) zeroes small floats;
# 15 is its maximum.
data_json=data.full_df.to_json(double_precision=15),
time_created=timestamp,
trial_index=trial_index,
structure_metadata_json=json.dumps(
Expand Down
20 changes: 20 additions & 0 deletions ax/storage/sqa_store/tests/test_sqa_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
session_context,
session_scope,
)
from ax.core.data import Data
from ax.storage.sqa_store.decoder import Decoder
from ax.storage.sqa_store.delete import delete_experiment, delete_generation_strategy
from ax.storage.sqa_store.encoder import Encoder
Expand Down Expand Up @@ -210,6 +211,25 @@ def setUp(self) -> None:
get_range_parameter2(), # x
]

def test_data_round_trip_keeps_small_metric_values(self) -> None:
# pandas' default ``double_precision`` of 10 decimals zeroed small values.
df = pd.DataFrame(
{
"trial_index": [0, 0],
"arm_name": ["0_0", "0_0"],
"metric_name": ["tiny", "nano"],
"metric_signature": ["tiny", "nano"],
"mean": [1.23456789e-12, 3.456789e-9],
"sem": [3.3e-13, 0.5],
}
)
config = SQAConfig()
sqa_data = Encoder(config).data_to_sqa(Data(df=df), trial_index=0, timestamp=0)
loaded = Decoder(config).data_from_sqa(sqa_data).df.set_index("metric_name")
self.assertAlmostEqual(loaded.loc["tiny", "mean"], 1.23456789e-12, delta=1e-15)
self.assertAlmostEqual(loaded.loc["nano", "mean"], 3.456789e-9, delta=1e-15)
self.assertAlmostEqual(loaded.loc["tiny", "sem"], 3.3e-13, delta=1e-15)

def test_creation_of_test_db(self) -> None:
init_test_engine_and_session_factory(tier_or_path=":memory:", force_init=True)
engine = get_engine()
Expand Down