dx = dx[:nx] x = np.cumsum(dx) print(nx, x.shape) # x0=x[0] # ymax=y.max() # y /=ymax # x-=x[0] # inz=y.nonzero() # x=x[inz] # y=y[inz] # y /=y.max() # x-=x[0] # plt.plot(xx,yy);plt.show() interp = BSpline(x, y, splorder) nx = x.shape[0] icycle = y.shape[0] // npeaks icyclef = y.shape[0] / npeaks print(icycle, '\n', icyclef) nx2 = nx * dnx dx2 = dx.min() / dnx # nx3 = int(nx*icycle / np.gcd(nx, icycle)) # print(nx2,nx3) # nx2= nx3 xx = np.linspace(x[0], dx2 * nx2, nx2, endpoint=True) print('xx\n', xx) yy = interp(xx) print('yy\n', yy)
def solar(self, p, type='Kurucz', coeff=None): """ Generate stellar model type: 'Kurucz' or 'Spline' """ # make v increasing order v = self.v logv = self.logv # Extract RVs from param array - make first index 0 #rvshifts = np.array([p['rv_%s'%i] for i in self.specnums]) rvshift = p['rv_shift'] rvshifts = np.log(1 - (-1 * (self.vel + rvshift)) / self.c) # Create all_v array , log space so can add all_logv = np.tile(logv,(self.nspec,1)) + \ np.tile(rvshifts,(self.npoints,1)).T + \ np.tile(np.log(1 - self.dnu/self.c),(self.npoints,1)).T all_v = np.tile(v,(self.nspec,1)) * \ np.tile(np.exp(rvshifts),(self.npoints,1)).T * \ np.tile(1 - self.dnu/self.c,(self.npoints,1)).T if type == 'Kurucz': try: vstel = self.vstel stel = self.stel except AttributeError: # Load stellar but only do this once stellar = fits.open(self.input_path + 'STELLAR/irradthuwn.fits') stel_v = stellar[1].data['wavenumber'] stel_s = stellar[1].data['irradiance'] stellar.close() vlo, vhi = np.min(v), np.max(v) isub = np.where((stel_v > vlo - 0.2) & (stel_v < vhi + 0.2))[0] stel = -1 * np.log(stel_s[isub] / np.max(stel_s[isub])) self.vstel = stel_v[isub] self.stel = -1 * np.log(stel_s[isub] / np.max(stel_s[isub])) # resample spec at each stel stell_arr = np.zeros((self.nspec, self.npoints)) for i in range(len(rvshifts)): tck = scipy.interpolate.splrep(self.vstel, self.stel, s=0) stell_arr[i] = scipy.interpolate.splev(all_v[i], tck, der=0, ext=1) # Return, flip back array if had to flip v order self.stell_arr = stell_arr #.T[::-1].T if flipped else stell_arr if type == 'Spline': try: self.knots except AttributeError: self.get_knots() coeffs = np.array( [p['coeff_%i' % i].value for i in range(len(self.knots))]) spl = BSpline(self.knots, coeffs, 3) # Generate stellar spec shifted stell_arr = spl(all_v) self.stell_arr = stell_arr #.T[::-1].T if flipped else stell_arr # no longer need to flip but now vel's go opposite way
resnet_accuracy = resnet_accuracy[sort_indices] # shift_values = shift_values[sort_indices] # originalPhase = 1.570796326794896558 # shift_values = shift_values - originalPhase # degrees = shift_values*1500/360 # seconds = degrees*3600 optimal_observer_accuracy = optimal_observer_accuracy[sort_indices] fig = plt.figure() ax = plt.axes() plt.xscale('log') plt.xlabel('Contrast values') plt.ylabel('Accuracy') plt.title('Freq 1 accuracy for various shifts') if smooth: x_new = np.linspace(contrast_values.min(), contrast_values.max(), 200) spl = BSpline() resnet_smooth = spline(contrast_values, resnet_accuracy, x_new) optimal_observer_smooth = spline(contrast_values, optimal_observer_accuracy, x_new) plt.plot(x_new, optimal_observer_smooth, label='Optimal Observer Accuracy') plt.plot(x_new, resnet_smooth, label='ResNet Accuracy') else: plt.plot(contrast_values, optimal_observer_accuracy, label='Optimal Observer Accuracy') plt.plot(contrast_values, resnet_accuracy, label='ResNet Accuracy') plt.legend(frameon=True) out_path = os.path.dirname(csv_path) fig.savefig(os.path.join(out_path, 'accuracy_curves.png'), dpi=200)
x0_prime = np.min(rot_rowp) xmax_prime = np.max(rot_rowp) x_dense = np.linspace(x0_prime, xmax_prime, 2000) @np.vectorize def arclength(x): '''Input x1_prime, get out arclength''' gi = x_dense <x s_integrand = np.sqrt(1 + p5_deriv(x_dense[gi]) ** 2) s = np.trapz(s_integrand, x=x_dense[gi]) return s al = arclength(rot_rowp)*4.0 #apply high-pass filter using BSplines with N-day breakpoints from scipy.interpolate import BSpline from scipy import interpolate N = 1.5 interior_knots = np.arange(time[0]+, time[0]+6, N) t,c,k = interpolate.splrep(time, flux, s=0, task=-1, t=interior_knots) bspl = BSpline(t,c,k) flux = flux/bspl(time) #remove data during thruster firing good = al = arclength(rot_rowp[good]) * platescale
# xx=np.copy(x) # yy=np.copy(y) # dx=np.diff(xx).min() # inz = y.nonzero() # x=x[inz] # y=y[inz] order = 1 dx = (1 / bpm) * 2 # dx = np.diff(dx).min()/2 x0, xf = x.min(), x.max() xx = np.arange(x0, xf, dx) # scipy.interpolate.interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False) interp = BSpline(x, y, order) yy = interp(xx) # dy = interp(xx,1) # ddy=interp(xx,2) nx = xx.shape[0] fx = fftfreq(nx, dx) # fx = fftfreq(xx.shape[0]//2, dx) fy = scipy.fft(yy) afy = np.abs(fy) fx = fx[:nx // 2 + 1] afy = afy[:nx // 2 + 1] print(fx.shape, fy.shape)
def test_calculate_sensitivity_from_science_equals_one_and_table_equals_one( path_to_outputs): def _create_fake_data(): astrofaker = pytest.importorskip('astrofaker') hdu = fits.ImageHDU() hdu.header['CCDSUM'] = "1 1" hdu.data = np.zeros((1000, 1)) _ad = astrofaker.create('GMOS-S') _ad.add_extension(hdu, pixel_scale=1.0) _ad[0].data = _ad[0].data.ravel() + 1. _ad[0].mask = np.zeros(_ad[0].data.size, dtype=np.uint16) # ToDo Requires mask _ad[0].variance = np.ones_like(_ad[0].data) # ToDo Requires Variance _ad[0].phu.set('OBJECT', "DUMMY") _ad[0].phu.set('EXPTIME', 1.) _ad[0].hdr.set('BUNIT', "electron") _ad[0].hdr.set('CTYPE1', "Wavelength") _ad[0].hdr.set('CUNIT1', "nm") _ad[0].hdr.set('CRPIX1', 1) _ad[0].hdr.set('CRVAL1', 350.) _ad[0].hdr.set('CDELT1', 0.1) _ad[0].hdr.set('CD1_1', 0.1) assert _ad.object() == 'DUMMY' assert _ad.exposure_time() == 1 _ad.create_gwcs() return _ad def _create_fake_table(): wavelengths = np.arange(200., 900., 5) * u.Unit("nm") flux = np.ones(wavelengths.size) * u.Unit("erg cm-2 s-1 AA-1") bandpass = np.ones(wavelengths.size) * u.Unit("nm") _table = QTable([wavelengths, flux, bandpass], names=['WAVELENGTH', 'FLUX', 'FWHM']) _table.name = os.path.join(path_to_outputs, 'std_data.dat') _table.write(_table.name, format='ascii.ecsv') return _table.name def _get_wavelength_calibration(hdr): from astropy.modeling.models import Linear1D, Const1D _wcal_model = ( Const1D(hdr.get('CRVAL1')) + Linear1D(slope=hdr.get('CD1_1'), intercept=hdr.get('CRPIX1')-1)) assert _wcal_model(0) == hdr.get('CRVAL1') return _wcal_model table_name = _create_fake_table() ad = _create_fake_data() p = primitives_gmos_spect.GMOSSpect([ad]) s_ad = p.calculateSensitivity(bandpass=5, filename=table_name, order=6).pop() assert hasattr(s_ad[0], 'SENSFUNC') for s_ext in s_ad: sens_table = s_ext.SENSFUNC sens_model = BSpline( sens_table['knots'].data, sens_table['coefficients'].data, 3) wcal_model = _get_wavelength_calibration(s_ext.hdr) wlengths = (wcal_model(np.arange(s_ext.data.size + 1)) * u.nm).to( sens_table['knots'].unit) sens_factor = sens_model(wlengths) * sens_table['coefficients'].unit np.testing.assert_allclose(sens_factor.physical.value, 1, atol=1e-4)
def spline_anomaly(df, smooth=0.5, plot=False, B=False, turning=False, filtered=False, threshold=1): """ Accepts: a Pandas dataframe of shape: obmt rate w1_rate 1. float float float or equivalent. This dataframe should be a hit neighbourhood as generated by isolate_anomaly(). The function will work on larger datasets but there is nothing to be gained by splining these - and the time taken to do so would be significantly larger. Runs scipy's splining algorithms on the hit neighbourhoods to generate a spline to fit the data. Kwargs: smooth (float, default=0.5): smoothing factor for the generated splines. plot (bool, default=False): if True, generates a plot of the data and the spline. B (bool, default=False): if True, generates B splines. turning (bool, default=False): if True, plots all the turning points alongside a normal plot. filtered (bool, default=False): if True, plots the filterd turning points alongside a normal plot. threshold (float, default=1.0): the threshold for filtering turning points Returns: tuple of the arrays of knots and coefficients (in that order) of the fitted spline. """ # Create spline. spl = UnivariateSpline(df['obmt'], df['rate'] - df['w1_rate']) spl.set_smoothing_factor(smooth) knots, coeffs = (spl.get_knots(), spl.get_coeffs()) xs = np.linspace(df['obmt'].tolist()[0], df['obmt'].tolist()[-1], 10000) if B: spl = BSpline(knots, coeffs, 3) # Plot original data and spline. if plot or turning or filtered: plt.scatter(df['obmt'], df['rate'] - df['w1_rate']) plt.plot(xs, spl(xs)) if filtered or turning: # Calls get_turning_points() to isolate the turning points. if turning: turning_points = get_turning_points(df) elif filtered: turning_points = filter_turning_points(get_turning_points(df), threshold=threshold) plt.scatter(turning_points['obmt'], turning_points['rate'] - turning_points['w1_rate'], color='red') plt.show() return (knots, coeffs)