Guest User

Untitled

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