def _cmp_vector(self, vec1, vec2, reversed=False): "comparison of two vectors - dx, dy, dz, var_dx, var_dy, var_dz" if reversed: ddx = vec1.dx + vec2.dx ddy = vec1.dy + vec2.dy ddz = vec1.dz + vec2.dz else: ddx = vec1.dx - vec2.dx ddy = vec1.dy - vec2.dy ddz = vec1.dz - vec2.dz # variances var1 = vec1.var var2 = vec2.var var_ddx = var1[0] + var2[0] var_ddy = var1[1] + var2[1] var_ddz = var1[2] + var2[2] # covariances cov1 = vec1.cov cov2 = vec2.cov cov_xy = cov1[0] + cov2[0] cov_xz = cov1[1] + cov2[1] cov_yz = cov1[2] + cov2[2] #if excludeZero: # if (ddx, ddy, ddz) == (0, 0, 0): # return # append vector vec = ObsVector(fromid=vec1.fromid, toid=vec1.toid, dx=ddx, dy=ddy, dz=ddz) self._cluster.append_obs(vec) # extend covariance matrix and add values self._cluster.covmat.extend_dim(3) vec.var = (var_ddx, var_ddy, var_ddz) vec.cov = (cov_xy, cov_xz, cov_yz)
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
for v in self._obsList: v.tran_2d(coordSystem, pointList) #self._lastIndex -= 1 # retuce from 3d to 2d - one dimension def scale_cov_mat(self, factor): self.covmat.scale_(factor) if __name__ == "__main__": from gizela.data.ObsVector import ObsVector from gizela.data.CovMat import CovMat v1 = ObsVector(fromid="A", toid="B", dx=100, dy=200, dz=300) v2 = ObsVector(fromid="B", toid="C", dx=400, dy=300, dz=200) cl = ObsClusterVector() cl.append_obs(v1) cl.append_obs(v2) print cl.make_gama_xml() print cl from gizela.data.obs_table import obs_vector_table 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)