Exemple #1
0
def stimx_waveform_factory(waveform):
    """
    Factory to create correct waveform class based on conf.
    Supports json config in conf as well as string pointer to a file.
    :rtype: BaseWaveformType
    """
    if isinstance(waveform, (str, unicode)):
        # if waveform_conf is str or unicode assume to be name of file in stim_dir
        # waveform_conf = str(waveform_conf)   # make consistent
        file_ext = os.path.splitext(waveform)
        if file_ext == 'csv':
            return WaveformCustom(waveform)

        elif file_ext == 'json':
            with open(waveform, 'r') as f:
                waveform = json.load(f)
        else:
            print "Warning: unknwon filetype for waveform"

    shape_key = waveform["shape"].lower()

    if shape_key not in shape_classes:
        io.log_warning("Waveform shape not known")  # throw error?

    Constructor = shape_classes[shape_key]
    return Constructor(waveform)
Exemple #2
0
def iclamp_waveform_factory(conf):
    """
    Factory to create correct waveform class based on conf.
    Supports json config in conf as well as string pointer to a file.
    :rtype: BaseWaveformType
    """
    iclamp_waveform_conf = conf["iclamp"]

    shape_key = iclamp_waveform_conf["shape"].lower()

    if shape_key not in shape_classes:
        io.log_warning('iclamp waveform shape not known')  # throw error?

    Constructor = shape_classes[shape_key]
    return Constructor(iclamp_waveform_conf)
Exemple #3
0
    def _set_connections(self, edge_prop, src_node, syn_weight, stim=None):
        tar_seg_ix, tar_seg_prob = self._morph.get_target_segments(edge_prop)
        src_gid = src_node.node_id
        nsyns = edge_prop.nsyns

        if len(tar_seg_ix) == 0:
            msg = 'Could not find target synaptic location for edge-type {}, Please check target_section and/or distance_range properties'.format(edge_prop.edge_type_id)
            io.log_warning(msg, all_ranks=True, display_once=True)
            return 0

        segs_ix = self.prng.choice(tar_seg_ix, nsyns, p=tar_seg_prob)
        secs = self._secs[segs_ix]  # sections where synapases connect
        xs = self._morph.seg_prop['x'][segs_ix]  # distance along the section where synapse connects, i.e., seg_x

        # TODO: this should be done just once
        synapses = [edge_prop.load_synapses(x, sec) for x, sec in zip(xs, secs)]

        delay = edge_prop['delay']
        self._synapses.extend(synapses)

        # TODO: Don't save this if not needed
        self._edge_type_ids.extend([edge_prop.edge_type_id]*len(synapses))

        for syn in synapses:
            # connect synapses
            if stim:
                nc = h.NetCon(stim.hobj, syn)
            else:
                nc = pc.gid_connect(src_gid, syn)

            nc.weight[0] = syn_weight
            nc.delay = delay
            self.netcons.append(nc)

            self._connections.append(ConnectionStruct(edge_prop, src_node, syn, nc, stim is not None))

        return nsyns
Exemple #4
0
    def from_config(cls, config, network, set_recordings=True):
        # TODO: convert from json to sonata config if necessary

        sim = cls(network=network,
                  dt=config.dt,
                  tstop=config.tstop,
                  v_init=config.v_init,
                  celsius=config.celsius,
                  cao0=config.cao0,
                  optocell=config.optocell,
                  nsteps_block=config.block_step)

        network.io.log_info('Building cells.')
        network.build_nodes()

        network.io.log_info('Building recurrent connections')
        network.build_recurrent_edges()

        # TODO: Need to create a gid selector
        for sim_input in inputs.from_config(config):
            node_set = network.get_node_set(sim_input.node_set)
            if sim_input.input_type == 'spikes':
                spikes = spike_trains.SpikesInput.load(
                    name=sim_input.name,
                    module=sim_input.module,
                    input_type=sim_input.input_type,
                    params=sim_input.params)
                io.log_info('Build virtual cell stimulations for {}'.format(
                    sim_input.name))
                network.add_spike_trains(spikes, node_set)

            elif sim_input.module == 'IClamp':
                # TODO: Parse from csv file
                amplitude = sim_input.params['amp']
                delay = sim_input.params['delay']
                duration = sim_input.params['duration']
                gids = sim_input.params['node_set']
                sim.attach_current_clamp(amplitude, delay, duration, node_set)

            elif sim_input.module == 'xstim':
                sim.add_mod(mods.XStimMod(**sim_input.params))

            else:
                io.log_exception('Can not parse input format {}'.format(
                    sim_input.name))

        if config.calc_ecp:
            for gid, cell in network.cell_type_maps('biophysical').items():
                cell.setup_ecp()
            sim.h.cvode.use_fast_imem(1)

        # Parse the "reports" section of the config and load an associated output module for each report
        sim_reports = reports.from_config(config)
        for report in sim_reports:
            if isinstance(report, reports.SpikesReport):
                mod = mods.SpikesMod(**report.params)

            elif isinstance(report, reports.SectionReport):
                mod = mods.SectionReport(**report.params)

            elif isinstance(report, reports.MembraneReport):
                if report.params['sections'] == 'soma':
                    mod = mods.SomaReport(**report.params)

                else:
                    mod = mods.MembraneReport(**report.params)

            elif isinstance(report, reports.ECPReport):
                assert config.calc_ecp
                mod = mods.EcpMod(**report.params)
                # Set up the ability for ecp on all relevant cells
                # TODO: According to spec we need to allow a different subset other than only biophysical cells
                # for gid, cell in network.cell_type_maps('biophysical').items():
                #     cell.setup_ecp()

            elif report.module == 'save_synapses':
                mod = mods.SaveSynapses(**report.params)

            else:
                # TODO: Allow users to register customized modules using pymodules
                io.log_warning('Unrecognized module {}, skipping.'.format(
                    report.module))
                continue

            sim.add_mod(mod)

        return sim
    def from_config(cls, config, network, set_recordings=True):
        # TODO: convert from json to sonata config if necessary

        #The network must be built before initializing the simulator because
        #gap junctions must be set up before the simulation is initialized.
        network.io.log_info('Building cells.')
        network.build_nodes()

        network.io.log_info('Building recurrent connections')
        network.build_recurrent_edges()

        sim = cls(network=network,
                  dt=config.dt,
                  tstop=config.tstop,
                  v_init=config.v_init,
                  celsius=config.celsius,
                  nsteps_block=config.block_step)

        # TODO: Need to create a gid selector
        for sim_input in inputs.from_config(config):
            try:
                network.get_node_set(sim_input.node_set)
            except:
                print(
                    "Parameter node_set must be given in inputs module of simulation_config file. If unsure of what node_set should be, set it to 'all'."
                )
            node_set = network.get_node_set(sim_input.node_set)
            if sim_input.input_type == 'spikes':
                io.log_info('Building virtual cell stimulations for {}'.format(
                    sim_input.name))
                path = sim_input.params['input_file']
                spikes = SpikeTrains.load(path=path,
                                          file_type=sim_input.module,
                                          **sim_input.params)
                network.add_spike_trains(spikes, node_set)

            elif sim_input.module == "FileIClamp":
                sim.attach_file_current_clamp(sim_input.params["input_file"])

            elif sim_input.module == 'IClamp':
                # TODO: Parse from csv file
                try:
                    len(sim_input.params['amp'])
                except:
                    sim_input.params['amp'] = [float(sim_input.params['amp'])]
                if len(sim_input.params['amp']) > 1:
                    sim_input.params['amp'] = [
                        float(i) for i in sim_input.params['amp']
                    ]

                try:
                    len(sim_input.params['delay'])
                except:
                    sim_input.params['delay'] = [
                        float(sim_input.params['delay'])
                    ]
                if len(sim_input.params['delay']) > 1:
                    sim_input.params['delay'] = [
                        float(i) for i in sim_input.params['delay']
                    ]

                try:
                    len(sim_input.params['duration'])
                except:
                    sim_input.params['duration'] = [
                        float(sim_input.params['duration'])
                    ]
                if len(sim_input.params['duration']) > 1:
                    sim_input.params['duration'] = [
                        float(i) for i in sim_input.params['duration']
                    ]

                amplitude = sim_input.params['amp']
                delay = sim_input.params['delay']
                duration = sim_input.params['duration']

                try:
                    sim_input.params['gids']
                except:
                    sim_input.params['gids'] = None
                if sim_input.params['gids'] is not None:
                    gids = sim_input.params['gids']
                else:
                    gids = list(node_set.gids())

                sim.attach_current_clamp(amplitude, delay, duration, gids)

            elif sim_input.module == "SEClamp":
                try:
                    len(sim_input.params['amps'])
                except:
                    sim_input.params['amps'] = [
                        float(sim_input.params['amps'])
                    ]

                try:
                    len(sim_input.params['durations'])
                except:
                    sim_input.params['durations'] = [
                        float(sim_input.params['durations'])
                    ]

                amplitudes = sim_input.params['amps']
                durations = sim_input.params['durations']
                rs = None

                if "rs" in sim_input.params.keys():
                    try:
                        len(sim_input.params['rs'])
                    except:
                        sim_input.params['rs'] = [
                            float(sim_input.params['rs'])
                        ]
                    if len(sim_input.params['rs']) > 1:
                        sim_input.params['rs'] = [
                            float(i) for i in sim_input.params['rs']
                        ]
                    rs = sim_input.params["rs"]

                try:
                    sim_input.params['gids']
                except:
                    sim_input.params['gids'] = None
                if sim_input.params['gids'] is not None:
                    gids = sim_input.params['gids']
                else:
                    gids = list(node_set.gids())

                sim.attach_se_voltage_clamp(amplitudes, durations, gids, rs)

            elif sim_input.module == 'xstim':
                sim.add_mod(mods.XStimMod(**sim_input.params))

            else:
                io.log_exception('Can not parse input format {}'.format(
                    sim_input.name))

        # Parse the "reports" section of the config and load an associated output module for each report
        sim_reports = reports.from_config(config)
        for report in sim_reports:
            if isinstance(report, reports.SpikesReport):
                mod = mods.SpikesMod(**report.params)

            elif report.module == 'netcon_report':
                mod = mods.NetconReport(**report.params)

            elif isinstance(report, reports.MembraneReport):
                if report.params['sections'] == 'soma':
                    mod = mods.SomaReport(**report.params)

                else:
                    mod = mods.MembraneReport(**report.params)
            elif isinstance(report, reports.ClampReport):
                mod = mods.ClampReport(**report.params)

            elif isinstance(report, reports.ECPReport):
                mod = mods.EcpMod(**report.params)
                # Set up the ability for ecp on all relevant cells
                # TODO: According to spec we need to allow a different subset other than only biophysical cells
                for gid, cell in network.cell_type_maps('biophysical').items():
                    cell.setup_ecp()

            elif report.module == 'save_synapses':
                mod = mods.SaveSynapses(**report.params)

            else:
                # TODO: Allow users to register customized modules using pymodules
                io.log_warning('Unrecognized module {}, skipping.'.format(
                    report.module))
                continue

            sim.add_mod(mod)

        return sim
Exemple #6
0
def set_params_allactive(hobj, params_dict):
    # params_dict = json.load(open(params_file_name, 'r'))
    passive = params_dict['passive'][0]
    genome = params_dict['genome']
    conditions = params_dict['conditions'][0]

    section_map = {}
    for sec in hobj.all:
        section_name = sec.name().split(".")[1][:4]
        if section_name in section_map:
            section_map[section_name].append(sec)
        else:
            section_map[section_name] = [sec]

    for sec in hobj.all:
        sec.insert('pas')
        # sec.insert('extracellular')

    if 'e_pas' in passive:
        e_pas_val = passive['e_pas']
        for sec in hobj.all:
            for seg in sec:
                seg.pas.e = e_pas_val

    if 'ra' in passive:
        ra_val = passive['ra']
        for sec in hobj.all:
            sec.Ra = ra_val

    if 'cm' in passive:
        # print('Setting cm')
        for cm_dict in passive['cm']:
            cm = cm_dict['cm']
            for sec in section_map.get(cm_dict['section'], []):
                sec.cm = cm

    for genome_dict in genome:
        g_section = genome_dict['section']
        if genome_dict['section'] == 'glob':
            io.log_warning(
                "There is a section called glob, probably old json file")
            continue

        g_value = float(genome_dict['value'])
        g_name = genome_dict['name']
        g_mechanism = genome_dict.get("mechanism", "")
        for sec in section_map.get(g_section, []):
            if g_mechanism != "":
                sec.insert(g_mechanism)
            setattr(sec, g_name, g_value)

    for erev in conditions['erev']:
        erev_section = erev['section']
        erev_ena = erev['ena']
        erev_ek = erev['ek']

        if erev_section in section_map:
            for sec in section_map.get(erev_section, []):
                if h.ismembrane('k_ion', sec=sec) == 1:
                    setattr(sec, 'ek', erev_ek)
                if h.ismembrane('na_ion', sec=sec) == 1:
                    setattr(sec, 'ena', erev_ena)
        else:
            io.log_warning(
                "Can't set erev for {}, section array doesn't exist".format(
                    erev_section))
Exemple #7
0
    def from_config(cls, config, network, set_recordings=True):
        simulation_inputs = inputs.from_config(config)

        # Special case for setting synapses to spontaneously (for a given set of pre-synaptic cell-types). Using this
        # input will change the way the network builds cells/connections and thus needs to be set first.
        for sim_input in simulation_inputs:
            if sim_input.input_type == 'syn_activity':
                network.set_spont_syn_activity(
                    precell_filter=sim_input.params['precell_filter'],
                    timestamps=sim_input.params['timestamps']
                )

        # The network must be built before initializing the simulator because
        # gap junctions must be set up before the simulation is initialized.
        network.io.log_info('Building cells.')
        network.build_nodes()

        network.io.log_info('Building recurrent connections')
        network.build_recurrent_edges()

        sim = cls(network=network,
                  dt=config.dt,
                  tstop=config.tstop,
                  v_init=config.v_init,
                  celsius=config.celsius,
                  nsteps_block=config.block_step)

        # TODO: Need to create a gid selector
        for sim_input in inputs.from_config(config):
            try:
                network.get_node_set(sim_input.node_set)
            except:
                print("Parameter node_set must be given in inputs module of simulation_config file. If unsure of what node_set should be, set it to 'all'.")
            node_set = network.get_node_set(sim_input.node_set)
            if sim_input.input_type == 'spikes':
                io.log_info('Building virtual cell stimulations for {}'.format(sim_input.name))
                path = sim_input.params['input_file']
                spikes = SpikeTrains.load(path=path, file_type=sim_input.module, **sim_input.params)
                network.add_spike_trains(spikes, node_set)

            elif sim_input.module == "FileIClamp":
                sim.attach_file_current_clamp(sim_input.params["input_file"])

            elif sim_input.module == 'IClamp':
                # TODO: Parse from csv file
                try: 
                    len(sim_input.params['amp'])
                except:
                    sim_input.params['amp']=[float(sim_input.params['amp'])]
                if len(sim_input.params['amp'])>1:
                    sim_input.params['amp']=[float(i) for i in sim_input.params['amp']]

                try: 
                    len(sim_input.params['delay'])
                except:
                    sim_input.params['delay']=[float(sim_input.params['delay'])]
                if len(sim_input.params['delay'])>1:
                    sim_input.params['delay']=[float(i) for i in sim_input.params['delay']]
                
                try: 
                    len(sim_input.params['duration'])
                except:
                    sim_input.params['duration']=[float(sim_input.params['duration'])]
                if len(sim_input.params['duration'])>1:
                    sim_input.params['duration']=[float(i) for i in sim_input.params['duration']]
                    
                amplitude = sim_input.params['amp']
                delay = sim_input.params['delay']
                duration = sim_input.params['duration']

                # specificed for location to place iclamp hobj.<section_name>[<section_index>](<section_dist>). The
                # default is hobj.soma[0](0.5), the center of the soma
                section_name = sim_input.params.get('section_name', 'soma')
                section_index = sim_input.params.get('section_index', 0)
                section_dist = sim_input.params.get('section_dist', 0.5)

                # section_name = section_name if isinstance(section_name, (list, tuple)) else [section_name]
                # section_index = section_index if isinstance(section_index, (list, tuple)) else [section_index]
                # section_dist = section_dist if isinstance(section_dist, (list, tuple)) else [section_dist]

                try:
                    sim_input.params['gids']
                except:
                    sim_input.params['gids'] = None
                if sim_input.params['gids'] is not None:
                    gids = sim_input.params['gids']
                else:
                    gids = list(node_set.gids())

                sim.attach_current_clamp(amplitude, delay, duration, gids, section_name, section_index, section_dist)

            elif sim_input.module == "SEClamp":
                try: 
                    len(sim_input.params['amps'])
                except:
                    sim_input.params['amps']=[float(sim_input.params['amps'])]
                
                try: 
                    len(sim_input.params['durations'])
                except:
                    sim_input.params['durations']=[float(sim_input.params['durations'])]
                    
                amplitudes = sim_input.params['amps']
                durations = sim_input.params['durations']
                rs = None

                if "rs" in sim_input.params.keys():
                    try: 
                        len(sim_input.params['rs'])
                    except:
                        sim_input.params['rs']=[float(sim_input.params['rs'])]
                    if len(sim_input.params['rs'])>1:
                        sim_input.params['rs']=[float(i) for i in sim_input.params['rs']]
                    rs = sim_input.params["rs"]
                                   
                try:
                    sim_input.params['gids']
                except:
                    sim_input.params['gids'] = None
                if sim_input.params['gids'] is not None:
                    gids = sim_input.params['gids']
                else:
                    gids = list(node_set.gids())

                sim.attach_se_voltage_clamp(amplitudes, durations, gids, rs)

            elif sim_input.module == 'xstim':
                sim.add_mod(mods.XStimMod(**sim_input.params))

            elif sim_input.module == 'syn_activity':
                pass

            else:
                io.log_exception('Can not parse input format {}'.format(sim_input.name))

        # Parse the "reports" section of the config and load an associated output module for each report
        sim_reports = reports.from_config(config)
        for report in sim_reports:
            if isinstance(report, reports.SpikesReport):
                mod = mods.SpikesMod(**report.params)

            elif report.module == 'netcon_report':
                mod = mods.NetconReport(**report.params)

            elif isinstance(report, reports.MembraneReport):
                if report.params['sections'] == 'soma':
                    mod = mods.SomaReport(**report.params)

                else:
                    mod = mods.MembraneReport(**report.params)
            elif isinstance(report, reports.ClampReport):
                mod = mods.ClampReport(**report.params)

            elif isinstance(report, reports.ECPReport):
                mod = mods.EcpMod(**report.params)
                # Set up the ability for ecp on all relevant cells
                # TODO: According to spec we need to allow a different subset other than only biophysical cells
                for gid, cell in network.cell_type_maps('biophysical').items():
                    cell.setup_ecp()

            elif report.module == 'save_synapses':
                mod = mods.SaveSynapses(**report.params)

            else:
                # TODO: Allow users to register customized modules using pymodules
                io.log_warning('Unrecognized module {}, skipping.'.format(report.module))
                continue

            sim.add_mod(mod)

        return sim
Exemple #8
0
    def __init__(self, network_dir, grp_keys):
        self._network_dir = network_dir
        self._grp_keys = list(grp_keys)

        self._edge_counts = {(s, t): 0 for s, t in self._grp_keys}
        self._biophys_edge_count = {(s, t): 0 for s, t in self._grp_keys}
        self._point_edge_count = {(s, t): 0 for s, t in self._grp_keys}

        self._tmp_files = {(s, t): [] for s, t in self._grp_keys}
        for (src_pop, trg_pop), r in product(self._grp_keys, range(N_HOSTS)):
            fname = '.core{}.{}_{}_edges.h5'.format(r, src_pop, trg_pop)
            fpath = os.path.join(self._network_dir, fname)
            if not os.path.exists(fpath):
                io.log_warning('Expected file {} is missing'.format(fpath))

            h5file = h5py.File(fpath, 'r')
            edges_grp = h5file['/edges/{}_{}'.format(src_pop, trg_pop)]
            self._tmp_files[(src_pop, trg_pop)].append(edges_grp)

            self._edge_counts[(src_pop,
                               trg_pop)] += len(edges_grp['source_node_id'])
            self._biophys_edge_count[(src_pop, trg_pop)] += len(
                edges_grp['0/syn_weight'])
            self._point_edge_count[(src_pop,
                                    trg_pop)] += len(edges_grp['1/syn_weight'])

        for (src_pop, trg_pop), in_grps in self._tmp_files.items():
            out_h5 = h5py.File(
                os.path.join(self._network_dir,
                             '{}_{}_edges.h5'.format(src_pop, trg_pop)), 'w')
            add_hdf5_magic(out_h5)
            add_hdf5_version(out_h5)
            pop_root = out_h5.create_group('/edges/{}_{}'.format(
                src_pop, trg_pop))
            n_edges_total = self._edge_counts[(src_pop, trg_pop)]
            n_edges_bio = self._biophys_edge_count[(src_pop, trg_pop)]
            n_edges_point = self._point_edge_count[(src_pop, trg_pop)]

            pop_root.create_dataset('source_node_id', (n_edges_total, ),
                                    dtype=np.uint64)
            pop_root['source_node_id'].attrs['node_population'] = src_pop

            pop_root.create_dataset('target_node_id', (n_edges_total, ),
                                    dtype=np.uint64)
            pop_root['target_node_id'].attrs['node_population'] = trg_pop

            pop_root.create_dataset('edge_group_id', (n_edges_total, ),
                                    dtype=np.uint16)
            pop_root.create_dataset('edge_group_index', (n_edges_total, ),
                                    dtype=np.uint16)
            pop_root.create_dataset('edge_type_id', (n_edges_total, ),
                                    dtype=np.uint32)

            pop_root.create_dataset('0/syn_weight', (n_edges_bio, ),
                                    dtype=np.float)
            pop_root.create_dataset('0/sec_id', (n_edges_bio, ),
                                    dtype=np.uint64)
            pop_root.create_dataset('0/sec_x', (n_edges_bio, ), dtype=np.float)
            pop_root.create_dataset('1/syn_weight', (n_edges_point, ),
                                    dtype=np.float)

            total_offset = 0
            bio_offset = 0
            point_offset = 0
            for grp in in_grps:
                n_ds = len(grp['source_node_id'])
                pop_root['source_node_id'][total_offset:(
                    total_offset + n_ds)] = grp['source_node_id'][()]
                pop_root['target_node_id'][total_offset:(
                    total_offset + n_ds)] = grp['target_node_id'][()]
                pop_root['edge_group_id'][total_offset:(
                    total_offset + n_ds)] = grp['edge_group_id'][()]
                pop_root['edge_group_index'][total_offset:(
                    total_offset + n_ds)] = grp['edge_group_index'][()]
                pop_root['edge_type_id'][total_offset:(
                    total_offset + n_ds)] = grp['edge_type_id'][()]
                total_offset += n_ds

                n_ds = len(grp['0/syn_weight'])
                # print(grp['0/syn_weight'][()])
                pop_root['0/syn_weight'][bio_offset:(
                    bio_offset + n_ds)] = grp['0/syn_weight'][()]
                pop_root['0/sec_id'][bio_offset:(bio_offset +
                                                 n_ds)] = grp['0/sec_id'][()]
                pop_root['0/sec_x'][bio_offset:(bio_offset +
                                                n_ds)] = grp['0/sec_x'][()]
                bio_offset += n_ds

                n_ds = len(grp['1/syn_weight'])
                pop_root['1/syn_weight'][point_offset:(
                    point_offset + n_ds)] = grp['1/syn_weight'][()]
                point_offset += n_ds

                fname = grp.file.filename
                grp.file.close()
                if os.path.exists(fname):
                    os.remove(fname)

            self._create_index(pop_root, index_type='target')
            self._create_index(pop_root, index_type='source')
            out_h5.close()