def test_io(): rng = numpy.random.default_rng(seed=8001) # Clean up in case of a failure ofile = 'test_covar_io.fits' if os.path.isfile(ofile): os.remove(ofile) # Build a bogus covariance matrix m = numpy.zeros(10, dtype=float) c = numpy.diag(numpy.full(10-2, 0.2, dtype=float), k=-2) \ + numpy.diag(numpy.full(10-1, 0.5, dtype=float), k=-1) \ + numpy.diag(numpy.full(10, 1.0, dtype=float), k=0) \ + numpy.diag(numpy.full(10-1, 0.5, dtype=float), k=1) \ + numpy.diag(numpy.full(10-2, 0.2, dtype=float), k=2) # Draw samples s = rng.multivariate_normal(m, c, size=100000) # Instantiate covar = Covariance.from_samples(s.T, cov_tol=0.1) # Write covar.write(ofile) # Read _covar = Covariance.from_fits(ofile) # Should be the same assert numpy.allclose(covar.toarray(), _covar.toarray()), 'Bad I/O' # Clean-up os.remove(ofile)
def test_read_drp(): cfg = MaNGAConfig(7815, 3702) drpfile = remote_data_file(cfg.file_name) assert os.path.isfile(drpfile), 'Did not find file' with fits.open(drpfile) as hdu: covar = Covariance.from_fits(hdu, ivar_ext=None, covar_ext='GCORREL', impose_triu=True, correlation=True) var = numpy.ma.power(hdu['IVAR'].data[hdu['GCORREL'].header['BBINDEX']].T.ravel(), -1).filled(0.0) covar = covar.apply_new_variance(var) covar.revert_correlation() assert numpy.array_equal(var, numpy.diag(covar.toarray())), 'New variance not applied'
# Read the DRP file print('reading') drpf = DRPFits(8138, 1901, 'CUBE', directory_path='.', read=True) # Get the variance at the wavelength used for the g-band correlation # matrix bbindex = int(drpf['GCORREL'].header['BBINDEX']) var = numpy.ma.power(drpf['IVAR'].data[:, :, bbindex].ravel(), -1).filled(0.0) # Use the Covariance class to setup the correlation matrix using the # DRP GCORREL extension C = Covariance.from_fits(drpf.hdu, ivar_ext=None, covar_ext='GCORREL', correlation=True, impose_triu=True, row_major=True) C.show() C = C.apply_new_variance(var) C.show() C.revert_correlation() C.show() # Recalculate the covariance from scratch recalc_C = drpf.covariance_matrix(bbindex) recalc_C.show() # Calculate the difference between the recalculation at the DRP # output diff = numpy.ma.MaskedArray(recalc_C.toarray() - C.toarray())