Skip to content

Commit c24c414

Browse files
committed
updates in analysis
1 parent cff5411 commit c24c414

File tree

4 files changed

+64
-72
lines changed

4 files changed

+64
-72
lines changed

expipe/analysis/misc/pretty_plotting.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import matplotlib.pyplot as plt
22

3-
def set_grid(ax, bgcolor="#EAEAF2" , linecolor="w", linestyle="-", linewidth=1.3):
3+
4+
def set_grid(ax, bgcolor="#EAEAF2", linecolor="w", linestyle="-", linewidth=1.3):
45
"""
56
Set background color and grid line options
67
@@ -21,13 +22,13 @@ def set_grid(ax, bgcolor="#EAEAF2" , linecolor="w", linestyle="-", linewidth=1.3
2122
linewidth : float
2223
linewidth
2324
"""
24-
ax.set_axis_bgcolor(bgcolor)
25+
ax.set_facecolor(bgcolor)
2526
ax.set_axisbelow("True")
26-
ax.grid(True, color=linecolor, linestyle=linestyle, linewidth=linewidth, zorder = 0)
27+
ax.grid(True, color=linecolor, linestyle=linestyle, linewidth=linewidth, zorder=0)
2728

2829

29-
def spines_edge_color(ax, edges = {"top": "none", "bottom": "w",
30-
"right": "none", "left": "w"}):
30+
def spines_edge_color(ax, edges={"top": "none", "bottom": "w",
31+
"right": "none", "left": "w"}):
3132
"""
3233
Set spines edge color
3334
@@ -71,7 +72,7 @@ def remove_ticklabels(ax, x_axis=True, y_axis=True):
7172
ax.set_yticklabels(empty_string_labels)
7273

7374

74-
def move_spines(ax, edges = {"bottom": 0, "left": 0}):
75+
def move_spines(ax, edges={"bottom": 0, "left": 0}):
7576
"""
7677
Move spines
7778
Parameters
@@ -84,7 +85,8 @@ def move_spines(ax, edges = {"bottom": 0, "left": 0}):
8485
"""
8586

8687
for edge, value in edges.iteritems():
87-
ax.spines[edge].set_position(('data',value))
88+
ax.spines[edge].set_position(('data', value))
89+
8890

8991
def remove_ticks(ax):
9092
"""
@@ -113,7 +115,7 @@ def set_font():
113115
'font.size': 14,
114116
'axes.titlesize': 18,
115117
'axes.labelsize': 16,
116-
'lines.linewidth':2,
118+
'lines.linewidth': 2,
117119
}
118120
plt.rcParams.update(params)
119121

@@ -123,15 +125,15 @@ def set_legend():
123125
legend options
124126
"""
125127
params = {
126-
'legend.fontsize' : 'medium',
127-
'legend.handlelength' : 2.2,
128+
'legend.fontsize': 'medium',
129+
'legend.handlelength': 2.2,
128130
'legend.frameon': False,
129131
'legend.numpoints': 1,
130132
'legend.scatterpoints': 1,
131-
'legend.fontsize' : 14,
132-
'legend.handlelength' : 2.2,
133-
'legend.borderpad' : 0.0,
134-
'legend.framealpha' : 2,
133+
'legend.fontsize': 14,
134+
'legend.handlelength': 2.2,
135+
'legend.borderpad': 0.0,
136+
'legend.framealpha': 2,
135137
}
136138
plt.rcParams.update(params)
137139

@@ -166,9 +168,9 @@ def colormap(color=0):
166168

167169
#########################################################################################
168170
if __name__ == "__main__":
169-
import numpy as np
170-
171-
t = np.linspace(0,100,1000)
171+
import numpy as np
172+
173+
t = np.linspace(0, 100, 1000)
172174
fig = plt.figure()
173175
ax = fig.add_subplot(111)
174176

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .plot import polar_tuning_curve
2+
from .tools import (average_rate)

expipe/analysis/visual_stimulus/plot.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
import numpy as np
22
import matplotlib.colors as colors
33
import matplotlib.pyplot as plt
4+
import quantities as pq
5+
import seaborn
6+
7+
8+
def polar_tuning_curve(orients, rates, params={}):
9+
"""
10+
Direction polar tuning curve
11+
"""
12+
import numpy as np
13+
import math
14+
from expipe.analysis.misc import pretty_plotting
15+
16+
assert len(orients) == len(rates)
17+
18+
fig, ax = plt.subplots()
19+
ax = plt.subplot(111, projection='polar')
20+
ax.plot(orients, rates, '-o', **params)
21+
22+
return fig, ax
23+
424

525
def _gauss_function(x, y, A=1, a=0.63):
626
r2 = x**2 + y**2
727
return A / a**2 / np.pi * np.exp(-r2 / a**2)
8-
928

29+
1030
def _dog_function(x, y, A=1.0, a=0.63, B=0.85, b=1.23):
1131
center = _gauss_function(x, y, A, a)
1232
surround = _gauss_function(x, y, B, b)
1333
return center - surround
34+
1435

1536
def _doe_function(t, a=1, b=2):
1637
return np.exp(-t / a) / a**2 - np.exp(-t / b) / b**2
1738

1839

1940
def _field(x, y, t, A=1.0, a=0.63, B=0.85, b=1.23, c=1, d=2):
20-
return _dog_function(x, y, A, a, B, b) * _doe_function (t, c, d)
41+
return _dog_function(x, y, A, a, B, b) * _doe_function(t, c, d)
2142

2243

2344
class MidpointNormalize(colors.Normalize):
@@ -30,7 +51,6 @@ def __call__(self, value, clip=None):
3051
return np.ma.masked_array(np.interp(value, x, y))
3152

3253

33-
3454
def rectangular_scalebar(ax, params={}):
3555
'''
3656
Adds scalebar (rectangle) to an axes object with text
@@ -180,29 +200,6 @@ def plot_advance_receptive_field(field):
180200

181201

182202

183-
def polar_tuning_curve(trails,
184-
params={}):
185-
"""
186-
Direction polar tuning curve
187-
"""
188-
import numpy as np
189-
import matplotlib.pyplot as plt
190-
from expipe.analysis.visual_stimulus.tools import average_rate
191-
from expipe.analysis.misc import pretty_plotting
192-
193-
194-
# TODO: average rate should give both radians and degrees
195-
av_rate, orients = average_rate(trails)
196-
orients *= np.pi/180.
197-
198-
fig, ax = plt.subplots()
199-
ax = plt.subplot(111, projection='polar')
200-
ax.plot(orients, av_rate, '-ro', **params)
201-
202-
pretty_plotting.set_grid(ax)
203-
204-
return fig, ax
205-
206203

207204

208205

expipe/analysis/visual_stimulus/tools.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def average_rate(trails):
1313
1414
Returns
1515
-------
16-
av_rate : array
16+
avg_rate : array
1717
average rates
1818
orients : array
1919
sorted stimulus orientations
@@ -23,21 +23,22 @@ def average_rate(trails):
2323
spike_count_rate = collect.defaultdict(list)
2424

2525
for trail in trails:
26-
orient = float(trail.annotations.values()[0])
27-
rate = mean_firing_rate(trail, trail.t_start, trail.t_stop )
26+
orient = float(list(trail.annotations.values())[0])
27+
rate = mean_firing_rate(trail, trail.t_start, trail.t_stop)
2828
spike_count_rate[orient].append(rate)
2929

30-
av_rate = np.zeros(len(spike_count_rate))
31-
orients = np.zeros(len(spike_count_rate))
30+
avg_rate = np.zeros(len(spike_count_rate))
31+
orients = np.zeros(len(spike_count_rate))
3232

3333
for i, orient in enumerate(spike_count_rate):
34-
av_rate[i] = np.mean(spike_count_rate[orient])
34+
avg_rate[i] = np.mean(spike_count_rate[orient])
3535
orients[i] = orient
3636

3737
sorted_indices = np.argsort(orients)
38-
orients = orients[sorted_indices]*pq.deg
39-
av_rate = av_rate[sorted_indices]*1./pq.s
40-
return av_rate, orients
38+
orients = orients[sorted_indices] * pq.deg
39+
avg_rate = avg_rate[sorted_indices] * 1./pq.s
40+
41+
return avg_rate, orients
4142

4243

4344
def wrap_angle(angle, wrap_range=360.):
@@ -55,7 +56,7 @@ def wrap_angle(angle, wrap_range=360.):
5556
angle in interval [0, wrap_range]
5657
5758
'''
58-
return angle - wrap_range * np.floor(angle/float(wrap_range));
59+
return angle - wrap_range * np.floor(angle/float(wrap_range))
5960

6061

6162
def compute_selectivity_index(av_rates, orients, selectivity_type):
@@ -77,36 +78,34 @@ def compute_selectivity_index(av_rates, orients, selectivity_type):
7778
out : float
7879
selectivity index
7980
'''
80-
81-
preferred = np.where(av_rates==av_rates.max())
82-
null_angle = wrap_angle(orients[preferred] + 180*pq.deg, wrap_range=360.)
81+
preferred = np.where(av_rates == av_rates.max())
82+
null_angle = wrap_angle(orients[preferred] + 180*pq.deg, wrap_range=360.)
8383

8484
null = np.where(orients == null_angle)
8585
if len(null[0]) == 0:
86-
raise Exception("orientation not found: "+ str(null_angle))
86+
raise Exception("orientation not found: "+str(null_angle))
8787

88-
if(selectivity_type=="dsi"):
89-
index = 1.- av_rates[null] / av_rates[preferred]
88+
if(selectivity_type == "dsi"):
89+
index = 1. - av_rates[null] / av_rates[preferred]
9090
return orients[preferred], index
9191

92-
elif(selectivity_type=="osi"):
92+
elif(selectivity_type == "osi"):
9393
orth_angle_p = wrap_angle(orients[preferred] + 90*pq.deg, wrap_range=360.)
9494
orth_angle_n = wrap_angle(orients[preferred] - 90*pq.deg, wrap_range=360.)
9595
orth_p = np.where(orients == orth_angle_p)
9696
orth_n = np.where(orients == orth_angle_n)
9797

9898
if len(orth_p[0]) == 0:
99-
raise Exception("orientation not found: "+ str(orth_angle_p))
99+
raise Exception("orientation not found: " BaseException+str(orth_angle_p))
100100
if len(orth_n[0]) == 0:
101-
raise Exception("orientation not found: "+ str(orth_angle_n))
101+
raise Exception("orientation not found: " + str(orth_angle_n))
102102

103-
index = 1.- (av_rates[orth_p] + av_rates[orth_n]) / (av_rates[preferred]+av_rates[null])
103+
index = 1. - (av_rates[orth_p] + av_rates[orth_n]) / (av_rates[preferred]+av_rates[null])
104104
return orients[preferred], index
105105

106106
else:
107107
raise ValueError("unknown selectivity type: ", str(selectivity_type), " options: osi, dsi")
108108

109-
110109

111110
def make_stim_off_epoch_array(epo, include_boundary=True):
112111
'''
@@ -136,11 +135,3 @@ def make_stim_off_epoch_array(epo, include_boundary=True):
136135
durations = np.append(epo.times[0], durations)*pq.s
137136

138137
return EpochArray(times=times, durations=durations, labels=[np.NAN]*len(times))
139-
140-
141-
if __name__ == "__main__":
142-
compute_receptive_field()
143-
144-
145-
146-

0 commit comments

Comments
 (0)