Browse Source

'script1_line-length_analysis.py' ändern

Piret Kleis 2 years ago
parent
commit
e66b38be7f
1 changed files with 77 additions and 77 deletions
  1. 77 77
      Linelength_analysis_Fig1.py

+ 77 - 77
Linelength_analysis_Fig1.py

@@ -1,78 +1,78 @@
-# -*- coding: utf-8 -*-
-"""
-Created on Wed Oct  9 17:29:15 2019
-
-@author: kleisp
-"""
-
-import os
-os.chdir('C:\EAfiles\EAdetection')
-
-import core.ea_management as eam
-from core.helpers import open_hdf5
-from glob import glob
-#import phases_ictalInter as pii
-#import core.ea_analysis as eana
-
-import pandas as pd
-import numpy as np
-
-
-os.chdir('C:\EAfiles\EAdetection')
-reload(eam)
-
-
-#%% function for getting line length directly from resampled raw data
-
-def get_linelength(filename,ll_dict_controls, recID): 
-  excelpath = 'C:\EAfiles\Excel\\startstop_PK_HCc.xlsx' #insert the directory for an excel file containing the start and stop times for each recording (first column: recID, second column: start time, third column stop time)
-  df = pd.read_excel(excelpath) #reading an excel file with start and stop times (s) of a recording
-  df.set_index('recID', inplace=True) #recID
-  ll_dict_controls[recID]['start'] = int(df.start[recID]) #start time
-  ll_dict_controls[recID]['stop'] = int(df.stop[recID]) #stop time
-  sr=500 #sampling rate of downsampled data
-  data_dict=open_hdf5(filename,group=None,read_maskedarr=False)
-  data=data_dict['data']['trace']
-  reccut=data[int(ll_dict_controls[recID]['start']*sr):int(ll_dict_controls[recID]['stop']*sr)] #cut data according to start and stop times
-  N_datapoints=len(reccut) 
-  duration=N_datapoints/sr
-  linelength = np.sum(np.abs(np.diff(reccut))) #line length is the sum of subsequent datapoints -->derivative
-  linelength_s=linelength/duration
-  ll_dict_controls[recID]['linelength/second']=linelength_s
-  ll_dict_controls[recID]['linelength']=linelength
-  
-#%% get linelength directly from h5 (downsampled data)
-
-os.chdir(r'C:\EAfiles\DATA\h5files') #enter directory, where all the needed h5 files are
-files = glob(r'C:\EAfiles\DATA\h5files\*.h5') #takes all h5 files from this folder
-hfile = files
-lines = np.arange(0, len(hfile)) #as many lines as there are h5 files
-ll_dict_controls={}
-ll_dict_controls_d={}
-
-for i in lines:
-    recID = os.path.basename(hfile[i]).split('__')[0] #getting the IDs
-    print (i)
-    print (recID)
-    
-    filename=os.path.basename(hfile[i])
-    ll_dict_controls[recID]= {}
-    get_linelength(filename,ll_dict_controls, recID)
-
-#%% creating arrays with data 
-recIDs = np.array(ll_dict_controls.keys())
-
-flavour = 'linelength/second'
-ii_linelengths = np.array([ll_dict_controls[recID][flavour] for recID in ll_dict_controls.keys()])
-
-#%% create excel file
-
-os.chdir('C:\EAfiles\RESULTS')
-
-df_sr=pd.DataFrame(columns=
-    ['recID', 
-    'linelength/s'])
-
-df_sr['recID'] = recIDs
-df_sr['linelength/s'] = ii_linelengths
+# -*- coding: utf-8 -*-
+"""
+Created on Wed Oct  9 17:29:15 2019
+
+@author: kleisp
+"""
+
+import os
+os.chdir('C:\EAfiles\EAdetection')
+
+import core.ea_management as eam
+from core.helpers import open_hdf5
+from glob import glob
+#import phases_ictalInter as pii
+#import core.ea_analysis as eana
+
+import pandas as pd
+import numpy as np
+
+
+os.chdir('C:\EAfiles\EAdetection')
+reload(eam)
+
+
+#%% function for getting line length directly from resampled raw data
+
+def get_linelength(filename,ll_dict_controls, recID): 
+  excelpath = 'C:\EAfiles\Excel\\startstop_PK_HCc.xlsx' #insert the directory for an excel file containing the start and stop times for each recording (first column: recID, second column: start time, third column stop time)
+  df = pd.read_excel(excelpath) #reading an excel file with start and stop times (s) of a recording
+  df.set_index('recID', inplace=True) #recID
+  ll_dict_controls[recID]['start'] = int(df.start[recID]) #start time
+  ll_dict_controls[recID]['stop'] = int(df.stop[recID]) #stop time
+  sr=500 #sampling rate of downsampled data
+  data_dict=open_hdf5(filename,group=None,read_maskedarr=False)
+  data=data_dict['data']['trace']
+  reccut=data[int(ll_dict_controls[recID]['start']*sr):int(ll_dict_controls[recID]['stop']*sr)] #cut data according to start and stop times
+  N_datapoints=len(reccut) 
+  duration=N_datapoints/sr
+  linelength = np.sum(np.abs(np.diff(reccut))) #line length is the sum of subsequent datapoints -->derivative
+  linelength_s=linelength/duration
+  ll_dict_controls[recID]['linelength/second']=linelength_s
+  ll_dict_controls[recID]['linelength']=linelength
+  
+#%% get linelength directly from h5 (downsampled data)
+
+os.chdir(r'C:\EAfiles\DATA\h5files') #enter directory, where all the needed h5 files are
+files = glob(r'C:\EAfiles\DATA\h5files\*.h5') #takes all h5 files from this folder
+hfile = files
+lines = np.arange(0, len(hfile)) #as many lines as there are h5 files
+ll_dict_controls={}
+ll_dict_controls_d={}
+
+for i in lines:
+    recID = os.path.basename(hfile[i]).split('__')[0] #getting the IDs
+    print (i)
+    print (recID)
+    
+    filename=os.path.basename(hfile[i])
+    ll_dict_controls[recID]= {}
+    get_linelength(filename,ll_dict_controls, recID)
+
+#%% creating arrays with data 
+recIDs = np.array(ll_dict_controls.keys())
+
+flavour = 'linelength/second'
+ii_linelengths = np.array([ll_dict_controls[recID][flavour] for recID in ll_dict_controls.keys()])
+
+#%% create excel file
+
+os.chdir('C:\EAfiles\RESULTS')
+
+df_sr=pd.DataFrame(columns=
+    ['recID', 
+    'linelength/s'])
+
+df_sr['recID'] = recIDs
+df_sr['linelength/s'] = ii_linelengths
 df_sr.to_excel('test_linelengths.xlsx') #name the file