diff --git a/src/scgenerator/__init__.py b/src/scgenerator/__init__.py index 2699f07..c5f3af3 100644 --- a/src/scgenerator/__init__.py +++ b/src/scgenerator/__init__.py @@ -13,7 +13,7 @@ from .plotting import ( get_extent, ) from .spectra import Spectrum, SimulationSeries -from ._utils import Paths, open_config, parameter +from ._utils import Paths, _open_config, parameter, open_single_config from ._utils.parameter import Configuration, Parameters from ._utils.utils import PlotRange from ._utils.legacy import convert_sim_folder diff --git a/src/scgenerator/_utils/__init__.py b/src/scgenerator/_utils/__init__.py index 5c653a5..b540d9e 100644 --- a/src/scgenerator/_utils/__init__.py +++ b/src/scgenerator/_utils/__init__.py @@ -103,12 +103,12 @@ def conform_toml_path(path: os.PathLike) -> str: def open_single_config(path: os.PathLike) -> dict[str, Any]: - d = open_config(path) + d = _open_config(path) f = d.pop("Fiber")[0] return d | f -def open_config(path: os.PathLike): +def _open_config(path: os.PathLike): """returns a dictionary parsed from the specified toml file This also handle having a 'INCLUDE' argument that will fill otherwise unspecified keys with what's in the INCLUDE file(s)""" @@ -170,7 +170,7 @@ def load_config_sequence(path: os.PathLike) -> tuple[Path, list[dict[str, Any]]] Parameters ---------- path : os.PathLike - path to the config toml file + path to the config toml file or a directory containing config files Returns ------- @@ -180,9 +180,15 @@ def load_config_sequence(path: os.PathLike) -> tuple[Path, list[dict[str, Any]]] one config per fiber """ - loaded_config = open_config(path) + path = Path(path) + fiber_list: list[dict[str, Any]] + if path.name.lower().endswith(".toml"): + loaded_config = _open_config(path) + fiber_list = loaded_config.pop("Fiber") + else: + loaded_config = dict(name=path.name) + fiber_list = [_open_config(p) for p in sorted(path.glob("initial_config*.toml"))] - fiber_list: list[dict[str, Any]] = loaded_config.pop("Fiber") if len(fiber_list) == 0: raise ValueError(f"No fiber in config {path}") final_path = loaded_config.get("name") diff --git a/src/scgenerator/_utils/legacy.py b/src/scgenerator/_utils/legacy.py index e198f55..fbd2c62 100644 --- a/src/scgenerator/_utils/legacy.py +++ b/src/scgenerator/_utils/legacy.py @@ -1,3 +1,4 @@ +from genericpath import exists import os import sys from pathlib import Path @@ -28,11 +29,13 @@ def load_config_sequence(path: os.PathLike) -> tuple[list[Path], list[dict[str, def convert_sim_folder(path: os.PathLike): path = Path(path).resolve() + new_root = path.parent / "sc_legagy_converter" / path.name + os.makedirs(new_root, exist_ok=True) config_paths, configs = load_config_sequence(path) master_config = dict(name=path.name, Fiber=configs) - with open(path / "initial_config.toml", "w") as f: + with open(new_root / "initial_config.toml", "w") as f: toml.dump(master_config, f, encoder=toml.TomlNumpyEncoder()) - configuration = Configuration(path / "initial_config.toml", final_output_path=path) + configuration = Configuration(path, final_output_path=new_root) pbar = PBars(configuration.total_num_steps, "Converting") new_paths: dict[VariationDescriptor, Parameters] = dict(configuration) diff --git a/src/scgenerator/_utils/parameter.py b/src/scgenerator/_utils/parameter.py index 6f4ba83..9621356 100644 --- a/src/scgenerator/_utils/parameter.py +++ b/src/scgenerator/_utils/parameter.py @@ -480,7 +480,7 @@ class Parameters(_AbstractParameters): @classmethod def load(cls, path: os.PathLike) -> "Parameters": - return cls(**utils.open_config(path)) + return cls(**utils._open_config(path)) @classmethod def load_and_compute(cls, path: os.PathLike) -> "Parameters": @@ -1011,7 +1011,7 @@ class Configuration: num = utils.find_last_spectrum_num(data_dir) if config_dict is None: try: - config_dict = utils.open_config(data_dir / PARAM_FN) + config_dict = utils._open_config(data_dir / PARAM_FN) except FileNotFoundError: self.logger.warning(f"did not find {PARAM_FN!r} in {data_dir}") return self.State.ABSENT, 0 diff --git a/src/scgenerator/physics/__init__.py b/src/scgenerator/physics/__init__.py index 24c3937..5bb6018 100644 --- a/src/scgenerator/physics/__init__.py +++ b/src/scgenerator/physics/__init__.py @@ -10,8 +10,7 @@ from scipy.optimize import minimize_scalar from .. import math from . import fiber, materials, units, pulse -from .. import _utils -from .._utils import cache +from .._utils import cache, load_material_dico T = TypeVar("T") @@ -62,7 +61,7 @@ def material_dispersion( ) return disp else: - material_dico = utils.load_material_dico(material) + material_dico = load_material_dico(material) if ideal: n_gas_2 = materials.sellmeier(wavelengths, material_dico, pressure, temperature) + 1 else: diff --git a/src/scgenerator/physics/pulse.py b/src/scgenerator/physics/pulse.py index ce8ab29..13fe4cc 100644 --- a/src/scgenerator/physics/pulse.py +++ b/src/scgenerator/physics/pulse.py @@ -810,7 +810,8 @@ def find_lobe_limits(x_axis, values, debug="", already_sorted=True): ) ax.legend() fig.savefig(out_path, bbox_inches="tight") - plt.close() + if fig is not None: + plt.close(fig) else: good_roots, left_lim, right_lim = _select_roots(d_spline, d_roots, dd_roots, fwhm_pos) diff --git a/src/scgenerator/physics/simulate.py b/src/scgenerator/physics/simulate.py index 3864ffc..0d9fef1 100644 --- a/src/scgenerator/physics/simulate.py +++ b/src/scgenerator/physics/simulate.py @@ -219,6 +219,17 @@ class RK4IP: return self.stored_spectra def irun(self) -> Generator[tuple[int, int, np.ndarray], None, None]: + """run the simulation as a generator obj + + Yields + ------- + int + current simulation step + int + current number of spectra returned + np.ndarray + spectrum + """ # Print introduction self.logger.debug( diff --git a/src/scgenerator/scripts/__init__.py b/src/scgenerator/scripts/__init__.py index fb1f473..c740781 100644 --- a/src/scgenerator/scripts/__init__.py +++ b/src/scgenerator/scripts/__init__.py @@ -12,7 +12,7 @@ from ..const import PARAM_FN, PARAM_SEPARATOR from ..physics import fiber, units from ..plotting import plot_setup from ..spectra import SimulationSeries -from .._utils import auto_crop, open_config, save_toml, translate_parameters +from .._utils import auto_crop, _open_config, save_toml, translate_parameters from .._utils.parameter import ( Configuration, Parameters, @@ -258,7 +258,7 @@ def finish_plot(fig, legend_axes, all_labels, params): def plot_helper(config_path: Path) -> Iterable[tuple[dict, list[str], Parameters]]: cc = cycler(color=[f"C{i}" for i in range(10)]) * cycler(ls=["-", "--"]) - pseq = Configuration(open_config(config_path)) + pseq = Configuration(_open_config(config_path)) for style, (variables, params) in zip(cc, pseq): lbl = [pretty_format_value(name, value) for name, value in variables[1:-1]] yield style, lbl, params @@ -267,7 +267,7 @@ def plot_helper(config_path: Path) -> Iterable[tuple[dict, list[str], Parameters def convert_params(params_file: os.PathLike): p = Path(params_file) if p.name == PARAM_FN: - d = open_config(params_file) + d = _open_config(params_file) d = translate_parameters(d) save_toml(params_file, d) print(f"converted {p}")