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

# # Work with all cells

# + code_folding=[0]
# Imports:
# %load_ext autoreload
# %autoreload 2

import json
import numpy as np
import math
import psycopg2
import numpy as np
import pandas as pd
from datetime import date
from tqdm import tqdm_notebook

# Plotting:
from matplotlib import pyplot as plt
import seaborn as sns
import matplotlib.cm as cm
import matplotlib as mpl
# %matplotlib inline
# %config InlineBackend.figure_format = 'retina'

sns.set()

# External functions from subfolder /database_helpers. 
# as soon as you change something in there and press save, it will auto reload on next execution.
from database_helpers.psql_start import *
from database_helpers.create_tables import *
from database_helpers.write2tables import *
from postgres_analysis_helpers.general import *
from postgres_analysis_helpers.get_data import *
from postgres_analysis_helpers.circstat_functions import *
# register pickle type to retrieve binary data from database
psycopg2.extensions.register_type(psycopg2.extensions.new_type(psycopg2.BINARY.values, 'BINARY-PICKLE', cast_pickle))

# + code_folding=[0]
# Database connection check
db_status = test_connect()
if db_status == False:
    print('Grrr... no database connection could be established.')
else:
    print('Woot! Database connection is established!')
# -

# ### Load dataframes 

# +
# 'dataframe_export/excited_cells.pkl'
# 'dataframe_export/inhibited_cells.pkl'
# 'dataframe_export/all_merged_df.pkl'
# -

all_cells = pd.read_pickle('dataframe_export/all_merged_df.pkl')

stimulus_stats = pd.DataFrame() # create empty dataframe or feed in a base dataframe from before! 
psql = psql_neuroballs(all_cells) # initialize retrieval

filter_sessions = "NOT LIKE '%las%' AND session_name NOT LIKE '%obj%' AND session_name NOT LIKE '%stab%' AND session_name NOT LIKE '%room%'"

dict_ = {'BNT_tb_screen': ''}
all_cells_data = psql.retrieve_data(user_sql_tables=dict_,user_sql_filter=filter_sessions)

all_cells_data.dropna(subset=['session_name'],inplace=True)

drop_subgroup = ['animal_id','n_drive_user','session_ts','cluster_no','tetrode_no']
other_cells = other_cells.drop_duplicates(drop_subgroup).sort_values(by=drop_subgroup).reset_index().copy()

all_cells_data.head()

all_cells_data.iloc[22:25]

len(all_cells_data[all_cells_data.grid_score_bnt > 0.3])/len(all_cells_data)

all_cells_data.sort_values(by='grid_score_bnt',ascending=False,inplace=True)

draw_ratemaps(all_cells_data,'masked_ratemap_bnt',25,0)

all_cells_data.iloc[23]

all_cells_data.iloc[24]

all_cells_data.iloc[25]



StarcoderdataJupyterScriptsDedupFiltered