def update_db_pcoa(n_procs=4): def medoids(abd_tables, metadata): for userchunk in partition_by(zip(abd_tables, metadata), getpid): otu_tables = zip(*userchunk)[0] median = dict(process_pcoa.abundance_medoids(otu_tables)) yield median dictify = lambda pids, points_list: dict(zip(pids, points_list)) pids = dedupe(map(getpid, metadata)) _, otu_tables, instances = zip(*metadata) bioms = list(process_pcoa.load_bioms(otu_tables)) sampledist = process_pcoa.dist_array(bioms, len(bioms), n_procs=n_procs) samplepcoa = dictify(instances, process_pcoa.pcoa_coords(sampledist)) meds = list(medoids(bioms, instances)) userdist = process_pcoa.dist_array(meds, len(meds), n_procs=n_procs) userpcoa = dictify(pids, process_pcoa.pcoa_coords(userdist)) sampledist, mask = process_pcoa.rm_outliers(sampledist) instances = take(instances, mask) userdist, mask = process_pcoa.rm_outliers(userdist) pids = take(pids, mask) f_userpcoa = dictify(pids, process_pcoa.pcoa_coords(userdist)) f_samplepcoa = dictify(instances, process_pcoa.pcoa_coords(sampledist)) global_user = models.User(models.GLOBAL_PID, db=db, load=True) global_user.state[models.User.PCOA_SAMPLE_KEY] = samplepcoa.values() global_user.state[models.User.PCOA_USER_KEY] = userpcoa.values() global_user.state[ models.User.filtered.PCOA_SAMPLE_KEY] = f_samplepcoa.values() global_user.state[models.User.filtered.PCOA_USER_KEY] = f_userpcoa.values() def _users(): for userchunk in partition_by(metadata, getpid): first, rest = peek(userchunk) rest = list(rest) pid = getpid(first) user = models.User(pid, db=db, load=True) if pid in f_userpcoa: user.state[ models.User.filtered.PCOA_USER_KEY] = f_userpcoa[pid] else: user.state[models.User.PCOA_USER_KEY] = userpcoa[pid] instances = [k[-1] for k in rest] if not all(k in f_samplepcoa for k in instances): user.state[models.User.PCOA_SAMPLE_KEY] = [ samplepcoa[k] for k in instances ] else: user.state[models.User.filtered.PCOA_SAMPLE_KEY] = [ f_samplepcoa[k] for k in instances ] yield user models.save_all(_users()) global_user.state[models.User.MTIME_KEY] = NOW global_user.save()
def test_sol_earth_equal(rover, sol, limit): ed = nasa.sol_to_earth_date(rover, sol) by_sol = take(limit, nasa.get_mars_photos(rover, sol=sol)) by_earth_date = take(limit, nasa.get_mars_photos(rover, earth_date=ed)) for sol_photo, ed_photo in zip(by_sol, by_earth_date): assert sol_photo == ed_photo # check metadata # following assertion should never fail as we ensured img_src are # the same by comparing metadata (if NASA's static server doesn't # give different images for the same urls, of course), # so this is only to add some image comparison to the task. assert not diff_img_by_urls(sol_photo['img_src'], ed_photo['img_src'])
def player_take(field, symbol): while True: position = input('Which position do you choose for your take? ') if input_check(field, position) == 'ok': break print(input_check(field, position)) return util.take(field, int(position)-1, symbol)
def main(biom_fnames, do_filter=False, n_procs=4, hsum_to="g__"): all_abds = load_bioms(biom_fnames, hsum_to=hsum_to) dist_arr = dist_array(all_abds, len(biom_fnames), n_procs=n_procs) if do_filter: dist_arr, mask = rm_outliers(dist_arr) biom_fnames = take(biom_fnames, mask) pcoa_arr = pcoa_coords(dist_arr) return zip(biom_fnames, pcoa_arr)
def find_smooth(n, fb, lim=20): try: xis, yis, evecs = zip(*take(len(fb) + 1, smooth_iter(n, fb, lim))) emat = [[evec.get(p, 0) for p in fb] for evec in evecs] bmat = [[a % 2 for a in row] for row in emat] return (xis, yis, emat, bmat) except: return None
def pc_take(field, symbol): if field.count('-') == 0: raise ValueError('Field has no available spots for new take.') while True: position = randrange(len(field)) if field[position] != '-': continue break return util.take(field, position, symbol)
def run(n, x, *goals): """ Run a logic program. Obtain n solutions to satisfy goals. n - number of desired solutions. See ``take`` 0 for all None for a lazy sequence x - Output variable goals - a sequence of goals. All must be true >>> from logpy import run, var, eq >>> run(1, x, eq(x, 1)) (1,) """ return take(n, unique(walkstar(x, s) for s in bindstar(({},), *goals)))
def run(n, x, *goals, **kwargs): """ Run a logic program. Obtain n solutions to satisfy goals. n - number of desired solutions. See ``take`` 0 for all None for a lazy sequence x - Output variable goals - a sequence of goals. All must be true >>> from logpy import run, var, eq >>> run(1, x, eq(x, 1)) (1,) """ return take(n, unique(reify(x, s) for s in goaleval(lallearly(*goals))({})))
def run(n, x, *goals, **kwargs): """ Run a logic program. Obtain n solutions to satisfy goals. n - number of desired solutions. See ``take`` 0 for all None for a lazy sequence x - Output variable goals - a sequence of goals. All must be true >>> from logpy import run, var, eq >>> x = var() >>> run(1, x, eq(x, 1)) (1,) """ return take(n, unique(reify(x, s) for s in goaleval(lallearly(*goals))({})))
def find_smooth(n, b, I): m = ceil(sqrt(n)) # Sieve from sieving interval V = [(x + m)**2 - n for x in range(I)] # In tandem, find b primes p for which n has roots modulo p p_root_pairs = list(take(b, iter_roots(n))) fb, roots = zip(*p_root_pairs) # Sieve! for (p, roots) in p_root_pairs: for r in roots: start = (r - m) % p for i in range(start, len(V), p): while V[i] % p == 0: V[i] //= p # Construct, collect and return the x's and y's (which are smooth) xs = [i + m for (i, v) in enumerate(V) if abs(v) == 1] ys = [x**2 - n for x in xs] return (xs, ys, fb)
def analyze_lambda_expr(self, Γ, args, e): arg_ids = tuple(U.take(len(args), U.fresh_ids)) arg_types = [T.EVar(name) for name in arg_ids] Γ1 = Γ.copy() for a, t in zip(map(U.ident2str, args), arg_types): Γ1.annotate(a, t, fixed=True) Γ2, t < -self.analyze([Γ1], e) #print('Γ2 =', Γ2) #print('t =', t, '=>', t.under(Γ2)) #print('args =', ', '.join(map(str, arg_types))) fn = T.Fun(T.Tuple(arg_types), t).under(Γ2) #print('Γ =', Γ) for name in Γ.Γ: t_Γ = Γ.typeof(name) t_Γ2 = t_Γ.under(Γ2) Γ.unify(t_Γ, t_Γ2) Γ.fix(t_Γ2.names() & (Γ2.fixed - Γ1.fixed)) #print('new Γ =', Γ) #print('returning', fn) #print() return [(Γ, fn)]
import psyco import util NUM = 100000 def triangles(): n = 0 while True: yield n * (n + 1) / 2 n += 1 triangles = set(util.take(NUM, triangles())) def pentagonals(): n = 0 while True: yield n * (3 * n - 1) / 2 n += 1 pentagonals = set(util.take(NUM, pentagonals())) def hexagonals(): n = 0 while True: yield n * (2 * n - 1) n += 1 hexagonals = set(util.take(NUM, hexagonals())) print sorted(triangles.intersection(pentagonals).intersection(hexagonals))
import sys import matplotlib.pyplot as plt import util if __name__ == '__main__': plt.ylabel("average time in milliseconds") for filename in sys.argv[1:]: info = util.take(filename) plt.plot(info.xAxis, info.yAxis, label=info.serverType) plt.xlabel(info.testingType) title = "number of requests = " + str(info.requestNumber) + '\n' if info.testingType != util.TestingType.ARRAY_SIZE: title += "size of array = " + str(info.arraySize) + "; " if info.testingType != util.TestingType.CLIENTS_NUMBER: title += "number of clients = " + str(info.clientsNumber) + "; " if info.testingType != util.TestingType.SENDING_DELTA: title += "delta of sending = " + str(info.sendingDelta) + "; " plt.title(title) plt.legend() plt.show()
# # Uses the Legendre symbol (i.e. Euler's criterion). # Note: every integer is a quadratic residue modulo 2. # # @param n An integer # @param p A prime (not necessarily odd, i.e. can be 2) # @return Whether integer n is a quadratic residue modulo odd prime p def is_quadratic_residue(n, p): return legendre(n, p) == 1 if p > 2 else True # Iterator over primes for which an integer is a quadratic residue. # # @param n An integer # @param start (default=2) Starting integer (nearest prime greater-than or equal to this) # @yields Primes $p_j,p_{j+1},\ldots$ for which integer n is a quadratic residue modulo $p_i$ # for $i>=j$ def legendre_primes(n, start=2): for p in primes(start): if is_quadratic_residue(n, p): yield p if __name__ == '__main__': from util import take import sys n = int(sys.argv[1]) b = int(sys.argv[2]) for (i, p) in enumerate(take(b, legendre_primes(n))): print(i, p)
def test_take(): field = util.take('--------------------', 1, 'o') assert field[1] == 'o' assert len(field) == 20 assert field.count('o') == 1 assert field.count('-') == 19