Skip to content

Symmetrization

The symmetrization module provides tools for processing data to enforce symmetry or antisymmetry. The symmetrize function averages values with their reversed counterparts to create symmetric datasets, while the antisymmetrize function computes the difference between values and their reversed counterparts to create antisymmetric datasets.

symmetrize(dfs, x_column, y_column, minimum, maximum, step)

Symmetrizes the given dataframes by combining them, binning the onto evenly spaced values of x_column between minimum and maximum, and then averaging the y_column values with their reversed counterparts.

Parameters:

Name Type Description Default
dfs list[DataFrame]

List of dataframes to be symmetrized.

required
x_column str

The name of the column to be used for filtering and sorting.

required
y_column str

The name of the column to be symmetrized.

required
minimum float

The minimum value for filtering the x_column.

required
maximum float

The maximum value for filtering the x_column.

required
step float

The step size for binning the x_column values.

required

Returns:

Type Description
DataFrame

pd.DataFrame: A new dataframe with symmetrized y_column values.

Examples:

>>> import quantalyze as qz
>>> df = qz.symmetrize(
>>>     dfs=[df1, df2],
>>>     x_column='field',
>>>     y_column='voltage',
>>>     minimum=-14,
>>>     maximum=14,
>>>     step=0.05,
>>> )
Source code in src/quantalyze/core/symmetrization.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def symmetrize(dfs, x_column, y_column, minimum, maximum, step) -> pd.DataFrame:
    """
    Symmetrizes the given dataframes by combining them, binning the onto evenly spaced values of x_column
    between minimum and maximum, and then averaging the y_column values with their reversed counterparts.

    Args:
        dfs (list[pd.DataFrame]): List of dataframes to be symmetrized.
        x_column (str): The name of the column to be used for filtering and sorting.
        y_column (str): The name of the column to be symmetrized.
        minimum (float): The minimum value for filtering the x_column.
        maximum (float): The maximum value for filtering the x_column.
        step (float): The step size for binning the x_column values.

    Returns:
        pd.DataFrame: A new dataframe with symmetrized y_column values.

    Examples:
        >>> import quantalyze as qz
        >>> df = qz.symmetrize(
        >>>     dfs=[df1, df2],
        >>>     x_column='field',
        >>>     y_column='voltage',
        >>>     minimum=-14,
        >>>     maximum=14,
        >>>     step=0.05,
        >>> )
    """
    dfs = [df[(df[x_column] >= minimum) & (df[x_column] <= maximum)] for df in dfs]
    combined = pd.concat(dfs)
    combined = combined.sort_values(by=x_column)
    combined = bin(combined, x_column, -maximum, maximum, step)
    new_df = pd.DataFrame(data={x_column: combined[x_column], 'a': combined[y_column], 'b': combined[y_column].values[::-1]})
    new_df[y_column] = (new_df["a"]+new_df["b"])/2
    new_df = new_df.drop(columns=['a', 'b'])
    return new_df

antisymmetrize(dfs, x_column, y_column, minimum, maximum, step)

Antisymmetrizes the given dataframes by combining them, binning the onto evenly spaced values of x_column between minimum and maximum, and then taking the difference between y_column values and their reversed counterparts.

Parameters:

Name Type Description Default
dfs list[DataFrame]

List of dataframes to be antisymmetrized.

required
x_column str

The name of the column representing the x-axis.

required
y_column str

The name of the column representing the y-axis.

required
minimum float

The minimum value of the x-axis range to consider.

required
maximum float

The maximum value of the x-axis range to consider.

required
step float

The step size for binning the x-axis values.

required

Returns:

Type Description
DataFrame

pd.DataFrame: A new dataframe with antisymmetrized y-values.

Examples:

>>> import quantalyze as qz
>>> df = qz.antisymmetrize(
>>>     dfs=[df1, df2],
>>>     x_column='field',
>>>     y_column='voltage',
>>>     minimum=-14,
>>>     maximum=14,
>>>     step=0.05,
>>> )
Source code in src/quantalyze/core/symmetrization.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def antisymmetrize(dfs, x_column, y_column, minimum, maximum, step) -> pd.DataFrame:
    """
    Antisymmetrizes the given dataframes by combining them, binning the onto evenly spaced values of x_column
    between minimum and maximum, and then taking the difference between y_column values and their reversed counterparts.

    Args:
        dfs (list[pd.DataFrame]): List of dataframes to be antisymmetrized.
        x_column (str): The name of the column representing the x-axis.
        y_column (str): The name of the column representing the y-axis.
        minimum (float): The minimum value of the x-axis range to consider.
        maximum (float): The maximum value of the x-axis range to consider.
        step (float): The step size for binning the x-axis values.

    Returns:
        pd.DataFrame: A new dataframe with antisymmetrized y-values.

    Examples:
        >>> import quantalyze as qz
        >>> df = qz.antisymmetrize(
        >>>     dfs=[df1, df2],
        >>>     x_column='field',
        >>>     y_column='voltage',
        >>>     minimum=-14,
        >>>     maximum=14,
        >>>     step=0.05,
        >>> )
    """
    dfs = [df[(df[x_column] >= minimum) & (df[x_column] <= maximum)] for df in dfs]
    combined = pd.concat(dfs)
    combined = combined.sort_values(by=x_column)
    combined = bin(combined, x_column, -maximum, maximum, step)
    new_df = pd.DataFrame(data={x_column: combined[x_column], 'a': combined[y_column], 'b': combined[y_column].values[::-1]})
    new_df[y_column] = (new_df["a"]-new_df["b"])/2
    new_df = new_df.drop(columns=['a', 'b'])
    return new_df