Exemple #1
0
    def from_electrophysiology(
            cell_id: int,
            ephys: NwbDataSet,
            duration=2.0) -> 'ProcessedAllenNeuronElectrophysiology':

        current_list = []
        voltage_list = []
        time_list = []
        stim_amp_list = []
        n_spikes_list = []
        spike_features_list = []

        for sweep_number in ephys.get_sweep_numbers():
            sweep_metadata = ephys.get_sweep_metadata(sweep_number)
            if sweep_metadata['aibs_stimulus_name'] == 'Long Square':
                sweep_data = ephys.get_sweep(sweep_number)
                amp = sweep_metadata['aibs_stimulus_amplitude_pa']
                index_range = sweep_data["index_range"]
                sampling_rate = sweep_data["sampling_rate"]
                current = sweep_data["stimulus"][
                    index_range[0]:index_range[1] + 1]
                voltage = sweep_data["response"][
                    index_range[0]:index_range[1] + 1]

                # truncate
                max_frames = int(duration * sampling_rate)
                assert max_frames < len(voltage)
                current = current[:max_frames] * 1e12  # in pA
                voltage = voltage[:max_frames] * 1e3  # in mV

                # extract featrures
                time = np.arange(0, max_frames,
                                 dtype=np.float) / sampling_rate  # in seconds
                ext = EphysSweepFeatureExtractor(t=time, v=voltage, i=current)
                ext.process_spikes()
                spike_features = ext.spikes()
                n_spikes = len(spike_features)

                current_list.append(current)
                voltage_list.append(voltage)
                time_list.append(time)
                stim_amp_list.append(amp)
                n_spikes_list.append(n_spikes)
                spike_features_list.append(spike_features)

        return ProcessedAllenNeuronElectrophysiology(
            cell_id=cell_id,
            current_list=current_list,
            voltage_list=voltage_list,
            time_list=time_list,
            stim_amp_list=stim_amp_list,
            n_spikes_list=n_spikes_list,
            spike_features_list=spike_features_list)
Exemple #2
0
def prepare_nwb_output(nwb_stimulus_path, nwb_result_path):
    """Copy the stimulus file, zero out the recorded voltages and spike times.
    
    Parameters
    ----------
    nwb_stimulus_path : string
        NWB file name
    nwb_result_path : string
        NWB file name
    """
    copy(nwb_stimulus_path, nwb_result_path)
    data_set = NwbDataSet(nwb_result_path)
    data_set.fill_sweep_responses(0.0)
    for sweep in data_set.get_sweep_numbers():
        data_set.set_spike_times(sweep, [])
Exemple #3
0
def prepare_nwb_output(nwb_stimulus_path, nwb_result_path):
    '''Copy the stimulus file, zero out the recorded voltages and spike times.

    Parameters
    ----------
    nwb_stimulus_path : string
        NWB file name
    nwb_result_path : string
        NWB file name
    '''

    output_dir = os.path.dirname(nwb_result_path)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    copy(nwb_stimulus_path, nwb_result_path)
    data_set = NwbDataSet(nwb_result_path)
    data_set.fill_sweep_responses(0.0, extend_experiment=True)
    for sweep in data_set.get_sweep_numbers():
        data_set.set_spike_times(sweep, [])
Exemple #4
0
def prepare_nwb_output(nwb_stimulus_path,
                       nwb_result_path):
    '''Copy the stimulus file, zero out the recorded voltages and spike times.
    
    Parameters
    ----------
    nwb_stimulus_path : string
        NWB file name
    nwb_result_path : string
        NWB file name
    '''

    output_dir = os.path.dirname(nwb_result_path)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    copy(nwb_stimulus_path, nwb_result_path)
    data_set = NwbDataSet(nwb_result_path)
    data_set.fill_sweep_responses(0.0)
    for sweep in data_set.get_sweep_numbers():
        data_set.set_spike_times(sweep, [])
    def save_cell_data_web(self, acceptable_stimtypes, non_standard_nwb=False,
                           ephys_dir='preprocessed', **kwargs):

        bpopt_stimtype_map = utility.bpopt_stimtype_map
        distinct_id_map = utility.aibs_stimname_map
        nwb_file = NwbDataSet(self.nwb_path)

        stim_map = defaultdict(list)
        stim_sweep_map = {}
        output_dir = os.path.join(os.getcwd(), ephys_dir)
        utility.create_dirpath(output_dir)

        sweep_numbers = kwargs.get('sweep_numbers') or nwb_file.get_sweep_numbers()
        for sweep_number in sweep_numbers:
            sweep_data = nwb_file.get_sweep_metadata(sweep_number)
            stim_type = sweep_data['aibs_stimulus_name']

            try:
                stim_type = stim_type.decode('UTF-8')
            except:
                pass

            if stim_type in acceptable_stimtypes:
                sweep = nwb_file.get_sweep(sweep_number)

                start_idx, stop_idx = sweep['index_range']

                stimulus_trace = sweep['stimulus'][start_idx:stop_idx]
                response_trace = sweep['response'][start_idx:stop_idx]

                sampling_rate = sweep['sampling_rate']

                time = np.arange(0, len(stimulus_trace)) / sampling_rate
                trace_name = '%s_%d' % (
                    distinct_id_map[stim_type], sweep_number)

                if non_standard_nwb:
                    calc_stimparams_func = self.calc_stimparams_nonstandard
                else:
                    calc_stimparams_func = self.calc_stimparams

                stim_start, stim_stop, stim_amp_start, stim_amp_end, \
                    tot_duration, hold_curr = calc_stimparams_func(
                        time, stimulus_trace, trace_name)

                response_trace_short_filename = '%s.%s' % (trace_name, 'txt')
                response_trace_filename = os.path.join(
                    output_dir, response_trace_short_filename)

                time *= 1e3  # in ms
                response_trace *= 1e3  # in mV
                response_trace = utility.correct_junction_potential(response_trace,
                                                                    self.junction_potential)
                stimulus_trace *= 1e9

                # downsampling
                time, stimulus_trace, response_trace = utility.downsample_ephys_data(
                    time, stimulus_trace, response_trace)

                if stim_type in utility.bpopt_current_play_stimtypes:
                    with open(response_trace_filename, 'wb') as response_trace_file:
                        np.savetxt(response_trace_file,
                                   np.transpose([time, response_trace, stimulus_trace]))

                else:
                    with open(response_trace_filename, 'wb') as response_trace_file:
                        np.savetxt(response_trace_file,
                                   np.transpose([time, response_trace]))

                holding_current = hold_curr  # sweep['bias_current']

                stim_map[distinct_id_map[stim_type]].append([
                    trace_name,
                    bpopt_stimtype_map[stim_type],
                    holding_current/1e12,
                    stim_amp_start / 1e12,
                    stim_amp_end/1e12,
                    stim_start * 1e3,
                    stim_stop * 1e3,
                    tot_duration * 1e3,
                    response_trace_short_filename])

                stim_sweep_map[trace_name] = sweep_number

        logger.debug('Writing stimmap.csv ...')
        stim_reps_sweep_map, stimmap_filename = self.write_stimmap_csv(stim_map,
                                                                       output_dir, stim_sweep_map)

        self.write_provenance(
            output_dir,
            self.nwb_path,
            stim_sweep_map,
            stim_reps_sweep_map)

        return output_dir, stimmap_filename
# all compartments are dictionaries of compartment properties
# compartments also keep track of ids of their children
for child in morphology.children_of(soma):
    print(child['x'], child['y'], child['z'], child['radius'])

#===============================================================================
# example 4
#===============================================================================

from allensdk.core.nwb_data_set import NwbDataSet

# if you ran the examples above, you will have a NWB file here
file_name = 'cell_types/specimen_485909730/ephys.nwb'
data_set = NwbDataSet(file_name)

sweep_numbers = data_set.get_sweep_numbers()
sweep_number = sweep_numbers[0] 
sweep_data = data_set.get_sweep(sweep_number)

# spike times are in seconds relative to the start of the sweep
spike_times = data_set.get_spike_times(sweep_number)

# stimulus is a numpy array in amps
stimulus = sweep_data['stimulus']

# response is a numpy array in volts
reponse = sweep_data['response']

# sampling rate is in Hz
sampling_rate = sweep_data['sampling_rate']
def allen_to_model_and_features(content):
    data_set, sweeps, specimen_id = content
    sweep_numbers_ = defaultdict(list)
    for sweep in sweeps:
        sweep_numbers_[sweep['stimulus_name']].append(sweep['sweep_number'])
    try:
        sweep_numbers = data_set.get_sweep_numbers()
    except:
        print('erroneous deletion of relevant ephys.nwb file')
        ctc = CellTypesCache(manifest_file='cell_types/manifest.json')

        data_set = ctc.get_ephys_data(specimen_id)
        sweeps = ctc.get_ephys_sweeps(specimen_id)
        file_name = 'cell_types/specimen_' + str(specimen_id) + '/ephys.nwb'
        data_set_nwb = NwbDataSet(file_name)
        try:
            sweep_numbers = data_set_nwb.get_sweep_numbers()
        except:
            return None

    for sn in sweep_numbers:
        spike_times = data_set.get_spike_times(sn)
        sweep_data = data_set.get_sweep(sn)

    ##
    # cell_features = extract_cell_features(data_set, sweep_numbers_['Ramp'],sweep_numbers_['Short Square'],sweep_numbers_['Long Square'])
    ##
    cell_features = None
    if cell_features is not None:
        spiking_sweeps = cell_features['long_squares']['spiking_sweeps'][0]
        multi_spike_features = cell_features['long_squares']['hero_sweep']
        biophysics = cell_features['long_squares']
        shapes = cell_features['long_squares']['spiking_sweeps'][0]['spikes'][
            0]

    supras = [
        s for s in sweeps
        if s['stimulus_name'] == str('Square - 2s Suprathreshold')
    ]
    if len(supras) == 0:
        return None
    supra_numbers = [s['sweep_number'] for s in supras]

    smallest_multi = 1000
    all_currents = []
    temp_vm = None
    for sn in supra_numbers:
        spike_times = data_set.get_spike_times(sn)
        sweep_data = data_set.get_sweep(sn)

        if len(spike_times) == 1:
            inj_rheobase = np.max(sweep_data['stimulus'])
            temp_vm = sweep_data['response']
            break
        if len(spike_times) < smallest_multi and len(spike_times) > 1:
            smallest_multi = len(spike_times)
            inj_multi_spike = np.max(sweep_data['stimulus'])
            inj_rheobase = inj_multi_spike
            temp_vm = sweep_data['response']

    spike_times = data_set.get_spike_times(supras[-1]['sweep_number'])
    sweep_data = data_set.get_sweep(supras[-1]['sweep_number'])
    sd = sweep_data['stimulus']
    # sampling rate is in Hz
    sampling_rate = sweep_data['sampling_rate']

    inj = AnalogSignal(sd, sampling_rate=sampling_rate * qt.Hz, units=qt.pA)

    indexs = np.where(sd == np.max(sd))[0]

    if temp_vm is None:
        return (None, None, None, None)
    vm = AnalogSignal(temp_vm, sampling_rate=sampling_rate * qt.Hz, units=qt.V)
    sm = models.StaticModel(vm)
    sm.allen = None
    sm.allen = True
    sm.protocol = {}
    sm.protocol['Time_Start'] = inj.times[indexs[0]]
    sm.protocol['Time_End'] = inj.times[indexs[-1]]

    sm.name = specimen_id
    sm.data_set = data_set
    sm.sweeps = sweeps
    sm.inject_square_current = MethodType(inject_square_current, sm)
    sm.get_membrane_potential = MethodType(get_membrane_potential, sm)

    sm.rheobase_current = inj_rheobase
    current = {}
    current['amplitude'] = sm.rheobase_current
    sm.vm_rheobase = sm.inject_square_current(current)

    try:
        import asciiplotlib as apl
        fig = apl.figure()
        fig.plot([float(t) for t in sm.vm30.times],
                 [float(v) for v in sm.vm30],
                 label="data",
                 width=100,
                 height=80)
        fig.show()

        import asciiplotlib as apl
        fig = apl.figure()
        fig.plot([float(t) for t in sm.vm15.times],
                 [float(v) for v in sm.vm15],
                 label="data",
                 width=100,
                 height=80)
        fig.show()
    except:
        pass
    sm.get_spike_count = MethodType(get_spike_count, sm)
    subs = [
        s for s in sweeps
        if s['stimulus_name'] == str('Square - 0.5ms Subthreshold')
    ]
    sub_currents = [(s['stimulus_absolute_amplitude'] * qt.A).rescale(qt.pA)
                    for s in subs]
    if len(sub_currents) == 3:
        sm.druckmann2013_input_resistance_currents = [
            sub_currents[0], sub_currents[1], sub_currents[2]
        ]

    elif len(sub_currents) == 2:

        sm.druckmann2013_input_resistance_currents = [
            sub_currents[0], sub_currents[0], sub_currents[1]
        ]
    elif len(sub_currents) == 1:
        # unfortunately only one inhibitory current available here.
        sm.druckmann2013_input_resistance_currents = [
            sub_currents[0], sub_currents[0], sub_currents[0]
        ]
    try:
        sm.inject_square_current(sub_currents[0])
    except:
        pass
    get_15_30(sm, inj_rheobase)
    everything = (sm, sweep_data, cell_features, vm)
    return everything
Exemple #8
0
from allensdk.core.nwb_data_set import NwbDataSet

file_name = 'example.nwb'
data_set = NwbDataSet(file_name)

sweep_numbers = data_set.get_sweep_numbers()
sweep_number = sweep_numbers[0]
sweep_data = data_set.get_sweep(sweep_number)

# spike times are in seconds relative to the start of the sweep
spike_times = data_set.get_spike_times(sweep_number)

# stimulus is a numpy array in amps
stimulus = sweep_data['stimulus']

# response is a numpy array in volts
reponse = sweep_data['response']

# sampling rate is in Hz
sampling_rate = sweep_data['sampling_rate']

# start/stop indices that exclude the experimental test pulse (if applicable)
index_range = sweep_data['index_range']