diff --git a/CHANGELOG.md b/CHANGELOG.md index c7800b170b..d03f110677 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- Error when loading a previously run `Batch` or `ComponentModeler` containing custom data. + ## [2.7.1] ### Added diff --git a/tidy3d/plugins/smatrix/component_modelers/base.py b/tidy3d/plugins/smatrix/component_modelers/base.py index 295ab4b050..973261a531 100644 --- a/tidy3d/plugins/smatrix/component_modelers/base.py +++ b/tidy3d/plugins/smatrix/component_modelers/base.py @@ -2,7 +2,6 @@ from __future__ import annotations -import json import os from abc import ABC, abstractmethod from typing import Dict, Tuple, Union @@ -120,19 +119,9 @@ def _task_name(port: Port, mode_index: int = None) -> str: def sim_dict(self) -> Dict[str, Simulation]: """Generate all the :class:`Simulation` objects for the S matrix calculation.""" - def json(self, **kwargs): - """Save component to dictionary. Add the ``batch`` if it has been cached.""" - - self_json = super().json(**kwargs) - - batch = self._cached_properties.get("batch") - - if not batch: - return self_json - - self_dict = json.loads(self_json) - self_dict["batch_cached"] = json.loads(batch.json()) - return json.dumps(self_dict) + def to_file(self, *args, **kwargs): + new_self = self.updated_copy(batch_cached=self.batch) + super(AbstractComponentModeler, new_self).to_file(*args, **kwargs) @cached_property def batch(self) -> Batch: @@ -180,7 +169,7 @@ def get_path_dir(self, path_dir: str) -> None: @cached_property def _batch_path(self) -> str: """Where we store the batch for this ComponentModeler instance after the run.""" - return os.path.join(self.path_dir, "batch" + str(hash(self)) + ".json") + return os.path.join(self.path_dir, "batch" + str(hash(self)) + ".hdf5") def _run_sims(self, path_dir: str = DEFAULT_DATA_DIR) -> BatchData: """Run :class:`Simulations` for each port and return the batch after saving.""" diff --git a/tidy3d/web/api/container.py b/tidy3d/web/api/container.py index 8fd94d3d9e..c3e45e33b6 100644 --- a/tidy3d/web/api/container.py +++ b/tidy3d/web/api/container.py @@ -3,7 +3,6 @@ from __future__ import annotations import concurrent -import json import os import time from abc import ABC @@ -171,19 +170,9 @@ class Job(WebContainer): "parent_tasks", ) - def json(self, **kwargs): - """Save ``Job`` to dictionary. Add the `task_id` if it has been cached.""" - - self_json = super().json(**kwargs) - - task_id = self._cached_properties.get("task_id") - - if not task_id: - return self_json - - self_dict = json.loads(self_json) - self_dict["task_id_cached"] = task_id - return json.dumps(self_dict) + def to_file(self, *args, **kwargs): + new_self = self.updated_copy(task_id_cached=self.task_id) + super(Job, new_self).to_file(*args, **kwargs) def run(self, path: str = DEFAULT_DATA_PATH) -> SimulationDataType: """Run :class:`Job` all the way through and return data. @@ -587,19 +576,9 @@ def jobs(self) -> Dict[TaskName, Job]: jobs[task_name] = job return jobs - def json(self, **kwargs): - """Save ``Batch`` to dictionary. Add the ``jobs`` if they have been cached.""" - - self_json = super().json(**kwargs) - - jobs = self._cached_properties.get("jobs") - - if not jobs: - return self_json - - self_dict = json.loads(self_json) - self_dict["jobs_cached"] = {k: json.loads(j.json()) for k, j in jobs.items()} - return json.dumps(self_dict) + def to_file(self, *args, **kwargs): + new_self = self.updated_copy(jobs_cached=self.jobs) + super(Batch, new_self).to_file(*args, **kwargs) @property def num_jobs(self) -> int: