<jupyter_start><jupyter_text>Prep stuff<jupyter_code>drive_path = 'd:/'
import numpy as np
import pandas as pd
import os
import sys
import seaborn
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.stats import variation
import h5py
from allensdk.core.brain_observatory_cache import BrainObservatoryCache
from allensdk.brain_observatory.stimulus_analysis import StimulusAnalysis
from allensdk.brain_observatory.findlevel import findlevel

manifest_path = os.path.join(drive_path,'BrainObservatory','manifest.json')
boc = BrainObservatoryCache(manifest_file=manifest_path)
#only get the sessions that contain the natural scenes: 79 sessions
expMetaData = pd.DataFrame(boc.get_ophys_experiments())
SessionThreeMD = expMetaData[expMetaData.session_type=='three_session_B']
#Gives you all of the relevant experiment containers
#SessionThreeMD.sort_values('experiment_container_id')
#SessionThreeMD.to_csv('SessionThreeMD.csv',index=False)<jupyter_output><empty_output><jupyter_text>Pull the dataframe from the hd5 file  CHANGE<jupyter_code>expID=507691834
df=pd.read_hdf('D:\BrainObservatory\ophys_analysis\%d_three_session_B_analysis.h5'%(expID),'analysis/peak')

df.response_variability_ns
expID=507691834
df=pd.read_hdf('D:\BrainObservatory\ophys_analysis\%d_three_session_B_analysis.h5'%(expID),'analysis/mean_sweep_response_ns')
del df['dx']
Experiment=SessionThreeMD[SessionThreeMD.id==expID]
expContainerID=Experiment.experiment_container_id

df.tail()<jupyter_output><empty_output><jupyter_text>Get Stuff From the Dataframe<jupyter_code>#all of the cell specimen ids in this experiment
exp=boc.get_ophys_experiment_data(ophys_experiment_id = expID)
expcells=exp.get_cell_specimen_ids()
CellSpecimenIDList=expcells.tolist()
CellSpecimenIDs=pd.DataFrame(CellSpecimenIDList)
rowCellSpecimenIDs=CellSpecimenIDs.T
rowCellSpecimenIDs.head()
# order of all of the stimuli for this experiment
naturalscenelist=exp.get_stimulus_table('natural_scenes')
naturalscenelist.tail()<jupyter_output><empty_output><jupyter_text>Gives COV for all stimuli from one experiment<jupyter_code>framelist=pd.DataFrame(naturalscenelist.frame)
full=[framelist,df]
fullexpDF=pd.concat(full,axis=1)
fullexpDF
np.unique(framelist)
len(framelist)
fullexpDF.tail()<jupyter_output><empty_output><jupyter_text>Save this Mean Sweep<jupyter_code>fullexpDF[fullexpDF.frame==0]
(variation(fullexpDF[fullexpDF.frame==0],axis=0)).T

MeanSweeps=fullexpDF#.iloc[:,1:]
MeanSweeps.head()
MeanSweeps.hist(figsize=[30,30]);
fullexpDF.to_csv('D:\BrainObservatory\ophys_analysis\%d_MeanSweep.csv')#%expID,index=False)
#groupedFrame=fullexpDF.groupby('frame')
#groupedFrame.last()
#type(groupedFrame)
#groupedFrame.get_group(1)
#SortedGF.iloc[:,1:]
#get_group pulls all trials/cells for a single stimulus i
for i in np.unique(framelist):
    SortedGF = fullexpDF[fullexpDF.frame==i]
#     SortedGF=groupedFrame.get_group(i)
    if i == -1:
        SortedGFD=pd.DataFrame(variation(SortedGF.iloc[:,1:],axis=0)).T
    if i >=0:
        tmp=(pd.DataFrame(variation(SortedGF.iloc[:,1:],axis=0))).T
        SortedGFD=SortedGFD.append(tmp,ignore_index=False)
SortedGFD.head()
#SortedGFData=SortedGFD.T
Full=[CellSpecimenIDs,SortedGFData]
FinalDF=pd.concat(Full,axis=1,ignore_index=False)
#colnames = FinalDF.columns.tolist()
#colnames[colnames[0]] = 'Cell_Specimen_ID'
#FinalDF.columns=colnames
#FinalDF
#CellSpecimenIDs.head()
#df.columns = names
#time_stamps,traces=exp.get_corrected_fluorescence_traces()
#drg=exp.get_stimulus_table('natural_scenes')
#fig,ax=plt.subplots()
#cell_index=39
#for i in drg.start:
    #ax.plot(range(7),traces[cell_index,i:i+7])
SortedGFData.to_csv('D:\BrainObservatory\ophys_analysis\%d_SortedGFData.csv'%(expID),index=False)
FinalDF.to_csv('D:\BrainObservatory\ophys_analysis\%d_FinalCOV.csv'%(expID),index=False)<jupyter_output><empty_output><jupyter_text>Plot?<jupyter_code>ax=seaborn.heatmap(SortedGFData,xticklabels=False,yticklabels=False)
plt.xlabel('Cells')
plt.ylabel('Stimuli')
fig,ax=plt.subplots(ncols=2);

ax[0].plot(SortedGFData);
ax[1].hist(SortedGFData);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.125366568914956, 'fraction_numerical': 0.012218963831867057, 'mean_word_length': 10.306629834254144, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '170171', 'license': 'MIT', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1542, 'n_words': 196, 'path': 'Variability_with_data_frame.ipynb', 'repo_name': 'annieliu77/SWDB2016', 'size': '4092'}