helper module
Define general usage functions.
- split_rows_by_masks(df, masks)[source]
Split the rows of
dfinto 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
dfsuch asstyles_from_column_cycle()expect every key ofmasksto 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:
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 areTrueat the same row index, aValueErroris raised.
- Return type:
- 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 dataCSVfile 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 offilepathif it is a string. If it is aPath, we consider it is absolute.extension (
str) – Extension of the output file, with the dot.
- Return type:
- Returns:
A full filepath.
- save_by_position(items, base_path, save_fn, kwargs)[source]
Save keys of
itemsaccording to their key (position).
- r_squared(residue, expected)[source]
Compute the \(R^2\) criterion to evaluate a fit.
For Scipy
curve_fitresultoutput:residueisresult[2]['fvec']andexpectedis the givendata.
- types_match(my_list, to_match)[source]
Check if all elements of
my_listhave typetype.Deprecated since version 1.9.0: Prefer
is_collection_of().
- is_collection_of(coll, typ)[source]
Return True if all elements of
collare instances oftyp.This is a clean replacement of
types_match().- Parameters:
coll (
Collection[object])
- Return type:
TypeGuard[Collection[TypeVar(T)]]
- drop_repeated_col(df, col=None)[source]
Remove consecutive rows with the same
colvalue.If
x_columnis not provided, we take the first column in the dataframe.This function is used with
RPACurrentandRPAPotentialdata.