{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:26: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0\n", "Please use `tqdm.notebook.*` instead of `tqdm._tqdm_notebook.*`\n" ] } ], "source": [ "import os\n", "import pathlib\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from matplotlib import colors\n", "import seaborn as sns\n", "import re\n", "import shutil\n", "import pandas as pd\n", "import scipy.stats\n", "import scipy\n", "from functools import reduce\n", "import exdir\n", "import expipe\n", "from distutils.dir_util import copy_tree\n", "import septum_mec\n", "import spatial_maps as sp\n", "import head_direction.head as head\n", "import septum_mec.analysis.data_processing as dp\n", "import septum_mec.analysis.registration\n", "from septum_mec.analysis.plotting import despine, savefig, plot_bootstrap_timeseries\n", "from spatial_maps.fields import find_peaks, calculate_field_centers, separate_fields_by_laplace\n", "from spike_statistics.core import permutation_resampling_test, block_bootstrap\n", "import speed_cells.speed as spd\n", "from tqdm import tqdm_notebook as tqdm\n", "from tqdm._tqdm_notebook import tqdm_notebook\n", "tqdm_notebook.pandas()\n", "\n", "from septum_mec.analysis.statistics import load_data_frames, make_paired_tables, make_statistics_table" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "project_path = dp.project_path()\n", "project = expipe.get_project(project_path)\n", "actions = project.actions\n", "\n", "output_path = pathlib.Path(\"output\") / \"longitudinal-comparisons\"\n", "(output_path / \"statistics\").mkdir(exist_ok=True, parents=True)\n", "(output_path / \"figures\").mkdir(exist_ok=True, parents=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Load cell statistics and shuffling quantiles" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of sessions above threshold 194\n", "Number of animals 4\n", "Number of individual gridcells 139\n", "Number of gridcell recordings 230\n" ] } ], "source": [ "data, labels, colors, queries = load_data_frames()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Analysis" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "max_speed = .5 # m/s only used for speed score\n", "min_speed = 0.02 # m/s only used for speed score\n", "position_sampling_rate = 100 # for interpolation\n", "position_low_pass_frequency = 6 # for low pass filtering of position\n", "\n", "box_size = [1.0, 1.0]\n", "bin_size = 0.02\n", "smoothing_low = 0.03\n", "smoothing_high = 0.06\n", "\n", "speed_binsize = 0.02\n", "\n", "stim_mask = True\n", "# baseline_duration = 600\n", "baseline_duration = None" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "data_loader = dp.Data(\n", " position_sampling_rate=position_sampling_rate, \n", " position_low_pass_frequency=position_low_pass_frequency,\n", " box_size=box_size, bin_size=bin_size, \n", " stim_mask=stim_mask, baseline_duration=baseline_duration\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def fftcorrelate2d(arr1, arr2, normalize=False, **kwargs):\n", " from copy import copy\n", " arr1 = copy(arr1)\n", " arr2 = copy(arr2)\n", " from astropy.convolution import convolve_fft\n", " if normalize:\n", " # https://stackoverflow.com/questions/53436231/normalized-cross-correlation-in-python\n", " a_ = arr1.ravel()\n", " v_ = arr2.ravel()\n", " arr1 = (arr1 - np.mean(a_)) / (np.std(a_) * len(a_))\n", " arr2 = (arr2 - np.mean(v_)) / np.std(v_)\n", " corr = convolve_fft(arr1, np.fliplr(np.flipud(arr2)), normalize_kernel=False, **kwargs)\n", " return corr\n", "\n", "\n", "def cross_correlation_distance(r1, r2):\n", " r12 = fftcorrelate2d(r1, r2)\n", " labels = separate_fields_by_laplace(r12, threshold=0)\n", " peaks = calculate_field_centers(r12, labels)\n", " centered_peaks = peaks - np.array(r1.shape) / 2\n", " offset = np.linalg.norm(centered_peaks, axis=1)\n", " distance_idx = np.argmin(offset)\n", " distance = offset[distance_idx]\n", " angle = np.arctan2(*centered_peaks[distance_idx])\n", " \n", " return distance, angle\n", "\n", "\n", "def cross_correlation_centre_of_mass(r1, r2):\n", " from scipy import ndimage\n", " r12 = fftcorrelate2d(r1, r2)\n", " cntr = ndimage.center_of_mass(r12)\n", " \n", " centered_cntr = cntr - np.array(r1.shape) / 2\n", " distance = np.linalg.norm(centered_cntr)\n", " angle = np.arctan2(*centered_cntr)\n", " \n", " return distance, angle" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "colors = ['#1b9e77','#d95f02','#7570b3','#e7298a']\n", "labels = [\n", " 'Baseline Ia', \n", " 'Baseline Ib', \n", " 'Baseline I', \n", " '11 Hz', \n", " 'Baseline IIa',\n", " 'Baseline IIb',\n", " 'Baseline II', \n", " '30 Hz'\n", "]\n", "queries = [\n", " 'baseline and i and Hz11',\n", " 'baseline and i and Hz11',\n", " 'baseline and i and Hz11',\n", " 'frequency==11 and stim_location==\"ms\"', \n", " 'baseline and ii and Hz30',\n", " 'baseline and ii and Hz30',\n", " 'baseline and ii and Hz30',\n", " 'frequency==30 and stim_location==\"ms\"']" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "keys = [\n", " 'gridness',\n", " 'speed_score',\n", " 'max_rate',\n", " 'average_rate',\n", " 'action',\n", " 'unit_name'\n", "]\n", "\n", "cell_types = [\n", " 'gridcell',\n", " 'ns_inhibited', \n", " 'ns_not_inhibited',\n", " 'bs',\n", " 'bs_not_gridcell'\n", "]\n", "\n", "results, labels = make_paired_tables(data, keys, cell_types=cell_types)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "results['gridcell']['xcorr_cntr_mass'] = results['gridcell']['action'].copy()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | unit_idnum | \n", "channel_group | \n", "date | \n", "Baseline I | \n", "11 Hz | \n", "Baseline II | \n", "30 Hz | \n", "
---|---|---|---|---|---|---|---|
entity | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
1833 | \n", "94 | \n", "94 | \n", "94 | \n", "43 | \n", "38 | \n", "36 | \n", "24 | \n", "
1834 | \n", "15 | \n", "15 | \n", "15 | \n", "8 | \n", "8 | \n", "4 | \n", "3 | \n", "
1839 | \n", "20 | \n", "20 | \n", "20 | \n", "9 | \n", "6 | \n", "5 | \n", "6 | \n", "
1849 | \n", "8 | \n", "8 | \n", "8 | \n", "3 | \n", "4 | \n", "1 | \n", "2 | \n", "
\n", " | Baseline Ia - Baseline Ib | \n", "entity | \n", "date | \n", "Baseline I - 11 Hz | \n", "Baseline IIa - Baseline IIb | \n", "Baseline II - 30 Hz | \n", "Baseline I - Baseline II | \n", "
---|---|---|---|---|---|---|---|
51 | \n", "0.408873 | \n", "1833 | \n", "20719 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
85 | \n", "NaN | \n", "1833 | \n", "20719 | \n", "NaN | \n", "0.076184 | \n", "0.722833 | \n", "NaN | \n", "
86 | \n", "NaN | \n", "1833 | \n", "20719 | \n", "NaN | \n", "0.132470 | \n", "NaN | \n", "NaN | \n", "
58 | \n", "0.284086 | \n", "1833 | \n", "200619 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
127 | \n", "NaN | \n", "1833 | \n", "200619 | \n", "NaN | \n", "0.077828 | \n", "0.301516 | \n", "NaN | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
139 | \n", "NaN | \n", "1849 | \n", "150319 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
43 | \n", "0.147306 | \n", "1849 | \n", "60319 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
65 | \n", "0.228274 | \n", "1849 | \n", "280219 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
74 | \n", "NaN | \n", "1849 | \n", "280219 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
105 | \n", "NaN | \n", "1849 | \n", "280219 | \n", "NaN | \n", "0.979388 | \n", "NaN | \n", "NaN | \n", "
137 rows × 7 columns
\n", "