Skip to content

Commit

Permalink
Minor fixes in fake measurement, full response and corrections (#404)
Browse files Browse the repository at this point in the history
* Added path conversions to fake measurement + some debug messages
* Skip modifiers if none (in lhc.py)
* Value error for all-zero weights in correction.
* Bumpversion
  • Loading branch information
JoschD authored Nov 4, 2022
1 parent 7e256b1 commit 61da514
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# OMC3 Changelog

#### 2022-11-01 - v0.6.6

- Bugfixes
- correction: fullresponse is converted to Path.
- fake measurement from model: dont randomize errors and values by default.

#### 2022-10-15 - v0.6.5

Expand All @@ -11,7 +16,6 @@

- Fixed the phase filtering for coupling calculation to not forget columns.


#### 2022-09-27 - v0.6.3

- Pandafied `knob_extractor` internally and python output.
Expand Down
2 changes: 1 addition & 1 deletion omc3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
__title__ = "omc3"
__description__ = "An accelerator physics tools package for the OMC team at CERN."
__url__ = "https://github.com/pylhc/omc3"
__version__ = "0.6.5"
__version__ = "0.6.6"
__author__ = "pylhc"
__author_email__ = "[email protected]"
__license__ = "MIT"
Expand Down
5 changes: 5 additions & 0 deletions omc3/correction/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ def get_measurement_data(
filtered_keys = keys
if w_dict is not None:
filtered_keys = [key for key in keys if w_dict[key] != 0]
if not len(filtered_keys):
raise ValueError(
"All given Parameters have been discarded due to all-zero weights. "
"Check given weights and weight default values."
)

for key in filtered_keys:
if key.startswith(f"{PHASE}"):
Expand Down
9 changes: 7 additions & 2 deletions omc3/global_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
NORM_DISP, PHASE, TUNE)
from omc3.model import manager
from omc3.utils import logging_tools
from omc3.utils.iotools import PathOrStr

LOG = logging_tools.get_logger(__name__)

Expand All @@ -201,11 +202,14 @@ def correction_params():
params = EntryPointParameters()
params.add_parameter(name="meas_dir",
required=True,
type=PathOrStr,
help="Path to the directory containing the measurement files.",)
params.add_parameter(name="output_dir",
required=True,
type=PathOrStr,
help="Path to the directory where to write the output files.", )
params.add_parameter(name="fullresponse_path",
type=PathOrStr,
help="Path to the fullresponse binary file.If not given, "
"calculates the response analytically.",)
params.add_parameter(name="optics_params",
Expand Down Expand Up @@ -293,6 +297,8 @@ def _check_opt_add_dicts(opt: dict) -> dict: # acts inplace...
opt[key] = dict(zip(opt.optics_params, opt[key]))
opt.meas_dir = Path(opt.meas_dir)
opt.output_dir = Path(opt.output_dir)
if opt.fullresponse_path:
opt.fullresponse_path = Path(opt.fullresponse_path)
return opt


Expand Down Expand Up @@ -352,5 +358,4 @@ def _get_default_values() -> Dict[str, Dict[str, float]]:


if __name__ == "__main__":
with logging_tools.DebugMode(active=False, log_file="iterative_correction.log"):
global_correction_entrypoint()
global_correction_entrypoint()
9 changes: 5 additions & 4 deletions omc3/model/accelerators/lhc.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,11 @@ def get_base_madx_script(self, best_knowledge: bool = False) -> str:
f"{self.load_main_seq_madx()}\n"
f"exec, define_nominal_beams();\n"
)
madx_script += "".join(
f"call, file = '{self.model_dir / modifier}'; {MODIFIER_TAG}\n"
for modifier in self.modifiers
)
if self.modifiers is not None:
madx_script += "".join(
f"call, file = '{self.model_dir / modifier}'; {MODIFIER_TAG}\n"
for modifier in self.modifiers
)
madx_script += (
f"\n! ----- Defining Configuration Specifics -----\n"
f"exec, cycle_sequences();\n"
Expand Down
12 changes: 9 additions & 3 deletions omc3/scripts/fake_measurement_from_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
choices: ``['values', 'errors']``
default: ``['values', 'errors']``
default: ``[]``
- **relative_errors** *(float)*:
Expand Down Expand Up @@ -143,7 +143,7 @@ def get_params():
"values and the errors will be equal to the relative error * measurement."
),
choices=[VALUES, ERRORS],
default=[VALUES, ERRORS],
default=[],
type=str,
nargs="*",
)
Expand Down Expand Up @@ -202,6 +202,7 @@ def generate(opt) -> Dict[str, tfs.TfsDataFrame]:
if opt.outputdir is not None:
LOG.info("Writing fake measurements to files.")
output_path = Path(opt.outputdir)
output_path.mkdir(parents=True, exist_ok=True)
for filename, df in results.items():
full_path = output_path / f"{filename}{EXT}"
tfs.write(full_path, df, save_index=NAME)
Expand Down Expand Up @@ -472,11 +473,16 @@ def _get_loop_parameters(parameters: Sequence[str], errors: Sequence[float]) ->

def _get_random_errors(errors: np.array, values: np.array) -> np.array:
""" Creates normal distributed error-values that will not be lower than EPSILON. """
LOG.debug("Calculating normal distributed random errors.")
random_errors = np.zeros_like(errors)
too_small = np.ones_like(errors, dtype=bool)
while sum(too_small):
n_too_small = 1
while n_too_small:
random_errors[too_small] = np.random.normal(errors[too_small], errors[too_small])
too_small = random_errors < EPSILON * np.abs(values)
n_too_small = sum(too_small)
LOG.debug(f"{n_too_small} error values are smaller than given eps.")
LOG.debug("Random errors created.")
return random_errors


Expand Down

0 comments on commit 61da514

Please sign in to comment.