Skip to content

Commit

Permalink
fix batch
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerflex committed Jul 10, 2024
1 parent d216b54 commit 0c70f7d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
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
38 changes: 23 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,28 @@ 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")
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 +188,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

0 comments on commit 0c70f7d

Please sign in to comment.