ready to merge
This commit is contained in:
@@ -13,7 +13,7 @@ from .plotting import (
|
|||||||
get_extent,
|
get_extent,
|
||||||
)
|
)
|
||||||
from .spectra import Spectrum, SimulationSeries
|
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.parameter import Configuration, Parameters
|
||||||
from ._utils.utils import PlotRange
|
from ._utils.utils import PlotRange
|
||||||
from ._utils.legacy import convert_sim_folder
|
from ._utils.legacy import convert_sim_folder
|
||||||
|
|||||||
@@ -103,12 +103,12 @@ def conform_toml_path(path: os.PathLike) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def open_single_config(path: os.PathLike) -> dict[str, Any]:
|
def open_single_config(path: os.PathLike) -> dict[str, Any]:
|
||||||
d = open_config(path)
|
d = _open_config(path)
|
||||||
f = d.pop("Fiber")[0]
|
f = d.pop("Fiber")[0]
|
||||||
return d | f
|
return d | f
|
||||||
|
|
||||||
|
|
||||||
def open_config(path: os.PathLike):
|
def _open_config(path: os.PathLike):
|
||||||
"""returns a dictionary parsed from the specified toml file
|
"""returns a dictionary parsed from the specified toml file
|
||||||
This also handle having a 'INCLUDE' argument that will fill
|
This also handle having a 'INCLUDE' argument that will fill
|
||||||
otherwise unspecified keys with what's in the INCLUDE file(s)"""
|
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
|
Parameters
|
||||||
----------
|
----------
|
||||||
path : os.PathLike
|
path : os.PathLike
|
||||||
path to the config toml file
|
path to the config toml file or a directory containing config files
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
@@ -180,9 +180,15 @@ def load_config_sequence(path: os.PathLike) -> tuple[Path, list[dict[str, Any]]]
|
|||||||
one config per fiber
|
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:
|
if len(fiber_list) == 0:
|
||||||
raise ValueError(f"No fiber in config {path}")
|
raise ValueError(f"No fiber in config {path}")
|
||||||
final_path = loaded_config.get("name")
|
final_path = loaded_config.get("name")
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from genericpath import exists
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
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):
|
def convert_sim_folder(path: os.PathLike):
|
||||||
path = Path(path).resolve()
|
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)
|
config_paths, configs = load_config_sequence(path)
|
||||||
master_config = dict(name=path.name, Fiber=configs)
|
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())
|
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")
|
pbar = PBars(configuration.total_num_steps, "Converting")
|
||||||
|
|
||||||
new_paths: dict[VariationDescriptor, Parameters] = dict(configuration)
|
new_paths: dict[VariationDescriptor, Parameters] = dict(configuration)
|
||||||
|
|||||||
@@ -480,7 +480,7 @@ class Parameters(_AbstractParameters):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, path: os.PathLike) -> "Parameters":
|
def load(cls, path: os.PathLike) -> "Parameters":
|
||||||
return cls(**utils.open_config(path))
|
return cls(**utils._open_config(path))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load_and_compute(cls, path: os.PathLike) -> "Parameters":
|
def load_and_compute(cls, path: os.PathLike) -> "Parameters":
|
||||||
@@ -1011,7 +1011,7 @@ class Configuration:
|
|||||||
num = utils.find_last_spectrum_num(data_dir)
|
num = utils.find_last_spectrum_num(data_dir)
|
||||||
if config_dict is None:
|
if config_dict is None:
|
||||||
try:
|
try:
|
||||||
config_dict = utils.open_config(data_dir / PARAM_FN)
|
config_dict = utils._open_config(data_dir / PARAM_FN)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
self.logger.warning(f"did not find {PARAM_FN!r} in {data_dir}")
|
self.logger.warning(f"did not find {PARAM_FN!r} in {data_dir}")
|
||||||
return self.State.ABSENT, 0
|
return self.State.ABSENT, 0
|
||||||
|
|||||||
@@ -10,8 +10,7 @@ from scipy.optimize import minimize_scalar
|
|||||||
|
|
||||||
from .. import math
|
from .. import math
|
||||||
from . import fiber, materials, units, pulse
|
from . import fiber, materials, units, pulse
|
||||||
from .. import _utils
|
from .._utils import cache, load_material_dico
|
||||||
from .._utils import cache
|
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
|
|
||||||
@@ -62,7 +61,7 @@ def material_dispersion(
|
|||||||
)
|
)
|
||||||
return disp
|
return disp
|
||||||
else:
|
else:
|
||||||
material_dico = utils.load_material_dico(material)
|
material_dico = load_material_dico(material)
|
||||||
if ideal:
|
if ideal:
|
||||||
n_gas_2 = materials.sellmeier(wavelengths, material_dico, pressure, temperature) + 1
|
n_gas_2 = materials.sellmeier(wavelengths, material_dico, pressure, temperature) + 1
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -810,7 +810,8 @@ def find_lobe_limits(x_axis, values, debug="", already_sorted=True):
|
|||||||
)
|
)
|
||||||
ax.legend()
|
ax.legend()
|
||||||
fig.savefig(out_path, bbox_inches="tight")
|
fig.savefig(out_path, bbox_inches="tight")
|
||||||
plt.close()
|
if fig is not None:
|
||||||
|
plt.close(fig)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
good_roots, left_lim, right_lim = _select_roots(d_spline, d_roots, dd_roots, fwhm_pos)
|
good_roots, left_lim, right_lim = _select_roots(d_spline, d_roots, dd_roots, fwhm_pos)
|
||||||
|
|||||||
@@ -219,6 +219,17 @@ class RK4IP:
|
|||||||
return self.stored_spectra
|
return self.stored_spectra
|
||||||
|
|
||||||
def irun(self) -> Generator[tuple[int, int, np.ndarray], None, None]:
|
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
|
# Print introduction
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from ..const import PARAM_FN, PARAM_SEPARATOR
|
|||||||
from ..physics import fiber, units
|
from ..physics import fiber, units
|
||||||
from ..plotting import plot_setup
|
from ..plotting import plot_setup
|
||||||
from ..spectra import SimulationSeries
|
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 (
|
from .._utils.parameter import (
|
||||||
Configuration,
|
Configuration,
|
||||||
Parameters,
|
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]]:
|
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=["-", "--"])
|
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):
|
for style, (variables, params) in zip(cc, pseq):
|
||||||
lbl = [pretty_format_value(name, value) for name, value in variables[1:-1]]
|
lbl = [pretty_format_value(name, value) for name, value in variables[1:-1]]
|
||||||
yield style, lbl, params
|
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):
|
def convert_params(params_file: os.PathLike):
|
||||||
p = Path(params_file)
|
p = Path(params_file)
|
||||||
if p.name == PARAM_FN:
|
if p.name == PARAM_FN:
|
||||||
d = open_config(params_file)
|
d = _open_config(params_file)
|
||||||
d = translate_parameters(d)
|
d = translate_parameters(d)
|
||||||
save_toml(params_file, d)
|
save_toml(params_file, d)
|
||||||
print(f"converted {p}")
|
print(f"converted {p}")
|
||||||
|
|||||||
Reference in New Issue
Block a user