boostlocroc 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ from .main import extract_loc_roc, extract_loc_roc_sklearn
2
+ from .vis import plot_spectrogram, plot_spectrogram_proba, plot_spectrogram_debug
File without changes
@@ -0,0 +1,160 @@
1
+ """Data processing."""
2
+
3
+ import glob
4
+ import os.path as op
5
+ import pickle
6
+
7
+ import numpy as np
8
+ import pandas as pd
9
+ from sklearn.model_selection import GroupShuffleSplit
10
+
11
+
12
+ def get_input_target(data):
13
+ """Get input and target from the psd's labels csv."""
14
+ df = data.copy()
15
+ X = df.iloc[:, :-1].set_index("ID")
16
+ Y = df.iloc[:, [0, -1]].set_index("ID").squeeze()
17
+
18
+ return X, Y
19
+
20
+
21
+ def sample_df(df):
22
+ """Get input and target from the psd's labels csv."""
23
+ df.drop(["epochs"], axis=1, inplace=True)
24
+
25
+ X = df.iloc[:, :-1].set_index("ID")
26
+ Y = df.iloc[:, [0, -1]].set_index("ID").squeeze()
27
+
28
+ return X, Y
29
+
30
+
31
+ def get_subject(X, Y, subject_name, labels):
32
+ """Get the right subject."""
33
+
34
+ subject_label = labels.loc[subject_name]
35
+ y_true = (subject_label.LOC_time, subject_label.ROC_time)
36
+
37
+ return X.loc[subject_name], Y.loc[subject_name], y_true
38
+
39
+
40
+ def get_paths_list(directory, regex):
41
+ """Get a list of path."""
42
+ return np.sort(glob.glob(op.join(directory, regex)))
43
+
44
+
45
+ def get_labels(csv_file, sort_by="ID", sep=";"):
46
+ """Load the labels."""
47
+ # Loading the labels for each patient and sort them by ID
48
+ return pd.read_csv(csv_file, sep=sep).sort_values(sort_by).set_index(sort_by)
49
+
50
+
51
+ def get_lists_of_raw_files():
52
+ """Get a lists of raw_files."""
53
+ parquets_list = get_paths_list(data_src_dir, "*/*" + parquet_extension)
54
+ raws_list = get_paths_list(data_src_dir, "*/*" + raw_extension)
55
+
56
+ return parquets_list, raws_list
57
+
58
+
59
+ # Dataset builder
60
+ def split_dataset(X, Y, test_size=0.2, random_state=42):
61
+ """Split the dataset."""
62
+ # Split data in test/train subgroups according to a provided group
63
+ groups = X.index
64
+
65
+ train_inds, test_inds = next(
66
+ GroupShuffleSplit(
67
+ n_splits=2, test_size=test_size, random_state=random_state
68
+ ).split(X, groups=groups)
69
+ )
70
+ X_train, X_test, y_train, y_test = (
71
+ X.iloc[train_inds],
72
+ X.iloc[test_inds],
73
+ Y[train_inds],
74
+ Y[test_inds],
75
+ )
76
+
77
+ train = (X_train, y_train)
78
+ test = (X_test, y_test)
79
+
80
+ return train, test
81
+
82
+
83
+ def subsample_dataset(X, Y, batching_size_factor=2, random_state=42,
84
+ binary=True):
85
+ """Get input and target from the psd's labels csv with batching a priori.
86
+ """
87
+
88
+ train_set = pd.concat([X, Y], axis=1)
89
+ train_1 = train_set[train_set.labels == 1]
90
+
91
+ if binary:
92
+ train_0 = train_set[train_set.labels == 0]
93
+ else:
94
+ train_0 = train_set[(train_set.labels == 0) | (train_set.labels == 2)]
95
+
96
+ sample_size = int(
97
+ batching_size_factor * len(train_0) / len(train_set.index.unique())
98
+ )
99
+
100
+ train_sample = train_1.groupby("ID").sample(
101
+ sample_size, random_state=random_state, replace=True
102
+ )
103
+ train_batch = pd.concat([train_sample, train_0], axis=0).sort_values(
104
+ by=["ID", "epochs"]
105
+ )
106
+
107
+ train_batch.drop(["epochs"], axis=1, inplace=True)
108
+
109
+ X = train_batch.iloc[:, :-1] # .set_index('ID')
110
+ Y = train_batch.iloc[:, -1] # [0, -1]].set_index('ID').squeeze()
111
+
112
+ return X, Y
113
+
114
+
115
+ def weighted_dataset(X, Y, batching_size_factor=2, random_state=42,
116
+ binary=True):
117
+ """Get input and target from the psd's labels csv with batching a priori.
118
+ """
119
+
120
+ train_set = pd.concat([X, Y], axis=1)
121
+ train_1 = train_set[train_set.labels == 1]
122
+
123
+ if binary:
124
+ train_0 = train_set[train_set.labels == 0]
125
+ else:
126
+ train_0 = train_set[(train_set.labels == 0) | (train_set.labels == 2)]
127
+
128
+ epochs_loc = train_1.groupby("ID").head(6)
129
+ epochs_roc = train_1.groupby("ID").tail(12)
130
+ epochs = (
131
+ train_1.groupby("ID", as_index=False)
132
+ .apply(lambda group: group.iloc[6:-12])
133
+ .reset_index(level="ID")
134
+ .set_index("ID")
135
+ )
136
+ sample_size = int(
137
+ batching_size_factor * len(train_0) / len(train_set.index.unique())
138
+ )
139
+ train_sample = epochs.groupby("ID").sample(
140
+ sample_size, random_state=random_state, replace=True
141
+ )
142
+
143
+ train_batch = pd.concat(
144
+ [train_sample, train_0, epochs_loc, epochs_roc], axis=0
145
+ ).sort_values(by=["ID", "epochs"])
146
+
147
+ train_batch.drop(["epochs"], axis=1, inplace=True)
148
+
149
+ X = train_batch.iloc[:, :-1]
150
+ Y = train_batch.iloc[:, -1]
151
+
152
+ return X, Y
153
+
154
+
155
+ def load_idx_train_test(template_name="cross_val_",
156
+ weights_dir="model_weights"):
157
+ """Load index."""
158
+ pickle_in = open(op.join(weights_dir, f"{template_name}_idx_spliting.pickle"), "rb")
159
+ (idx_train, idx_test) = pickle.load(pickle_in)
160
+ return idx_train, idx_test