-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_experiments.py
More file actions
139 lines (105 loc) · 5.15 KB
/
run_experiments.py
File metadata and controls
139 lines (105 loc) · 5.15 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import subprocess
import polars as pl
import pandas as pd
import os
import time
import joblib
from App.utils.stats import summary_stats
def main():
start_all = time.time() # Start measuring total time of main()
full_datasets_path = "App/datasets"
num_runs = 5 # Number of times to run each dataset
datasets_list = [item for item in os.listdir(full_datasets_path)
if os.path.isdir(os.path.join(full_datasets_path, item))]
for dataset in datasets_list:
dataset_path = os.path.join(full_datasets_path, dataset)
# Skip this dataset if it already has a "runs" folder
experiments_folder = os.path.join(dataset_path, "runs")
# if os.path.exists(experiments_folder):
# continue
dtype_str, task = dataset.split('_')[-2:]
if dtype_str == "protein":
data_type = "Protein"
if dtype_str == "dnarna":
data_type = "DNA/RNA"
train_path = os.path.join(dataset_path, "train")
train_files = [os.path.join(train_path, file) for file in os.listdir(train_path)]
train_labels = [os.path.splitext(os.path.basename(file))[0] for file in train_files]
test_path = os.path.join(dataset_path, "test")
if os.path.exists(test_path):
test_files = [os.path.join(test_path, file) for file in os.listdir(test_path)]
test_labels = [os.path.splitext(os.path.basename(file))[0] for file in test_files]
# Create a runs folder for this dataset
os.makedirs(experiments_folder, exist_ok=True)
for run_num in range(1, num_runs + 1):
# Create a folder for this run inside the runs folder
run_folder = os.path.join(experiments_folder, f"run_{run_num}")
if not os.path.exists(run_folder):
os.makedirs(run_folder, exist_ok=True)
classifier = False
command = [
"python",
"engineering.py",
"--dtype",
dtype_str,
"--estimations",
"200", # 200
"--patience",
"80",
"--tuning",
"150", # 150
"--difference",
"0.001",
"--task",
task,
"--fasta_train",
]
command.extend(train_files)
command.append("--fasta_label_train")
command.extend(train_labels)
testing_set = True if os.path.exists(test_path) else False
if testing_set:
command.append("--fasta_test")
command.extend(test_files)
command.append("--fasta_label_test")
command.extend(test_labels)
command.extend(["--n_cpu", "8"])
command.extend(["--output", run_folder]) # Output to the run-specific folder
print(f"Running dataset {dataset}, iteration {run_num}")
# === START TIMING THIS RUN ===
start_run = time.time()
subprocess.run(command)
# === END TIMING THIS RUN ===
run_time_seconds = round(time.time() - start_run, 2)
# Save time spent for this run
df_time = pl.DataFrame({
"dataset": [dataset],
"run": [run_num],
"time_seconds": [run_time_seconds],
})
time_csv_path = os.path.join(run_folder, "time_spent.csv")
df_time.write_csv(time_csv_path)
job_data = {
"data_type": [data_type],
"task": ["Classification" if task == "0" else "Regression"],
"training_set": ["Training set"],
"testing_set": ["Test set" if testing_set else "No test set"],
"classifier_selected": [classifier]
}
df_job_data = pl.DataFrame(job_data)
tsv_path = os.path.join(run_folder, "job_info.tsv")
df_job_data.write_csv(tsv_path, separator='\t')
# Update paths for summary stats to use the run folder
summary_stats(os.path.join(run_folder if run_num == 1 else os.path.join(experiments_folder, "run_1"), "feat_extraction", "train"), data_type, run_folder, False)
if testing_set:
summary_stats(os.path.join(run_folder if run_num == 1 else os.path.join(experiments_folder, "run_1"), "feat_extraction", "test"), data_type, run_folder, False)
model_path = os.path.join(run_folder, "trained_model.sav")
if os.path.exists(model_path):
model = joblib.load(model_path)
model["train_stats"] = pd.read_csv(os.path.join(run_folder, "train_stats.csv"))
joblib.dump(model, model_path)
# End total time
total_time = round(time.time() - start_all, 2)
print(f"\n⏱ Total execution time for all datasets: {total_time} seconds")
if __name__ == "__main__":
main()