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

Fix bug in loading previously run Batch and ComponentModeler from file when custom medium present #1817

Merged
merged 1 commit into from
Jul 11, 2024
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 24 additions & 15 deletions tidy3d/plugins/smatrix/component_modelers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import json
import os
from abc import ABC, abstractmethod
from typing import Dict, Tuple, Union
Expand Down Expand Up @@ -120,19 +119,29 @@ 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, fname: str) -> None:
"""Exports :class:`Tidy3dBaseModel` instance to .yaml, .json, or .hdf5 file

Parameters
----------
fname : str
Full path to the .yaml or .json file to save the :class:`Tidy3dBaseModel` to.

Example
-------
>>> simulation.to_file(fname='folder/sim.json') # doctest: +SKIP
"""

batch_cached = self._cached_properties.get("batch")
jobs_cached = batch_cached._cached_properties.get("jobs")
if jobs_cached is not None:
jobs = {}
for key, job in jobs_cached.items():
task_id = job._cached_properties.get("task_id")
jobs[key] = job.updated_copy(task_id_cached=task_id)
batch_cached = batch_cached.updated_copy(jobs_cached=jobs)
self = self.updated_copy(batch_cached=batch_cached)
super(AbstractComponentModeler, self).to_file(fname=fname) # noqa: UP008

@cached_property
def batch(self) -> Batch:
Expand Down Expand Up @@ -180,7 +189,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."""
Expand Down
53 changes: 31 additions & 22 deletions tidy3d/web/api/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import concurrent
import json
import os
import time
from abc import ABC
Expand Down Expand Up @@ -171,19 +170,22 @@ class Job(WebContainer):
"parent_tasks",
)

def json(self, **kwargs):
"""Save ``Job`` to dictionary. Add the `task_id` if it has been cached."""
def to_file(self, fname: str) -> None:
"""Exports :class:`Tidy3dBaseModel` instance to .yaml, .json, or .hdf5 file

self_json = super().json(**kwargs)

task_id = self._cached_properties.get("task_id")
Parameters
----------
fname : str
Full path to the .yaml or .json file to save the :class:`Tidy3dBaseModel` to.

if not task_id:
return self_json
Example
-------
>>> simulation.to_file(fname='folder/sim.json') # doctest: +SKIP
"""

self_dict = json.loads(self_json)
self_dict["task_id_cached"] = task_id
return json.dumps(self_dict)
task_id_cached = self._cached_properties.get("task_id")
self = self.updated_copy(task_id_cached=task_id_cached)
super(Job, self).to_file(fname=fname) # noqa: UP008

def run(self, path: str = DEFAULT_DATA_PATH) -> SimulationDataType:
"""Run :class:`Job` all the way through and return data.
Expand Down Expand Up @@ -587,19 +589,26 @@ 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)
def to_file(self, fname: str) -> None:
"""Exports :class:`Tidy3dBaseModel` instance to .yaml, .json, or .hdf5 file

jobs = self._cached_properties.get("jobs")

if not jobs:
return self_json
Parameters
----------
fname : str
Full path to the .yaml or .json file to save the :class:`Tidy3dBaseModel` to.

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)
Example
-------
>>> simulation.to_file(fname='folder/sim.json') # doctest: +SKIP
"""
jobs_cached = self._cached_properties.get("jobs")
if jobs_cached is not None:
jobs = {}
for key, job in jobs_cached.items():
task_id = job._cached_properties.get("task_id")
jobs[key] = job.updated_copy(task_id_cached=task_id)
self = self.updated_copy(jobs_cached=jobs)
super(Batch, self).to_file(fname=fname) # noqa: UP008

@property
def num_jobs(self) -> int:
Expand Down
Loading