# Generated from npzfile: fitd1d2-D1-D1_Patch_Sample_2_post_injection_curve_tau_and_full_charging_curve_tmp_358.npz of fit number: 10253

from moose_nerp.prototypes.util import NamedDict
from moose_nerp.prototypes.chan_proto import (
    AlphaBetaChannelParams,
    StandardMooseTauInfChannelParams,
    TauInfMinChannelParams,
    ZChannelParams,
    BKChannelParams,
    ChannelSettings,
    TypicalOneD,
    TwoD,
    )

#contains all gating parameters and reversal potentials
# Gate equations have the form:
# AlphaBetaChannelParams (specify forward and backward transition rates):
# alpha(v) or beta(v) = (rate + B * v) / (C + exp((v + vhalf) / vslope))
# OR
# StandardMooseTauInfChannelParams (specify steady state and time constants):
# tau(v) or inf(v) = (rate + B * v) / (C + exp((v + vhalf) / vslope))
# OR
# TauInfMinChannelParams (specify steady state and time constants with non-zero minimum - useful for tau):
# inf(v) = min + max / (1 + exp((v + vhalf) / vslope))
# tau(v) = taumin + tauVdep / (1 + exp((v + tauVhalf) / tauVslope))
# or if tau_power=2: tau(v) = taumin + tauVdep / (1 + exp((v + tauVhalf) / tauVslope))* 1 / (1 + exp((v + tauVhalf) / -tauVslope))
#
# where v is membrane potential in volts, vhalf and vslope have units of volts
# C, min and max are dimensionless; and C should be either +1, 0 or -1
# Rate has units of per sec, and B has units of per sec per volt
# taumin and tauVdep have units of per sec
#

#units for membrane potential: volts
clrev=-60e-3
krev=-90e-3
narev=50e-3
carev=140e-3 #assumes CaExt=2 mM and CaIn=50e-3
ZpowCDI=2

VMIN = -140e-3
VMAX = 80e-3
VDIVS = 3401 #0.5 mV steps

#units for calcium concentration: mM
CAMIN = 0.001e-3   #10 nM
CAMAX = 150e-3  #40 uM, might want to go up to 100 uM with spines
CADIVS = 4001 #10 nM steps

#mtau: Ogata fig 5, no qfactor accounted in mtau, 1.2 will improve spike shape
#activation minf fits Ogata 1990 figure 3C (which is cubed root)
#inactivation hinf fits Ogata 1990 figure 6B
#htau fits the main -50 through -10 slope of Ogata figure 9 (log tau), but a qfact of 2 is already taken into account.

qfactNaF = 2.5

Na_m_params = TauInfMinChannelParams(T_min=0.00011916138208411449,
 T_vdep=0.002505368058318507,
 T_vhalf=-0.06180983895507333,
 T_vslope=0.008,
 SS_min=0.0,
 SS_vdep=1.0,
 SS_vhalf=-0.024809838955073337,
 SS_vslope=-0.01,
 T_power=2)

Na_h_params = TauInfMinChannelParams(T_min=0.0005699726479054598,
 T_vdep=0.002483540949479127,
 T_vhalf=-0.03390383342558563,
 T_vslope=0.003,
 SS_min=0.0,
 SS_vdep=1.0,
 SS_vhalf=-0.051903833425585626,
 SS_vslope=0.006,
 T_power=1)

NaFparam = ChannelSettings(Xpow=3, Ypow=1, Zpow=0, Erev=narev, name='NaF')

#This is from Migliore.
# KDrparam = ChannelSettings(Xpow=1, Ypow=0, Zpow=0, Erev=krev, name='KDr')

# KDr_X_params = AlphaBetaChannelParams(A_rate = 28.2,
#                                       A_B = 0,
#                                       A_C = 0.0,
#                                       A_vhalf = 0,
#                                       A_vslope = -12.5e-3,
#                                       B_rate = 6.78,
#                                       B_B = 0.0,
#                                       B_C = 0.0,
#                                       B_vhalf = 0.0,
#                                       B_vslope = 33.5e-3)
# KDr_Y_params = []

Krpparam = ChannelSettings(Xpow=2, Ypow=1, Zpow=0, Erev=krev, name='Krp')

#Act tuned to fit Nisenbaum 1996 fig6C (minf^2) and fig 8C (mtau)
qfactKrp=3  #Used by RE

Krp_X_params = AlphaBetaChannelParams(A_rate=48,
 A_B=0,
 A_C=0.0,
 A_vhalf=0,
 A_vslope=-0.02,
 B_rate=7.199999999999999,
 B_B=0.0,
 B_C=0.0,
 B_vhalf=0.0,
 B_vslope=0.04)

# tuned to fit Nisenbaum 1996 fig 9D (hinf, 87% inactivating) and 9B (htau)
Krp_Y_params = TauInfMinChannelParams(T_min=0.8639999999999999,
 T_vdep=12.48,
 T_vhalf=-0.042,
 T_vslope=0.013000000000000001,
 SS_min=0.13,
 SS_vdep=0.87,
 SS_vhalf=-0.056,
 SS_vslope=0.015,
 T_power=1)

Kirparam = ChannelSettings(Xpow=1, Ypow=0, Zpow=0, Erev=krev, name='Kir')
qfactKir = 1

Kir_X_params = AlphaBetaChannelParams(A_rate=0.013757306765051271,
 A_B=0.0,
 A_C=0.0,
 A_vhalf=0.006207993724463923,
 A_vslope=0.011,
 B_rate=1719.6633456314087,
 B_B=0.0,
 B_C=1.0,
 B_vhalf=-0.03379200627553608,
 B_vslope=-0.04)

KaFparam = ChannelSettings(Xpow=2, Ypow=1, Zpow=0, Erev=krev, name='KaF')

# activation constants for alphas and betas (obtained by
# matching m2 to Tkatch et al., 2000 Figs 2c, and mtau to fig 2b)

qfactKaF = 2
KaF_X_params = AlphaBetaChannelParams(A_rate=3547.5514150114404,
 A_B=0.0,
 A_C=1.0,
 A_vhalf=0.017206839305036175,
 A_vslope=-0.013,
 B_rate=886.8878537528601,
 B_B=0.0,
 B_C=1.0,
 B_vhalf=-0.0027931606949638233,
 B_vslope=0.011)

#inactivation consts for alphas and betas obtained by matching Tkatch et al., 2000 Fig 3b,
#and tau voltage dependence consistent with their value for V=0 in fig 3c.
#slowing down inact improves spike shape tremendously
KaF_Y_params = AlphaBetaChannelParams(A_rate=275.2069380057036,
 A_B=0.0,
 A_C=1.0,
 A_vhalf=0.12763503353421415,
 A_vslope=0.022,
 B_rate=170.36619971781653,
 B_B=0.0,
 B_C=1.0,
 B_vhalf=0.06163503353421417,
 B_vslope=-0.011)

#KaS based on Shen 2004 data and Wolf 2005 model code. Note that the Wolf model
#code on ModelDB notes that, via personal correspondance with Shen 2004 author,
#parameters in Shen 2004 were misreported, and are subsequently corrected in
#the Wolf 2005 code. Alpha/Beta channel params were fit to be similar to the
#Wolf steady state and tau equations--see fitKaS.py for fitting script.
#Note: Fit looks ok with these parameters but could stand to be improved.
KaSparam = ChannelSettings(Xpow=2, Ypow=1, Zpow=0, Erev=krev, name='KaS')
qfactKaS = 3 # Shen 2004/Wolf 2005
KaS_X_params = AlphaBetaChannelParams(A_rate=226078.0935153391,
 A_B=0.0,
 A_C=1,
 A_vhalf=-0.09640815015635876,
 A_vslope=-0.0162951634,
 B_rate=3567116370.856606,
 B_B=0.0,
 B_C=1,
 B_vhalf=0.3897103836436412,
 B_vslope=0.0218235302)

KaS_Y_params = AlphaBetaChannelParams(A_rate=41596177.34640679,
 A_B=0.0,
 A_C=1.0,
 A_vhalf=1.224990398271486,
 A_vslope=0.0645391447,
 B_rate=2.0915993497820335,
 B_B=0.0,
 B_C=1.0,
 B_vhalf=0.0036260010734860434,
 B_vslope=-0.0262013787)

#SS values from Churchill and MacVicar, assuming Xpow = 1
##time constants extrapolated from scarce measurements - Song & Surmeier
#SS values measured by Kasai and Neher are quite similar, except
#they use Xpow=2 to fit, thus params would be different
#they have nice time constant measurements which are ~2x slower than above
#but they measured at room temp, so qfact=1 on S&S and qfact=2 on K&N would equate them
#Vdep inact by Kasai ~10% and very slow (>50 ms). For now, have no Vdep inact.
# CDI measured by Kasai
#Note that CaL13 for D1 has mvhalf 10 mV more negative than for D2
#CaL12 does not differ between D1 and D2.
CaL12param = ChannelSettings(Xpow=1, Ypow=1, Zpow=ZpowCDI, Erev=carev, name='CaL12')
qfactCaL = 2

CaL12_X_params = AlphaBetaChannelParams(A_rate=-1760.132,
 A_B=-440000.0,
 A_C=-1.0,
 A_vhalf=0.0040003,
 A_vslope=-0.008,
 B_rate=-568.0426,
 B_B=142000.0,
 B_C=-1.0,
 B_vhalf=-0.0040003,
 B_vslope=0.005)


CaL12_Y_params = TauInfMinChannelParams(T_min=0.02215,
 T_vdep=0,
 T_vhalf=0.0040003,
 T_vslope=-0.0075,
 SS_min=0.83,
 SS_vdep=0.17,
 SS_vhalf=-0.055,
 SS_vslope=0.008,
 T_power=1)

# Using Xpow=1 produced too high a basal calcium,
# so used Xpow=2 and retuned params - much better basal calcium
CaL13param = ChannelSettings(Xpow=1, Ypow=1, Zpow=ZpowCDI, Erev=carev, name='CaL13')

CaL13_X_params = AlphaBetaChannelParams(A_rate=3000,
 A_B=0,
 A_C=1,
 A_vhalf=-0.005,
 A_vslope=-0.018,
 B_rate=4000,
 B_B=0,
 B_C=1.0,
 B_vhalf=0.052,
 B_vslope=0.008)
CaL13_Y_params = TauInfMinChannelParams(T_min=0.02215,
 T_vdep=0,
 T_vhalf=0.037,
 T_vslope=0.005,
 SS_min=0,
 SS_vdep=1,
 SS_vhalf=-0.037,
 SS_vslope=0.005,
 T_power=1)
#Params from McRory J Biol Chem, alpha1I subunit
CaTparam = ChannelSettings(Xpow=3, Ypow=1, Zpow=0, Erev=carev, name='CaT')
qfactCaT = 2
CaT_X_params = AlphaBetaChannelParams(A_rate=2000,
 A_B=0.0,
 A_C=0.0,
 A_vhalf=0.0,
 A_vslope=-0.019,
 B_rate=2673.0099,
 B_B=33000,
 B_C=-1.0,
 B_vhalf=0.0810003,
 B_vslope=0.00712)

#Original inactivation ws too slow compared to activation, made closder the alpha1G
CaT_Y_params = AlphaBetaChannelParams(A_rate=7684.020399999999,
 A_B=68000,
 A_C=-1.0,
 A_vhalf=0.1130003,
 A_vslope=0.00512,
 B_rate=640,
 B_B=0,
 B_C=0.0,
 B_vhalf=0.0,
 B_vslope=-0.017)

CaT32param = ChannelSettings(Xpow=3, Ypow=1, Zpow=0, Erev=carev, name='CaT32')
qfactCaT32 = 2
CaT32_X_params = TauInfMinChannelParams(T_min = .916e-3/qfactCaT32,
                                     T_vdep = 13.3e-3/qfactCaT32,
                                     T_vhalf = -53.6e-3,
                                     T_vslope = 10.4e-3,
                                     SS_min = 0.0,
                                     SS_vdep = 1.0,
                                     SS_vhalf = -52.3e-3,
                                     SS_vslope = -6.8e-3)
CaT32_Y_params = TauInfMinChannelParams(T_min = 20.4e-3/qfactCaT32,
                                     T_vdep = 45.6e-3/qfactCaT32,
                                     T_vhalf = -45.5e-3,
                                     T_vslope = 3.0e-3,
                                     SS_min = 0.0,
                                     SS_vdep = 1.0,
                                     SS_vhalf = -73.8e-3,
                                     SS_vslope = 2.8e-3)

CaT33param = ChannelSettings(Xpow=3, Ypow=1, Zpow=0, Erev=carev, name='CaT33')
qfactCaT33 = 2
CaT33_X_params = TauInfMinChannelParams(T_min = 3.2e-3/qfactCaT33,
                                     T_vdep = 201e-3/qfactCaT33,
                                     T_vhalf = -81.4e-3,
                                     T_vslope = 8.9e-3,
                                     SS_min = 0.003,
                                     SS_vdep = 1.0,
                                     SS_vhalf = -72.7e-3,
                                     SS_vslope = -8.7e-3)
CaT33_Y_params = TauInfMinChannelParams(T_min = 114.1e-3/qfactCaT33,
                                     T_vdep = 1/qfactCaT33,
                                     T_vhalf = -69.9e-3,
                                     T_vslope = 4.4e-3,
                                     SS_min = 0.005,
                                     SS_vdep = 1.0,
                                     SS_vhalf = -93.2e-3,
                                     SS_vslope = 4.9e-3)


# CaN SS parameters tuned so m2 fits Bargas and Surmeier 1994 boltzmann curve
# CaN tau from kasai 1992.
# Kasai measures calcium dependent inactivation
#McNaughton has act and inact, tau and ss for human CaN
CaNparam = ChannelSettings(Xpow=2, Ypow=1, Zpow=ZpowCDI, Erev=carev, name='CaN')
qfactCaN = 2
CaN_X_params = AlphaBetaChannelParams(A_rate=608,
 A_B=0,
 A_C=0.0,
 A_vhalf=0.0,
 A_vslope=-0.014,
 B_rate=1499.5231680000002,
 B_B=105600,
 B_C=-1.0,
 B_vhalf=0.01420003,
 B_vslope=0.01)

CaN_Y_params = TauInfMinChannelParams(T_min=0.035,
 T_vdep=0,
 T_vhalf=0.0,
 T_vslope=-0.014,
 SS_min=0.79,
 SS_vdep=0.21,
 SS_vhalf=-0.0748,
 SS_vslope=0.0065,
 T_power=1)
# CaR SS (Act and Inact) parameters from Foerhing et al., 2000
# Was Xpow=3 taken into account during fit?
# CaR tau from a few measurements from pyramidal neurons by Foerhing
# CaR inact tau from Brevi 2001
#Inact params are a bit too steep for ss, and not steep enough for tau
CaRparam = ChannelSettings(Xpow=3, Ypow=1, Zpow=0, Erev=carev, name='CaR')
qfactCaR = 2
CaR_X_params = AlphaBetaChannelParams(A_rate=480/4.,
 A_B=0,
 A_C=0.0,
 A_vhalf=0.0-10e-3,
 A_vslope=-0.028,
 B_rate=16000000.0/4.*(0.158-10e-3),
 B_B=16000000.0/4.,
 B_C=-1.0,
 B_vhalf=0.158-10e-3,
 B_vslope=0.0136)

CaR_Y_params = AlphaBetaChannelParams(A_rate=10000/4.*(0.11-10e-3),
 A_B=10000/4.,
 A_C=-1.0,
 A_vhalf=0.11-10e-3,
 A_vslope=0.017,
 B_rate=20/4.,
 B_B=0,
 B_C=0.0,
 B_vhalf=0.0-10e-3,
 B_vslope=-0.03)

#Reference: Maylie Bond Herson Lee Adelman 2004, Fig 2 steady state
#Fast component has tau~4 ms; not used: slow tau = 70 ms
#Fast component, tau=4.9ms from Hirschberg et al., 1998 figure 13.
SKparam = ChannelSettings(Xpow=0, Ypow=0, Zpow=1, Erev=krev, name='SKCa')

SK_Z_params = ZChannelParams(Kd=0.00057,
 power=5.2,
 tau=0.0049,
 taumax=0,
 tau_power=0,
 cahalf=0)

#Reference: Berkefeld et al. Science 2006 314(5799):615-20. Moczydlowski and Latorre 1983, J. Gen. Physiol. 82:511-542.

BKparam = ChannelSettings(Xpow=1, Ypow=0, Zpow=0, Erev=krev, name='BKCa')

BK_X_params=[BKChannelParams(alphabeta=480, K=0.18, delta=-0.84),
             BKChannelParams(alphabeta=280, K=0.011, delta=-1.0)]
#These CDI params can be used with every channel, make ZpowCDI=2
#If ZpowCDI=0 the CDI will not be used, power=-4 is to transform
#(Ca/Kd)^pow/(1+(Ca/Kd)^pow) to 1/(1+(ca/Kd)^-pow)
CDI_Z_params = ZChannelParams(Kd=0.00012,
 power=-4,
 tau=0.142,
 taumax=0,
 tau_power=0,
 cahalf=0)


# Calcium-activated chloride channel params
# Reference: Pifferi, S., Dibattista, M., & Menini, A. (2009). TMEM16B induces chloride
#            currents activated by calcium in mammalian cells. Pflügers Archiv-European
#            Journal of Physiology, 458(6), 1023-1038.
CaCCparam = ChannelSettings(Xpow=0, Ypow=0, Zpow=1, Erev=clrev, name='CaCC')

CaCC_Z_params = ZChannelParams(Kd=0.00183,
 power=2.3,
 tau=0.0238462184555153,
 taumax=0.0,
 tau_power=0,
 cahalf=0)


#Dictionary of "standard" channels, to create channels using a loop
#NaF doesn't fit since it uses different prototype form
#will need separate dictionary for BK

Channels = NamedDict(
    'Channels',
    Krp =   TypicalOneD(Krpparam, Krp_X_params,Krp_Y_params),
    KaF =   TypicalOneD(KaFparam, KaF_X_params, KaF_Y_params),
    KaS =   TypicalOneD(KaSparam, KaS_X_params, KaS_Y_params),
    Kir =   TypicalOneD(Kirparam,  Kir_X_params, []),
    CaL12 = TypicalOneD(CaL12param,CaL12_X_params, CaL12_Y_params, CDI_Z_params, calciumPermeable=True, calciumDependent=True),
    CaL13 = TypicalOneD(CaL13param, CaL13_X_params,CaL13_Y_params, CDI_Z_params, calciumPermeable=True, calciumDependent=True),
    CaN =   TypicalOneD(CaNparam, CaN_X_params, CaN_Y_params, CDI_Z_params, calciumPermeable=True, calciumDependent=True),
    CaR =   TypicalOneD(CaRparam, CaR_X_params, CaR_Y_params, [], calciumPermeable=True),
    CaT =   TypicalOneD(CaTparam,CaT_X_params, CaT_Y_params, [], calciumPermeable=True),
    CaT32 =   TypicalOneD(CaT32param,CaT32_X_params, CaT32_Y_params, [], calciumPermeable=True),
    CaT33 =   TypicalOneD(CaT33param,CaT33_X_params, CaT33_Y_params, [], calciumPermeable=True),
    SKCa =  TypicalOneD(SKparam, [], [], SK_Z_params, calciumDependent=True),
    NaF =   TypicalOneD(NaFparam, Na_m_params, Na_h_params),
    BKCa =  TwoD(BKparam, BK_X_params, calciumDependent=True),
    CaCC =  TypicalOneD(CaCCparam, [], [], CaCC_Z_params, calciumDependent=True),
)
