def test_midpoints_of_series(): assert (np.array([1, 3, 5, 7, 9]) == misc.midpoints_of_series( np.array([0, 2, 4, 6, 8, 10]))).all() assert (np.array([-3, 4.5, 65]) == misc.midpoints_of_series( np.array([-5, -1, 10, 120]))).all() assert np.array([-499.9, 500.1, 50500]) == pytest.approx( misc.midpoints_of_series(np.array([-1000, 0.20, 1000, 100000])))
def get_bins(self): self.x_bins = tools.get_bins(self.x, self.bin_size, min_val=0, max_val=self.x_max) self.y_bins = tools.get_bins(self.y, self.bin_size, min_val=0, max_val=self.y_max) self.x_bin_centers = midpoints_of_series(self.x_bins) self.y_bin_centers = midpoints_of_series(self.y_bins)
def velocity_hist(self): logging.info("Calculating velocity histogram") self.bins = tools.get_bins(self.velocity, self.bin_size) frames_per_velocity_bin, b = np.histogram(self.velocity, self.bins) self.velocity_hist_seconds = (frames_per_velocity_bin / self.camera_frames_per_sec) self.velocity_hist_centers = midpoints_of_series(b)
def ahv_hist(self): logging.info("Calculating angular head velocity histogram") self.bins = tools.get_bins(self.ang_vel, self.bin_size, deal_with_decimals=False) frames_per_ang_vel_bin, b = np.histogram(self.ang_vel, self.bins) self.ahv_hist_seconds = (frames_per_ang_vel_bin / self.camera_frames_per_sec) self.ahv_hist_centers = midpoints_of_series(b)
def get_spike_hist_single( angles_w_firing, firing_weighting, bin_spacing, head_angle_sampling ): spikes_per_bin, b = np.histogram( angles_w_firing, weights=firing_weighting, bins=np.arange(0, 360 + bin_spacing, bin_spacing), density=False, ) spikes_per_bin = np.divide(spikes_per_bin, head_angle_sampling) bin_centers = np.deg2rad(midpoints_of_series(b)) return spikes_per_bin, bin_centers
def radial_spike_histogram( angle_timeseries, spikes_timeseries, bin_width, bin_occupancy=None, normalise=False, degrees=True, smooth_width=None, ): """ From a timeseries of angles and spikes, calculate a radial spiking histogram :param angle_timeseries: array like timeseries of angles :param spikes_timeseries: array like timeseries of spikes, with N spikes per timepoint :param bin_width: Size of bin used for histogram :param bin_occupancy: Array like timeseries of temporal occupancy of bins. If specified, the relative spike rates will be returned. :param normalise: Normalise the resulting histogram :param degrees: Use degrees, rather than radians :param smooth_width: If not None, smooth with a kernel of this size :return: Spikes (or spike rate) per radial bin and histogram bin centers (in radians) """ spikes_per_bin, bins = np.histogram( angle_timeseries, weights=spikes_timeseries, bins=radial_bins(bin_width, degrees=degrees), density=normalise, ) if smooth_width is not None: smooth_width_sigma = int(round(smooth_width / bin_width)) # if the smooth width is less than the bin size, set it to # the bin size if smooth_width_sigma < 1: smooth_width_sigma = 1 spikes_per_bin = filters.gaussian_filter1d(spikes_per_bin, smooth_width_sigma, mode="wrap") if bin_occupancy is not None: spikes_per_bin = np.divide(spikes_per_bin, bin_occupancy) if degrees: bin_centers = np.deg2rad(midpoints_of_series(bins)) return spikes_per_bin, bin_centers