<jupyter_start><jupyter_text>Getting started Getting started with all kinds of functions related to the postgresql database<jupyter_code># the following two lines indicate that external functions are auto-reloaded as soon as they change. 
%load_ext autoreload
%autoreload 2
# Print statements 
from __future__ import print_function # Python 2.x
# General stuff:
import sys
import argparse
import os
import json
import numpy as np
import math
import psycopg2
import cPickle
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
sns.set_style('whitegrid')
# 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))
db_status = test_connect()
if db_status == False:
    print('Grrr... no database connection could be established.')
else:
    print('Yippiyeah! Database connection is established!')<jupyter_output>Connecting to the PostgreSQL database...
Yippiyeah! Database connection is established!<jupyter_text>Query the database<jupyter_code>base_dataframe = pd.DataFrame() # create empty dataframe or feed in a base dataframe from before! *
psql = psql_neuroballs(base_dataframe)
psql.retrieve_data()<jupyter_output><empty_output><jupyter_text>get out data ...<jupyter_code>grids = psql.data()
grids.sort_values('grid_score_bnt',ascending=False,inplace=True)
# it looks like this:
grids.head(3)
grids_dave = grids.iloc[:50]
grids_horst = grids.iloc[:50]
psql = psql_neuroballs(grids_dave)
psql.retrieve_data()
grids_dave = psql.data()
grids_dave.head(2)
plt.hist(grids_horst.grid_score_bnt,color='k')
plt.hist(grids_dave.grid_score_bnt,color='k')
base_dataframe.animal_id.unique()
grids.columns
base_dataframe_exc = base_dataframe[(base_dataframe.salt_p < 0.01) & (base_dataframe.salt_i > 0.1)]
plt.hist([x for x in base_dataframe_exc.ex_latency_median if (np.isfinite(x) and x < 10 and x > -8)],50,color='k',alpha=.7)
plt.show()<jupyter_output><empty_output><jupyter_text>Now that you have a dataframe of inhibited cells, get the rest of the columns<jupyter_code>base_dataframe_exc.drop('session_name',1,inplace=True)
psql = psql_neuroballs(base_dataframe_exc)
# select ALL the info that you want
psql.retrieve_data()
base_dataframe_exc = psql.data()
base_dataframe_exc.head(2)
# sort ascending by p-value (in 30 ms interval - works well)
base_dataframe_exc.sort_values(by='salt_i', ascending=False,inplace=True)
base_dataframe_exc.columns
base_dataframe.sort_values(by='grid_score_bnt',ascending=False,inplace=True)
grids = base_dataframe.iloc[:20]<jupyter_output><empty_output><jupyter_text>Drawing functions<jupyter_code># draw_path_spike_plots 
# expects 'spiket_tracking_session' and 'tracking_session' from KLUSTA ! 
# Can be found under "Spiketimes + Tracking" and "Tracking" in the GUI
#draw_path_spike_plots(grids,grids,7,speed_cutoff=5.,offset=0,plot_hd=False)
# draw_ratemaps
# feed in a dataframe and a column name (for example ratemaps or autocorrelation maps from BNT)
draw_ratemaps(grids,'masked_ratemap_bnt',25,25) # 25 plots, from start ('0') of dataframe
# ... and here are the corresponding auto correlations
draw_ratemaps(grids,'autocorr_bnt',25,25)
sns.set(font_scale=1.5) 
sns.set_style('whitegrid')
grids = grids.iloc[:50]
grids['first_grid_orientation'] = [x[0] for x in grids['gridstats_orientation_bnt']]
grids['first_grid_spacing'] = [x[0] for x in grids['gridstats_spacing_bnt']]
grids['second_grid_orientation'] = [x[1] for x in grids['gridstats_orientation_bnt']]
grids['second_grid_spacing'] = [x[1] for x in grids['gridstats_spacing_bnt']]
grids['third_grid_orientation'] = [x[2] for x in grids['gridstats_orientation_bnt']]
grids['third_grid_spacing'] = [x[2] for x in grids['gridstats_spacing_bnt']]
g = sns.jointplot(grids['first_grid_orientation'],grids['first_grid_spacing'], kind="kde", size=7,
                  space=0,color='k',shade=False,n_levels=15,alpha=.3,stat_func=None)
g.plot_joint(plt.scatter, marker='o', c='k', s=100,alpha=.7)
g.ax_joint.set(xlim=(-140, 20), ylim=(0, 40),xlabel='Grid orientation [degrees]',ylabel='Grid spacing [cm]')

plt.show(g)

20/14.
20*.2
grids.px_to_cm
# draw_tuning_curves
# Can be used for both theta-tuning as well as HD tuning plots. 
# Feed in a dataframe (first argument) and (second argument) in square brackets the "center" and "smoothed points" vectors ... 
draw_tuning_curves(base_dataframe,['bins_angle_center_bnt','hist_angle_smooth_bnt'],25,0,'W')
# create_spike_plots_stimulus
# Requires the data from "Stimulus mats" and "Stimulus stats" in the GUI
# However - BUG ALERT!: You can not retrieve them at the same time (mark the entries of both tables), but only sequentially 
# The "True" at the end means that spikes will be plotted as dark squares (takes forever!)
# Replace with "False" if you just want the binned activity in 1 ms bins.
create_spike_plots_stimulus(base_dataframe_exc,5,0,False)
create_spike_plots_stimulus(base_dataframe_exc,5,0,True)
# draw_spiket_LFP:
# Draws spike triggered LFP (found under "Theta phase tuning" in GUI)
draw_spiket_LFP(base_dataframe_exc,5,0,sample_rate=250.)
# draw_auto_corrs
# Draws spiketime auto correlations (Found under "Spiketime autocorr" in GUI)
draw_auto_corrs(base_dataframe_exc, 5, 0)
# draw_waveforms
# Draws mean waveforms of every channel
draw_waveforms(base_dataframe_exc, 5, 0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1023976023976024, 'fraction_numerical': 0.015817515817515816, 'mean_word_length': 7.161684782608695, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '75953', 'license': 'MIT', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2083, 'n_words': 531, 'path': 'Notebook archive/get_started-all_functions.ipynb', 'repo_name': 'davidcrowland/layer_vb_tagging', 'size': '6006'}