Ejemplo n.º 1
0
    def process_data(self):
        data = self.data
        run_param = self.run_param
        process_param = self.process_param

        LFP_amp = LFPy_util.data_extraction.maxabs(data['LFP'],axis=1)
        LFP_amp = LFP_amp.reshape([data['n_elec_x'],data['n_elec_y']])

        grid_x, grid_y = np.meshgrid(data['lin_x'],data['lin_y'])

        signal = data['soma_v']
        spike, spikes_t_vec, I = de.extract_spikes(
            data['t_vec'],
            signal,
            pre_dur=process_param['pre_dur'],
            post_dur=process_param['post_dur'],
            threshold=process_param['threshold'],
            amp_option=process_param['amp_option'], 
            )
        # Gather all spikes from the same indices as where the spike appears
        # in the first electrode.
        spike_index = process_param['spike_to_measure']
        if spike.shape[0] < spike_index:
            raise ValueError("Found fewer spikes than process_param['spike_to_measure']")
        spikes = data['LFP'][:, I[spike_index, 0]:I[spike_index, 1]]

        amps_I = de.find_amplitude_type_I(spikes, amp_option=process_param['amp_option'])
        amps_II = de.find_amplitude_type_II(spikes)
        widths_I, widths_I_trace = de.find_wave_width_type_I(spikes,
                                                             dt=data['dt'])
        widths_II, widths_II_trace = de.find_wave_width_type_II(
            spikes,
            dt=data['dt'],
            amp_option=process_param['amp_option'])

        widths_I = widths_I.reshape([data['n_elec_x'],data['n_elec_y']])
        widths_II = widths_II.reshape([data['n_elec_x'],data['n_elec_y']])

        data['LFP_amp'] = LFP_amp*1000
        data['grid_x'] = grid_x
        data['grid_y'] = grid_y

        data['amps_I'] = amps_I * 1000

        data['amps_II'] = amps_II * 1000

        data['widths_I'] = widths_I
        data['widths_I_trace'] = widths_I_trace * 1000

        data['widths_II'] = widths_II
        data['widths_II_trace'] = widths_II_trace * 1000

        data['spikes'] = spikes * 1000
        data['spikes_t_vec'] = spikes_t_vec
Ejemplo n.º 2
0
 def process_data(self):
     data = self.data
     process_param = self.process_param
     signal = data['soma_v']
     spike, spikes_t_vec, I = de.extract_spikes(
         data['t_vec'],
         signal,
         pre_dur=process_param['pre_dur'],
         post_dur=process_param['post_dur'],
         threshold=process_param['threshold'],
         amp_option=process_param['amp_option'], 
         )
     # Gather all spikes from the same indices as where the spike appears
     # in the first electrode.
     spike_index = process_param['spike_to_measure']
     if spike.shape[0] < spike_index:
         raise ValueError("Found fewer spikes than process_param['spike_to_measure']")
     spikes = data['LFP'][:, I[spike_index, 0]:I[spike_index, 1]]
     self.data['LFP'] = spikes
Ejemplo n.º 3
0
    def process_data(self):
        data = self.data
        run_param = self.run_param
        process_param = self.process_param

        # Get the signal from the soma potential.
        signal = data['soma_v']
        spike, spikes_t_vec, I = de.extract_spikes(
            data['t_vec'],
            signal,
            pre_dur=process_param['pre_dur'],
            post_dur=process_param['post_dur'],
            threshold=process_param['threshold'],
            amp_option=process_param['amp_option'], 
            )
        # Gather all spikes from the same indices as where the spike appears
        # in the first electrode.
        spike_index = process_param['spike_to_measure']
        if spike.shape[0] < spike_index:
            raise ValueError("Found fewer spikes than process_param['spike_to_measure']")
        spikes = data['LFP'][:, I[spike_index, 0]:I[spike_index, 1]]

        amps_I = de.find_amplitude_type_I(spikes, amp_option=process_param['amp_option'])
        amps_II = de.find_amplitude_type_II(spikes)

        widths_I, widths_I_trace = de.find_wave_width_type_I(spikes,
                                                             dt=data['dt'])
        widths_II, widths_II_trace = de.find_wave_width_type_II(
            spikes,
            threshold=process_param['width_II_thresh'],
            dt=data['dt'],
            amp_option=process_param['amp_option'])

        widths_III, widths_III_trace = de.find_wave_width_type_III(
            spikes,
            threshold=process_param['width_III_thresh'],
            dt=data['dt'],
            amp_option=process_param['amp_option'])

        p = run_param['n_phi']
        n = run_param['n']

        amps_I = np.reshape(amps_I, (p, n))
        amps_II = np.reshape(amps_II, (p, n))
        widths_I = np.reshape(widths_I, (p, n))
        widths_II = np.reshape(widths_II, (p, n))
        widths_III = np.reshape(widths_III, (p, n))

        # Becomes vector with length n.
        amps_I_mean = np.mean(amps_I, 0)
        amps_I_std = np.std(amps_I, 0)
        amps_II_mean = np.mean(amps_II, 0)
        amps_II_std = np.std(amps_II, 0)
        widths_I_mean = np.mean(widths_I, 0)
        widths_I_std = np.std(widths_I, 0)
        widths_II_mean = np.mean(widths_II, 0)
        widths_II_std = np.std(widths_II, 0)
        widths_III_mean = np.mean(widths_III, 0)
        widths_III_std = np.std(widths_III, 0)

        data['amps_I_mean'] = amps_I_mean * 1000
        data['amps_I_std'] = amps_I_std * 1000
        data['amps_I'] = amps_I * 1000
        data['amps_II_mean'] = amps_II_mean * 1000
        data['amps_II_std'] = amps_II_std * 1000
        data['amps_II'] = amps_II * 1000

        data['widths_I_mean'] = widths_I_mean
        data['widths_I_std'] = widths_I_std
        data['widths_I'] = widths_I
        data['widths_I_trace'] = widths_I_trace * 1000

        data['widths_II_mean'] = widths_II_mean
        data['widths_II_std'] = widths_II_std
        data['widths_II'] = widths_II
        data['widths_II_trace'] = widths_II_trace * 1000

        data['widths_III_mean'] = widths_III_mean
        data['widths_III_std'] = widths_III_std
        data['widths_III'] = widths_III
        data['widths_III_trace'] = widths_III_trace * 1000

        data['spikes'] = spikes * 1000
        data['spikes_t_vec'] = spikes_t_vec
        data['r_vec'] = np.linspace(run_param['R_0'], run_param['R'],
                                    run_param['n'])
Ejemplo n.º 4
0
    def process_data(self):
        data = self.data
        run_param = self.run_param
        process_param = self.process_param
        t_vec = np.array(data['t_vec'])
        v_vec = np.array(data['LFP'])*1000
        x = np.array(data['elec_x']).flatten()
        y = np.array(data['elec_y']).flatten()
        z = np.array(data['elec_z']).flatten()
        # Calculate the radial distance to the electrodes.
        r = np.sqrt(x * x + y * y + z * z).flatten()

        # Get the signal from the soma potential.
        # signal = data['LFP'][0]
        signal = data['soma_v']
        spikes, spikes_t_vec, I = de.extract_spikes(
            data['t_vec'],
            signal,
            pre_dur=process_param['pre_dur'],
            post_dur=process_param['post_dur'],
            threshold=process_param['threshold'],
            amp_option=process_param['amp_option'], 
            )
        # Gather all spikes from the same indices as where the spike appears
        # in the membrane potential.
        spike_index = process_param['spike_to_measure']
        if spikes.shape[0] < spike_index:
            raise ValueError("Found fewer spikes than process_param['spike_to_measure']")
        spikes = v_vec[:, I[spike_index, 0]:I[spike_index, 1]]

        # Find widths of the spikes, trace can be used for plotting.
        widths_I, widths_I_trace = de.find_wave_width_type_I(spikes,
                                                             dt=data['dt'])
        widths_II, widths_II_trace = de.find_wave_width_type_II(
            spikes,
            threshold=process_param['width_half_thresh'],
            dt=data['dt'],
            amp_option=self.process_param['amp_option'])
        amps_I = de.find_amplitude_type_I(spikes, amp_option=self.process_param['amp_option'])
        amps_II = de.find_amplitude_type_II(spikes)

        # Remove spikes which does not look nice.
        data['widths_I_original'] = widths_I
        data['widths_II_original'] = widths_II
        if process_param['assert_width']:
            low = np.where(widths_I < process_param['assert_width_I_low'])[0]
            high = np.where(widths_I > process_param['assert_width_I_high'])[0]
            ignored_spikes_I = np.union1d(low, high)
            low = np.where(widths_II < process_param['assert_width_II_low'])[0]
            high = np.where(widths_II > process_param['assert_width_II_high'])[0]
            ignored_spikes_II = np.union1d(low, high)
            ignored_spikes = np.union1d(ignored_spikes_I, ignored_spikes_II)

            spikes = np.delete(spikes, ignored_spikes, axis=0)
            widths_I = np.delete(widths_I, ignored_spikes, axis=0)
            widths_II = np.delete(widths_II, ignored_spikes, axis=0)
            amps_I = np.delete(amps_I, ignored_spikes, axis=0)
            amps_II = np.delete(amps_II, ignored_spikes, axis=0)
            x = np.delete(x, ignored_spikes, axis=0)
            y = np.delete(y, ignored_spikes, axis=0)
            z = np.delete(z, ignored_spikes, axis=0)
            r = np.delete(r, ignored_spikes, axis=0)
            self.info['ignored_spikes_cnt'] = len(ignored_spikes)

        # Put widths_I in bins decided by the radial distance. 
        # Then calculate std and mean.
        bins = np.linspace(0, run_param['R'], self.process_param['bins'], endpoint=True)
        # Find the indices of the bins to which each value in r array belongs.
        inds = np.digitize(r, bins)
        # Widths and amps binned by distance.
        widths_I_at_r = [widths_I[inds == i] for i in range(len(bins))]
        widths_II_at_r = [widths_II[inds == i] for i in range(len(bins))]
        amps_I_at_r = [amps_I[inds == i] for i in range(len(bins))]
        amps_II_at_r = [amps_II[inds == i] for i in range(len(bins))]

        with warnings.catch_warnings():
            # Ignore warnings where mean or var has zero sized array as input.
            warnings.simplefilter("ignore", category=RuntimeWarning)
            widths_I_mean = [widths_I[inds == i].mean() for i in range(len(bins))]
            widths_I_std = [np.sqrt(widths_I[inds == i].var()) for i in range(len(bins))]
            widths_II_mean = [widths_II[inds == i].mean() for i in range(len(bins))]
            widths_II_std = [np.sqrt(widths_II[inds == i].var()) for i in range(len(bins))]
            amps_I_mean = [amps_I[inds == i].mean() for i in range(len(bins))]
            amps_I_std = [np.sqrt(amps_I[inds == i].var()) for i in range(len(bins))]
            amps_II_mean = [amps_II[inds == i].mean() for i in range(len(bins))]
            amps_II_std = [np.sqrt(amps_II[inds == i].var()) for i in range(len(bins))]

            widths_I_mean = np.array(widths_I_mean)
            widths_II_mean = np.array(widths_II_mean)
            amps_I_mean = np.array(amps_I_mean)
            amps_II_mean = np.array(amps_II_mean)
            widths_I_std = np.array(widths_I_std)
            widths_II_std = np.array(widths_II_std)
            amps_I_std = np.array(amps_I_std)
            amps_II_std = np.array(amps_II_std)

        data['elec_r_all'] = r

        data['amps_I_mean'] = amps_I_mean
        data['amps_I_std'] = amps_I_std
        data['amps_I'] = amps_I
        data['amps_I_at_r'] = amps_I_at_r

        data['amps_II_mean'] = amps_II_mean
        data['amps_II_std'] = amps_II_std
        data['amps_II'] = amps_II
        data['amps_II_at_r'] = amps_II_at_r

        data['widths_I_mean'] = widths_I_mean
        data['widths_I_std'] = widths_I_std
        data['widths_I'] = widths_I
        data['widths_I_trace'] = widths_I_trace 
        data['widths_I_at_r'] = widths_I_at_r 

        data['widths_II_mean'] = widths_II_mean
        data['widths_II_std'] = widths_II_std
        data['widths_II'] = widths_II
        data['widths_II_trace'] = widths_II_trace 
        data['widths_II_at_r'] = widths_II_at_r

        data['bins'] = bins
        data['elec_r'] = r
        data['spikes'] = spikes
        data['spikes_t_vec'] = spikes_t_vec

        self.info['spike_to_measure'] = process_param['spike_to_measure']
        self.info['dt'] = data['dt']
Ejemplo n.º 5
0
    def process_data(self):
        data = self.data
        run_param = self.run_param
        process_param = self.process_param

        signal = data['soma_v']
        spikes_soma, spikes_t_vec, I = de.extract_spikes(
            data['t_vec'],
            signal,
            pre_dur=process_param['pre_dur'],
            post_dur=process_param['post_dur'],
            threshold=process_param['threshold'],
            amp_option=process_param['amp_option'], 
            )

        spike_index = process_param['spike_to_measure']
        if spikes_soma.shape[0] < spike_index:
            raise ValueError("Found fewer spikes than process_param['spike_to_measure']")
        # Gather all spikes from the same indices as where the spike appears
        # in the membrane potential.
        spikes = data['LFP'][:, I[spike_index, 0]:I[spike_index, 1]]

        widths_I, widths_I_trace = de.find_wave_width_type_I(
            spikes,
            dt=data['dt'],
            )

        widths_II, widths_II_trace = de.find_wave_width_type_II(
            spikes,
            dt=data['dt'],
            amp_option=process_param['amp_option'],
            )

        widths_I_soma, widths_I_trace_soma = de.find_wave_width_type_I(
            spikes_soma[spike_index],
            dt=data['dt'],
            )

        widths_II_soma, widths_II_trace_soma = de.find_wave_width_type_II(
            spikes_soma[spike_index],
            dt=data['dt'],
            amp_option=process_param['amp_option'],
            )

        freq, amp, phase = \
            LFPy_util.data_extraction.find_freq_and_fft(data['dt'], spikes)
        # Remove the first coefficient as we don't care about the baseline.
        freq = np.delete(freq, 0)
        amp = np.delete(amp, 0, axis=1)

        data['freq'] = freq * pq.kHz
        data['amp'] = amp
        data['phase'] = phase

        data['spikes'] = spikes
        data['spikes_t_vec'] = spikes_t_vec
        data['spike_soma'] = spikes_soma[spike_index]
        data['width_I'] = widths_I
        data['width_I_trace'] = widths_I_trace
        data['width_II'] = widths_II
        data['width_II_trace'] = widths_II_trace
        data['width_I_soma'] = widths_I_soma
        data['width_I_trace_soma'] = widths_I_trace_soma[0]
        data['width_II_soma'] = widths_II_soma
        data['width_II_trace_soma'] = widths_II_trace_soma[0]

        self.info['elec_x'] = data['elec_x']
        self.info['elec_y'] = data['elec_y']
        self.info['elec_z'] = data['elec_z']
Ejemplo n.º 6
0
    def process_data(self):
        data = self.data
        run_param = self.run_param
        process_param = self.process_param

        if not data:
            raise ValueError('No data to process.')

        freqs = np.zeros(run_param['sweeps'])
        isi = np.zeros(run_param['sweeps'])
        freqs_elec = np.zeros(run_param['sweeps'])
        isi_elec = np.zeros(run_param['sweeps'])
        
        # Find firing frequency and inter spike interval from soma.
        for i in xrange(run_param['sweeps']):
            spike_indices = de.find_spikes(
                data['t_vec'],
                data['v_vec_soma'][i],
                process_param['threshold'],
                pre_dur=process_param['pre_dur'],
                post_dur=process_param['post_dur'],
                threshold_abs=process_param['threshold_abs_soma']
                )
            isi_tmp = np.diff(spike_indices) * data['dt']
            if isi_tmp.size != 0:
                isi[i] = np.mean(isi_tmp)
                freqs[i] = 1000/isi[i] # Hz
            else:
                isi[i] = 0
                freqs[i] = 0

        spikes_soma = [None]*run_param['sweeps']
        widths_I_soma = [None]*run_param['sweeps']
        widths_I_soma_mean = np.zeros(run_param['sweeps'])
        widths_I_soma_std = np.zeros(run_param['sweeps'])
        widths_II_soma = [None]*run_param['sweeps']
        widths_II_soma_mean = np.zeros(run_param['sweeps'])
        widths_II_soma_std = np.zeros(run_param['sweeps'])

        amps_I_soma = [None]*run_param['sweeps']
        amps_I_soma_mean = np.zeros(run_param['sweeps'])
        amps_I_soma_std = np.zeros(run_param['sweeps'])

        # Soma spikes and widths.
        for i in xrange(run_param['sweeps']):
            with warnings.catch_warnings():
                # Ignore warnings when not finding spikes.
                warnings.simplefilter("ignore", category=RuntimeWarning)

                spikes_soma[i], spikes_t_vec, I = de.extract_spikes(
                    data['t_vec'],
                    data['v_vec_soma'][i],
                    pre_dur=process_param['pre_dur'],
                    post_dur=process_param['post_dur'],
                    threshold=process_param['threshold'],
                    amp_option=process_param['amp_option'], 
                    threshold_abs=process_param['threshold_abs_soma']
                    )
            if spikes_soma[i].shape[0] == 0:
                widths_I_soma[i] = []
                widths_I_soma_mean[i] = np.nan
                widths_II_soma[i] = []
                widths_II_soma_mean[i] = np.nan
                amps_I_soma_mean[i] = np.nan
                amps_I_soma_std[i] = np.nan
                continue

            widths_I_soma[i], trace = de.find_wave_width_type_I(
                spikes_soma[i],
                dt=data['dt'],
                )

            widths_II_soma[i], trace = de.find_wave_width_type_II(
                spikes_soma[i],
                dt=data['dt'],
                amp_option=process_param['amp_option'],
                )

            widths_I_soma_mean[i] = np.mean(widths_I_soma[i])
            widths_II_soma_mean[i] = np.mean(widths_II_soma[i])
            widths_I_soma_std[i] = np.sqrt(np.var(widths_I_soma[i]))
            widths_II_soma_std[i] = np.sqrt(np.var(widths_II_soma[i]))

            amps_I_soma[i] = de.find_amplitude_type_I(
                    spikes_soma[i],
                    process_param['amp_option'])

            amps_I_soma_mean[i] = np.mean(amps_I_soma[i])
            amps_I_soma_std[i] = np.sqrt(np.var(amps_I_soma[i]))


        # There are a variable amount of spikes, so these varibles have
        # one dimension with unknown lenght.
        spikes_elec = [None]*run_param['sweeps']
        widths_I_elec = [None]*run_param['sweeps']
        widths_II_elec = [None]*run_param['sweeps']
        amps_I_elec = [None]*run_param['sweeps']
        for i in xrange(run_param['sweeps']):
            spikes_elec[i] = [None]*run_param['n_elec']
            widths_I_elec[i] = [None]*run_param['n_elec']
            widths_II_elec[i] = [None]*run_param['n_elec']
            amps_I_elec[i] = [None]*run_param['n_elec']

        widths_I_elec_mean  = np.zeros([run_param['sweeps'], run_param['n_elec']])
        widths_I_elec_std  = np.zeros([run_param['sweeps'], run_param['n_elec']])

        widths_II_elec_mean = np.zeros([run_param['sweeps'], run_param['n_elec']])
        widths_II_elec_std = np.zeros([run_param['sweeps'], run_param['n_elec']])
        
        amps_I_elec_mean = np.zeros([run_param['sweeps'], run_param['n_elec']])
        amps_I_elec_std = np.zeros([run_param['sweeps'], run_param['n_elec']])

        # Electrode spikes and widths.
        for i in xrange(run_param['sweeps']):
            for j in xrange(run_param['n_elec']):

                with warnings.catch_warnings():
                    # Ignore warnings when not finding spikes.
                    warnings.simplefilter("ignore", category=RuntimeWarning)

                    spikes_elec[i][j], spikes_t_vec, I = de.extract_spikes(
                        data['t_vec'],
                        data['v_vec_elec'][i, j],
                        pre_dur=process_param['pre_dur'],
                        post_dur=process_param['post_dur'],
                        threshold=process_param['threshold'],
                        amp_option=process_param['amp_option'], 
                        threshold_abs=process_param['threshold_abs_elec']
                        )

                if spikes_elec[i][j].shape[0] == 0:
                    widths_I_elec[i][j] = []
                    widths_I_elec_mean[i, j] = np.nan
                    widths_II_elec[i][j] = []
                    widths_II_elec_mean[i, j] = np.nan
                    amps_I_elec_mean[i, j] = np.nan
                    amps_I_elec_std[i, j] = np.nan
                    continue

                widths_I_elec[i][j], trace = de.find_wave_width_type_I(
                    spikes_elec[i][j],
                    dt=data['dt'],
                    )

                widths_II_elec[i][j], trace = de.find_wave_width_type_II(
                    spikes_elec[i][j],
                    dt=data['dt'],
                    amp_option=process_param['amp_option'],
                    )

                widths_I_elec_mean[i, j] = np.mean(widths_I_elec[i][j])
                widths_II_elec_mean[i, j] = np.mean(widths_II_elec[i][j])
                widths_I_elec_std[i, j] = np.sqrt(np.var(widths_I_elec[i][j]))
                widths_II_elec_std[i, j] = np.sqrt(np.var(widths_II_elec[i][j]))

                amps_I_elec[i] = de.find_amplitude_type_I(
                        spikes_elec[i][j],
                        process_param['amp_option'])

                amps_I_elec_mean[i, j] = np.mean(amps_I_elec[i][j])
                amps_I_elec_std[i, j] = np.sqrt(np.var(amps_I_elec[i][j]))

        data['spikes_t_vec'] = spikes_t_vec
        data['spikes_soma'] = spikes_soma
        data['spikes_elec'] = spikes_elec

        data['freqs'] = freqs
        data['isi'] = isi

        data['widths_I_soma'] = widths_I_soma
        data['widths_I_soma_mean'] = widths_I_soma_mean
        data['widths_I_soma_std'] = widths_I_soma_std

        data['widths_II_soma'] = widths_II_soma
        data['widths_II_soma_mean'] = widths_II_soma_mean
        data['widths_II_soma_std'] = widths_II_soma_std

        data['amps_I_soma'] = amps_I_soma
        data['amps_I_soma_mean'] = amps_I_soma_mean
        data['amps_I_soma_std'] = amps_I_soma_std

        data['widths_I_elec'] = widths_I_elec
        data['widths_I_elec_mean'] = widths_I_elec_mean
        data['widths_I_elec_std'] = widths_I_elec_std

        data['widths_II_elec'] = widths_II_elec
        data['widths_II_elec_mean'] = widths_II_elec_mean
        data['widths_II_elec_std'] = widths_II_elec_std

        data['amps_I_elec'] = amps_I_elec
        data['amps_I_elec_mean'] = amps_I_elec_mean
        data['amps_I_elec_std'] = amps_I_elec_std

        self.info['elec_positions'] = zip(data['elec_x'],
                                          data['elec_y'],
                                          data['elec_z'])