forked from AllenCellModeling/pytorch_integrated_cell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_variation_figures.py
148 lines (102 loc) · 4.29 KB
/
print_variation_figures.py
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
import pandas as pd
import matplotlib as mpl
import os
import pickle
import numpy as np
import matplotlib as mpl
mpl.use('Agg')
from matplotlib import pyplot as plt
import pdb
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--parent_dir', help='save dir')
args = parser.parse_args()
from tqdm import tqdm
# parent_dir = './test_aaegan/aaegan3Dv8_v2/'
parent_dir = args.parent_dir
analysis_dir = parent_dir + os.sep + 'analysis'
data_var_dir = analysis_dir + os.sep + 'data_variation'
figure_dir = parent_dir + os.sep + 'figures'
def print_hist(data_var_info, bins, label_inds):
for row in data_var_info.iterrows():
with open(row[1]['save_path'], 'rb') as file:
data = pickle.load(file)
try:
triu_vals = data['corr_mat_struct'][np.triu_indices(data['corr_mat_struct'].shape[0])]
except:
triu_vals = data['corr_mat'][np.triu_indices(data['corr_mat'].shape[0])]
plt.hist(triu_vals, bins=bins, normed=True, alpha = 0.5, label=data['label_name'],
color=colors[np.where(label_inds == row[1]['label_id'])[0]])
plt.axis(axis)
plt.xlabel('correlation')
def set_axis_style(ax, labels):
# ax.get_yaxis().set_tick_params(direction='out')
# ax.yaxis.set_ticks_position('bottom')
ax.set_yticks(np.arange(0, len(labels)))
plt.gca().set_yticklabels([])
ax.set_ylim(-1.25, len(labels))
ax.invert_yaxis()
# for tick in ax.get_xticklabels():
# tick.set_rotation(90)
def print_violin(data_var_info, bins, ulabels):
data_list = list()
for row in tqdm(data_var_info.iterrows()):
with open(row[1]['save_path'], 'rb') as file:
data = pickle.load(file)
try:
triu_vals = data['corr_mat_struct'][np.triu_indices(data['corr_mat_struct'].shape[0], k=1)]
except:
triu_vals = data['corr_mat'][np.triu_indices(data['corr_mat'].shape[0], k=1)]
## comment in for fast testing
# triu_vals = triu_vals[np.random.rand(triu_vals.shape[0]) <= 0.01]
plt.violinplot(triu_vals, [row[1]['label_id']], vert=False, showmedians=True, showextrema=False)
plt.axis(axis)
set_axis_style(plt.gca(), ulabels)
plt.xlabel('correlation')
figsize = (6,6)
colormap = 'Vega20'
################
# DATA VARIATION
################
data_var_dir = analysis_dir + os.sep + 'data_variation'
data_var_info = pd.read_csv(data_var_dir + os.sep + 'info.csv')
[ulabels, label_inds] = np.unique(data_var_info.label_name, return_inverse=True)
ulabels[ulabels == 'Desmoplakin'] = 'desmoplakin'
ulabels[ulabels == 'Fibrillarin'] = 'fibrillarin'
ulabels[ulabels == 'Lamin B1'] = 'lamin B1'
colors = plt.get_cmap(colormap)(np.linspace(0, 1, len(ulabels)+1))*0.8
bins = np.arange(0, 1, 0.005)
axis = [-0.05, 1, 0, len(ulabels)+2]
plt.figure(figsize=figsize)
print_violin(data_var_info, bins, label_inds)
plt.title('data')
plt.gca().set_yticklabels(ulabels)
plt.savefig('{0}/distr.png'.format(figure_dir), bbox_inches='tight')
plt.close('all')
#################
# Encode-Decode variation
#################
data_var_dir = analysis_dir + os.sep + 'model_structure_variation'
data_var_info = pd.read_csv(data_var_dir + os.sep + 'info.csv')
train_test_dict = {'train': 1, 'test': 2}
for train_or_test in train_test_dict:
plt.figure(figsize=figsize)
data_var_info_tmp = data_var_info[data_var_info['train_or_test'] == train_or_test]
print_violin(data_var_info_tmp, bins, label_inds)
plt.title(train_or_test)
if train_or_test == 'train':
plt.gca().set_yticklabels(ulabels)
plt.savefig('{0}/distr_{1}.png'.format(figure_dir, train_or_test), bbox_inches='tight')
plt.close('all')
data_var_dir = analysis_dir + os.sep + 'model_structure_variation_sampled'
data_var_info = pd.read_csv(data_var_dir + os.sep + 'info.csv')
#################
# SAMPLED VARIATION
#################
# pdb.set_trace()
plt.figure(figsize=figsize)
print_violin(data_var_info, bins, label_inds)
plt.title('sampled')
# plt.legend( loc=1, borderaxespad=0, frameon=False)
plt.savefig('{0}/distr_sampled.png'.format(figure_dir), bbox_inches='tight')
plt.close('all')