Download this file in .ipynb format here.

In [2]:
# based on allensdk.model.biophysical.biophysical_perisomatic.runner

# These will be useful for accessing and configuring the downloaded model
from allensdk.model.biophys_sim.config import Config
from allensdk.model.biophysical.utils import Utils

# not using NwbDataSet
# from allensdk.core.nwb_data_set import NwbDataSet

# We'll save results to a simple text file instead
from allensdk.core.dat_utilities import DatUtilities
In [3]:
from allensdk.api.queries.biophysical_api import BiophysicalApi

neuronal_model_id = 472451419    # get this from the web site
#neuronal_model_id = 480361288
model_directory = '.'

bp = BiophysicalApi('http://api.brain-map.org')
bp.cache_stimulus = False # don't want to download the large stimulus NWB file
bp.cache_data(neuronal_model_id, working_directory=model_directory)
In [4]:
import os

os.system('nrnivmodl modfiles')
Out[4]:
0
In [5]:
description = Config().load('manifest.json')
utils = Utils(description)
h = utils.h
In [6]:
# configure model
manifest = description.manifest
morphology_path = description.manifest.get_path('MORPHOLOGY')
utils.generate_morphology(morphology_path.encode('ascii', 'ignore'))
utils.load_cell_parameters()

# At this point the cell model has been fully set up in NEURON
In [12]:
# configure a simple current-clamp stimulus to generate some spikes
stim = h.IClamp(h.soma[0](0.5))
stim.amp = 0.18
stim.delay = 1000.0
stim.dur = 1000.0

h.tstop = 3000.0
In [13]:
vec = utils.record_values()
In [14]:
h.finitialize()
h.run()
Out[14]:
0.0
In [15]:
# save the result to a simple time and voltage space-separated text file
import numpy

output_path = 'output_voltage.dat'

junction_potential = description.data['fitting'][0]['junction_potential']
mV = 1.0e-3
ms = 1.0e-3

output_data = (numpy.array(vec['v']) - junction_potential) * mV
output_times = numpy.array(vec['t']) * ms

data = numpy.transpose(numpy.vstack((output_times, output_data)))
with open (output_path, "w") as f:
    numpy.savetxt(f, data)
In [16]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(vec['t'], numpy.array(vec['v']) - junction_potential)
plt.xlabel('time (ms)')
plt.ylabel('membrane potential (mV)')
plt.show()
In [ ]: