1
- '''
2
- This script is used to test Current Source Density Estimates,
1
+ """
2
+ This script is used to test Current Source Density Estimates,
3
3
using the kCSD method Jan et.al (2012)
4
4
5
5
This script is in alpha phase.
6
6
7
7
This was written by :
8
- Chaitanya Chintaluri,
8
+ Chaitanya Chintaluri,
9
9
Laboratory of Neuroinformatics,
10
10
Nencki Institute of Exprimental Biology, Warsaw.
11
- '''
11
+ """
12
+
12
13
import os
13
14
import numpy as np
14
- from scipy .integrate import simps
15
+ from scipy .integrate import simpson
15
16
import matplotlib .pyplot as plt
16
17
from kcsd import KCSD1D
17
18
from figure_LC import make_plots
18
19
from figure_LCandCVperformance import make_plot_perf
19
20
from figure_LCandCV import plot_surface
20
21
22
+
21
23
def do_kcsd (i , ele_pos , pots , ** params ):
22
24
"""
23
25
Function that calls the KCSD1D module
@@ -26,127 +28,197 @@ def do_kcsd(i, ele_pos, pots, **params):
26
28
pots = pots .reshape (num_ele , 1 )
27
29
ele_pos = ele_pos .reshape (num_ele , 1 )
28
30
k = KCSD1D (ele_pos , pots , ** params )
29
- noreg_csd = k .values (' CSD' )
31
+ noreg_csd = k .values (" CSD" )
30
32
k .cross_validate (Rs = Rs , lambdas = lambdas )
31
33
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" )
35
37
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" )
40
42
return k , est_csd , est_pot , noreg_csd , errsy , est_csd_cv
41
43
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
+ ):
46
56
"""
47
57
Gives CSD profile at the requested spatial location, at 'res' resolution
48
58
"""
49
59
csd_y = np .linspace (start_x , end_x , res_x )
50
60
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
55
65
csd_y = np .linspace (start_x , end_x , res_x )
56
66
return csd_x , csd_y , f
57
67
68
+
58
69
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 )
60
71
y = csd * m
61
- I = simps (y , csd_x )
72
+ I = simpson (y , csd_x )
62
73
return I
63
74
75
+
64
76
def calculate_potential_1D (csd , measure_locations , csd_space_x , h ):
65
77
sigma = 1
66
78
print (measure_locations .shape , csd_space_x .shape , csd .shape )
67
79
pots = np .zeros (len (measure_locations ))
68
80
for ii in range (len (measure_locations )):
69
81
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
71
83
return pots
72
84
85
+
73
86
def transformAndNormalizeLongGrid (D ):
74
- #D is a dictionary
87
+ # D is a dictionary
75
88
locations = np .zeros ((len (D ), 2 ))
76
89
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" ]
79
92
return locations
80
93
94
+
81
95
def generate_electrodes (inpos , lpos , b ):
82
96
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
+
85
100
def _addLocToDict (D , chnum , x , y ):
86
101
d = {}
87
- d ['x' ] = x
88
- d ['y' ] = y
102
+ d ["x" ] = x
103
+ d ["y" ] = y
89
104
D [chnum ] = d
90
- #first add locations just as they suppose to be
105
+
106
+ # first add locations just as they suppose to be
91
107
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 )
93
109
loc_mtrx = transformAndNormalizeLongGrid (loc )
94
110
loc_mtrx = loc_mtrx .T
95
111
return loc_mtrx [0 ], loc_mtrx [1 ]
96
112
113
+
97
114
def electrode_config (ele_res , true_csd , csd_x , csd_y , inpos , lpos ):
98
115
"""
99
116
What is the configuration of electrode positions, between what and what positions
100
117
"""
101
118
ele_x , ele_y = generate_electrodes (inpos , lpos , ele_res )
102
119
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
104
121
return ele_pos , pots
105
122
123
+
106
124
def main_loop (src_width , total_ele , inpos , lpos , nm , noise = 0 , srcs = 1 ):
107
125
"""
108
126
Loop that decides the random number seed for the CSD profile,
109
127
electrode configurations and etc.
110
128
"""
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
118
145
for i , noise in enumerate (n_spec ):
119
- plt .close (' all' )
146
+ plt .close (" all" )
120
147
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
+ )
124
153
ele_y = ele_pos [:, 1 ]
125
154
gdX = 0.01
126
- x_lims = [0 , 1 ] # CSD estimation place
155
+ x_lims = [0 , 1 ] # CSD estimation place
127
156
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 ))
132
174
m_norm = k .m_norm
133
175
m_resi = k .m_resi
134
176
curve_surf = k .curve_surf
135
177
title = [str (k .lambd ), str (k .R ), str (noise ), nm ]
136
178
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__" :
150
222
saveDir = "./LCurve/"
151
223
try :
152
224
os .chdir (saveDir )
@@ -158,26 +230,25 @@ def main_loop(src_width, total_ele, inpos, lpos, nm, noise=0, srcs=1):
158
230
src_width = 0.001
159
231
noises = 9
160
232
seeds = 9
161
- inpos = [0.5 , 0.1 ]# od dolu
233
+ inpos = [0.5 , 0.1 ] # od dolu
162
234
lpos = [0.5 , 0.9 ]
163
235
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 )
165
237
lambdas = np .logspace (- 7 , - 3 , 50 )
166
238
sim_results = np .zeros ((2 , 4 , seeds , noises ))
167
239
sim_results [:, 3 ] = noise_lvl
168
240
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 )
174
247
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
178
250
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" )
182
254
make_plot_perf (sim_results , noise_lvl )
183
-
0 commit comments