202 feature configuration support wildcards and regex for array definitions#206
Conversation
gubaidulinvadim
left a comment
There was a problem hiding this comment.
I've added a test to check that duplicates are still detected if there's a wildcard in the config. Maybe it's unnecessary, I just wanted to be sure.
| - BPM_C03-08 | ||
| - BPM_C03-09 | ||
| - BPM_C03-10 | ||
| - BPM* |
There was a problem hiding this comment.
This might break some examples or at least change behaviour. For BPMs, the order matters too. The correct array should be ordered starting from C04-02.
There was a problem hiding this comment.
In this specific case, it does work because the device declaration order is preserved. The macro at the end causes the sequence to start at C04-02. But I’ll look for a more explicit expression in two steps.
There was a problem hiding this comment.
I actually implemented it in my previous commit:
- re:^BPM_C(0[4-9]|1[0-9]|2[0-9]|3[0-2]).*$
- re:^BPM_C(0[1-3]).*$
I’m not sure it’s really clearer. Which version do you prefer?
There was a problem hiding this comment.
Additionally, I wanted to make sure that the BPM* declaration actually preserves the order. And it does, because the tests would not work otherwise.
There was a problem hiding this comment.
In this specific case, it does work because the device declaration order is preserved. The macro at the end causes the sequence to start at C04-02. But I’ll look for a more explicit expression in two steps.
I think the re: version is more explicit and clearer. With the macro, I would've just thought that the elements are created in that order, but I wouldn't understand that this will be the order of the elements in the array.
If it preserves the order, it's good to go. But I wasn't sure if any tests actually test for order preservation. For example, orbit correction would still work with "incorrect" order of bpms. The order is more of a convention.
I like the idea of @JeanLucPons to have an exclusion wildcard. I'd imagine it's one of the really common cases. For example, let's say we have a faulty BPM and want to quickly check if everything will work correctly if we exclude it from the BPM_ORBIT array.
There was a problem hiding this comment.
Ok i'll look for a set of improvements.
…tion---support-wildcards-and-regex-for-array-definitions
|
Would it be possible to update EBSTune and EBSOrbit using wildcard. EBSTune.yaml (if possible) - type: pyaml.arrays.magnet
name: QForTune
elements:
- QD2*
- QF1*
- !QD2A-C04 # excluded
- !QF1E-C03 # excluded- type: pyaml.arrays.magnet
name: HCorr
elements:
- S*-H
- type: pyaml.arrays.magnet
name: VCorr
elements:
- S*-V
etc... |
I haven’t implemented any behavior to explicitly exclude elements so far. Moreover, since the order matters, I only made very simple and obvious changes. That’s also why I added the possibility to integrate some code. It can become clearer at some point than maintaining a long list of complex regex patterns. |
|
Forget the exclusion the EBSTune.yaml, these 2 magnets are in the lattice but not in the yaml file. |
|
I suggest the following three-step approach:
|
|
I just have a doubt about the sorting. It does not allow users to freely sort the items independently of the device ordering. However, I don’t think this is really a problem in our context. |
I think in the vast majority (if not in all) cases, the elements in families/arrays are sorted with respect to their position in the lattice. There could be some exceptions, for example, if you merge two arrays A1 and A2, I'd imagine the order expected by a user will be [A1[0],... A1[n], A2[0],... A1[n]] and not something that preserves position-in-the-lattice-order. |
Yes, I'm currently adapting |
…ble to add an exclude pattern but a test must be added for that part. Also, the test `test_tune_hardware.py` is not working anymore but it is probably due to some numeric accuracy problems. Those 2 points will be solved later.
|
Ok it's done but I still have un issue with a test: test_tune_hardware.py |
tests/test_tune_hardware.py
Outdated
| assert np.abs(currents[62] - 88.04522942) < 1e-8 | ||
| assert np.abs(currents[63] - 88.26677735) < 1e-8 |
There was a problem hiding this comment.
How did currents[0] and currents[1] become currents[62] and currents[63]? From what I remember, the currents array shape should be (2, )
There was a problem hiding this comment.
For a single CFM, the shape could be (3,). In this case, however, it corresponds to an array of all correctors (QForTune, size 124). Since the order has changed, the matrix computation becomes slightly different, introducing numerical errors. I updated the indices to reflect the new element positions, but the resulting values differ slightly.
|
Consider that you cannot use exclamation mark in ! in yaml. It is a reserved character (not compatible with JSON). |
Ah, you're right! I'll find another solution. |
|
Ok, it's done. I used the |
- type: pyaml.arrays.magnet
name: QForTune
elements:
- QD2*
- QF1*Theoretically i do not expect any difference between the configuration above and the one with the full list. |
I checked, and the two lists were different. The one in the device section and the one using the wildcard are now identical and match the ring ordering. The original |
…tion---support-wildcards-and-regex-for-array-definitions
|
I restored test and native order of devices. |
|
I merge the PR. |

Description
This pull request extends the configuration layer to improve scalability and maintainability of YAML-based definitions.
Two complementary features are introduced:
Pattern-based element selection inside
elements:*or?are interpreted as wildcard patterns.re:are interpreted as regular expressions.Python-based configuration macros via a new optional key:
elements_codeAllows scripted generation of configuration entries directly from YAML.
The embedded Python code must return either:
dict(single entry), orlist[dict](expanded into the surrounding list).These changes are necessary to:
The macro mechanism does not replace external YAML generation scripts. External generation using Python remains fully possible and unaffected.
Related Issue
Features/issues described there are:
*and?directly insideelementsentries to keep the schema unchanged and preserve backward compatibility.re:prefix to explicitly distinguish regular expressions from literal IDs.elements_code, executed before object construction, replacing the macro block with the returned configuration structure.Changes to existing functionality
Array resolution logic was extended to interpret wildcard and regex patterns inside
elementsentries.This was implemented at the configuration parsing level to preserve object model integrity and deterministic ordering.
Configuration parsing pipeline was extended with a pre-processing step detecting
elements_codeblocks.The code is executed in a controlled namespace and must return either a
dictorlist[dict].The returned structure replaces the macro block before normal parsing continues.
Duplicate resolution behavior after pattern expansion was made deterministic.
No existing YAML files are impacted. All previous configurations remain valid.
Testing
The feature is validated primarily through modifications to existing configuration files.
Only YAML configuration files were updated in order to:
elements,re:prefix,elements_code.All existing pytest test suites pass without modification, meaning:
In addition:
test_load_conf_with_codewas addedThis test explicitly verifies
elements_codeexecution and serves as a minimal documented example of configuration scripting.This test is not strictly required for coverage, since the feature is already exercised indirectly through existing tests, but it improves clarity and provides a reference example.
Verify that your checklist complies with the project