helper module

Define general usage functions.

is_nested_list(obj)[source]

Tell if obj is a nested list.

Parameters:

obj (list[TypeVar(T)] | list[list[TypeVar(T)]])

Return type:

bool

flatten(nest)[source]

Flatten nested list of lists of…

Parameters:

nest (Iterable[TypeVar(T)])

Return type:

Iterator[TypeVar(T)]

split_rows_by_masks(df, masks)[source]

Split the rows of df into new columns based on a boolean mask.

For each column in the original data, one new column per mask is created with the corresponding suffix. Rows not selected by a mask are filled with np.nan.

Important

Functions using the splitted df such as styles_from_column_cycle() expect every key of masks to start with a double underscore (__).

Examples

>>> mask = np.array([True, False, True])
>>> masks = {"__(grows)": mask, "__(decreases)": ~mask}
>>> ser = pd.Series([1, 2, 3], name=data)
>>> print(split_rows_by_masks(ser, masks))
   data__(grows) data__(decreases)
0  1.0           NaN
1  NaN           2.0
2  3.0           NaN
>>> df = pd.DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6]})
>>> print(split_rows_by_masks(df, masks))
   col1__(grows)  col1__(decreases)  col2__(grows)  col2__(decreases)
0  1.0            NaN                4.0            NaN
1  NaN            2.0                NaN            5.0
2  3.0            NaN                6.0            NaN
Raises:

ValueError – If any row is matched by more than one mask or if mask lengths do not match the input.

Parameters:
  • df (Series | DataFrame) – The input data to split row-wise.

  • masks (dict[str, ndarray[tuple[Any, ...], dtype[bool]]]) – A dictionary where each key is a suffix used to label the split columns, and each value is a boolean mask of the same length as the input data. Keys must start with two underscores (`__`) to enable consistent column naming and compatibility with downstream styling logic (e.g., grouping lines by base column in plots). If multiple masks are True at the same row index, a ValueError is raised.

Return type:

DataFrame

Returns:

A new DataFrame with columns split according to the masks.

output_filepath(filepath, swr, freq_mhz, out_folder, extension)[source]

Return a new path to save output files.

Parameters:
  • filepath (Path) – Name of the data CSV file from LabViewer.

  • swr (float) – Theoretical \(SWR\) to add to the output file name.

  • freq_mhz (float) – Theoretical rf frequency to add to the output file name.

  • out_folder (str | Path) – Relative name of the folder where data will be saved; it is defined w.r.t. to the parent folder of filepath if it is a string. If it is a Path, we consider it is absolute.

  • extension (str) – Extension of the output file, with the dot.

Return type:

Path

Returns:

A full filepath.

save_by_position(items, base_path, save_fn, kwargs)[source]

Save keys of items according to their key (position).

Parameters:
  • items (dict[float, Any]) – Objects to save, grouped by position.

  • base_path (Path) – Common path of all objects to save.

  • save_fn (Callable) – Function to call for saving the objects.

  • kwargs (dict) – Passed to save_fn.

r_squared(residue, expected)[source]

Compute the \(R^2\) criterion to evaluate a fit.

For Scipy curve_fit result output: residue is result[2]['fvec'] and expected is the given data.

Parameters:
Return type:

float

types(my_list)[source]

Get all different types in given list.

Parameters:

my_list (Sequence)

Return type:

set[type]

types_match(my_list, to_match)[source]

Check if all elements of my_list have type type.

Deprecated since version 1.9.0: Prefer is_collection_of().

Parameters:
Return type:

bool

is_collection_of(coll, typ)[source]

Return True if all elements of coll are instances of typ.

This is a clean replacement of types_match().

Parameters:
Return type:

TypeGuard[Collection[TypeVar(T)]]

drop_repeated_col(df, col=None)[source]

Remove consecutive rows with the same col value.

If x_column is not provided, we take the first column in the dataframe.

This function is used with RPACurrent and RPAPotential data.

Parameters:
Return type:

DataFrame