Ejemplo n.º 1
0
 def test_pca_ev(self):
     A = Analyzer(setup_data1(nt=70, nx=150))
     ev = A.pca_ev()
     self.assertTrue(npy.allclose(ev, npy.array(
         [ 24.98560392,  18.04025736,   3.59796042,   1.44110266,
         0.88847967,   0.45467402,   0.24646941,   0.22392288,
         0.16899249,   0.14435935]
     )))
Ejemplo n.º 2
0
 def test_pca_xrec(self):
     var = setup_data1()
     A = Analyzer(var)
     rec = A.pca_rec()
     xeof = A.pca_eof()
     xpc = A.pca_pc()
     xrec = A.pca_rec(xeof=xeof, xpc=xpc)
     self.assertTrue(npy.allclose(rec, xrec))
Ejemplo n.º 3
0
 def test_pca_eof(self):
     A = Analyzer(setup_data1(nt=70, nx=150))
     raw_eof = A.pca_eof(raw=True)
     np_eof, np_pc, np_ev = pca_numpy(A.stacked_data.T, A.npca)
     self.assertTrue(npy.allclose(npy.abs(raw_eof[:2,:2]), npy.abs(npy.array(
         [[ 0.15702435,  0.05325682],
         [ 0.14647224,  0.05082613]]))))
     eof = A.pca_eof()
     self.assertTrue(npy.allclose(npy.abs(eof[:2,:3].filled(0.)),  npy.abs(npy.array(
         [[ 0.15702435,  0., 0.14647224],
         [ 0.05325682,  0., 0.05082613]]))))
Ejemplo n.º 4
0
 def test_pca_ec(self):
     data = setup_data1(nt=70, nx=150)
     A = Analyzer(data)
     A.pca_eof()
     ec = A.pca_ec()
     pc = A.pca_pc()
     self.assertTrue(npy.allclose(ec, pc))
     ece = A.pca_ec(xeof=A.pca_eof(raw=True), xraw=True)
     self.assertTrue(npy.allclose(ece, pc))
     ecd = A.pca_ec(xdata=data)
     self.assertTrue(npy.allclose(ecd, pc))
     ecrd = A.pca_ec(xdata=A.stacked_data, xraw=True)
     self.assertTrue(npy.allclose(ecrd, pc))
     ecre = A.pca_ec(xeof=A.pca_eof(raw=True), xraw=True)
     self.assertTrue(npy.allclose(ecrd, pc))
Ejemplo n.º 5
0
    def test_pca(self):

        # spanlib
        A = Analyzer(setup_data1(nt=70, nx=50))
        sp_eof = A.pca_eof(raw=True)
        sp_pc = A.pca_pc(raw=True)
        sp_ev = A.pca_ev(raw=True)

        # numpy
        np_eof, np_pc, np_ev = pca_numpy(A.stacked_data.T, A.npca)

        # checks
        self.assertTrue(npy.allclose(sp_ev, np_ev))
        signs = npy.sign(sp_eof[0])*npy.sign(np_eof[0])
        np_eof *= npy.resize(signs, np_eof.shape)
        np_pc *= npy.resize(signs, np_pc.shape)
        self.assertTrue(npy.allclose(sp_eof, np_eof))
        self.assertTrue(npy.allclose(sp_pc, np_pc))
        self.assertTrue(npy.allclose(sp_ev, np_ev))
Ejemplo n.º 6
0
    def test_fill_simple(self):
        """Test hole filling and forecast estimation with a single variable"""
        # Init
        ref = setup_data1(nt=50, nx=120, xyfact=0)
        withholes = ref.copy()
        withholes[25:35, 50:60] = npy.ma.masked

        # Fill
        F = Filler(withholes, loglevel='error', cvfield_level=10., npca=2)
        filtered = F.filtered
        del F

        # Check
#        import pylab as P
#        P.subplot(311)
#        P.pcolor(ref, vmin=ref.min(), vmax=ref.max())
#        P.subplot(312)
#        P.pcolor(withholes, vmin=ref.min(), vmax=ref.max())
#        P.subplot(313)
#        P.pcolor(filtered, vmin=ref.min(), vmax=ref.max())
#        P.show()
#        print filtered.filled()[22:24,0]
        npy.testing.assert_almost_equal(filtered.filled()[22:24,0],
            npy.array([-0.1510115, -0.421256]))
Ejemplo n.º 7
0
 def test_pca_rec(self):
     A = Analyzer(setup_data1())
     rec1 = A.pca_rec()
     self.assertAlmostEqual(rec1.sum(), 18464.31988812385)
Ejemplo n.º 8
0
 def test_pca_pc(self):
     A = Analyzer(setup_data1(nt=70, nx=150))
     pc = A.pca_pc()
     self.assertTrue(npy.allclose(pc[:2,:2],  npy.array(
         [[ 2.75532971,   2.96740389],
         [-8.78119373,  -8.49616261]])))
Ejemplo n.º 9
0
 def setup_Analyzer3(self):
     var1 = setup_data1()
     var2 = setup_data1(nx=45, tfreq=4, xfreq=5)
     var3 = setup_data1(nt=120, nx=20)
     return Analyzer([[var1, var2], var3])