helper module

Define some helper functions to filter list of elements.

Todo

Filtering consistency.

is_list_of(elts, type_to_check)[source]

Check that all items of elts are of type type_to_check.

Parameters:
Return type:

TypeGuard[Type]

is_list_of_elements(elts)[source]

Check that all elements of input are Element.

Parameters:

elts (Sequence)

Return type:

TypeGuard[list[Element]]

is_list_of_list_of_elements(elts)[source]

Check that input is a nested list of Element.

Parameters:

elts (Sequence)

Return type:

TypeGuard[list[list[Element]]]

is_list_of_list_of_field_maps(elts)[source]

Check that input is a nested list of Element.

Parameters:

elts (Sequence)

Return type:

TypeGuard[list[list[FieldMap]]]

filter_out(elts, to_exclude)[source]

Filter out types while keeping the input list structure.

Note

Function not used anymore. Keeping it just in case.

Parameters:
Return type:

Any

filter_elts(elts, type_to_check)[source]

Filter elements according to their type.

Note

Used only for filter_cav(), may be simpler?

Parameters:
Return type:

list[Type]

filter_cav(elts, *, type_to_check=<class 'lightwin.core.elements.field_maps.field_map.FieldMap'>)

Filter elements according to their type.

Note

Used only for filter_cav(), may be simpler?

Parameters:
Return type:

list[Type]

elt_at_this_s_idx(elts, s_idx, show_info=False)[source]

Give the element where the given index is.

Parameters:
  • elts (TypeVar(ListOfElements) | Sequence[Element]) – List of elements in which to look for.

  • s_idx (int) – Index to look for.

  • show_info (bool, default: False) – If the element that we found should be outputed.

Return type:

Element | None

Returns:

Element where the mesh index s_idx is in elts.

equivalent_elt_idx(elts, elt)[source]

Return the index of element from elts corresponding to elt.

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 (TypeVar(ListOfElements) | list[Element]) – List of elements where you want the position.

  • elt (str | Element | Literal['first', 'last']) – 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.

Return type:

int

Returns:

Index of equivalent element.

equivalent_elt(elts, elt)[source]
Overloads:
  • elts (ListOfElements | list[Element] | list[FieldMap]), elt (FieldMap) → FieldMap

  • elts (ListOfElements | list[Element] | list[FieldMap]), elt (Element | str | GET_ELT_ARG_T) → Element

Parameters:
Return type:

Element | FieldMap

Return the element from elts corresponding to elt.

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 (TypeVar(ListOfElements) | list[Element] | list[FieldMap]) – List of elements where you want the position.

  • elt (Element | str | FieldMap | Literal['first', 'last']) – 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:

Equivalent element.

Return type:

Element | FieldMap

indiv_to_cumul_transf_mat(tm_cumul_in, r_zz_elt, n_steps)[source]

Compute cumulated transfer matrix.

Parameters:
  • tm_cumul_in (ndarray) – Cumulated transfer matrix @ first element. Should be eye matrix if we are at the first element.

  • r_zz_elt (list[ndarray]) – List of individual transfer matrix of the elements.

  • n_steps (int) – Number of elements or elements slices.

Return type:

ndarray

Returns:

Cumulated transfer matrices.

group_elements_by_section(elts, n_to_check=10)[source]

Group elements by section.

Parameters:
Return type:

list[list[Element]]

group_elements_by_section_and_lattice(by_section)[source]

Regroup Elements by Section and then by Lattice.

Parameters:

by_section (Sequence[Sequence[Element]])

Return type:

list[list[list[Element]]]

group_elements_by_lattice(elts)[source]

Regroup the Element belonging to the same Lattice.

Parameters:

elts (Sequence[Element])

Return type:

list[list[Element]]

_get_first_key_of_idx_dict_higher_than(elts, *, index_name, first_or_last, higher_than=-1, n_to_check=10)[source]

Take first valid idx in n_to_check first/last elements of elts.

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 in the idx attribute of Element.

  • first_or_last (Literal['first', 'last']) – If we want to check the n_to_check first or last elements.

  • higher_than (int, default: -1) – 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, default: 10) – Number of elements in which we will look for the index.

Return type:

int

Returns:

The first valid index that is found.

first(iterable, default=None, condition=<function <lambda> at 0x7318faa86ac0>)[source]

Return the first item in iterable satisfying condition.

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, the default 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
Parameters:
Return type:

TypeVar(T)