-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml_prediction.py
More file actions
68 lines (55 loc) · 2.9 KB
/
ml_prediction.py
File metadata and controls
68 lines (55 loc) · 2.9 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
import os
import pandas as pd
import numpy as np
from pathlib import Path
import joblib
def ml_prediction(output_dir, feature_file):
models = ["mlp_200_nokey", "mlp_400_nokey"]
for model in models:
feature_df = pd.read_csv(feature_file)
filename_col = feature_df['Filename'].copy()
software_col = feature_df['software'].copy()
activation_method = feature_df['activation_method'].copy()
feature_pattern = pd.read_csv(f"models/{model}_train_features.csv")
# Load model, preprocessor and label-encoders
clf = joblib.load(f"models/{model}_model.pkl")
preprocessor = joblib.load(f"models/{model}_preprocessor.pkl")
label_encoders = joblib.load(f"models/{model}_label_encoders.pkl")
ml_metrics = pd.read_csv(f"models/{model}_metrics.csv")
# preprocess feature df
new_fea_cols = feature_df.columns
for col in feature_pattern.columns:
if col not in new_fea_cols:
if "avgevalhits" in col:
feature_df[col] = np.mean(feature_pattern[col])
elif "counthits" in col:
feature_df[col] = np.mean(feature_pattern[col])
elif "precursor" in col:
feature_df[col] = np.mean(feature_pattern[col])
if col in new_fea_cols:
if "avgevalhits" in col:
feature_df[col].fillna(np.mean(feature_pattern[col]))
elif "counthits" in col:
feature_df[col].fillna(np.mean(feature_pattern[col]))
elif "precursor" in col:
feature_df[col].fillna(np.mean(feature_pattern[col]))
for fea_df_col in feature_df.columns:
if fea_df_col not in feature_pattern.columns:
feature_df = feature_df.drop(columns = [fea_df_col]).copy()
# Sort feature_df as feature_pattern from training process
feature_df = feature_df[feature_pattern.columns]
X = feature_df.copy()
label_columns = ["Domain","Organism", "Organism part", "Diseases", "Modification",
"Experiment Type", "Instrument", "Quantification", "Software"]
# Apply preprocessing to X
X_preprocessed = preprocessor.transform(X)
y_pred = clf.predict(X_preprocessed)
# Decode predictions
y_pred_decoded = pd.DataFrame(y_pred, columns=label_columns)
for i, col in enumerate(label_columns):
y_pred_decoded[col] = label_encoders[col].inverse_transform(y_pred[:, i])
### Todo: only story one metadata.csv (w model for best accuracy per column)
y_pred_decoded.insert(0, 'Filename', filename_col)
y_pred_decoded["Parsed Software"] = software_col
y_pred_decoded["Activation Method"] = activation_method
y_pred_decoded.to_csv(os.path.join(output_dir, f"{model}_predicted_metadata.csv"), index = False)