-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_load_analysis.py
More file actions
107 lines (93 loc) · 4.25 KB
/
push_load_analysis.py
File metadata and controls
107 lines (93 loc) · 4.25 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
#
# This file is part of the Push-Pull Medium Access repository:
# https://github.com/signetlabdei/push-pull-anomaly-tracking
# Copyright (c) 2025:
# Fabio Saggese (fabio.saggese@ing.unipi.it)
# Federico Chiariotti (federico.chiariotti@unipd.it)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import os
import time
from concurrent.futures import ProcessPoolExecutor
import numpy as np
import pandas as pd
from push_frame_analysis import run_episode
import common as cmn
if __name__ == "__main__":
# Parse arguments, if any
parallel, savedir, debug, overwrite = cmn.common_parser()
if savedir is not None:
push_folder = savedir
else:
push_folder = cmn.push_folder
# Simulation variables
dec = 6
schedulers = cmn.push_scheduler_names
pps_scheduler_mode = 1
P = 10
# Anomaly and algorithm parameters
rates = np.arange(0.01, 0.051, 0.001)
# Check if files exist and load it if there
prefix = 'push_load'
filename_avg = os.path.join(push_folder, prefix + '_avg.csv')
filename_99 = os.path.join(push_folder, prefix + '_99.csv')
filename_999 = os.path.join(push_folder, prefix + '_999.csv')
if os.path.exists(filename_avg) and not overwrite:
prob_avg = pd.read_csv(filename_avg).iloc[:, 1:].to_numpy().T
else:
prob_avg = np.full((len(schedulers), len(rates)), np.nan)
if os.path.exists(filename_99) and not overwrite:
prob_99 = pd.read_csv(filename_99).iloc[:, 1:].to_numpy().T
else:
prob_99 = np.full((len(schedulers), len(rates)), np.nan)
if os.path.exists(filename_999) and not overwrite:
prob_999 = pd.read_csv(filename_999).iloc[:, 1:].to_numpy().T
else:
prob_999 = np.full((len(schedulers), len(rates)), np.nan)
for s, scheduler in enumerate(schedulers):
for r, rate in enumerate(rates):
# Logging #
print(f"Scheduler: {scheduler}; rate={rate*100:.1f}. Status:")
# Check if data is there
if overwrite or np.isnan(prob_avg[s, r]):
args = (s, cmn.aoii_hbins, P, cmn.T, cmn.N, cmn.max_age,
rate, cmn.SIGMA, pps_scheduler_mode, debug)
start_time = time.time()
if parallel:
with ProcessPoolExecutor() as executor:
futures = [executor.submit(run_episode, ep, *args) for ep in range(cmn.E)]
results = [f.result() for f in futures]
else:
results = []
for ep in range(cmn.E):
print(f'\tEpisode: {ep:02d}/{cmn.E - 1:02d}')
results.append(run_episode(ep, *args))
# Average the results
anomaly_aoii_hist = np.mean(np.array([res[0] for res in results]), axis=0)
# Divide data
anomaly_aoii_cdf = np.cumsum(anomaly_aoii_hist)
prob_99[s, r] = np.where(anomaly_aoii_cdf > 0.99)[0][0]
prob_999[s, r] = np.where(anomaly_aoii_cdf > 0.999)[0][0]
prob_avg[s, r] = np.dot(anomaly_aoii_hist, np.arange(0, cmn.aoii_hbins + 1, 1))
# Generate data frame and save it (redundant but to avoid to lose data for any reason)
for res, file in [(prob_avg, filename_avg), (prob_99, filename_99), (prob_999, filename_999)]:
df = pd.DataFrame(res.T.round(dec), columns=schedulers)
df.insert(0, 'rate', rates * 100)
df.to_csv(file, index=False)
# Print time
elapsed = time.time() - start_time
print(f"\t...done in {elapsed:.3f} seconds")
else:
print("\t...already done!")
continue