From be151b208f9361f0906f84a2866bbdcdc48f80c9 Mon Sep 17 00:00:00 2001 From: Rui Coelho Date: Wed, 31 Jan 2024 17:48:20 +0000 Subject: [PATCH] Add curve fitting solver --- piglot/solver/__init__.py | 4 +- piglot/solver/{dummy => curve}/__init__.py | 0 piglot/solver/curve/fields.py | 157 +++++++++++++++ piglot/solver/curve/solver.py | 184 ++++++++++++++++++ piglot/solver/dummy/fields.py | 159 --------------- piglot/solver/dummy/solver.py | 131 ------------- test/examples/dummy_composite.yaml | 20 +- test/examples/dummy_composite_stochastic.yaml | 28 ++- test/examples/dummy_design.yaml | 16 +- test/examples/dummy_filtering.yaml | 20 +- test/examples/dummy_simple.yaml | 20 +- test/examples/dummy_simple_stochastic.yaml | 28 ++- test/examples/dummy_transformer.yaml | 21 +- test/examples/reference.txt | 5 - test/examples/reference_curve.txt | 6 + .../dummy_invalid_parameters.yaml | 29 --- .../dummy_invalid_parameters2.yaml | 30 --- test/examples_assertions/reference.txt | 5 - test/examples_assertions/reference_curve.txt | 6 + test/examples_plots/dummy_composite.yaml | 20 +- .../dummy_composite_stochastic.yaml | 28 ++- test/examples_plots/dummy_design.yaml | 16 +- test/examples_plots/dummy_filtering.yaml | 20 +- test/examples_plots/dummy_simple.yaml | 20 +- .../dummy_simple_stochastic.yaml | 28 ++- test/examples_plots/dummy_transformer.yaml | 21 +- test/examples_plots/reference.txt | 5 - test/examples_plots/reference_curve.txt | 6 + test/test_examples_assertions.py | 8 - 29 files changed, 487 insertions(+), 554 deletions(-) rename piglot/solver/{dummy => curve}/__init__.py (100%) create mode 100644 piglot/solver/curve/fields.py create mode 100644 piglot/solver/curve/solver.py delete mode 100644 piglot/solver/dummy/fields.py delete mode 100644 piglot/solver/dummy/solver.py delete mode 100644 test/examples/reference.txt create mode 100644 test/examples/reference_curve.txt delete mode 100644 test/examples_assertions/dummy_invalid_parameters.yaml delete mode 100644 test/examples_assertions/dummy_invalid_parameters2.yaml delete mode 100644 test/examples_assertions/reference.txt create mode 100644 test/examples_assertions/reference_curve.txt delete mode 100644 test/examples_plots/reference.txt create mode 100644 test/examples_plots/reference_curve.txt diff --git a/piglot/solver/__init__.py b/piglot/solver/__init__.py index 055deae..25b2f78 100644 --- a/piglot/solver/__init__.py +++ b/piglot/solver/__init__.py @@ -3,12 +3,12 @@ from piglot.parameter import ParameterSet from piglot.solver.solver import Solver from piglot.solver.links.solver import LinksSolver -from piglot.solver.dummy.solver import DummySolver +from piglot.solver.curve.solver import CurveSolver AVAILABLE_SOLVERS: Dict[str, Type[Solver]] = { 'links': LinksSolver, - 'dummy': DummySolver, + 'curve': CurveSolver, } diff --git a/piglot/solver/dummy/__init__.py b/piglot/solver/curve/__init__.py similarity index 100% rename from piglot/solver/dummy/__init__.py rename to piglot/solver/curve/__init__.py diff --git a/piglot/solver/curve/fields.py b/piglot/solver/curve/fields.py new file mode 100644 index 0000000..fe3d54d --- /dev/null +++ b/piglot/solver/curve/fields.py @@ -0,0 +1,157 @@ +"""Module for output fields from Curve solver.""" +from __future__ import annotations +from typing import Dict, Any, Tuple +import os +import copy +import numpy as np +from piglot.parameter import ParameterSet +from piglot.solver.solver import InputData, OutputField, OutputResult +from piglot.utils.solver_utils import write_parameters, get_case_name + + +class CurveInputData(InputData): + """Container for dummy input data.""" + + def __init__( + self, + case_name: str, + expression: str, + parametric: str, + bounds: Tuple[float, float], + points: int, + ) -> None: + super().__init__() + self.case_name = case_name + self.expression = expression + self.parametric = parametric + self.bounds = bounds + self.points = points + self.input_file: str = None + + def prepare( + self, + values: np.ndarray, + parameters: ParameterSet, + tmp_dir: str = None, + ) -> CurveInputData: + """Prepare the input data for the simulation with a given set of parameters. + + Parameters + ---------- + values : np.ndarray + Parameters to run for. + parameters : ParameterSet + Parameter set for this problem. + tmp_dir : str, optional + Temporary directory to run the analyses, by default None + + Returns + ------- + CurveInputData + Input data prepared for the simulation. + """ + result = copy.copy(self) + # Write the input file (with the name placeholder) + tmp_file = os.path.join(tmp_dir, f'{self.case_name}.tmp') + with open(tmp_file, 'w', encoding='utf8') as file: + file.write(f'{self.expression}') + # Write the parameters to the input file + result.input_file = os.path.join(tmp_dir, f'{self.case_name}.dat') + write_parameters(parameters.to_dict(values), tmp_file, result.input_file) + return result + + def check(self, parameters: ParameterSet) -> None: + """Check if the input data is valid according to the given parameters. + + Parameters + ---------- + parameters : ParameterSet + Parameter set for this problem. + """ + # Generate a dummy set of parameters (to ensure proper handling of output parameters) + values = np.array([parameter.inital_value for parameter in parameters]) + param_dict = parameters.to_dict(values, input_normalised=False) + for parameter in param_dict: + if parameter not in self.expression: + raise ValueError(f"Parameter '{parameter}' not found in expression.") + + def name(self) -> str: + """Return the name of the input data. + + Returns + ------- + str + Name of the input data. + """ + return self.case_name + + def get_current(self, target_dir: str) -> CurveInputData: + """Get the current input data. + + Parameters + ---------- + target_dir : str + Target directory to copy the input file. + + Returns + ------- + CurveInputData + Current input data. + """ + result = CurveInputData(os.path.join(target_dir, self.case_name), self.expression, + self.parametric, self.bounds, self.points) + result.input_file = os.path.join(target_dir, self.case_name + '.dat') + return result + + +class Curve(OutputField): + """Curve output reader.""" + + def check(self, input_data: CurveInputData) -> None: + """Sanity checks on the input file. + + Parameters + ---------- + input_data : CurveInputData + Input data for this case. + + """ + + def get(self, input_data: CurveInputData) -> OutputResult: + """Reads reactions from a Curve analysis. + + Parameters + ---------- + input_data : CurveInputData + Input data for this case. + + Returns + ------- + array + 2D array with parametric value and corresponding expression value. + """ + input_file = input_data.input_file + casename = get_case_name(input_file) + output_dir = os.path.dirname(input_file) + output_filename = os.path.join(output_dir, f'{casename}.out') + # Ensure the file exists + if not os.path.exists(output_filename): + return OutputResult(np.empty(0), np.empty(0)) + data = np.genfromtxt(output_filename) + return OutputResult(data[:, 0], data[:, 1]) + + @staticmethod + def read(config: Dict[str, Any]) -> Curve: + """Read the output field from the configuration dictionary. + + Parameters + ---------- + config : Dict[str, Any] + Configuration dictionary. + + Returns + ------- + Reaction + Output field to use for this problem. + """ + return Curve() diff --git a/piglot/solver/curve/solver.py b/piglot/solver/curve/solver.py new file mode 100644 index 0000000..6e8953b --- /dev/null +++ b/piglot/solver/curve/solver.py @@ -0,0 +1,184 @@ +"""Module for Curve solver.""" +from typing import Dict, Any +import os +import time +import shutil +from multiprocessing.pool import ThreadPool as Pool +import numpy as np +import sympy +from piglot.parameter import ParameterSet +from piglot.solver.solver import Solver, Case, CaseResult, OutputField, OutputResult +from piglot.solver.curve.fields import CurveInputData, Curve + + +class CurveSolver(Solver): + """Curve solver.""" + + def __init__( + self, + cases: Dict[Case, Dict[str, OutputField]], + parameters: ParameterSet, + output_dir: str, + parallel: int, + tmp_dir: str, + ) -> None: + """Constructor for the Curve solver class. + + Parameters + ---------- + cases : Dict[Case, Dict[str, OutputField]] + Cases to be run and respective output fields. + parameters : ParameterSet + Parameter set for this problem. + output_dir : str + Path to the output directory. + parallel : int + Number of parallel processes to use. + tmp_dir : str + Path to the temporary directory. + """ + super().__init__(cases, parameters, output_dir) + self.parallel = parallel + self.tmp_dir = tmp_dir + + def _run_case(self, values: np.ndarray, case: Case, tmp_dir: str) -> CaseResult: + """Run a single case wth Curve. + + Parameters + ---------- + values: np.ndarray + Current parameter values + case : Case + Case to run. + tmp_dir: str + Temporary directory to run the simulation + + Returns + ------- + CaseResult + Results for this case + """ + # Copy input file replacing parameters by passed value + input_data: CurveInputData = case.input_data.prepare(values, self.parameters, tmp_dir) + # Run dummy solver + begin_time = time.time() + # Read the expression from the input file + with open(input_data.input_file, 'r', encoding='utf8') as file: + expression_str = file.read() + symbs = sympy.symbols(input_data.parametric) + expression = sympy.lambdify(symbs, expression_str) + # Evaluate the expression on the grid + grid = np.linspace(input_data.bounds[0], input_data.bounds[1], input_data.points) + curve = [expression(**{input_data.parametric: x}) for x in grid] + # Write out the curve + output_file = os.path.splitext(input_data.input_file)[0] + '.out' + with open(output_file, 'w', encoding='utf8') as file: + for x, y in zip(grid, curve): + file.write(f'{x} {y}\n') + # Read results from output directories + responses = {name: field.get(input_data) for name, field in case.fields.items()} + end_time = time.time() + return CaseResult( + begin_time, + end_time - begin_time, + values, + True, + self.parameters.hash(values), + responses, + ) + + def _solve( + self, + values: np.ndarray, + concurrent: bool, + ) -> Dict[Case, CaseResult]: + """Internal solver for the prescribed problems. + + Parameters + ---------- + values : array + Current parameters to evaluate. + concurrent : bool + Whether this run may be concurrent to another one (so use unique file names). + + Returns + ------- + Dict[Case, CaseResult] + Results for each case. + """ + # Ensure tmp directory is clean + tmp_dir = f'{self.tmp_dir}_{self.parameters.hash(values)}' if concurrent else self.tmp_dir + if os.path.isdir(tmp_dir): + shutil.rmtree(tmp_dir) + os.mkdir(tmp_dir) + + def run_case(case: Case) -> CaseResult: + return self._run_case(values, case, tmp_dir) + # Run cases (in parallel if specified) + if self.parallel > 1: + with Pool(self.parallel) as pool: + results = pool.map(run_case, self.cases) + else: + results = map(run_case, self.cases) + # Ensure we actually resolve the map + results = list(results) + # Cleanup temporary directories + if concurrent: + shutil.rmtree(tmp_dir) + # Build output dict + return dict(zip(self.cases, results)) + + def get_current_response(self) -> Dict[str, OutputResult]: + """Get the responses from a given output field for all cases. + + Returns + ------- + Dict[str, OutputResult] + Output responses. + """ + fields = self.get_output_fields() + return { + name: field.get(case.input_data.get_current(self.tmp_dir)) + for name, (case, field) in fields.items() + } + + @staticmethod + def read(config: Dict[str, Any], parameters: ParameterSet, output_dir: str) -> Solver: + """Read the solver from the configuration dictionary. + + Parameters + ---------- + config : Dict[str, Any] + Configuration dictionary. + parameters : ParameterSet + Parameter set for this problem. + output_dir : str + Path to the output directory. + + Returns + ------- + Solver + Solver to use for this problem. + """ + # Read the parallelism and temporary directory (if present) + parallel = int(config.get('parallel', 1)) + tmp_dir = os.path.join(output_dir, config.get('tmp_dir', 'tmp')) + # Read the cases + if 'cases' not in config: + raise ValueError("Missing 'cases' in solver configuration.") + cases = [] + for case_name, case_config in config['cases'].items(): + if 'expression' not in case_config: + raise ValueError("Missing 'expression' in solver configuration.") + if 'parametric' not in case_config: + raise ValueError("Missing 'parametric' in solver configuration.") + if 'bounds' not in case_config: + raise ValueError("Missing 'bounds' in solver configuration.") + expression = case_config['expression'] + parametric = case_config['parametric'] + bounds = case_config['bounds'] + points = int(case_config['points']) if 'points' in case_config else 100 + input_data = CurveInputData(case_name, expression, parametric, bounds, points) + cases.append(Case(input_data, {case_name: Curve()})) + # Return the solver + return CurveSolver(cases, parameters, output_dir, parallel=parallel, tmp_dir=tmp_dir) diff --git a/piglot/solver/dummy/fields.py b/piglot/solver/dummy/fields.py deleted file mode 100644 index 57bf04d..0000000 --- a/piglot/solver/dummy/fields.py +++ /dev/null @@ -1,159 +0,0 @@ -"""Module for output fields from Dummy solver.""" -from __future__ import annotations -from typing import Dict, Any -import numpy as np -from piglot.parameter import ParameterSet -from piglot.solver.solver import InputData, OutputField, OutputResult - - -class DummyInputData(InputData): - """Container for dummy input data.""" - - def __init__(self, parameters: Dict[str, float], case_name: str) -> None: - super().__init__() - self.parameters = parameters - self.case_name = case_name - - def prepare( - self, - values: np.ndarray, - parameters: ParameterSet, - tmp_dir: str = None, - ) -> DummyInputData: - """Prepare the input data for the simulation with a given set of parameters. - - Parameters - ---------- - values : np.ndarray - Parameters to run for. - parameters : ParameterSet - Parameter set for this problem. - tmp_dir : str, optional - Temporary directory to run the analyses, by default None - - Returns - ------- - DummyInputData - Input data prepared for the simulation. - """ - return DummyInputData(parameters.to_dict(values), self.case_name) - - def check(self, parameters: ParameterSet) -> None: - """Check if the input data is valid according to the given parameters. - - Parameters - ---------- - parameters : ParameterSet - Parameter set for this problem. - """ - # Generate a dummy set of parameters (to ensure proper handling of output parameters) - values = np.array([parameter.inital_value for parameter in parameters]) - param_dict = parameters.to_dict(values, input_normalised=False) - # Check if the require parameters are present in the input file - parameters_dummy = set(('m', 'c')) - if set(param_dict.keys()) != parameters_dummy: - raise ValueError("Invalid parameters: the parameters 'm' and 'c' are required.") - - def name(self) -> str: - """Return the name of the input data. - - Returns - ------- - str - Name of the input data. - """ - return self.case_name - - -class Modifier(OutputField): - """Modifier outputs reader.""" - - def __init__(self, y_offset: float = 0.0, y_scale: float = 1.0): - """Constructor for reaction reader - - Parameters - ---------- - y_offset : float, optional - Offset to apply to the y coordinate, by default 0.0 - y_scale : float, optional - Scale to apply to the y coordinate, by default 1.0 - """ - super().__init__() - self.y_offset = y_offset - self.y_scale = y_scale - - def check(self, input_data: DummyInputData) -> None: - """Sanity checks on the input file. - - Parameters - ---------- - input_data : DummyInputData - Input data for this case. - - """ - # Is macroscopic file? - pass - - def get(self, input_data: DummyInputData) -> OutputResult: - """Reads reactions from a Dummy analysis. - - Parameters - ---------- - input_data : DummyInputData - Input data for this case. - - Returns - ------- - array - 2D array with x in the first column and modified y in the second. - """ - x = np.linspace(0.0, 1.0, 100) - parameters = input_data.parameters - y = parameters['m'] * x + parameters['c'] - return OutputResult(x, y*self.y_scale + self.y_offset) - - @staticmethod - def read(config: Dict[str, Any]) -> Modifier: - """Read the output field from the configuration dictionary. - - Parameters - ---------- - config : Dict[str, Any] - Configuration dictionary. - - Returns - ------- - Reaction - Output field to use for this problem. - """ - # Read the y_offset (if passed) - y_offset = int(config.get('y_offset', 0.0)) - # Read the y_scale (if passed) - y_scale = int(config.get('y_scale', 1.0)) - return Modifier(y_offset, y_scale) - - -def dummy_fields_reader(config: Dict[str, Any]) -> OutputField: - """Read the output fields for the dummy solver. - - Parameters - ---------- - config : Dict[str, Any] - Configuration dictionary. - - Returns - ------- - OutputField - Output field to use for this problem. - """ - # Extract name of output field - if 'name' not in config: - raise ValueError("Missing 'name' in output field configuration.") - field_name = config['name'] - # Delegate to the appropriate reader - readers = { - 'Modifier': Modifier, - } - if field_name not in readers: - raise ValueError(f"Unknown output field name '{field_name}'.") - return readers[field_name].read(config) diff --git a/piglot/solver/dummy/solver.py b/piglot/solver/dummy/solver.py deleted file mode 100644 index 3a78753..0000000 --- a/piglot/solver/dummy/solver.py +++ /dev/null @@ -1,131 +0,0 @@ -"""Module for Dummy solver.""" -from typing import Dict, Any -import time -import numpy as np -from piglot.parameter import ParameterSet -from piglot.solver.solver import Solver, Case, CaseResult, OutputField, OutputResult -from piglot.solver.dummy.fields import dummy_fields_reader, DummyInputData - - -class DummySolver(Solver): - """Dummy solver.""" - - def __init__( - self, - cases: Dict[Case, Dict[str, OutputField]], - parameters: ParameterSet, - output_dir: str, - ) -> None: - """Constructor for the Dummy solver class. - - Parameters - ---------- - cases : Dict[Case, Dict[str, OutputField]] - Cases to be run and respective output fields. - parameters : ParameterSet - Parameter set for this problem. - output_dir : str - Path to the output directory. - """ - super().__init__(cases, parameters, output_dir) - - def _run_case(self, values: np.ndarray, case: Case) -> CaseResult: - """Run a single case wth Dummy. - - Parameters - ---------- - values: np.ndarray - Current parameter values - case : Case - Case to run. - - Returns - ------- - CaseResult - Results for this case - """ - # Copy input file replacing parameters by passed value - input_data = case.input_data.prepare(values, self.parameters) - # Run dummy solver - begin_time = time.time() - # Read results from output directories - responses = {name: field.get(input_data) for name, field in case.fields.items()} - end_time = time.time() - return CaseResult( - begin_time, - end_time - begin_time, - values, - True, - self.parameters.hash(values), - responses, - ) - - def _solve( - self, - values: np.ndarray, - concurrent: bool, - ) -> Dict[Case, CaseResult]: - """Internal solver for the prescribed problems. - - Parameters - ---------- - values : array - Current parameters to evaluate. - concurrent : bool - Whether this run may be concurrent to another one (so use unique file names). - - Returns - ------- - Dict[Case, CaseResult] - Results for each case. - """ - def run_case(case: Case) -> CaseResult: - return self._run_case(values, case) - results = map(run_case, self.cases) - # Ensure we actually resolve the map - results = list(results) - # Build output dict - return dict(zip(self.cases, results)) - - def get_current_response(self) -> Dict[str, OutputResult]: - """Get the responses from a given output field for all cases. - - Returns - ------- - Dict[str, OutputResult] - Output responses. - """ - raise NotImplementedError("Dummy solver does not support current plotting.") - - @staticmethod - def read(config: Dict[str, Any], parameters: ParameterSet, output_dir: str) -> Solver: - """Read the solver from the configuration dictionary. - - Parameters - ---------- - config : Dict[str, Any] - Configuration dictionary. - parameters : ParameterSet - Parameter set for this problem. - output_dir : str - Path to the output directory. - - Returns - ------- - Solver - Solver to use for this problem. - """ - # Read the cases - if 'cases' not in config: - raise ValueError("Missing 'cases' in solver configuration.") - cases = [] - for case_name, case_config in config['cases'].items(): - if 'fields' not in case_config: - raise ValueError(f"Missing 'fields' in case '{case_name}' configuration.") - fields = { - field_name: dummy_fields_reader(field_config) - for field_name, field_config in case_config['fields'].items() - } - cases.append(Case(DummyInputData(None, case_name), fields)) - # Return the solver - return DummySolver(cases, parameters, output_dir) diff --git a/test/examples/dummy_composite.yaml b/test/examples/dummy_composite.yaml index b836082..273ce16 100644 --- a/test/examples/dummy_composite.yaml +++ b/test/examples/dummy_composite.yaml @@ -6,8 +6,7 @@ optimiser: parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] + a: [0, -4, 4] @@ -15,16 +14,13 @@ objective: name: fitting composite: True solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 - - + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1'] + 'reference_curve.txt': + prediction: ['case_1'] diff --git a/test/examples/dummy_composite_stochastic.yaml b/test/examples/dummy_composite_stochastic.yaml index 6f1e6ed..638ecde 100644 --- a/test/examples/dummy_composite_stochastic.yaml +++ b/test/examples/dummy_composite_stochastic.yaml @@ -6,9 +6,7 @@ optimiser: parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] - + a: [0, -4, 4] objective: @@ -16,22 +14,20 @@ objective: composite: True stochastic: True solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 'case_2': - fields: - 'modifier_2': - name: Modifier - y_scale: 2.0 - y_offset: 0.0 + expression: 1.1 * * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1', 'modifier_2'] + 'reference_curve.txt': + prediction: ['case_1', 'case_2'] diff --git a/test/examples/dummy_design.yaml b/test/examples/dummy_design.yaml index cbbcac8..602b9e6 100644 --- a/test/examples/dummy_design.yaml +++ b/test/examples/dummy_design.yaml @@ -5,24 +5,22 @@ optimiser: random parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] + a: [0, -4, 4] objective: name: design solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 targets: 'maximum_force': quantity: max - prediction: ['modifier_1'] + prediction: ['case_1'] negate: True diff --git a/test/examples/dummy_filtering.yaml b/test/examples/dummy_filtering.yaml index dae72c5..9a7602e 100644 --- a/test/examples/dummy_filtering.yaml +++ b/test/examples/dummy_filtering.yaml @@ -5,8 +5,7 @@ optimiser: random parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] + a: [0, -4, 4] @@ -14,17 +13,14 @@ objective: name: fitting composite: False solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 - - + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1'] + 'reference_curve.txt': + prediction: ['case_1'] filter_tol: 1e-6 diff --git a/test/examples/dummy_simple.yaml b/test/examples/dummy_simple.yaml index 4dec141..4a1726d 100644 --- a/test/examples/dummy_simple.yaml +++ b/test/examples/dummy_simple.yaml @@ -5,8 +5,7 @@ optimiser: random parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] + a: [0, -4, 4] @@ -14,16 +13,13 @@ objective: name: fitting composite: False solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 - - + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1'] + 'reference_curve.txt': + prediction: ['case_1'] diff --git a/test/examples/dummy_simple_stochastic.yaml b/test/examples/dummy_simple_stochastic.yaml index 498e4bf..c1544e8 100644 --- a/test/examples/dummy_simple_stochastic.yaml +++ b/test/examples/dummy_simple_stochastic.yaml @@ -6,9 +6,7 @@ optimiser: parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] - + a: [0, -4, 4] objective: @@ -16,22 +14,20 @@ objective: composite: False stochastic: True solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 'case_2': - fields: - 'modifier_2': - name: Modifier - y_scale: 2.0 - y_offset: 0.0 + expression: 1.1 * * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1', 'modifier_2'] + 'reference_curve.txt': + prediction: ['case_1', 'case_2'] diff --git a/test/examples/dummy_transformer.yaml b/test/examples/dummy_transformer.yaml index a1f0dff..eae67c9 100644 --- a/test/examples/dummy_transformer.yaml +++ b/test/examples/dummy_transformer.yaml @@ -5,28 +5,23 @@ optimiser: random parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] - + a: [0, -4, 4] objective: name: fitting composite: False solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 - - + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1'] + 'reference_curve.txt': + prediction: ['case_1'] transformer: x_scale: -1 y_scale: -2 diff --git a/test/examples/reference.txt b/test/examples/reference.txt deleted file mode 100644 index d38197d..0000000 --- a/test/examples/reference.txt +++ /dev/null @@ -1,5 +0,0 @@ -0.0 0.0 -0.25 50 -0.5 100 -0.75 150 -1.0 200 \ No newline at end of file diff --git a/test/examples/reference_curve.txt b/test/examples/reference_curve.txt new file mode 100644 index 0000000..3d109ab --- /dev/null +++ b/test/examples/reference_curve.txt @@ -0,0 +1,6 @@ +-4.0 32.0 +-2.4 11.52 +-0.7999999999999998 1.2799999999999994 +0.8000000000000007 1.2800000000000022 +2.4000000000000004 11.520000000000003 +4.0 32.0 diff --git a/test/examples_assertions/dummy_invalid_parameters.yaml b/test/examples_assertions/dummy_invalid_parameters.yaml deleted file mode 100644 index 8d9500e..0000000 --- a/test/examples_assertions/dummy_invalid_parameters.yaml +++ /dev/null @@ -1,29 +0,0 @@ - -iters: 10 - -optimiser: random - - -parameters: - m: [500, 0, 1000] - a: [0, -1000, 1000] - - - -objective: - name: fitting - composite: False - solver: - name: dummy - cases: - 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 - - - references: - 'reference.txt': - prediction: ['modifier_1'] diff --git a/test/examples_assertions/dummy_invalid_parameters2.yaml b/test/examples_assertions/dummy_invalid_parameters2.yaml deleted file mode 100644 index da42c3f..0000000 --- a/test/examples_assertions/dummy_invalid_parameters2.yaml +++ /dev/null @@ -1,30 +0,0 @@ - -iters: 10 - -optimiser: random - - -parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] - a: [0, -1000, 1000] - - - -objective: - name: fitting - composite: False - solver: - name: dummy - cases: - 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 - - - references: - 'reference.txt': - prediction: ['modifier_1'] diff --git a/test/examples_assertions/reference.txt b/test/examples_assertions/reference.txt deleted file mode 100644 index d38197d..0000000 --- a/test/examples_assertions/reference.txt +++ /dev/null @@ -1,5 +0,0 @@ -0.0 0.0 -0.25 50 -0.5 100 -0.75 150 -1.0 200 \ No newline at end of file diff --git a/test/examples_assertions/reference_curve.txt b/test/examples_assertions/reference_curve.txt new file mode 100644 index 0000000..3d109ab --- /dev/null +++ b/test/examples_assertions/reference_curve.txt @@ -0,0 +1,6 @@ +-4.0 32.0 +-2.4 11.52 +-0.7999999999999998 1.2799999999999994 +0.8000000000000007 1.2800000000000022 +2.4000000000000004 11.520000000000003 +4.0 32.0 diff --git a/test/examples_plots/dummy_composite.yaml b/test/examples_plots/dummy_composite.yaml index b836082..273ce16 100644 --- a/test/examples_plots/dummy_composite.yaml +++ b/test/examples_plots/dummy_composite.yaml @@ -6,8 +6,7 @@ optimiser: parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] + a: [0, -4, 4] @@ -15,16 +14,13 @@ objective: name: fitting composite: True solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 - - + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1'] + 'reference_curve.txt': + prediction: ['case_1'] diff --git a/test/examples_plots/dummy_composite_stochastic.yaml b/test/examples_plots/dummy_composite_stochastic.yaml index 6f1e6ed..638ecde 100644 --- a/test/examples_plots/dummy_composite_stochastic.yaml +++ b/test/examples_plots/dummy_composite_stochastic.yaml @@ -6,9 +6,7 @@ optimiser: parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] - + a: [0, -4, 4] objective: @@ -16,22 +14,20 @@ objective: composite: True stochastic: True solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 'case_2': - fields: - 'modifier_2': - name: Modifier - y_scale: 2.0 - y_offset: 0.0 + expression: 1.1 * * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1', 'modifier_2'] + 'reference_curve.txt': + prediction: ['case_1', 'case_2'] diff --git a/test/examples_plots/dummy_design.yaml b/test/examples_plots/dummy_design.yaml index d5e5b32..30be600 100644 --- a/test/examples_plots/dummy_design.yaml +++ b/test/examples_plots/dummy_design.yaml @@ -5,24 +5,22 @@ optimiser: random parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] + a: [2, 0.5, 4] objective: name: design solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 targets: 'maximum_force': quantity: max - prediction: ['modifier_1'] + prediction: ['case_1'] negate: False diff --git a/test/examples_plots/dummy_filtering.yaml b/test/examples_plots/dummy_filtering.yaml index dae72c5..9a7602e 100644 --- a/test/examples_plots/dummy_filtering.yaml +++ b/test/examples_plots/dummy_filtering.yaml @@ -5,8 +5,7 @@ optimiser: random parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] + a: [0, -4, 4] @@ -14,17 +13,14 @@ objective: name: fitting composite: False solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 - - + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1'] + 'reference_curve.txt': + prediction: ['case_1'] filter_tol: 1e-6 diff --git a/test/examples_plots/dummy_simple.yaml b/test/examples_plots/dummy_simple.yaml index 4dec141..4a1726d 100644 --- a/test/examples_plots/dummy_simple.yaml +++ b/test/examples_plots/dummy_simple.yaml @@ -5,8 +5,7 @@ optimiser: random parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] + a: [0, -4, 4] @@ -14,16 +13,13 @@ objective: name: fitting composite: False solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 - - + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1'] + 'reference_curve.txt': + prediction: ['case_1'] diff --git a/test/examples_plots/dummy_simple_stochastic.yaml b/test/examples_plots/dummy_simple_stochastic.yaml index 498e4bf..c1544e8 100644 --- a/test/examples_plots/dummy_simple_stochastic.yaml +++ b/test/examples_plots/dummy_simple_stochastic.yaml @@ -6,9 +6,7 @@ optimiser: parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] - + a: [0, -4, 4] objective: @@ -16,22 +14,20 @@ objective: composite: False stochastic: True solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 'case_2': - fields: - 'modifier_2': - name: Modifier - y_scale: 2.0 - y_offset: 0.0 + expression: 1.1 * * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1', 'modifier_2'] + 'reference_curve.txt': + prediction: ['case_1', 'case_2'] diff --git a/test/examples_plots/dummy_transformer.yaml b/test/examples_plots/dummy_transformer.yaml index a1f0dff..eae67c9 100644 --- a/test/examples_plots/dummy_transformer.yaml +++ b/test/examples_plots/dummy_transformer.yaml @@ -5,28 +5,23 @@ optimiser: random parameters: - m: [500, 0, 1000] - c: [0, -1000, 1000] - + a: [0, -4, 4] objective: name: fitting composite: False solver: - name: dummy + name: curve cases: 'case_1': - fields: - 'modifier_1': - name: Modifier - y_scale: 2.0 - y_offset: -1.0 - - + expression: * x ** 2 + parametric: x + bounds: [-5, 5] + points: 100 references: - 'reference.txt': - prediction: ['modifier_1'] + 'reference_curve.txt': + prediction: ['case_1'] transformer: x_scale: -1 y_scale: -2 diff --git a/test/examples_plots/reference.txt b/test/examples_plots/reference.txt deleted file mode 100644 index d38197d..0000000 --- a/test/examples_plots/reference.txt +++ /dev/null @@ -1,5 +0,0 @@ -0.0 0.0 -0.25 50 -0.5 100 -0.75 150 -1.0 200 \ No newline at end of file diff --git a/test/examples_plots/reference_curve.txt b/test/examples_plots/reference_curve.txt new file mode 100644 index 0000000..3d109ab --- /dev/null +++ b/test/examples_plots/reference_curve.txt @@ -0,0 +1,6 @@ +-4.0 32.0 +-2.4 11.52 +-0.7999999999999998 1.2799999999999994 +0.8000000000000007 1.2800000000000022 +2.4000000000000004 11.520000000000003 +4.0 32.0 diff --git a/test/test_examples_assertions.py b/test/test_examples_assertions.py index 01166cb..e052da6 100644 --- a/test/test_examples_assertions.py +++ b/test/test_examples_assertions.py @@ -80,14 +80,6 @@ RuntimeError, "Failed to parse the config file: YAML syntax seems invalid.", ), - 'dummy_invalid_parameters.yaml': ( - ValueError, - "Invalid parameters: the parameters 'm' and 'c' are required.", - ), - 'dummy_invalid_parameters2.yaml': ( - ValueError, - "Invalid parameters: the parameters 'm' and 'c' are required.", - ), }