def convert_vm_to_spike_train_from_file(path_to_file="/file/path", theta=0.0):
    """
    Use case: convert_vm_to_spike_train_from_file()
    """
    # ============Extract spikes from Voltage Response==============
    # for each location load the file containing voltage response
    # the file is such that 1st column is time stamps and
    # 2nd column is the corressponding voltages
    # Neo's IrregularlySampledSignal (iss) is used to convert the
    # voltage response into analog signal.
    # Neo's peak_detection (pd) is used to extract the spikes from
    # the analog signal. These times @ spike occurrences are
    # written into a .txt file.
    #
    #signal_sign = [ "above" if np.sign(x)==0 or 0.0 or 1.0 or 1
    #                else "below"
    #                for x in [theta] ][0]
    #
    signal_sign = [
        "above" if np.sign(x) == 0 or 0.0 or 1.0 or 1 else "below"
        for x in [theta]
    ][0]
    data = np.loadtxt(path_to_file)
    column_time = data[:, 0]
    column_volts = data[:, 1]
    # convert voltage response into analog signal and get spikes
    signal = iss(column_time, column_volts, units='mV', time_units='ms')
    spikes = pd(signal,
                threshold=np.array(theta) * mV,
                sign=signal_sign,
                format=None)
    return spikes
Ejemplo n.º 2
0
    def voltage_to_spiketrain(cls, model, recordings):
        """Transforms voltage recordings into spikes.

        **Arguments:**

        +-----------------------+--------------------+
        | Ordered argument      | Value type         |
        +=======================+====================+
        | 1. model instance     | instantiated model |
        +-----------------------+--------------------+
        | 2. response recording | dictionary         |
        +-----------------------+--------------------+

        *NOTE:*

        * the model has the attribute 'regions'
        * recordings is a product after running the model

        **Use case:**

        First we set up as follows

        ``>> sm = SimulationManager()``

        ``>> rm = RecordManager()``

        ``>> rec = {"time": None, "response": None, "stimulus": None}``

        ``>> par = {"dt": 0.1, "celsius": 30, "tstop": 10, "v_init": 65}``

        ``>> sm.prepare_model_NEURON(parameters=par, chosenmodel=chosenmodel)``

        ``>> rec["time"], rec["response"], rec["stimulus"] = rm.prepare_recording_NEURON(chosenmodel)``

        ``>> sm.engage_NEURON``

        ``>> co = Converter()``

        Then,

        ``>> spikes = co.voltage_to_spiketrain(chosenmodel, rec)``

        """
        spikes = {}
        for cell_region, with_thresh in model.regions.items():
            # convert voltage recordings to an analog signal
            analog = iss(recordings["time"],
                         recordings["response"][cell_region],
                         time_units='ms',
                         units='mV')
            # determine signal sign from analog signal based on thresh
            analog_sign = cls.determine_signalsign_from_threshold(with_thresh)
            # extract spikes from analog based on signal sign
            spike_train = pd(analog,
                             threshold=np.array(with_thresh) * mV,
                             sign=analog_sign,
                             format=None)
            # record the spike train
            spikes.update({cell_region: spike_train})
        return spikes
def convert_voltage_response_to_spike_train(model):
    """
    Use case: convert_voltage_response_to_spike_train
    """
    response_type = "spike_train"
    model.predictions.update({response_type: {}})
    for cell_region, with_thresh in model.cell_regions.items():
        t_vm = model.predictions["voltage_response"][cell_region]
        # convert voltage response into analog signal
        signal = iss(t_vm[:, 0], t_vm[:, 1], units='mV', time_units='ms')
        # determine the signal sign from the analog signal based on thresh
        signal_sign = [
            "above" if np.sign(x) == 0 or 0.0 or 1 or 1.0 else "below"
            for x in [with_thresh]
        ][0]
        # based on the signal_sign and threshold extract spikes from
        # the analog signal
        spikes = pd(signal,
                    threshold=np.array(with_thresh) * mV,
                    sign=signal_sign,
                    format=None)
        # attach the spike train into the model
        a_prediction = {cell_region: spikes}
        model.predictions[response_type].update(a_prediction)