def load_experiment(specimen):

    path, bath, breakin, end, giga, last_bias, mid_bias, SS_amp, spec_name = Find_Critical_Sweeps(
        specimen)

    ds = NwbDataSet(path)

    if bath is 'error':
        bath_peak = 9999999
        bath_ss = 9999999
    else:
        bath_sweep = ds.get_sweep(bath)
        bath_i = bath_sweep['response'] * 1e12
        bath_peak, bath_ss = get_Reses(70, bath_i)

    if breakin is 'error':
        breakin_peak = 9999999
        breakin_ss = 9999999
    else:
        breakin_sweep = ds.get_sweep(breakin)
        breakin_i = breakin_sweep['response'] * 1e12
        breakin_peak, breakin_ss = get_Reses(70, breakin_i)

    if end is 'error':
        end_peak = 9999999
        end_ss = 9999999
        end_leak = 9999999
    else:
        end_sweep = ds.get_sweep(end)
        end_i = end_sweep['response'] * 1e12
        end_peak, end_ss = get_Reses(70, end_i)
        end_leak = np.mean(end_i[0:100])

    if giga is 'error':
        giga_peak = 9999999
        giga_ss = 9999999
    else:
        giga_sweep = ds.get_sweep(giga)
        giga_i = giga_sweep['response'] * 1e12
        giga_peak, giga_ss = get_Reses(70, giga_i)

    features = []
    features.append(spec_name)
    features.append(bath_ss)
    features.append(breakin_peak)
    features.append(breakin_ss)
    features.append(end_peak)
    features.append(end_ss)
    features.append(end_leak)
    features.append(giga_ss)
    features.append(last_bias)
    features.append(mid_bias)
    features.append(SS_amp)

    return features
Exemple #2
0
    def read_stimulus(self, stimulus_path, sweep=0):
        '''Load current values for a specific experiment sweep and setup simulation
        and stimulus sampling rates.

        NOTE: NEURON only allows simulation timestamps of multiples of 40KHz.  To 
        avoid aliasing, we set the simulation sampling rate to the least common
        multiple of the stimulus sampling rate and 40KHz.

        Parameters
        ----------
        stimulus path : string
            NWB file name
        sweep : integer, optional
            sweep index
        '''
        Utils._log.info("reading stimulus path: %s, sweep %s", stimulus_path,
                        sweep)

        stimulus_data = NwbDataSet(stimulus_path)
        sweep_data = stimulus_data.get_sweep(sweep)

        # convert to nA for NEURON
        self.stim_curr = sweep_data['stimulus'] * 1.0e9

        # convert from Hz
        hz = int(sweep_data['sampling_rate'])
        neuron_hz = Utils.nearest_neuron_sampling_rate(hz)

        self.simulation_sampling_rate = neuron_hz
        self.stimulus_sampling_rate = hz

        if hz != neuron_hz:
            Utils._log.debug(
                "changing sampling rate from %d to %d to avoid NEURON aliasing",
                hz, neuron_hz)
Exemple #3
0
    def read_stimulus(self, stimulus_path, sweep=0):
        '''Load current values for a specific experiment sweep and setup simulation
        and stimulus sampling rates.

        NOTE: NEURON only allows simulation timestamps of multiples of 40KHz.  To 
        avoid aliasing, we set the simulation sampling rate to the least common
        multiple of the stimulus sampling rate and 40KHz.

        Parameters
        ----------
        stimulus path : string
            NWB file name
        sweep : integer, optional
            sweep index
        '''
        Utils._log.info(
            "reading stimulus path: %s, sweep %s",
            stimulus_path,
            sweep)

        stimulus_data = NwbDataSet(stimulus_path)
        sweep_data = stimulus_data.get_sweep(sweep)

        # convert to nA for NEURON
        self.stim_curr = sweep_data['stimulus'] * 1.0e9

        # convert from Hz
        hz = int(sweep_data['sampling_rate'])
        neuron_hz = Utils.nearest_neuron_sampling_rate(hz)

        self.simulation_sampling_rate = neuron_hz
        self.stimulus_sampling_rate = hz

        if hz != neuron_hz:
            Utils._log.debug("changing sampling rate from %d to %d to avoid NEURON aliasing", hz, neuron_hz)
Exemple #4
0
    def get_voltage(self, neuron_config, stim_name):

        ephys_sweeps = self.cfg.ephys_sweeps

        ephys_sweep = next(s for s in ephys_sweeps
                           if s['stimulus_name'] == stim_name)

        ds = NwbDataSet(self.ephys_file_name)
        data = ds.get_sweep(ephys_sweep['sweep_number'])
        stimulus = data['stimulus']
        stimulus = stimulus[stimulus != 0]
        stimulus = stimulus[:self.cfg.stimulus_allow]

        # initialize the neuron
        neuron = GlifNeuron.from_dict(neuron_config)

        # Set dt
        neuron.dt = 1.0 / data['sampling_rate']

        # simulate the neuron
        output = neuron.run(stimulus)

        voltage = output['voltage'] * 1e3

        voltage = voltage[~np.isnan(voltage)]
        voltage = voltage[:self.cfg.signal_allow]

        return output, voltage, neuron, stimulus
def load_sweep(file_name, sweep_number, desired_dt=None, cut=0, bessel=False):
    '''load a data sweep and do specified data processing.
    Inputs:
        file_name: string
            name of .nwb data file
        sweep_number: 
            number specifying the sweep to be loaded
        desired_dt: 
            the size of the time step the data should be subsampled to
        cut:
            indicie of which to start reporting data (i.e. cut off data before this indicie)
        bessel: dictionary
            contains parameters 'N' and 'Wn' to implement standard python bessel filtering
    Returns:
        dictionary containing
            voltage: array
            current: array
            dt: time step of the returned data
            start_idx: the index at which the first stimulus starts (excluding the test pulse)
    '''
    ds = NwbDataSet(file_name)
    data = ds.get_sweep(sweep_number)

    data["dt"] = 1.0 / data["sampling_rate"]

    if cut > 0:
        data["response"] = data["response"][cut:]
        data["stimulus"] = data["stimulus"][cut:]

    if bessel:
        sample_freq = 1. / data["dt"]
        filt_coeff = (bessel["freq"]) / (
            sample_freq / 2.)  # filter fraction of Nyquist frequency
        b, a = signal.bessel(bessel["N"], filt_coeff, "low")
        data['response'] = signal.filtfilt(b, a, data['response'], axis=0)

    if desired_dt is not None:
        if data["dt"] != desired_dt:
            data["response"] = subsample_data(data["response"], "mean",
                                              data["dt"], desired_dt)
            data["stimulus"] = subsample_data(data["stimulus"], "mean",
                                              data["dt"], desired_dt)
            data["start_idx"] = int(data["index_range"][0] /
                                    (desired_dt / data["dt"]))
            data["dt"] = desired_dt

    if "start_idx" not in data:
        data["start_idx"] = data["index_range"][0]

    return {
        "voltage": data["response"],
        "current": data["stimulus"],
        "dt": data["dt"],
        "start_idx": data["start_idx"]
    }
Exemple #6
0
def stimulus(neuron_config_file, ephys_sweeps_file):
    ephys_sweeps = json_utilities.read(ephys_sweeps_file)
    ephys_file_name = 'stimulus.nwb'

    # pull out the stimulus for the first sweep
    ephys_sweep = ephys_sweeps[0]
    ds = NwbDataSet(ephys_file_name)
    data = ds.get_sweep(ephys_sweep['sweep_number'])
    stimulus = data['stimulus']

    return stimulus
def load_experiment(file_name, sweep_number):
    ds = NwbDataSet(file_name)
    sweep = ds.get_sweep(sweep_number)

    r = sweep['index_range']
    v = sweep['response'] * 1e3
    i = sweep['stimulus'] * 1e12
    dt = 1.0 / sweep['sampling_rate']
    t = np.arange(0, len(v)) * dt

    return (v, i, t, r, dt)
Exemple #8
0
def stimulus(neuron_config_file, ephys_sweeps_file):
    neuron_config = json_utilities.read(neuron_config_file)
    ephys_sweeps = json_utilities.read(ephys_sweeps_file)
    ephys_file_name = 'stimulus.nwb'

    # pull out the stimulus for the first sweep
    ephys_sweep = ephys_sweeps[0]
    ds = NwbDataSet(ephys_file_name)
    data = ds.get_sweep(ephys_sweep['sweep_number'])
    stimulus = data['stimulus']

    return stimulus
Exemple #9
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)
def get_sweep_from_nwb(nwb_file, sweep_num):
    '''
    Read a sweep from an NWB file and convert Volts -> mV and Amps -> pA. 
    '''
    ds = NwbDataSet(nwb_file)
    data = ds.get_sweep(sweep_num)

    v = data['response'] * 1e3 # convert to mV
    i = data['stimulus'] * 1e12 # convert to pA

    dt = 1.0 / data['sampling_rate']
    t = np.arange(0,len(v)) * dt
    
    return (v, i, t)
Exemple #11
0
 def read_stimulus(self, stimulus_path, sweep=0):
     """load current values for a specific experiment sweep.
     
     Parameters
     ----------
     stimulus path : string
         NWB file name
     sweep : integer, optional
         sweep index
     """
     Utils._log.info("reading stimulus path: %s, sweep %s" % (stimulus_path, sweep))
     stimulus_data = NwbDataSet(stimulus_path)
     sweep_data = stimulus_data.get_sweep(sweep)
     self.stim_curr = sweep_data["stimulus"] * 1.0e9  # convert to nA for NEURON
     self.sampling_rate = 1.0e3 / sweep_data["sampling_rate"]  # convert from Hz
Exemple #12
0
def get_sweep_data(nwb_file, sweep_number, time_scale=1e3, voltage_scale=1e3, stim_scale=1e12):
    """
    Extract data and stim characteristics for a specific DC sweep from nwb file
    Parameters
    ----------
    nwb_file : string
        File name of a pre-existing NWB file.
    sweep_number : integer
        
    time_scale : float
        Convert to ms scale
    voltage_scale : float
        Convert to mV scale
    stim_scale : float
        Convert to pA scale

    Returns
    -------
    t : numpy array
        Sampled time points in ms
    v : numpy array
        Recorded voltage at the sampled time points in mV
    stim_start_time : float
        Stimulus start time in ms
    stim_end_time : float
        Stimulus end time in ms
    """
    nwb = NwbDataSet(nwb_file)
    sweep = nwb.get_sweep(sweep_number)
    stim = sweep['stimulus'] * stim_scale  # in pA
    stim_diff = np.diff(stim)
    stim_start = np.where(stim_diff != 0)[0][-2]
    stim_end = np.where(stim_diff != 0)[0][-1]
    
    # read v and t as numpy arrays
    v = sweep['response'] * voltage_scale  # in mV
    dt = time_scale / sweep['sampling_rate']  # in ms
    num_samples = len(v)
    t = np.arange(num_samples) * dt
    stim_start_time = t[stim_start]
    stim_end_time = t[stim_end]
    return t, v, stim_start_time, stim_end_time
def extract_single_sweep_features(features, nwb_file, sweep_number):
    '''
    Run feature extraction on a single sweep.  

    Parameters
    ----------
    
    features: EphysFeatureExtractor instance
    
    nwb_file: string
        File name of an NWB file

    sweep_numbers: int
        Sweep number in the NWB file
       
    '''

    nwb = NwbDataSet(nwb_file)
    data = nwb.get_sweep(sweep_number)

    v = data['response']
    curr = data['stimulus']

    idx0 = data['index_range'][0]
    idx1 = data['index_range'][1]

    if idx0 >= idx1:
        logging.warning("Sweep %s stop index precedes start index, skipping spike identification" % sweep_number)
        return 

    hz = data['sampling_rate']
    dt = 1.0 / hz
    t = np.arange(0, len(v)) * dt
    
    features.process_instance(sweep_number, v*1e3, curr*1e12, t, dt*idx0, dt*(idx1-idx0-2), None)
    
    results = {}
    results["mean"] = features.feature_list[-1].mean
    results["stdev"] = features.feature_list[-1].stdev

    return results
Exemple #14
0
def output(neuron_config_file, ephys_sweeps_file):
    neuron_config = json_utilities.read(neuron_config_file)
    ephys_sweeps = json_utilities.read(ephys_sweeps_file)
    ephys_file_name = 'stimulus.nwb'

    # pull out the stimulus for the first sweep
    ephys_sweep = ephys_sweeps[0]
    ds = NwbDataSet(ephys_file_name)
    data = ds.get_sweep(ephys_sweep['sweep_number'])
    stimulus = data['stimulus']

    # initialize the neuron
    # important! update the neuron's dt for your stimulus
    neuron = GlifNeuron.from_dict(neuron_config)
    neuron.dt = 1.0 / data['sampling_rate']

    # simulate the neuron
    truncate = 56041
    output = neuron.run(stimulus[0:truncate])

    return output
Exemple #15
0
    def read_stimulus(self, stimulus_path, sweep=0):
        '''load current values for a specific experiment sweep.
        
        Parameters
        ----------
        stimulus path : string
            NWB file name
        sweep : integer, optional
            sweep index
        '''
        Utils._log.info("reading stimulus path: %s, sweep %s", stimulus_path,
                        sweep)

        stimulus_data = NwbDataSet(stimulus_path)
        sweep_data = stimulus_data.get_sweep(sweep)

        # convert to nA for NEURON
        self.stim_curr = sweep_data['stimulus'] * 1.0e9

        # convert from Hz
        self.sampling_rate = 1.0e3 / sweep_data['sampling_rate']
Exemple #16
0
def output():
    neuron_config = json_utilities.read('neuron_config.json')
    ephys_sweeps = json_utilities.read('ephys_sweeps.json')
    ephys_file_name = 'stimulus.nwb'

    # pull out the stimulus for the first sweep
    ephys_sweep = ephys_sweeps[0]
    ds = NwbDataSet(ephys_file_name)
    data = ds.get_sweep(ephys_sweep['sweep_number'])
    stimulus = data['stimulus']

    # initialize the neuron
    # important! update the neuron's dt for your stimulus
    neuron = GlifNeuron.from_dict(neuron_config)
    neuron.dt = 1.0 / data['sampling_rate']

    # simulate the neuron
    truncate = 56041
    output = neuron.run(stimulus[0:truncate])

    return output
    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
Exemple #18
0
import allensdk.core.json_utilities as json_utilities
from allensdk.model.glif.glif_neuron import GlifNeuron
from allensdk.core.nwb_data_set import NwbDataSet

neuron_config = json_utilities.read("neuron_config.json")
ephys_sweeps = json_utilities.read("ephys_sweeps.json")
ephys_file_name = "stimulus.nwb"

# pull out the stimulus for the first sweep
ephys_sweep = ephys_sweeps[0]
ds = NwbDataSet(ephys_file_name)
data = ds.get_sweep(ephys_sweep["sweep_number"])
stimulus = data["stimulus"]

# initialize the neuron
# important! update the neuron's dt for your stimulus
neuron = GlifNeuron.from_dict(neuron_config)
neuron.dt = 1.0 / data["sampling_rate"]

# simulate the neuron
output = neuron.run(stimulus)

voltage = output["voltage"]
threshold = output["threshold"]
spike_times = output["interpolated_spike_times"]
neuron_config = cfg.neuron_config
neuron_id = next(iter(neuron_config))
neuron_config = neuron_config[neuron_id]
ephys_sweeps = cfg.ephys_sweeps

stim_names = ["Noise 1"]
params = create_param_dict(neuron_config)

for stim in stim_names:

    sweeps = [s for s in ephys_sweeps if s['stimulus_name'] == stim]

    for sweep_id, ephys_sweep in enumerate(sweeps):
        ds = NwbDataSet(ephys_file_name)
        data = ds.get_sweep(ephys_sweep['sweep_number'])
        stimulus = data['stimulus']
        stimulus = stimulus[stimulus != 0]
        stimulus = stimulus[:cfg.stimulus_allow]

        for param_id, param_dict in enumerate(params):
            train = {}

            # update neuron_config based on params
            neuron_config = param_dict

            # initialize the neuron
            neuron = GlifNeuron.from_dict(neuron_config)

            # Set dt
            neuron.dt = 1.0 / data['sampling_rate']
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']

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

import allensdk.core.json_utilities as json_utilities
from allensdk.model.glif.glif_neuron import GlifNeuron
from allensdk.core.nwb_data_set import NwbDataSet

neuron_config = json_utilities.read('neuron_config.json')['566302806']
ephys_sweeps = json_utilities.read('ephys_sweeps.json')
ephys_file_name = 'stimulus.nwb'

# pull out the stimulus for the current-clamp first sweep
ephys_sweep = next( s for s in ephys_sweeps 
                    if s['stimulus_units'] == 'Amps' )
ds = NwbDataSet(ephys_file_name)
data = ds.get_sweep(ephys_sweep['sweep_number']) 
stimulus = data['stimulus']

# initialize the neuron
# important! update the neuron's dt for your stimulus
neuron = GlifNeuron.from_dict(neuron_config)
neuron.dt = 1.0 / data['sampling_rate']

# simulate the neuron
output = neuron.run(stimulus)

voltage = output['voltage']
threshold = output['threshold']
spike_times = output['interpolated_spike_times']

#===============================================================================
    print('Loading data from: %s' % raw_ephys_file_name)

    from allensdk.core.nwb_data_set import NwbDataSet
    data_set = NwbDataSet(raw_ephys_file_name)

    import matplotlib.pyplot as plt
    import numpy as np
    fig = plt.figure()

    sweep_numbers = sweep_numbers_for_data[dataset_id]

    subset = {}

    for sweep_number in sweep_numbers:
        sweep_data = data_set.get_sweep(sweep_number)

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

        # stimulus is a numpy array in amps
        stimulus = sweep_data['stimulus'][index_range[0]:index_range[-1]]

        # response is a numpy array in volts
        response = sweep_data['response'][index_range[0]:index_range[-1]] * 1000
        subset[sweep_number] = response

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

        # define some time points in seconds (i.e., convert to absolute time)
Exemple #23
0
def noise1_response_comparison(ids, ve_paths, file_prefix):
    filename = 'Noise2'

    ids1 = find_cells_with_all_models(ids)
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
    for specimen_id in ids1:
        data_set = expt_data_set(specimen_id)
        exp_sweeps = lims_utils.get_sweeps_of_type("C1NSSEED_2",
                                                   specimen_id,
                                                   passed_only=True)

        if len(exp_sweeps) == 0:
            continue

        fig, axes = plt.subplots(3, 1, sharex=True)
        plt.subplots_adjust(hspace=0.1, wspace=0.1)
        axes[2].set_xlim([0, 25])
        sweep_data = data_set.get_sweep(exp_sweeps[0])
        spike_times = data_set.get_spike_times(exp_sweeps[0])
        index_range = sweep_data["index_range"]
        i = sweep_data["stimulus"][0:index_range[1] + 1]  # in A
        v = sweep_data["response"][0:index_range[1] + 1]  # in V
        i *= 1e12  # to pA
        v *= 1e3  # to mV
        sampling_rate = sweep_data["sampling_rate"]  # in Hz
        t = np.arange(0, len(v)) * (1.0 / sampling_rate)

        axes[1].plot(t, v, color='k', linewidth=0.3)
        axes[0].scatter(spike_times, [10] * len(spike_times),
                        color='k',
                        s=100,
                        marker="|",
                        linewidth=0.3)
        axes[2].plot(t, i, color='k', linewidth=0.3)
        axes[1].set_ylabel("mV")
        axes[2].set_ylabel("pA")
        axes[2].set_xlabel("seconds")

        for model, label, color in zip(BASE_ORDER, LABELS, colors):
            ve_path = ve_paths[model][specimen_id]
            data_set = NwbDataSet(ve_path)
            exp_sweeps = lims_utils.get_sweeps_of_type("C1NSSEED_2",
                                                       specimen_id,
                                                       passed_only=True)
            sweep_data = data_set.get_sweep(exp_sweeps[0])
            spike_times = data_set.get_spike_times(exp_sweeps[0])
            index_range = sweep_data["index_range"]
            i = sweep_data["stimulus"][0:index_range[1] + 1]  # in A
            v = sweep_data["response"][0:index_range[1] + 1]  # in V
            i *= 1e12  # to pA
            v *= 1e3  # to mV
            sampling_rate = sweep_data["sampling_rate"]  # in Hz
            t = np.arange(0, len(v)) * (1.0 / sampling_rate)
            axes[0].scatter(spike_times,
                            [80 - (10 * BASE_ORDER.index(model))] *
                            len(spike_times),
                            color='k',
                            s=100,
                            marker="|",
                            linewidth=0.3)
        axes[0].set_yticklabels([
            "", 'Experimental Data', LABELS[6], LABELS[5], LABELS[4],
            LABELS[3], LABELS[2], LABELS[1], LABELS[0]
        ])
        axes[0].set_ylabel("model type")
        axes[0].set_title("Spike Times")

        plt.subplots_adjust(hspace=0.1, wspace=0.1)
        plt.savefig(str(file_prefix + "_" + filename + "_" + str(specimen_id)),
                    bbox_inches="tight")
        #plt.show()
        plt.close()
Exemple #24
0
def extract_info_from_nwb_file(dataset_id, raw_ephys_file_name):

    info = {}

    import h5py
    import numpy as np
    h5f = h5py.File(raw_ephys_file_name, "r")
    metas = [
        'aibs_cre_line', 'aibs_dendrite_type',
        'intracellular_ephys/Electrode 1/location'
    ]
    for m in metas:
        d = h5f.get('/general/%s' % m)
        print("%s = \t%s" % (m, d.value))
        info[m.split('/')[-1]] = str(d.value)
    h5f.close()

    from allensdk.core.nwb_data_set import NwbDataSet
    data_set = NwbDataSet(raw_ephys_file_name)

    sweep_numbers = data_set.get_experiment_sweep_numbers()
    if test:
        sweep_numbers = [33, 45]

    sweep_numbers.sort()

    info[DH.DATASET] = dataset_id
    info[DH.COMMENT] = 'Data analysed on %s' % (time.ctime())

    info[DH.PYELECTRO_VERSION] = pyel_ver
    info[DH.ALLENSDK_VERSION] = allensdk_ver
    info[DH.SWEEPS] = {}

    for sweep_number in sweep_numbers:

        sweep_data = data_set.get_sweep(sweep_number)

        if data_set.get_sweep_metadata(
                sweep_number)['aibs_stimulus_name'] == "Long Square":
            sweep_info = {}
            sweep_info[DH.METADATA] = data_set.get_sweep_metadata(sweep_number)
            info[DH.SWEEPS]['%i' % sweep_number] = sweep_info
            sweep_info[DH.SWEEP] = sweep_number

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

            # stimulus is a numpy array in amps
            stimulus = sweep_data['stimulus'][index_range[0]:index_range[-1]]

            # response is a numpy array in volts
            response = sweep_data['response'][
                index_range[0]:index_range[-1]] * 1000

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

            # define some time points in seconds (i.e., convert to absolute time)
            time_pts = np.arange(0,
                                 len(stimulus) / sampling_rate,
                                 1. / sampling_rate) * 1000

            comment = 'Sweep: %i in %i; %sms -> %sms; %sA -> %sA; %smV -> %smV' % (
                sweep_number, dataset_id, time_pts[0], time_pts[-1],
                np.amin(stimulus), np.amax(stimulus), np.amin(response),
                np.amax(response))
            print(comment)

            sweep_info[DH.COMMENT] = comment

            analysis = utils.simple_network_analysis(
                {sweep_number: response},
                time_pts,
                extra_targets=[
                    '%s:value_280' % sweep_number,
                    '%s:average_1000_1200' % sweep_number,
                    '%s:average_100_200' % sweep_number
                ],
                end_analysis=1500,
                plot=plot,
                show_plot_already=False,
                verbose=True)

            sweep_info[DH.ICLAMP_ANALYSIS] = analysis

    analysis_file_name = '%s_analysis.json' % (dataset_id)
    analysis_file = open(analysis_file_name, 'w')
    pretty = pp.pformat(info)
    pretty = pretty.replace('\'', '"')
    pretty = pretty.replace('u"', '"')
    analysis_file.write(pretty)
    analysis_file.close()

    print('Written info to %s' % analysis_file_name)
def extract_info_from_nwb_file(dataset_id, raw_ephys_file_name):

    info = {}
 
    import h5py
    import numpy as np
    h5f = h5py.File(raw_ephys_file_name, "r")
    metas = ['aibs_cre_line','aibs_dendrite_type','intracellular_ephys/Electrode 1/location']
    for m in metas:
        d = h5f.get('/general/%s'%m)
        print("%s = \t%s"%(m,d.value))
        info[m.split('/')[-1]]=str(d.value)
    h5f.close()
    
    from allensdk.core.nwb_data_set import NwbDataSet
    data_set = NwbDataSet(raw_ephys_file_name)


    sweep_numbers = data_set.get_experiment_sweep_numbers()
    if test:
        sweep_numbers = [33,45]
        
    sweep_numbers.sort()

    info[DH.DATASET] = dataset_id
    info[DH.COMMENT] = 'Data analysed on %s'%(time.ctime())
    
    info[DH.PYELECTRO_VERSION] = pyel_ver
    info[DH.ALLENSDK_VERSION] = allensdk_ver
    info[DH.SWEEPS] = {}

    for sweep_number in sweep_numbers:

        sweep_data = data_set.get_sweep(sweep_number)
        
        if data_set.get_sweep_metadata(sweep_number)['aibs_stimulus_name'] == "Long Square":
            sweep_info = {}
            sweep_info[DH.METADATA] = data_set.get_sweep_metadata(sweep_number)
            info[DH.SWEEPS]['%i'%sweep_number] = sweep_info
            sweep_info[DH.SWEEP] = sweep_number


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

            # stimulus is a numpy array in amps
            stimulus = sweep_data['stimulus'][index_range[0]:index_range[-1]]

            # response is a numpy array in volts
            response = sweep_data['response'][index_range[0]:index_range[-1]]*1000

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

            # define some time points in seconds (i.e., convert to absolute time)
            time_pts = np.arange(0,len(stimulus)/sampling_rate,1./sampling_rate)*1000

            comment = 'Sweep: %i in %i; %sms -> %sms; %sA -> %sA; %smV -> %smV'%(sweep_number, dataset_id, time_pts[0], time_pts[-1], np.amin(stimulus), np.amax(stimulus), np.amin(response), np.amax(response))
            print(comment)

            sweep_info[DH.COMMENT] = comment

            analysis = utils.simple_network_analysis({sweep_number:response}, 
                                                     time_pts, 
                                                     extra_targets = ['%s:value_280'%sweep_number,      
                                                                      '%s:average_1000_1200'%sweep_number,      
                                                                      '%s:average_100_200'%sweep_number],
                                                     end_analysis=1500, 
                                                     plot=plot, 
                                                     show_plot_already=False,
                                                     verbose=True)

            sweep_info[DH.ICLAMP_ANALYSIS] = analysis

    analysis_file_name = '%s_analysis.json'%(dataset_id)
    analysis_file = open(analysis_file_name, 'w')
    pretty = pp.pformat(info)
    pretty = pretty.replace('\'', '"')
    pretty = pretty.replace('u"', '"')
    analysis_file.write(pretty)
    analysis_file.close()
    
    print('Written info to %s'%analysis_file_name)