CalibrationRec2_ExportDatFile.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sat Oct 13 23:47:41 2018
  4. @author: aemdlabs
  5. """
  6. # -*- coding: utf-8 -*-
  7. """
  8. Created on Thu Sep 27 08:36:30 2018
  9. @author: aemdlabs
  10. """
  11. from PhyREC.NeoInterface import NeoSegment#, ReadMCSFile
  12. import PhyREC.PlotWaves as Rplt
  13. import quantities as pq
  14. import matplotlib.pyplot as plt
  15. import numpy as np
  16. import neo
  17. import PhyREC.SignalProcess as RPro
  18. import deepdish as dd
  19. def ReadMCSFile(McsFile, OutSeg=None, SigNamePrefix=''):
  20. import McsPy.McsData as McsData
  21. Dat = McsData.RawData(McsFile)
  22. Rec = Dat.recordings[0]
  23. NSamps = Rec.duration
  24. if OutSeg is None:
  25. OutSeg = NeoSegment()
  26. for AnaStrn, AnaStr in Rec.analog_streams.iteritems():
  27. if len(AnaStr.channel_infos) == 1:
  28. continue
  29. for Chn, Chinfo in AnaStr.channel_infos.iteritems():
  30. print ('Analog Stream ', Chinfo.label, Chinfo.sampling_frequency)
  31. ChName = str(SigNamePrefix + Chinfo.label)
  32. print(ChName)
  33. Fs = Chinfo.sampling_frequency
  34. Var, Unit = AnaStr.get_channel_in_range(Chn, 0, NSamps)
  35. sig = neo.AnalogSignal(pq.Quantity(Var, Chinfo.info['Unit']),
  36. t_start=0*pq.s,
  37. sampling_rate=Fs.magnitude*pq.Hz,
  38. name=ChName)
  39. OutSeg.AddSignal(sig)
  40. return OutSeg
  41. MCSMapI={'SE1':'Ch03',
  42. 'SE2':'Ch05',
  43. 'SE3':'Ch01',
  44. 'SE4':'Ch02',
  45. 'SE5':'Ch22',
  46. 'SE6':'Ch06',
  47. 'SE7':'Ch16',
  48. 'SE8':'Ch37',
  49. 'SE9':'Ch20',
  50. 'SE10':'Ch10',
  51. 'SE11':'Ch24',
  52. 'SE12':'Ch08',
  53. 'SE13':'Ch14',
  54. 'SE14':'Ch04',
  55. 'SE15':'Ch18',
  56. 'SE16':'Ch33',
  57. 'SE17':'Ch34',
  58. 'SE18':'Ch60',
  59. 'SE19':'Ch38',
  60. 'SE20':'Ch64',
  61. 'SE21':'Ch40',
  62. 'SE22':'Ch56',
  63. 'SE23':'Ch42',
  64. 'SE24':'Ch70',
  65. 'SE25':'Ch66',
  66. 'SE26':'Ch65',
  67. 'SE27':'Ch68',
  68. 'SE28':'Ch67',
  69. 'SE29':'Ch55',
  70. 'SE30':'Ch62',
  71. 'SE31':'Ch58',
  72. 'SE32':'Ch69',
  73. 'ME1':'Ch57',
  74. 'ME2':'Ch61',
  75. 'ME3':'Ch53',
  76. 'ME4':'Ch63',
  77. 'ME5':'Ch52',
  78. 'ME6':'Ch41',
  79. 'ME7':'Ch49',
  80. 'ME8':'Ch51',
  81. 'ME9':'Ch46',
  82. 'ME10':'Ch45',
  83. 'ME11':'Ch44',
  84. 'ME12':'Ch39',
  85. 'ME13':'Ch54',
  86. 'ME14':'Ch43',
  87. 'ME15':'Ch50',
  88. 'ME16':'Ch47',
  89. 'ME17':'Ch32',
  90. 'ME18':'Ch27',
  91. 'ME19':'Ch30',
  92. 'ME20':'Ch29',
  93. 'ME21':'Ch28',
  94. 'ME22':'Ch25',
  95. 'ME23':'Ch26',
  96. 'ME24':'Ch07',
  97. 'ME25':'Ch21',
  98. 'ME26':'Ch11',
  99. 'ME27':'Ch17',
  100. 'ME28':'Ch15',
  101. 'ME29':'Ch13',
  102. 'ME30':'Ch31',
  103. 'ME31':'Ch19',
  104. 'ME32':'Ch09',
  105. 'ME0':'Ch12',
  106. 'SE33':'Ch59'}
  107. MCSMap = {v: k for k, v in MCSMapI.iteritems()}
  108. #Col, Row
  109. MCSMapFacingDown={'Ch58':(0,1),
  110. 'Ch57':(0,2),
  111. 'Ch56':(0,3),
  112. 'Ch55':(0,4),
  113. 'Ch54':(0,5),
  114. 'Ch53':(0,6),
  115. 'Ch52':(0,7),
  116. 'Ch51':(0,8),
  117. 'Ch50':(0,9),
  118. 'Ch49':(0,10),
  119. 'Ch60':(1,0),
  120. 'Ch61':(1,1),
  121. 'Ch62':(1,2),
  122. 'Ch63':(1,3),
  123. 'Ch64':(1,4),
  124. 'Ch65':(1,5),
  125. 'Ch43':(1,6),
  126. 'Ch44':(1,7),
  127. 'Ch45':(1,8),
  128. 'Ch46':(1,9),
  129. 'Ch47':(1,10),
  130. 'Ch70':(2,0),
  131. 'Ch69':(2,1),
  132. 'Ch68':(2,2),
  133. 'Ch67':(2,3),
  134. 'Ch66':(2,4),
  135. 'Ch42':(2,5),
  136. 'Ch41':(2,6),
  137. 'Ch40':(2,7),
  138. 'Ch39':(2,8),
  139. 'Ch38':(2,9),
  140. 'Ch37':(2,10),
  141. 'Ch01':(3,0),
  142. 'Ch02':(3,1),
  143. 'Ch03':(3,2),
  144. 'Ch04':(3,3),
  145. 'Ch05':(3,4),
  146. 'Ch06':(3,5),
  147. 'Ch30':(3,6),
  148. 'Ch31':(3,7),
  149. 'Ch32':(3,8),
  150. 'Ch33':(3,9),
  151. 'Ch34':(3,10),
  152. 'Ch11':(4,0),
  153. 'Ch10':(4,1),
  154. 'Ch09':(4,2),
  155. 'Ch08':(4,3),
  156. 'Ch07':(4,4),
  157. 'Ch29':(4,5),
  158. 'Ch28':(4,6),
  159. 'Ch27':(4,7),
  160. 'Ch26':(4,8),
  161. 'Ch25':(4,9),
  162. 'Ch24':(4,10),
  163. 'Ch12':(0,0),
  164. 'Ch59':(5,0),
  165. 'Ch13':(5,1),
  166. 'Ch14':(5,2),
  167. 'Ch15':(5,3),
  168. 'Ch16':(5,4),
  169. 'Ch17':(5,5),
  170. 'Ch18':(5,6),
  171. 'Ch19':(5,7),
  172. 'Ch20':(5,8),
  173. 'Ch21':(5,9),
  174. 'Ch22':(5,10)}
  175. if __name__ == '__main__':
  176. ColTrs = {}
  177. for i in range(6):
  178. ColTrs[i] = [None] * 11
  179. for k, v in MCSMapFacingDown.iteritems():
  180. ColTrs[v[0]][v[1]] = k
  181. ##### 3rd session
  182. InFileM = '../Raw Data/2019-07-31T18-00-05B12784O18-T2-Longterm-Rec2.h5'
  183. InFileS = '../Raw Data/2019-07-31T18-00-05B12784O18-T2-Longterm-Rec2_2.h5'
  184. CalFile = dd.io.load('../CalibrationFile/Cal-Gm20190731_033840.h5')
  185. DatFile = '../Matlab/2019-07-31T18-00-05B12784O18-T2-Longterm-Rec2'
  186. Bits = 16
  187. vgs = 0.25
  188. gmsign = -1
  189. Range = 20e-3
  190. ivgainDC = pq.A/(118.8*pq.V)
  191. ivgainAC = pq.A/(1188*pq.V)
  192. Fhigh = 100
  193. Flow = 0.001
  194. DCchs = ('ME5', 'ME7', 'ME29', 'ME31', 'SE5', 'SE7', 'SE29', 'SE31')
  195. ACchs = MCSMapFacingDown.keys()
  196. Enc = ['M1','M2','S1','S2']
  197. Rec = ReadMCSFile(InFileM,
  198. OutSeg=None,
  199. SigNamePrefix='M')
  200. Rec = ReadMCSFile(InFileS,
  201. OutSeg=Rec,
  202. SigNamePrefix='S')
  203. plt.close('all')
  204. #%% Plot AC ############################################################
  205. plt.close('all')
  206. FigSize = (25*0.394, 15*0.394)
  207. Twind = (0*pq.s, None)
  208. VgsInVivo = CalFile['Vgs']
  209. CalRec = NeoSegment()
  210. tstop = np.array([])
  211. for sig in Rec.Signals():
  212. tstop = np.append(tstop,sig.t_stop)
  213. Twind = [0*pq.s, np.min(tstop)*pq.s]
  214. for isig, sig in enumerate(Rec.Signals()):
  215. if isig == 0:
  216. sig01 = sig.duplicate_with_new_array(np.zeros(len(sig.GetSignal(Time = Twind))))
  217. sig01.name = 'ME0'
  218. CalRec.AddSignal(sig01)
  219. if isig == 54:
  220. sig70 = sig.duplicate_with_new_array(np.zeros(len(sig.GetSignal(Time = Twind))))
  221. sig70.name = 'SE33'
  222. CalRec.AddSignal(sig70)
  223. if sig.name in Enc:
  224. continue
  225. gm = gmsign * np.interp(vgs,VgsInVivo,CalFile['GM'][sig.name])*pq.A/pq.V
  226. if CalFile['ChKo'][sig.name]==True:
  227. gm = 1.0*pq.A/pq.V
  228. SigProAC = [{'function': RPro.Gain, 'args': {'Gain': ivgainAC}},
  229. {'function': RPro.Gain, 'args': {'Gain': 1/gm}},
  230. {'function': RPro.RemoveDC, 'args': {}},
  231. ]
  232. SigProDC = [{'function': RPro.Gain, 'args': {'Gain': ivgainDC}},
  233. {'function': RPro.Gain, 'args': {'Gain': 1/gm}},
  234. {'function': RPro.SetZero, 'args': {'TWind': (None, 20*pq.s)}},
  235. {'function': RPro.Filter, 'args': {'Type':'highpass',
  236. 'Order':2,
  237. 'Freqs':(Flow,)}}
  238. ]
  239. sig = Rec.GetSignal(sig.name)
  240. if sig.name in DCchs:
  241. sig.ProcessChain = SigProDC
  242. else:
  243. sig.ProcessChain = SigProAC
  244. sig = sig.GetSignal(Time = Twind)
  245. CalRec.AddSignal(sig)
  246. ChNames = {val:key for (key, val) in CalRec.SigNames.items()}
  247. SigProEnc = [{'function': RPro.Gain, 'args': {'Gain': 1.0/50}},
  248. ]
  249. M1sig = Rec.GetSignal('M1')
  250. M2sig = Rec.GetSignal('M2')
  251. M1sig.ProcessChain = SigProEnc
  252. M2sig.ProcessChain = SigProEnc
  253. M1sig = M1sig.GetSignal(Time = Twind)
  254. M2sig = M2sig.GetSignal(Time = Twind)
  255. CalRec.AddSignal(M1sig)
  256. CalRec.AddSignal(M2sig)
  257. ChNames[66] = 'M1'
  258. ChNames[67] = 'M2'
  259. SortMap = np.ndarray([7, 11], dtype=object)
  260. for key, val in MCSMapFacingDown.items():
  261. SortMap[val[0], val[1]] = MCSMap[key]
  262. SortMap[6,0] ='M1'
  263. SortMap[6,1] ='M2'
  264. NeuroScopeMap = []
  265. for col in SortMap:
  266. ColName = []
  267. ColCh = []
  268. for ch in col:
  269. if ch ==None:
  270. continue
  271. ColName.append(ch)
  272. ColCh.append(CalRec.SigNames[ch])
  273. NeuroScopeMap.append((ColName, ColCh))
  274. ExpDat = CalRec.ExportNeuroscope(DatFile, Range, Bits,'V', ChNames, NeuroScopeMap)
  275. print('File exported to Neuroscope format, see "Cal Data" folder')