Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit JSON print to 10 significant figures #86

Merged
merged 5 commits into from
Aug 2, 2024
Merged
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
43 changes: 41 additions & 2 deletions cfspopcon/file_io.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""Functions for saving results to file and loading those files."""

import json
import sys
from pathlib import Path
from typing import Any, Literal

if sys.version_info >= (3, 11, 0):
from typing import Self # type:ignore[attr-defined,unused-ignore]
else:
from typing_extensions import Self # type:ignore[attr-defined,unused-ignore]

import numpy as np
import xarray as xr

@@ -91,6 +97,38 @@ def read_dataset_from_netcdf(filepath: Path) -> xr.Dataset:
return dataset


# These following lines are needed to modify the representation of floats
# in the JSON file. This is needed, because otherwise small errors in the
# floats lead to a large, meaningless diff of the reference JSON files.


class _RoundingFloat(float):
"""A modified version of the 'float' built-in, with a modified __repr__.
This is needed because `iterencode` directly uses `float.__repr__` in `floatstr`.
From: https://stackoverflow.com/questions/54370322/how-to-limit-the-number-of-float-digits-jsonencoder-produces
"""

__repr__ = staticmethod(lambda x: f"{x:#.10g}") # type:ignore[assignment,unused-ignore]


class _ModifyJSONFloatRepr:
"""A ContextManager to locally modify the representation of floats."""

def __enter__(self) -> Self:
"""Change the float representation to fixed precision on entry."""
self.c_make_encoder = json.encoder.c_make_encoder # type:ignore[attr-defined]
json.encoder.c_make_encoder = None # type:ignore[attr-defined]
json.encoder.float = _RoundingFloat # type:ignore[attr-defined]
return self

def __exit__(self, *args: Any) -> None:
"""Change the float representation back to the default."""
json.encoder.c_make_encoder = self.c_make_encoder # type:ignore[attr-defined]
json.encoder.float = float # type:ignore[attr-defined]


def write_point_to_file(dataset: xr.Dataset, point_key: str, point_params: dict, output_dir: Path) -> None:
"""Write the analysis values at the named points to a json file."""
mask = build_mask_from_dict(dataset, point_params)
@@ -121,5 +159,6 @@ def write_point_to_file(dataset: xr.Dataset, point_key: str, point_params: dict,

output_dir.mkdir(parents=True, exist_ok=True)

with open(output_dir / f"{point_key}.json", "w") as file:
json.dump(point.to_dict(), file, indent=4, sort_keys=True)
with _ModifyJSONFloatRepr():
with open(output_dir / f"{point_key}.json", "w") as file:
json.dump(point.to_dict(), file, indent=4, sort_keys=True)
62 changes: 31 additions & 31 deletions tests/regression_results/AUG_SepOS_minTe.json
Original file line number Diff line number Diff line change
@@ -3,12 +3,12 @@
"coords": {
"dim_separatrix_electron_density": {
"attrs": {},
"data": 1.443846153846154,
"data": 1.443846154,
"dims": []
},
"dim_separatrix_electron_temp": {
"attrs": {},
"data": 57.51724137931035,
"data": 57.51724138,
"dims": []
}
},
@@ -17,203 +17,203 @@
"attrs": {
"units": "tesla"
},
"data": 0.3265306124226533,
"data": 0.3265306124,
"dims": []
},
"B_t_out_mid": {
"attrs": {
"units": "tesla"
},
"data": 1.927570093457944,
"data": 1.927570093,
"dims": []
},
"SepOS_LH_transition": {
"attrs": {
"units": "dimensionless"
},
"data": 1.0025081265471087,
"data": 1.002508127,
"dims": []
},
"SepOS_MHD_limit": {
"attrs": {
"units": "dimensionless"
},
"data": 0.3560483425183738,
"data": 0.3560483425,
"dims": []
},
"SepOS_density_limit": {
"attrs": {
"units": "dimensionless"
},
"data": 0.5502074424382402,
"data": 0.5502074424,
"dims": []
},
"alpha_t": {
"attrs": {
"units": "dimensionless"
},
"data": 0.5195258259822426,
"data": 0.5195258260,
"dims": []
},
"areal_elongation": {
"attrs": {
"units": "dimensionless"
},
"data": 1.64,
"data": 1.640000000,
"dims": []
},
"average_ion_mass": {
"attrs": {
"units": "unified_atomic_mass_unit"
},
"data": 2.0,
"data": 2.000000000,
"dims": []
},
"critical_alpha_MHD": {
"attrs": {
"units": "dimensionless"
},
"data": 2.5486604604701935,
"data": 2.548660460,
"dims": []
},
"cylindrical_safety_factor": {
"attrs": {
"units": "dimensionless"
},
"data": 4.476700876332845,
"data": 4.476700876,
"dims": []
},
"elongation_psi95": {
"attrs": {
"units": "dimensionless"
},
"data": 1.6,
"data": 1.600000000,
"dims": []
},
"elongation_ratio_areal_to_psi95": {
"attrs": {
"units": "dimensionless"
},
"data": 1.025,
"data": 1.025000000,
"dims": []
},
"fraction_of_P_SOL_to_divertor": {
"attrs": {
"units": "dimensionless"
},
"data": 0.6,
"data": 0.6000000000,
"dims": []
},
"inverse_aspect_ratio": {
"attrs": {
"units": "dimensionless"
},
"data": 0.296969696969697,
"data": 0.2969696970,
"dims": []
},
"ion_heat_diffusivity": {
"attrs": {
"units": "meter ** 2 / second"
},
"data": 0.5,
"data": 0.5000000000,
"dims": []
},
"magnetic_field_on_axis": {
"attrs": {
"units": "tesla"
},
"data": 2.5,
"data": 2.500000000,
"dims": []
},
"major_radius": {
"attrs": {
"units": "meter"
},
"data": 1.65,
"data": 1.650000000,
"dims": []
},
"mean_ion_charge_state": {
"attrs": {
"units": "dimensionless"
},
"data": 1.0,
"data": 1.000000000,
"dims": []
},
"minor_radius": {
"attrs": {
"units": "meter"
},
"data": 0.49,
"data": 0.4900000000,
"dims": []
},
"plasma_current": {
"attrs": {
"units": "ampere"
},
"data": 800000.0,
"data": 800000.0000,
"dims": []
},
"poloidal_sound_larmor_radius": {
"attrs": {
"units": "millimeter"
},
"data": 4.4794720291352315,
"data": 4.479472029,
"dims": []
},
"separatrix_electron_density": {
"attrs": {
"units": "_1e19_per_cubic_metre"
},
"data": 1.443846153846154,
"data": 1.443846154,
"dims": []
},
"separatrix_electron_temp": {
"attrs": {
"units": "electron_volt"
},
"data": 57.51724137931035,
"data": 57.51724138,
"dims": []
},
"surface_area": {
"attrs": {
"units": "meter ** 2"
},
"data": 40.01085451537566,
"data": 40.01085452,
"dims": []
},
"sustainment_power_in_electron_channel": {
"attrs": {
"units": "megawatt"
},
"data": 0.7413990364829475,
"data": 0.7413990365,
"dims": []
},
"sustainment_power_in_ion_channel": {
"attrs": {
"units": "megawatt"
},
"data": 0.16744964593929906,
"data": 0.1674496459,
"dims": []
},
"target_electron_temp": {
"attrs": {
"units": "electron_volt"
},
"data": 10.0,
"data": 10.00000000,
"dims": []
},
"triangularity_psi95": {
"attrs": {
"units": "dimensionless"
},
"data": 0.3,
"data": 0.3000000000,
"dims": []
},
"z_effective": {
"attrs": {
"units": "dimensionless"
},
"data": 1.25,
"data": 1.250000000,
"dims": []
}
},
790 changes: 395 additions & 395 deletions tests/regression_results/PRD.json

Large diffs are not rendered by default.