linux goes windows

This commit is contained in:
Lisa 2019-10-23 15:53:39 +02:00
parent fd558b213c
commit a483f40056
4 changed files with 556 additions and 598 deletions

View File

@ -1,12 +1,18 @@
import nixio as nix import nixio as nix
from IPython import embed
import numpy as np import numpy as np
import os import os
import pandas as pd import pandas as pd
import pickle import pickle
import helperfunctions as hf
from copy import deepcopy from copy import deepcopy
import gc
'''
This File is meant to convert .nix Files into a more readable format (pandas.DataFrame)
NixToFrame(<folder>) searches all folders in <folder> for .nix files and converts them with
DataFrame(<nixfile>) and saves them as picklefile
df = load_data(<filename>) can load these picklefiles
'''
def DataFrame(nixfile, before=0.001, after=0.001, savefile=False, saveto='./', mindepth=0, delimiter=';'): def DataFrame(nixfile, before=0.001, after=0.001, savefile=False, saveto='./', mindepth=0, delimiter=';'):
@ -14,9 +20,13 @@ def DataFrame(nixfile, before=0.001, after=0.001, savefile=False, saveto='./', m
opens a nix file, extracts the data and converts it to a pandas.DataFrame opens a nix file, extracts the data and converts it to a pandas.DataFrame
:param nixfile (string): path and name of .nix file :param nixfile (string): path and name of .nix file
:param overlap (float): time before and after sweeps that is saved, too :param before (float): time before sweeps that is saved, too (in the unit that is given in the file. Usually [s])
:param savefile (string): if not False, the dataframe will be saved as <savefile>.pickle :param after (float): time after sweeps that is saved, too (in the unit that is given in the file. Usually [s])
:param saveto (string): path to save the files in NOT IMPLEMENTED YET :param savefile (string, bool): if not False and a string, the dataframe will be saved as <savefile>.pickle, if True it will be saved as nixfile.split('.nix')[0]
:param saveto (string): path to save the files in path defined by saveto
:param mindepth (int): minimum number of nested entries in dataframe
:param delimiter (string): internally used to create the nested dataframe, use a delimiter that does not naturally occur in your protocol names
:return dataframe (pandas.DataFrame): pandas.DataFrame with available nix data :return dataframe (pandas.DataFrame): pandas.DataFrame with available nix data
''' '''
@ -59,13 +69,19 @@ def DataFrame(nixfile, before=0.001, after=0.001, savefile=False, saveto='./', m
try: try:
meta = tag[i].metadata meta = tag[i].metadata
tag_metadata[meta.id] = GetMetadataDict(meta) tag_metadata[meta.id] = GetMetadataDict(meta)
# tag_metadata[meta.id].update(DicCrawl(GetMetadataDict(meta), mindepth=mindepth))
tag_id_times[meta.id] = [tag[i].position[0], tag[i].position[0]+tag[i].extent[0]] tag_id_times[meta.id] = [tag[i].position[0], tag[i].position[0]+tag[i].extent[0]]
except: except:
print(nixfile.split('/')[-1] + ' has no tags, no file will be saved') print(nixfile.split('/')[-1] + ' has no tags, no file will be saved')
return None return None
protocol_idcs = np.where([(' onset times' in name) for name in names])[0] protocol_idcs_old = np.where([(' onset times' in name) for name in names])[0]
durations_idcs_old = np.where([(' durations' in name) for name in names])[0]
for idx in protocol_idcs_old:
names[idx] = names[idx].split(' onset times')[0] + '_onset_times'
for idx in durations_idcs_old:
names[idx] = names[idx].split(' durations')[0] + '_durations'
protocol_idcs = np.where([('_onset_times' in name) for name in names])[0]
if len(protocol_idcs) == 0: if len(protocol_idcs) == 0:
print(nixfile.split('/')[-1] + ' is empty, no file will be saved') print(nixfile.split('/')[-1] + ' is empty, no file will be saved')
@ -108,31 +124,37 @@ def DataFrame(nixfile, before=0.001, after=0.001, savefile=False, saveto='./', m
keys = np.unique(k) keys = np.unique(k)
if 'repro_tag_id' not in keys: if 'repro_tag_id' not in keys:
for i in range(len(data_df)): for i in range(len(data_df)):
data_df[i]['repro_tag_id'] = protocols[~(protocols.position >= data_df[i]['onset times'])]['id'].iloc[-1] data_df[i]['repro_tag_id'] = protocols[~(protocols.position >= data_df[i]['onset_times'])]['id'].iloc[-1]
traces_idx = np.where([("data.sampled" in d.type) or ("data.events" in d.type) for d in data_arrays])[0] traces_idx = np.where([("data.sampled" in d.type) or ("data.events" in d.type) for d in data_arrays])[0]
traces_df = [] traces_df = []
for i,idx in enumerate(traces_idx): for i,idx in enumerate(traces_idx):
# print(i,i/len(traces_idx))
for j in range(len(data_df)): for j in range(len(data_df)):
if i == 0: if i == 0:
traces_df.append({}) traces_df.append({})
if "data.sampled" in data_arrays[names[int(idx)]].type: if "data.sampled" in data_arrays[names[int(idx)]].type:
idx0 = int((data_df[j]['onset times'] - before) / dt) idx0 = int((data_df[j]['onset_times'] - before) / dt)
idx1 = int((data_df[j]['onset times'] + data_df[j]['durations'] + after) / dt + 1) idx1 = int((data_df[j]['onset_times'] + data_df[j]['durations'] + after) / dt)
# traces_df[names[idx]] = data_arrays[int(idx)][:][idx0:idx1] if idx0>=idx1:
traces_df[j][names[idx]] = data_arrays[names[int(idx)]][idx0:idx1] traces_df[j][names[idx]] = np.array([np.nan])
else:
traces_df[j][names[idx]] = data_arrays[names[int(idx)]][idx0:idx1]
elif "data.events" in data_arrays[names[int(idx)]].type: elif "data.events" in data_arrays[names[int(idx)]].type:
t0 = data_df[j]['onset times'] - before idx0 = int((data_df[j]['onset_times'] - before) / dt)
t1 = data_df[j]['onset times'] + data_df[j]['durations'] + after idx1 = int((data_df[j]['onset_times'] + data_df[j]['durations'] + after) / dt)
arr = data_arrays[names[int(idx)]][:] t0 = data_df[j]['onset_times'] - before
traces_df[j][names[idx]] = arr[(arr>=t0) & (arr<=t1)] t1 = data_df[j]['onset_times'] + data_df[j]['durations'] + after
if t0>=t1:
traces_df[j][names[idx]] = np.array([np.nan])
else:
arr = data_arrays[names[int(idx)]][:]
traces_df[j][names[idx]] = arr[(arr>=t0) & (arr<=t1)] - data_df[j]['onset_times']
if i == 0: if i == 0:
traces_df[j]['time'] = time[idx0:idx1] - data_df[j]['onset times'] traces_df[j]['time'] = time[idx0:idx1] - data_df[j]['onset_times']
traces_df[j]['time_before_stimulus'] = before traces_df[j]['time_before_stimulus'] = before
traces_df[j]['time_after_stimulus'] = after traces_df[j]['time_after_stimulus'] = after
traces_df[j]['samplingrate'] = 1 / dt traces_df[j]['samplingrate'] = 1 / dt
@ -144,9 +166,7 @@ def DataFrame(nixfile, before=0.001, after=0.001, savefile=False, saveto='./', m
traces_df[j]['protocol_number'] = np.array(protocols[protocols.id == str(data_df[j]['repro_tag_id'])].name)[0].split('_')[1] traces_df[j]['protocol_number'] = np.array(protocols[protocols.id == str(data_df[j]['repro_tag_id'])].name)[0].split('_')[1]
traces_df[j]['id'] = data_df[j]['repro_tag_id'] traces_df[j]['id'] = data_df[j]['repro_tag_id']
metadic = {} metadic = {}
print('meta')
for i,key in enumerate(protocols.id): for i,key in enumerate(protocols.id):
d = GetMetadataDict(tag[key].metadata) d = GetMetadataDict(tag[key].metadata)
if (len(d.keys()) == 1) and ('RePro-Info' in list(d.keys())[0]): if (len(d.keys()) == 1) and ('RePro-Info' in list(d.keys())[0]):
@ -158,134 +178,8 @@ def DataFrame(nixfile, before=0.001, after=0.001, savefile=False, saveto='./', m
meta_df[i] = metadic[str(data_df[i]['repro_tag_id'])] meta_df[i] = metadic[str(data_df[i]['repro_tag_id'])]
dics = [meta_df, data_df, traces_df] dics = [meta_df, data_df, traces_df]
# old_maxcount = mindepth
# for i in range(len(protocol_idcs)):
# protocol = names[protocol_idcs[i]].split(' onset times')[0]
#
# #skip certain protocols
# if 'VC=' in protocol:
# # print('skip this protocol')
# continue
# #number of meta data entries
# if i == len(protocol_idcs)-1:
# meta_len = len(names) - protocol_idcs[i]
# else:
# meta_len = protocol_idcs[i+1] - protocol_idcs[i]
#
# #get new line for every sweep and save the data, make a pn subtraction if necessary
# if any([protocol + '_pn' == string for string in names[protocol_idcs[i]:protocol_idcs[i]+meta_len]]):
# pn = data_arrays[protocol + '_pn'][0]
# sweeps = np.arange(np.abs(pn),len(data_arrays[int(protocol_idcs[i])][:]),(np.abs(pn)+1), dtype=int)
# else:
# pn = np.nan
# sweeps = np.arange(len(data_arrays[int(protocol_idcs[i])][:]), dtype=int)
#
# for sweep in sweeps:
# stim_num +=1
# data.append({})
#
# # save protocol names
# split_vec = protocol.split('-')
# if len(split_vec)>2:
# prot_name = split_vec[0]
# prot_num = int(split_vec[-1])
# for j in range(len(split_vec)-2):
# prot_name += '-' + split_vec[j+1]
# else:
# prot_name = split_vec[0]
# prot_num = split_vec[-1]
# data[stim_num]['protocol'] = prot_name
# data[stim_num]['protocol_number'] = prot_num
# data[stim_num]['time_before_stimulus'] = before
# data[stim_num]['time_after_stimulus'] = after
#
# #save id
# data[stim_num]['id'] = data_arrays[int(protocol_idcs[i])].id
#
# #save rest of stored data
# for idx in range(meta_len):
# j = int(protocol_idcs[i] + idx)
# if (' durations' in names[j]) or (' onset times' in names[j]):
# continue
# if len(data_arrays[j][sweep]) == 1:
# data[stim_num][names[j].split(protocol + '_')[-1]] = data_arrays[j][sweep][0]
# else:
# data[stim_num][names[j].split(protocol+'_')[-1]] = data_arrays[j][sweep]
# data[stim_num]['samplingrate'] = 1/dt
#
# #save data arrays
# onset = data_arrays[protocol + ' onset times'][sweep]
# dur = data_arrays[protocol + ' durations'][sweep]
# t0 = int((onset-before)/dt)
# t1 = int((onset+after+dur)/dt+1)
# data[stim_num]['onset time'] = onset
# data[stim_num]['duration'] = dur
#
# for name,idx in data_names:
# data[stim_num][name] = data_traces[int(idx)][t0:t1]
#
# for j in np.arange(int(idx)+1,protocol_idcs[0]):
# bool_vec = (data_arrays[names[j]][:]>=onset) & (data_arrays[names[j]][:]<=onset+dur)
# data[stim_num][names[j]] = np.array(data_arrays[names[j]])[bool_vec]
#
# data[stim_num]['time'] = time[t0:t1] - data[stim_num]['onset time']
#
# #pn-subtraction (if necessary)
# '''
# change the location of the pn (its already in the metadata, you dont need it as option
# '''
# if pn != np.nan and np.abs(pn)>0:
# pn_curr = np.zeros(len(data[stim_num][name]))
# idx = np.where(data_names[:,0] == 'Current-1')[0][0]
# for j in range(int(np.abs(pn))):
# onset = data_arrays[protocol + ' onset times'][sweep-j-1]
# t0 = int((onset-before) / dt)
# t1 = int(t0 + len(pn_curr))
#
# pn_curr += data_traces[int(idx),t0:t1] - np.mean(data_traces[int(idx),t0:int(t0+before/dt)])
# import matplotlib.pyplot as plt
# plt.plot(data_traces[int(idx),t0:t1] - np.mean(data_traces[int(idx),t0:int(t0+before/dt)]))
#
# data[stim_num]['Current-2'] = data[stim_num]['Current-1'] - pn/np.abs(pn)*pn_curr - np.mean(data[stim_num]['Current-1'][0:int(before/dt)])
#
# for mt in block.tags:
# # if 'Trace' in mt.metadata.name:
# print(mt.name)
# # mt.metadata.pprint()
# embed()
#
# '''
# this one saves the complete metadata in EVERY line
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!THINK OF SOMETHING BETTER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# '''
#
# tag_id = None
# for key in tag_id_times.keys():
# if (data[stim_num]['onset time'] >= tag_id_times[key][0]) and (data[stim_num]['onset time'] <= tag_id_times[key][1]):
# tag_id = key
# # # save metadata
# # data[stim_num]['block_meta'] = block_metadata[list(block_metadata.keys())[0]]
# data[stim_num].update(DicCrawl(block_metadata[list(block_metadata.keys())[0]], key='block_meta', delimiter=delimiter))
#
# if tag_id != None:
# tagmetadata = tag_metadata[tag_id]
# if (len(tagmetadata)==1) & (list(tagmetadata.keys())[0] == 'RePro-Info'):
# # data[stim_num]['tag_meta'] = tag_metadata[tag_id]['RePro-Info']
# data[stim_num].update(DicCrawl(tag_metadata[tag_id]['RePro-Info'], key='tag_meta', delimiter=delimiter))
# else:
# # data[stim_num]['tag_meta'] = tag_metadata[tag_id]
# data[stim_num].update(DicCrawl(tag_metadata[tag_id], delimiter=delimiter))
#
# elif tag_id == None:
# print('no tag metadata for ' + nixfile.split('/')[-1] + ', ' + protocol)
#
# # add block id
# data[stim_num]['block_id'] = list(block_metadata.keys())[0]
# data[stim_num]['tag_id'] = tag_id
# add delimiters at end of keys to be able to correctly split dataframe
# count maximum depth of the keylist (count max. of delimiters in a row)
old_maxcount = mindepth old_maxcount = mindepth
for k in range(len(dics)): for k in range(len(dics)):
for j in range(len(dics[k])): for j in range(len(dics[k])):
keys = list(dics[k][j].keys()) keys = list(dics[k][j].keys())
@ -311,12 +205,11 @@ def DataFrame(nixfile, before=0.001, after=0.001, savefile=False, saveto='./', m
traces_df = pd.DataFrame(traces_df) traces_df = pd.DataFrame(traces_df)
meta_df = pd.DataFrame(meta_df) meta_df = pd.DataFrame(meta_df)
data = pd.concat([meta_df, data_df, traces_df], axis=1, sort=False) data = pd.concat([meta_df, data_df, traces_df], axis=1)
data.columns = data.columns.str.split(';', expand=True) data.columns = data.columns.str.split(';', expand=True)
if savefile != False: if savefile != False:
if savefile == True: if savefile == True:
# savefile = nixfile.split('/')[-1].split('.nix')[0]
savefile = nixfile.split('.nix')[0] savefile = nixfile.split('.nix')[0]
if saveto != None: if saveto != None:
savefile = savefile.split('/')[-1] savefile = savefile.split('/')[-1]
@ -351,6 +244,12 @@ def NixToFrame(folder, before=0.0, after=0.0, skipold=True, saveto=None, mindept
DataFrame(folder+dir+'/'+file, before, after, True, saveto, mindepth=mindepth) DataFrame(folder+dir+'/'+file, before, after, True, saveto, mindepth=mindepth)
def load_data(filename):
with open(filename, 'rb') as f:
data = pickle.load(f) # load data with pickle
return data
def GetMetadataDict(metadata): def GetMetadataDict(metadata):
def unpackMetadata(sec): def unpackMetadata(sec):
metadata = dict() metadata = dict()
@ -358,7 +257,6 @@ def GetMetadataDict(metadata):
if hasattr(sec, 'sections') and len(sec.sections) > 0: if hasattr(sec, 'sections') and len(sec.sections) > 0:
metadata.update({subsec.name: unpackMetadata(subsec) for subsec in sec.sections}) metadata.update({subsec.name: unpackMetadata(subsec) for subsec in sec.sections})
return metadata return metadata
return unpackMetadata(metadata) return unpackMetadata(metadata)
@ -391,15 +289,19 @@ def KeylistToDic(keylist):
return dic return dic
def load_data(filename):
with open(filename, 'rb') as f:
data = pickle.load(f) # load data with pickle
return data
def PNSubtraction(df, currenttrace='Current-1', newcurrenttrace='Current-2', kernelcurrenttrace='Current-3', delimiter=';'): def PNSubtraction(df, currenttrace='Current-1', newcurrenttrace='Current-2', kernelcurrenttrace='Current-3', delimiter=';'):
'''
Only for VoltageClamp experiments and WILL BE OBSOLETE, SOON
:param df (pandas.DataFrame): NixFrame.DataFrame
:param currenttrace:
:param newcurrenttrace:
:param kernelcurrenttrace:
:param delimiter:
:return:
'''
df = deepcopy(df) df = deepcopy(df)
embed()
if 'TraceId' not in df.columns: if 'TraceId' not in df.columns:
for id in np.unique(df.repro_tag_id): for id in np.unique(df.repro_tag_id):
dfp = df[df.repro_tag_id == id] dfp = df[df.repro_tag_id == id]
@ -432,7 +334,7 @@ def PNSubtraction(df, currenttrace='Current-1', newcurrenttrace='Current-2', ker
trace_id = df.TraceId.iloc[i] trace_id = df.TraceId.iloc[i]
idxvec = np.where((df.TraceId == trace_id) & (df.type == 'PNSubtraction'))[0] idxvec = np.where((df.TraceId == trace_id) & (df.type == 'PNSubtraction'))[0]
else: else:
delays = (df['onset times'][df.type == 'PNSubtraction']) - df['onset times'].iloc[i] delays = (df['onset_times'][df.type == 'PNSubtraction']) - df['onset_times'].iloc[i]
maxidx = delays[np.array(delays)<0].index[-1] maxidx = delays[np.array(delays)<0].index[-1]
idxvec = np.arange(maxidx-np.abs(df.pn.iloc[i])+1, maxidx+.1, dtype=int) idxvec = np.arange(maxidx-np.abs(df.pn.iloc[i])+1, maxidx+.1, dtype=int)
@ -464,44 +366,19 @@ def PNSubtraction(df, currenttrace='Current-1', newcurrenttrace='Current-2', ker
ftoutput = np.fft.fft(pn_traces[idx0:idx1 - 1]) ftoutput = np.fft.fft(pn_traces[idx0:idx1 - 1])
kernel = np.fft.ifft(ftoutput / ftinput) kernel = np.fft.ifft(ftoutput / ftinput)
kernel = np.append(kernel, np.zeros(len(I_trace) - len(kernel))+np.mean(kernel[-20:])) kernel = np.append(kernel, np.zeros(len(I_trace) - len(kernel))+np.mean(kernel[-20:]))
# kernel = kernel - np.mean(kernel[-20:])
# kernel2 = ttr*0.0
# kernel2[ttr>=0] = np.mean(kernel[-20:])
kerneldic[idx] = {} kerneldic[idx] = {}
kerneldic[idx][kernelcurrenttrace + affix] = I_trace - np.convolve(np.diff(Vtr), kernel)[:len(I_trace)] #- np.convolve(np.diff(Vtr),kernel2)[:len(I_trace)] kerneldic[idx][kernelcurrenttrace + affix] = I_trace - np.convolve(np.diff(Vtr), kernel)[:len(I_trace)] #- np.convolve(np.diff(Vtr),kernel2)[:len(I_trace)]
''' end of kernels ''' ''' end of kernels '''
if len(pn_trace) < len(I_trace): if len(pn_trace) < len(I_trace):
# print(len(pn_traces) - len(I_trace))
I_trace = I_trace[:len(pn_trace)] I_trace = I_trace[:len(pn_trace)]
elif len(pn_trace) > len(I_trace): elif len(pn_trace) > len(I_trace):
# print(len(pn_traces) - len(I_trace))
pn_trace = pn_trace[:len(I_trace)] pn_trace = pn_trace[:len(I_trace)]
currentdic[idx] = {} currentdic[idx] = {}
currentdic[idx][newcurrenttrace+affix] = I_trace - pn_trace currentdic[idx][newcurrenttrace+affix] = I_trace - pn_trace
if df.protocol.iloc[i] == 'Recovery':
import matplotlib.pyplot as plt
plt.plot(I_trace,'k', label='original')
plt.plot(kerneldic[idx][kernelcurrenttrace + affix], label='kernel')
plt.plot(currentdic[idx][newcurrenttrace+affix],label='pn')
plt.legend()
plt.show()
embed()
# import matplotlib.pyplot as plt
# if df.protocol.iloc[i] == 'Activation':
# plt.figure()
# plt.title(df.step.iloc[i])
# # plt.plot(hf.get_traces(currenttrace, df.iloc[idxvec]), 'k')
# # plt.plot(np.array(df[currenttrace].iloc[i]))
# plt.plot(pn_traces, 'k')
# plt.plot(I_trace)
# df[i][newcurrenttrace] = I_trace
# plt.show()
currentdf = pd.DataFrame(currentdic, index=df[df.type == 'Trace'].index) currentdf = pd.DataFrame(currentdic, index=df[df.type == 'Trace'].index)
currentdf.columns = currentdf.columns.str.split(';', expand=True) currentdf.columns = currentdf.columns.str.split(';', expand=True)
df = pd.concat([df, currentdf], axis=1) df = pd.concat([df, currentdf], axis=1)
@ -510,6 +387,10 @@ def PNSubtraction(df, currenttrace='Current-1', newcurrenttrace='Current-2', ker
def QualityControl(df, currenttrace='Current-1', potentialtrace='V-1', delimiter=';'): def QualityControl(df, currenttrace='Current-1', potentialtrace='V-1', delimiter=';'):
'''
only for VoltageClamp experiments
'''
qc = [[]]*len(df[df.type == 'QualityControl']) qc = [[]]*len(df[df.type == 'QualityControl'])
prefix = 'qualitycontrol' + delimiter prefix = 'qualitycontrol' + delimiter
affix = '' affix = ''
@ -527,7 +408,7 @@ def QualityControl(df, currenttrace='Current-1', potentialtrace='V-1', delimiter
qc[i] = {} qc[i] = {}
qc[i][prefix + currenttrace + affix] = df[df.type == 'QualityControl'][currenttrace].iloc[i] qc[i][prefix + currenttrace + affix] = df[df.type == 'QualityControl'][currenttrace].iloc[i]
qc[i][prefix + potentialtrace + affix] = df[df.type == 'QualityControl'][potentialtrace].iloc[i] qc[i][prefix + potentialtrace + affix] = df[df.type == 'QualityControl'][potentialtrace].iloc[i]
qc[i][prefix + 'onset times' + affix] = df[df.type == 'QualityControl']['onset times'].iloc[i] qc[i][prefix + 'onset_times' + affix] = df[df.type == 'QualityControl']['onset_times'].iloc[i]
qc[i][prefix + 'holdingpotential' + affix] = df[df.type == 'QualityControl']['tag_meta']['settings']['holdingpotential'].iloc[i] qc[i][prefix + 'holdingpotential' + affix] = df[df.type == 'QualityControl']['tag_meta']['settings']['holdingpotential'].iloc[i]
if 'TraceId' in df.columns: if 'TraceId' in df.columns:
qc[i][prefix + 'TraceId' + affix] = df[df.type == 'QualityControl']['TraceId'].iloc[i] qc[i][prefix + 'TraceId' + affix] = df[df.type == 'QualityControl']['TraceId'].iloc[i]
@ -552,17 +433,14 @@ def QualityControl(df, currenttrace='Current-1', potentialtrace='V-1', delimiter
qc_dic[i] = qc[np.where(qc_df[prefix + 'TraceId' + affix] == df.TraceId.loc[idx])[0][0]] qc_dic[i] = qc[np.where(qc_df[prefix + 'TraceId' + affix] == df.TraceId.loc[idx])[0][0]]
else: else:
None None
delays = qc_df[prefix + 'onset times' + affix] - df['onset times'].loc[idx] delays = qc_df[prefix + 'onset_times' + affix] - df['onset_times'].loc[idx]
maxidx = np.where(delays[np.array(delays) < 0])[0][0] maxidx = np.where(delays[np.array(delays) < 0])[0][0]
qc_dic[i] = qc[maxidx] qc_dic[i] = qc[maxidx]
qc = pd.DataFrame(qc_dic, index=df.index) qc = pd.DataFrame(qc_dic, index=df.index)
qc.columns = qc.columns.str.split(';', expand=True) qc.columns = qc.columns.str.split(';', expand=True)
# try:
df = pd.concat([df, qc], axis=1) df = pd.concat([df, qc], axis=1)
# qc = qc.drop(index=np.where(df.type == 'QualityControl')[0])
df = df.drop(index=df[df.type == 'QualityControl'].index) df = df.drop(index=df[df.type == 'QualityControl'].index)
# except: embed()
return df return df

View File

@ -7,17 +7,12 @@ from IPython import embed
def stimulus_collector(filename): def stimulus_collector(filename):
try: stims = []
stims = [] f = nix.File.open(filename, nix.FileMode.ReadOnly)
f = nix.File.open(filename, nix.FileMode.ReadOnly) b = f.blocks[0]
b = f.blocks[0] embed()
for g in b.groups: exit()
# embed()
# exit()
stims.append(g.multi_tags[0].name)
except KeyError:
embed()
exit()
return stims return stims
@ -26,22 +21,18 @@ def analyze_sams(filename):
b = f.blocks[0] b = f.blocks[0]
b.metadata.pprint(max_depth=-1) # max_depth=-1: alles rausschreiben b.metadata.pprint(max_depth=-1) # max_depth=-1: alles rausschreiben
for g in b.groups: for g in b.groups:
if 'sam' in g.name.lower(): # go through loop, until 'sam' is found if 'sam' in g.name.lower(): # go through loop, until 'sam' is found
break break
# embed()
# exit() rtag_data = g.tags[0] # rtag_data = tags within this group
rtag_data = g.tags[0]
rtag_data.metadata.pprint(max_depth=-1) rtag_data.metadata.pprint(max_depth=-1)
print(40*'*') print(40*'*')
stim_tag = g.multi_tags[0] stim_tag = g.multi_tags[0]
stim_type = g.multi_tags[0].name stim_type = g.multi_tags[0].name
stim_pos = stim_tag.positions[:] # beginnings of stimulations stim_pos = stim_tag.positions[:] # beginnings of stimuli
stim_extent = stim_tag.extents[:] # duration of stimulations stim_extent = stim_tag.extents[:] # duration of stimuli
# for r in rtag_data.references:
# print(r.name, r.type)
voltage_trace = rtag_data.references['V-1'] voltage_trace = rtag_data.references['V-1']
@ -56,15 +47,16 @@ def analyze_sams(filename):
stim_y = [np.max(voltage_trace)+10, np.max(voltage_trace)+10] stim_y = [np.max(voltage_trace)+10, np.max(voltage_trace)+10]
plt.plot([stim_pos[i], stim_pos[i] + stim_extent[i]], stim_y, 'k') plt.plot([stim_pos[i], stim_pos[i] + stim_extent[i]], stim_y, 'k')
os.chdir('/home/lisa/Dropbox/Masterarbeit/')
for j in range(len(spike_data)): for j in range(len(spike_data)):
for k in range(len(spike_data[j])): for k in range(len(spike_data[j])):
plt.plot(spike_data[j][k], np.max(voltage_trace)+5, 'o', color='k') plt.plot(spike_data[j][k], np.max(voltage_trace)+2.5, 'o', color='k')
plt.xlim([stim_pos[0]-1, stim_extent[-1]+1]) plt.xlim([stim_pos[0]-.5, stim_pos[0] + stim_extent[-1] + .5])
plt.title(filename[-26:-13] + ': ' + stim_type) plt.xlabel('time [s]')
# plt.show() plt.ylabel('voltage [mV]')
plt.show()
print('saving...') print('saving...')
os.chdir('/home/lisa/Dropbox/Masterarbeit/') plt.savefig(filename + stim_type + '.png')
plt.savefig(filename[-26:-13] + ': ' + stim_type + '.png')
plt.close() plt.close()
# f.close() # f.close()
# embed() # embed()
@ -73,13 +65,61 @@ def analyze_sams(filename):
return return
def analyze_fis(filename):
f = nix.File.open(filename, nix.FileMode.ReadOnly)
b = f.blocks[0]
b.metadata.pprint(max_depth=-1) # max_depth=-1: alles rausschreiben
for g in b.groups:
# if 'FICurve' in g.name: # go through loop, until 'sam' is found
# break
rtag_data = g.tags[0] # rtag_data = tags within this group
rtag_data.metadata.pprint(max_depth=-1)
print(40*'*')
stim_tag = g.multi_tags[0]
stim_type = g.multi_tags[0].name
stim_pos = stim_tag.positions[:] # beginnings of stimuli
stim_extent = stim_tag.extents[:] # duration of stimuli
voltage_trace = rtag_data.references['V-1']
for i in range(len(voltage_trace)):
print(voltage_trace[i])
spike_data = []
for idx in range(len(stim_pos)):
spike_data.append(stim_tag.retrieve_data(idx, 'Spikes-1')[:])
# embed()
# exit()
dims = voltage_trace.dimensions[0].axis(len(voltage_trace))
for i in range(len(stim_pos)):
plt.plot(dims[:-1], voltage_trace[:-1], color='#3673A4')
stim_y = [np.max(voltage_trace)+1.25, np.max(voltage_trace)+1.25]
plt.plot([stim_pos[i], stim_pos[i] + stim_extent[i]], stim_y, 'k')
xmin = stim_pos[i] - .1
xmax = stim_pos[i] + stim_extent[i] + .1
y = np.ones(len(spike_data[i]))*(np.max(voltage_trace)+2.5)
plt.plot(spike_data[i], y, 'o', color='#DC143C')
plt.xlim(xmin, xmax)
plt.xlabel('time [s]')
plt.ylabel('voltage [mV]')
plt.show()
return
if __name__ == '__main__': if __name__ == '__main__':
data_dir = '/home/lisa/data/' data_dir = '/home/lisa/data/'
os.chdir(data_dir) os.chdir(data_dir)
data_set = glob.glob('2019-*') data_set = glob.glob('2019*') # only look at single cells
# data_set = '2019*' # only look at single cells
for i in range(len(data_set)): for i in range(len(data_set)):
# data_set = '2019-08-22-aa-invivo-1' if '08' in data_set[i]:
print(data_dir + data_set[i] + '/' + data_set[i]) continue
# print(data_dir + data_set + '/' + data_set)
analyze_sams(data_dir + data_set[i] + '/' + data_set[i] + '.nix') analyze_sams(data_dir + data_set[i] + '/' + data_set[i] + '.nix')
# analyze_sams(data_dir + data_set + '/' + data_set + '.nix')

32
frame_analysis.py Normal file
View File

@ -0,0 +1,32 @@
import numpy as np
import matplotlib.pyplot as plt
import glob
import os
import NixFrame as nf
from IPython import embed
if __name__ == '__main__':
data_dir = '/home/lisa/data/'
os.chdir(data_dir)
data_sets = glob.glob('2019-*')
for data_set in data_sets:
print(data_set)
df = nf.load_data(data_dir + data_set + '/' + data_set + '_dataframe.pickle')
embed()
exit()
for i in range(len(df)):
stim_type = df['tag_meta']['RePro'][i]
smpl_rt = df['samplingrate'][i]
# t = df['time'][i+1]
voltage = df['V-1'][i]
t = np.arange(0, len(voltage)*1./smpl_rt, 1./smpl_rt)
print(len(t))
spiketimes = df['Spikes-1'][i]
stim_onset = df['onset_times'][i]
stim_dur = df['durations'][i]
plt.plot(t, voltage, color='#BA2D22')
plt.plot([stim_onset, stim_dur], [np.max(voltage)+0.75, np.max(voltage)+0.75], color='#53379B')
plt.plot([spiketimes, spiketimes], [np.max(voltage)+1, np.max(voltage)+1.5], color='black')
plt.show()

8
frame_maker.py Normal file
View File

@ -0,0 +1,8 @@
from IPython import embed
import NixFrame as nf
folder = '../../data'
df = nf.NixToFrame(folder, before=0.0, after=0.0, skipold=True, saveto=None, mindepth=0)
print(df)