def classequal( cls1, cls2 ) : tc = unittest.TestCase() atr1 = vars( cls1 ) ld1 = list( atr1.keys() ) ld1.sort() atr2 = vars( cls2 ) ld2 = list( atr2.keys() ) ld2.sort() if len( ld1 ) == len( ld2 ) : assertAE( ld1, ld2 ) else : for key in ld1 : print( key, "\t:", atr1[key] ) print( ) for key in ld2 : print( key, "\t:", atr2[key] ) print() for key1,key2 in zip( ld1, ld2 ) : val1 = atr1[key1] val2 = atr2[key2] if isinstance( val1, (list, numpy.ndarray) ) : assertAE( val1, val2 ) elif isinstance( val1, ( int, float, str ) ) : tc.assertTrue( val1 == val2 )
def test_3d(): """ 3 dimensions. """ assertAE( rdp( np.array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]).reshape(5, 3)), np.array([0, 0, 0, 4, 4, 4]).reshape(2, 3))
def test9(self): print("==== test ========= fixed parameters ===========") fm = BasicSplinesModel(nrknots=8, min=0, max=10, fixed={ 0: 0.0, -1: 0.0 }) print(fm.parlist) assertAE(fm.parlist, numpy.arange(8, dtype=int) + 1) print(fm.fixed) assertAE(list(fm.fixed.keys()), [0, 9]) fm = BasicSplinesModel(nrknots=8, min=0, max=10, fixed={ 0: 0, 1: 0, -2: 0, -1: 0 }) print(fm.parlist) assertAE(fm.parlist, numpy.arange(6, dtype=int) + 2) print(fm.fixed) assertAE(list(fm.fixed.keys()), [0, 1, 8, 9])
def test3(self): print("==== test 3 BasicSplinesModel ====================") x = numpy.linspace(0, 100, 1001, dtype=float) kn = [ 0.000, 1.470, 16.049, 28.465, 36.358, 36.374, 36.829, 59.589, 59.699, 70.193, 75.844, 88.918, 88.919, 100.000 ] sm = BasicSplinesModel(knots=kn) par = numpy.ones(sm.npars, dtype=float) y = sm.result(x, par) dy = sm.derivative(x, par) pt = sm.partial(x, par) fm = BasicSplinesModel(knots=kn, fixed={0: 0.0, -1: 0.0}) far = numpy.ones(fm.npars, dtype=float) print(fm.npbase, fm.npars, fm.npmax, len(far)) print(fm.parlist) assertAE(fm.parlist, numpy.arange(14, dtype=int) + 1) print(fm.fixed) assertAE(list(fm.fixed.keys()), [0, 15]) fy = fm.result(x, far) fdy = fm.derivative(x, far) fpt = fm.partial(x, far) cc = ['k-', 'b-', 'r-', 'g-', 'c-', 'm-'] if self.doplot: for k in kn: plt.plot([k, k], [0, 1], 'k:') plt.plot(x, y, cc[0]) plt.plot(x, dy, cc[3]) for i in range(sm.npars): plt.plot(x, pt[:, i]) plt.plot(x, fy + 2, cc[0]) plt.plot(x, fdy + 2, cc[3]) for i in range(fm.npars): plt.plot(x, fpt[:, i] + 2) plt.ylim(-0.3, 3.3) plt.show()
def classequal(cls1, cls2): tc = unittest.TestCase() atr1 = vars(cls1) ld1 = list(atr1.keys()) ld1.sort() atr2 = vars(cls2) ld2 = list(atr2.keys()) ld2.sort() assertAE(ld1, ld2) for key1, key2 in zip(ld1, ld2): val1 = atr1[key1] val2 = atr2[key2] # print( "Cls1 ", key1, val1 ) # print( "Cls2 ", key2, val2 ) if isinstance(val1, (list, numpy.ndarray)): assertAE(val1, val2) elif isinstance(val1, (int, float, str)): tc.assertTrue(val1 == val2)
def testImplicit(self): print("==== BracketModel Test =======================") m1 = GaussModel() m1 += PolynomialModel(0) # Gauss on a constant background m2 = BracketModel(m1) m3 = SineModel() m3 *= m2 # sine * ( gauss + const ) print("Explicit Use") print(m3) g1 = GaussModel() g1 += PolynomialModel(0) # m1 is a chain of models g3 = SineModel() g3 *= g1 # sine * ( gauss + const ) print("Implicit Use") print(g3) # exactly the same p = [8.0, 1.0, 0.0, 1.0, 0.0, 0.2, 1.0] x = numpy.asarray( [-1.0, -0.8, -0.6, -0.4, -0.2, 0.0, 0.2, 0.4, 0.6, 0.8, 1.0]) assertAE(g3.result(x, p), m3.result(x, p)) assertAE(g3.partial(x, p), m3.partial(x, p)) assertAE(g3.derivative(x, p), m3.derivative(x, p)) stdModeltest(g3, p, plot=self.doplot)
def stdstarttest(self, mystarteng, np=4, nwalker=10): problem = self.initProblem(np=np) errdis = DistanceCostFunction() n2 = np * np pars = numpy.arange(n2, dtype=int) fi = numpy.arange(n2, dtype=int) sl = WalkerList(problem, nwalker, pars, fi) steng = mystarteng(sl, errdis, verbose=5) for wlkr in steng.walkers: logL = errdis.logLikelihood(problem, wlkr.allpars) wid = wlkr.id print("Walker :", wid, wlkr.allpars, fmt(logL)) steng.execute(wid, -math.inf) nwlk = steng.walkers[wid] print(" ", nwlk.allpars, fmt(nwlk.logL)) psort = numpy.sort(nwlk.allpars) assertAE(psort, pars)
def test2(self): print("******USER MODEL 2***********************") m1 = UserModel(4, etalonRes, userPartial=etalonPart, userDeriv=etalonDer) m1.parameters = [1, 1, 1, 1] p = numpy.asarray([2.3, 1.1, 3.0, 5.0], dtype=float) stdModeltest(m1, p, plot=self.doplot) N = 201 x = numpy.arange(N, dtype=float) / 25 - 2 m2 = EtalonModel() assertAE(m1.result(x, p), m2.result(x, p)) assertAE(m1.partial(x, p), m2.partial(x, p)) assertAE(m1.derivative(x, p), m2.derivative(x, p))
def testConstantModel( self ): print( "==== BracketModel Test 1 =======================" ) x = numpy.asarray( [ -1.0, -0.8, -0.6, -0.4, -0.2, 0.0, 0.2, 0.4, 0.6, 0.8, 1.0] ) c = ConstantModel() m = BracketModel( c ) self.assertTrue( m.npbase == 0 ) p = [] print( m ) print( p ) self.assertTrue( m.testPartial( x[0], p ) == 0 ) part = m.partial( x, p ) print( part ) nump = m.numPartial( x, p ) print( nump ) assertAE( part, numpy.ndarray( (11,0), dtype=float ) ) assertAE( nump, numpy.ndarray( (11,0), dtype=float ) ) mc = m.copy( ) mc.parameters = p m.parameters = p assertAE( m.result( x ), mc.result( x ) ) self.assertTrue( mc.priors is None )
def test_eps0(self): """ Epsilon being to small to be simplified. """ assertAE(rdp(np.array([0, 0, 5, 1, 10, 1]).reshape(3, 2)), np.array([0, 0, 5, 1, 10, 1]).reshape(3, 2))
def test_diag(self): """ Diagonal line. """ assertAE(rdp(np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4]).reshape(5, 2)), np.array([0, 0, 4, 4]).reshape(2, 2))
def test_ver(self): """ Vertical line. """ assertAE(rdp(np.array([0, 0, 0, 1, 0, 2, 0, 3, 0, 4]).reshape(5, 2)), np.array([0, 0, 0, 4]).reshape(2, 2))
def test_hor(self): """ Horizontal line. """ assertAE(rdp(np.array([0, 0, 1, 0, 2, 0, 3, 0, 4, 0]).reshape(5, 2)), np.array([0, 0, 4, 0]).reshape(2, 2))
def test2(self): print("\n CombiModel Test 2\n") gm = GaussModel() pars = numpy.linspace(0.0, 1.0, 3, dtype=float) gm.parameters = pars cm = CombiModel(gm, 3) Tools.printclass(cm) self.assertTrue(cm.deep == 1) print(str(cm._head)) self.assertTrue(str(cm._head) == "Combi of 3 times Gauss") self.assertTrue(cm._next is None) self.assertTrue(cm._npchain == 9 and cm.npchain == 9) assertAE(cm.addindex, []) assertAE(cm.addvalue, []) assertAE(cm.mulindex, []) assertAE(cm.mulvalue, []) assertAE(cm.expandindex, numpy.arange(9, dtype=int)) assertAE(cm.select, numpy.arange(9, dtype=int)) assertAE(cm.parameters, [0.0, 0.5, 1.0] * 3) p = numpy.ones(9, dtype=float) p[1:9:3] /= 2 p[2:9:3] /= 10 stdModeltest(cm, p, plot=self.doplot)
def test_rec_iter2(): for i in range(0, 40): assertAE(rdp(nice_line, algo="iter", epsilon=i * 0.1), rdp(nice_line, algo="rec", epsilon=i * 0.1))
def test_two(self): """ Point sequence with only two elements. """ assertAE(rdp(np.array([[0, 0], [4, 4]])), np.array([[0, 0], [4, 4]]))
def test_eps1(self): """ Epsilon large enough to be simplified. """ assertAE(rdp(np.array([0, 0, 5, 1, 10, 1]).reshape(3, 2), 1), np.array([0, 0, 10, 1]).reshape(2, 2))
def test_L0(self): """ Point sequence which has the form of a L. """ assertAE(rdp(np.array([5, 0, 4, 0, 3, 0, 3, 1, 3, 2]).reshape(5, 2)), np.array([5, 0, 3, 0, 3, 2]).reshape(3, 2))
def stdenginetest(self, myengine, nwalker=4, np=10, plot=False, random=False): problem = self.initProblem(np=np, random=random) errdis = DistanceCostFunction() # print( problem.xdata ) n2 = np * np pars = numpy.arange(n2, dtype=int) fi = numpy.arange(n2, dtype=int) sl = WalkerList(problem, nwalker, pars, fi) if not random: n1 = np - 1 m = n1 * n1 s = np * n1 s += math.sqrt(m + 1) * (np - 1) s += math.sqrt(m + m) logL = numpy.sum(problem.result(pars)) print(s, logL, abs(s - logL)) self.assertTrue(abs(s - logL) < 1.0e-10) steng = StartOrderEngine(sl, errdis, verbose=5) myeng = myengine(sl, errdis, verbose=5) for wlkr in steng.walkers: print("=========", wlkr) pars = wlkr.allpars # print( "Before ", wlkr.allpars ) wid = wlkr.id myeng.execute(wid, -math.inf) neww = steng.walkers[wid] # print( "After ", neww.allpars ) newp = neww.allpars psort = numpy.sort(neww.allpars) assertAE(psort, pars) if plot: xy0 = problem.xdata[pars] plt.plot(xy0[:, 0], xy0[:, 1], 'k-') plt.plot([xy0[-1, 0], xy0[0, 0]], [xy0[-1, 1], xy0[0, 1]], 'b-') xy1 = problem.xdata[newp] plt.plot(xy1[:, 0], xy1[:, 1], 'r-.') plt.plot([xy1[-1, 0], xy1[0, 0]], [xy1[-1, 1], xy1[0, 1]], 'g-.') for kp in range(n2): plt.text(xy0[kp, 0], xy0[kp, 1], "%d" % kp, { 'color': 'black', 'ha': 'right' }) plt.text(xy1[kp, 0], xy1[kp, 1], "%d" % kp, { 'color': 'red', 'ha': 'left' }) plt.show() myeng.verbose = 0 wid = wlkr.id for k in range(1000): myeng.execute(wid, -math.inf) psort = numpy.sort(steng.walkers[wid].allpars) assertAE(psort, pars)
def test_rec_iter3d(line3d): for e in range(0, 10): assertAE(rdp(line3d, e * .1, algo="iter"), rdp(line3d, e * .1, algo="rec"))