Ejemplo n.º 1
0
 def test_pca_mssa_rec(self):
     data = setup_data2(nx=30, ny=20)
     span = Analyzer(data)
     rec = span.mssa_rec(nmssa=10)
     self.assertTrue(npy.allclose(
         rec[2,:2,:2].compressed(),
         npy.array([ 0.36712853,  0.28310489,  0.27881545])))
Ejemplo n.º 2
0
 def test_mssa_eof(self):
     data = setup_data2(nx=3, ny=2)
     span = Analyzer(data)
     eof = span.mssa_eof(nmssa=4)
     self.assertTrue(npy.allclose(
         eof[1,1].compressed(),
         npy.array([ 0.10764546, -0.0763087 , -0.07211439,  0.05060925,  0.128224  ])))
Ejemplo n.º 3
0
 def test_pca_mssa_ec(self):
     data = setup_data2(nx=30, ny=20)
     span = Analyzer(data)
     span.mssa(nmssa=4)
     ec = span.mssa_ec(xeof=span.mssa_eof(), xdata=data)
     pc = span.mssa_pc()
     self.assertTrue(npy.allclose(ec, pc))
Ejemplo n.º 4
0
 def test_pca_mssa_xrec(self):
     data = setup_data2(nx=30, ny=20)
     span = Analyzer(data)
     rec = span.mssa_rec(nmssa=4)
     xeof = span.mssa_eof()
     xpc = span.mssa_pc()
     xrec = span.mssa_rec(xeof=xeof, xpc=xpc)
     self.assertTrue(npy.allclose(rec, xrec))
Ejemplo n.º 5
0
 def test_mssa_eof_restack(self):
     data = setup_data2(nx=3, ny=2)
     span = Analyzer(data)
     steof = span.mssa_eof(nmssa=4)
     stacked_eof = npy.ascontiguousarray(span.restack(steof, scale=False))
     self.assertTrue(npy.allclose(steof[1,1].compressed(), stacked_eof[:,1,1]))
     raw_eof = span._mssa_raw_eof.reshape(stacked_eof.shape)
     self.assertTrue(npy.allclose(stacked_eof, raw_eof))
Ejemplo n.º 6
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.º 7
0
 def test_ssa_xrec(self):
     data = setup_data0()
     span = Analyzer(data, nmssa=6)
     rec = span.mssa_rec()
     eof = span.mssa_eof()
     pc = span.mssa_pc()
     xrec = span.mssa_rec(xeof=eof, xpc=pc)
     self.assertTrue(npy.allclose(rec,xrec))
Ejemplo n.º 8
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.º 9
0
 def test_mssa_phases(self):
     nt = 100.
     nx = 5
     data = npy.cos(npy.linspace(0, 5*6.2, nt*nx)*npy.pi*2).reshape((nx, nt)).T
     data += npy.cos(npy.linspace(0, 5*3.4, nt*nx)*npy.pi*2).reshape((nx, nt)).T
     span = Analyzer(data)
     rec = span.mssa_rec(imodes=[-1], phases=8)
     self.assertTrue(npy.allclose(
         rec[:2,:2].ravel(),
         npy.array([ 0.5525298 ,  0.32848071,  0.15829024,  1.19338772])))
Ejemplo n.º 10
0
    def __init__(self,
                 ldataset,
                 rdataset,
                 lweights=None,
                 rweights=None,
                 lnorms=None,
                 rnorms=None,
                 lminvalid=None,
                 rminvalid=None,
                 logger=None,
                 loglevel=None,
                 zerofill=0,
                 **kwargs):

        # Loggers
        Logger.__init__(self,
                        logger=logger,
                        loglevel=loglevel,
                        **dict_filter(kwargs, 'log_'))
        self._quiet = False

        # Left and right Analyzer instances
        if zerofill == 2:
            kwargs['zerofill'] = 2
        kwleft, kwright = self._dict_filter_lr_(kwargs)
        kwargs['logger'] = self.logger
        self.lspan = Analyzer(ldataset,
                              weights=lweights,
                              norms=lnorms,
                              minvalid=lminvalid,
                              **kwargs)
        self.rspan = Analyzer(rdataset,
                              weights=rweights,
                              norms=rnorms,
                              minvalid=rminvalid,
                              **kwargs)

        # Init results
        self.clean(pca=False)

        # Check and save parameters
        self.update_params(None, **kwargs)
Ejemplo n.º 11
0
 def test_pca_mssa(self):
     data = setup_data2(nx=30, ny=20)
     span = Analyzer(data, nmssa=2)
     self.assertTrue(npy.allclose(
         span.mssa_ev(),
         npy.array([ 2482.65882191,  2355.53413819])))
     pc = span.mssa_pc()
     self.assertTrue(npy.allclose(
         span.mssa_pc()[:,:2].ravel(),
         npy.array([-19.47701848, -20.97653195,   2.99480916,  -0.20444392])))
     self.assertAlmostEqual(span.mssa_eof()[1,2,3,4], 0.0041007559488027926)
Ejemplo n.º 12
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.º 13
0
    def test_mssa_rec(self):
        data = setup_data2(nx=3, ny=2, xyfact=0)
        span = Analyzer(data)
        rec = span.mssa_rec(nmssa=20)
        print span.mssa_ev(cumsum=True, relative=True)
#        import pylab as P;P.subplot(211);P.pcolormesh(data.reshape(-1, 6));P.subplot(212);P.pcolormesh(rec.reshape(-1, 6));P.show()
        print rec[50].compressed()
        print data[50].compressed()
        print (rec-data).var()/data.var()
        self.assertTrue(npy.allclose(
            rec[50].compressed(),
            npy.array([0.30848007,   0.8622368,    0.96028445,   0.8236466,    0.56329503 ])))
Ejemplo n.º 14
0
 def analyze(self, data=None, npca=15, nmssa=15, **kwargs):
     # Setup analyzer
     if data is None: data = self._data
     kwargs['keep_invalids'] = True
     kwargs.setdefault('nvalid', 1)
     kwargs.setdefault('quiet', True)
     npca = kwargs.pop('npcamax', npca)
     nmssa = kwargs.pop('nmssamax', nmssa)
     kwargs.setdefault('window', kwargs.pop('win', None))
     span = self.span = Analyzer(data, logger=self.logger, **kwargs)
     span.update_params()
     self.mask = span.invalids
     return span
Ejemplo n.º 15
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.º 16
0
 def test_ssa(self):
     data = setup_data0()
     span = Analyzer(data)
     span.mssa(nmssa=4)
     self.assertTrue(npy.allclose(
         span._mssa_raw_ev[0],
         npy.array([ 20.25648796,  16.57547325,  16.51572589,  16.31088181])))
     self.assertTrue(npy.allclose(
         span._mssa_raw_pc[0][:2,:2],
         npy.array([[ 6.9105607 ,  0.32054539],
    [ 5.77266597,  2.92740569]])))
     self.assertTrue(npy.allclose(
         span._mssa_raw_eof[0][:2,:2],
         npy.array([[ 0.13637961, -0.12778613],
    [ 0.13417614,  0.13187513]])))
Ejemplo n.º 17
0
    def test_mssa(self):
        data = setup_data2(nx=3, ny=2)
        span = Analyzer(data)
        span.mssa(nmssa=4)
        print data.reshape(120, 6).mask
#        self.assertTrue(npy.allclose(
#            span._mssa_raw_ev,
#            npy.array([ 84.65284564,  39.94531834,  36.70936929,  21.49023578])))
        print span._mssa_raw_pc[:2,:2]
        self.assertTrue(npy.allclose(
            span._mssa_raw_pc[:2,:2],
            npy.array([[-13.66377591,   2.62887011],
       [-13.28622293,   2.74443558]])))
        self.assertTrue(npy.allclose(
            span._mssa_raw_eof[:2,:2],
            npy.array([[ 0.05067246,  0.10596479],
       [ 0.05300994,  0.10764546]])))
Ejemplo n.º 18
0
    def test_pca_rec(self):
        """Test a PCA analysis and reconstruction with a gap inside"""
        # Init
        data = gensin2d(xnper=5, xper=30, ynper=5, yper=20)
        data[0:30, 0:55] = npy.ma.masked

        # Fill
        A = Analyzer(data, npca=10)
        rec = A.pca_rec()

        # Check
#        import pylab as P
#        P.subplot(211)
#        P.pcolor(data, vmin=data.min(), vmax=data.max())
#        P.subplot(212)
#        P.pcolor(rec, vmin=data.min(), vmax=data.max())
#        P.show()
        self.assertAlmostEqual(((data-rec)**2).sum()/(data**2).sum()*100,
            0.064630381956367611)
Ejemplo n.º 19
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.º 20
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])
Ejemplo n.º 21
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.º 22
0
 def test_ssa_rec(self):
     data = setup_data0()
     span = Analyzer(data)
     rec = span.mssa_rec(nmssa=6)
     self.assertTrue(npy.allclose(rec[:4],
         npy.array([  2.14699609,  1.3861899 ,  2.70336958,  0.70994501])))
Ejemplo n.º 23
0
 def test_pca_rec(self):
     A = Analyzer(setup_data1())
     rec1 = A.pca_rec()
     self.assertAlmostEqual(rec1.sum(), 18464.31988812385)
Ejemplo n.º 24
0
 def test_pca_ndim3(self):
     A = Analyzer(setup_data2())
     A.pca()
     self.assertTrue(npy.allclose(
         npy.abs(A.pca_eof(raw=True)[190:193,5]),
         npy.abs(npy.array([-0.03623882, -0.02312414, -0.00942417]))))