-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnormalization_utils.py
More file actions
70 lines (56 loc) · 2.02 KB
/
Copy pathnormalization_utils.py
File metadata and controls
70 lines (56 loc) · 2.02 KB
1
2
3
4
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
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
import pathlib
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from load_utils import compile_mitocheck_batch_data, split_data
def get_normalization_scaler(
norm_pop_path: pathlib.Path, dataset: str = "CP_and_DP"
) -> StandardScaler():
"""
get normalization scaler from a normalization population
Parameters
----------
norm_pop_path : pathlib.Path
path to normalization output in form of mitocheck IDR stream output
dataset : str, optional
which dataset columns to load in (in addition to metadata),
can be "CP" or "DP" or by default "CP_and_DP"
Returns
-------
StandardScaler
scaler to be used for data normalization
"""
# get normalization population
norm_pop_data = compile_mitocheck_batch_data(norm_pop_path, dataset)
# derive normalization scaler
_, norm_pop_feature_data = split_data(norm_pop_data, dataset)
scaler = StandardScaler()
scaler.fit(norm_pop_feature_data)
return scaler
def get_normalized_mitocheck_data(
data: pd.DataFrame, scaler: StandardScaler()
) -> pd.DataFrame:
"""
get normalized version of mitocheck data
Parameters
----------
data : pd.DataFrame
data to be normalized, in form of compiled mitocheck IDR output
scaler : StandardScaler
scaler to use for data normalization
Returns
-------
pd.DataFrame
normalized data
"""
# normalize features from data
col_list = data.columns.tolist()
derived_features = [col_name for col_name in col_list if "P__" in col_name]
features = data[derived_features].to_numpy()
features = scaler.transform(features)
# make features a dataframe so it can be combined with metadata
features = pd.DataFrame(features, columns=derived_features)
# replace original features of data with normalized features
metadata = [col_name for col_name in col_list if "P__" not in col_name]
metadata = data[metadata]
return pd.concat([metadata, features], axis=1)