Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

lfp_analysis2.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. """
  2. description: offline analysis of kiap data
  3. author: Ioannis Vlachos
  4. date: 16.04.2019
  5. Copyright (c) 2019 Ioannis Vlachos.
  6. All rights reserved."""
  7. import re
  8. import os
  9. import glob
  10. import importlib
  11. import urllib
  12. import time
  13. import numpy as np
  14. import matplotlib.pyplot as plt
  15. import matplotlib as mpl
  16. from scipy import signal
  17. from scipy import stats
  18. # from cachetools import cached, LRUCache, TTLCache
  19. from neo.io.blackrockio import BlackrockIO
  20. import aux
  21. from analytics import analytics1
  22. importlib.reload(analytics1)
  23. # @cached(cache={})
  24. def read_data():
  25. '''returns first array1 and then array2 channels'''
  26. data = reader.get_analogsignal_chunk(i_stop=eval(str(params.lfp.i_stop)),
  27. channel_indexes=params.lfp.array1 + params.lfp.array2)
  28. return data
  29. params = aux.load_config()
  30. ch_nr1 = len(params.lfp.array1)
  31. ch_nr2 = len(params.lfp.array2)
  32. psth_win = params.lfp.psth_win
  33. # file_name = '/home/vlachos/data_kiap/20190321-135311/20190321-135311-001'
  34. path = '/kiap/data/neural/k01/20190414-123429/'
  35. file_name0 = '20190414-123429-001.ns2'
  36. file_name = path + file_name0
  37. plt.ioff()
  38. # if not 'data0' in locals():
  39. for fid in range(1, 76):
  40. # for fid in range(62,63):
  41. file_name0 = f'20190414-123429-{fid:03d}.ns2'
  42. file_name = path + file_name0
  43. print(file_name)
  44. reader = BlackrockIO(filename=file_name)
  45. data0 = read_data() # (samples, channels)
  46. data0 = data0.copy().astype(np.float64)
  47. # trials_msk = get_valid_trials(triggers_no_ns2, triggers_yes_ns2)
  48. array1 = params.lfp.array1
  49. array2 = params.lfp.array2
  50. # data1[data1 > params.lfp.artifact_thr] = 0
  51. # data1[data1 < -params.lfp.artifact_thr] = 0
  52. # data0 = data0 - np.mean(data0, 1)[:, np.newaxis]
  53. # reader.nev_data['Comments'][0]
  54. data0 = stats.zscore(data0)
  55. data01 = data0[:, :len(array1)]
  56. data02 = data0[:, len(array1):len(array1) + len(array2)]
  57. data1 = np.zeros((data01.shape[0], data01.shape[1], 3)) # (samples, channels, 3)
  58. data2 = np.zeros((data02.shape[0], data02.shape[1], 3)) # (samples, channels, 3)
  59. # Y1 = np.zeros((data01.shape[0], data01.shape[1], 3))
  60. # data1[:, :, 0], _, _ = analytics1.bw_filter(data01.T, params.lfp.filter_fc_lb, params.lfp.fs, params.lfp.filter_order_lb, plot=params.lfp.plot.filters)
  61. # data1[:, :, 1], _, _ = analytics1.bw_filter(data01.T, params.lfp.filter_fc_mb, params.lfp.fs, params.lfp.filter_order_mb, plot=params.lfp.plot.filters)
  62. # data1[:, :, 2], _, _ = analytics1.bw_filter(data01.T, params.lfp.filter_fc_hb, params.lfp.fs, params.lfp.filter_order_hb, plot=params.lfp.plot.filters)
  63. # data2[:, :, 0], _, _ = analytics1.bw_filter(data02.T, params.lfp.filter_fc_lb, params.lfp.fs, params.lfp.filter_order_lb, plot=params.lfp.plot.filters)
  64. # data2[:, :, 1], _, _ = analytics1.bw_filter(data02.T, params.lfp.filter_fc_mb, params.lfp.fs, params.lfp.filter_order_mb, plot=params.lfp.plot.filters)
  65. # data2[:, :, 2], _, _ = analytics1.bw_filter(data02.T, params.lfp.filter_fc_hb, params.lfp.fs, params.lfp.filter_order_hb, plot=params.lfp.plot.filters)
  66. # Y01, ff01 = analytics1.calc_fft(data01, params.lfp.fs, i_stop=1e5, axis=0)
  67. # Y02, ff02 = analytics1.calc_fft(data02, params.lfp.fs, i_stop=1e5, axis=0)
  68. # Y1, ff1 = analytics1.calc_fft(data1, params.lfp.fs, i_stop=1e5, axis=0)
  69. # Y2, ff2 = analytics1.calc_fft(data2, params.lfp.fs, i_stop=1e5, axis=0)
  70. # SPECTROGRAMS
  71. # ----------------------------------
  72. nperseg = 20000
  73. ff, tt, Sxx1 = signal.spectrogram(data01, params.lfp.fs, axis=0, nperseg=nperseg, noverlap=int(nperseg * 0.5))
  74. ff, tt, Sxx2 = signal.spectrogram(data02, params.lfp.fs, axis=0, nperseg=nperseg, noverlap=int(nperseg * 0.5))
  75. f_idx = (ff > 0) & (ff < 200)
  76. fig1 = plt.figure(1, figsize=(14, 10))
  77. plt.clf()
  78. for ii in range(array1.__len__()):
  79. # plt.plot()
  80. ax = plt.subplot(8, 8, ii+1)
  81. ax.title.set_text(reader.header['signal_channels'][array1[ii] + 1][0])
  82. # plt.plot(ff01[fidx], Y01[fidx])
  83. plt.pcolormesh(tt, ff[f_idx], np.log10(Sxx1[f_idx, ii, :]))
  84. # plt.pcolormesh(tt, ff[f_idx], np.log10(Sxx2[f_idx, ii, :]))
  85. # plt.pcolormesh(tt, ff[f_idx], np.log10(Sxx1[f_idx, ii, :]/Sxx1[f_idx,:,:].mean(1)))
  86. print('plotting array1 channel: ', ii)
  87. if ii < 56:
  88. ax.set_xticks([])
  89. if np.mod(ii, 8) != 0:
  90. ax.set_yticks([])
  91. fname1 = path + os.path.splitext(file_name0)[0] + '-arr1.png'
  92. fig1.savefig(fname1)
  93. print(fname1, 'Saved')
  94. fig2 = plt.figure(1, figsize=(14, 10))
  95. plt.clf()
  96. for ii in range(array2.__len__()):
  97. # plt.plot()
  98. ax = plt.subplot(8, 8, ii+1)
  99. ax.title.set_text(reader.header['signal_channels'][array2[ii] + 1][0])
  100. # plt.plot(ff01[fidx], Y01[fidx])
  101. # plt.pcolormesh(tt, ff[f_idx], np.log10(Sxx1[f_idx, ii, :]))
  102. plt.pcolormesh(tt, ff[f_idx], np.log10(Sxx2[f_idx, ii, :]))
  103. # plt.pcolormesh(tt, ff[f_idx], np.log10(Sxx1[f_idx, ii, :]/Sxx1[f_idx,:,:].mean(1)))
  104. print('plotting array2 channel: ', ii)
  105. if ii < 56:
  106. ax.set_xticks([])
  107. if np.mod(ii, 8) != 0:
  108. ax.set_yticks([])
  109. fname2 = path + os.path.splitext(file_name0)[0]+'-arr2.png'
  110. fig2.savefig(fname2)
  111. print(fname2, 'Saved')
  112. print('lfp_analysis2 script finished!')
  113. # plt.show()