Esempio n. 1
0
    def _mean_vector(self, data):
        "returns mean of vector with same fromid and toid"
        
        # add vectors to list
        lvec = [] # helper list of vectors
        for c in data:
            for v in c:
                lvec.append(v)

        datamean = ObsClusterList() 
        dmcluster = ObsClusterVector(covmat=CovMat(dim=0, band=2))
        datamean.append_cluster(dmcluster) 
        # list of vector means and single vectors (without mean)

        cll_mean = ObsClusterList()
        cll_obs = ObsClusterList()
        cll_res = ObsClusterList()
        
        if len(lvec) <= 1:
            return # nothink to compare
        while len(lvec) > 1:
            # find vectors with same fromid and toid
            v1 = lvec.pop()
            fromid = v1.fromid
            toid = v1.toid
            lvecm = [v1] # list of vectors for mean
            lsign = [1.0] # list of signs: for revers vectors -1.0
            for v2 in lvec:
                if v2.fromid == fromid and v2.toid == toid:
                    lvecm.append(v2)
                    lvec.remove(v2)
                    lsign.append(1.0)
                    continue
                if self.revers and\
                v2.fromid == toid and v2.toid == fromid:
                    lvecm.append(v2)
                    lvec.remove(v2)
                    lsign.append(-1.0)

            # compute mean
            if float(len(lvecm)) == 1:
                # nothink to compute
                vec = lvecm[0]
                covmat = vec.covmat
                dmcluster.append_obs(vec)
                dmcluster.covmat.extend_dim(3)
                vec.covmat = covmat
                continue

            
            from numpy import mat, mean
            
            ll = [mat([i.dx, i.dy, i.dz])*sign for i,sign in zip(lvecm,lsign)] 
            # row vectors of observ.

            #lm = mean(ll, axis=0)
            #print "Mean  0:", lm
            #print "covmat0:", lvecm[0].covmat.mat

            # compute covariance matrix 
            # covariances between vectors not handled
            
            sil = [i.covmat.mat.I for i in lvecm] 
            # covariance matrice inverses inv(Sigma)
            
            sill = [si*l.T for si,l in zip(sil,ll)]
            # product inv(Sigma)*l
            
            cmm = sum(sil).I # covariance matrix of mean
            mn = cmm * sum(sill) # mean value
            #print "Mean  1:", mn
            #print "covmat1:", cmm
        
            # append clusters 
            cl_mean = ObsClusterVector(covmat=CovMat(dim=3, band=2))
            cl_obs  = ObsClusterVector(covmat=CovMat(dim=0, band=2))
            cl_res  = ObsClusterVector(covmat=None)
            cll_mean.append_cluster(cl_mean)
            cll_obs.append_cluster(cl_obs)
            cll_res.append_cluster(cl_res)
            
            # append vector mean
            vec1 = ObsVector(fromid=lvecm[0].fromid, toid=lvecm[0].toid,
                            dx=float(mn[0]), dy=float(mn[1]), dz=float(mn[2]))
            vec2 = ObsVector(fromid=lvecm[0].fromid, toid=lvecm[0].toid,
                            dx=float(mn[0]), dy=float(mn[1]), dz=float(mn[2]))
            # vec1 == vec2
            cl_mean.append_obs(vec1)
            dmcluster.append_obs(vec2)

            # set covmat of mean
            covmat = CovMat(dim=3, band=2)
            covmat.mat = cmm
            vec1.covmat = covmat
            dmcluster.covmat.extend_dim(3)
            vec2.covmat = covmat
            
            # append observations with covmat
            for v in lvecm:
                vcovmat = v.covmat
                cl_obs.append_obs(v)
                cl_obs.covmat.extend_dim(3)
                v.covmat = vcovmat

            # append residuals
            for l,v,s in zip(ll,lvecm,lsign):
                res = (mn - l.T) * s
                vec = ObsVector(fromid=v.fromid, toid=v.toid,
                                dx=float(res[0]), 
                                dy=float(res[1]),
                                dz=float(res[2]))
                cl_res.append_obs(vec)
        
        return datamean, cll_mean, cll_obs, cll_res
Esempio n. 2
0
    cl.textTable = obs_vector_table()
    print cl
    
    cl.covmat = CovMat(6,5)
    #cl.covmat.stdev = (0.01, 0.02, 0.03, 0.04, 0.05, 0.06)
    #cl.covmat.var = (0.01, 0.02, 0.03, 0.04, 0.05)
    cl.covmat.data = [[1,2,3,4,5,6], [2,3,4,5,6], [3,4,5,6], [4,5,6], [5,6], [6]]

    print cl.make_gama_xml()
    cl.textTable = obs_vector_stdev_table()
    print cl
    
    # iterator
    print "\n".join(["from=%s to=%s" % (obs.fromid, obs.toid) for obs in cl])
    
    #covmat of vectors
    print v1.covmat.data
    print v2.covmat.data
    cm = CovMat(3,2)
    cm.data = [[0.1, 0.2, 0.3],[0.2, 0.3],[0.3]]
    v1.covmat = cm
    print cl.covmat.data
    
    # transformation
    #from gizela.tran.Tran3D import Tran3D
    #tr = Tran3D()
    #from math import pi
    #tr.rotation_xyz(pi/2, pi/2, pi/2)
    #cl.tran_3d(tr)
    #print cl