|
1 |
| -from numbers import Number |
2 |
| - |
3 |
| -import numpy as np |
4 |
| -import pandas as pd |
5 |
| -from beartype import beartype |
6 |
| -from beartype.typing import Optional |
7 |
| - |
8 |
| - |
9 |
| -@beartype |
10 |
| -def _normalize(row: pd.Series, sum_value: Number = 1.0) -> pd.Series: |
11 |
| - """ |
12 |
| - Normalize the series to a given value. |
13 |
| -
|
14 |
| - If no value is provided, normalize to 1. |
15 |
| -
|
16 |
| - Args: |
17 |
| - row: The series to normalize. |
18 |
| -
|
19 |
| - Returns: |
20 |
| - A series containing the normalized values. |
21 |
| - """ |
22 |
| - scale = np.float64(np.sum(row)) / sum_value |
23 |
| - return np.divide(row, scale) |
24 |
| - |
25 |
| - |
26 |
| -@beartype |
27 |
| -def _closure(df: pd.DataFrame, scale: Optional[Number] = None) -> pd.DataFrame: |
28 |
| - """ |
29 |
| - Perform the closure operation on the dataframe. |
30 |
| -
|
31 |
| - If a scale value representing the constant sum is not provided, assumes the standard simplex, |
32 |
| - in which the sum of th components of each composition vector is 1. |
33 |
| -
|
34 |
| - Args: |
35 |
| - df: A dataframe of shape (N, D) compositional data. |
36 |
| - scale: The sum to which each data row should result to. Default is 1. |
37 |
| -
|
38 |
| - Returns: |
39 |
| - A new dataframe of shape (N, D) where each row has been normalized to the given scale value. |
40 |
| - """ |
41 |
| - |
42 |
| - dfc = df.copy() |
43 |
| - |
44 |
| - for idx, row in df.iterrows(): |
45 |
| - dfc.iloc[idx] = _normalize(row, scale) if scale is not None else _normalize(row) |
46 |
| - |
47 |
| - return dfc |
| 1 | +from numbers import Number |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +from beartype import beartype |
| 6 | +from beartype.typing import Optional |
| 7 | + |
| 8 | + |
| 9 | +@beartype |
| 10 | +def _normalize(row: pd.Series, sum_value: Number = 1.0) -> pd.Series: |
| 11 | + """ |
| 12 | + Normalize the series to a given value. |
| 13 | +
|
| 14 | + If no value is provided, normalize to 1. |
| 15 | +
|
| 16 | + Args: |
| 17 | + row: The series to normalize. |
| 18 | +
|
| 19 | + Returns: |
| 20 | + A series containing the normalized values. |
| 21 | + """ |
| 22 | + scale = np.float64(np.sum(row)) / sum_value |
| 23 | + return np.divide(row, scale) |
| 24 | + |
| 25 | + |
| 26 | +@beartype |
| 27 | +def _closure(df: pd.DataFrame, scale: Optional[Number] = None) -> pd.DataFrame: |
| 28 | + """ |
| 29 | + Perform the closure operation on the dataframe. |
| 30 | +
|
| 31 | + Assumes the standard simplex, in which the sum of the components of each composition vector is 1. |
| 32 | +
|
| 33 | + Args: |
| 34 | + df: A dataframe of shape (N, D) compositional data. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + A new dataframe of shape (N, D) where each row has been normalized to 1. |
| 38 | + """ |
| 39 | + |
| 40 | + dfc = df.copy() |
| 41 | + |
| 42 | + for idx, row in df.iterrows(): |
| 43 | + dfc.iloc[idx] = _normalize(row, scale) if scale is not None else _normalize(row) |
| 44 | + |
| 45 | + return dfc |
0 commit comments