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 028cc98
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 42 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
19 changes: 4 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,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:
Expand Down Expand Up @@ -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."""
Expand Down
33 changes: 6 additions & 27 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,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.
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 028cc98

Please sign in to comment.