PhaseAmplitude_Rec2.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Jul 5 09:21:49 2019
  4. @author: rgarcia1
  5. """
  6. from PhyREC.NeoInterface import NeoSegment, NeoSignal#, ReadMCSFile
  7. import PhyREC.SignalAnalysis as Ran
  8. import PhyREC.PlotWaves as Rplt
  9. import PhyREC.SignalAnalysis as Ran
  10. import quantities as pq
  11. import matplotlib.pyplot as plt
  12. import numpy as np
  13. import neo
  14. import PhyREC.SignalProcess as RPro
  15. import deepdish as dd
  16. import csv
  17. from datetime import datetime
  18. import os
  19. from scipy import integrate
  20. import deepdish as dd
  21. from matplotlib.pyplot import cohere
  22. from scipy.signal import hilbert
  23. from matplotlib.colors import LogNorm
  24. import matplotlib.colors as mcolors
  25. from matplotlib.pyplot import cohere
  26. import PyGFET.AnalyzeData as FETana
  27. import PyGFET.PlotDataClass as FETplt
  28. def ReadMCSFile(McsFile, Include, OutSeg=None, SigNamePrefix=''):
  29. import McsPy.McsData as McsData
  30. Dat = McsData.RawData(McsFile)
  31. Rec = Dat.recordings[0]
  32. NSamps = Rec.duration
  33. if OutSeg is None:
  34. OutSeg = NeoSegment()
  35. for AnaStrn, AnaStr in Rec.analog_streams.iteritems():
  36. if len(AnaStr.channel_infos) == 1:
  37. continue
  38. for Chn, Chinfo in AnaStr.channel_infos.iteritems():
  39. print 'Analog Stream ', Chinfo.label, Chinfo.sampling_frequency
  40. ChName = str(SigNamePrefix + Chinfo.label)
  41. print ChName
  42. if ChName not in Include:
  43. continue
  44. Fs = Chinfo.sampling_frequency
  45. Var, Unit = AnaStr.get_channel_in_range(Chn, 0, NSamps)
  46. sig = neo.AnalogSignal(pq.Quantity(Var, Chinfo.info['Unit']),
  47. t_start=0*pq.s,
  48. sampling_rate=Fs.magnitude*pq.Hz,
  49. name=ChName)
  50. OutSeg.AddSignal(sig)
  51. return OutSeg
  52. def MeanStd(Data):
  53. Arr = np.zeros([len(Data.keys()),len(Data[Data.keys()[0]]['psd'])])
  54. for iT,TrtName in enumerate(Data.keys()):
  55. Arr[iT,:] = Data[TrtName]['psd'][:,0]
  56. return np.mean(Arr,0), np.std(Arr,0)
  57. Path = '../Raw Data/'
  58. InFileM = Path + '2019-07-31T18-00-05B12784O18-T2-Longterm-Rec2.h5' ############
  59. InFileS = Path + '2019-07-31T18-00-05B12784O18-T2-Longterm-Rec2_2.h5' ##########
  60. Gm = dd.io.load('../CalibrationFile/Cal-Gm20190731_033840.h5')
  61. BW = 100
  62. ivgainDC = 118.8*pq.V
  63. ivgainAC = 1188*pq.V
  64. TWind = (12850*pq.s,15450*pq.s)
  65. DCch = ('SE7','ME31',)
  66. Include = DCch
  67. color = ['r','orange','g','b']
  68. Rec = ReadMCSFile(InFileM,
  69. Include,
  70. OutSeg=None,
  71. SigNamePrefix='M')
  72. Rec = ReadMCSFile(InFileS,
  73. Include,
  74. OutSeg=Rec,
  75. SigNamePrefix='S')
  76. #%% Plot Sig
  77. plt.close('all')
  78. TWind = (None,20800*pq.s)
  79. Slots= []
  80. SlotsComp = []
  81. counter = -1
  82. for sig in Rec.Signals():
  83. SigProDC = [{'function': RPro.Gain, 'args': {'Gain': pq.A/(Gm['GM'][sig.name][8]*(pq.A/pq.V)*ivgainDC)}},
  84. {'function': RPro.SetZero, 'args' : {'TWind' :TWind}},
  85. # {'function': RPro.DownSampling, 'args' : {'Fact' :10}},
  86. {'function': RPro.Filter, 'args': {'Type':'bandpass',
  87. 'Order':2,
  88. 'Freqs':(0.005,0.05)}},
  89. ]
  90. SigProRMS = [{'function':RPro.sliding_window, 'args': {'func':RPro.rms,'timewidth': 300*pq.s}}]
  91. if sig.name not in DCch:
  92. continue
  93. counter +=1
  94. sig.ProcessChain = SigProDC
  95. sig = sig.GetSignal(TWind)
  96. sigRMS = sig.GetSignal(TWind)
  97. sigRMS.ProcessChain = SigProRMS
  98. sigRMS = sigRMS.GetSignal(TWind)
  99. if counter==0:
  100. sig0 = sig.GetSignal(TWind)
  101. else:
  102. sig1 = sig.GetSignal(TWind)
  103. Slots.append(Rplt.WaveSlot(sig,
  104. Position=0,
  105. Alpha=0.5))
  106. SlotsComp.append(Rplt.WaveSlot(sig,
  107. Position=0,
  108. Alpha=0.5))
  109. Splots = Rplt.PlotSlots(Slots)
  110. Splots.PlotChannels(Time=TWind,
  111. Units='V')
  112. HT0 = hilbert(np.array(sig0).T[0,:])
  113. HT1= hilbert(np.array(sig1).T[0,:])
  114. fig, ax = plt.subplots()
  115. phaseShift = np.angle(HT1,deg=True)-np.angle(HT0,deg=True)
  116. for iSamp, samp in enumerate(phaseShift):
  117. if samp <= -180:
  118. phaseShift[iSamp] = samp+360
  119. if samp >=180:
  120. phaseShift[iSamp] = samp-360
  121. h = plt.hist2d(phaseShift, np.log10(np.abs(HT0)), bins=50, normed = True, range= [[np.min(phaseShift), np.max(phaseShift)], [-6, -2.5]],vmin=0, vmax=0.006,cmap='jet')#norm = LogNorm())#
  122. plt.colorbar(h[3])