Exemple #1
0
class RunSimulate(object):
    _log = logging.getLogger('allensdk.model.biophysical.run_simulate')

    def __init__(self, input_json, output_json):
        self.input_json = input_json
        self.output_json = output_json
        self.app_config = None
        self.manifest = None

    def load_manifest(self):
        self.app_config = Config().load(self.input_json)
        self.manifest = self.app_config.manifest
        fix_sections = ['passive', 'axon_morph,', 'conditions', 'fitting']
        self.app_config.fix_unary_sections(fix_sections)

    def nrnivmodl(self):
        RunSimulate._log.debug("nrnivmodl")

        subprocess.call(['nrnivmodl', './modfiles'])

    def simulate(self):
        from allensdk.internal.api.queries.biophysical_module_reader \
            import BiophysicalModuleReader

        self.load_manifest()

        try:
            stimulus_path = self.manifest.get_path('stimulus_path')
            RunSimulate._log.info("stimulus path: %s" % (stimulus_path))
        except:
            raise Exception(
                'Could not read input stimulus path from input config.')

        try:
            out_path = self.manifest.get_path('output_path')
            RunSimulate._log.info("result NWB file: %s" % (out_path))
        except:
            raise Exception('Could not read output path from input config.')

        try:
            morphology_path = self.manifest.get_path('MORPHOLOGY')
            RunSimulate._log.info("morphology path: %s" % (morphology_path))
        except:
            raise Exception(
                'Could not read morphology path from input config.')

        single_cell.run(self.app_config)

        lims_upload_config = BiophysicalModuleReader()
        lims_upload_config.read_json(
            self.manifest.get_path('neuronal_model_run_data'))
        lims_upload_config.update_well_known_file(out_path)
        lims_upload_config.set_workflow_state('passed')
        lims_upload_config.write_file(self.output_json)
def main(limit, manifest_path):
    app_config = Config()
    description = app_config.load(manifest_path)

    if 'LOG_CFG' in os.environ:
        log_config = os.environ['LOG_CFG']
    else:
        log_config = resource_filename('allensdk.model.biophysical',
                                       'logging.conf')
        os.environ['LOG_CFG'] = log_config
    lc.fileConfig(log_config)

    run_passive_fit(description)
def main():
    import sys

    manifest_path = sys.argv[-1]
    limit = float(sys.argv[-2])
    os.chdir(os.path.dirname(manifest_path))
    app_config = Config()
    description = app_config.load(manifest_path)

    upfile = description.manifest.get_path('upfile')
    up_data = np.loadtxt(upfile)
    downfile = description.manifest.get_path('downfile')
    down_data = np.loadtxt(downfile)
    swc_path = description.manifest.get_path('MORPHOLOGY')
    output_file = description.manifest.get_path('fit_2_file')

    data = neuron_passive_fit2(up_data, down_data, swc_path, limit)

    json_utilities.write(output_file, data)
Exemple #4
0
def choose_bps_command(command='bps_simple', conf_file=None):
    log = logging.getLogger('allensdk.model.biophys_sim.bps_command')
    
    log.info("bps command: %s" % (command))
    
    if conf_file:
        conf_file = os.path.abspath(conf_file)
    
    if command == 'help':
        print Config().argparser.parse_args(['--help'])
    elif command == 'nrnivmodl':
        sp.call(['nrnivmodl', 'modfiles']) # TODO: alternate location in manifest?
    elif command == 'run_simple':
        app_config = Config()
        description = app_config.load(conf_file)
        sys.path.insert(1, description.manifest.get_path('CODE_DIR'))
        (module_name, function_name) = description.data['runs'][0]['main'].split('#')
        run_module(description, module_name, function_name)
    else:
        raise Exception("unknown command %s" %(command))
Exemple #5
0
def load_description(manifest_json_path):
    '''Read configuration file.
    
    Parameters
    ----------
    manifest_json_path : string
        File containing the experiment configuration.
    
    Returns
    -------
    Config
        Object with all information needed to run the experiment.
    '''
    description = Config().load(manifest_json_path)
    
    # fix nonstandard description sections
    fix_sections = ['passive', 'axon_morph,', 'conditions', 'fitting']
    description.fix_unary_sections(fix_sections)
    
    return description
Exemple #6
0
def load_description(manifest_json_path):
    '''Read configuration file.
    
    Parameters
    ----------
    manifest_json_path : string
        File containing the experiment configuration.
    
    Returns
    -------
    Config
        Object with all information needed to run the experiment.
    '''
    description = Config().load(manifest_json_path)
    
    # fix nonstandard description sections
    fix_sections = ['passive', 'axon_morph,', 'conditions', 'fitting']
    description.fix_unary_sections(fix_sections)
    
    return description
Exemple #7
0
def choose_bps_command(command='bps_simple', conf_file=None):
    log = logging.getLogger('allensdk.model.biophys_sim.bps_command')

    log.info("bps command: %s" % (command))

    if conf_file:
        conf_file = os.path.abspath(conf_file)

    if command == 'help':
        print(Config().argparser.parse_args(['--help']))
    elif command == 'nrnivmodl':
        sp.call(['nrnivmodl', 'modfiles'])
    elif command == 'run_simple':
        app_config = Config()
        description = app_config.load(conf_file)
        sys.path.insert(1, description.manifest.get_path('CODE_DIR'))
        (module_name,
         function_name) = description.data['runs'][0]['main'].split('#')
        run_module(description, module_name, function_name)
    else:
        raise Exception("unknown command %s" % (command))
Exemple #8
0
def simple_config():
    manifest = '''{
        "manifest": [
            { "type": "dir",
              "spec": "MOCK_DOT",
              "key": "BASEDIR"
            }],
        "biophys":
            [{ "hoc": [ "stdgui.hoc"] }]
    }'''

    with patch(builtins.__name__ + ".open", mock_open(read_data=manifest)):
        config = Config().load('config.json', False)

    return config
def simple_config():
    manifest = {
        'manifest': [{
            'type': 'dir',
            'spec': 'MOCK_DOT',
            'key': 'BASEDIR'
        }],
        'biophys': [{
            'hoc': ['stdgui.hoc']
        }],
    }

    ju = JsonComments
    ju.read_file = MagicMock(return_value=manifest)

    config = Config().load('config.json', False)

    return config
Exemple #10
0
    def init_model(self):
        '''
        Retrieves the model information for self.model_id, and initializes the modeling and visualization data.
        '''
        # Retrieve the model data
        try:
            self.bp.cache_data(self.model_id, working_directory=self.model_dir)
        except:
            print "Could not access biophysical data for model " + str(
                self.model_id)
            print "Please confirm model ID and try again."
            quit()

        # Compile the mod files for the current model
        curr_dir = os.getcwd()
        os.chdir(self.model_dir)
        self.__compile_modfiles()

        # set up the NEURON environment using the manifest file
        description = Config().load('manifest.json')

        # Specify model type (perisomatic/all-active)
        currType = description.data['biophys'][0]['model_type']

        # initialize the Hoc interpreter we'll use to affect the model
        utils = create_utils(description, model_type=currType)

        # configure the model for NEURON
        morphologyLoc = description.manifest.get_path('MORPHOLOGY')
        utils.generate_morphology(morphologyLoc.encode('ascii', 'ignore'))
        utils.load_cell_parameters()

        # store the hoc interpreter and cell information in the object instance
        self.h = utils.h
        self.utils = utils
        self.description = description

        # set up a monitor to log simulation time
        self.section_output["t"] = self.h.Vector()
        self.section_output["t"].record(self.h._ref_t)

        # return to the previous directory
        os.chdir(curr_dir)
def test_biophysical():
    neuronal_model_id = 472451419    # get this from the web site

    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)
    os.system('nrnivmodl modfiles')

    description = Config().load('manifest.json')
    utils = Utils(description)
    h = utils.h

    manifest = description.manifest
    morphology_path = manifest.get_path('MORPHOLOGY')
    utils.generate_morphology(morphology_path.encode('ascii', 'ignore'))
    utils.load_cell_parameters()

    stim = h.IClamp(h.soma[0](0.5))
    stim.amp = 0.18
    stim.delay = 1000.0
    stim.dur = 1000.0

    h.tstop = 3000.0

    vec = utils.record_values()

    h.finitialize()
    h.run()

    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

    DatUtilities.save_voltage(output_path, output_data, output_times)
    
    assert numpy.count_nonzero(output_data) > 0
def test_biophysical_peri():
    """
    Test for backward compatibility of the perisomatic models
    """

    subprocess.check_call(['nrnivmodl', 'modfiles/'])

    description = Config().load('manifest.json')
    utils = Utils(description)
    h = utils.h

    manifest = description.manifest
    morphology_path = manifest.get_path('MORPHOLOGY')
    utils.generate_morphology(
        morphology_path.encode('ascii', 'ignore').decode("utf-8"))
    utils.load_cell_parameters()

    stim = h.IClamp(h.soma[0](0.5))
    stim.amp = 0.35  # Sweep 47
    stim.delay = 1000.0
    stim.dur = 1000.0

    h.tstop = 3000.0

    vec = utils.record_values()

    h.finitialize()
    h.run()

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

    output_data = (numpy.array(vec['v']) - junction_potential)  # in mV
    output_times = numpy.array(vec['t']) * ms  # in s
    output_path = 'output_voltage.dat'

    DatUtilities.save_voltage(output_path, output_data, output_times)

    num_spikes = len(
        ephys_features.detect_putative_spikes(output_data, output_times))
    assert num_spikes == 27  # taken from the web app
Exemple #13
0
def test_simulate(hoc_init, mock_h, run_simulate):
    # import allensdk.eclipse_debug

    mock_utils = Mock(name='mock_utils', h=mock_h)

    with patch(
            'allensdk.internal.api.queries.biophysical_module_reader.BiophysicalModuleReader',
            MagicMock(name="bio_mod_reader")) as bio_mod_reader:
        with patch('allensdk.model.biophysical.runner.save_nwb',
                   MagicMock(name="save_nwb")) as save_nwb:
            with patch('allensdk.model.biophysical.runner.NwbDataSet',
                       MagicMock(name='nwb_data_set')) as nwb_data_set:
                with patch('allensdk.model.biophysical.runner.copy',
                           MagicMock(name='shutil_copy')) as cp:
                    with patch('allensdk.model.biophysical.utils.create_utils',
                               return_value=mock_utils) as cu:
                        with patch(builtins.__name__ + ".open",
                                   mock_open(read_data=MANIFEST_JSON)):
                            fit_description = Config().load('manifest.json')
                            Utils.description = fit_description
                            run_simulate.simulate()
def AllenCell(path=None):
    owd = os.getcwd()
    os.chdir(path)
    # sys.path.append('/home/craig_kelley_downstate_edu/allensdk/lib/python3.6/site-packages/')
    from allensdk.model.biophys_sim.config import Config
    from allensdk.model.biophysical import utils as Utils  # this is basically "implied" in the tutorial
    description = Config().load('manifest.json')
    manifest = description.manifest
    morphology_path = description.manifest.get_path('MORPHOLOGY')
    utils = Utils.create_utils(
        description, model_type='Biophysical - all active'
    )  # this is insane - help doc says ALL_ACTIVE_TYPE or PERISOMATIC_TYPE for model_type
    h = utils.h
    utils.generate_morphology(
        morphology_path
    )  # in tutorial, they instead use mophology_path.encode('ascii','ignore')
    utils.load_cell_parameters()
    cell = allenCell()
    cell.generate_cell(h)
    os.chdir(owd)
    return cell
Exemple #15
0
def run_nrn(manifest, current, t_start, t_stop):
    description = Config().load(manifest)

    description.update_data({'axon_type': 'truncated'}, 'biophys')

    fix_sections = ['passive', 'axon_morph,', 'conditions', 'fitting']
    description.fix_unary_sections(fix_sections)

    utils = create_utils(description)
    hoc = utils.h

    manifest = description.manifest
    morphology_path = manifest.get_path('MORPHOLOGY').encode(
        'ascii', 'ignore').decode("utf-8")
    stimulus_path = description.manifest.get_path('stimulus_path')

    utils.generate_morphology(morphology_path)
    utils.load_cell_parameters()
    utils.read_stimulus(stimulus_path, sweep=35)  # needed for sampling rates

    stim = hoc.IClamp(hoc.soma[0](0.5))
    stim.amp = current
    stim.delay = t_start
    stim.dur = t_stop - t_start

    simulation_dt = 1.0e3 / utils.simulation_sampling_rate

    hoc.dt = simulation_dt
    hoc.tstop = t_start + t_stop

    vec = utils.record_values()
    hoc.finitialize()
    hoc.run()

    recorded_data = utils.get_recorded_data(vec)

    vs = recorded_data['v']
    ts = recorded_data['t']

    return ts, vs
Exemple #16
0
 def load_manifest(self):
     self.app_config = Config().load(self.input_json)
     self.manifest = self.app_config.manifest
     self.data_set = NwbDataSet(self.manifest.get_path('stimulus_path'))
Exemple #17
0
def run_simulation(config_f_name):
  h('starttime = startsw()')
  config = Config().load(config_f_name)   # read configuration for the model
  utils_obj = Utils(config)   # Instantiate an object of a class Utils which configures NEURON and provides the interface to the necessary functions to set up the simulation.


  cells_db = utils_obj.load_cell_db() # Load the information about individual cells.

  utils_obj.set_run_params()  # set h.dt and h.tsop


  workdir_n = str(utils_obj.description.data['biophys'][0]['output_dir'])
  if (int(pc.id()) == 0):
    if not os.path.exists(workdir_n):
      os.mkdir(workdir_n)
    print 'Workdir: %s.' % workdir_n
    print ''

  pc.barrier() # Wait for all hosts to get to this point

  instantiate_cells_and_cell_types(cells_db)

  mkcells(cells_db, utils_obj)
  pc.barrier()

  utils_obj.load_syn_data() # Load synaptic parameters (for types of sources and targets).
  pc.barrier()

  if ('connections' in utils_obj.description.data.keys()):
    con_path = utils_obj.description.data['connections']
    connectcells(con_path, cells_db, utils_obj)
  pc.barrier()

  # Go over all source of external inputs and instantiate them for the appropriate cells.
  for ext_inp_path in reversed(utils_obj.description.data['ext_inputs'].keys()):
    if not os.path.exists(ext_inp_path):
      print "Error: the external inputs file %s does not exist; exiting." % (ext_inp_path)
      h.finish()  

    ext_inp_map = pd.read_csv(utils_obj.description.data['ext_inputs'][ext_inp_path]['map'], sep=' ')
    for tar_gid in cells:
      if pc.gid_exists(tar_gid):
        tar_ext_inp_map = ext_inp_map[ext_inp_map['index'] == tar_gid]
        external_inputs(tar_gid, ext_inp_path, tar_ext_inp_map, utils_obj.description.data['ext_inputs'][ext_inp_path], h.tstop, utils_obj)

  pc.barrier() # wait for all hosts to get to this point

  h.load_file('mkstim_SEClamp.hoc')
  SEClamp_gids = []
  if (utils_obj.description.data['cell_data_tracking']['SEClamp_insert'] == 'yes'):
    SEClamp_insert_first_cell = utils_obj.description.data['cell_data_tracking']['SEClamp_insert_first_cell']
    for gid in cells:
      if ((gid >= SEClamp_insert_first_cell) and ((gid - SEClamp_insert_first_cell) % utils_obj.description.data['cell_data_tracking']['SEClamp_insert_cell_gid_step'] == 0) and ( cell_types[type_index(gid)] not in ['LIF_exc', 'LIF_inh'])):
        tmp_line  = str(pc.gid2cell(gid).hname()) + '.soma[0]'
        h.mkstim_SEClamp(tmp_line, 0.5, -70.0, h.tstop)
        SEClamp_gids.append(gid)

  pc.barrier() # wait for all hosts to get to this point


  h.load_file("save_t_series.hoc")
  save_value_ID_list = []
  k = 0
  if (utils_obj.description.data['cell_data_tracking']['do_save_t_series'] == 'yes'):
    for cell_gid in cells:
      if ((cell_gid % utils_obj.description.data['cell_data_tracking']['id_step_save_t_series'] == 0) and (cell_types[type_index(cell_gid)] not in ['LIF_exc', 'LIF_inh'])):
        save_value  = str(pc.gid2cell(cell_gid).hname()) + '.soma[0].v(0)'
        save_value_ID_list.append(k)
        k += 1
        h.save_t_series_prep(save_value)
  pc.barrier() # wait for all hosts to get to this point


  h.load_file("spikefile.hoc")
  h.record_spikes(cell_displ[-1])
  pc.barrier() # wait for all hosts to get to this point

  pc.timeout(0)
  #h.cvode.debug_event(1)
  h.load_file('progress.hoc')
  fih = h.FInitializeHandler(2, "progress(0)")
  h.cvode.cache_efficient(1)
  pc.barrier() # wait for all hosts to get to this point

  h.prun(h.tstop)
  pc.barrier() # wait for all hosts to get to this point

  h.spike2file('%s/spk.dat' % (workdir_n))
  pc.barrier() # wait for all hosts to get to this point

  # Save t series values to files.
  k = 0
  if (utils_obj.description.data['cell_data_tracking']['do_save_t_series'] == 'yes'):
    for cell_gid in cells:
      if ((cell_gid % utils_obj.description.data['cell_data_tracking']['id_step_save_t_series'] == 0) and (cell_types[type_index(cell_gid)] not in ['LIF_exc', 'LIF_inh'])):
        tmp_value_list = []
        save_value_out_file_n = '%s/v_out-cell-%d.h5' % (workdir_n, cell_gid)
        for i_tmp in xrange(int(h.rec_value_vec_list[save_value_ID_list[k]].size())):
          tmp_value_list.append( h.rec_value_vec_list[save_value_ID_list[k]][i_tmp] )
        dt = h.rec_t_vec_list[save_value_ID_list[k]][1] - h.rec_t_vec_list[save_value_ID_list[k]][0]
        h5 = h5py.File(save_value_out_file_n, 'w', libver='latest')
        h5.attrs['dt']=dt
        h5.create_dataset('values',(len(tmp_value_list),),maxshape=(None,),chunks=True)
        h5['values'][0:len(tmp_value_list)] = tmp_value_list
        h5.close()
        k += 1

  # Save currents from SEClamps.
  if (len(h.rec_SEClamp_i_list) > 0):
    k = 0
    for cell_gid in SEClamp_gids:
      if (cell_types[type_index(cell_gid)] in ['LIF_exc', 'LIF_inh']):
        continue
      tmp_value_list = []
      save_value_out_file_n = '%s/i_SEClamp-cell-%d.h5' % (workdir_n, cell_gid)
      for i_tmp in xrange(int(h.rec_SEClamp_i_list[k].size())):
        tmp_value_list.append( h.rec_SEClamp_i_list[k][i_tmp] )
      dt = h.rec_SEClamp_t_list[k][1] - h.rec_SEClamp_t_list[k][0]
      h5 = h5py.File(save_value_out_file_n, 'w', libver='latest')
      h5.attrs['dt']=dt
      h5.create_dataset('values',(len(tmp_value_list),),maxshape=(None,),chunks=True)
      h5['values'][0:len(tmp_value_list)] = tmp_value_list
      h5.close()
      k += 1
  pc.barrier() # wait for all hosts to get to this point

  # Postprocessing.
  if (int(pc.id()) == 0):
    f_rate.tot_f_rate(workdir_n+'/spk.dat', workdir_n+'/tot_f_rate.dat', utils_obj.description.data['postprocessing']['in_t_omit'], (h.tstop - utils_obj.description.data['postprocessing']['post_t_omit']), h.tstop, cell_displ[-1])

  h.finish()
 def __init__(self, description=None):
     description = Config().load('manifest.json')
     super(Utils, self).__init__(description)
     self.generate_cell()
from allensdk.model.biophys_sim.config import Config
from utils import Utils

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np

config = Config().load('config.json')

# configure NEURON
utils = Utils(config)
h = utils.h

# configure model
manifest = config.manifest

utils.generate_cells()
utils.connect_cells()

# configure stimulus
utils.setup_iclamp_step(utils.cells[0], 0.27, 1020.0, 750.0)
h.dt = 0.025
h.tstop = 3000

# configure recording
vec = utils.record_values()

# run the model
h.finitialize()
h.run()
Exemple #20
0
import pylab as plt
import visual as vis

from allensdk.model.biophys_sim.config import Config
from utils import Utils

config = Config().load('config.json')  # read configuration for the model

utils = Utils(
    config
)  # instantiate an object of a class Utils which configures NEURONand provides the interface to the necessary functions to set up the simuation
h = utils.h

db = utils.load_cell_db()

cells = utils.generate_cells(db)  # read cell information from csv file

print "load connectivity:"
con = utils.load_connectivity()

print 'connecting cells:'
[netcons, syns] = utils.connect_cell(cells, db, con)
print '# of netcons, syns: ', len(netcons), len(syns)

utils.set_run_params()  # set h.dt and h.tsop

print "setting stimulus:"
stims = utils.setIClamps(cells)

print "set recordings"
rec_vecs = utils.record_values(cells)
Exemple #21
0
 def load_manifest(self):
     self.app_config = Config().load(self.input_json)
     self.manifest = self.app_config.manifest
     fix_sections = ['passive', 'axon_morph,', 'conditions', 'fitting']
     self.app_config.fix_unary_sections(fix_sections)