Esempio n. 1
0
    def load_morpho(self, filepath):
        """
        :param filepath:
            swc file path
        """
        if not path.exists(filepath):
            raise FileNotFoundError(filepath)

        # SWC
        fileformat = filepath.split('.')[-1]
        if fileformat == 'swc':
            morpho = h.Import3d_SWC_read()
        # Neurolucida
        elif fileformat == 'asc':
            morpho = h.Import3d_Neurolucida3()
        else:
            raise Exception('file format `%s` not recognized' % filepath)

        self.all = []
        morpho.input(filepath)
        i3d = h.Import3d_GUI(morpho, 0)
        i3d.instantiate(self)

        for hoc_sec in self.all:
            name = hoc_sec.name().split('.')[-1]  # eg. name="dend[19]"
            if len(self.filter_secs(name)) > 0:
                raise LookupError(
                    "The name '%s' is already taken by another section of the cell: '%s' of type: '%s'."
                    % (name, self.name, self.__class__.__name__))
            sec = Sec(hoc_sec, cell=self, name=name)
            self.secs.append(sec)

        del self.all
Esempio n. 2
0
 def __init__(self, morph):
     morph = str(morph)
     self.axon = []
     cell = h.Import3d_Neurolucida3()
     cell.input(morph)
     i3d = h.Import3d_GUI(cell, 0)
     i3d.instantiate(self)
     self.add_axon()
     self.init_once()
Esempio n. 3
0
    def _load_geometry(self):
        '''Load the morphology-file in NEURON'''
        try:
            h.sec_counted = 0
        except LookupError:
            h('sec_counted = 0')

        #import the morphology, try and determine format
        fileEnding = self.morphology.split('.')[-1]
        if fileEnding == 'hoc' or fileEnding == 'HOC':
            h.load_file(1, self.morphology)
        elif fileEnding == 'py':
            geom_func = imp.load_source('shape_3D', self.morphology)
            geom_func.shape_3D(self)
        else:
            neuron.h('objref this')
            if fileEnding == 'asc' or fileEnding == 'ASC':
                Import = h.Import3d_Neurolucida3()
                if not self.verbose:
                    Import.quiet = 1
            elif fileEnding == 'swc' or fileEnding == 'SWC':
                Import = h.Import3d_SWC_read()
            elif fileEnding == 'xml' or fileEnding == 'XML':
                Import = h.Import3d_MorphML()
            else:
                raise ValueError(
                    '%s is not a recognised morphology file format!'
                ).with_traceback('Should be either .hoc, .asc, .swc, .xml!' %
                                 self.morphology)

            #assuming now that morphologies file is the correct format
            try:
                Import.input(self.morphology)
            except:
                if not hasattr(neuron, 'neuroml'):
                    raise Exception('Can not import, try and copy the ' + \
                    'nrn/share/lib/python/neuron/neuroml ' + \
                    'folder into %s' % neuron.__path__[0])
                else:
                    raise Exception('something wrong with file, see output')
            try:
                imprt = neuron.h.Import3d_GUI(Import, 0)
            except:
                raise Exception('See output, try to correct the file')
            imprt.instantiate(neuron.h.this)

        h.define_shape()
        self._create_sectionlists()
Esempio n. 4
0
def create_model(model_name, morph_file):
    h("objref cell, tobj")
    morph_file = "../morphs/" + morph_file
    model_path = "../PassiveModels/"
    h.load_file(model_path + model_name + ".hoc")
    h.execute("cell = new " + model_name + "()")  #replace?
    nl = h.Import3d_Neurolucida3()
    nl.quiet = 1
    nl.input(morph_file)
    imprt = h.Import3d_GUI(nl, 0)
    imprt.instantiate(h.cell)
    cell = h.cell
    cell.geom_nseg()
    cell.delete_axon()
    cell.biophys()

    return cell
Esempio n. 5
0
    def load_morpho(self, filepath, seg_per_L_um=1.0, add_const_segs=11):
        """
        :param filepath:
            swc file path
        :param seg_per_L_um:
            how many segments per single um of L, Length.  Can be < 1. None is 0.
        :param add_const_segs:
            how many segments have each section by default.
            With each um of L this number will be increased by seg_per_L_um
        """
        if not path.exists(filepath):
            raise FileNotFoundError()

        # SWC
        fileformat = filepath.split('.')[-1]
        if fileformat == 'swc':
            morpho = h.Import3d_SWC_read()
        # Neurolucida
        elif fileformat == 'asc':
            morpho = h.Import3d_Neurolucida3()
        else:
            raise Exception('file format `%s` not recognized' % filepath)

        morpho.input(filepath)
        h.Import3d_GUI(morpho, 0)
        i3d = h.Import3d_GUI(morpho, 0)
        i3d.instantiate(self)

        # add all SWC sections to self.secs; self.all is defined by SWC import
        new_secs = {}
        for sec in self.all:
            name = sec.name().split('.')[-1]  # eg. name="dend[19]"
            new_secs[name] = sec

        # change segment number based on seg_per_L_um and add_const_segs
        for sec in new_secs.values():
            add = int(sec.L * seg_per_L_um) if seg_per_L_um is not None else 0
            sec.nseg = add_const_segs + add

        self.secs.update(new_secs)
        del self.all
Esempio n. 6
0
def create_model(morphology_file, model_obj_name, instance_name, create_type):
    '''Creates a full morphology cell instance from Neurolucida morphology model template filenames

    This function is called to create the original instance to be reduced and
    to create the control cell

    TODO: To support non-Neurolucida morphology files, this function should be CHANGED.
    '''
    assert instance_name in ('original_cell', 'control_cell'), \
        "name must be one of ('original_cell', 'control_cell')"

    h("{objref %s}" % instance_name)
    model = dict(instance_name=instance_name, model_obj_name=model_obj_name)
    if create_type in ('basic', 'human'):
        h("{instance_name} = new {model_obj_name}()".format(**model))

        # instantiates according to morphology using import3d
        nl = h.Import3d_Neurolucida3()
        nl.quiet = 1
        nl.input(morphology_file)
        import_3d = h.Import3d_GUI(nl, 0)

        instance_created = getattr(h, instance_name)
        # associates cell instance with the morphology file
        import_3d.instantiate(instance_created)
        # this function should be included in the model.hoc file given as a parameter
        instance_created.complete_full_model_creation()
    elif create_type in ('bbp', 'bbpactive'):
        h('{instance_name} = new {model_obj_name}(1, "{morphology_file}")'.format(
            morphology_file=morphology_file, **model))
        h('{instance_name} = {instance_name}.CellRef'.format(**model))

    elif create_type in ('bbpnew', ):
        with chdir(morphology_file[:morphology_file.rindex('/')]):
            h('{instance_name} = new {model_obj_name}(0)'.format(**model))
    elif create_type in ('hay', 'almog', 'allen'):
        h('{instance_name} = new {model_obj_name}("{morphology_file}")'.format(
            morphology_file=morphology_file, **model))

    return getattr(h, instance_name)
def create_model(model_file,
                 morph_file,
                 model_path="../PassiveModels/",
                 morph_path="../morphs/"):

    # creating the model
    h.load_file("import3d.hoc")
    h.load_file("nrngui.hoc")
    h("objref cell, tobj")
    h.load_file(model_path + model_file + ".hoc")
    h.execute("cell = new " + model_file + "()")
    nl = h.Import3d_Neurolucida3()
    nl.quiet = 1
    nl.input(morph_path + morph_file)
    imprt = h.Import3d_GUI(nl, 0)
    imprt.instantiate(h.cell)
    HCell = h.cell
    HCell.geom_nseg()
    HCell.create_model()
    HCell.biophys()

    return HCell
Esempio n. 8
0
NUMBER_OF_SYNAPSES = len(trees)

PARALLEL_ENV = 1
h.load_file("import3d.hoc")
h.load_file("nrngui.hoc")
h("objref cell, tobj")

path = "../"
morph_file = path + "Morphs/2013_03_06_cell08_876_H41_05_Cell2.ASC"
model_file = "cell0603_08_model_cm_0_45"
model_path = path + "PassiveModels/"
print os.getcwd()
h.load_file(model_path + model_file + ".hoc")
h.execute("cell = new " + model_file + "()")  #replace?
nl = h.Import3d_Neurolucida3()
nl.quiet = 1
nl.input(morph_file)
imprt = h.Import3d_GUI(nl, 0)
imprt.instantiate(h.cell)
HCell = h.cell
HCell.geom_nseg()
HCell.create_model()
HCell.biophys()

T_DATA, V_DATA = read_epsp_file(PATH_exp + expname + ".dat")
h.dt = T_DATA[1] - T_DATA[0]
h.steps_per_ms = 1.0 / h.dt
h.tstop = T_DATA[-1]

V_DATA_Neuron = h.Vector(V_DATA.size)
Esempio n. 9
0
    def __init__(self, _id):
        self._id = _id

        h.load_file('stdlib.hoc')
        h.load_file('import3d.hoc')

        cell = h.Import3d_Neurolucida3()
        cell.input('morphology/GrC2020.asc')

        i3d = h.Import3d_GUI(cell, 0)
        i3d.instantiate(self)

        #Soma channels
        self.soma[0].nseg = 1 + (2 * int(self.soma[0].L / 40))
        self.soma[0].Ra = 100
        self.soma[0].cm = 2

        self.soma[0].insert('Leak')
        self.soma[0].gmax_Leak = 0.00020821612897999999
        self.soma[0].e_Leak = -60

        self.soma[0].insert('Kv3_4')
        self.soma[0].gkbar_Kv3_4 = 0.00053837153610999998

        self.soma[0].insert('Kv4_3')
        self.soma[0].gkbar_Kv4_3 = 0.0032501728450999999
        self.soma[0].ek = -88

        self.soma[0].insert('Kir2_3')
        self.soma[0].gkbar_Kir2_3 = 0.00080747403035999997

        self.soma[0].insert('GRC_CA')
        self.soma[0].gcabar_GRC_CA = 0.00066384354030999998

        self.soma[0].insert('Kv1_1')
        self.soma[0].gbar_Kv1_1 = 0.0046520692281700003

        self.soma[0].insert('Kv1_5')
        self.soma[0].gKur_Kv1_5 = 0.00106988075956

        self.soma[0].insert('Kv2_2_0010')
        self.soma[0].gKv2_2bar_Kv2_2_0010 = 2.5949576899999998e-05

        self.soma[0].insert('cdp5_CR')

        self.soma[0].push()
        self.soma[0].eca = 137.5
        h.pop_section()

        self.whatami = "GrC_2020_mild"

        #DEND
        for i in self.dend:
            i.nseg = 1 + (2 * int(i.L / 40))
            i.Ra = 100
            i.cm = 2.5

            i.insert('Leak')
            i.gmax_Leak = 0.00020424219215
            i.e_Leak = -60

            i.insert('GRC_CA')
            i.gcabar_GRC_CA = 0.01841833779253

            i.insert('Kca1_1')
            i.gbar_Kca1_1 = 0.02998872868395
            i.ek = -88

            i.insert('Kv1_1')
            i.gbar_Kv1_1 = 0.00010675447184

            i.insert('cdp5_CR')

            i.push()
            i.eca = 137.5
            h.pop_section()

#Hilock
        self.axon = h.Section(name='hilock', cell=self)
        self.axon.L = 1
        self.axon.nseg = 1
        self.axon.diam = 1.5
        self.axon.Ra = 100
        self.axon.cm = 2

        self.axon.insert('Leak')
        self.axon.gmax_Leak = 0.00025295417368000002
        self.axon.e_Leak = -60

        self.axon.insert('GRC_NA_FHF')
        self.axon.gnabar_GRC_NA_FHF = 0.011082499796400001
        self.axon.ena = 87.39

        self.axon.insert('Kv3_4')
        self.axon.gkbar_Kv3_4 = 0.050732563882920002
        self.axon.ek = -88

        self.axon.insert('GRC_CA')
        self.axon.gcabar_GRC_CA = 0.00028797253573000002

        self.axon.insert('cdp5_CR')

        self.axon.push()
        self.axon.eca = 137.5
        h.pt3dadd(0.0, 5.62232, 0.0, self.axon.diam)
        h.pt3dadd(0.0, 6.62232, 0.0, self.axon.diam)
        h.pop_section()

        self.axon.connect(self.soma[0], 0, 0)

        #AIS
        self.ais = h.Section(name='ais', cell=self)
        self.ais.L = 10
        self.ais.nseg = 1
        self.ais.diam = 0.7
        self.ais.Ra = 100
        self.ais.cm = 1

        self.ais.insert('GRC_NA_FHF')
        self.ais.gnabar_GRC_NA_FHF = 1.06883116205825
        self.ais.ena = 87.39

        self.ais.insert('Kv3_4')
        self.ais.gkbar_Kv3_4 = 0.034592458064240002
        self.ais.ek = -88

        self.ais.insert('Leak')
        self.ais.gmax_Leak = 0.00025011065810000001
        self.ais.e_Leak = -60

        self.ais.insert('GRC_CA')
        self.ais.gcabar_GRC_CA = 0.00011630629281

        self.ais.insert('GRC_KM')
        self.ais.gkbar_GRC_KM = 0.00044764153078999998

        self.ais.insert('cdp5_CR')

        self.ais.push()
        self.ais.eca = 137.5

        h.pt3dadd(0.0, 6.62232, 0.0, self.ais.diam)
        h.pt3dadd(0.0, 16.62232, 0.0, self.ais.diam)
        h.pop_section()

        lensec = 7
        secnumber_aa = int(126 / lensec)
        secnumber_pf = int(1000 / lensec)

        self.ais.connect(self.axon, 1, 0)

        self.HD_aa = [
            h.Section(cell=self, name='aa_' + str(x))
            for x in range(secnumber_aa)
        ]
        for b in self.HD_aa:
            b.L = lensec
            b.nseg = 1
            b.diam = 0.3
            b.Ra = 100
            b.cm = 1

            b.insert('GRC_NA')
            b.gnabar_GRC_NA = 0.029973709719629999
            b.ena = 87.39

            b.insert('Kv3_4')
            b.gkbar_Kv3_4 = 0.0046029972380800003
            b.ek = -88

            b.insert('Leak')
            b.gmax_Leak = 7.8963697590000003e-05
            b.e_Leak = -60

            b.insert('GRC_CA')
            b.gcabar_GRC_CA = 0.00059214434259999998

            b.insert('cdp5_CR')

            b.push()
            b.eca = 137.5

            len_initial = 16.62232
            len_ending = 7

            h.pt3dadd(0.0, len_initial, 0.0, b.diam)
            h.pt3dadd(0.0, len_initial + len_ending, 0.0, b.diam)
            h.pop_section()

            len_initial = len_initial + len_ending

        self.HD_pf1 = [
            h.Section(cell=self, name='pf_' + str(x))
            for x in range(secnumber_pf)
        ]

        for i in self.HD_pf1:
            i.L = lensec
            i.nseg = 1
            i.diam = 0.15
            i.Ra = 100
            i.cm = 1

            i.insert('GRC_NA')
            i.gnabar_GRC_NA = 0.01896618618573
            i.ena = 87.39

            i.insert('Kv3_4')
            i.gkbar_Kv3_4 = 0.0094015060525799998
            i.ek = -88

            i.insert('Leak')
            i.gmax_Leak = 4.1272473000000001e-07
            i.e_Leak = -60

            i.insert('GRC_CA')
            i.gcabar_GRC_CA = 0.00064742320254000001

            i.insert('cdp5_CR')

            i.push()
            i.eca = 137.5

            len_initial = 142.62232
            len_ending = 7

            h.pt3dadd(len_initial, len_initial, 0.0, i.diam)
            h.pt3dadd(len_initial + len_ending, len_initial, 0.0, i.diam)
            h.pop_section()

            len_initial = len_initial + len_ending

        self.HD_pf2 = [
            h.Section(cell=self, name='pf_' + str(x))
            for x in range(secnumber_pf)
        ]
        for z in self.HD_pf2:
            z.L = lensec
            z.nseg = 1
            z.diam = 0.15
            z.Ra = 100
            z.cm = 1

            z.insert('GRC_NA')
            z.gnabar_GRC_NA = 0.01896618618573
            z.ena = 87.39

            z.insert('Kv3_4')
            z.gkbar_Kv3_4 = 0.0094015060525799998
            z.ek = -88

            z.insert('Leak')
            z.gmax_Leak = 4.1272473000000001e-07
            z.e_Leak = -60

            z.insert('GRC_CA')
            z.gcabar_GRC_CA = 0.00064742320254000001

            z.insert('cdp5_CR')

            z.push()
            z.eca = 137.5

            len_initial = 142.62232
            len_ending = 7

            h.pt3dadd(len_initial, len_initial, 0.0, i.diam)
            h.pt3dadd(len_initial - len_ending, len_initial, 0.0, i.diam)
            h.pop_section()

            len_initial = len_initial - len_ending

#Connections

#AA
        for j in range(secnumber_aa - 1):
            l = j + 1
            self.HD_aa[l].connect(self.HD_aa[j], 1, 0)
#PF
        for i in range(secnumber_pf - 1):
            l = i + 1
            self.HD_pf1[l].connect(self.HD_pf1[i], 1, 0)
            self.HD_pf2[l].connect(self.HD_pf2[i], 1, 0)

        self.HD_pf1[0].connect(self.HD_aa[secnumber_aa - 1], 1, 0)
        self.HD_pf2[0].connect(self.HD_aa[secnumber_aa - 1], 1, 0)

        #Axon connection to the AIS
        self.HD_aa[0].connect(self.ais, 1, 0)

        #Time and Voltage vectors
        self.time_vector = h.Vector()
        self.time_vector.record(h._ref_t)

        self.vm_soma = h.Vector()
        self.vm_soma.record(self.soma[0](0.5)._ref_v)
Esempio n. 10
0
def load(filename,
         fileformat=None,
         cell=None,
         use_axon=True,
         xshift=0,
         yshift=0,
         zshift=0):
    """
    Load an SWC from filename and instantiate inside cell. Code kindly provided
    by @ramcdougal.

    Args:
        filename = .swc file containing morphology
        cell = Cell() object. (Default: None, creates new object)
        filename = the filename of the SWC file
        use_axon = include the axon? Default: True (yes)
        xshift, yshift, zshift = use to position the cell

    Returns:
        Cell() object with populated soma, axon, dend, & apic fields

    Minimal example:
        # pull the morphology for the demo from NeuroMorpho.Org
        from PyNeuronToolbox import neuromorphoorg
        with open('c91662.swc', 'w') as f:
            f.write(neuromorphoorg.morphology('c91662'))
        cell = load_swc(filename)

    """

    if cell is None:
        cell = Cell(name=string.join(filename.split('.')[:-1]))

    if fileformat is None:
        fileformat = filename.split('.')[-1]

    name_form = {1: 'soma[%d]', 2: 'axon[%d]', 3: 'dend[%d]', 4: 'apic[%d]'}

    # load the data. Use Import3d_SWC_read for swc, Import3d_Neurolucida3 for
    # Neurolucida V3, Import3d_MorphML for MorphML (level 1 of NeuroML), or
    # Import3d_Eutectic_read for Eutectic.
    if fileformat == 'swc':
        morph = h.Import3d_SWC_read()
    elif fileformat == 'asc':
        morph = h.Import3d_Neurolucida3()
    else:
        raise Exception('file format `%s` not recognized' % (fileformat))
    morph.input(filename)

    # easiest to instantiate by passing the loaded morphology to the Import3d_GUI
    # tool; with a second argument of 0, it won't display the GUI, but it will allow
    # use of the GUI's features
    i3d = h.Import3d_GUI(morph, 0)

    # get a list of the swc section objects
    swc_secs = i3d.swc.sections
    swc_secs = [swc_secs.object(i) for i in xrange(int(swc_secs.count()))]

    # initialize the lists of sections
    sec_list = {1: cell.soma, 2: cell.axon, 3: cell.dend, 4: cell.apic}

    # name and create the sections
    real_secs = {}
    for swc_sec in swc_secs:
        cell_part = int(swc_sec.type)

        # skip everything else if it's an axon and we're not supposed to
        # use it... or if is_subsidiary
        if (not (use_axon) and cell_part == 2) or swc_sec.is_subsidiary:
            continue

        # figure out the name of the new section
        if cell_part not in name_form:
            raise Exception('unsupported point type')
        name = name_form[cell_part] % len(sec_list[cell_part])

        # create the section
        sec = h.Section(name=name)

        # connect to parent, if any
        if swc_sec.parentsec is not None:
            sec.connect(real_secs[swc_sec.parentsec.hname()](swc_sec.parentx))

        # define shape
        if swc_sec.first == 1:
            h.pt3dstyle(1,
                        swc_sec.raw.getval(0, 0),
                        swc_sec.raw.getval(1, 0),
                        swc_sec.raw.getval(2, 0),
                        sec=sec)

        j = swc_sec.first
        xx, yy, zz = [swc_sec.raw.getrow(i).c(j) for i in xrange(3)]
        dd = swc_sec.d.c(j)
        if swc_sec.iscontour_:
            # never happens in SWC files, but can happen in other formats supported
            # by NEURON's Import3D GUI
            raise Exception('Unsupported section style: contour')

        if dd.size() == 1:
            # single point soma; treat as sphere
            x, y, z, d = [dim.x[0] for dim in [xx, yy, zz, dd]]
            for xprime in [x - d / 2., x, x + d / 2.]:
                h.pt3dadd(xprime + xshift, y + yshift, z + zshift, d, sec=sec)
        else:
            for x, y, z, d in zip(xx, yy, zz, dd):
                h.pt3dadd(x + xshift, y + yshift, z + zshift, d, sec=sec)

        # store the section in the appropriate list in the cell and lookup table
        sec_list[cell_part].append(sec)
        real_secs[swc_sec.hname()] = sec

    cell.all = cell.soma + cell.apic + cell.dend + cell.axon
    return cell
Esempio n. 11
0
def simulate_models_IF(cells_list,
                       models_dirs,
                       models_path="../ActiveModels/",
                       morphs_path="../Morphs/",
                       save_traces=False,
                       save_path='simulated_models_IF_Curve/',
                       thresh=0):
    h.load_file("import3d.hoc")
    h.steps_per_ms = 25
    h.dt = 1.0 / h.steps_per_ms
    h.tstop = 1500
    h.celsius = 37
    h.v_init = -86

    for ix, cell in enumerate(cells_list):
        model = models_dirs[cell]['model']
        print "simulates model", model
        h.load_file(models_path + model + ".hoc")
        h("objref HCell")
        h("HCell = new " + model + "()")
        HCell = h.HCell
        nl = h.Import3d_Neurolucida3()

        # Creating the model
        nl.quiet = 1
        nl.input(morphs_path + models_dirs[cell]['morph'])
        imprt = h.Import3d_GUI(nl, 0)
        imprt.instantiate(HCell)
        HCell.indexSections(imprt)
        HCell.geom_nsec()
        HCell.geom_nseg()
        HCell.delete_axon()
        HCell.insertChannel()
        HCell.init_biophys()
        HCell.biophys()

        # The stimulus
        icl = h.IClamp(0.5, sec=HCell.soma[0])
        icl.dur = 1000
        icl.delay = 120.33
        icl.amp = 0

        # Record the voltage at the soma
        Vsoma = h.Vector()
        Vsoma.record(HCell.soma[0](.5)._ref_v)
        tvec = h.Vector()
        tvec.record(h._ref_t)

        HCell.soma[0].push()
        MODEL_IF = []
        range_amps = range(0, 3000, 100)
        bar = progressbar.ProgressBar(max_value=len(range_amps),
                                      widgets=[
                                          ' [',
                                          progressbar.Timer(),
                                          '] ',
                                          progressbar.Bar(),
                                          ' (',
                                          progressbar.ETA(),
                                          ') ',
                                      ])

        for jx, amp1000 in enumerate(range_amps):
            amp = amp1000 / 1000.0
            icl.amp = amp
            h.init(h.v_init)
            h.run()
            n = count_spikes(np.array(Vsoma), thresh)
            MODEL_IF.append((amp, n))

            bar.update(jx)

        MODEL_IF = np.array(MODEL_IF)

        models_dirs[cell]['I'] = MODEL_IF[:, 0]
        models_dirs[cell]['F'] = MODEL_IF[:, 1]

        if save_traces:
            if not os.path.exists(save_path):
                os.mkdir(save_path)
            np.savetxt(save_path + model + "IF.txt", np.array(MODEL_IF))
Esempio n. 12
0
def run_RTHW(WRITE_TO_FILE=1,
             output_filename="rise_and_width_synapses_locations.txt"):
    # creating the model
    h.load_file("import3d.hoc")
    h.load_file("nrngui.hoc")
    h("objref cell, tobj")
    morph_file = "../morphs/2013_03_06_cell08_876_H41_05_Cell2.ASC"
    model_file = "cell0603_08_model_cm_0_45"
    model_path = "../PassiveModels/"
    h.load_file(model_path + model_file + ".hoc")
    h.execute("cell = new " + model_file + "()")
    nl = h.Import3d_Neurolucida3()
    nl.quiet = 1
    nl.input(morph_file)
    imprt = h.Import3d_GUI(nl, 0)
    imprt.instantiate(h.cell)
    HCell = h.cell
    HCell.geom_nseg()
    HCell.create_model()
    HCell.biophys()

    PLOT_MODE = 0

    TAU_1 = 0.3
    TAU_2 = 1.8
    E_SYN = 0
    WEIGHT = 0.0003
    E_PAS = -86
    Spike_time = 10
    DELAY = 0
    NUM_OF_SYNAPSES = 1
    DT = 0.01

    h.tstop = 100
    h.v_init = E_PAS
    h.steps_per_ms = 1.0 / DT
    h.dt = DT

    if WRITE_TO_FILE:
        f1 = open(output_filename, 'w+')

    Stim1 = h.NetStim()
    Stim1.interval = 10000
    Stim1.start = Spike_time
    Stim1.noise = 0
    Stim1.number = 1

    # for a given synapse, this function run the simulation  and calculate its shape index
    def calc_rise_and_width(PLOT_MODE=0):
        Vvec = h.Vector()
        Vvec.record(HCell.soma[0](0.5)._ref_v)
        h.init(h.v_init)
        h.run()
        np_v = np.array(Vvec)
        max_idx = np.argmax(np_v)
        rise_time = max_idx * DT - Stim1.start
        half_v = E_PAS + (np_v[max_idx] - E_PAS) / 2.0

        for i in range(max_idx):
            if np_v[i] > half_v:
                rise_half = i * h.dt
                break

        for i in range(max_idx, np_v.size):
            if np_v[i] < half_v:
                decay_half = i * h.dt
                break

        half_width = decay_half - rise_half

        if PLOT_MODE:
            print "rise ,", rise_time, " half width ", half_width
            np_t = np.arange(0, h.tstop + h.dt, h.dt)
            print np_v.size, np_t.size
            plt.plot(np_t, np_v, 'b')
            plt.plot(np_t[max_idx], np_v[max_idx], 'b*')
            plt.plot(np.array([rise_half, decay_half]),
                     np.array([half_v, half_v]), 'r')
            plt.show()

        return rise_time, half_width

    output_txt = "secanme\tx\tsoma_distance\trise_time\thalf_width\n"

    h.distance(sec=HCell.soma[0])

    # run over all the electrical segments in the model.
    # in each one of them put a synapse and run the simulation.

    print "re-creating the theoretical_RTHW"
    num_secs = len([sec for sec in HCell.all])
    bar = progressbar.ProgressBar(max_value=num_secs,
                                  widgets=[
                                      ' [',
                                      progressbar.Timer(),
                                      '] ',
                                      progressbar.Bar(),
                                      ' (',
                                      progressbar.ETA(),
                                      ') ',
                                  ])
    for ix, sec in enumerate(HCell.all):
        for seg in list(sec) + [sec(1)]:
            Syn1 = h.Exp2Syn(seg.x, sec=sec)
            Syn1.e = E_SYN
            Syn1.tau1 = TAU_1
            Syn1.tau2 = TAU_2
            Con1 = h.NetCon(Stim1, Syn1)
            Con1.weight[0] = WEIGHT
            Con1.delay = DELAY

            rise_time, half_width = calc_rise_and_width()
            output_txt += sec.name() + '\t' + str(seg.x) + '\t' + str(
                h.distance(seg.x, sec=sec)) + '\t'
            output_txt += str(rise_time) + '\t' + str(half_width) + '\n'
        bar.update(ix)

    if WRITE_TO_FILE:
        f1.write(output_txt)
        f1.close()

    return output_txt.strip().split("\n")
    def __init__(self, _id):
        self._id = _id

        h.load_file('stdlib.hoc')
        h.load_file('import3d.hoc')

        cell = h.Import3d_Neurolucida3()
        cell.input('morphology/GrC2020.asc')

        i3d = h.Import3d_GUI(cell, 0)
        i3d.instantiate(self)

        #Soma channels
        self.soma[0].nseg = 1 + (2 * int(self.soma[0].L / 40))
        self.soma[0].Ra = 100
        self.soma[0].cm = 2

        self.soma[0].insert('Leak')
        self.soma[0].gmax_Leak = 0.00027672909671000001
        self.soma[0].e_Leak = -60

        self.soma[0].insert('Kv3_4')
        self.soma[0].gkbar_Kv3_4 = 0.00373151328841

        self.soma[0].insert('Kv4_3')
        self.soma[0].gkbar_Kv4_3 = 0.0027313162972600002
        self.soma[0].ek = -88

        self.soma[0].insert('Kir2_3')
        self.soma[0].gkbar_Kir2_3 = 0.00094360184424999995

        self.soma[0].insert('GRC_CA')
        self.soma[0].gcabar_GRC_CA = 0.00029165028328999998

        self.soma[0].insert('Kv1_1')
        self.soma[0].gbar_Kv1_1 = 0.0031675812802999998

        self.soma[0].insert('Kv1_5')
        self.soma[0].gKur_Kv1_5 = 0.00107176612352

        self.soma[0].insert('Kv2_2_0010')
        self.soma[0].gKv2_2bar_Kv2_2_0010 = 6.710092624e-05

        self.soma[0].insert('cdp5_CR')

        self.soma[0].push()
        self.soma[0].eca = 137.5
        h.pop_section()

        self.whatami = "GrC_2020_mild"

        #DEND
        for i in self.dend:
            i.nseg = 1 + (2 * int(i.L / 40))
            i.Ra = 100
            i.cm = 2.5

            i.insert('Leak')
            i.gmax_Leak = 0.00029871180381000001
            i.e_Leak = -60

            i.insert('GRC_CA')
            i.gcabar_GRC_CA = 0.024687091736070001

            i.insert('Kca1_1')
            i.gbar_Kca1_1 = 0.01185742892862
            i.ek = -88

            i.insert('Kv1_1')
            i.gbar_Kv1_1 = 0.00015853886699000001

            i.insert('cdp5_CR')

            i.push()
            i.eca = 137.5
            h.pop_section()

#Hilock
        self.axon = h.Section(name='hilock', cell=self)
        self.axon.L = 1
        self.axon.nseg = 1
        self.axon.diam = 1.5
        self.axon.Ra = 100
        self.axon.cm = 2

        self.axon.insert('Leak')
        self.axon.gmax_Leak = 0.00031475453130000002
        self.axon.e_Leak = -60

        self.axon.insert('GRC_NA_FHF')
        self.axon.gnabar_GRC_NA_FHF = 0.020910983616370001
        self.axon.ena = 87.39

        self.axon.insert('Kv3_4')
        self.axon.gkbar_Kv3_4 = 0.03097630887484
        self.axon.ek = -88

        self.axon.insert('GRC_CA')
        self.axon.gcabar_GRC_CA = 0.00019803691988000001

        self.axon.insert('cdp5_CR')

        self.axon.push()
        self.axon.eca = 137.5
        h.pt3dadd(0.0, 5.62232, 0.0, self.axon.diam)
        h.pt3dadd(0.0, 6.62232, 0.0, self.axon.diam)
        h.pop_section()

        self.axon.connect(self.soma[0], 0, 0)

        #AIS
        self.ais = h.Section(name='ais', cell=self)
        self.ais.L = 10
        self.ais.nseg = 1
        self.ais.diam = 0.7
        self.ais.Ra = 100
        self.ais.cm = 1

        self.ais.insert('GRC_NA_FHF')
        self.ais.gnabar_GRC_NA_FHF = 1.5810107836409499
        self.ais.ena = 87.39

        self.ais.insert('Kv3_4')
        self.ais.gkbar_Kv3_4 = 0.039582385081389997
        self.ais.ek = -88

        self.ais.insert('Leak')
        self.ais.gmax_Leak = 0.00025512657995000002
        self.ais.e_Leak = -60

        self.ais.insert('GRC_CA')
        self.ais.gcabar_GRC_CA = 0.00038160760886000002

        self.ais.insert('GRC_KM')
        self.ais.gkbar_GRC_KM = 0.00049717923887

        self.ais.insert('cdp5_CR')

        self.ais.push()
        self.ais.eca = 137.5

        h.pt3dadd(0.0, 6.62232, 0.0, self.ais.diam)
        h.pt3dadd(0.0, 16.62232, 0.0, self.ais.diam)
        h.pop_section()

        lensec = 7
        secnumber_aa = int(126 / lensec)
        secnumber_pf = int(1000 / lensec)

        self.ais.connect(self.axon, 1, 0)

        self.HD_aa = [
            h.Section(cell=self, name='aa_' + str(x))
            for x in range(secnumber_aa)
        ]
        for b in self.HD_aa:
            b.L = lensec
            b.nseg = 1
            b.diam = 0.3
            b.Ra = 100
            b.cm = 1

            b.insert('GRC_NA')
            b.gnabar_GRC_NA = 0.025441894508310001
            b.ena = 87.39

            b.insert('Kv3_4')
            b.gkbar_Kv3_4 = 0.0046504514953399998
            b.ek = -88

            b.insert('Leak')
            b.gmax_Leak = 5.3037170669999997e-05
            b.e_Leak = -60

            b.insert('GRC_CA')
            b.gcabar_GRC_CA = 0.00031374692347000001

            b.insert('cdp5_CR')

            b.push()
            b.eca = 137.5

            len_initial = 16.62232
            len_ending = 7

            h.pt3dadd(0.0, len_initial, 0.0, b.diam)
            h.pt3dadd(0.0, len_initial + len_ending, 0.0, b.diam)
            h.pop_section()

            len_initial = len_initial + len_ending

        self.HD_pf1 = [
            h.Section(cell=self, name='pf_' + str(x))
            for x in range(secnumber_pf)
        ]

        for i in self.HD_pf1:
            i.L = lensec
            i.nseg = 1
            i.diam = 0.15
            i.Ra = 100
            i.cm = 1

            i.insert('GRC_NA')
            i.gnabar_GRC_NA = 0.0142518259615
            i.ena = 87.39

            i.insert('Kv3_4')
            i.gkbar_Kv3_4 = 0.0098649550733799999
            i.ek = -88

            i.insert('Leak')
            i.gmax_Leak = 1.4118927999999999e-07
            i.e_Leak = -60

            i.insert('GRC_CA')
            i.gcabar_GRC_CA = 0.00024821458382999999

            i.insert('cdp5_CR')

            i.push()
            i.eca = 137.5

            len_initial = 142.62232
            len_ending = 7

            h.pt3dadd(len_initial, len_initial, 0.0, i.diam)
            h.pt3dadd(len_initial + len_ending, len_initial, 0.0, i.diam)
            h.pop_section()

            len_initial = len_initial + len_ending

        self.HD_pf2 = [
            h.Section(cell=self, name='pf_' + str(x))
            for x in range(secnumber_pf)
        ]
        for z in self.HD_pf2:
            z.L = lensec
            z.nseg = 1
            z.diam = 0.15
            z.Ra = 100
            z.cm = 1

            z.insert('GRC_NA')
            z.gnabar_GRC_NA = 0.0142518259615
            z.ena = 87.39

            z.insert('Kv3_4')
            z.gkbar_Kv3_4 = 0.0098649550733799999
            z.ek = -88

            z.insert('Leak')
            z.gmax_Leak = 1.4118927999999999e-07
            z.e_Leak = -60

            z.insert('GRC_CA')
            z.gcabar_GRC_CA = 0.00024821458382999999

            z.insert('cdp5_CR')

            z.push()
            z.eca = 137.5

            len_initial = 142.62232
            len_ending = 7

            h.pt3dadd(len_initial, len_initial, 0.0, i.diam)
            h.pt3dadd(len_initial - len_ending, len_initial, 0.0, i.diam)
            h.pop_section()

            len_initial = len_initial - len_ending

#Connections

#AA
        for j in range(secnumber_aa - 1):
            l = j + 1
            self.HD_aa[l].connect(self.HD_aa[j], 1, 0)
#PF
        for i in range(secnumber_pf - 1):
            l = i + 1
            self.HD_pf1[l].connect(self.HD_pf1[i], 1, 0)
            self.HD_pf2[l].connect(self.HD_pf2[i], 1, 0)

        self.HD_pf1[0].connect(self.HD_aa[secnumber_aa - 1], 1, 0)
        self.HD_pf2[0].connect(self.HD_aa[secnumber_aa - 1], 1, 0)

        #Axon connection to the AIS
        self.HD_aa[0].connect(self.ais, 1, 0)

        #Time and Voltage vectors
        self.time_vector = h.Vector()
        self.time_vector.record(h._ref_t)

        self.vm_soma = h.Vector()
        self.vm_soma.record(self.soma[0](0.5)._ref_v)
    def __init__(self, _id):
        self._id = _id

        h.load_file('stdlib.hoc')
        h.load_file('import3d.hoc')

        cell = h.Import3d_Neurolucida3()
        cell.input('morphology/GrC2020.asc')

        i3d = h.Import3d_GUI(cell, 0)
        i3d.instantiate(self)

        #Soma channels
        self.soma[0].nseg = 1 + (2 * int(self.soma[0].L / 40))
        self.soma[0].Ra = 100
        self.soma[0].cm = 2

        self.soma[0].insert('Leak')
        self.soma[0].gmax_Leak = 0.00029038073716
        self.soma[0].e_Leak = -60

        self.soma[0].insert('Kv3_4')
        self.soma[0].gkbar_Kv3_4 = 0.00076192450951999995

        self.soma[0].insert('Kv4_3')
        self.soma[0].gkbar_Kv4_3 = 0.0028149683906099998
        self.soma[0].ek = -88

        self.soma[0].insert('Kir2_3')
        self.soma[0].gkbar_Kir2_3 = 0.00074725514701999996

        self.soma[0].insert('GRC_CA')
        self.soma[0].gcabar_GRC_CA = 0.00060938071783999998

        self.soma[0].insert('Kv1_1')
        self.soma[0].gbar_Kv1_1 = 0.0056973826455499997

        self.soma[0].insert('Kv1_5')
        self.soma[0].gKur_Kv1_5 = 0.00083407556713999999

        self.soma[0].insert('Kv2_2_0010')
        self.soma[0].gKv2_2bar_Kv2_2_0010 = 1.203410852e-05

        self.soma[0].insert('cdp5_CR_CAM')

        self.soma[0].push()
        self.soma[0].eca = 137.5
        h.pop_section()

        self.whatami = "GrC_2020_mild"

        #DEND
        for i in self.dend:
            i.nseg = 1 + (2 * int(i.L / 40))
            i.Ra = 100
            i.cm = 2.5

            i.insert('Leak')
            i.gmax_Leak = 0.00025029700736999997
            i.e_Leak = -60

            i.insert('GRC_CA')
            i.gcabar_GRC_CA = 0.0050012800845900002

            i.insert('Kca1_1')
            i.gbar_Kca1_1 = 0.010018074546510001
            i.ek = -88

            i.insert('Kv1_1')
            i.gbar_Kv1_1 = 0.00381819207934

            i.insert('Ubc_TRP')
            i.gtrp_Ubc_TRP = 0.0005

            i.insert('cdp5_CR_CAM')

            i.push()
            i.eca = 137.5
            h.pop_section()

#Hilock
        self.axon = h.Section(name='hilock', cell=self)
        self.axon.L = 1
        self.axon.nseg = 1
        self.axon.diam = 1.5
        self.axon.Ra = 100
        self.axon.cm = 2

        self.axon.insert('Leak')
        self.axon.gmax_Leak = 0.00036958189720000001
        self.axon.e_Leak = -60

        self.axon.insert('GRC_NA_FHF')
        self.axon.gnabar_GRC_NA_FHF = 0.0092880585146199995
        self.axon.ena = 87.39

        self.axon.insert('Kv3_4')
        self.axon.gkbar_Kv3_4 = 0.020373463109149999
        self.axon.ek = -88

        self.axon.insert('GRC_CA')
        self.axon.gcabar_GRC_CA = 0.00057726155447

        self.axon.insert('cdp5_CR_CAM')

        self.axon.push()
        self.axon.eca = 137.5
        h.pt3dadd(0.0, 5.62232, 0.0, self.axon.diam)
        h.pt3dadd(0.0, 6.62232, 0.0, self.axon.diam)
        h.pop_section()

        self.axon.connect(self.soma[0], 0, 0)

        #AIS
        self.ais = h.Section(name='ais', cell=self)
        self.ais.L = 10
        self.ais.nseg = 1
        self.ais.diam = 0.7
        self.ais.Ra = 100
        self.ais.cm = 1

        self.ais.insert('GRC_NA_FHF')
        self.ais.gnabar_GRC_NA_FHF = 1.28725006737226
        self.ais.ena = 87.39

        self.ais.insert('Kv3_4')
        self.ais.gkbar_Kv3_4 = 0.0064959534065400001
        self.ais.ek = -88

        self.ais.insert('Leak')
        self.ais.gmax_Leak = 0.00029276697557000002
        self.ais.e_Leak = -60

        self.ais.insert('GRC_CA')
        self.ais.gcabar_GRC_CA = 0.00031198539471999999

        self.ais.insert('GRC_KM')
        self.ais.gkbar_GRC_KM = 0.00056671971737000002

        self.ais.insert('cdp5_CR_CAM')

        self.ais.push()
        self.ais.eca = 137.5

        h.pt3dadd(0.0, 6.62232, 0.0, self.ais.diam)
        h.pt3dadd(0.0, 16.62232, 0.0, self.ais.diam)
        h.pop_section()

        lensec = 7
        secnumber_aa = int(126 / lensec)
        secnumber_pf = int(1000 / lensec)

        self.ais.connect(self.axon, 1, 0)

        self.HD_aa = [
            h.Section(cell=self, name='aa_' + str(x))
            for x in range(secnumber_aa)
        ]
        for b in self.HD_aa:
            b.L = lensec
            b.nseg = 1
            b.diam = 0.3
            b.Ra = 100
            b.cm = 1

            b.insert('GRC_NA')
            b.gnabar_GRC_NA = 0.026301636815019999
            b.ena = 87.39

            b.insert('Kv3_4')
            b.gkbar_Kv3_4 = 0.00237386061632
            b.ek = -88

            b.insert('Leak')
            b.gmax_Leak = 9.3640921249999996e-05
            b.e_Leak = -60

            b.insert('GRC_CA')
            b.gcabar_GRC_CA = 0.00068197420273000001

            b.insert('cdp5_CR_CAM')

            b.push()
            b.eca = 137.5

            len_initial = 16.62232
            len_ending = 7

            h.pt3dadd(0.0, len_initial, 0.0, b.diam)
            h.pt3dadd(0.0, len_initial + len_ending, 0.0, b.diam)
            h.pop_section()

            len_initial = len_initial + len_ending

        self.HD_pf1 = [
            h.Section(cell=self, name='pf_' + str(x))
            for x in range(secnumber_pf)
        ]

        for i in self.HD_pf1:
            i.L = lensec
            i.nseg = 1
            i.diam = 0.15
            i.Ra = 100
            i.cm = 1

            i.insert('GRC_NA')
            i.gnabar_GRC_NA = 0.017718484492610001
            i.ena = 87.39

            i.insert('Kv3_4')
            i.gkbar_Kv3_4 = 0.0081756804703699993
            i.ek = -88

            i.insert('Leak')
            i.gmax_Leak = 3.5301616000000001e-07
            i.e_Leak = -60

            i.insert('GRC_CA')
            i.gcabar_GRC_CA = 0.00020856833529999999

            i.insert('cdp5_CR_CAM')

            i.push()
            i.eca = 137.5

            len_initial = 142.62232
            len_ending = 7

            h.pt3dadd(len_initial, len_initial, 0.0, i.diam)
            h.pt3dadd(len_initial + len_ending, len_initial, 0.0, i.diam)
            h.pop_section()

            len_initial = len_initial + len_ending

        self.HD_pf2 = [
            h.Section(cell=self, name='pf_' + str(x))
            for x in range(secnumber_pf)
        ]
        for z in self.HD_pf2:
            z.L = lensec
            z.nseg = 1
            z.diam = 0.15
            z.Ra = 100
            z.cm = 1

            z.insert('GRC_NA')
            z.gnabar_GRC_NA = 0.017718484492610001
            z.ena = 87.39

            z.insert('Kv3_4')
            z.gkbar_Kv3_4 = 0.0081756804703699993
            z.ek = -88

            z.insert('Leak')
            z.gmax_Leak = 1.4118927999999999e-07
            z.e_Leak = -60

            z.insert('GRC_CA')
            z.gcabar_GRC_CA = 0.00020856833529999999

            z.insert('cdp5_CR_CAM')

            z.push()
            z.eca = 137.5

            len_initial = 142.62232
            len_ending = 7

            h.pt3dadd(len_initial, len_initial, 0.0, i.diam)
            h.pt3dadd(len_initial - len_ending, len_initial, 0.0, i.diam)
            h.pop_section()

            len_initial = len_initial - len_ending

#Connections

#AA
        for j in range(secnumber_aa - 1):
            l = j + 1
            self.HD_aa[l].connect(self.HD_aa[j], 1, 0)
#PF
        for i in range(secnumber_pf - 1):
            l = i + 1
            self.HD_pf1[l].connect(self.HD_pf1[i], 1, 0)
            self.HD_pf2[l].connect(self.HD_pf2[i], 1, 0)

        self.HD_pf1[0].connect(self.HD_aa[secnumber_aa - 1], 1, 0)
        self.HD_pf2[0].connect(self.HD_aa[secnumber_aa - 1], 1, 0)

        #Axon connection to the AIS
        self.HD_aa[0].connect(self.ais, 1, 0)

        #Time and Voltage vectors
        self.time_vector = h.Vector()
        self.time_vector.record(h._ref_t)

        self.vm_soma = h.Vector()
        self.vm_soma.record(self.soma[0](0.5)._ref_v)