{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.signal import find_peaks\n",
    "def turn_trace_to_spk(data, min_hight_nspk = -50, min_hight_bspk = -20, prom_nspk = 10, prom_bspk = 30):\n",
    "\n",
    "    ln_data = len(data)\n",
    "    dict_all = {}\n",
    "    peaks_nspk_all = []\n",
    "    peaks_bspk_all = []\n",
    "    properties_nspk_all = []\n",
    "    properties_bspk_all = []\n",
    "\n",
    "    for i in range (0, ln_data):\n",
    "        x = np.squeeze(data[i])\n",
    "        peaks_nspk, properties_nspk = find_peaks(x, height=(min_hight_nspk, min_hight_bspk),prominence = prom_nspk)\n",
    "        peaks_bspk, properties_bspk = find_peaks(x, height=(min_hight_bspk),prominence = prom_bspk)\n",
    "        peaks_nspk_all.append(peaks_nspk)\n",
    "        peaks_bspk_all.append(peaks_bspk)\n",
    "        properties_nspk_all.append(properties_nspk)\n",
    "        properties_bspk_all.append(properties_bspk)\n",
    "\n",
    "    dict_all['peaks_nspk_all'] =  peaks_nspk_all\n",
    "    dict_all['peaks_bspk_all'] = peaks_bspk_all\n",
    "    dict_all['properties_nspk_all'] =  properties_nspk_all\n",
    "    dict_all['properties_bspk_all'] = properties_bspk_all\n",
    "\n",
    "    return dict_all\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.ndimage import gaussian_filter as gaus_filter\n",
    "def make_spk_rate(data, len_vec, do_Nspk = True, do_Bspk = True, Method = 'reflect',Nspk_time_ms = 5, Bspk_time_ms = 8): \n",
    "    dict_all = {}\n",
    "    Bspk_number = np.zeros(len_vec)\n",
    "    Nspk_number = np.zeros(len_vec)\n",
    "    dt = 0.025\n",
    "    nr_iter = len(data['peaks_nspk_all'])\n",
    "    for i in range (0, nr_iter):\n",
    "        pk_nspk = data['peaks_nspk_all'][i]\n",
    "        pk_bspk = data['peaks_bspk_all'][i]\n",
    "        Nspk_number[pk_nspk] = Nspk_number[pk_nspk]+1\n",
    "        Bspk_number[pk_bspk] = Bspk_number[pk_bspk]+1\n",
    "    Bspk_rate = Bspk_number/nr_iter/(dt*0.001) \n",
    "    Nspk_rate = Nspk_number/nr_iter/(dt*0.001) \n",
    "\n",
    "#     if gaus_positive_half:\n",
    "#         long_rate_Bspk = np.append([[Bspk_rate]],[[Bspk_rate]])\n",
    "#         stand_dev_Bspk = int(Bspk_time_ms/dt)\n",
    "#         x = np.arange(0,int(nr_std*stand_dev_Bspk)+1)\n",
    "#         gaussian_curve_Bspk = np.exp(-np.square(x/stand_dev_Bspk)/2)\n",
    "#         len_gauss = len(gaussian_curve_Bspk)\n",
    "\n",
    "#         smoothed_Bspk = np.zeros(len_vec)\n",
    "#         indx = 0\n",
    "#         for jj in range(len(smoothed_Bspk)):\n",
    "#             smoothed_Bspk[jj] = np.sum(long_rate_Bspk[indx:indx+len_gauss]*gaussian_curve_Bspk) \n",
    "#             indx = indx+1\n",
    "#         smoothed_Bspk = smoothed_Bspk/np.sum(gaussian_curve_Bspk)\n",
    "\n",
    "#     else:\n",
    "#         long_rate_Bspk = np.append([[Bspk_rate],[Bspk_rate]],[[Bspk_rate]])\n",
    "#         stand_dev_Bspk = int(Bspk_time_ms/dt)\n",
    "#         x = np.arange(int(-nr_std*stand_dev_Bspk),int(nr_std*stand_dev_Bspk)+1)\n",
    "#         gaussian_curve_Bspk = np.exp(-np.square(x/stand_dev_Bspk)/2)\n",
    "#         len_gauss = len(gaussian_curve_Bspk)\n",
    "\n",
    "#         smoothed_Bspk = np.zeros(len_vec)\n",
    "#         indx = len_vec - int(nr_std*stand_dev_Bspk)\n",
    "#         for jj in range(len(smoothed_Bspk)):\n",
    "#             smoothed_Bspk[jj] = np.sum(long_rate_Bspk[indx:indx+len_gauss]*gaussian_curve_Bspk) \n",
    "#             indx = indx+1\n",
    "#         smoothed_Bspk = smoothed_Bspk/np.sum(gaussian_curve_Bspk)\n",
    "\n",
    "    \n",
    "    smoothed_data_Nspk = gaus_filter(Nspk_rate, Nspk_time_ms/dt, order=0, mode=Method, truncate=4.0)\n",
    "    smoothed_data_Bspk = gaus_filter(Bspk_rate, Bspk_time_ms/dt, order=0, mode=Method, truncate=4.0)\n",
    "    dict_all['Bspk_number'] = Bspk_number\n",
    "    dict_all['Nspk_number'] = Nspk_number\n",
    "    dict_all['Bspk_rate_smoothed'] = smoothed_data_Bspk\n",
    "    dict_all['Nspk_rate_smoothed'] = smoothed_data_Nspk\n",
    "\n",
    "    return dict_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_shaped_spk_rate(data, shape_data, do_Nspk = True, do_Bspk = True, Method = 'reflect',Nspk_time_ms = 5, Bspk_time_ms = 8): \n",
    "    dict_all = {}\n",
    "    dt = 0.025\n",
    "    nr_iter = shape_data[0]\n",
    "    len_data = shape_data[0]*shape_data[1]\n",
    "    mr_iter = shape_data[0]\n",
    "    Bspk_vec = np.zeros(len_data)\n",
    "    Nspk_vec = np.zeros(len_data)\n",
    "\n",
    "    peaks_nspk_loc = data['peaks_nspk_all']\n",
    "    peaks_bspk_loc = data['peaks_bspk_all']\n",
    "    Nspk_vec[np.asarray(peaks_nspk_loc)] = 1\n",
    "    Bspk_vec[np.asarray(peaks_bspk_loc)] = 1\n",
    "    \n",
    "    Nspk_vec = np.reshape(Nspk_vec,shape_data)\n",
    "    Bspk_vec = np.reshape(Bspk_vec,shape_data)\n",
    "\n",
    "    Nspk_number = np.sum(Nspk_vec, axis=0)\n",
    "    Bspk_number = np.sum(Bspk_vec, axis=0)\n",
    "    Bspk_rate = Bspk_number/nr_iter/(dt*0.001) \n",
    "    Nspk_rate = Nspk_number/nr_iter/(dt*0.001) \n",
    "\n",
    "    smoothed_data_Nspk = gaus_filter(Nspk_rate, Nspk_time_ms/dt, order=0, mode=Method, truncate=4.0)\n",
    "    smoothed_data_Bspk = gaus_filter(Bspk_rate, Bspk_time_ms/dt, order=0, mode=Method, truncate=4.0)\n",
    "    dict_all['Bspk_number'] = Bspk_number\n",
    "    dict_all['Nspk_number'] = Nspk_number\n",
    "    dict_all['Bspk_rate_smoothed'] = smoothed_data_Bspk\n",
    "    dict_all['Nspk_rate_smoothed'] = smoothed_data_Nspk\n",
    "\n",
    "    return dict_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mean_traces(data):\n",
    "    nr_iter = len(data)\n",
    "    sum_all = np.zeros(len(data[1]))\n",
    "    for i in range (0, nr_iter):\n",
    "        sum_all = sum_all + data[i]\n",
    "    mean_all = sum_all/nr_iter \n",
    "    return mean_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def trace_to_trials(data, eliminate_trials = 1, trial_length = 200, dt = 0.025):\n",
    "    \n",
    "    eliminate_ms = eliminate_trials*trial_length\n",
    "    elim_dt = int(eliminate_ms/dt)\n",
    "    trial_length_dt = int(trial_length/dt)\n",
    "    nr_trials = int((len(data) - elim_dt)*dt/trial_length)\n",
    "\n",
    "    all_trials = []\n",
    "    idx = elim_dt\n",
    "    for ii in range(nr_trials):\n",
    "        trial = data[idx:idx+trial_length_dt]\n",
    "        all_trials.append(trial)\n",
    "        idx = idx+trial_length_dt\n",
    "    return all_trials, trial_length_dt;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bin_data(data,eliminate_trials = 0,trial_length = 5):\n",
    "    vec_cut,leng = trace_to_trials(data, eliminate_trials = eliminate_trials,trial_length = trial_length)\n",
    "    one_trace = np.ones(np.shape(vec_cut)[1]).T[:,np.newaxis]\n",
    "    bin_trace = np.matmul(vec_cut,one_trace)\n",
    "    return bin_trace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}