{
 "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.filters 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",
    "    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 = np.int(eliminate_ms/dt)\n",
    "    trial_length_dt = np.int(trial_length/dt)\n",
    "    nr_trials = np.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": [
    "def run_sim(trial_length = 200, Inh = False, pf_exc_canc_inh = False, Nspk_up_Bspk_down = False,\\\n",
    "            Inh_gmax = 0.03, canc_gmax = .00021, stim_dur = 10000, record_conduc = False,record_distal_apic = False):\n",
    "\n",
    "\n",
    "    ampaA = []\n",
    "    ampaA_all = []\n",
    "    idx = 0\n",
    "    for ii in np.arange(10,stim_dur,18):\n",
    "        ampaA = h.AmpaSyn(h.dend[1](0.5)) \n",
    "        ampaA.onset = ii + np.random.normal(0, 6, 1) \n",
    "        ampaA.gmax = .004 + .001*np.random.normal(0,.1,1) + .005*np.random.poisson(0.074) \n",
    "        ampaA_all.append(ampaA)\n",
    "        idx += 1\n",
    "\n",
    "    if Inh:    \n",
    "        gaba = []\n",
    "        gaba_all = [] \n",
    "        for sec in vml:\n",
    "            idx = 0\n",
    "            for ii in np.arange(97,stim_dur,trial_length):\n",
    "                gaba = h.GabaASyn(sec(0.45)) \n",
    "                gaba.onset = ii + np.random.normal(0, 5, 1) \n",
    "                gaba.gmax = Inh_gmax + .001*np.random.normal(1, 1, 1) \n",
    "                gaba_all.append(gaba)\n",
    "                idx += 1\n",
    "\n",
    "    if pf_exc_canc_inh: \n",
    "        ampaAB = []\n",
    "        ampaAB_all = []\n",
    "        exclude_comp = ['apic[0]']\n",
    "        for sec in h.apical:\n",
    "            if sec.hname() not in exclude_comp:\n",
    "                idx = 0    \n",
    "                for ii in np.arange(97,stim_dur,trial_length): \n",
    "                    ampa_AB = h.AmpaSyn(sec(0.5)) \n",
    "                    ampa_AB.onset = ii + np.random.normal(0.2, 3, 1) \n",
    "                    ampa_AB.gmax = canc_gmax + .00002*np.random.normal(1, 2, 1)\n",
    "                    ampaAB_all.append(ampa_AB)\n",
    "                    idx += 1\n",
    "\n",
    "\n",
    "    if Nspk_up_Bspk_down:\n",
    "        gaba = []\n",
    "        gaba_all = []    \n",
    "        for sec in vml:\n",
    "            idx = 0\n",
    "            for ii in np.arange(97,stim_dur,trial_length):\n",
    "                gaba = h.GabaASyn(sec(0.45)) \n",
    "                gaba.onset = ii + np.random.normal(0, 2, 1) \n",
    "                gaba.gmax = 0.2 + .001*np.random.normal(1, 1, 1) \n",
    "                gaba_all.append(gaba)\n",
    "                idx += 1\n",
    "\n",
    "        ampaAB = []\n",
    "        ampaAB_all = []\n",
    "        idx = 0    \n",
    "        for ii in np.arange(100,stim_dur,trial_length):\n",
    "            ampa_AB = h.AmpaSyn(h.dend[1](0.5)) \n",
    "            ampa_AB.onset = ii + np.random.normal(0.3, 4, 1) \n",
    "            ampa_AB.gmax = 0.02 + .001*np.random.normal(1, 1, 1) \n",
    "            ampaAB_all.append(ampa_AB)\n",
    "            idx += 1\n",
    "\n",
    "\n",
    "    dict_all = {}\n",
    "    v_vec_apic = h.Vector()        # Membrane potential vector\n",
    "    v_vec_axon = h.Vector()        # Membrane potential vector\n",
    "    v_vec_soma = h.Vector()        # Membrane potential vector\n",
    "    t_vec = h.Vector()             # Time stamp vector\n",
    "\n",
    "    if record_distal_apic:\n",
    "        v_vec_apic.record(h.apic[13](1)._ref_v)\n",
    "    else:\n",
    "        v_vec_apic.record(h.apic[1](0.5)._ref_v)\n",
    "    v_vec_axon.record(h.axon[1](0.5)._ref_v)\n",
    "    v_vec_soma.record(h.soma(0.5)._ref_v)\n",
    "    t_vec.record(h._ref_t)\n",
    "    \n",
    "    if record_conduc:\n",
    "        ina_vec_apic = h.Vector()        \n",
    "        ik_vec_apic = h.Vector() \n",
    "        m_vec_apic = h.Vector()        \n",
    "        h_vec_apic = h.Vector() \n",
    "        n_vec_apic = h.Vector() \n",
    "\n",
    "        ina_vec_apic.record(h.apic[1](0.5)._ref_ina)\n",
    "        ik_vec_apic.record(h.apic[1](0.5)._ref_ik)\n",
    "        m_vec_apic.record(h.apic[1](0.5)._ref_m_hh)\n",
    "        h_vec_apic.record(h.apic[1](0.5)._ref_h_hh)\n",
    "        n_vec_apic.record(h.apic[1](0.5)._ref_n_hh)\n",
    "\n",
    "    h.tstop = stim_dur\n",
    "    h.run()\n",
    "    dict_all['t_vec'] =  t_vec\n",
    "    dict_all['v_vec_apic'] = v_vec_apic\n",
    "    dict_all['v_vec_axon'] = v_vec_axon\n",
    "    dict_all['v_vec_soma'] = v_vec_soma\n",
    "    \n",
    "    if record_conduc:\n",
    "        dict_all['ina_vec_apic'] = ina_vec_apic        \n",
    "        dict_all['ik_vec_apic'] = ik_vec_apic \n",
    "        dict_all['m_vec_apic'] = m_vec_apic        \n",
    "        dict_all['h_vec_apic'] = h_vec_apic \n",
    "        dict_all['n_vec_apic'] = n_vec_apic \n",
    "\n",
    "    ampaA = []\n",
    "    ampaA_all = []\n",
    "\n",
    "    ampaAAB = []\n",
    "    ampaAAB_all = []\n",
    "\n",
    "    gaba = []\n",
    "    gaba_all = []\n",
    "\n",
    "    ampaAB = []\n",
    "    ampaAB_all = []\n",
    "\n",
    "    return dict_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_sim_bse(trial_length = 200, remove_Inh = False, Nspk_down_Bspk_up = False,\\\n",
    "            bl_dend = False, bl_apical = False, bl_inh = False, stim_dur = 10000):\n",
    "\n",
    "    if bl_dend:\n",
    "        ampaA = []\n",
    "        ampaA_all = []\n",
    "        for ii in np.arange(10,stim_dur,36):\n",
    "            ampaA = h.AmpaSyn(h.dend[1](0.5)) \n",
    "            ampaA.onset = ii + np.random.normal(0, 10, 1) \n",
    "            ampaA.gmax = .004 + .0053*np.random.poisson(0.3) \n",
    "            ampaA_all.append(ampaA)\n",
    "\n",
    "    if bl_apical:            \n",
    "        ampaAAB = []\n",
    "        ampaAAB_all = []\n",
    "        for ii in np.arange(28,stim_dur,36):\n",
    "            ampa_AAB = h.AmpaSyn(h.apic[0](0.5)) \n",
    "            ampa_AAB.onset = ii + np.random.normal(0,10,1) \n",
    "            ampa_AAB.gmax =  .002 + .0055*np.random.poisson(0.3) \n",
    "            ampaAAB_all.append(ampa_AAB)\n",
    "        \n",
    "    if bl_inh and not remove_Inh:  \n",
    "        gaba = []\n",
    "        gaba_all = []    \n",
    "        for sec in vml:\n",
    "            for ii in np.arange(10,stim_dur,10):\n",
    "                gaba = h.GabaASyn(sec(0.45)) \n",
    "                gaba.onset = ii + np.random.normal(0, 5, 1) \n",
    "                gaba.gmax = 0.009 + .001*np.random.normal(1, 1, 1)\n",
    "                gaba_all.append(gaba)\n",
    "\n",
    "\n",
    "    if remove_Inh:  \n",
    "        stim_time = np.arange(100,stim_dur,trial_length)\n",
    "        list_all = []\n",
    "        for jj in range(-10,10):\n",
    "            b = stim_time + jj\n",
    "            c = b.tolist()\n",
    "            list_all = list_all + c\n",
    "        gaba = []\n",
    "        gaba_all = []    \n",
    "        for sec in vml:\n",
    "            for ii in np.arange(10,stim_dur,10):\n",
    "                if ii not in list_all:\n",
    "                    gaba = h.GabaASyn(sec(0.45)) \n",
    "                    gaba.onset = ii + np.random.normal(0, 5, 1) \n",
    "                    gaba.gmax = 0.009 + .001*np.random.normal(1, 1, 1)\n",
    "                    gaba_all.append(gaba)\n",
    "            \n",
    "\n",
    " \n",
    "    if Nspk_down_Bspk_up:\n",
    "        gabaB = []\n",
    "        gabaB_all = [] \n",
    "        for ii in np.arange(97,stim_dur,trial_length):\n",
    "            gabaB = h.GabaASyn(h.dend[1](0.45)) \n",
    "            gabaB.onset = ii + np.random.normal(0, 7, 1) \n",
    "            gabaB.gmax = 0.1 + .0005*np.random.normal(1, 1, 1) # 0.09 \n",
    "            gabaB_all.append(gabaB)\n",
    "\n",
    "\n",
    "    dict_all = {}\n",
    "    v_vec_apic = h.Vector()        # Membrane potential vector\n",
    "    v_vec_axon = h.Vector()        # Membrane potential vector\n",
    "    v_vec_soma = h.Vector()        # Membrane potential vector\n",
    "    t_vec = h.Vector()             # Time stamp vector\n",
    "\n",
    "    v_vec_apic.record(h.apic[1](0.5)._ref_v)\n",
    "    v_vec_axon.record(h.axon[1](0.5)._ref_v)\n",
    "    v_vec_soma.record(h.soma(0.5)._ref_v)\n",
    "    t_vec.record(h._ref_t)\n",
    "\n",
    "    h.tstop = stim_dur\n",
    "    h.run()\n",
    "    dict_all['t_vec'] =  t_vec\n",
    "    dict_all['v_vec_apic'] = v_vec_apic\n",
    "    dict_all['v_vec_axon'] = v_vec_axon\n",
    "    dict_all['v_vec_soma'] = v_vec_soma\n",
    "\n",
    "    ampaA = []\n",
    "    ampaA_all = []\n",
    "\n",
    "    ampaAAB = []\n",
    "    ampaAAB_all = []\n",
    "\n",
    "    gaba = []\n",
    "    gaba_all = []\n",
    "    \n",
    "    ampaAB = []\n",
    "    ampaAB_all = []\n",
    "\n",
    "    gabaB = []\n",
    "    gabaB_all = []  \n",
    "    \n",
    "    return dict_all"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}