def calc_time_series(self): data = zeros(self.Nbins, 'f') data.savespace() # Place the non-pulsed photons points = randint(0, self.Nbins, self.backgdphot) for point in points: data[point] = data[point] + 1.0 # Place the pulsed photons pulses = randint(0, self.freq, self.pulsedphot) if pulsetype=='Sine': x = arange(10001, typecode='d') * TWOPI/10000 coscdf = (x + sin(x))/TWOPI uvar = random(self.pulsedphot) phases = take((x + TWOPI * random()) % TWOPI, searchsorted(coscdf, uvar)) / TWOPI # plotxy(coscdf, x) # closeplot() # hist = histogram(phases, 100, [0.0, TWOPI]) # plotbinned(hist[:,1], hist[:,0]) # closeplot() elif pulsetype=='Gaussian': phases = normal(0.0, width, self.pulsedphot) % 1.0 + random() points = ((pulses + phases) / self.freq * self.Nbins).astype('i') for point in points: data[point] = data[point] + 1.0 # tmp = average(data) # print 'Average Data = ', tmp # print ' sqrt(avg) = ', sqrt(tmp) # print ' StdDev Data = ', standardDeviation(data) return data
def calc_time_series(self): data = zeros(self.Nbins, 'f') data.savespace() # Place the non-pulsed photons points = randint(0, self.Nbins, self.backgdphot) for point in points: data[point] = data[point] + 1.0 # Place the pulsed photons pulses = randint(0, self.freq, self.pulsedphot) if pulsetype == 'Sine': x = arange(10001, typecode='d') * TWOPI / 10000 coscdf = (x + sin(x)) / TWOPI uvar = random(self.pulsedphot) phases = take((x + TWOPI * random()) % TWOPI, searchsorted(coscdf, uvar)) / TWOPI # plotxy(coscdf, x) # closeplot() # hist = histogram(phases, 100, [0.0, TWOPI]) # plotbinned(hist[:,1], hist[:,0]) # closeplot() elif pulsetype == 'Gaussian': phases = normal(0.0, width, self.pulsedphot) % 1.0 + random() points = ((pulses + phases) / self.freq * self.Nbins).astype('i') for point in points: data[point] = data[point] + 1.0 # tmp = average(data) # print 'Average Data = ', tmp # print ' sqrt(avg) = ', sqrt(tmp) # print ' StdDev Data = ', standardDeviation(data) return data
def testLinearLeastSquares(self): """ From bug #503733. """ # XXX not positive on this yet import LinearAlgebra from RandomArray import seed, random seed(7,19) (n, m) = (180, 35) yp = random((n,m)) y = random(n) x, residuals, rank, sv = LinearAlgebra.linear_least_squares(yp, y) # shouldn't segfault. assert rank == m
def test_randomSequence(self): """randomSequence: 99% of new frequencies should be within 3*SD""" r_num, c_num = 100,20 num_elements = r_num*c_num alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" r = random([r_num,c_num]) p = Profile(r,alpha[:c_num]) p.normalizePositions() d = p.Data n = 1000 #Test only works on normalized profile, b/c of 1-d below means = n*d three_stds = sqrt(d*(1-d)*n)*3 a = Alignment([p.randomSequence() for x in range(n)]) def absoluteProfile(alignment,char_order): f = a.columnFrequencies() res = zeros([len(f),len(char_order)]) for row, freq in enumerate(f): for i in freq: col = char_order.index(i) res[row, col] = freq[i] return res ap = absoluteProfile(a,p.CharOrder) failure = abs(ap-means) > three_stds assert sum(sum(failure))/num_elements <= 0.01
def test_big(self): from RandomArray import seed, random seed(13, 17) X = random((1024, 512)) Y = fwt2(X) X1 = ifwt2(Y) assert Numeric.allclose(X - X1, 0)
def test_ifwt2c(self): from RandomArray import seed, random seed(13, 17) X = random((8, 4)) Y = ifwt2(X) Y = ifwt2(Y) Y = fwt2(Y) X1 = fwt2(Y) assert Numeric.allclose(X - X1, 0)
def AutoCorrelationFunction(series): n = 2*len(series) FFTSeries = FFT.fft(series,n,0) FFTSeries = FFTSeries*N.conjugate(FFTSeries) FFTSeries = FFT.inverse_fft(FFTSeries,len(FFTSeries),0) return FFTSeries[:len(series)]/(len(series)-N.arange(len(series))) from MMTK.Random import gaussian from Scientific.Statistics import mean from Scientific.IO.ArrayIO import readArray from Gnuplot import plot from RandomArray import random dt = 1. t = dt*N.arange(500) if 0: data = N.sin(t) + N.cos(3.*t) + 0.1*(random(len(t))-0.5) data = data + 0.1j*(random(len(t))-0.5) if 0: data = [0.] for i in range(500+len(t)-1): data.append(mean(data[-500:]) + gaussian(0., 0.1)) data = N.exp(1j*N.array(data[500:])) if 0: #data = readArray('~/scientific/Test/data') string = open('/users1/hinsen/scientific/Test/data').read()[4:] data = N.array(eval(string)) data = data[:,0] model = AutoRegressiveModel(20, data, dt) print model.coeff print model.poles()
# Scroll scene center right to follow data if len(ch.x) > 0 and ch.x[-1] > 10: # scroll center of the scene with x new_x_center = (ch.x[0] + ch.x[-1]) / 2. self.scene.center = (new_x_center, 0, 0) self.updateTicks(ch.x, ch.y) self.grid.x = new_x_center self.ticklabels.x = new_x_center # shift curve and append ch.pos[:-1] = ch.pos[1:] ch.pos[-1] = point else: ch.append(pos=point) st = Strip(2) ch1 = st.channels[0] ch2 = st.channels[1] start = time.clock() i = -10 while True: rate(50) if i < 20: st.plot(ch1, (i, 10 * sin(i), 0)) st.plot(ch2, (i, 10 * cos(i) + random(), 0)) #rate(50)#set maximum frame rate i += 0.1 #print "frame rate: ", 1/((time.clock()-start)/210.)
def AutoCorrelationFunction(series): n = 2 * len(series) FFTSeries = FFT.fft(series, n, 0) FFTSeries = FFTSeries * N.conjugate(FFTSeries) FFTSeries = FFT.inverse_fft(FFTSeries, len(FFTSeries), 0) return FFTSeries[:len(series)] / (len(series) - N.arange(len(series))) from MMTK.Random import gaussian from Scientific.Statistics import mean from Scientific.IO.ArrayIO import readArray from Gnuplot import plot from RandomArray import random dt = 1. t = dt * N.arange(500) if 1: data = N.sin(t) + N.cos(3. * t) + 0.1 * (random(len(t)) - 0.5) data = data + 0.1j * (random(len(t)) - 0.5) if 0: data = [0.] for i in range(500 + len(t) - 1): data.append(mean(data[-500:]) + gaussian(0., 0.1)) data = N.exp(1j * N.array(data[500:])) if 0: #data = readArray('~/scientific/Test/data') string = open('/users1/hinsen/scientific/Test/data').read()[4:] data = N.array(eval(string)) data = data[:, 0] model = AutoRegressiveModel(20, data, dt) print model.coeff print model.poles()
from Numeric import * from RandomArray import random tolerance = .001 from FFT import real_fft,inverse_real_fft for size in range (1,25): for i in range(3): a = random(size) b = real_fft(a) c = inverse_real_fft(b,size) assert not sometrue(greater(abs(a-c),tolerance)) #example from previously fixed real_fft bug (OK) x = cos(arange(30.0)/30.0*2*pi) y = real_fft(x) z = inverse_real_fft(y,30) assert not sometrue(greater(abs(x-z),tolerance))
def test(): print fft( (0,1)*4 ) print inverse_fft( fft((0,1)*4) ) print fft( (0,1)*4, n=16 ) print fft( (0,1)*4, n=4 ) print fft2d( [(0,1),(1,0)] ) print inverse_fft2d (fft2d( [(0, 1), (1, 0)] ) ) print real_fft2d([(0,1),(1,0)] ) print real_fft2d([(1,1),(1,1)] ) import sys oosq2 = 1.0/Numeric.sqrt(2.0) toler = 1.e-10 p = Numeric.array(((1, 1, 1, 1, 1, 1, 1, 1), (1, oosq2, 0, -oosq2, -1, -oosq2, 0, oosq2), (1, 0, -1, 0, 1, 0, -1, 0), (1, -oosq2, 0, oosq2, -1, oosq2, 0, -oosq2), (1, -1, 1, -1, 1, -1, 1, -1), (1, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0))) q = Numeric.array(((8,0,0,0,0,0,0,0), (0,4,0,0,0,0,0,4), (0,0,4,0,0,0,4,0), (0,0,0,4,0,4,0,0), (0,0,0,0,8,0,0,0), (1,1,1,1,1,1,1,1), (0,0,0,0,0,0,0,0))) def cndns(m): return Numeric.maximum.reduce(abs(m).flat) try: junk = hermite_fft new = 1 except NameError: new = 0 # Tests for correctness. # Somewhat limited since # p (and thus q also) is real and hermite, and the dimension we're # testing is a power of 2. If someone can cook up more general data # in their head or with another program/library, splice it in! print "\nCorrectness testing dimension -1." sys.stdout.write("fft: ") sys.stdout.flush() P = fft(p) if cndns(P-q) / cndns(q) > toler: print "inaccurate" else: print "OK" sys.stdout.write("real_fft: ") sys.stdout.flush() RP = real_fft(p) npt = p.shape[-1] rpt = npt/2 + 1 qr = q[:,:rpt] if cndns(RP-qr) / cndns(qr) > toler: print "inaccurate" else: print "OK" sys.stdout.write("inverse_real_fft: ") sys.stdout.flush() if cndns(inverse_real_fft(q, npt)-p) / cndns(p) > toler: print "inaccurate" else: print "OK" # now just test consistency for dim in range(len(p.shape)): print "\nConsistency testing dimension %d, length %d." % \ (dim, p.shape[dim]) sys.stdout.write("fft/inverse_fft: ") sys.stdout.flush() P = fft(p, None, dim) Q = inverse_fft(P, None, dim) if cndns(Q-p) / cndns(p) > toler: print "inconsistent" else: print "OK" sys.stdout.write("fft/real_fft: ") sys.stdout.flush() RP = real_fft(p, None, dim) npt = p.shape[dim] rpt = npt/2 + 1 P = Numeric.take(P, range(rpt), dim) if cndns(RP-P) / cndns(RP) > toler: print "inconsistent" else: print "OK" sys.stdout.write("inverse_fft/inverse_hermite_fft: ") sys.stdout.flush() hp = inverse_hermite_fft(q, npt, dim) Q = inverse_fft(q, None, dim) Q = Numeric.take(Q, range(rpt), dim) if cndns(hp-Q) / cndns(hp) > toler: print "inconsistent" else: print "OK" sys.stdout.write("real_fft/inverse_real_fft: ") sys.stdout.flush() if cndns(inverse_real_fft(RP, npt, dim)-p) / cndns(p) > toler: print "inconsistent" else: print "OK" sys.stdout.write("inverse_hermite_fft/hermite_fft: ") sys.stdout.flush() if cndns(hermite_fft(hp, npt, dim)-q) / cndns(q) > toler: print "inconsistent" else: print "OK" print "" # test multi-dim stuff print "Multi-dimensional tests:" tee = Numeric.array(((2.0, 0, 2, 0), (0, 2, 0, 2), (2, 0, 2, 0), (0, 2, 0, 2))) eff = Numeric.array(((16.0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 16, 0), (0, 0, 0, 0))) sys.stdout.write("fftnd: ") ftest = fftnd(tee) if cndns(ftest - eff) / cndns(eff) > toler: "inaccurate" else: print "OK" sys.stdout.write("inverse_fftnd: ") if cndns(inverse_fftnd(ftest) - tee) / cndns(tee) > toler: print "inconsistent with fftnd" else: print "OK" sys.stdout.write("real_fftnd: ") fred = real_fftnd(p) npts = p.shape[-1] rpts = npts/2 + 1 actual = fftnd(p) ract = actual[..., :rpts] if cndns(fred-ract) / cndns(ract) > toler: print "inconsistent with fftnd" else: print "OK" sys.stdout.write("inverse_real_fftnd: ") ethel = inverse_real_fftnd(fred) if cndns(p-ethel) / cndns(p) > toler: print "inconsistent with real_fftnd" print fred print ethel print p else: print "OK" sys.stdout.write("\nfft2d shape test: ") success = 1 axes = (0,1) shape = (7,4) barney = Numeric.zeros(shape,'d') betty = fft2d(barney) success = success and betty.shape == barney.shape betty = fft2d(barney, None, axes) success = success and betty.shape == barney.shape betty = fft2d(barney, shape) success = success and betty.shape == barney.shape betty = fft2d(barney, shape, axes) success = success and betty.shape == barney.shape betty = real_fft2d(barney) wilma = inverse_real_fft2d(betty) success = success and wilma.shape == barney.shape wilma = inverse_real_fft2d(betty, shape) success = success and wilma.shape == barney.shape wilma = inverse_real_fft2d(betty, None, axes) success = success and wilma.shape == barney.shape wilma = inverse_real_fft2d(betty, shape, axes) success = success and wilma.shape == barney.shape if success: print "OK" else: print "fail" sys.stdout.write("\nCodelet order test: ") sys.stdout.flush() from RandomArray import random success = 1 for size in range (1,25): for i in range(3): a = random(size) b = real_fft(a) c = inverse_real_fft(b,size) if cndns(c-a) / cndns(a) > toler: print "real transforms failed for size %d" % size success = 0 a = a + random(size) * 1j b = fft(a) c = inverse_fft(b,size) if cndns(c-a) / cndns(a) > toler: print "complex transforms failed for size %d" % size success = 0 if success: print "OK"
def setRandomTransitionProba(self): """set transition probability matrix to some random values""" self.A = random(self.A.shape) self.A /= sum(self.A) # normalization
def setRandomInitialProba(self): """set initial state probability matrix to some random values""" self.pi = random(self.pi.shape) self.pi /= sum(self.pi) # normalization
def setRandomObservationProba(self): """set observation probability matrix to some random values""" self.B = random(self.B.shape) self.B /= sum(self.B) # normalization