Advertisement
Guest User

Untitled

a guest
Jul 24th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. '''Simple implementations of connectivity measures.'''
  4.  
  5. # Authors : pravsripad@gmail.com
  6. # daniel.vandevelden@yahoo.de
  7.  
  8. import sys
  9. import numpy as np
  10. import matplotlib.pyplot as pl
  11. import matplotlib.mlab as mlab
  12.  
  13.  
  14. con_name = 'coh'
  15. if con_name == '':
  16. print 'Please provided the connectivity method to use.'
  17. sys.exit()
  18.  
  19. amp = 1.
  20. amp2 = 1.
  21. sfreq = 1000.
  22. #_________
  23. nse_amp = 1
  24. noise_mu = 0.1 # mean phase, "center" of noise distribution
  25. nse_kappa = 2. # dispersion of noise dispritbution
  26. phase_mu = np.abs(np.deg2rad(45)) # mean circular phase (ENTER IN DEGREE), "center" of phase distribution
  27. phase_kappa = 10. # dispersion of phase dispritbution
  28. #____
  29. n_epochs = 120
  30. epoch_length = 1 # make epoch of 1 second each
  31. times = np.arange(0,epoch_length,1/(sfreq))
  32. nfft = 512
  33. #____
  34. freq1_start = 150
  35. freq1_end = 220
  36. freq_steps = 0.#___
  37. freq2_start = freq1_start
  38. freq2_end = freq1_end
  39. #___
  40. freq_band1 = np.linspace(freq1_start, freq1_end, times.size)
  41. freq_band2 = np.linspace(freq2_start, freq2_end, times.size)
  42. """#############################################################################
  43. #_____________________________Signal generated in epochs_______________________#
  44. #############################################################################"""
  45.  
  46. def simulate_x_y(times, n_epochs, amp, amp2, freq_band1, freq_band2, phase_mu, phase_kappa, nse_amp, noise_mu, nse_kappa):
  47. x = np.zeros((n_epochs, times.size))
  48. y = np.zeros((n_epochs, times.size))
  49. phase_shift_y = np.random.vonmises(phase_mu, phase_kappa, n_epochs)
  50. for i in range(0, n_epochs):
  51. print i
  52. for j in xrange(freq1_start,freq1_end):
  53. print j
  54. x[i] = (amp * np.sin(2 * np.pi * freq_band1 * times )+ np.sin(2 * np.pi * j * times)) + nse_amp * np.random.normal(loc=noise_mu, scale= nse_kappa, size=times.size)
  55. y[i] = (amp2 * np.sin(2 * np.pi * freq_band2 * times + phase_shift_y[i])+ np.sin(2 * np.pi * j * times)) + nse_amp * np.random.normal(loc=noise_mu, scale= nse_kappa, size=times.size)
  56.  
  57. return x, y, phase_shift_y
  58.  
  59. # return the values from the function is just for the coding process to check every variable
  60. x, y, phase_shift_y = simulate_x_y(times, n_epochs, amp, amp2, freq_band1, freq_band2, phase_mu, phase_kappa, nse_amp, noise_mu, nse_kappa)
  61.  
  62. x0 = x.flatten()
  63. y0 = y.flatten()
  64.  
  65. def compute_mean_psd_csd(x, y, n_epochs, nfft, sfreq):
  66. '''Computes mean of PSD and CSD for signals.'''
  67. n_freqs = nfft/2 + 1
  68.  
  69. Rxy = np.zeros((n_epochs, n_freqs), dtype=complex)
  70. Rxx = np.zeros((n_epochs, n_freqs), dtype=complex)
  71. Ryy = np.zeros((n_epochs, n_freqs), dtype=complex)
  72. for i in range(n_epochs):
  73. Rxy[i], freqs = mlab.csd(x[i], y[i], NFFT=nfft, Fs=sfreq)
  74. Rxx[i], _____ = mlab.psd(x[i], NFFT=nfft, Fs=sfreq)
  75. Ryy[i], _____ = mlab.psd(y[i], NFFT=nfft, Fs=sfreq)
  76.  
  77. Rxy_mean = np.mean(Rxy, axis=0)
  78. Rxx_mean = np.mean(Rxx, axis=0)
  79. Ryy_mean = np.mean(Ryy, axis=0)
  80.  
  81. return freqs, Rxy, Rxy_mean, np.real(Rxx_mean), np.real(Ryy_mean)
  82.  
  83. def my_coherence(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean):
  84. ''' Computes coherence. '''
  85. coh = np.zeros((n_freqs))
  86. for i in range(0, n_freqs):
  87. coh[i] = np.abs(Rxy_mean[i]) / np.sqrt(Rxx_mean[i] * Ryy_mean[i])
  88.  
  89. return coh
  90.  
  91. def my_imcoh(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean):
  92. ''' Computes imaginary coherence. '''
  93. imcoh = np.zeros((n_freqs))
  94. for i in range(0, n_freqs):
  95. imcoh[i] = np.imag(Rxy_mean[i]) / np.sqrt(Rxx_mean[i] * Ryy_mean[i])
  96.  
  97. return imcoh
  98.  
  99. def my_cohy(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean):
  100. ''' Computes coherency. '''
  101. cohy = np.zeros((n_freqs))
  102. for i in range(0, n_freqs):
  103. cohy[i] = np.real(Rxy_mean[i]) / np.sqrt(Rxx_mean[i] * Ryy_mean[i])
  104.  
  105. return cohy
  106.  
  107. def my_plv(n_freqs, Rxy, Rxy_mean):
  108. ''' Computes PLV. '''
  109. Rxy_plv = np.zeros((n_epochs, n_freqs), dtype=np.complex)
  110. for i in range(0, n_epochs):
  111. Rxy_plv[i] = Rxy[i] / np.abs(Rxy[i])
  112.  
  113. plv = np.abs(np.mean(Rxy_plv, axis=0))
  114. return plv
  115.  
  116. def my_pli(n_freqs, Rxy, Rxy_mean):
  117. ''' Computes PLI. '''
  118. Rxy_pli = np.zeros((n_epochs, n_freqs), dtype=np.complex)
  119. for i in range(0, n_epochs):
  120. Rxy_pli[i] = np.sign(np.imag(Rxy[i]))
  121.  
  122. pli = np.abs(np.mean(Rxy_pli, axis=0))
  123. return pli
  124.  
  125. def my_wpli(n_freqs, Rxy, Rxy_mean):
  126. ''' Computes WPLI. '''
  127. Rxy_wpli_1 = np.zeros((n_epochs, n_freqs), dtype=complex)
  128. Rxy_wpli_2 = np.zeros((n_epochs, n_freqs), dtype=complex)
  129. for i in range(0, n_epochs):
  130. Rxy_wpli_1[i] = np.imag(Rxy[i])
  131. Rxy_wpli_2[i] = np.abs(np.imag(Rxy[i]))
  132.  
  133. # handle divide by zero
  134. denom = np.mean(Rxy_wpli_2, axis=0)
  135. idx_denom = np.where(denom == 0.)
  136. denom[idx_denom] = 1.
  137. wpli = np.abs(np.mean(Rxy_wpli_1, axis=0)) / denom
  138. wpli[idx_denom] = 0.
  139. return wpli
  140.  
  141.  
  142. def my_con(x, y, n_epochs, nfft, sfreq, con_name='coh'):
  143. '''Computes connectivity measure mentioned on provided signal pair and its surrogates.'''
  144. n_freqs = nfft/2 + 1
  145. freqs, Rxy, Rxy_mean, Rxx_mean, Ryy_mean = compute_mean_psd_csd(x, y, n_epochs, nfft, sfreq)
  146.  
  147. # compute surrogates
  148. x_surr = x.copy()
  149. y_surr = y.copy()
  150. np.random.shuffle(x_surr)
  151. np.random.shuffle(y_surr)
  152. freqs_surro, Rxy_s, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean = compute_mean_psd_csd(x_surr, y_surr, n_epochs, nfft, sfreq)
  153.  
  154. m = {
  155. 'coh': lambda: (my_coherence(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean), my_coherence(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean)),
  156. 'imcoh': lambda: (my_imcoh(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean), my_imcoh(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean)),
  157. 'cohy': lambda: (my_cohy(n_freqs, Rxy_mean, Rxx_mean, Ryy_mean), my_cohy(n_freqs, Rxy_s_mean, Rxx_s_mean, Ryy_s_mean)),
  158. 'plv': lambda: (my_plv(n_freqs, Rxy, Rxy_mean), my_plv(n_freqs, Rxy_s, Rxy_s_mean)),
  159. 'pli': lambda: (my_pli(n_freqs, Rxy, Rxy_mean), my_pli(n_freqs, Rxy_s, Rxy_s_mean)),
  160. 'wpli': lambda: (my_wpli(n_freqs, Rxy, Rxy_mean), my_wpli(n_freqs, Rxy_s, Rxy_s_mean)),
  161. }
  162. return m[con_name]() + (freqs, freqs_surro)
  163.  
  164. con, con_surro, freqs, freqs_surro = my_con(x, y, n_epochs, nfft, sfreq, con_name)
  165.  
  166. #----------------
  167. # plot results
  168. fig = pl.figure('Connectivity')
  169. ax = fig.add_subplot(2, 1, 1)
  170. pl.plot(freqs, con)
  171. #pl.plot(freqs_surro, con_surro)
  172. pl.title("Connectivity Method is %s" %(con_name), color="black", fontsize=20)
  173. pl.xlabel("Frequency [ Hz ]", color="black", fontsize=14)
  174. pl.ylabel("Connectivity Value [ ]", color="black", fontsize=14)
  175. pl.ylim(0,1.1)
  176. pl.grid()
  177. pl.legend(['%s'%con_name,'ImCoh','PLV','PLI','WPLI'])
  178. #-----------------
  179. ax = fig.add_subplot(2, 1, 2)
  180. ax = pl.subplot(2, 1, 2, polar=True)
  181. pl.title("Phase Shift for mu=%srad and kappa= %s" %(phase_mu, phase_kappa), color="black", fontsize=16)
  182. radii = np.ones((n_epochs))
  183. pl.grid()
  184. bars = ax.bar(phase_shift_y, radii, bottom=0., width=0.001)
  185. ax.set_yticklabels([])
  186. pl.grid()
  187. #----------------
  188.  
  189. pl.ion()
  190. pl.show()
  191. pl.tight_layout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement