-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheaml_run.py
More file actions
executable file
·145 lines (96 loc) · 4.54 KB
/
Copy patheaml_run.py
File metadata and controls
executable file
·145 lines (96 loc) · 4.54 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
140
141
142
143
144
145
import tqdm
import time
import psutil
import pandas as pd
import random
from river import metrics,stream
from EvOAutoML import classification
from collecter import WindowClassificationPerformanceEvaluator
from codecarbon import OfflineEmissionsTracker
import json
import argparse
import warnings
import os
import sys
warnings.filterwarnings("ignore")
def main(dataset_name, population_size, sampling_size, sampling_rate, seed):
file_name = f"EvoAutoML_{dataset_name}_seed_{seed}_population_size_{population_size}_sampling_size_{sampling_size}_sampling_rate_{sampling_rate}.json"
# if os.path.exists(f"experiment-results/EvoAutoML/{file_name}"):
# print(f"File already exists. Skipping execution.")
# sys.exit(0)
print(f"Loading dataset: {dataset_name}, Random Seed:{seed}")
df = pd.read_csv(f"stream_datasets/{dataset_name}.csv")
x = df.drop('class', axis=1)
y = df['class']
dataset = stream.iter_pandas(x, y)
model = classification.EvolutionaryBaggingClassifier(
population_size=population_size,
sampling_size=sampling_size,
sampling_rate=sampling_rate,
metric=metrics.Accuracy,
seed=seed
)
metric = metrics.Accuracy()
wcpe = WindowClassificationPerformanceEvaluator(metric=metrics.Accuracy(),
window_width=1000,
print_every=1000)
scores_evo = []
times_evo = []
memories_evo = []
emissions = []
energy = []
tracker=OfflineEmissionsTracker(country_iso_code="EST",log_level="critical",allow_multiple_runs=True)#,experiment_id=run_id,save_to_file=True,output_file='emissions.csv')
tracker.start()
for x, y in tqdm.tqdm(dataset, leave=True):
tracker.start_task()
mem_before = psutil.Process(os.getpid()).memory_info().rss
start = time.time()
y_pred = model.predict_one(x) # make a prediction
metric.update(y, y_pred) # update the metric
wcpe.update(y, y_pred) #windows Update
model = model.learn_one(x,y) # make the model learn
end = time.time()
mem_after = psutil.Process(os.getpid()).memory_info().rss
iteration_mem = mem_after - mem_before
memories_evo.append(iteration_mem)
iteration_time = end - start
emission_record=tracker.stop_task()
scores_evo.append(metric.get())
times_evo.append(abs(iteration_time))
emissions.append(emission_record.emissions)
energy.append(emission_record.energy_consumed)
save_record = {
"model": "EvoAutoML",
"dataset": dataset_name,
"prequential_scores": scores_evo,
"windows_scores": wcpe.get(),
"time": times_evo,
"memory": memories_evo,
"emission": emissions,
"energy_consumed": energy #kwh
}
#file_name = f"{save_record['model']}_{save_record['dataset']}.json"
# file_name = f"{save_record['model']}_{save_record['dataset']}_population_size_{population_size}_sampling_size_{sampling_size}_sampling_rate_{sampling_rate}.json"
#print("Result Saved path:",file_name)
# To store the dictionary in a JSON file
dir_path = "experiment-results/EvoAutoML"
os.makedirs(dir_path, exist_ok=True) # Make sure the directory exists
file_path = os.path.join(dir_path, file_name)
# To store the dictionary in a JSON file
with open(file_path, 'w') as json_file:
json.dump(save_record, json_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="EvoAutoMl Script")
parser.add_argument("dataset_name", type=str, help="Name of the dataset file (without extension)")
parser.add_argument("--population_size", type=int, default=10, help="Population size for the model (default: 10)")
parser.add_argument("--sampling_size", type=int, default=1, help="Sampling size for the model (default: 1)")
parser.add_argument("--sampling_rate", type=int, default=1000, help="Sampling rate for the model (default: 1000)")
parser.add_argument("--seed", type=int, default=42, help="Random seed for reproducibility (default: 42)")
args = parser.parse_args()
main(
dataset_name=args.dataset_name,
population_size=args.population_size,
sampling_size=args.sampling_size,
sampling_rate=args.sampling_rate,
seed=args.seed
)