-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
393 lines (308 loc) · 14.4 KB
/
visualization.py
File metadata and controls
393 lines (308 loc) · 14.4 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import pandas as pd
import json
import matplotlib.pyplot as plt
import numpy as np
import re
import random
def plot_box(datas, names, title, save_path, showfliers=True, labels=['Shots', 'AUC']):
fig, ax = plt.subplots()
ax.boxplot(datas, notch=False, vert=True, patch_artist=True, showfliers=showfliers)
# Draw grid lines
ax.yaxis.grid(True) # vertical lines
plt.title(title)
plt.xlabel(labels[0])
plt.ylabel(labels[1])
# Set the x-axis tick labels to be the provided names
plt.xticks(range(1, len(names) + 1), names)
# Save the plot as a PNG file
plt.savefig(save_path, dpi=300)
def plot_box_block(datas, names, conds, title, save_path, showfliers=True, labels=['Shots', 'AUC']):
"""
Plot a boxplot with blocks for each condition within shots.
:param datas: A 3D list where each element is a list of conditions for a shot.
e.g., [[[cond1_values], [cond2_values], ...], [[cond1_values], [cond2_values], ...], ...]
:param names: Names for each shot.
:param conds: Names for each condition.
:param title: Title of the plot.
:param save_path: Path to save the plot.
:param showfliers: Whether to show fliers in the boxplot.
:param labels: Labels for the x and y axes.
"""
fig, ax = plt.subplots(figsize=(12, 8))
# Flatten the data for plotting
flat_data = [cond_data for shot_data in datas for cond_data in shot_data]
# Calculate positions for each boxplot
num_shots = len(datas)
num_conds = len(conds)
positions = []
spacing_between_shots = 0.4 # 调整这个值来控制不同shot之间的间距
spacing_within_shot = 0.7 # 调整这个值来控制同一shot内不同cond之间的间距
for i in range(num_shots):
for j in range(num_conds):
positions.append(i * (num_conds + spacing_between_shots) + j * spacing_within_shot + 1)
# Define colors for each condition using a blue colormap
cmap = plt.cm.Blues
colors = [cmap(i / (num_conds - 1)) for i in range(num_conds)] # 从浅到深的蓝色
# Create a boxplot
bp = ax.boxplot(flat_data, positions=positions, notch=False, vert=True, patch_artist=True, showfliers=showfliers)
# Set the color for each box
for i, box in enumerate(bp['boxes']):
box.set(facecolor=colors[i % num_conds])
# Draw grid lines
ax.yaxis.grid(True) # vertical lines
# Set the title and labels
plt.title(title)
plt.xlabel(labels[0])
plt.ylabel(labels[1])
# Set the x-axis tick labels
tick_positions = [i * (num_conds + spacing_between_shots) + 0.5 for i in range(num_shots)] # 每个shot的第一个box位置
plt.xticks(tick_positions, names, rotation=45, ha='right')
# Add a legend and place it in the upper right corner
for i, cond in enumerate(conds):
ax.plot([], c=colors[i], label=cond)
ax.legend(title='Conditions', loc='upper right')
# Save the plot as a PNG file
plt.tight_layout()
plt.savefig(save_path, dpi=300)
plt.show()
def calculate_stats(arr):
"""
计算每行数据的均值、中位数、第一四分位数(Q1)和第三四分位数(Q3)
:param arr: 输入数组,形状为(n, k),其中n是行数(组数),k是每组中的数据点个数
:return: 返回一个字典,包含每一行的均值、中位数、Q1和Q3
字典的形式如下:{'mean': 均值数组, 'median': 中位数数组, 'q1': Q1数组, 'q3': Q3数组}
"""
# 计算每行的统计量
means = np.mean(arr, axis=1)
medians = np.median(arr, axis=1)
q1s = np.percentile(arr, 25, axis=1) # 第一四分位数
q3s = np.percentile(arr, 75, axis=1) # 第三四分位数
return {'mean': means, 'median': medians, 'q1': q1s, 'q3': q3s}
def simple_extract_result(str, type='result'):
# 给定的字符串
input_string = str
# 正则表达式匹配所有小括号内的数字(包括小数点)
pattern = r"tensor\(([\d\.]+),\s*device='[^']+'\)"
# 查找所有匹配项并转换为float类型
matches = [float(value) for value in re.findall(pattern, input_string)]
# 将一维列表转换为二维列表,每一行4个元素
output_list = [matches[i:i+4] for i in range(0, len(matches), 4)]
#print(output_list)
return output_list
def extract_result(file_path, type='dist'):
# 定义正则表达式模式来匹配所需的字符串格式
if type == 'dist':
pattern = r'dist:\s*\[(.*?)\]'
elif type == 'result':
pattern = r'result(\d+\.\d+)'
elif type == 'results':
pattern = r'result\[(.*?)\]'
elif type == 'loss':
pattern = r'loss:\s*(\d+\.\d+)'
elif type == 'val':
pattern = r'val:\s*(\d+(\.\d+)?)'
elif type == 'cancer_ratio':
pattern = r'cancer_ratio:\s*\[(.*?)\]'
elif type == 'gt_cancer_ratio':
#pattern = r'cancer ratio:\s*\[(.*?)\]' #For condX
pattern = r'gt cancer ratio:\s*([\d.eE+-]+)' #For newloss2
# 初始化结果列表
result_list = []
# 读取文件内容
with open(file_path, 'r') as file:
content = file.read()
# 查找所有匹配的字符串
matches = re.findall(pattern, content)
# 将每个匹配项转换为numpy数组并添加到结果列表中
for match in matches:
print(match)
try:
# 替换 np.float64(xxx) 为 xxx
match = re.sub(r'np\.float64\(([\d.eE+-]+)\)', r'\1', match)
# 去掉多余的空格并将字符串分割成浮点数列表
numbers = list(map(float, match.split(',')))
array = np.array(numbers)
result_list.append(array)
except ValueError as e:
print(f"Error converting to float: {match}")
print(e)
return result_list
def plot_pie(data, title='Loss distribution', filename='pie_chart.png', cat=["Patch Loss", "Contrast Loss", "Normal Loss"]):
"""
绘制并保存已归一化的一维numpy数组的饼图。
参数:
data (np.array): 归一化的一维numpy数组,每个元素表示一个类别的占比。
filename (str): 保存的图片文件名,默认为'pie_chart.png'。
"""
# 检查数据是否归一化
if not np.isclose(np.sum(data), 1):
raise ValueError("The array must be normalized so that the sum of its elements equals 1.")
# 创建饼图
plt.figure(figsize=(8, 8))
plt.pie(data, labels=cat, autopct='%1.1f%%', startangle=90, wedgeprops=dict(width=0.3))
plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.title(title)
# 保存图片
plt.savefig(filename)
print(f"Pie chart saved as {filename}")
# 显示图形
plt.show()
def plot_grouped_bar(data, title, c_labels=["Patch Loss", "Contrast Loss", "Normal Loss"], g_labels=None, filename='grouped_bar_chart.png'):
"""
绘制并保存三组数据的分组条形图。
参数:
data (np.array): 三维numpy数组,形状为(3, n),表示三组数据中n个类别的占比。
labels (list): 类别的标签列表,默认为None,将自动生成默认标签。
filename (str): 保存的图片文件名,默认为'grouped_bar_chart.png'。
"""
# 检查数据维度
if data.shape[0] != 3:
raise ValueError("The first dimension of 'data' must be 3.")
# 如果没有提供标签,则生成默认标签
if c_labels is None:
c_labels = [f'Category {i+1}' for i in range(data.shape[1])]
elif len(c_labels) != data.shape[1]:
raise ValueError("The length of 'labels' must match the number of categories in 'data'.")
# 设置数据
categories = g_labels
group_labels = c_labels
bar_width = 0.25
index = np.arange(len(categories))
# 创建分组条形图
fig, ax = plt.subplots(figsize=(10, 6))
for i in range(len(data)):
ax.bar(index + i * bar_width, data[:, i], bar_width, label=group_labels[i])
# 添加标签和标题
ax.set_title(title)
ax.set_xticks(index + bar_width)
ax.set_xticklabels(categories)
ax.legend(title='Groups')
# 保存图片
plt.savefig(filename)
print(f"Grouped bar chart saved as {filename}")
# 显示图形
plt.show()
if __name__ == "__main__":
data = np.load('log\cm\CM_det_auc_train-test-.npy')
data = data[[0, 3, 2, 1], :]
print(calculate_stats(data))
plot_box(datas=data.tolist(),
names=["0-shot", "1-shot", "5-shot", "10-shot"],
title="Detection AUC",
save_path='CM_det_auc_train-test-',
labels=['Shots', 'AUC'])
# data_old = np.load('log/cam_seg_dice.npy')
# data_new = np.load('cam_seg_dice_newloss.npy')
# data_new[0] = data_old[0]
# data = data_new[[0, 3, 2, 1], :]
# np.save('log/cam_seg_dice_newloss.npy', data)
# # plot_box(datas=data.tolist(), names=["zero-shot", "one-shot", "two-shot", "five-shot"], title="", save_path='test')
# data_lst = [np.load(f'CM_det_auc_n_ctx_{2**(i+2)}.npy')[[3,2,1],:] for i in range(5)]
# oneshot_data = [data[0] for data in data_lst]
# fiveshot_data = [data[1] for data in data_lst]
# tenshot_data = [data[2] for data in data_lst]
# plot_box(datas=oneshot_data, names=["4", "8", "16", "32", "64"], title="CM detection auc", labels=['n_ctx', 'AUC'], save_path='cm_det_auc_ctx_1shot')
# plot_box(datas=fiveshot_data, names=["4", "8", "16", "32", "64"], title="CM detection auc", labels=['n_ctx', 'AUC'], save_path='cm_det_auc_ctx_5shot')
# plot_box(datas=tenshot_data, names=["4", "8", "16", "32", "64"], title="CM detection auc", labels=['n_ctx', 'AUC'], save_path='cm_det_auc_ctx_10shot')
# data = extract_result('log/cam/cam_seg_cond1.txt', type='results')
# data = np.array(data).squeeze()
# print(data.shape)
# print(data)
# new_data= [[],[],[],[]]
# for i, d in enumerate(data):
# idx = i%4
# if idx==0:
# new_data[idx].append(d)
# else:
# new_data[4-idx].append(d)
# plot_box(datas=new_data, names=["zero-shot", "one-shot", "two-shot", "five-shot"], title="", save_path='test')
# #去除nan
# mask = np.isnan(data).any(axis=1)
# data[mask] = [0,0,0,0]
# print(data.shape)
# # group_data = np.zeros((4,4))
# # for i, d in enumerate(data):
# # shot = i % 30 // 10
# # group_data[shot] += d
# # period = i % 10
# # if period < 3:
# # group_data[0] += d
# # elif period > 6:
# # group_data[2] += d
# # else:
# # group_data[1] += d
# # group_data /= group_data.sum(axis=1)[:, np.newaxis]
# # print(group_data)
# #plot_grouped_bar(group_data, title='Different period', c_labels=["Patch Loss", "Contrast Loss", "Normal Loss"], g_labels=["epoch 0-2", "epoch 3-6", "epoch 7-9"],filename='gbmlgg_sub_loss_bar_period.png')
# # group_data = group_data[[2,1,0], :]
# # plot_grouped_bar(group_data, title='Different shots', c_labels=["Patch Loss", "Contrast Loss", "Normal Loss"], g_labels=["1-shot", "5-shot", "10-shot"],filename='gbmlgg_sub_loss_bar_shot.png')
# s = np.sum(data, axis=0)
# s /= s.sum()
# print(s)
# plot_pie(s, title='Loss Distribution', filename='cm_det_loss_pie.png', cat=["Patch Loss normal", "Patch loss cancer", "Contrast Loss", "Normal Loss"])
# bacc = np.array(bacc)[[0, 3, 2, 1]]
# wsf1 = np.array(wsf1)[[0, 3, 2, 1]]
# print(calculate_stats(bacc))
# print(calculate_stats(wsf1))
# plot_box(datas=bacc.tolist(), names=["0-shot", "1-shot", "5-shot", "10-shot"], title="GBM LGG subtyping BACC", save_path='gbmlgg_sub_bacc')
# plot_box(datas=wsf1.tolist(), names=["0-shot", "1-shot", "5-shot", "10-shot"], title="GBM LGG subtyping WF1", save_path='gbmlgg_sub_wf1')
# data_cond1 = np.load('log/cam/cam_seg_auc_cond1.npy')
# data_cond2 = np.load('log/cam/cam_seg_auc_cond2.npy')
# data_cond3 = np.load('log/cam/cam_seg_auc_cond3.npy')
# data_cond4 = np.load('log/cam/cam_seg_auc_cond4.npy')
# datas = [[],[],[],[]]
# for i in range(4):
# if i==0:
# datas[i].append(data_cond1[0])
# datas[i].append(data_cond2[0])
# datas[i].append(data_cond3[0])
# datas[i].append(data_cond4[0])
# else:
# datas[i].append(data_cond1[4-i])
# datas[i].append(data_cond2[4-i])
# datas[i].append(data_cond3[4-i])
# datas[i].append(data_cond4[4-i])
# names = ['0-shot', '1-Shot', '5-Shot', '10-Shot']
# conds = ['Cond1', 'Cond2', 'Cond3', 'Cond4']
# title = 'camelyon segmentation auc'
# save_path = 'cam_seg_auc_conds.png'
# # 调用函数
# plot_box_block(datas, names, conds, title, save_path)
# # 检查文件是否存在
# import os
# if os.path.exists(save_path):
# print(f"Plot saved successfully at {save_path}")
# else:
# print("Failed to save the plot.")
# data = extract_result('log/cam/cam_seg_newloss2.txt', type='cancer_ratio')
# data = np.array(data)
# mu = []
# for i in range(2700):
# idx = i % 90
# if idx == 29:
# mu.append(data[i])
# elif idx == 59:
# mu.append(data[i])
# elif idx == 89:
# mu.append(data[i])
# else:
# continue
# mu = np.array(mu)[:, 1]
# gt_data = extract_result('log/cam/cam_seg_newloss2.txt', type='gt_cancer_ratio')
# r = []
# for d in gt_data:
# r.append(np.mean(d))
# r = np.array(r)
# result = np.load('log/cam/cam_seg_dice_newloss2.npy')[1:, ]
# result = np.concatenate(result)
# print(result.shape)
# delta = np.abs(mu - r)
# print(delta.shape)
# shot10_idx = [3*i for i in range(30)]
# plt.scatter(delta, result)
# plt.xlabel('delta')
# plt.ylabel('dice')
# plt.savefig('cam_seg_dice_delta_newloss2.png')
# plt.show()
# r, p = stats.pearsonr(delta, result)
# print(r, p)