Skip to content

Commit af7552e

Browse files
committed
Fix updater for hdf5 files with custom data, and add hdf5 files to automatic updater testing
1 parent 07648ad commit af7552e

File tree

5 files changed

+11
-3
lines changed

5 files changed

+11
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
### Fixed
1616
- Properly handle `.freqs` in `output_monitors` of adjoint plugin.
17+
- Simulation updater for `.hdf5` files with custom data.
1718

1819
## [2.4.2] - 2023-9-28
1920

tests/sims/simulation_2_4_2.h5

361 KB
Binary file not shown.

tests/test_components/test_IO.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ def set_datasets_to_none(sim):
5050
def test_simulation_load_export():
5151
major, minor, patch = __version__.split(".")
5252
path = os.path.join(SIM_DIR, f"simulation_{major}_{minor}_{patch}.json")
53+
# saving as .h5 since *.hdf5 is git ignored
54+
path_hdf5 = os.path.join(SIM_DIR, f"simulation_{major}_{minor}_{patch}.h5")
5355
SIM.to_file(path)
56+
SIM.to_hdf5(path_hdf5)
5457
SIM2 = td.Simulation.from_file(path)
58+
SIM_HDF5 = td.Simulation.from_hdf5(path_hdf5)
5559
assert set_datasets_to_none(SIM) == SIM2, "original and loaded simulations are not the same"
60+
assert SIM == SIM_HDF5, "original and loaded from hdf5 simulations are not the same"
5661

5762

5863
def test_simulation_load_export_yaml(tmp_path):

tidy3d/components/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ def ndarray_encoder(val):
6565
def _get_valid_extension(fname: str) -> str:
6666
"""Return the file extension from fname, validated to accepted ones."""
6767
extension = "".join(pathlib.Path(fname).suffixes).lower()
68-
if extension not in {".json", ".yaml", ".hdf5", ".hdf5.gz"}:
68+
valid_extensions = [".json", ".yaml", ".hdf5", ".hdf5.gz", ".h5"]
69+
if extension not in valid_extensions:
6970
raise FileError(
70-
f"{fname}::File extension must be .json, .yaml, .hdf5, or .hdf5.gz, but '{extension}' given"
71+
f"{fname}::File extension must be in {valid_extensions}, but '{extension}' given"
7172
)
7273
return extension
7374

@@ -225,6 +226,7 @@ def dict_from_file(cls, fname: str, group_path: str = None) -> dict:
225226
".yaml": cls.dict_from_yaml,
226227
".hdf5": cls.dict_from_hdf5,
227228
".hdf5.gz": cls.dict_from_hdf5_gz,
229+
".h5": cls.dict_from_hdf5,
228230
}[extension]
229231
return converter(**kwargs)
230232

tidy3d/updater.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def update_1_8(sim_dict: dict) -> dict:
210210

211211
def fix_missing_scalar_field(mnt_dict: dict) -> dict:
212212
for key, val in mnt_dict["field_dataset"].items():
213-
if val == "XR.DATAARRAY":
213+
if isinstance(val, str) and val == "XR.DATAARRAY":
214214
mnt_dict["field_dataset"][key] = "ScalarFieldDataArray"
215215
return mnt_dict
216216

0 commit comments

Comments
 (0)