forked from ycanerol/pymer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_checker_stas.py
93 lines (79 loc) · 3.25 KB
/
plot_checker_stas.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 12 19:00:09 2017
@author: ycan
"""
import os
import numpy as np
import matplotlib.pyplot as plt
import plotfuncs as plf
import iofuncs as iof
import analysis_scripts as asc
def plot_checker_stas(exp_name, stim_nr, filename=None):
"""
Plot and save all STAs from checkerflicker analysis. The plots
will be saved in a new folder called STAs under the data analysis
path of the stimulus.
<exp_dir>/data_analysis/<stim_nr>_*/<stim_nr>_data.h5 file is
used by default. If a different file is to be used, filename
should be supplied.
"""
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
exp_dir = iof.exp_dir_fixer(exp_name)
stim_nr = str(stim_nr)
if filename:
filename = str(filename)
_, metadata = asc.read_spikesheet(exp_dir)
px_size = metadata['pixel_size(um)']
if not filename:
savefolder = 'STAs'
label = ''
else:
label = filename.strip('.npz')
savefolder = 'STAs_' + label
data = iof.load(exp_name, stim_nr, fname=filename)
clusters = data['clusters']
stas = data['stas']
filter_length = data['filter_length']
stx_h = data['stx_h']
exp_name = data['exp_name']
stimname = data['stimname']
for j in range(clusters.shape[0]):
a = stas[j]
subplot_arr = plf.numsubplots(filter_length)
sta_max = np.max(np.abs([np.max(a), np.min(a)]))
sta_min = -sta_max
plt.figure(dpi=250)
for i in range(filter_length):
ax = plt.subplot(subplot_arr[0], subplot_arr[1], i+1)
im = ax.imshow(a[:, :, i], vmin=sta_min, vmax=sta_max,
cmap=iof.config('colormap'))
ax.set_aspect('equal')
plt.axis('off')
if i == 0:
scalebar = AnchoredSizeBar(ax.transData,
10, '{} µm'.format(10*stx_h
* px_size),
'lower left',
pad=0,
color='k',
frameon=False,
size_vertical=1)
ax.add_artist(scalebar)
if i == filter_length-1:
plf.colorbar(im, ticks=[sta_min, 0, sta_max], format='%.2f')
plt.suptitle('{}\n{}\n'
'{:0>3}{:0>2} Rating: {}'.format(exp_name,
stimname+label,
clusters[j][0],
clusters[j][1],
clusters[j][2]))
savepath = os.path.join(exp_dir, 'data_analysis', stimname,
savefolder,
'{:0>3}{:0>2}'.format(clusters[j][0],
clusters[j][1]))
os.makedirs(os.path.split(savepath)[0], exist_ok=True)
plt.savefig(savepath+'.png', bbox_inches='tight')
plt.close()
print(f'Plotted checkerflicker STA for {stimname}')