def testConsistencyOfBases(self): m = 6 sa = iisignature.rotinv2dprepare(m, "a") sk = iisignature.rotinv2dprepare(m, "k") ca = iisignature.rotinv2dcoeffs(sa)[-1] ck = iisignature.rotinv2dcoeffs(sk)[-1] #every row of ck should be in the span of the rows of ca #i.e. every column of ck.T should be in the span of the columns of #ca.T #i.e. there's a matrix b s.t. ca.T b = ck.T residuals = lstsq(ca.T, ck.T)[1] self.assertLess(numpy.max(numpy.abs(residuals)), 0.000001) sq = iisignature.rotinv2dprepare(m, "q") cq = iisignature.rotinv2dcoeffs(sq)[-1] ss = iisignature.rotinv2dprepare(m, "s") cs = iisignature.rotinv2dcoeffs(ss)[-1] # every row of cs and cq should be in the span of the rows of ca residuals2 = lstsq(ca.T, cs.T)[1] self.assertLess(numpy.max(numpy.abs(residuals2)), 0.000001) residuals2 = lstsq(ca.T, cq.T)[1] self.assertLess(numpy.max(numpy.abs(residuals2)), 0.000001) self.assertEqual(cq.shape, cs.shape) #check that the invariants are linearly independent (not for k) for c, name in ((cs, "s"), (ca, "a"), (cq, "q")): self.assertEqual(numpy.linalg.matrix_rank(c), c.shape[0], name) #check that rows with nonzeros in evil columns are all before #rows with nonzeros in odious columns #print ((numpy.abs(ca)>0.00000001).astype("int8")) for c, name in ((cs, "s"), (ck, "k"), (ca, "a"), (cq, "q")): evilRows = [] odiousRows = [] for i in range(c.shape[0]): evil = 0 odious = 0 for j in range(c.shape[1]): if numpy.abs(c[i, j]) > 0.00001: if bin(j).count("1") % 2: odious = odious + 1 else: evil = evil + 1 if evil > 0: evilRows.append(i) if odious > 0: odiousRows.append(i) #print (evilRows, odiousRows) self.assertLess(numpy.max(evil), numpy.min(odious), "bad order of rows in " + name)
def testConsistencyOfBases(self): m = 6 sa = iisignature.rotinv2dprepare(m,"a") sk = iisignature.rotinv2dprepare(m,"k") ca = iisignature.rotinv2dcoeffs(sa)[-1] ck = iisignature.rotinv2dcoeffs(sk)[-1] #every row of ck should be in the span of the rows of ca #i.e. every column of ck.T should be in the span of the columns of #ca.T #i.e. there's a matrix b s.t. ca.T b = ck.T residuals = lstsq(ca.T,ck.T)[1] self.assertLess(numpy.max(numpy.abs(residuals)),0.000001) sq = iisignature.rotinv2dprepare(m, "q") cq = iisignature.rotinv2dcoeffs(sq)[-1] ss = iisignature.rotinv2dprepare(m, "s") cs = iisignature.rotinv2dcoeffs(ss)[-1] # every row of cs and cq should be in the span of the rows of ca residuals2 = lstsq(ca.T, cs.T)[1] self.assertLess(numpy.max(numpy.abs(residuals2)), 0.000001) residuals2 = lstsq(ca.T, cq.T)[1] self.assertLess(numpy.max(numpy.abs(residuals2)), 0.000001) self.assertEqual(cq.shape, cs.shape) #check that the invariants are linearly independent (not for k) for c, name in ((cs, "s"), (ca, "a"), (cq, "q")): self.assertEqual(numpy.linalg.matrix_rank(c),c.shape[0],name) #check that rows with nonzeros in evil columns are all before #rows with nonzeros in odious columns #print ((numpy.abs(ca)>0.00000001).astype("int8")) for c, name in ((cs, "s"), (ck, "k"), (ca, "a"), (cq, "q")): evilRows = [] odiousRows = [] for i in range(c.shape[0]): evil = 0 odious = 0 for j in range(c.shape[1]): if numpy.abs(c[i, j]) > 0.00001: if bin(j).count("1") % 2: odious = odious + 1 else: evil = evil + 1 if evil > 0: evilRows.append(i) if odious > 0: odiousRows.append(i) #print (evilRows, odiousRows) self.assertLess(numpy.max(evil),numpy.min(odious),"bad order of rows in " + name)
def dotest(self,type): m = 8 nPaths = 95 nAngles = 348 numpy.random.seed(775) s = iisignature.rotinv2dprepare(m,type) coeffs = iisignature.rotinv2dcoeffs(s) angles = numpy.random.uniform(0,math.pi * 2,size=nAngles + 1) angles[0] = 0 rotationMatrices = [numpy.array([[math.cos(i),math.sin(i)],[-math.sin(i),math.cos(i)]]) for i in angles] paths = [numpy.random.uniform(-1,1,size=(32,2)) for i in range(nPaths)] samePathRotInvs = [iisignature.rotinv2d(numpy.dot(paths[0],mtx),s) for mtx in rotationMatrices] #check the length matches (length,) = samePathRotInvs[0].shape self.assertEqual(length,sum(i.shape[0] for i in coeffs)) self.assertEqual(length,iisignature.rotinv2dlength(s)) if type == "a": self.assertEqual(length,sumCentralBinomialCoefficient(m // 2)) self.assertLess(length,nAngles)#sanity check on the test itself #check that the invariants are invariant if 0: print("\n",numpy.column_stack(samePathRotInvs[0:7])) for i in range(nAngles): if 0 and diff(samePathRotInvs[0],samePathRotInvs[1 + i]) > 0.01: print(i) print(samePathRotInvs[0] - samePathRotInvs[1 + i]) print(diff(samePathRotInvs[0],samePathRotInvs[1 + i])) self.assertLess(diff(samePathRotInvs[0],samePathRotInvs[1 + i]),0.01) #check that the invariants match the coefficients if 1: sigLevel=iisignature.sig(paths[0],m)[iisignature.siglength(2,m-1):] lowerRotinvs = 0 if 2==m else iisignature.rotinv2dlength(iisignature.rotinv2dprepare(m-2,type)) #print("\n",numpy.dot(coeffs[-1],sigLevel),"\n",samePathRotInvs[0][lowerRotinvs:]) #print(numpy.dot(coeffs[-1],sigLevel)-samePathRotInvs[0][lowerRotinvs:]) self.assertTrue(numpy.allclose(numpy.dot(coeffs[-1],sigLevel),samePathRotInvs[0][lowerRotinvs:],atol=0.000001)) #check that we are not missing invariants if type == "a": #print("\nrotinvlength=",length," #siglength=",iisignature.siglength(2,m)) sigOffsets = [] for path in paths: samePathSigs = [iisignature.sig(numpy.dot(path,mtx),m) for mtx in rotationMatrices[1:70]] samePathSigsOffsets = [i - samePathSigs[0] for i in samePathSigs[1:]] sigOffsets.extend(samePathSigsOffsets) #print(numpy.linalg.svd(numpy.row_stack(sigOffsets))[1]) def split(a, dim, level): start = 0 out = [] for m in range(1,level + 1): levelLength = dim ** m out.append(a[:,start:(start + levelLength)]) start = start + levelLength assert(start == a.shape[1]) return out allOffsets = numpy.row_stack(sigOffsets) #print (allOffsets.shape) splits = split(allOffsets,2,m) #print() rank_tolerance = 0.01 # this is hackish #print #([numpy.linalg.matrix_rank(i.astype("float64"),rank_tolerance) #for #i in splits]) #print ([i.shape for i in splits]) #print(numpy.linalg.svd(splits[-1])[1]) #sanity check on the test self.assertLess(splits[-1].shape[1],splits[0].shape[0]) totalUnspannedDimensions = sum(i.shape[1] - numpy.linalg.matrix_rank(i,rank_tolerance) for i in splits) self.assertEqual(totalUnspannedDimensions,length) if 0: #This doesn't work - the rank of the whole thing is less than #sigLength-totalUnspannedDimensions, which suggests that there are #inter-level dependencies, #even though the shuffle product dependencies aren't linear. #I don't know why this is. sigLength = iisignature.siglength(2,m) numNonInvariant = numpy.linalg.matrix_rank(numpy.row_stack(sigOffsets)) predictedNumberInvariant = sigLength - numNonInvariant print(sigLength,length,numNonInvariant) self.assertLess(sigLength,nAngles) self.assertEqual(predictedNumberInvariant,length)
def test_batch(self): numpy.random.seed(734) d=2 m=2 n=15 paths = [numpy.random.uniform(-1,1,size=(6,d)) for i in range(n)] pathArray15=stack(paths) pathArray1315=numpy.reshape(pathArray15,(1,3,1,5,6,d)) sigs = [iisignature.sig(i,m) for i in paths] sigArray=stack(sigs) sigArray15=iisignature.sig(pathArray15,m) sigArray1315=iisignature.sig(pathArray1315,m) siglength=iisignature.siglength(d,m) self.assertEqual(sigArray1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(sigArray1315.reshape(n,siglength),sigs)) self.assertEqual(sigArray15.shape,(15,siglength)) self.assertTrue(numpy.allclose(sigArray15,sigs)) backsigs=[iisignature.sigbackprop(i,j,m) for i,j in zip(sigs,paths)] backsigArray = stack(backsigs) backsigs1315=iisignature.sigbackprop(sigArray1315,pathArray1315,m) self.assertEqual(backsigs1315.shape,(1,3,1,5,6,d)) self.assertTrue(numpy.allclose(backsigs1315.reshape(n,6,2),backsigArray)) data=[numpy.random.uniform(size=(d,)) for i in range(n)] dataArray1315=stack(data).reshape((1,3,1,5,d)) joined=[iisignature.sigjoin(i,j,m) for i,j in zip(sigs,data)] joined1315=iisignature.sigjoin(sigArray1315,dataArray1315,m) self.assertEqual(joined1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(joined1315.reshape(n,-1),stack(joined))) backjoined=[iisignature.sigjoinbackprop(i,j,k,m) for i,j,k in zip(joined,sigs,data)] backjoinedArrays=[stack([i[j] for i in backjoined]) for j in range(2)] backjoined1315=iisignature.sigjoinbackprop(joined1315,sigArray1315,dataArray1315,m) self.assertEqual(backjoined1315[0].shape,sigArray1315.shape) self.assertEqual(backjoined1315[1].shape,dataArray1315.shape) self.assertTrue(numpy.allclose(backjoined1315[0].reshape(n,-1),backjoinedArrays[0])) self.assertTrue(numpy.allclose(backjoined1315[1].reshape(n,-1),backjoinedArrays[1])) dataAsSigs=[iisignature.sig(numpy.row_stack([numpy.zeros((d,)),i]),m) for i in data] dataArray13151=dataArray1315[:,:,:,:,None,:] dataArray13151=numpy.repeat(dataArray13151,2,4)*[[0.0],[1.0]] dataArrayAsSigs1315=iisignature.sig(dataArray13151,m) combined1315=iisignature.sigcombine(sigArray1315,dataArrayAsSigs1315,d,m) self.assertEqual(joined1315.shape,combined1315.shape) self.assertTrue(numpy.allclose(joined1315,combined1315)) backcombined1315=iisignature.sigcombinebackprop(joined1315,sigArray1315,dataArrayAsSigs1315,d,m) backcombined=[iisignature.sigcombinebackprop(i,j,k,d,m) for i,j,k in zip(joined,sigs,dataAsSigs)] backcombinedArrays=[stack([i[j] for i in backcombined]) for j in range(2)] self.assertEqual(backcombined1315[0].shape,sigArray1315.shape) self.assertEqual(backcombined1315[1].shape,sigArray1315.shape) self.assertTrue(numpy.allclose(backjoined1315[0],backcombined1315[0])) self.assertTrue(numpy.allclose(backcombined1315[0].reshape(n,-1),backcombinedArrays[0])) self.assertTrue(numpy.allclose(backcombined1315[1].reshape(n,-1),backcombinedArrays[1])) scaled=[iisignature.sigscale(i,j,m) for i,j in zip(sigs,data)] scaled1315=iisignature.sigscale(sigArray1315,dataArray1315,m) self.assertEqual(scaled1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(scaled1315.reshape(n,-1),stack(scaled))) backscaled=[iisignature.sigscalebackprop(i,j,k,m) for i,j,k in zip(scaled,sigs,data)] backscaledArrays=[stack([i[j] for i in backscaled]) for j in range(2)] backscaled1315=iisignature.sigscalebackprop(scaled1315,sigArray1315,dataArray1315,m) self.assertEqual(backscaled1315[0].shape,sigArray1315.shape) self.assertEqual(backscaled1315[1].shape,dataArray1315.shape) self.assertTrue(numpy.allclose(backscaled1315[0].reshape(n,-1),backscaledArrays[0])) self.assertTrue(numpy.allclose(backscaled1315[1].reshape(n,-1),backscaledArrays[1])) s_s=(iisignature.prepare(d,m,"cosax"),iisignature.prepare(d,m,"cosahx")) for type in ("c","o","s","x","a","ch","oh","sh","ah"): s=s_s[1 if "h" in type else 0] logsigs = [iisignature.logsig(i,s,type) for i in paths] logsigArray=stack(logsigs) logsigArray1315=iisignature.logsig(pathArray1315,s,type) self.assertEqual(logsigArray1315.shape,(1,3,1,5,logsigs[0].shape[0]),type) self.assertTrue(numpy.allclose(logsigArray1315.reshape(n,-1),logsigArray),type) if type in ("s","x","sh"): backlogs = stack(iisignature.logsigbackprop(i,j,s,type) for i,j in zip(logsigs,paths)) backlogs1315 = iisignature.logsigbackprop(logsigArray1315,pathArray1315,s,type) self.assertEqual(backlogs1315.shape,backsigs1315.shape) self.assertTrue(numpy.allclose(backlogs1315.reshape(n,6,d),backlogs),type) a=iisignature.rotinv2dprepare(m,"a") rots=stack([iisignature.rotinv2d(i,a) for i in paths]) rots1315=iisignature.rotinv2d(pathArray1315,a) self.assertEqual(rots1315.shape,(1,3,1,5,rots.shape[1])) self.assertTrue(numpy.allclose(rots1315.reshape(n,-1),rots))
mem_limit_mb=30 mem_limit_b = mem_limit_mb * 1024 * 1024 if os.name=="nt": import ctypes from ctypes import wintypes kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) def errcheck_bool(result, func, args): if not result: raise ctypes.WinError(ctypes.get_last_error()) return args kernel32.GetCurrentProcess.restype = wintypes.HANDLE kernel32.SetProcessWorkingSetSizeEx.errcheck = errcheck_bool kernel32.SetProcessWorkingSetSizeEx.argtypes = (wintypes.HANDLE, ctypes.c_size_t, ctypes.c_size_t, wintypes.DWORD) hProcess = kernel32.GetCurrentProcess() kernel32.SetProcessWorkingSetSizeEx(hProcess, 204800, mem_limit_b, 6) elif os.name=="posix": import resource resource.setrlimit(resource.RLIMIT_AS,(mem_limit_b,mem_limit_b)) else: raise RuntimeError("what system") iisignature.rotinv2dprepare(14,"a") print ("ok") #input()
def test_batch(self): numpy.random.seed(734) d=2 m=2 n=15 paths = [numpy.random.uniform(-1,1,size=(6,d)) for i in range(n)] pathArray15=stack(paths) pathArray1315=numpy.reshape(pathArray15,(1,3,1,5,6,d)) sigs = [iisignature.sig(i,m) for i in paths] sigArray=stack(sigs) sigArray15=iisignature.sig(pathArray15,m) sigArray1315=iisignature.sig(pathArray1315,m) siglength=iisignature.siglength(d,m) self.assertEqual(sigArray1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(sigArray1315.reshape(n,siglength),sigs)) self.assertEqual(sigArray15.shape,(15,siglength)) self.assertTrue(numpy.allclose(sigArray15,sigs)) backsigs=[iisignature.sigbackprop(i,j,m) for i,j in zip(sigs,paths)] backsigArray = stack(backsigs) backsigs1315=iisignature.sigbackprop(sigArray1315,pathArray1315,m) self.assertEqual(backsigs1315.shape,(1,3,1,5,6,d)) self.assertTrue(numpy.allclose(backsigs1315.reshape(n,6,2),backsigArray)) data=[numpy.random.uniform(size=(d,)) for i in range(n)] dataArray1315=stack(data).reshape((1,3,1,5,d)) joined=[iisignature.sigjoin(i,j,m) for i,j in zip(sigs,data)] joined1315=iisignature.sigjoin(sigArray1315,dataArray1315,m) self.assertEqual(joined1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(joined1315.reshape(n,-1),stack(joined))) backjoined=[iisignature.sigjoinbackprop(i,j,k,m) for i,j,k in zip(joined,sigs,data)] backjoinedArrays=[stack([i[j] for i in backjoined]) for j in range(2)] backjoined1315=iisignature.sigjoinbackprop(joined1315,sigArray1315,dataArray1315,m) self.assertEqual(backjoined1315[0].shape,sigArray1315.shape) self.assertEqual(backjoined1315[1].shape,dataArray1315.shape) self.assertTrue(numpy.allclose(backjoined1315[0].reshape(n,-1),backjoinedArrays[0])) self.assertTrue(numpy.allclose(backjoined1315[1].reshape(n,-1),backjoinedArrays[1])) scaled=[iisignature.sigscale(i,j,m) for i,j in zip(sigs,data)] scaled1315=iisignature.sigscale(sigArray1315,dataArray1315,m) self.assertEqual(scaled1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(scaled1315.reshape(n,-1),stack(scaled))) backscaled=[iisignature.sigscalebackprop(i,j,k,m) for i,j,k in zip(scaled,sigs,data)] backscaledArrays=[stack([i[j] for i in backscaled]) for j in range(2)] backscaled1315=iisignature.sigscalebackprop(scaled1315,sigArray1315,dataArray1315,m) self.assertEqual(backscaled1315[0].shape,sigArray1315.shape) self.assertEqual(backscaled1315[1].shape,dataArray1315.shape) self.assertTrue(numpy.allclose(backscaled1315[0].reshape(n,-1),backscaledArrays[0])) self.assertTrue(numpy.allclose(backscaled1315[1].reshape(n,-1),backscaledArrays[1])) s_s=(iisignature.prepare(d,m,"cosax"),iisignature.prepare(d,m,"cosahx")) for type in ("c","o","s","x","a","ch","oh","sh","ah"): s=s_s[1 if "h" in type else 0] logsigs = [iisignature.logsig(i,s,type) for i in paths] logsigArray=stack(logsigs) logsigArray1315=iisignature.logsig(pathArray1315,s,type) self.assertEqual(logsigArray1315.shape,(1,3,1,5,logsigs[0].shape[0]),type) self.assertTrue(numpy.allclose(logsigArray1315.reshape(n,-1),logsigArray),type) if type in ("s","x","sh"): backlogs = stack(iisignature.logsigbackprop(i,j,s,type) for i,j in zip(logsigs,paths)) backlogs1315 = iisignature.logsigbackprop(logsigArray1315,pathArray1315,s,type) self.assertEqual(backlogs1315.shape,backsigs1315.shape) self.assertTrue(numpy.allclose(backlogs1315.reshape(n,6,d),backlogs),type) a=iisignature.rotinv2dprepare(m,"a") rots=stack([iisignature.rotinv2d(i,a) for i in paths]) rots1315=iisignature.rotinv2d(pathArray1315,a) self.assertEqual(rots1315.shape,(1,3,1,5,rots.shape[1])) self.assertTrue(numpy.allclose(rots1315.reshape(n,-1),rots))