File size: 3,024 Bytes
29d330e | 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 | import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import os
from itertools import chain, combinations
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
# base_path = "./workspace/Jammer_detection/Src/federated_darcy/results/cusuton_methods/"
base_path = "./workspace/Jammer_detection/Src/federated_synthetic_data/federated_scr/FedAvgM_53mmd/model/"
output_folder = os.path.join(base_path, "Test_round_10") ##added
summary_path = base_path + "summary_all_round.txt" # summary.txt" "summary_all_round.txt"
if not os.path.exists(output_folder):
os.makedirs(output_folder)
with open(summary_path, "r") as f:
data_raw = f.readlines()
results = []
methods = []
swap = 0
cur_results = []
cur_results_comb = []
for di, dr in enumerate(data_raw):
dr_split = dr.split("\n")[0].split("\t")
if len(dr_split) == 1:
methods.append(dr_split[0])
swap = -1
if di != 0:
results.append(cur_results_comb)
cur_results_comb = []
else:
cur_results.append([float(x) for x in dr_split[1:-1] if len(x) != 0])
if swap == 1:
cur_results_comb.append(cur_results)
cur_results = []
swap = (swap + 1) % 2
results.append(cur_results_comb)
results = np.array(results)
print(f"shape od result is ----------{results.shape}")
additional_classes = [3 + np.array(list(x)).astype(int) for x in
list(powerset("".join((np.array([3, 4, 5]) - 3).astype("str"))))]
print(additional_classes)
font = {'family': 'serif',
'weight': 'bold',
'size': 10}
for i, res in enumerate(results):
trained_on = np.array([0, 1, 2])
trained_on.sort()
img = np.around(res[:, 0, 2].reshape((4, 2)) * 100, 1)
img_arg = np.around(res[:, 1, 2].reshape((4, 2)) * 100, 1)
prec_rec = np.around(res[:, 0, 3:].reshape((4, 2, 2)) * 100, 1).astype("str")
plt.figure(figsize=(10, 5))
plt.title("Trained on: " + methods[i].split("_")[0])
plt.imshow(img, cmap="rainbow", vmin=0, vmax=100)
for ci in range(8):
cii = ci // 2
cij = ci % 2
# ac = " ".join(additional_classes[ci].astype("str")) + "\n" + str(img[cii, cij]) + " " + str(img_arg[cii, cij]) + "\n" + " ".join(prec_rec[cii, cij])
ac = " ".join(additional_classes[ci].astype("str")) + "\n" + str(img[cii, cij]) + "\n" + " ".join(
prec_rec[cii, cij])
plt.text(cij, cii, ac, ha='center', va='center', **font)
print(np.arange(-0.5, 4.0, 1.0))
print(np.arange(5))
plt.xticks(np.arange(-0.5, 4.0, 1.0)[:2], "")
plt.yticks(np.arange(-0.5, 4.0, 1.0), "")
plt.colorbar()
plt.grid(color='k', linestyle='-', linewidth=2)
plt.tight_layout()
plt.savefig(os.path.join(output_folder, "Res%d.png" % i))
# plt.show()
plt.close()
|