コード例 #1
0
ファイル: test_covariance.py プロジェクト: sdss/mangadap
def test_samples():

    rng = numpy.random.default_rng(seed=8001)
    
    # 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)

    # Check the values are very nearly the same as the input
    assert numpy.all(numpy.absolute(c - covar.toarray()) < 0.02), 'Covariances are too different'

    # Check that `find` returns the same indices
    # NOTE: For some reason the ordering of numpy.where and
    # scipy.sparse.find are different.  So I run lexsort before checking
    # if the arrays are the same.

    coo = numpy.array([ [i,j] for i,j in zip(*numpy.where(c > 0))])
    srt = numpy.lexsort((coo[:,0], coo[:,1]))
    coo = coo[srt,:]

    _coo = numpy.array([ [i,j] for i,j,k in zip(*covar.find())])
    srt = numpy.lexsort((_coo[:,0], _coo[:,1]))
    _coo = _coo[srt,:]

    assert numpy.array_equal(coo, _coo), 'Did not find the same indices'
コード例 #2
0
ファイル: test_covariance.py プロジェクト: sdss/mangadap
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)