def test_convolve_1D_kernels(self): """ Check if convolving two kernels with each other works correctly. """ gauss_1 = Gaussian1DKernel(3) gauss_2 = Gaussian1DKernel(4) test_gauss_3 = Gaussian1DKernel(5) with pytest.warns(AstropyUserWarning, match=r'Both array and kernel ' r'are Kernel instances'): gauss_3 = convolve(gauss_1, gauss_2) assert np.all(np.abs((gauss_3 - test_gauss_3).array) < 0.01)
def test_kernel1d_int_size(self): """ Test that an error is raised if ``Kernel1D`` ``x_size`` is not an integer. """ with pytest.raises(TypeError): Gaussian1DKernel(3, x_size=1.2)
def test_multiply_kernel1d_kernel2d(self): """ Test that multiplying a 1D kernel with a 2D kernel raises an exception. """ with pytest.raises(Exception): Gaussian1DKernel(3) * Gaussian2DKernel(3)
def test_rmultiply_scalar_type(self, number): """ Check if multiplying a kernel with a scalar works correctly. """ gauss = Gaussian1DKernel(3) gauss_new = gauss * number assert type(gauss_new) is Gaussian1DKernel
def test_multiply_scalar(self, number): """ Check if multiplying a kernel with a scalar works correctly. """ gauss = Gaussian1DKernel(3) gauss_new = number * gauss assert_almost_equal(gauss_new.array, gauss.array * number, decimal=12)
def smooth1d(data, fwhm): """ Convolve data with 1d Gaussian kernel and return. For cube data, the convolution applies only to velocity axis. Parameters: data : ndarray (1d or 3d array) fwhm : float FWHM size of 1d Gaussian kernel. Returns: ndarray (smoothed data) """ if float(fwhm) == 0.: return data kernel = Gaussian1DKernel(fwhm/_f2s) kl = len(kernel.array) data = np.r_[data[kl-1:0:-1], data, data[-2:-kl-1:-1]] if len(data.shape) == 1: return convolve(data, kernel, boundary=None)[kl-1:-kl+1] elif len(data.shape) == 2: raise TypeError('1D-smoothing can not be applied to 2D-array.') elif len(data.shape) == 3: smodata = np.full(data.shape, np.nan) for i in range(data.shape[1]): for j in range(data.shape[2]): smodata[:, i, j] = convolve(data[:, i, j], kernel, boundary=None) return smodata[kl-1:-kl+1]
def test_model_1D_kernel(self): """ Check Model1DKernel against Gaussian1Dkernel """ stddev = 5. gauss = Gaussian1D(1. / np.sqrt(2 * np.pi * stddev**2), 0, stddev) model_gauss_kernel = Model1DKernel(gauss, x_size=21) gauss_kernel = Gaussian1DKernel(stddev, x_size=21) assert_almost_equal(model_gauss_kernel.array, gauss_kernel.array, decimal=12)
def test_scipy_filter_gaussian(self, width): """ Test GaussianKernel against SciPy ndimage gaussian filter. """ gauss_kernel_1D = Gaussian1DKernel(width) gauss_kernel_1D.normalize() gauss_kernel_2D = Gaussian2DKernel(width) gauss_kernel_2D.normalize() astropy_1D = convolve(delta_pulse_1D, gauss_kernel_1D, boundary='fill') astropy_2D = convolve(delta_pulse_2D, gauss_kernel_2D, boundary='fill') scipy_1D = filters.gaussian_filter(delta_pulse_1D, width) scipy_2D = filters.gaussian_filter(delta_pulse_2D, width) assert_almost_equal(astropy_1D, scipy_1D, decimal=12) assert_almost_equal(astropy_2D, scipy_2D, decimal=12)
def test_array_keyword_not_allowed(self): """ Regression test for issue #10439 """ x = np.ones([10, 10]) with pytest.raises(TypeError, match=r".* allowed .*"): AiryDisk2DKernel(2, array=x) Box1DKernel(2, array=x) Box2DKernel(2, array=x) Gaussian1DKernel(2, array=x) Gaussian2DKernel(2, array=x) RickerWavelet1DKernel(2, array=x) RickerWavelet2DKernel(2, array=x) Model1DKernel(Gaussian1D(1, 0, 2), array=x) Model2DKernel(Gaussian2D(1, 0, 0, 2, 2), array=x) Ring2DKernel(9, 8, array=x) Tophat2DKernel(2, array=x) Trapezoid1DKernel(2, array=x) Trapezoid1DKernel(2, array=x)
def run_final(model, scale_factor, center_ego_params, center_dist_params, allo_params, speed_params, Xe, Xd, Xa, Xs, spike_train): ''' run the model and collect the results ''' if model != 'uniform': u = np.zeros(len(spike_train)) if 'center_ego' in model: u += Xe * center_ego_params if 'center_dist' in model: u += Xd * center_dist_params if 'allo' in model: u += Xa * allo_params if 'speed' in model: u += Xs * speed_params rate = np.exp(u) else: rate = np.mean(spike_train) f = -np.sum(rate * scale_factor - spike_train * np.log(rate * scale_factor)) lgammas = np.zeros(len(spike_train)) for h in range(len(spike_train)): lgammas[h] = np.log(math.gamma(spike_train[h] + 1)) f -= np.sum(lgammas) #change from nats to bits f = f / np.log(2) llps = f / np.sum(spike_train) #calculate pearson r between the estimated spike train and #actual spike train, first smoothing with a gaussian filter smoothed_spikes = convolve(spike_train, Gaussian1DKernel(stddev=2, x_size=11)) r, p = pearsonr(smoothed_spikes, rate * scale_factor) if np.isnan(r): r = 0 #now calculate the percent of variance explained by the model estimates mean_fr = np.mean(smoothed_spikes) explained_var = 1 - np.nansum( (smoothed_spikes - rate * scale_factor)**2) / np.sum( (smoothed_spikes - mean_fr)**2) pseudo_r2 = 1 - np.nansum(spike_train * np.log(spike_train / (rate * scale_factor)) - (spike_train - rate * scale_factor)) / np.nansum( spike_train * np.log(spike_train / np.mean(spike_train))) uniform_r2 = 0 pseudo_r2 = pseudo_r2 - uniform_r2 print('-----------------------') print(model) print(' ') print('scale_factor: %f' % scale_factor) print('log-likelihood: %f' % f) print('llps: %f' % llps) print('correlation: %f' % r) print('explained_var: %f' % explained_var) print('pseudo_r2: %f' % pseudo_r2) print(' ') print('-----------------------') cdict = {} #add relevant variables to the appropriate dictionary cdict['ll'] = f if np.sum(spike_train) > 0: cdict['llps'] = float(f / np.sum(spike_train)) else: cdict['llps'] = f cdict['lambda'] = rate * scale_factor cdict['corr_r'] = r cdict['pseudo_r2'] = pseudo_r2 cdict['explained_var'] = explained_var cdict['test_spikes'] = spike_train cdict['tot_spikes'] = np.sum(spike_train) cdict['scale_factor'] = scale_factor return cdict
def test_Gaussian1DKernel_even_size(self): """ Check if even size for GaussianKernel works. """ gauss = Gaussian1DKernel(3, x_size=10) assert gauss.array.size == 10
def test_add_kernel_scalar(self): """Test that adding a scalar to a kernel raises an exception.""" with pytest.raises(Exception): Gaussian1DKernel(3) + 1
def test_multiply_kernel1d(self): """Test that multiplying two 1D kernels raises an exception.""" gauss = Gaussian1DKernel(3) with pytest.raises(Exception): gauss * gauss