def add_noise_helper(self, node, noise_std): if (not (node.depth == 0 or node.depth == 1)): node.noised_position = [ norm(node.position[0], noise_std), norm(node.position[1], noise_std), norm(node.position[2], noise_std) ] else: node.noised_position = node.position[:3] for child in node.children: self.add_noise_helper(child, noise_std)
def source_wander(self, scale = BODY_LEN): """Where is that smell coming from?""" if SOURCE_WANDERS is True: self.source = [s+norm(scale=scale) for s in self.source] if any((self.source[0]<0, self.source[0]>BOX_SIZE[0], self.source[0]<0, self.source[0]>BOX_SIZE[0])): self.source_wander(scale)
def test_multiple_update(self): measurement_count = 60 initial_truth = np.array([24, 45]) error_variance = 5.0 error_covariance = np.multiply(error_variance, np.identity(2)) measurements = norm(mean=initial_truth, cov=error_covariance, size=measurement_count) # pass in first point: without previous state continuity priorEvent = None events = [] results = np.empty_like(measurements) resultIndex = 0 for location in measurements: currentEvent = createEvent(location, prior=priorEvent) result = lambda_handler(currentEvent, None) results[resultIndex] = [result['latitude'], result['longitude']] resultIndex += 1 # if 'prior' in currentEvent: # print("....[{:>8.6}, {:>8.6}] => [{:>8.6}, {:>8.6}]( {:>6.6})".format( # location[0], location[1], # currentEvent['prior']['latitude'], currentEvent['prior']['longitude'], currentEvent['prior']['variance'])) # print("........................... [{:>8.6}, {:>8.6}]( {:>6.6})".format( # result['latitude'], result['longitude'], result['variance'])) priorEvent = result # print( results ) self.assertAlmostEqual(result['latitude'], initial_truth[0], delta=5) self.assertAlmostEqual(result['longitude'], initial_truth[1], delta=5) self.assertAlmostEqual(result['variance'], 1, delta=2)
def add_noise_vary_helper(self, node, noise_std): if (not (node.depth == 0 or node.depth == 1)): if ("Hand" in node.name or "Foot" in node.name): node.noised_position = [ norm(node.position[0], noise_std), norm(node.position[1], noise_std), norm(node.position[2], noise_std) ] else: node.noised_position = [ norm(node.position[0], 0.1), norm(node.position[1], 0.1), norm(node.position[2], 0.1) ] else: node.noised_position = node.position[:3] for child in node.children: self.add_noise_vary_helper(child, noise_std)
from numpy.random import normal as norm import matplotlib.pyplot as plt from general_class_balancer import * # Sample script showing how this balances on simulated, random data. show_balanced = True N = 12000 # Number of datapoints confounds = np.random.rand(2, N) # Unfortunately, numpy arrays don't support mixed types, so in order to mix # floats (i.e., continuous covariates) and strings (i.e., discrete, or labeled, # covariates), it is necessary to cast them all as objects. Slows things down, # but if covariates are all of one type, they can be cast as one or the other. c1 = np.array([norm(1, 2, N / 3), norm(0.5, 2, N / 3), norm(2, 1, N / 3)], dtype=object) c2 = np.array([norm(3, 2, N / 3), norm(0.5, 2, N / 3), norm(3.1, 0.25, N / 3)], dtype=object) c3 = np.array([norm(2, 1, N / 3), norm(0.5, 2, N / 3), norm(2.9, 1, N / 3)], dtype=object) confounds = np.concatenate((c1, c2, c3), axis=1) c4 = np.array([[np.random.choice(["a", "b", "c"]) for x in range(N)]], dtype=object) confounds = np.concatenate((confounds, c4), axis=0)
for n in range(numtrials): r = N / (4 * numharm) + uniform(0.0, 1.0, 1)[0] # average freq over "observation" z = uniform(-100, 100, 1)[0] # average fourier f-dot w = uniform(-600, 600, 1)[0] # fourier freq double deriv # w = 0.0 # fourier freq double deriv data = np.zeros_like(us) for ii in range(numharm): rh = r * (ii + 1) zh = z * (ii + 1) wh = w * (ii + 1) r0 = rh - 0.5 * zh + wh / 12.0 # Make symmetric for all z and w z0 = zh - 0.5 * wh phss = 2.0 * np.pi * (us * (us * (us * wh / 6.0 + z0 / 2.0) + r0)) data += np.cos(phss) data += noiseamp * norm(N) ft = presto.rfft(data) offset = uniform(-1.0, 1.0, 3) * np.array([0.5, 2.0, 20.0]) / numharm a = time.clock() if (numharm > 1): [maxpow, rmax, zmax, rds] = presto.maximize_rz_harmonics(ft, r + offset[0], z + offset[1], numharm, norm=1.0) else: [maxpow, rmax, zmax, rd] = presto.maximize_rz(ft, r + offset[0], z + offset[1],
import ppgplot from Pgplot import pgpalette from numpy.random import standard_normal as norm import time N = 2**14 r = N/4.0 # average freq over "observation" #r = N/4.0 + 0.5 # average freq over "observation" rint = num.floor(r) dr = 1.0/32.0 dz = 0.18 np = 512 # number of pixels across for f-fdot image z = 10.0 # average fourier f-dot w = -40.0 # fourier freq double deriv noise = 0.0 noise = 1.0*norm(N) us = num.arange(N, dtype=num.float64) / N # normalized time coordinate r0 = r - 0.5 * z + w / 12.0 # Make symmetric for all z and w z0 = z - 0.5 * w phss = 2.0 * num.pi * (us * (us * (us * w/6.0 + z0/2.0) + r0)) ft = presto.rfft(num.cos(phss)+noise) ffdot = presto.ffdot_plane(ft, rint-np/2*dr, dr, np, 0.0-np/2*dz, dz, np) pffdot = presto.spectralpower(ffdot.flat) theo_max_pow = N**2.0/4.0 frp = max(pffdot) / theo_max_pow # Fraction of recovered power print "Fraction of recovered signal power = %f" % frp a = time.clock() [maxpow, rmax, zmax, rd] = presto.maximize_rz(ft, r+norm(1)[0]/5.0, z+norm(1)[0], norm=1.0) print "Time for rz:", time.clock()-a
for n in range(numtrials): r = N/(4*numharm) + uniform(0.0, 1.0, 1)[0] # average freq over "observation" z = uniform(-100, 100, 1)[0] # average fourier f-dot w = uniform(-600, 600, 1)[0] # fourier freq double deriv w = 0.0 # fourier freq double deriv data = np.zeros_like(us) for ii in range(numharm): rh = r * (ii + 1) zh = z * (ii + 1) wh = w * (ii + 1) r0 = rh - 0.5 * zh + wh / 12.0 # Make symmetric for all z and w z0 = zh - 0.5 * wh phss = 2.0 * np.pi * (us * (us * (us * wh/6.0 + z0/2.0) + r0)) data += np.cos(phss) data += noiseamp * norm(N) ft = presto.rfft(data) offset = uniform(-1.0, 1.0, 3) * np.array([0.5, 2.0, 20.0]) / (0.5 * numharm) a = time.clock() if (numharm > 1): [maxpow, rmax, zmax, rds] = presto.maximize_rz_harmonics(ft, r+offset[0], z+offset[1], numharm, norm=1.0) else: [maxpow, rmax, zmax, rd] = presto.maximize_rz(ft, r+offset[0], z+offset[1], norm=1.0) rztime += time.clock() - a rzerrs[n] = (maxpow/numharm - theo_max_pow) / theo_max_pow, rmax - r, zmax - z
def gaussianSample(lower, upper): dis = upper - lower ans = int(norm((lower + upper) / 2, dis / 6)) if ans >= upper: ans = upper - 1 if ans < lower: ans = lower return ans