FelixOtt's picture
Upload folder using huggingface_hub
29d330e verified
Raw History Blame Contribute Delete
3.02 kB
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()