Skip to content

Keep small metric values when saving Data to JSON or SQL storage - #5327

Open
sohailshk wants to merge 2 commits into
meta:mainfrom
sohailshk:fix/json-store-dataframe-precision
Open

sohailshk wants to merge 2 commits into
meta:mainfrom
sohailshk:fix/json-store-dataframe-precision

Conversation

@sohailshk

Copy link
Copy Markdown

Summary

object_to_json serialises every pd.DataFrame (the Data of each trial) with obj.to_json(). pandas' default double_precision is 10 decimal places, so a metric mean or SEM between about 1e-15 and 5e-11 is written as 0.0, and larger values silently lose digits (for example 3.456789e-9 reloads as 3.5e-9), on save_to_json_file / load_from_json_file. Encoder.data_to_sqa has the same to_json() call for SQL storage.

A lossless fix is already proposed in #1750 ("Store floats exactly in Pandas DataFrames"), which changes the stored format and has been idle since 2023. This PR is a small stopgap that keeps the current format and decoder.

Repro (Ax main, pandas 3.1.2):

import tempfile, os
from ax.api.client import Client
from ax.api.configs import RangeParameterConfig

c = Client(random_seed=0)
c.configure_experiment(name="t", parameters=[RangeParameterConfig(name="x", parameter_type="float", bounds=(0, 1))])
c.configure_optimization(objective="-loss")
idx, _ = next(iter(c.get_next_trials(max_trials=1).items()))
c.complete_trial(idx, raw_data={"loss": (1.23456789e-12, 3.3e-13)})
with tempfile.TemporaryDirectory() as d:
    f = os.path.join(d, "e.json")
    c.save_to_json_file(f)
    c2 = Client.load_from_json_file(f)
print(c._experiment.lookup_data().df[["metric_name", "mean", "sem"]].values.tolist())
print(c2._experiment.lookup_data().df[["metric_name", "mean", "sem"]].values.tolist())

main: [['loss', 1.23456789e-12, 3.3e-13]] then [['loss', 0.0, 0.0]].
With this change the reloaded values are 1.235e-12 / 3.3e-13 (15 decimal places, pandas' maximum), instead of 0.0.

Change

ax/storage/json_store/encoder.py and ax/storage/sqa_store/encoder.py (data_json): to_json(double_precision=15). The stored format is unchanged, so older files still decode. 15 decimals is pandas' upper limit, so this stays lossy: about half of random floats round trip bit-exactly (0.3% at 10 decimals), values around 1e-12 keep 3 to 4 digits, and values of order 1e-9 and above are exact in the cases I tried. It no longer zeroes values above 1e-15.

Test plan

  • New JSONStoreTest.test_encode_decode_data_keeps_small_metric_values (means 1.23e-12, 3.456789e-9, SEM 3.3e-13) and SQAStoreTest.test_data_round_trip_keeps_small_metric_values (data_to_sqa / data_from_sqa).
  • On main the new tests fail (0.0 != 1.23456789e-12).
  • With the change: pytest ax/storage/json_store: 59 passed. pytest ax/storage/sqa_store: 134 passed, 1 skipped, 1 failed (test_save_experiment_with_surrogate_as_model_kwarg; the installed botorch lacks fmin_l_bfgs_b_batched, same failure without this change).
  • Not run: the rest of the suite.

The implementation and tests were written with AI assistance.

The JSON encoder wrote DataFrames with pandas' default double_precision of
10 decimal places, so a metric mean/SEM below ~5e-11 was stored as 0.0 and
larger values lost digits on save_to_json_file / load_from_json_file. Use
pandas' maximum of 15.
Apply the same double_precision=15 to the SQA Data encoder and add a SQL
round trip test; extend the JSON test with a 3.456789e-9 value.
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant