Compute column-wise means and standard deviations for feature normalization.
Zero variance columns will be dropped from the normalizer to avoid a
divide by zero error.
Parameters: |
-
feature_df
(DataFrame )
–
Feature matrix with samples as rows and features as columns.
-
meta_data_df
(Optional[DataFrame] , default:
None
)
–
Metadata aligned row-wise with feature_df . Required if
fit_only_on_control=True .
-
fit_only_on_control
(bool , default:
False
)
–
If True, compute normalization statistics only from control samples
indicated by the _is_control column in meta_data_df .
|
Returns: |
-
Normalizer
–
Object containing per-column means and standard deviations.
|
Source code in src/fisseq_data_pipeline/normalize.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
77
78
79
80
81
82 | def fit_normalizer(
feature_df: pl.DataFrame,
meta_data_df: Optional[pl.DataFrame] = None,
fit_only_on_control: bool = False,
) -> Normalizer:
"""
Compute column-wise means and standard deviations for feature normalization.
Zero variance columns will be dropped from the normalizer to avoid a
divide by zero error.
Parameters
----------
feature_df : pl.DataFrame
Feature matrix with samples as rows and features as columns.
meta_data_df : Optional[pl.DataFrame], default=None
Metadata aligned row-wise with `feature_df`. Required if
``fit_only_on_control=True``.
fit_only_on_control : bool, default=False
If True, compute normalization statistics only from control samples
indicated by the ``_is_control`` column in `meta_data_df`.
Returns
-------
Normalizer
Object containing per-column means and standard deviations.
"""
if fit_only_on_control and meta_data_df is None:
raise ValueError("Meta data required to fit to control samples")
elif fit_only_on_control:
logging.info(
"Filtering control samples, number of samples before filtering=%d",
len(feature_df),
)
feature_df = feature_df.filter(meta_data_df.get_column("_is_control"))
logging.info(
"Filtering complete, remaining train set samples shape=%s",
len(feature_df.shape),
)
logging.info("Fitting Normalizer")
means = feature_df.mean()
stds = feature_df.std()
zero_var_cols = [
k for k, v in stds.row(0, named=True).items() if v < np.finfo(np.float32).eps
]
if len(zero_var_cols) > 0:
logging.warning("Dropping %d zero variance columns", len(zero_var_cols))
means = means.select(pl.exclude(zero_var_cols))
stds = stds.select(pl.exclude(zero_var_cols))
normalizer = Normalizer(means=means, stds=stds)
logging.info("Done")
return normalizer
|