desinez commited on
Commit
72a7da3
·
1 Parent(s): 1a72e1f

model packaging updates

Browse files
backend/PvDeepSetNet/datasets/pvAmpSeq_results_multi_prior.csv ADDED
The diff for this file is too large to render. See raw diff
 
backend/PvDeepSetNet/src/DeepSets/Build_tensors.py DELETED
@@ -1,81 +0,0 @@
1
- import torch
2
- import pandas as pd
3
- import json
4
- from collections import defaultdict
5
-
6
- def build_deepsets_tensors(df, M_max = 11, A_max = 5, purpose='prediction', prior='not_given'):
7
- """
8
- df = dataframe
9
- M_max = the maximum number of unique marker within the dataset (default 11 <- used during training)
10
- A_max = the maximum number of unique allele per marker (default 5 <- used during training)
11
- allele_to_id = alleles with their corresponding ids
12
- Optimized Deep Sets preprocessing for Pv3Rs surrogate
13
- """
14
- # checking the existance of prior columns, otherwise the default values will be assigned
15
- required_cols = {'prior_C', 'prior_L', 'prior_I'}
16
- if not required_cols.issubset(df.columns):
17
- df['prior_C']=df['prior_L']=df['prior_I']=1/3
18
-
19
-
20
-
21
-
22
- with open('allele_dict.json', 'r') as file:
23
- allele_to_id = json.load(file)
24
-
25
-
26
- # ----------------------------------------------------
27
- # 1. Encode categorical variables once
28
- # ----------------------------------------------------
29
- marker_to_id = {m: i + 1 for i, m in enumerate(df.marker.unique())}
30
- allele_to_id = allele_to_id
31
-
32
- grouped = list(df.groupby("sample_id_paired")) #computationally intensive
33
- N = len(grouped)
34
-
35
- # ----------------------------------------------------
36
- # 2. Allocate tensors
37
- # ----------------------------------------------------
38
- X_alleles = torch.zeros((N, M_max, 2, A_max, 2))
39
- allele_mask = torch.zeros((N, M_max, 2, A_max), dtype=torch.bool)
40
- marker_mask = torch.zeros((N, M_max), dtype=torch.bool)
41
-
42
- priors = torch.zeros((N, 3))
43
- MOI = torch.zeros((N, 2))
44
-
45
- # ----------------------------------------------------
46
- # 3. Main loop (optimized)
47
- # ----------------------------------------------------
48
- for i, (_, df_pair) in enumerate(grouped):
49
- # ---- pair-level values (constant)
50
- priors[i] = torch.tensor(df_pair[["prior_C", "prior_L", "prior_I"]].iloc[0].values)
51
- MOI1 = df_pair.loc[df_pair.episode == 1, 'MOI'].iloc[0]
52
- MOI2 = df_pair.loc[df_pair.episode == 2, 'MOI'].iloc[0]
53
-
54
- MOI[i] = torch.tensor([MOI1, MOI2])
55
-
56
- # ---- pre-group alleles by (marker, episode)
57
- allele_dict = defaultdict(list)
58
- for row in df_pair.itertuples():
59
- allele_dict[(row.marker, row.episode)].append((row.allele, row.frequency))
60
-
61
- # ---- marker union
62
- markers = sorted({m for (m, _) in allele_dict.keys()})[:M_max]
63
-
64
- for m_idx, marker in enumerate(markers):
65
- marker_mask[i, m_idx] = True
66
-
67
- for e_idx, episode in enumerate([1, 2]):
68
- alleles = allele_dict.get((marker, episode), [])
69
-
70
- for a_idx, (allele, freq) in enumerate(alleles[:A_max]):
71
- X_alleles[i, m_idx, e_idx, a_idx, 0] = allele_to_id[allele]
72
- X_alleles[i, m_idx, e_idx, a_idx, 1] = freq
73
- allele_mask[i, m_idx, e_idx, a_idx] = True
74
-
75
- return {
76
- "X_alleles": X_alleles,
77
- "allele_mask": allele_mask,
78
- "marker_mask": marker_mask,
79
- "priors": priors,
80
- "MOI": MOI,
81
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/PvDeepSetNet/src/DeepSets/DeepSets_pipeline.py CHANGED
@@ -1,24 +1,44 @@
1
- import numpy as np
2
- import seaborn as sns
3
- import matplotlib.pyplot as plt
4
  from sklearn.metrics import r2_score
5
- from DeepSets import DataPreprocessor, build_deepsets_tensors
6
 
7
 
8
 
 
9
 
10
- def run_Prediction(data, mode='prediction'):
11
-
12
- # perform basic preprocess
13
- preprocessed_tensor = (
 
14
  data
15
  .pipe(DataPreprocessor.removing_single_episode_patients)
16
  .pipe(DataPreprocessor.calculate_population_Allele_frequencies, population='country')
17
  .pipe(DataPreprocessor.calculate_MOIs)
18
  .pipe(DataPreprocessor.paired_episode_assigner)
19
- .pipe(DataPreprocessor.build_deepsets_tensors, mode=mode)
20
  )
21
- loaded_data = DataPreprocessor.load_tensor(preprocessed_tensor, mode)
22
- return loaded_data
 
 
 
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
 
1
+ import json
2
+ import torch
 
3
  from sklearn.metrics import r2_score
4
+ from DeepSets import DataPreprocessor, PvDeepSet_model
5
 
6
 
7
 
8
+ def run_prediction(data, mode='prediction'):
9
 
10
+ #-------------------------------------#
11
+ # 1. perform basic preprocess
12
+ #-------------------------------------#
13
+
14
+ preprocessed_data, meta_df = (
15
  data
16
  .pipe(DataPreprocessor.removing_single_episode_patients)
17
  .pipe(DataPreprocessor.calculate_population_Allele_frequencies, population='country')
18
  .pipe(DataPreprocessor.calculate_MOIs)
19
  .pipe(DataPreprocessor.paired_episode_assigner)
 
20
  )
21
+ tensors = DataPreprocessor.build_deepsets_tensors(df=preprocessed_data, mode=mode)
22
+ loaded_data = DataPreprocessor.load_tensor(tensors, mode)
23
+
24
+ #-------------------------------------#
25
+ # 2. model prediction and evaluation
26
+ #-------------------------------------#
27
 
28
+ if mode == 'prediction':
29
+ return PvDeepSet_model.run_prediction(meta_df=meta_df,
30
+ pair_id=tensors['pair_id'],
31
+ data_loader = loaded_data,
32
+ basic_summary=True)
33
+ elif mode == 'evaluation':
34
+ return PvDeepSet_model.run_evaluation(meta_df=meta_df,
35
+ pair_id=tensors['pair_id'],
36
+ data_loader=loaded_data,
37
+ probability_plot = True,
38
+ metrics=True,
39
+ confusion_matrix=True,
40
+ basic_summary=True)
41
+ else:
42
+ return 'incorrect mode'
43
+
44
 
backend/PvDeepSetNet/src/DeepSets/Model_result.py ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import json
3
+ import pandas as pd
4
+ from DeepSets import PvDeepSets
5
+ from sklearn.metrics import classification_report, confusion_matrix
6
+
7
+
8
+
9
+
10
+ import numpy as np
11
+ import seaborn as sns
12
+ import matplotlib.pyplot as plt
13
+ from sklearn.metrics import r2_score
14
+
15
+
16
+ class PvDeepSet_model:
17
+ @staticmethod
18
+ def load_model_state():
19
+ #-------------------------------------#
20
+ # 2. Loading model state dict
21
+ #-------------------------------------#
22
+
23
+ # reading allele dictionary
24
+ with open('allele_dict.json', 'r') as file:
25
+ allele_to_id = json.load(file)
26
+
27
+ # Define the device
28
+ device = torch.device("cpu")
29
+
30
+ # Initialize the model with the same parameters used during training
31
+ # Make sure len(allele_to_id) matches the training setup!
32
+ n_alleles = len(allele_to_id)
33
+ model = PvDeepSets(n_alleles=n_alleles)
34
+ print(n_alleles)
35
+ # Load the weights (state_dict)
36
+ model.load_state_dict(torch.load("../src/DeepSets/pv_deepsets_weights20260402.pth", map_location=device))
37
+
38
+ return device, model
39
+
40
+ @staticmethod
41
+ def get_model_results(model, dataloader, device, threshold=0.5, mode='prediction'):
42
+ model.eval()
43
+ all_predicted = []
44
+ all_actual = []
45
+
46
+ with torch.no_grad():
47
+ for batch in dataloader:
48
+ # Flexible unpacking: if evaluating, 'batch' has one extra item (y)
49
+ if mode == 'evaluation':
50
+ X_alleles, allele_mask, marker_mask, priors, MOI, y = batch
51
+ true_labels = torch.argmax(y, dim=1)
52
+ all_actual.append(y.cpu().numpy())
53
+ elif mode == 'prediction':
54
+ X_alleles, allele_mask, marker_mask, priors, MOI = batch
55
+
56
+ # Model Forward Pass
57
+ y_pred = model(X_alleles.to(device), allele_mask.to(device),
58
+ marker_mask.to(device), priors.to(device), MOI.to(device))
59
+
60
+ probs = y_pred # Assuming Softmax is handled inside or by thresholding
61
+
62
+ all_predicted.append(y_pred.cpu().numpy())
63
+
64
+ # Consolidate results
65
+ pred_stack = np.vstack(all_predicted)
66
+
67
+ if mode == 'evaluation':
68
+ actual_stack = np.vstack(all_actual)
69
+ return actual_stack, pred_stack
70
+
71
+ return pred_stack
72
+
73
+ @staticmethod
74
+ def run_prediction(meta_df,
75
+ pair_id,
76
+ data_loader,
77
+ basic_summary=True):
78
+
79
+ # 1. call a function that load saved model state
80
+ device, model = PvDeepSet_model.load_model_state()
81
+
82
+ # 2. get model prediction result
83
+ # Shape: [N, 3] -> Column 0: C, Column 1: L, Column 2: I
84
+ predicted_probs = PvDeepSet_model.get_model_results(model=model, dataloader=data_loader, device=device, mode='prediction')
85
+
86
+ # 3. Merge IDs with Probabilities
87
+ # Create a DataFrame from the results
88
+ results_df = pd.DataFrame(
89
+ predicted_probs,
90
+ columns=['prob_C', 'prob_L', 'prob_I']
91
+ )
92
+
93
+ # Insert the pair_id as the first column
94
+ results_df.insert(0, 'pair_id', pair_id)
95
+
96
+ # 4. Hardclass Classification with Threshold Logic
97
+ def classify_row(row):
98
+ probs = [row['prob_C'], row['prob_L'], row['prob_I']]
99
+ max_p = max(probs)
100
+
101
+ # Check if the highest probability meets the 0.5 threshold
102
+ if max_p < 0.5:
103
+ return "undefined"
104
+
105
+ # Otherwise, find the name of the max class
106
+ class_names = ['C', 'L', 'I']
107
+ return class_names[np.argmax(probs)]
108
+
109
+ # Apply the logic to create the new column
110
+ results_df['predicted_class'] = results_df.apply(classify_row, axis=1)
111
+
112
+ def get_basic_summary(result_df):
113
+ return f"\n--- Prediction Summary --- {result_df['predicted_class'].value_counts()} --------------------------\n"
114
+
115
+ results_df = results_df.merge(meta_df, left_on='pair_id', right_on='sample_id_paired', how='inner')
116
+
117
+ columns = ['pair_id', 'patient_id', 'true_episode', 'prob_C', 'prob_L', 'prob_I', 'predicted_class']
118
+ results = {
119
+ 'results_table':results_df[columns],
120
+ 'basic_summary': get_basic_summary(results_df)
121
+ }
122
+
123
+ return results
124
+
125
+ def run_evaluation(meta_df,
126
+ pair_id,
127
+ data_loader,
128
+ probability_plot = True,
129
+ metrics=True,
130
+ confusion_matrix=True,
131
+ basic_summary=True):
132
+
133
+ # 1. Load model state
134
+ device, model = PvDeepSet_model.load_model_state()
135
+
136
+ # 2. Get probabilities
137
+ actual_probs, predicted_probs = PvDeepSet_model.get_model_results(
138
+ model=model, dataloader=data_loader, device=device, mode='evaluation'
139
+ )
140
+
141
+ class_names = ['C', 'L', 'I']
142
+
143
+ # --- 3. CREATE CONSOLIDATED RESULTS TABLE ---
144
+ eval_df = pd.DataFrame({
145
+ 'pair_id': pair_id,
146
+ 'true_C': actual_probs[:, 0],
147
+ 'true_L': actual_probs[:, 1],
148
+ 'true_I': actual_probs[:, 2],
149
+ 'pred_C': predicted_probs[:, 0],
150
+ 'pred_L': predicted_probs[:, 1],
151
+ 'pred_I': predicted_probs[:, 2]
152
+ })
153
+
154
+ eval_df['true_class'] = [class_names[i] for i in np.argmax(actual_probs, axis=1)]
155
+
156
+ def classify_with_threshold(row):
157
+ probs = [row['pred_C'], row['pred_L'], row['pred_I']]
158
+ max_p = max(probs)
159
+ if max_p < 0.5:
160
+ return "undefined"
161
+ return class_names[np.argmax(probs)]
162
+
163
+ eval_df['predicted_class'] = eval_df.apply(classify_with_threshold, axis=1)
164
+
165
+ # --- 4. PREPARE DATA FOR PLOTS ---
166
+ name_to_idx = {name: i for i, name in enumerate(class_names)}
167
+ y_true_hard = np.array([name_to_idx[c] for c in eval_df['true_class']])
168
+ y_pred_hard = np.array([name_to_idx.get(c, -1) for c in eval_df['predicted_class']])
169
+
170
+ mask = y_pred_hard != -1
171
+ y_true_filtered = y_true_hard[mask]
172
+ y_pred_filtered = y_pred_hard[mask]
173
+
174
+ # --- INNER FUNCTION 1: PROBABILITY REGRESSION ---
175
+ def get_basic_summary(eval_df):
176
+ return f"\n--- Prediction Summary --- {eval_df['predicted_class'].value_counts()} --------------------------\n"
177
+
178
+ # --- INNER FUNCTION 2: PROBABILITY REGRESSION ---
179
+ def get_prob_plot(y_true, y_prob, names):
180
+ fig, axes = plt.subplots(1, 3, figsize=(18, 6), sharey=True)
181
+ for i, ax in enumerate(axes):
182
+ actual, predicted = y_true[:, i], y_prob[:, i]
183
+ jitter = np.random.normal(0, 0.008, size=actual.shape)
184
+ sns.regplot(x=actual + jitter, y=predicted, ax=ax,
185
+ scatter_kws={'alpha': 0.3, 's': 8, 'color': "#1672cf"},
186
+ line_kws={'color': '#e74c3c', 'linewidth': 2.5})
187
+
188
+ ax.set_title(f"{names[i]}\n$R^2 = {r2_score(actual, predicted):.3f}$", pad=20, fontsize=15)
189
+ ax.set_xlim(-0.05, 1.05); ax.set_ylim(-0.05, 1.05)
190
+ plt.tight_layout()
191
+ plt.close(fig)
192
+ return fig
193
+
194
+ # --- INNER FUNCTION 3: METRICS BAR & TABLE ---
195
+ def get_metrics_plot(y_true, y_pred, names):
196
+ report = classification_report(y_true, y_pred, target_names=names, output_dict=True)
197
+ df = pd.DataFrame(report).transpose()
198
+
199
+ fig = plt.figure(figsize=(8, 8))
200
+ gs = fig.add_gridspec(2, 1, height_ratios=[1, 0.5], hspace=0.3)
201
+ ax_bar = fig.add_subplot(gs[0])
202
+ ax_tbl = fig.add_subplot(gs[1])
203
+
204
+ # Bar Chart
205
+ df.loc[names, ['precision', 'recall', 'f1-score']].plot(kind='bar', ax=ax_bar, color=['#1672cf', '#4da1ff', '#aecff2'])
206
+ ax_bar.set_title("Performance Metrics", fontsize=15, weight='bold')
207
+ ax_bar.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=3)
208
+
209
+ # Table
210
+ ax_tbl.axis('off')
211
+ the_table = ax_tbl.table(cellText=df.round(3).values, rowLabels=df.index,
212
+ colLabels=df.columns, loc='center', cellLoc='center')
213
+ the_table.scale(1, 1.5)
214
+
215
+ plt.close(fig)
216
+ return fig
217
+
218
+ # --- INNER FUNCTION 4: CONFUSION MATRIX ---
219
+ def get_conf_matrix_plot(y_true, y_pred, names):
220
+ cm = confusion_matrix(y_true, y_pred)
221
+ cm_perc = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
222
+ annot_labels = (np.array([f"{c}\n({p:.1%})" for c, p in zip(cm.flatten(), cm_perc.flatten())])).reshape(3, 3)
223
+
224
+ fig, ax = plt.subplots(figsize=(7, 6))
225
+ sns.heatmap(cm_perc, annot=annot_labels, fmt="", cmap="Blues", ax=ax,
226
+ xticklabels=names, yticklabels=names, annot_kws={"size": 12, "weight": "bold"})
227
+ ax.set_title("Confusion Matrix", pad=20, fontsize=15, weight='bold')
228
+ ax.set_xlabel('Predicted'); ax.set_ylabel('True')
229
+
230
+ plt.close(fig)
231
+ return fig
232
+
233
+ results_df = eval_df.merge(meta_df, left_on='pair_id', right_on='sample_id_paired', how='inner')
234
+
235
+ # 5. Execute and Collect Plots
236
+ results = {
237
+ "results_table": results_df,
238
+ "data": (actual_probs, predicted_probs, y_true_hard, y_pred_hard)
239
+ }
240
+
241
+ if basic_summary:
242
+ results['basic_summary'] = get_basic_summary(eval_df)
243
+ if probability_plot:
244
+ results['prob_plot'] = get_prob_plot(actual_probs, predicted_probs, class_names)
245
+
246
+ if metrics:
247
+ results['metrics_plot'] = get_metrics_plot(y_true_filtered, y_pred_filtered, class_names)
248
+
249
+ if confusion_matrix:
250
+ results['conf_plot'] = get_conf_matrix_plot(y_true_filtered, y_pred_filtered, class_names)
251
+
252
+ return results
253
+
backend/PvDeepSetNet/src/DeepSets/Preprocess.py CHANGED
@@ -132,7 +132,9 @@ class DataPreprocessor:
132
 
133
  df.rename(columns={'episode':'true_episode', 'episode_order':'episode'}, inplace=True)
134
 
135
- return df
 
 
136
 
137
  @staticmethod
138
  def build_deepsets_tensors(df, M_max=11, A_max=5, mode='prediction'):
@@ -177,12 +179,14 @@ class DataPreprocessor:
177
  priors = torch.zeros((N, 3))
178
  MOI = torch.zeros((N, 2))
179
  y = torch.zeros((N, 3))
 
180
 
181
  # ----------------------------------------------------
182
  # 3. Main loop (optimized)
183
  # ----------------------------------------------------
184
  for i, (_, df_pair) in enumerate(grouped):
185
  # ---- pair-level values (constant)
 
186
  priors[i] = torch.tensor(df_pair[["prior_C", "prior_L", "prior_I"]].iloc[0].values)
187
  MOI1 = df_pair.loc[df_pair.episode == 1, 'MOI'].iloc[0]
188
  MOI2 = df_pair.loc[df_pair.episode == 2, 'MOI'].iloc[0]
@@ -210,7 +214,7 @@ class DataPreprocessor:
210
  X_alleles[i, m_idx, e_idx, a_idx, 1] = freq
211
  allele_mask[i, m_idx, e_idx, a_idx] = True
212
  final_tensors = {
213
- "X_alleles": X_alleles, "allele_mask": allele_mask,
214
  "marker_mask": marker_mask, "priors": priors,
215
  "MOI": MOI
216
  }
 
132
 
133
  df.rename(columns={'episode':'true_episode', 'episode_order':'episode'}, inplace=True)
134
 
135
+ meta_info = df[['sample_id_paired', 'patient_id', 'true_episode']].drop_duplicates().reset_index(drop=True)
136
+
137
+ return df, meta_info
138
 
139
  @staticmethod
140
  def build_deepsets_tensors(df, M_max=11, A_max=5, mode='prediction'):
 
179
  priors = torch.zeros((N, 3))
180
  MOI = torch.zeros((N, 2))
181
  y = torch.zeros((N, 3))
182
+ pair_name = [None]*N
183
 
184
  # ----------------------------------------------------
185
  # 3. Main loop (optimized)
186
  # ----------------------------------------------------
187
  for i, (_, df_pair) in enumerate(grouped):
188
  # ---- pair-level values (constant)
189
+ pair_name[i] = _
190
  priors[i] = torch.tensor(df_pair[["prior_C", "prior_L", "prior_I"]].iloc[0].values)
191
  MOI1 = df_pair.loc[df_pair.episode == 1, 'MOI'].iloc[0]
192
  MOI2 = df_pair.loc[df_pair.episode == 2, 'MOI'].iloc[0]
 
214
  X_alleles[i, m_idx, e_idx, a_idx, 1] = freq
215
  allele_mask[i, m_idx, e_idx, a_idx] = True
216
  final_tensors = {
217
+ "pair_id": pair_name, "X_alleles": X_alleles, "allele_mask": allele_mask,
218
  "marker_mask": marker_mask, "priors": priors,
219
  "MOI": MOI
220
  }
backend/PvDeepSetNet/src/DeepSets/__init__.py CHANGED
@@ -1,3 +1,5 @@
1
  from .Build_tensors import build_deepsets_tensors
2
  from .Preprocess import DataPreprocessor
3
- from .Model import PvDeepSets
 
 
 
1
  from .Build_tensors import build_deepsets_tensors
2
  from .Preprocess import DataPreprocessor
3
+ from .Model import PvDeepSets
4
+ from .DeepSets_pipeline import run_prediction
5
+ from .Model_result import PvDeepSet_model
backend/PvDeepSetNet/src/DeepSets/__pycache__/DeepSets_pipeline.cpython-310.pyc ADDED
Binary file (1.07 kB). View file
 
backend/PvDeepSetNet/src/DeepSets/__pycache__/Model_result.cpython-310.pyc ADDED
Binary file (7.86 kB). View file
 
backend/PvDeepSetNet/src/DeepSets/__pycache__/Preprocess.cpython-310.pyc CHANGED
Binary files a/backend/PvDeepSetNet/src/DeepSets/__pycache__/Preprocess.cpython-310.pyc and b/backend/PvDeepSetNet/src/DeepSets/__pycache__/Preprocess.cpython-310.pyc differ
 
backend/PvDeepSetNet/src/DeepSets/__pycache__/__init__.cpython-310.pyc CHANGED
Binary files a/backend/PvDeepSetNet/src/DeepSets/__pycache__/__init__.cpython-310.pyc and b/backend/PvDeepSetNet/src/DeepSets/__pycache__/__init__.cpython-310.pyc differ
 
backend/PvDeepSetNet/tests/temp.ipynb CHANGED
@@ -10,7 +10,7 @@
10
  },
11
  {
12
  "cell_type": "code",
13
- "execution_count": 1,
14
  "id": "580a4347",
15
  "metadata": {},
16
  "outputs": [
@@ -101,20 +101,108 @@
101
  " <td>ethiopia</td>\n",
102
  " <td>3</td>\n",
103
  " </tr>\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  " </tbody>\n",
105
  "</table>\n",
 
106
  "</div>"
107
  ],
108
  "text/plain": [
109
- " patient_id sample_id episode marker allele frequency country MOIs\n",
110
- "0 Sero-040 Sero-040-R01 1.0 Chr01 Chr01-1 0.531524 ethiopia 6\n",
111
- "1 Sero-182 Sero-182-R12 3.0 Chr11 Chr11-2 0.149320 ethiopia 3\n",
112
- "2 Sero-182 Sero-182-R12 3.0 Chr13 Chr13-1 0.729045 ethiopia 3\n",
113
- "3 Sero-182 Sero-182-R12 3.0 Chr13 Chr13-2 0.129575 ethiopia 3\n",
114
- "4 Sero-182 Sero-182-R12 3.0 Chr14 Chr14-1 0.931269 ethiopia 3"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  ]
116
  },
117
- "execution_count": 1,
118
  "metadata": {},
119
  "output_type": "execute_result"
120
  }
@@ -122,7 +210,429 @@
122
  "source": [
123
  "import pandas as pd\n",
124
  "real_data = pd.read_csv('../datasets/pvAmpSeq_simulation_ready.csv')\n",
125
- "real_data.head()"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  ]
127
  },
128
  {
@@ -289,23 +799,415 @@
289
  },
290
  {
291
  "cell_type": "code",
292
- "execution_count": 17,
293
  "id": "8f6bda0c",
294
  "metadata": {},
295
  "outputs": [
296
  {
297
  "data": {
298
  "text/plain": [
299
- "np.float32(1.0)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  ]
301
  },
302
- "execution_count": 17,
303
  "metadata": {},
304
  "output_type": "execute_result"
305
  }
306
  ],
307
  "source": [
308
- "predicted_probs[1].sum()"
309
  ]
310
  },
311
  {
 
10
  },
11
  {
12
  "cell_type": "code",
13
+ "execution_count": 67,
14
  "id": "580a4347",
15
  "metadata": {},
16
  "outputs": [
 
101
  " <td>ethiopia</td>\n",
102
  " <td>3</td>\n",
103
  " </tr>\n",
104
+ " <tr>\n",
105
+ " <th>...</th>\n",
106
+ " <td>...</td>\n",
107
+ " <td>...</td>\n",
108
+ " <td>...</td>\n",
109
+ " <td>...</td>\n",
110
+ " <td>...</td>\n",
111
+ " <td>...</td>\n",
112
+ " <td>...</td>\n",
113
+ " <td>...</td>\n",
114
+ " </tr>\n",
115
+ " <tr>\n",
116
+ " <th>6734</th>\n",
117
+ " <td>AR-322</td>\n",
118
+ " <td>AR-322-140</td>\n",
119
+ " <td>2.0</td>\n",
120
+ " <td>Chr03</td>\n",
121
+ " <td>Chr03-1</td>\n",
122
+ " <td>0.526098</td>\n",
123
+ " <td>solomon_island</td>\n",
124
+ " <td>2</td>\n",
125
+ " </tr>\n",
126
+ " <tr>\n",
127
+ " <th>6735</th>\n",
128
+ " <td>AR-322</td>\n",
129
+ " <td>AR-322-140</td>\n",
130
+ " <td>2.0</td>\n",
131
+ " <td>Chr02</td>\n",
132
+ " <td>Chr02-2</td>\n",
133
+ " <td>0.148837</td>\n",
134
+ " <td>solomon_island</td>\n",
135
+ " <td>2</td>\n",
136
+ " </tr>\n",
137
+ " <tr>\n",
138
+ " <th>6736</th>\n",
139
+ " <td>AR-322</td>\n",
140
+ " <td>AR-322-140</td>\n",
141
+ " <td>2.0</td>\n",
142
+ " <td>Chr02</td>\n",
143
+ " <td>Chr02-6</td>\n",
144
+ " <td>0.042156</td>\n",
145
+ " <td>solomon_island</td>\n",
146
+ " <td>2</td>\n",
147
+ " </tr>\n",
148
+ " <tr>\n",
149
+ " <th>6737</th>\n",
150
+ " <td>AR-302</td>\n",
151
+ " <td>AR-302-0</td>\n",
152
+ " <td>2.0</td>\n",
153
+ " <td>Chr11</td>\n",
154
+ " <td>Chr11-2</td>\n",
155
+ " <td>0.232755</td>\n",
156
+ " <td>solomon_island</td>\n",
157
+ " <td>2</td>\n",
158
+ " </tr>\n",
159
+ " <tr>\n",
160
+ " <th>6738</th>\n",
161
+ " <td>AR-382</td>\n",
162
+ " <td>AR-382-168</td>\n",
163
+ " <td>2.0</td>\n",
164
+ " <td>Chr14</td>\n",
165
+ " <td>Chr14-1</td>\n",
166
+ " <td>0.244186</td>\n",
167
+ " <td>solomon_island</td>\n",
168
+ " <td>1</td>\n",
169
+ " </tr>\n",
170
  " </tbody>\n",
171
  "</table>\n",
172
+ "<p>6739 rows × 8 columns</p>\n",
173
  "</div>"
174
  ],
175
  "text/plain": [
176
+ " patient_id sample_id episode marker allele frequency \\\n",
177
+ "0 Sero-040 Sero-040-R01 1.0 Chr01 Chr01-1 0.531524 \n",
178
+ "1 Sero-182 Sero-182-R12 3.0 Chr11 Chr11-2 0.149320 \n",
179
+ "2 Sero-182 Sero-182-R12 3.0 Chr13 Chr13-1 0.729045 \n",
180
+ "3 Sero-182 Sero-182-R12 3.0 Chr13 Chr13-2 0.129575 \n",
181
+ "4 Sero-182 Sero-182-R12 3.0 Chr14 Chr14-1 0.931269 \n",
182
+ "... ... ... ... ... ... ... \n",
183
+ "6734 AR-322 AR-322-140 2.0 Chr03 Chr03-1 0.526098 \n",
184
+ "6735 AR-322 AR-322-140 2.0 Chr02 Chr02-2 0.148837 \n",
185
+ "6736 AR-322 AR-322-140 2.0 Chr02 Chr02-6 0.042156 \n",
186
+ "6737 AR-302 AR-302-0 2.0 Chr11 Chr11-2 0.232755 \n",
187
+ "6738 AR-382 AR-382-168 2.0 Chr14 Chr14-1 0.244186 \n",
188
+ "\n",
189
+ " country MOIs \n",
190
+ "0 ethiopia 6 \n",
191
+ "1 ethiopia 3 \n",
192
+ "2 ethiopia 3 \n",
193
+ "3 ethiopia 3 \n",
194
+ "4 ethiopia 3 \n",
195
+ "... ... ... \n",
196
+ "6734 solomon_island 2 \n",
197
+ "6735 solomon_island 2 \n",
198
+ "6736 solomon_island 2 \n",
199
+ "6737 solomon_island 2 \n",
200
+ "6738 solomon_island 1 \n",
201
+ "\n",
202
+ "[6739 rows x 8 columns]"
203
  ]
204
  },
205
+ "execution_count": 67,
206
  "metadata": {},
207
  "output_type": "execute_result"
208
  }
 
210
  "source": [
211
  "import pandas as pd\n",
212
  "real_data = pd.read_csv('../datasets/pvAmpSeq_simulation_ready.csv')\n",
213
+ "real_data"
214
+ ]
215
+ },
216
+ {
217
+ "cell_type": "code",
218
+ "execution_count": 62,
219
+ "id": "7fa2cb72",
220
+ "metadata": {},
221
+ "outputs": [
222
+ {
223
+ "name": "stdout",
224
+ "output_type": "stream",
225
+ "text": [
226
+ "The autoreload extension is already loaded. To reload it, use:\n",
227
+ " %reload_ext autoreload\n"
228
+ ]
229
+ }
230
+ ],
231
+ "source": [
232
+ "%load_ext autoreload\n",
233
+ "%autoreload 2"
234
+ ]
235
+ },
236
+ {
237
+ "cell_type": "code",
238
+ "execution_count": 49,
239
+ "id": "67c34870",
240
+ "metadata": {},
241
+ "outputs": [],
242
+ "source": [
243
+ "from DeepSets import DataPreprocessor\n",
244
+ "preprocessed_data, x = (real_data \n",
245
+ " .pipe(DataPreprocessor.removing_single_episode_patients)\n",
246
+ " .pipe(DataPreprocessor.calculate_population_Allele_frequencies, population='country')\n",
247
+ " .pipe(DataPreprocessor.calculate_MOIs)\n",
248
+ " .pipe(DataPreprocessor.paired_episode_assigner)\n",
249
+ " # .pipe(DataPreprocessor.build_deepsets_tensors, mode='prediction')\n",
250
+ "\n",
251
+ ")\n"
252
+ ]
253
+ },
254
+ {
255
+ "cell_type": "code",
256
+ "execution_count": 61,
257
+ "id": "ec103b00",
258
+ "metadata": {},
259
+ "outputs": [
260
+ {
261
+ "data": {
262
+ "text/plain": [
263
+ "Index(['pair_id', 'prob_C', 'prob_L', 'prob_I', 'predicted_class',\n",
264
+ " 'sample_id_paired', 'patient_id', 'true_episode'],\n",
265
+ " dtype='object')"
266
+ ]
267
+ },
268
+ "execution_count": 61,
269
+ "metadata": {},
270
+ "output_type": "execute_result"
271
+ }
272
+ ],
273
+ "source": [
274
+ "result['results_table'].columns"
275
+ ]
276
+ },
277
+ {
278
+ "cell_type": "code",
279
+ "execution_count": 63,
280
+ "id": "b25f2086",
281
+ "metadata": {},
282
+ "outputs": [
283
+ {
284
+ "name": "stdout",
285
+ "output_type": "stream",
286
+ "text": [
287
+ "143\n"
288
+ ]
289
+ },
290
+ {
291
+ "name": "stderr",
292
+ "output_type": "stream",
293
+ "text": [
294
+ "D:\\Educational\\PvRecNet\\backend\\PvDeepSetNet\\src\\DeepSets\\Model_result.py:36: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
295
+ " model.load_state_dict(torch.load(\"../src/DeepSets/pv_deepsets_weights20260402.pth\", map_location=device))\n"
296
+ ]
297
+ },
298
+ {
299
+ "data": {
300
+ "text/html": [
301
+ "<div>\n",
302
+ "<style scoped>\n",
303
+ " .dataframe tbody tr th:only-of-type {\n",
304
+ " vertical-align: middle;\n",
305
+ " }\n",
306
+ "\n",
307
+ " .dataframe tbody tr th {\n",
308
+ " vertical-align: top;\n",
309
+ " }\n",
310
+ "\n",
311
+ " .dataframe thead th {\n",
312
+ " text-align: right;\n",
313
+ " }\n",
314
+ "</style>\n",
315
+ "<table border=\"1\" class=\"dataframe\">\n",
316
+ " <thead>\n",
317
+ " <tr style=\"text-align: right;\">\n",
318
+ " <th></th>\n",
319
+ " <th>pair_id</th>\n",
320
+ " <th>patient_id</th>\n",
321
+ " <th>true_episode</th>\n",
322
+ " <th>prob_C</th>\n",
323
+ " <th>prob_L</th>\n",
324
+ " <th>prob_I</th>\n",
325
+ " <th>predicted_class</th>\n",
326
+ " </tr>\n",
327
+ " </thead>\n",
328
+ " <tbody>\n",
329
+ " <tr>\n",
330
+ " <th>0</th>\n",
331
+ " <td>AR-024_P1</td>\n",
332
+ " <td>AR-024</td>\n",
333
+ " <td>1.0</td>\n",
334
+ " <td>3.471411e-07</td>\n",
335
+ " <td>0.304471</td>\n",
336
+ " <td>0.695529</td>\n",
337
+ " <td>I</td>\n",
338
+ " </tr>\n",
339
+ " <tr>\n",
340
+ " <th>1</th>\n",
341
+ " <td>AR-024_P1</td>\n",
342
+ " <td>AR-024</td>\n",
343
+ " <td>2.0</td>\n",
344
+ " <td>3.471411e-07</td>\n",
345
+ " <td>0.304471</td>\n",
346
+ " <td>0.695529</td>\n",
347
+ " <td>I</td>\n",
348
+ " </tr>\n",
349
+ " <tr>\n",
350
+ " <th>2</th>\n",
351
+ " <td>AR-024_P10</td>\n",
352
+ " <td>AR-024</td>\n",
353
+ " <td>5.0</td>\n",
354
+ " <td>3.380386e-09</td>\n",
355
+ " <td>0.173901</td>\n",
356
+ " <td>0.826099</td>\n",
357
+ " <td>I</td>\n",
358
+ " </tr>\n",
359
+ " <tr>\n",
360
+ " <th>3</th>\n",
361
+ " <td>AR-024_P10</td>\n",
362
+ " <td>AR-024</td>\n",
363
+ " <td>4.0</td>\n",
364
+ " <td>3.380386e-09</td>\n",
365
+ " <td>0.173901</td>\n",
366
+ " <td>0.826099</td>\n",
367
+ " <td>I</td>\n",
368
+ " </tr>\n",
369
+ " <tr>\n",
370
+ " <th>4</th>\n",
371
+ " <td>AR-024_P2</td>\n",
372
+ " <td>AR-024</td>\n",
373
+ " <td>1.0</td>\n",
374
+ " <td>9.909202e-04</td>\n",
375
+ " <td>0.999007</td>\n",
376
+ " <td>0.000002</td>\n",
377
+ " <td>L</td>\n",
378
+ " </tr>\n",
379
+ " <tr>\n",
380
+ " <th>...</th>\n",
381
+ " <td>...</td>\n",
382
+ " <td>...</td>\n",
383
+ " <td>...</td>\n",
384
+ " <td>...</td>\n",
385
+ " <td>...</td>\n",
386
+ " <td>...</td>\n",
387
+ " <td>...</td>\n",
388
+ " </tr>\n",
389
+ " <tr>\n",
390
+ " <th>783</th>\n",
391
+ " <td>Sero-277_P1</td>\n",
392
+ " <td>Sero-277</td>\n",
393
+ " <td>1.0</td>\n",
394
+ " <td>1.636915e-06</td>\n",
395
+ " <td>0.992661</td>\n",
396
+ " <td>0.007338</td>\n",
397
+ " <td>L</td>\n",
398
+ " </tr>\n",
399
+ " <tr>\n",
400
+ " <th>784</th>\n",
401
+ " <td>Sero-281_P1</td>\n",
402
+ " <td>Sero-281</td>\n",
403
+ " <td>1.0</td>\n",
404
+ " <td>2.164313e-08</td>\n",
405
+ " <td>0.304484</td>\n",
406
+ " <td>0.695516</td>\n",
407
+ " <td>I</td>\n",
408
+ " </tr>\n",
409
+ " <tr>\n",
410
+ " <th>785</th>\n",
411
+ " <td>Sero-281_P1</td>\n",
412
+ " <td>Sero-281</td>\n",
413
+ " <td>2.0</td>\n",
414
+ " <td>2.164313e-08</td>\n",
415
+ " <td>0.304484</td>\n",
416
+ " <td>0.695516</td>\n",
417
+ " <td>I</td>\n",
418
+ " </tr>\n",
419
+ " <tr>\n",
420
+ " <th>786</th>\n",
421
+ " <td>Sero-288_P1</td>\n",
422
+ " <td>Sero-288</td>\n",
423
+ " <td>1.0</td>\n",
424
+ " <td>3.749500e-08</td>\n",
425
+ " <td>0.155791</td>\n",
426
+ " <td>0.844209</td>\n",
427
+ " <td>I</td>\n",
428
+ " </tr>\n",
429
+ " <tr>\n",
430
+ " <th>787</th>\n",
431
+ " <td>Sero-288_P1</td>\n",
432
+ " <td>Sero-288</td>\n",
433
+ " <td>2.0</td>\n",
434
+ " <td>3.749500e-08</td>\n",
435
+ " <td>0.155791</td>\n",
436
+ " <td>0.844209</td>\n",
437
+ " <td>I</td>\n",
438
+ " </tr>\n",
439
+ " </tbody>\n",
440
+ "</table>\n",
441
+ "<p>788 rows × 7 columns</p>\n",
442
+ "</div>"
443
+ ],
444
+ "text/plain": [
445
+ " pair_id patient_id true_episode prob_C prob_L prob_I \\\n",
446
+ "0 AR-024_P1 AR-024 1.0 3.471411e-07 0.304471 0.695529 \n",
447
+ "1 AR-024_P1 AR-024 2.0 3.471411e-07 0.304471 0.695529 \n",
448
+ "2 AR-024_P10 AR-024 5.0 3.380386e-09 0.173901 0.826099 \n",
449
+ "3 AR-024_P10 AR-024 4.0 3.380386e-09 0.173901 0.826099 \n",
450
+ "4 AR-024_P2 AR-024 1.0 9.909202e-04 0.999007 0.000002 \n",
451
+ ".. ... ... ... ... ... ... \n",
452
+ "783 Sero-277_P1 Sero-277 1.0 1.636915e-06 0.992661 0.007338 \n",
453
+ "784 Sero-281_P1 Sero-281 1.0 2.164313e-08 0.304484 0.695516 \n",
454
+ "785 Sero-281_P1 Sero-281 2.0 2.164313e-08 0.304484 0.695516 \n",
455
+ "786 Sero-288_P1 Sero-288 1.0 3.749500e-08 0.155791 0.844209 \n",
456
+ "787 Sero-288_P1 Sero-288 2.0 3.749500e-08 0.155791 0.844209 \n",
457
+ "\n",
458
+ " predicted_class \n",
459
+ "0 I \n",
460
+ "1 I \n",
461
+ "2 I \n",
462
+ "3 I \n",
463
+ "4 L \n",
464
+ ".. ... \n",
465
+ "783 L \n",
466
+ "784 I \n",
467
+ "785 I \n",
468
+ "786 I \n",
469
+ "787 I \n",
470
+ "\n",
471
+ "[788 rows x 7 columns]"
472
+ ]
473
+ },
474
+ "execution_count": 63,
475
+ "metadata": {},
476
+ "output_type": "execute_result"
477
+ }
478
+ ],
479
+ "source": [
480
+ "from DeepSets import run_prediction\n",
481
+ "\n",
482
+ "result = run_prediction(real_data, mode='prediction')\n",
483
+ "result['results_table']"
484
+ ]
485
+ },
486
+ {
487
+ "cell_type": "code",
488
+ "execution_count": 45,
489
+ "id": "64754c91",
490
+ "metadata": {},
491
+ "outputs": [
492
+ {
493
+ "data": {
494
+ "text/html": [
495
+ "<div>\n",
496
+ "<style scoped>\n",
497
+ " .dataframe tbody tr th:only-of-type {\n",
498
+ " vertical-align: middle;\n",
499
+ " }\n",
500
+ "\n",
501
+ " .dataframe tbody tr th {\n",
502
+ " vertical-align: top;\n",
503
+ " }\n",
504
+ "\n",
505
+ " .dataframe thead th {\n",
506
+ " text-align: right;\n",
507
+ " }\n",
508
+ "</style>\n",
509
+ "<table border=\"1\" class=\"dataframe\">\n",
510
+ " <thead>\n",
511
+ " <tr style=\"text-align: right;\">\n",
512
+ " <th></th>\n",
513
+ " <th>sample_id_paired</th>\n",
514
+ " <th>patient_id</th>\n",
515
+ " <th>true_episode</th>\n",
516
+ " </tr>\n",
517
+ " </thead>\n",
518
+ " <tbody>\n",
519
+ " <tr>\n",
520
+ " <th>0</th>\n",
521
+ " <td>AR-024_P1</td>\n",
522
+ " <td>AR-024</td>\n",
523
+ " <td>1.0</td>\n",
524
+ " </tr>\n",
525
+ " <tr>\n",
526
+ " <th>1</th>\n",
527
+ " <td>AR-024_P1</td>\n",
528
+ " <td>AR-024</td>\n",
529
+ " <td>2.0</td>\n",
530
+ " </tr>\n",
531
+ " <tr>\n",
532
+ " <th>2</th>\n",
533
+ " <td>AR-024_P2</td>\n",
534
+ " <td>AR-024</td>\n",
535
+ " <td>1.0</td>\n",
536
+ " </tr>\n",
537
+ " <tr>\n",
538
+ " <th>3</th>\n",
539
+ " <td>AR-024_P2</td>\n",
540
+ " <td>AR-024</td>\n",
541
+ " <td>3.0</td>\n",
542
+ " </tr>\n",
543
+ " <tr>\n",
544
+ " <th>4</th>\n",
545
+ " <td>AR-024_P3</td>\n",
546
+ " <td>AR-024</td>\n",
547
+ " <td>4.0</td>\n",
548
+ " </tr>\n",
549
+ " <tr>\n",
550
+ " <th>...</th>\n",
551
+ " <td>...</td>\n",
552
+ " <td>...</td>\n",
553
+ " <td>...</td>\n",
554
+ " </tr>\n",
555
+ " <tr>\n",
556
+ " <th>783</th>\n",
557
+ " <td>Sero-277_P1</td>\n",
558
+ " <td>Sero-277</td>\n",
559
+ " <td>1.0</td>\n",
560
+ " </tr>\n",
561
+ " <tr>\n",
562
+ " <th>784</th>\n",
563
+ " <td>Sero-281_P1</td>\n",
564
+ " <td>Sero-281</td>\n",
565
+ " <td>1.0</td>\n",
566
+ " </tr>\n",
567
+ " <tr>\n",
568
+ " <th>785</th>\n",
569
+ " <td>Sero-281_P1</td>\n",
570
+ " <td>Sero-281</td>\n",
571
+ " <td>2.0</td>\n",
572
+ " </tr>\n",
573
+ " <tr>\n",
574
+ " <th>786</th>\n",
575
+ " <td>Sero-288_P1</td>\n",
576
+ " <td>Sero-288</td>\n",
577
+ " <td>1.0</td>\n",
578
+ " </tr>\n",
579
+ " <tr>\n",
580
+ " <th>787</th>\n",
581
+ " <td>Sero-288_P1</td>\n",
582
+ " <td>Sero-288</td>\n",
583
+ " <td>2.0</td>\n",
584
+ " </tr>\n",
585
+ " </tbody>\n",
586
+ "</table>\n",
587
+ "<p>788 rows × 3 columns</p>\n",
588
+ "</div>"
589
+ ],
590
+ "text/plain": [
591
+ " sample_id_paired patient_id true_episode\n",
592
+ "0 AR-024_P1 AR-024 1.0\n",
593
+ "1 AR-024_P1 AR-024 2.0\n",
594
+ "2 AR-024_P2 AR-024 1.0\n",
595
+ "3 AR-024_P2 AR-024 3.0\n",
596
+ "4 AR-024_P3 AR-024 4.0\n",
597
+ ".. ... ... ...\n",
598
+ "783 Sero-277_P1 Sero-277 1.0\n",
599
+ "784 Sero-281_P1 Sero-281 1.0\n",
600
+ "785 Sero-281_P1 Sero-281 2.0\n",
601
+ "786 Sero-288_P1 Sero-288 1.0\n",
602
+ "787 Sero-288_P1 Sero-288 2.0\n",
603
+ "\n",
604
+ "[788 rows x 3 columns]"
605
+ ]
606
+ },
607
+ "execution_count": 45,
608
+ "metadata": {},
609
+ "output_type": "execute_result"
610
+ }
611
+ ],
612
+ "source": [
613
+ "preprocessed_data[['sample_id_paired', 'patient_id', 'true_episode']].drop_duplicates().reset_index(drop=True)"
614
+ ]
615
+ },
616
+ {
617
+ "cell_type": "code",
618
+ "execution_count": 15,
619
+ "id": "0e5d6196",
620
+ "metadata": {},
621
+ "outputs": [
622
+ {
623
+ "name": "stdout",
624
+ "output_type": "stream",
625
+ "text": [
626
+ "11\n",
627
+ "394\n",
628
+ "5\n"
629
+ ]
630
+ }
631
+ ],
632
+ "source": [
633
+ "from DeepSets import run_prediction\n",
634
+ "\n",
635
+ "result = run_prediction(real_data, mode='prediction')"
636
  ]
637
  },
638
  {
 
799
  },
800
  {
801
  "cell_type": "code",
802
+ "execution_count": 40,
803
  "id": "8f6bda0c",
804
  "metadata": {},
805
  "outputs": [
806
  {
807
  "data": {
808
  "text/plain": [
809
+ "[2,\n",
810
+ " 2,\n",
811
+ " 1,\n",
812
+ " 0,\n",
813
+ " 2,\n",
814
+ " 2,\n",
815
+ " 2,\n",
816
+ " 2,\n",
817
+ " 0,\n",
818
+ " 2,\n",
819
+ " 1,\n",
820
+ " 2,\n",
821
+ " 2,\n",
822
+ " 1,\n",
823
+ " 1,\n",
824
+ " 0,\n",
825
+ " 0,\n",
826
+ " 0,\n",
827
+ " 0,\n",
828
+ " 2,\n",
829
+ " 1,\n",
830
+ " 2,\n",
831
+ " 1,\n",
832
+ " 0,\n",
833
+ " 2,\n",
834
+ " 2,\n",
835
+ " 2,\n",
836
+ " 1,\n",
837
+ " 0,\n",
838
+ " 1,\n",
839
+ " 2,\n",
840
+ " 2,\n",
841
+ " 1,\n",
842
+ " 2,\n",
843
+ " 2,\n",
844
+ " 2,\n",
845
+ " 0,\n",
846
+ " 0,\n",
847
+ " 0,\n",
848
+ " 1,\n",
849
+ " 2,\n",
850
+ " 0,\n",
851
+ " 1,\n",
852
+ " 1,\n",
853
+ " 2,\n",
854
+ " 1,\n",
855
+ " 0,\n",
856
+ " 1,\n",
857
+ " 2,\n",
858
+ " 2,\n",
859
+ " 2,\n",
860
+ " 1,\n",
861
+ " 2,\n",
862
+ " 2,\n",
863
+ " 2,\n",
864
+ " 0,\n",
865
+ " 2,\n",
866
+ " 1,\n",
867
+ " 1,\n",
868
+ " 2,\n",
869
+ " 2,\n",
870
+ " 2,\n",
871
+ " 2,\n",
872
+ " 2,\n",
873
+ " 2,\n",
874
+ " 0,\n",
875
+ " 0,\n",
876
+ " 2,\n",
877
+ " 1,\n",
878
+ " 0,\n",
879
+ " 0,\n",
880
+ " 2,\n",
881
+ " 0,\n",
882
+ " 2,\n",
883
+ " 2,\n",
884
+ " 0,\n",
885
+ " 0,\n",
886
+ " 0,\n",
887
+ " 2,\n",
888
+ " 0,\n",
889
+ " 0,\n",
890
+ " 0,\n",
891
+ " 2,\n",
892
+ " 2,\n",
893
+ " 0,\n",
894
+ " 2,\n",
895
+ " 2,\n",
896
+ " 0,\n",
897
+ " 2,\n",
898
+ " 1,\n",
899
+ " 1,\n",
900
+ " 2,\n",
901
+ " 2,\n",
902
+ " 2,\n",
903
+ " 2,\n",
904
+ " 0,\n",
905
+ " 2,\n",
906
+ " 2,\n",
907
+ " 1,\n",
908
+ " 2,\n",
909
+ " 2,\n",
910
+ " 2,\n",
911
+ " 2,\n",
912
+ " 2,\n",
913
+ " 2,\n",
914
+ " 2,\n",
915
+ " 2,\n",
916
+ " 2,\n",
917
+ " 1,\n",
918
+ " 2,\n",
919
+ " 2,\n",
920
+ " 2,\n",
921
+ " 2,\n",
922
+ " 2,\n",
923
+ " 2,\n",
924
+ " 2,\n",
925
+ " 2,\n",
926
+ " 2,\n",
927
+ " 2,\n",
928
+ " 2,\n",
929
+ " 2,\n",
930
+ " 0,\n",
931
+ " 2,\n",
932
+ " 2,\n",
933
+ " 2,\n",
934
+ " 0,\n",
935
+ " 2,\n",
936
+ " 2,\n",
937
+ " 2,\n",
938
+ " 2,\n",
939
+ " 2,\n",
940
+ " 0,\n",
941
+ " 1,\n",
942
+ " 0,\n",
943
+ " 2,\n",
944
+ " 2,\n",
945
+ " 2,\n",
946
+ " 2,\n",
947
+ " 2,\n",
948
+ " 2,\n",
949
+ " 2,\n",
950
+ " 2,\n",
951
+ " 2,\n",
952
+ " 2,\n",
953
+ " 2,\n",
954
+ " 2,\n",
955
+ " 1,\n",
956
+ " 1,\n",
957
+ " 2,\n",
958
+ " 2,\n",
959
+ " 1,\n",
960
+ " 2,\n",
961
+ " 2,\n",
962
+ " 1,\n",
963
+ " 1,\n",
964
+ " 2,\n",
965
+ " 1,\n",
966
+ " 1,\n",
967
+ " 1,\n",
968
+ " 2,\n",
969
+ " 2,\n",
970
+ " 2,\n",
971
+ " 2,\n",
972
+ " 2,\n",
973
+ " 2,\n",
974
+ " 2,\n",
975
+ " 2,\n",
976
+ " 2,\n",
977
+ " 2,\n",
978
+ " 2,\n",
979
+ " 2,\n",
980
+ " 1,\n",
981
+ " 1,\n",
982
+ " 2,\n",
983
+ " 2,\n",
984
+ " 2,\n",
985
+ " 2,\n",
986
+ " 2,\n",
987
+ " 1,\n",
988
+ " 2,\n",
989
+ " 1,\n",
990
+ " 2,\n",
991
+ " 2,\n",
992
+ " 2,\n",
993
+ " 2,\n",
994
+ " 1,\n",
995
+ " 2,\n",
996
+ " 1,\n",
997
+ " 1,\n",
998
+ " 2,\n",
999
+ " 1,\n",
1000
+ " 1,\n",
1001
+ " 1,\n",
1002
+ " 1,\n",
1003
+ " 2,\n",
1004
+ " 2,\n",
1005
+ " 2,\n",
1006
+ " 1,\n",
1007
+ " 2,\n",
1008
+ " 2,\n",
1009
+ " 2,\n",
1010
+ " 1,\n",
1011
+ " 2,\n",
1012
+ " 1,\n",
1013
+ " 1,\n",
1014
+ " 2,\n",
1015
+ " 2,\n",
1016
+ " 1,\n",
1017
+ " 2,\n",
1018
+ " 1,\n",
1019
+ " 1,\n",
1020
+ " 2,\n",
1021
+ " 2,\n",
1022
+ " 2,\n",
1023
+ " 2,\n",
1024
+ " 1,\n",
1025
+ " 2,\n",
1026
+ " 2,\n",
1027
+ " 2,\n",
1028
+ " 1,\n",
1029
+ " 2,\n",
1030
+ " 2,\n",
1031
+ " 2,\n",
1032
+ " 2,\n",
1033
+ " 2,\n",
1034
+ " 1,\n",
1035
+ " 1,\n",
1036
+ " 2,\n",
1037
+ " 2,\n",
1038
+ " 1,\n",
1039
+ " 2,\n",
1040
+ " 2,\n",
1041
+ " 2,\n",
1042
+ " 2,\n",
1043
+ " 2,\n",
1044
+ " 2,\n",
1045
+ " 2,\n",
1046
+ " 2,\n",
1047
+ " 2,\n",
1048
+ " 2,\n",
1049
+ " 1,\n",
1050
+ " 1,\n",
1051
+ " 1,\n",
1052
+ " 1,\n",
1053
+ " 2,\n",
1054
+ " 2,\n",
1055
+ " 2,\n",
1056
+ " 2,\n",
1057
+ " 2,\n",
1058
+ " 2,\n",
1059
+ " 2,\n",
1060
+ " 2,\n",
1061
+ " 2,\n",
1062
+ " 1,\n",
1063
+ " 1,\n",
1064
+ " 1,\n",
1065
+ " 1,\n",
1066
+ " 2,\n",
1067
+ " 2,\n",
1068
+ " 1,\n",
1069
+ " 1,\n",
1070
+ " 1,\n",
1071
+ " 2,\n",
1072
+ " 2,\n",
1073
+ " 1,\n",
1074
+ " 2,\n",
1075
+ " 1,\n",
1076
+ " 2,\n",
1077
+ " 2,\n",
1078
+ " 2,\n",
1079
+ " 1,\n",
1080
+ " 1,\n",
1081
+ " 1,\n",
1082
+ " 1,\n",
1083
+ " 1,\n",
1084
+ " 0,\n",
1085
+ " 2,\n",
1086
+ " 2,\n",
1087
+ " 2,\n",
1088
+ " 2,\n",
1089
+ " 2,\n",
1090
+ " 2,\n",
1091
+ " 1,\n",
1092
+ " 1,\n",
1093
+ " 2,\n",
1094
+ " 2,\n",
1095
+ " 1,\n",
1096
+ " 2,\n",
1097
+ " 2,\n",
1098
+ " 1,\n",
1099
+ " 2,\n",
1100
+ " 1,\n",
1101
+ " 1,\n",
1102
+ " 1,\n",
1103
+ " 2,\n",
1104
+ " 2,\n",
1105
+ " 2,\n",
1106
+ " 2,\n",
1107
+ " 1,\n",
1108
+ " 1,\n",
1109
+ " 1,\n",
1110
+ " 1,\n",
1111
+ " 1,\n",
1112
+ " 2,\n",
1113
+ " 1,\n",
1114
+ " 2,\n",
1115
+ " 2,\n",
1116
+ " 2,\n",
1117
+ " 1,\n",
1118
+ " 2,\n",
1119
+ " 2,\n",
1120
+ " 2,\n",
1121
+ " 1,\n",
1122
+ " 2,\n",
1123
+ " 2,\n",
1124
+ " 2,\n",
1125
+ " 2,\n",
1126
+ " 1,\n",
1127
+ " 2,\n",
1128
+ " 2,\n",
1129
+ " 2,\n",
1130
+ " 2,\n",
1131
+ " 1,\n",
1132
+ " 2,\n",
1133
+ " 2,\n",
1134
+ " 2,\n",
1135
+ " 2,\n",
1136
+ " 2,\n",
1137
+ " 1,\n",
1138
+ " 2,\n",
1139
+ " 1,\n",
1140
+ " 2,\n",
1141
+ " 1,\n",
1142
+ " 2,\n",
1143
+ " 1,\n",
1144
+ " 2,\n",
1145
+ " 2,\n",
1146
+ " 1,\n",
1147
+ " 2,\n",
1148
+ " 2,\n",
1149
+ " 1,\n",
1150
+ " 1,\n",
1151
+ " 2,\n",
1152
+ " 2,\n",
1153
+ " 2,\n",
1154
+ " 2,\n",
1155
+ " 1,\n",
1156
+ " 1,\n",
1157
+ " 1,\n",
1158
+ " 1,\n",
1159
+ " 0,\n",
1160
+ " 2,\n",
1161
+ " 2,\n",
1162
+ " 1,\n",
1163
+ " 2,\n",
1164
+ " 2,\n",
1165
+ " 1,\n",
1166
+ " 1,\n",
1167
+ " 1,\n",
1168
+ " 1,\n",
1169
+ " 0,\n",
1170
+ " 0,\n",
1171
+ " 0,\n",
1172
+ " 1,\n",
1173
+ " 1,\n",
1174
+ " 1,\n",
1175
+ " 1,\n",
1176
+ " 1,\n",
1177
+ " 1,\n",
1178
+ " 1,\n",
1179
+ " 1,\n",
1180
+ " 1,\n",
1181
+ " 1,\n",
1182
+ " 2,\n",
1183
+ " 1,\n",
1184
+ " 1,\n",
1185
+ " 2,\n",
1186
+ " 1,\n",
1187
+ " 1,\n",
1188
+ " 1,\n",
1189
+ " 1,\n",
1190
+ " 1,\n",
1191
+ " 1,\n",
1192
+ " 1,\n",
1193
+ " 1,\n",
1194
+ " 1,\n",
1195
+ " 1,\n",
1196
+ " 1,\n",
1197
+ " 1,\n",
1198
+ " 0,\n",
1199
+ " 1,\n",
1200
+ " 2,\n",
1201
+ " 2]"
1202
  ]
1203
  },
1204
+ "execution_count": 40,
1205
  "metadata": {},
1206
  "output_type": "execute_result"
1207
  }
1208
  ],
1209
  "source": [
1210
+ "y_pred_hard"
1211
  ]
1212
  },
1213
  {