# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [Root]
#     language: python
#     name: Python [Root]
# ---

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)
# -

expMetaData = pd.DataFrame(boc.get_ophys_experiments())
SessionThreeMD = expMetaData[expMetaData.session_type=='three_session_B']

#PULL CSV file Complete for COV
for i, expt_id in enumerate(SessionThreeMD['id']):
    if i == 0:
        df = pd.read_csv("D:\BrainObservatory\ophys_analysis\%d_FinalCOV.csv" % expt_id)
    if i>0:
        tmp = pd.read_csv("D:\BrainObservatory\ophys_analysis\%d_FinalCOV.csv" % expt_id)
        df=df.append(tmp,ignore_index=False)
df.rename(columns={'0': 'Cell_Specimen_ID','0.1':'0'}, inplace=True)

CompleteCOV=pd.read_csv('D:\BrainObservatory\ophys_analysis\CompleteCSV.csv')
CompleteCOV.head()

# +
cell_specimens_dfdf=pd.DataFrame(cell_specimens_df)
AllCells=cell_specimens_dfdf[~np.isnan(cell_specimens_dfdf.pref_image_ns)]
RelevCells_df=pd.DataFrame(AllCells, columns=['cell_specimen_id','pref_image_ns'])
# RelevCells_df.head()

VISpRClist=AllCells.loc[AllCells.area=='VISp']
VISpRC=pd.DataFrame(VISpRClist, columns=['cell_specimen_id','pref_image_ns'])

VISpmRClist=AllCells.loc[AllCells.area=='VISpm']
VISpmRC=pd.DataFrame(VISpmRClist, columns=['cell_specimen_id','pref_image_ns'])

VISlRClist=AllCells.loc[AllCells.area=='VISl']
VISlRC=pd.DataFrame(VISlRClist, columns=['cell_specimen_id','pref_image_ns'])

VISalRClist=AllCells[AllCells.area=='VISal']
VISalRC=pd.DataFrame(VISalRClist, columns=['cell_specimen_id','pref_image_ns'])
# -

Cux2DF=SessionThreeMD[SessionThreeMD.cre_line == 'Cux2-CreERT2']
Cux2DF_expid=pd.DataFrame(Cux2DF.experiment_container_id)
for i, expid in enumerate(RelevCells['experiment_container_id']):
    

# +
# Cux2DF=SessionThreeMD[SessionThreeMD.cre_line == 'Cux2-CreERT2']
# for i, celltype in Cux2DF:
#     creline=str(int(Cux2DF.experiment_container_id[RelevCells_df.cell_specimen_id==cs_id]))
#     if i == 0:
#         csid_COV = pd.DataFrame({'Cell_Specimen_id': str(cs_id),'COV':CompleteCOV.loc[CompleteCOV.Cell_Specimen_ID==cs_id,pref_csid]})
#     if i>0:
#         tmp = pd.DataFrame({'Cell_Specimen_id': str(cs_id),'COV':CompleteCOV.loc[CompleteCOV.Cell_Specimen_ID==cs_id,pref_csid]})
#         csid_COV=csid_COV.append(tmp)

# +
for i, celltype in cre_line == 'Cux2-CreERT2'
Cux2RClist=AllCells[AllCells.area=='VISp']
Cux2RC=pd.DataFrame(Cux2RClist, columns=['cell_specimen_id','pref_image_ns'])
                    
# RorbRClist=AllCells[AllCells.area=='Rorb']
# RorbRC=pd.DataFrame(RorbRClist, columns=['cell_specimen_id','pref_image_ns'])

# Rbp4RClist=AllCells[AllCells.area=='R']
# Rbp4RC=pd.DataFrame(Rbp4RClist, columns=['cell_specimen_id','pref_image_ns'])
                    
# ScnnlaRClist=AllCells[AllCells.area=='Rorb']
# ScnnlaRC=pd.DataFrame(ScnnlaRClist, columns=['cell_specimen_id','pref_image_ns'])

# +
for i, cs_id in enumerate(RelevCells['cell_specimen_id']):
    pref_csid=str(int(RelevCells_df.pref_image_ns[RelevCells_df.cell_specimen_id==cs_id]))
    if i == 0:
        csid_COV = pd.DataFrame({'Cell_Specimen_id': str(cs_id),'COV':CompleteCOV.loc[CompleteCOV.Cell_Specimen_ID==cs_id,pref_csid]})
    if i>0:
        tmp = pd.DataFrame({'Cell_Specimen_id': str(cs_id),'COV':CompleteCOV.loc[CompleteCOV.Cell_Specimen_ID==cs_id,pref_csid]})
        csid_COV=csid_COV.append(tmp)

#for i, cs_id in enumerate(RelevCells['cell_specimen_id']):
#    =RelevCells_df.pref_image_ns[RelevCells_df.cell_specimen_id==cs_id]
# for i, expt_id in enumerate(RelevCells['cell_specimen_id']):
#     col_str="%d" % int(RelevCells.loc[i]['pref_image_ns'])
#     df['cell_specimen_id' == expt_it][col_str]
#     if i == 0:
#         newdfRelevCells['cell_specimen_id']=
csid_COV.head()
# -

#csidCOVheatmap=csidCOVheatmap
csid_COV_hm=pd.DataFrame(csid_COV,columns=['COV'])

ax=seaborn.heatmap(csid_COV_hm,xticklabels=False,yticklabels=False)
plt.xlabel('Cells')
plt.ylabel('Stimuli')

csid_COV.hist(column='COV',bins=1000);

# +
#df.to_csv('D:\BrainObservatory\ophys_analysis\CompleteCSV.csv',index=False)

# +
# #PULL HF5 file
# #Get Mean Sweep with cell specimen ids
# for i, exp_id in enumerate(SessionThreeMD['id']):
#     if i==0:
#         df=pd.read_csv("D:\BrainObservatory\ophys_analysis\%d_MeanSweep.csv" % expt_id)
#     if i>0:
#         tmp=pd.read_csv("D:\BrainObservatory\ophys_analysis\%d_MeanSweep.csv" % expt_id)
        
#     df = pd.read_csv("D:\BrainObservatory\ophys_analysis\%d_MeanSweep.csv" % expt_id)
#     exp=boc.get_ophys_experiment_data(ophys_experiment_id = exptnum)
#     expcells=exp.get_cell_specimen_ids()
#     CellSpecimenIDList=expcells.tolist()
#     CellSpecimenIDs=pd.DataFrame(CellSpecimenIDList)
#     naturalscenelist=exp.get_stimulus_table('natural_scenes')
#     return df
#     #else:
#        # df.append(read_csv...)
#         #tmp=(pd.DataFrame(variation(SortedGF.iloc[:,1:],axis=0))).T
#         #SortedGFD=SortedGFD.append(tmp,ignore_index=True)

# +
# expt_id=512149367
# expContainerID = SessionThreeMD.experiment_container_id[SessionThreeMD.id==expt_id]
# expContainerID
# #Experiment=SessionThreeMD[SessionThreeMD.id==512149367]
# # expContainerID=Experiment.experiment_container_id
# cell_specimens_df = boc.get_cell_specimens()

# +
#AllCells.keys()

# +
# for i, expt_id in enumerate(SessionThreeMD['id']):
#     expContainerID = SessionThreeMD.experiment_container_id[SessionThreeMD.id==expt_id]
#     cell_specimens_df = pd.DataFrame(boc.get_cell_specimens(experiment_container_ids=expContainerID))
#     if i == 0:
#         PrefStim=pd.DataFrame(cell_specimens_df, columns=['cell_specimen_id','pref_image_ns'])
#     if i > 0:
#         tmp = pd.DataFrame(cell_specimens_df, columns=['cell_specimen_id','pref_image_ns'])
#         PrefStim=PrefStim.append(tmp)
# PrefStim
# #GoodCells=PrefStim.dropna()

# +
#df.shape

# +
# cell_specimens_df = pd.DataFrame(boc.get_cell_specimens(experiment_container_ids=[expContainerID]))
# PrefStim=pd.DataFrame(cell_specimens_df, columns=['cell_specimen_id','pref_image_ns'])
# #for i in (df['pref_image_ns']):
#     #if i == nan:
#         #continue
# PrefStim['pref_image_ns'='110']
# -

ax=seaborn.heatmap(df.iloc[:,1:],vmin=-100,vmax=100,xticklabels=False,yticklabels=False)
plt.xlabel('Cells')
plt.ylabel('Stimuli')

# +
# #expID=506278598
# #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.values
# #exp=boc.get_ophys_experiment_data(ophys_experiment_id = expID)
# cell_specimens_df = pd.DataFrame(boc.get_cell_specimens(experiment_container_ids=[expContainerID]))
# PrefStim=pd.DataFrame(cell_specimens_df, columns=['cell_specimen_id','pref_image_ns'])
# GoodCells=PrefStim.dropna()
# #Cell_SID=pd.DataFrame(PrefStim['cell_specimen_id'])
# GoodCells

StarcoderdataJupyterScriptsDedupFiltered