Extracellular stimulation (from Monica Bell Vila)

195 views
Skip to first unread message

Salvador Dura

unread,
Sep 22, 2022, 12:53:31 PM9/22/22
to NetPyNE Q&A forum
Monica Bell Vila  12:55 PM
Hi Salvador! Thank you for getting back to me about the morphology files. I have tried to load them but am still seeing the same results as with the other morphology files. Are there additional mechanisms I need to apply to the morphology pickle files after importing them? I thought perhaps that my addition of the extracellular mechanism was interfering with the functioning of the .pkl files, but even without including it I seem to be unable to make NetStim work. I noticed I had not been including the cellModel (i.e. HH_reduced) when defining the populations in the code I had sent you, but updating this doesn't seem to change the response.
I think next I will try to apply the extracellular mechanism + stimulation directly to the netParams.py file within the M1detailed folder and seeing if I can generate the response that way, as the issue seems to lie with how I am importing the morphology files/applying NetStim :-(

salvadord  1:58 PM
hi monica, can you share your code via github so I can clone it and give it a try? It might be some small issue that’s easy to fix

Monica Bell Vila  2:38 PM
Apologies for my delay - I have uploaded the test code here: https://github.com/m-bellv/netpyne_extracellularstim

Eugenio Urdapilleta

unread,
Sep 22, 2022, 7:56:43 PM9/22/22
to NetPyNE Q&A forum
Hi Monica,
 I reviewed your code.  I found the following issues:

line 65 and 66: label of the pop -> 'ITL23' (not the cellParams label)
line 141: typo in the pop
lines 181 and 184: 'sec', not 'secList'. This is critical, as you are not getting any NetStim. In addition, 'allSegs' does not define anything in NetPyNE.
line 225: I have to reduce the amp to get something meaningful

Regarding additional mechanisms, yes, you should compile all files inside the folder "mechs" in the model M1detailed. Probably not all are used in these two particular cell types, but those not used won't do anything harmful. Next, I copy the code I ran (producing realistic traces), with all the mods compiled with "nrnivmodl". Best regards,

Eugenio
---------------------------------------------------------------------------------------------------------------------------
#Testing extracellular stim with morphology files - only using 2 neuron types

from netpyne import sim
from netpyne import specs
import math
import numpy as np
import neuron
import os
from re import search
import sys
import tkinter as tk
from tkinter import filedialog

# object of class NetParams to store the network parameters
netParams = specs.NetParams()
# object of class SimConfig to store the simulation configuration
simConfig = specs.SimConfig()


# Simulation parameters
simConfig.duration = 3000  # Duration of the simulation, in ms
simConfig.dt = 0.05  # Internal integration timestep to use
# Seeds for randomizers (connectivity, input stimulation and cell locations)
simConfig.seeds = {'conn': 1, 'stim': 1, 'loc': 1}
simConfig.createNEURONObj = 1  # create HOC objects when instantiating network
# create Python structure (simulator-independent) when instantiating network
simConfig.createPyStruct = 1
simConfig.verbose = 0  # Whether to write diagnostic information on events


# Recording
simConfig.recordCells = []  # list of cells to record from
# 'V':{'sec':'soma','loc':0.5,'var':'v'}}
simConfig.recordTraces = {'V': {'sec': 'soma', 'loc': 0.5, 'var': 'v'}}
#simConfig.recordLFP = [[0.5*netParams.sizeX, 0.215*netParams.sizeY, 0.5*netParams.sizeZ]]

simConfig.recordStim = False  # record spikes of cell stims
# Step size in ms to save data (eg. V traces, LFP, etc)
simConfig.recordStep = 0.1
simConfig.hParams = {'celsius': 34, 'v_init': -80}


# Saving
simConfig.filename = 'M1_ynorm_izhi'  # Set file output name
#simConfig.saveDataInclude = (['simData'])
simConfig.saveFileStep = 3000  # step size in ms to save data to disk
simConfig.savePickle = False  # save to pickle file
simConfig.saveJson = False  # save to json file
simConfig.saveMat = False  # save to mat file
simConfig.saveTxt = False  # save to txt file
simConfig.saveDpk = False  # save to .dpk pickled file
simConfig.saveHDF5 = False  # save to HDF5 file

# Analysis and plotting
simConfig.analysis['plotRaster'] = {'orderInverse': True, 'figSize': (12, 10), 'lw': 0.3, 'markerSize': 6, 'marker': '.', 'dpi': 300, 'saveFig': 'raster.png'}  # Whether or not to plot a raster
simConfig.analysis['plotTraces'] = {'include': [('ITL23', 10), ('ITL23', 1), ('ITL23', 18), ('ITL23', 11), ],'saveFig': 'traces.png'}  # plot recorded traces for this list of cells
simConfig.analysis['plot2Dnet'] = {'showConns': False}
simConfig.analysis['plotLFP'] = False


###############################################################################
# NETWORK PARAMETERS
###############################################################################


# General network parameters
netParams.scale = 1  # Scale factor for number of cells
# x-dimension (horizontal length) size in um
netParams.sizeX = 10*math.sqrt(netParams.scale)
# y-dimension (vertical height or cortical depth) size in um
netParams.sizeY = 10*math.sqrt(netParams.scale)
# z-dimension (horizontal depth) size in um
netParams.sizeZ = 10*math.sqrt(netParams.scale)


# General connectivity parameters
netParams.scaleConnWeight = 0.0003  # Connection weight scale factor
# Connection weight scale factor for NetStims
netParams.scaleConnWeightNetStims = 0.25
netParams.defaultDelay = 2.0  # default conn delay (ms)
netParams.propVelocity = 100.0  # propagation velocity (um/ms)
# length constant (lambda) for connection probability decay (um)
netParams.probLambda = 100.0
netParams.defaultThreshold = 0.0


# print('Select folder containing morphology files')
# #Morphology files obtained from: https://github.com/suny-downstate-medical-center/netpyne/tree/development/examples/M1detailed

# root = tk.Tk()
# root.withdraw()

# morph_folder_path = filedialog.askdirectory()
# root.update()
# print(morph_folder_path)

# sys.path.append(morph_folder_path)
# netParams.loadCellParamsRule(
#     label='IT_L23', fileName=morph_folder_path +'/IT2_reduced_cellParams.pkl')
# netParams.loadCellParamsRule(
#     label='PV_L23', fileName= morph_folder_path + '/PV_simple_cellParams.pkl')
# copy .pkl to the working directory
netParams.loadCellParamsRule(
    label='IT_L23', fileName='IT2_reduced_cellParams.pkl')
netParams.loadCellParamsRule(
    label='PV_L23', fileName='PV_simple_cellParams.pkl')


# #Importing cell params and adding extracellular mechanism
netParams.cellParams['IT_L23']['conds'] = {'cellType': 'IT_L23'}
ITL23seg = []
ITL23dendlist = []
for secName in netParams.cellParams['IT_L23']['secs']:
    if search('dend', secName):
        ITL23seg.append(secName)
        ITL23dendlist.append(secName)
    if search('soma', secName):
        ITL23seg.append(secName)
    if search('axon', secName):
        ITL23seg.append(secName)
for segname in ITL23seg:
    netParams.cellParams['IT_L23']['secs'][segname]['mechs']['extracellular'] = {
    }
netParams.cellParams['IT_L23']['conds']['ynorm'] = [0, 1]


netParams.cellParams['PV_L23']['conds'] = {'cellType': 'PV_L23'}
PVL23seg = []
for secName in netParams.cellParams['PV_L23']['secs']:
    if search('dend', secName):
        PVL23seg.append(secName)
    if search('soma', secName):
        PVL23seg.append(secName)
    if search('axon', secName):
        PVL23seg.append(secName)
for segname in PVL23seg:
    netParams.cellParams['PV_L23']['secs'][segname]['mechs']['extracellular'] = {
    }
netParams.cellParams['PV_L23']['conds']['ynorm'] = [0, 1]


netParams.popParams['ITL23'] = {'cellModel': 'HH_reduced', 'cellType': 'IT_L23', 'numCells': 20}
netParams.popParams['PVL23'] = {'cellModel': 'HH_simple', 'cellType': 'PV_L23', 'numCells': 20}


# Synaptic mechanism parameters
netParams.synMechParams['AMPA'] = {
    'mod': 'MyExp2SynBB', 'tau1': 0.05, 'tau2': 5.3, 'e': 0}  # AMPA
netParams.synMechParams['NMDA'] = {
    'mod': 'MyExp2SynNMDABB', 'tau1NMDA': 15, 'tau2NMDA': 150, 'e': 0}  # NMDA
netParams.synMechParams['exc'] = {
    'mod': 'Exp2Syn', 'tau1': 0.05, 'tau2': 5.3, 'e': 0}
netParams.synMechParams['GABAB'] = {
    'mod': 'MyExp2SynBB', 'tau1': 3.5,    'tau2': 260.9,  'e': -93}
netParams.synMechParams['GABAA'] = {
    'mod': 'MyExp2SynBB', 'tau1': 0.07,   'tau2': 18.2,   'e': -80}
netParams.synMechParams['GABAASlow'] = {
    'mod': 'MyExp2SynBB', 'tau1': 2,      'tau2': 100,    'e': -80}
netParams.synMechParams['GABAASlowSlow'] = {
    'mod': 'MyExp2SynBB', 'tau1': 200,    'tau2': 400,    'e': -80}

ESynMech = ['AMPA', 'NMDA']
SOMESynMech = ['GABAASlow', 'GABAB']
SOMISynMech = ['GABAASlow']
PVSynMech = ['GABAA']


# Stimulation parameters
netParams.stimSourceParams['background_E'] = {
    'type': 'NetStim',  'rate': 20, 'noise': 1.0}  # background inputs to Exc
netParams.stimSourceParams['background_I'] = {
    'type': 'NetStim',  'rate': 10, 'noise': 1.0}  # background inputs to Inh

netParams.stimTargetParams['bgE->IT'] = {'source': 'background_E', 'conds': {'pop': ['ITL23']},
                                         'sec': ITL23seg, 'allSegs': True, 'synMech': ESynMech, 'weight': 0.1, 'delay': '2+normal(5,3)'}

netParams.stimTargetParams['bgI->PV'] = {'source': 'background_E', 'conds': {'pop': ['PVL23']},
                                         'sec': PVL23seg, 'allSegs': True, 'synMech': ESynMech, 'weight': 0.05, 'delay': '2+normal(5,3)'}

## 'allSegs': True ??? I don't know what is this


# List of connectivity rules/params
netParams.ItoIweight = 0.5

netParams.addConnParams(None, {'preConds': {'cellType': ['IT_L23']},
                                'postConds': {'cellType': 'IT_L23'},
                                'synMech': ESynMech,
                                'probability': 0.1,
                                'weight': 0.2,
                                'synMechWeightFactor': [0.5, 0.5],
                                'delay': 0})


netParams.addConnParams(None, {'preConds': {'cellType': 'IT_L23'},
                                'postConds': {'cellType': 'PV_L23'},
                                'synMech': ESynMech,
                                'probability': 0.1,
                                'synMechWeightFactor': [0.5, 0.5],
                                'weight': 0.2,
                                'delay': 0})


netParams.addConnParams(None, {'preConds': {'cellType': 'PV_L23'},
                                'postConds': {'ynorm': [0, 1]},
                                'synMech': PVSynMech,
                                'probability': '1.0 * exp(-dist_3D/probLambda)',
                                'weight': 'ItoIweight',
                                'delay': 0})


# Dictionary of annotations
netParams.annots = {}


sim.create(netParams=netParams)
sim.net.defineCellShapes()

# The parameters of the extracellular point current source
acs_params = {'position': [0, 0, 0.],  # um
              'amp': 1.,  # uA,
              'stimstart': 1000,  # ms
              'stimend': 2000,  # ms
              'frequency': 10,  # Hz
              'sigma': 0.3  # decay constant
              }


def insert_v_ext(cell, v_ext, t_ext):

    cell.t_ext = neuron.h.Vector(t_ext)
    cell.v_ext = []
    for v in v_ext:
        cell.v_ext.append(neuron.h.Vector(v))

    # play v_ext into e_extracellular reference
    i = 0
    cell.v_ext[i].play(cell.secs['soma']['hObj'](0.5)._ref_e_extracellular, cell.t_ext)


def make_extracellular_stimuli(acs_params, cell):
    """ Function to calculate and apply external potential """
    x0, y0, z0 = acs_params['position']
    ext_field = np.vectorize(lambda x, y, z: 1 / (4 * np.pi * acs_params['sigma'] * np.sqrt((x0 - x)**2 + (y0 - y)**2 + (z0 - z)**2)))

    stimstart = acs_params['stimstart']
    stimend = acs_params['stimend']
    stimdif = stimend-stimstart

    # MAKING THE EXTERNAL FIELD
    n_tsteps = int(stimdif / simConfig.dt + 1)
    n_start = int(stimstart/simConfig.dt)
    n_end = int(stimend/simConfig.dt + 1)
    t = np.arange(start=n_start, stop=n_end) * simConfig.dt
    pulse = acs_params['amp'] * 1000. * \
        np.sin(2 * np.pi * acs_params['frequency'] * t / 1000)

    #v_cell_ext = np.zeros((cell.secs['soma']['hObj'].nseg, n_tsteps))
    v_cell_ext = np.zeros((1, n_tsteps))
    #v_cell_ext[:, :] = ext_field(cell.getSomaPos()[0], cell.getSomaPos()[1], cell.getSomaPos()[2]).reshape(cell.secs['soma']['hObj'].nseg, 1) * pulse.reshape(1, n_tsteps)
    v_cell_ext[:, :] = ext_field(cell.getSomaPos()[0], cell.getSomaPos()[1], cell.getSomaPos()[2]).reshape(1, 1) * pulse.reshape(1, n_tsteps)
    insert_v_ext(cell, v_cell_ext, t)

    return ext_field, pulse

#Add extracellular stim
for c in range(len(sim.net.cells)):
    ext_field, pulse = make_extracellular_stimuli(acs_params, sim.net.cells[c])


sim.simulate()
sim.analyze()
Reply all
Reply to author
Forward
0 new messages