Skip to content

Commit 47001e7

Browse files
committed
Replace deprecated scipy integrate.simps with integrate.simpson.
1 parent 21418c5 commit 47001e7

10 files changed

+2160
-1343
lines changed

figures/kCSD_properties/L_curve_simulation.py

Lines changed: 148 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
'''
2-
This script is used to test Current Source Density Estimates,
1+
"""
2+
This script is used to test Current Source Density Estimates,
33
using the kCSD method Jan et.al (2012)
44
55
This script is in alpha phase.
66
77
This was written by :
8-
Chaitanya Chintaluri,
8+
Chaitanya Chintaluri,
99
Laboratory of Neuroinformatics,
1010
Nencki Institute of Exprimental Biology, Warsaw.
11-
'''
11+
"""
12+
1213
import os
1314
import numpy as np
14-
from scipy.integrate import simps
15+
from scipy.integrate import simpson
1516
import matplotlib.pyplot as plt
1617
from kcsd import KCSD1D
1718
from figure_LC import make_plots
1819
from figure_LCandCVperformance import make_plot_perf
1920
from figure_LCandCV import plot_surface
2021

22+
2123
def do_kcsd(i, ele_pos, pots, **params):
2224
"""
2325
Function that calls the KCSD1D module
@@ -26,127 +28,197 @@ def do_kcsd(i, ele_pos, pots, **params):
2628
pots = pots.reshape(num_ele, 1)
2729
ele_pos = ele_pos.reshape(num_ele, 1)
2830
k = KCSD1D(ele_pos, pots, **params)
29-
noreg_csd = k.values('CSD')
31+
noreg_csd = k.values("CSD")
3032
k.cross_validate(Rs=Rs, lambdas=lambdas)
3133
errsy = k.errs
32-
LandR[1,0,i] = k.lambd
33-
LandR[1,1,i] = k.R
34-
est_csd_cv = k.values('CSD')
34+
LandR[1, 0, i] = k.lambd
35+
LandR[1, 1, i] = k.R
36+
est_csd_cv = k.values("CSD")
3537
k.L_curve(lambdas=lambdas, Rs=Rs)
36-
LandR[0,0,i] = k.lambd
37-
LandR[0,1,i] = k.R
38-
est_csd = k.values('CSD')
39-
est_pot = k.values('POT')
38+
LandR[0, 0, i] = k.lambd
39+
LandR[0, 1, i] = k.R
40+
est_csd = k.values("CSD")
41+
est_pot = k.values("POT")
4042
return k, est_csd, est_pot, noreg_csd, errsy, est_csd_cv
4143

42-
def generate_csd_1D(src_width, name, srcs=np.array([0]),
43-
start_x=0., end_x=1.,
44-
start_y=0., end_y=1.,
45-
res_x=50, res_y=50):
44+
45+
def generate_csd_1D(
46+
src_width,
47+
name,
48+
srcs=np.array([0]),
49+
start_x=0.0,
50+
end_x=1.0,
51+
start_y=0.0,
52+
end_y=1.0,
53+
res_x=50,
54+
res_y=50,
55+
):
4656
"""
4757
Gives CSD profile at the requested spatial location, at 'res' resolution
4858
"""
4959
csd_y = np.linspace(start_x, end_x, res_x)
5060
csd_x = np.linspace(start_x, end_x, res_x)
51-
src_peak = .7*np.exp(-((csd_x-0.25)**2)/(2.*src_width))
52-
src_thr1 = .35*np.exp(-((csd_x-0.45)**2)/(2.*src_width))
53-
src_thr2 = .35*np.exp(-((csd_x-0.65)**2)/(2.*src_width))
54-
f = src_peak-src_thr1-src_thr2
61+
src_peak = 0.7 * np.exp(-((csd_x - 0.25) ** 2) / (2.0 * src_width))
62+
src_thr1 = 0.35 * np.exp(-((csd_x - 0.45) ** 2) / (2.0 * src_width))
63+
src_thr2 = 0.35 * np.exp(-((csd_x - 0.65) ** 2) / (2.0 * src_width))
64+
f = src_peak - src_thr1 - src_thr2
5565
csd_y = np.linspace(start_x, end_x, res_x)
5666
return csd_x, csd_y, f
5767

68+
5869
def integrate_1D(x0, csd_x, csd, h):
59-
m = np.sqrt((csd_x-x0)**2 + h**2) - abs(csd_x-x0)
70+
m = np.sqrt((csd_x - x0) ** 2 + h**2) - abs(csd_x - x0)
6071
y = csd * m
61-
I = simps(y, csd_x)
72+
I = simpson(y, csd_x)
6273
return I
6374

75+
6476
def calculate_potential_1D(csd, measure_locations, csd_space_x, h):
6577
sigma = 1
6678
print(measure_locations.shape, csd_space_x.shape, csd.shape)
6779
pots = np.zeros(len(measure_locations))
6880
for ii in range(len(measure_locations)):
6981
pots[ii] = integrate_1D(measure_locations[ii], csd_space_x, csd, h)
70-
pots *= 1/(2.*sigma) #eq.: 26 from Potworowski et al
82+
pots *= 1 / (2.0 * sigma) # eq.: 26 from Potworowski et al
7183
return pots
7284

85+
7386
def transformAndNormalizeLongGrid(D):
74-
#D is a dictionary
87+
# D is a dictionary
7588
locations = np.zeros((len(D), 2))
7689
for i in range(len(D)):
77-
locations[i, 0] = D[i]['x']
78-
locations[i, 1] = D[i]['y']
90+
locations[i, 0] = D[i]["x"]
91+
locations[i, 1] = D[i]["y"]
7992
return locations
8093

94+
8195
def generate_electrodes(inpos, lpos, b):
8296
loc = {}
83-
cordx = -(inpos[0] - lpos[0])/b
84-
cordy = -(inpos[1] - lpos[1])/b
97+
cordx = -(inpos[0] - lpos[0]) / b
98+
cordy = -(inpos[1] - lpos[1]) / b
99+
85100
def _addLocToDict(D, chnum, x, y):
86101
d = {}
87-
d['x'] = x
88-
d['y'] = y
102+
d["x"] = x
103+
d["y"] = y
89104
D[chnum] = d
90-
#first add locations just as they suppose to be
105+
106+
# first add locations just as they suppose to be
91107
for i in range(b):
92-
_addLocToDict(loc, i, inpos[0] + cordx*i, inpos[1] + cordy*i)
108+
_addLocToDict(loc, i, inpos[0] + cordx * i, inpos[1] + cordy * i)
93109
loc_mtrx = transformAndNormalizeLongGrid(loc)
94110
loc_mtrx = loc_mtrx.T
95111
return loc_mtrx[0], loc_mtrx[1]
96112

113+
97114
def electrode_config(ele_res, true_csd, csd_x, csd_y, inpos, lpos):
98115
"""
99116
What is the configuration of electrode positions, between what and what positions
100117
"""
101118
ele_x, ele_y = generate_electrodes(inpos, lpos, ele_res)
102119
pots = calculate_potential_1D(true_csd, ele_y.T, csd_y, h=1)
103-
ele_pos = np.vstack((ele_x, ele_y)).T #Electrode configs
120+
ele_pos = np.vstack((ele_x, ele_y)).T # Electrode configs
104121
return ele_pos, pots
105122

123+
106124
def main_loop(src_width, total_ele, inpos, lpos, nm, noise=0, srcs=1):
107125
"""
108126
Loop that decides the random number seed for the CSD profile,
109127
electrode configurations and etc.
110128
"""
111-
#TrueCSD
112-
t_csd_x, t_csd_y, true_csd = generate_csd_1D(src_width, nm, srcs=srcs,
113-
start_x=0, end_x=1.,
114-
start_y=0, end_y=1,
115-
res_x=101, res_y=101)
116-
if type(noise) == float: n_spec = [noise]
117-
else: n_spec = noise
129+
# TrueCSD
130+
t_csd_x, t_csd_y, true_csd = generate_csd_1D(
131+
src_width,
132+
nm,
133+
srcs=srcs,
134+
start_x=0,
135+
end_x=1.0,
136+
start_y=0,
137+
end_y=1,
138+
res_x=101,
139+
res_y=101,
140+
)
141+
if type(noise) == float:
142+
n_spec = [noise]
143+
else:
144+
n_spec = noise
118145
for i, noise in enumerate(n_spec):
119-
plt.close('all')
146+
plt.close("all")
120147
noise = np.round(noise, 5)
121-
print('number of reconstruction: ', i, 'noise level: ', noise)
122-
#Electrodes
123-
ele_pos, pots = electrode_config(total_ele, true_csd, t_csd_x, t_csd_y, inpos, lpos)
148+
print("number of reconstruction: ", i, "noise level: ", noise)
149+
# Electrodes
150+
ele_pos, pots = electrode_config(
151+
total_ele, true_csd, t_csd_x, t_csd_y, inpos, lpos
152+
)
124153
ele_y = ele_pos[:, 1]
125154
gdX = 0.01
126-
x_lims = [0, 1] #CSD estimation place
155+
x_lims = [0, 1] # CSD estimation place
127156
np.random.seed(srcs)
128-
pots_n = pots + (np.random.rand(total_ele)*np.max(abs(pots))-np.max(abs(pots))/2)*noise
129-
k, est_csd, est_pot, noreg_csd, errsy, est_csd_cv = do_kcsd(i, ele_y, pots_n, h=1., gdx=gdX, sigma = 1,
130-
xmin=x_lims[0], xmax=x_lims[1], n_src_init=1e3)
131-
save_as = nm + '_noise' + str(np.round(noise*100, 1))
157+
pots_n = (
158+
pots
159+
+ (np.random.rand(total_ele) * np.max(abs(pots)) - np.max(abs(pots)) / 2)
160+
* noise
161+
)
162+
k, est_csd, est_pot, noreg_csd, errsy, est_csd_cv = do_kcsd(
163+
i,
164+
ele_y,
165+
pots_n,
166+
h=1.0,
167+
gdx=gdX,
168+
sigma=1,
169+
xmin=x_lims[0],
170+
xmax=x_lims[1],
171+
n_src_init=1e3,
172+
)
173+
save_as = nm + "_noise" + str(np.round(noise * 100, 1))
132174
m_norm = k.m_norm
133175
m_resi = k.m_resi
134176
curve_surf = k.curve_surf
135177
title = [str(k.lambd), str(k.R), str(noise), nm]
136178
if plot:
137-
make_plots(title, m_norm, m_resi, true_csd, curve_surf, ele_y, pots_n,
138-
pots, k.estm_x, est_pot, est_csd, noreg_csd, save_as)
139-
plot_surface(curve_surf, errsy, save_as+'surf')
140-
vals_to_save = {'m_norm':m_norm, 'm_resi':m_resi, 'true_csd':true_csd,
141-
'curve_surf':curve_surf, 'ele_y':ele_y, 'pots_n':pots_n,
142-
'pots':pots, 'estm_x':k.estm_x, 'est_pot':est_pot,
143-
'est_csd':est_csd, 'noreg_csd':noreg_csd, 'errsy':errsy}
144-
np.savez('data_fig4_and_fig13_'+save_as, **vals_to_save)
145-
print(true_csd.shape, est_csd[:,0].shape)
146-
RMS_wek[0, i] = np.linalg.norm(true_csd/np.linalg.norm(true_csd) - est_csd[:,0]/np.linalg.norm(est_csd[:,0]))
147-
RMS_wek[1, i] = np.linalg.norm(true_csd/np.linalg.norm(true_csd) - est_csd_cv[:,0]/np.linalg.norm(est_csd_cv[:,0]))
148-
149-
if __name__=='__main__':
179+
make_plots(
180+
title,
181+
m_norm,
182+
m_resi,
183+
true_csd,
184+
curve_surf,
185+
ele_y,
186+
pots_n,
187+
pots,
188+
k.estm_x,
189+
est_pot,
190+
est_csd,
191+
noreg_csd,
192+
save_as,
193+
)
194+
plot_surface(curve_surf, errsy, save_as + "surf")
195+
vals_to_save = {
196+
"m_norm": m_norm,
197+
"m_resi": m_resi,
198+
"true_csd": true_csd,
199+
"curve_surf": curve_surf,
200+
"ele_y": ele_y,
201+
"pots_n": pots_n,
202+
"pots": pots,
203+
"estm_x": k.estm_x,
204+
"est_pot": est_pot,
205+
"est_csd": est_csd,
206+
"noreg_csd": noreg_csd,
207+
"errsy": errsy,
208+
}
209+
np.savez("data_fig4_and_fig13_" + save_as, **vals_to_save)
210+
print(true_csd.shape, est_csd[:, 0].shape)
211+
RMS_wek[0, i] = np.linalg.norm(
212+
true_csd / np.linalg.norm(true_csd)
213+
- est_csd[:, 0] / np.linalg.norm(est_csd[:, 0])
214+
)
215+
RMS_wek[1, i] = np.linalg.norm(
216+
true_csd / np.linalg.norm(true_csd)
217+
- est_csd_cv[:, 0] / np.linalg.norm(est_csd_cv[:, 0])
218+
)
219+
220+
221+
if __name__ == "__main__":
150222
saveDir = "./LCurve/"
151223
try:
152224
os.chdir(saveDir)
@@ -158,26 +230,25 @@ def main_loop(src_width, total_ele, inpos, lpos, nm, noise=0, srcs=1):
158230
src_width = 0.001
159231
noises = 9
160232
seeds = 9
161-
inpos = [0.5, 0.1]#od dolu
233+
inpos = [0.5, 0.1] # od dolu
162234
lpos = [0.5, 0.9]
163235
noise_lvl = np.linspace(0, 0.5, noises)
164-
Rs = np.linspace(0.025, 8*0.025, 8)
236+
Rs = np.linspace(0.025, 8 * 0.025, 8)
165237
lambdas = np.logspace(-7, -3, 50)
166238
sim_results = np.zeros((2, 4, seeds, noises))
167239
sim_results[:, 3] = noise_lvl
168240
for src in range(seeds):
169-
print('noise seed:', src)
170-
RMS_wek = np.zeros((2,len(noise_lvl)))
171-
LandR = np.zeros((2,2, len(noise_lvl)))
172-
mypath = 'LC' + str(src) + '/'
173-
if not os.path.isdir(mypath): os.makedirs(mypath)
241+
print("noise seed:", src)
242+
RMS_wek = np.zeros((2, len(noise_lvl)))
243+
LandR = np.zeros((2, 2, len(noise_lvl)))
244+
mypath = "LC" + str(src) + "/"
245+
if not os.path.isdir(mypath):
246+
os.makedirs(mypath)
174247
os.chdir(mypath)
175-
main_loop(src_width, total_ele, inpos,
176-
lpos, 'LC', noise=noise_lvl, srcs=src)
177-
sim_results[:,:2, src] = LandR
248+
main_loop(src_width, total_ele, inpos, lpos, "LC", noise=noise_lvl, srcs=src)
249+
sim_results[:, :2, src] = LandR
178250
sim_results[:, 2, src] = RMS_wek
179-
os.chdir('..')
180-
np.save('sim_results', sim_results)
181-
sim_results = np.load('sim_results.npy')
251+
os.chdir("..")
252+
np.save("sim_results", sim_results)
253+
sim_results = np.load("sim_results.npy")
182254
make_plot_perf(sim_results, noise_lvl)
183-

0 commit comments

Comments
 (0)