core.list_of_elements.helper module
Define some helper functions to filter list of elements.
Todo
Filtering consistency.
- _get_first_key_of_idx_dict_higher_than(elts: Sequence[Element], *, index_name: str, first_or_last: str, higher_than: int = -1, n_to_check: int = 10) int
Take first valid idx in
n_to_check
first/last elements ofelts
.Typical usage is getting the number of sections or lattice by taking the last element with a section/lattice index higher than -1.
- Parameters:
elts (Sequence[Element]) – List of elements to check.
index_name (str) – Name of the index to get. Must be a key of
Element.idx
.first_or_last ({'first', 'last'}) – If we want to check the
n_to_check
first or last elements.higher_than (int, optional) – The index under which the value is invalid. The default is -1, which is the initialisation index for all the values of the
idx
dictionary.n_to_check (int, optional) – Number of elements in which we will look for the index.
- Returns:
The first valid index that is found.
- Return type:
int
- elt_at_this_s_idx(elts: ListOfElements | Sequence[Element], s_idx: int, show_info: bool = False) Element | None
Give the element where the given index is.
- Parameters:
elts (ListOfElements | list[Element]) – List of elements in which to look for.
s_idx (int) – Index to look for.
show_info (bool) – If the element that we found should be outputed.
- Returns:
elt – Element where the mesh index
s_idx
is inelts
.- Return type:
Element | None
- equivalent_elt(elts: ListOfElements | list[Element], elt: Element | str) Element
Return the element from
elts
corresponding toelt
.Important
This routine uses the name of the element and not its adress. So it will not complain if the
Element
object that you asked for is not in this list of elements. In the contrary, it was meant to find equivalent cavities between different lists of elements.- Parameters:
elts (ListOfElements | list[Element]) – List of elements where you want the position.
elt (Element | str) – Element of which you want the position. If you give a str, it should be the name of an element. If it is an
Element
, we take its name in the routine. Magic keywords'first'
,'last'
are also accepted.
- Returns:
out_elt – Equivalent element.
- Return type:
- equivalent_elt_idx(elts: ListOfElements | list[Element], elt: Element | str) int
Return the index of element from
elts
corresponding toelt
.Important
This routine uses the name of the element and not its adress. So it will not complain if the
Element
object that you asked for is not in this list of elements. In the contrary, it was meant to find equivalent cavities between different lists of elements.See also
- Parameters:
elts (ListOfElements | list[Element]) – List of elements where you want the position.
elt (Element | str) – Element of which you want the position. If you give a str, it should be the name of an element. If it is an
Element
, we take its name in the routine. Magic keywords'first'
,'last'
are also accepted.
- Returns:
Index of equivalent element.
- Return type:
int
- filter_cav(elts: ~core.list_of_elements.helper.ListOfElements | ~typing.Sequence[~core.elements.element.Element], *, type_to_check: ~typing.Type = <class 'core.elements.field_maps.field_map.FieldMap'>) list[Type]
Filter elements according to their type.
Note
Used only for
filter_cav()
, may be simpler?
- filter_elts(elts: ListOfElements | Sequence[Element], type_to_check: Type) list[Type]
Filter elements according to their type.
Note
Used only for
filter_cav()
, may be simpler?
- filter_out(elts: ListOfElements | Sequence[Element] | Sequence[Sequence[Element]], to_exclude: tuple[type]) Any
Filter out types while keeping the input list structure.
Note
Function not used anymore. Keeping it just in case.
- first(iterable: ~collections.abc.Iterable, default: T | None = None, condition: ~collections.abc.Callable[[T], bool] = <function <lambda>>) T
Return the first item in
iterable
satisfyingcondition
.If the condition is not given, returns the first item of the iterable.
If the
default
argument is given and the iterable is empty, or if it has no items matching the condition, thedefault
argument is returned if it matches the condition.The
default
argument being None is the same as it not being given.Raises
StopIteration
if no item satisfying the condition is found and default is not given or doesn’t satisfy the condition.>>> first( (1,2,3), condition=lambda x: x % 2 == 0) 2 >>> first(range(3, 100)) 3 >>> first( () ) Traceback (most recent call last): ... StopIteration >>> first([], default=1) 1 >>> first([], default=1, condition=lambda x: x % 2 == 0) Traceback (most recent call last): ... StopIteration >>> first([1,3,5], default=1, condition=lambda x: x % 2 == 0) Traceback (most recent call last): ... StopIteration
- group_elements_by_lattice(elts: Sequence[Element]) list[list[Element]]
Regroup the Element belonging to the same Lattice.
- group_elements_by_section(elts: Sequence[Element], n_to_check: int = 10) Sequence[Sequence[Element]]
Group elements by section.
- group_elements_by_section_and_lattice(by_section: Sequence[Sequence[Element]]) list[list[list[Element]]]
Regroup Elements by Section and then by Lattice.
- indiv_to_cumul_transf_mat(tm_cumul_in: ndarray, r_zz_elt: list[ndarray], n_steps: int) ndarray
Compute cumulated transfer matrix.
- Parameters:
tm_cumul_in (np.ndarray) – Cumulated transfer matrix @ first element. Should be eye matrix if we are at the first element.
r_zz_elt (list[np.ndarray]) – List of individual transfer matrix of the elements.
n_steps (int) – Number of elements or elements slices.
- Returns:
cumulated_transfer_matrices – Cumulated transfer matrices.
- Return type:
np.ndarray
- is_list_of(elts: Sequence, type_to_check: Type) TypeGuard[Type]
Check that all items of
elts
are of typetype_to_check
.
- is_list_of_elements(elts: Sequence) TypeGuard[list[Element]]
Check that all elements of input are
Element
.