-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfig4.py
97 lines (80 loc) · 3.21 KB
/
fig4.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
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import tifffile
from operator import itemgetter
from skimage.filters import gaussian
from functions import init_fig, gfp
import ca_source_extraction as cse # https://github.com/j-friedrich/CaImAn/tree/multi-scale_paper
if matplotlib.__version__[0] == '2':
matplotlib.style.use('classic')
try:
from sys import argv
from os.path import isdir
figpath = argv[1] if isdir(argv[1]) else False
except:
figpath = False
init_fig()
plt.rc('font', size=20, **{'family': 'sans-serif', 'sans-serif': ['Computer Modern']})
#
# Load data
d1, d2, T = (512, 512, 2000)
filename = '180um_20fps_350umX350um.tif'
Yr = tifffile.TiffFile(filename).asarray().astype(dtype='float32')
Yr = np.transpose(Yr, (1, 2, 0))
Ymax = Yr.max(-1)
Yr = np.reshape(Yr, (d1 * d2, T), order='F')
A2, b2 = itemgetter('A2', 'b2')(np.load('results/CNMF-HRshapes.npz'))
A2 = A2.item()
N = A2.shape[1]
ssub = np.load('results/decimate.npz')['ssub'].item()
A_or, C_or, srt = cse.utilities.order_components(A2, ssub[1][0])
A_or = np.transpose([gaussian(a.reshape(d1, d2), 1).ravel() for a in A_or.T])
#
# Plot data
fig = plt.figure(figsize=(13, 12))
fig.add_axes([0, .5, .46, .46])
crd = cse.utilities.plot_contours(A_or, Ymax, thr=0.9, display_numbers=False, colors='w')
ax = plt.gca()
ax.imshow(Yr.reshape(-1, T / 20, 20).mean(-1).max(-1).reshape(d1, d2).T,
cmap=gfp, vmin=400, vmax=4000)
ax.set_xticks([])
ax.set_yticks([])
plt.axis('off')
plt.title('data', fontsize=20)
fig.add_axes([.45, .5, .46, .46])
crd = cse.utilities.plot_contours(A_or, Ymax, thr=0.9, display_numbers=False, colors='w')
ax = plt.gca()
denoised = b2.dot(ssub[1][1]).astype('float32') + A2.dot(ssub[1][0]).astype('float32')
ax.imshow(denoised.reshape(d1, d2, T / 10, 10).mean(-1).max(-1).T, cmap=gfp, vmin=400, vmax=4000)
ax.set_xticks([])
ax.set_yticks([])
plt.axis('off')
plt.title('denoised', fontsize=20)
fig.add_axes([0, .005, .46, .46])
crd = cse.utilities.plot_contours(A_or, Ymax, thr=0.9, display_numbers=False, colors='w')
ax = plt.gca()
ax.imshow(Yr.reshape(d1 / 16, 16, d2 / 16, 16, T / 10, 10).mean(-1).mean(-2).mean(-3)
.max(-1).T.repeat(16, 0).repeat(16, 1), cmap=gfp, vmin=400, vmax=4000)
ax.set_xticks([])
ax.set_yticks([])
plt.axis('off')
plt.title('low resolution data (downsampled by 16x16)', fontsize=20)
fig.add_axes([.45, .005, .46, .46])
crd = cse.utilities.plot_contours(A_or, Ymax, thr=0.9, display_numbers=False, colors='w')
ax = plt.gca()
denoised = b2.dot(ssub[16][1]).astype('float32') + A2.dot(ssub[16][0]).astype('float32')
im = ax.imshow(denoised.reshape(d1, d2, T / 10, 10).mean(-1).max(-1).T,
cmap=gfp, vmin=400, vmax=4000)
del denoised # free memory
ax.set_xticks([])
ax.set_yticks([])
plt.axis('off')
plt.title('high resolution reconstruction', fontsize=20)
cax = fig.add_axes([.91, .008, .02, .95])
cb = plt.colorbar(im, cax=cax, label='Maximum Projection')
cb.set_ticks(range(0, 4000, 1000))
plt.yticks(range(0, 4000, 1000), range(1000, 5000, 1000), fontsize=18)
cb.ax.yaxis.label.set_font_properties(matplotlib.font_manager.FontProperties(size=18))
plt.savefig(figpath + '/data+reconstruction.pdf', bbox_inches='tight', pad_inches=.01)\
if figpath else plt.show(block=True)