def __calculate_area_pixels(self): xc = 0 yc = 0 z = 0 xs, ys, zs = self.spacing proj = Project() orig_orien = proj.original_orientation xy = (xs, ys) yz = (ys, zs) xz = (xs, zs) if (orig_orien == const.SAGITAL): orientation_based_spacing = {"SAGITAL" : xy, "AXIAL" : yz, "CORONAL" : xz} elif(orig_orien == const.CORONAL): orientation_based_spacing = {"CORONAL" : xy, "AXIAL" : xz, "SAGITAL" : yz} else: orientation_based_spacing = {"AXIAL" : xy, "SAGITAL" : yz, "CORONAL" : xz} xs, ys = orientation_based_spacing[self.orientation] self.pixel_list = [] for i in utils.frange(yc - self.y_length/2, yc + self.y_length/2, ys): for k in utils.frange(xc - self.x_length/2, xc + self.x_length/2, xs): self.pixel_list.append((k, i))
def draw(self, a, b, cell, cam, shadow): building = cell.building if building: camera(cam.c.x - a * self.cell_size, cam.c.y - b * self.cell_size) if shadow: for i in frange(0, building.height / 2, 4): t = Vec2(building.s.x, building.s.y) t._mul(i * 0.015) t._add(building.pos) rectfill(t.x - building.w, t.y - building.h, t.x + building.w, t.y + building.h, 5) else: for i in frange(building.height / 2, building.height - 1, 4): t = Vec2(building.s.x, building.s.y) t._mul(i * 0.015) t._add(building.pos) rectfill(t.x - building.w, t.y - building.h, t.x + building.w, t.y + building.h, 5) s = building.s.mul(building.height * 0.015) s._add(building.pos) rectfill(s.x - building.w, s.y - building.h, s.x + building.w, s.y + building.h, building.color)
def __init__(self): self.names = ['fl', 'sax', 'vla', 'vc', 'tbn', 'gtr'] self.fl = fl =Piccolo() self.sax = sax = SopranoSaxophone() self.vla = vla = Viola() self.vc = vc =Violoncello() self.tbn = tbn = Trombone() self.gtr = gtr = ElectricGuitar() self.l = [fl, sax, vla, vc, tbn, gtr] self.d = {} for name, inst in zip(self.names, self.l): inst.nickname = name self.d[name] = inst # lowest, highest notes ranges = [ ('D5', 'C7'), # Piccolo ('C4', 'C6'), # Soprano Sax ('C3', 'E5'), # Viola ('C2', 'E4'), # Cello ('E2', 'B-4'), # Trombone ('E2', 'A5') # Guitar ] for r, i in zip(ranges, self.l): i.lowest_note = Pitch(r[0]) i.highest_note = Pitch(r[1]) i.all_notes = list(frange(i.lowest_note.ps, i.highest_note.ps + 1)) i.all_notes_24 = list(frange(i.lowest_note.ps, i.highest_note.ps + 1, 0.5)) self.piece_range = list(frange(Pitch('E2').ps, Pitch('C7').ps + 1)) self.piece_range_at_least_two_instruments = list(frange(Pitch('E2').ps, Pitch('C6').ps + 1))
def CreateBBGrid(xMin, xMax, yMin, yMax, cellSize, fixes, intervals, searchArea=None, progressor=None): """Build a brownian bridge grid by dividing the extents into square cells of size cellSize. The grid is a list of rows. Each row is a list of cell value. Each row is built left to right, and the rows are built top to bottom. See the EvaluateGridPoint() for details on the remaining parameters.""" if progressor is not None: rowCount = 1 + int((yMax - yMin) / cellSize) if progressor == True: rowIndex = 0 print "Start Building Grid", rowCount else: progressor.SetProgressor("step", "Building Grid...", 0, rowCount, 1) if searchArea is not None: point = arcpy.Point() grid = [] #build the grid from the top down for y in utils.frange(yMax, yMin, -cellSize): row = [] for x in utils.frange(xMin, xMax, cellSize): if searchArea is None: cell = EvaluateGridPoint(x, y, fixes, intervals) #cell = 1 else: point.X, point.Y = x, y if searchArea.contains(point): cell = EvaluateGridPoint(x, y, fixes, intervals) #cell = 1 else: cell = 0 row.append(cell) grid.append(row) if progressor is not None: if progressor == True: rowIndex += 1 print "Finished row", rowIndex, "of", rowCount else: progressor.SetProgressorPosition() return grid
def __init__(self): self.names = [ 'fl', 'ob', 'cl', 'sax', 'tpt', 'vln', 'vib', 'bs' ] self.fl = fl = Flute() self.ob = ob = Oboe() self.cl = cl = Clarinet() self.sax = sax = AltoSaxophone() self.tpt = tpt = Trumpet() self.vln = vln = Violin() self.vib = vib = Vibraphone() self.bs = bs = Contrabass() self.l = [ fl, ob, cl, sax, tpt, vln, vib, bs ] self.d = {} for name, inst in zip(self.names, self.l): inst.nickname = name self.d[name] = inst # lowest, highest notes ranges = [ ('C4', 'C7'), # Flute 60 96 ('B-3', 'G#6'), # Oboe 58 92 ('D3', 'G6'), # Clarinet 50 91 ('D-3', 'A-5'), # Sax 49 80 ('E3', 'B-5'), # Trumpet 52 82 ('G3', 'B6'), # Violin ('F3', 'F6'), # Vibraphone ('E1', 'G3') # Bass ] for r, i in zip(ranges, self.l): i.lowest_note = Pitch(r[0]) i.highest_note = Pitch(r[1]) i.all_notes = list(frange(i.lowest_note.ps, i.highest_note.ps + 1)) i.all_notes_24 = list(frange(i.lowest_note.ps, i.highest_note.ps + 1, 0.5))
def run_experiment1(args): """ Main entry for executing experiment1 and executing yielded files one by one """ threads = [] tcp_agents = OrderedDict({}) tcp_agents['Tahoe'] = 'Agent/TCP' tcp_agents['Reno'] = 'Agent/TCP/Reno' tcp_agents['NewReno'] = 'Agent/TCP/Newreno' tcp_agents['Vegas'] = 'Agent/TCP/Vegas' cbr_rates = frange(1.00, 10.00, args.granularity) for agent_name, agent in tcp_agents.items(): for cbr_rate in cbr_rates: out_file = format_trace_file_name(args.out, agent_name, cbr_rate) nsargs = [ agent, cbr_rate, out_file, args.cbr_start, args.cbr_stop, args.tcp_start, args.tcp_stop, args.duration ] rc = ns('../ns2/experiment1.tcl', nsargs) nsargs.append(str(rc)) nsargs[0] = agent_name print("experiment1, tcp_agent: %s, cbr_rate: %s, " + "out_to: %s, cbr_start: %s, cbr_stop: %s, " + "tcp_start: %s, tcp_stop: %s, duration: %s, " + "return code: %s") % tuple(map(lambda x: str(x), nsargs)) kwargs = { 'cbr_rate': str(cbr_rate), 'agent_name': agent_name, } t = post_process(args.which, out_file, **kwargs) threads.append(t) # waits for all thread finish once get out of the loop wait_for(threads)
def manipulate_latent(model, data, args): print('-' * 30 + 'Begin: manipulate' + '-' * 30) x_test, y_test = data index = np.argmax(y_test, 1) == args.digit number = np.random.randint(low=0, high=sum(index) - 1) x, y = x_test[index][number], y_test[index][number] x, y = np.expand_dims(x, 0), np.expand_dims(y, 0) noise = np.zeros([1, conf.NUM_CLASSES, 16]) x_recons = [] for dim in range(16): # for r in [-0.25, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25]: for r in utils.frange(-0.25, 0.25, 0.05): tmp = np.copy(noise) tmp[:, :, dim] = r x_recon = model.predict([x, y, tmp]) x_recons.append(x_recon) x_recons = np.concatenate(x_recons) img = utils.combine_images(x_recons, height=16) image = img * 255 Image.fromarray(image.astype( np.uint8)).save(args.save_dir + '/manipulate-%d.png' % args.digit) print('manipulated result saved to %s/manipulate-%d.png' % (args.save_dir, args.digit)) print('-' * 30 + 'End: manipulate' + '-' * 30)
def create_iter_elems(param, start_only): """ Creates the elements corresponding to an iteration element. :param param: :param start_only: :return: """ name_attr = param['@name'] from_attr = float(param['@from']) to_attr = float(param['@to']) by_attr = float(param['@by']) # Sanity check (positive steps only), no more than 10 steps # If this test fails, the parameter is replaced by its starting point if not start_only: if from_attr < to_attr and by_attr > 0 and (to_attr - from_attr) / 10 <= 10: logging.info("Parameter iteration found. Adding metrics for range: ") param_range = frange(from_attr, to_attr+by_attr, by_attr) else: logging.warn("Illegal parameter iteration specified: from %f, to %f, by %f", from_attr, to_attr, by_attr) param_range = [from_attr] else: logging.warn("Extra iteration parameters replaced with start point %s") param_range = [from_attr] iter_elems = [{'@name': name_attr, '#text': param_value} for param_value in param_range] return iter_elems
def getisosweights_gauss(weights,age,metallicity,isos,sigma): """ same as getisoweights, but adds Gaussian scatter """ w=[0.]*len(isos) for m,a,feh in zip(weights,age,metallicity): for x in utils.frange(feh-4,feh+4,0.1): w[getn(x,a,isos)]+=m*utils.gauss(x,feh,sigma) return w
def getisosweights_gauss(weights, age, metallicity, isos, sigma): """ same as getisoweights, but adds Gaussian scatter """ w = [0.] * len(isos) for m, a, feh in zip(weights, age, metallicity): for x in utils.frange(feh - 4, feh + 4, 0.1): w[getn(x, a, isos)] += m * utils.gauss(x, feh, sigma) return w
def choose_melody_pitches(self, notes, register, harmonies): print 'Choosing pitches' for h in harmonies: h['pitch_classes'] = [p % 12 for p in h['pitch']] set_start_end(notes) set_start_end(harmonies) previous_note_index = random.choice(range(int(len(register) * .4))) prev = register[previous_note_index] for note in notes: beats = list( frange(note['start'], note['start'] + note['duration'], .25)) note_harmonies = [] for b in beats: h = get_at(b, harmonies) h = h['pitch_classes'] if h not in note_harmonies: note_harmonies.append(h) print 'note_harmonies', note_harmonies if len(note_harmonies) == 1: pitch_options = self.get_pitch_options(note_harmonies[0], prev) else: pitch_options = [] c = Counter() for h in note_harmonies: for p in h: c[p] += 1 common = [] for p, count in c.most_common(): if count == len(note_harmonies): common.append(p) if len(common) > 0: pitch_options = self.get_pitch_options(common, prev) if len(pitch_options) == 0: ranked = [p for p, _ in c.most_common() if p not in common] for p in ranked: pitch_options = self.get_pitch_options([p], prev) if len(pitch_options) > 0: break if len(pitch_options) == 0: pitch_options = [prev - 2, prev - 1, prev + 1, prev + 2] note['pitch'] = random.choice(pitch_options) print note['pitch'] % 12 print prev = note['pitch'] self.add_ornament(note, prev, note_harmonies)
def obtener_resultado(self, min, max=None, interval=None, velocidad=None): if max == None: max = min min = 0 tiempo_aproximado = None tiempo_real = None for tiempo in utils.frange(min, max, interval): self.velocidad_anterior.append( self.obtener_velocidad_aproximada(tiempo)) self.velocidad_anterior_real.append(self.obtener_velocidad(tiempo)) self.tiempo_anterior.append(tiempo) if utils.isclose(self.obtener_velocidad_aproximada(tiempo), velocidad, rel_tol=0.001) and tiempo_aproximado is None: tiempo_aproximado = tiempo if utils.isclose(self.obtener_velocidad(tiempo), velocidad, rel_tol=0.001) and tiempo_real is None: tiempo_real = tiempo if tiempo_real is not None and tiempo_aproximado is not None: return [tiempo_aproximado, tiempo_real]
def update(self, x, y, cell, cam, cells, blobs): building = cell.building if building: cellp = Vec2(cam.pos.x % self.cell_size - x * self.cell_size, cam.pos.y % self.cell_size - y * self.cell_size) building.s = building.pos.sub( cellp.add(self.config.perspective_offset)) s1 = max(building.w, building.h) s2 = min(building.w, building.h) for i in frange(-s1 + s2 / 2, s1 - s2 / 2, s2): p1 = Vec2((cells.pos.x + x) * self.cell_size, (cells.pos.y + y) * self.cell_size).add(building.pos) if s1 == building.w: p1.x += i else: p1.y += i blobs.add_blob(p1, s2) p2 = Vec2((cells.pos.x + x) * self.cell_size, (cells.pos.y + y) * self.cell_size).add(building.pos) if s1 == building.w: p2.x += s1 - s2 / 2 else: p2.y += s1 - s2 / 2 if p2.dist(p1) > 2: blobs.add_blob(p2, s2)
def run_experiment1(args): """ Main entry for executing experiment1 and executing yielded files one by one """ threads = [] tcp_agents = OrderedDict({}) tcp_agents['Tahoe'] = 'Agent/TCP' tcp_agents['Reno'] = 'Agent/TCP/Reno' tcp_agents['NewReno'] = 'Agent/TCP/Newreno' tcp_agents['Vegas'] = 'Agent/TCP/Vegas' cbr_rates = frange(1.00, 10.00, args.granularity) for agent_name, agent in tcp_agents.items(): for cbr_rate in cbr_rates: out_file = format_trace_file_name(args.out, agent_name, cbr_rate) nsargs = [agent, cbr_rate, out_file, args.cbr_start, args.cbr_stop, args.tcp_start, args.tcp_stop, args.duration] rc = ns('../ns2/experiment1.tcl', nsargs) nsargs.append(str(rc)) nsargs[0] = agent_name print ("experiment1, tcp_agent: %s, cbr_rate: %s, " + "out_to: %s, cbr_start: %s, cbr_stop: %s, " + "tcp_start: %s, tcp_stop: %s, duration: %s, " + "return code: %s") % tuple(map(lambda x: str(x), nsargs)) kwargs = { 'cbr_rate': str(cbr_rate), 'agent_name': agent_name, } t = post_process(args.which, out_file, **kwargs) threads.append(t) # waits for all thread finish once get out of the loop wait_for(threads)
def __calculate_area_pixels(self): """ Return the cursor's pixels. This method scans the circle line by line. Extracted equation. http://www.mathopenref.com/chord.html """ xc = 0.0 yc = 0.0 z = 0.0 xs, ys, zs = self.spacing proj = Project() orig_orien = proj.original_orientation xy = (xs, ys) yz = (ys, zs) xz = (xs, zs) if (orig_orien == const.SAGITAL): orientation_based_spacing = {"SAGITAL" : xy, "AXIAL" : yz, "CORONAL" : xz} elif(orig_orien == const.CORONAL): orientation_based_spacing = {"CORONAL" : xy, "AXIAL" : xz, "SAGITAL" : yz} else: orientation_based_spacing = {"AXIAL" : xy, "SAGITAL" : yz, "CORONAL" : xz} xs, ys = orientation_based_spacing[self.orientation] self.pixel_list = [] radius = self.radius for i in utils.frange(yc - radius, yc + radius, ys): # distance from the line to the circle's center d = yc - i # line size line = sqrt(radius**2 - d**2) * 2 # line initial x xi = xc - line/2.0 # line final xf = line/2.0 + xc yi = i for k in utils.frange(xi,xf,xs): self.pixel_list.append((k, yi))
def choose_melody_pitches(self, notes, register, harmonies): print 'Choosing pitches' for h in harmonies: h['pitch_classes'] = [p % 12 for p in h['pitch']] set_start_end(notes) set_start_end(harmonies) previous_note_index = random.choice(range(int(len(register) * .4))) prev = register[previous_note_index] for note in notes: beats = list(frange(note['start'], note['start'] + note['duration'], .25)) note_harmonies = [] for b in beats: h = get_at(b, harmonies) h = h['pitch_classes'] if h not in note_harmonies: note_harmonies.append(h) print 'note_harmonies', note_harmonies if len(note_harmonies) == 1: pitch_options = self.get_pitch_options(note_harmonies[0], prev) else: pitch_options = [] c = Counter() for h in note_harmonies: for p in h: c[p] += 1 common = [] for p, count in c.most_common(): if count == len(note_harmonies): common.append(p) if len(common) > 0: pitch_options = self.get_pitch_options(common, prev) if len(pitch_options) == 0: ranked = [p for p, _ in c.most_common() if p not in common] for p in ranked: pitch_options = self.get_pitch_options([p], prev) if len(pitch_options) > 0: break if len(pitch_options) == 0: pitch_options = [prev - 2, prev - 1, prev + 1, prev + 2] note['pitch'] = random.choice(pitch_options) print note['pitch'] % 12 print prev = note['pitch'] self.add_ornament(note, prev, note_harmonies)
def sumisos_gauss_slow(mass,age,metallicity,fehlist,agelist,isos,sigma): """slower version""" summed_iso = 0.*isos[isos.keys()[0]] for m,a,z in zip(mass,age,metallicity): feh = numarray.log10(z)-numarray.log10(SOLAR) for x in utils.frange(feh-4,feh+4,0.1): summed_iso+= utils.gauss(x,feh,sigma)*m*getiso(x,a,fehlist,agelist,isos) return summed_iso
def sumisos_gauss_slow(mass, age, metallicity, fehlist, agelist, isos, sigma): """slower version""" summed_iso = 0. * isos[isos.keys()[0]] for m, a, z in zip(mass, age, metallicity): feh = numarray.log10(z) - numarray.log10(SOLAR) for x in utils.frange(feh - 4, feh + 4, 0.1): summed_iso += utils.gauss(x, feh, sigma) * m * getiso( x, a, fehlist, agelist, isos) return summed_iso
def choose_solo_notes(self): rest_beginning = random.choice([True, False, False, False, False]) rest_middle = random.choice([True, True, False]) rest_end = random.choice([True, True, True, False, False]) rests = [rest_beginning, rest_middle, rest_end] num_rests = rests.count(True) min_num_divs = 3 + num_rests num_divs = random.randint(min_num_divs, 9) divs = divide(16, num_divs) notes = [{'pitch': None, 'duration': div / 4.0} for div in divs] if rest_beginning: notes[0]['pitch'] = 'rest' if rest_end: notes[-1]['pitch'] = 'rest' if rest_middle: if rest_beginning: start = 2 else: start = 1 if rest_end: end = -2 else: end = -1 middle_rest_index = random.choice(range(len(notes))[start:end]) notes[middle_rest_index]['pitch'] = 'rest' for note in notes: if note['pitch'] != 'rest': note['pitch'] = ps = random.choice(self.soloist_shared_notes) self.soloist_shared_notes = [p for p in frange(ps - 2, ps + 3) if p in self.all_soloists_shared_notes] self.soloist_shared_notes = [p for p in frange(ps - 5, ps + 6) if p in self.all_soloists_shared_notes] return notes
def getisosweights_vgauss(weights, age, metallicity, isos, sigma, dsigmadlogt): """ same as getisoweights, but adds age-dependent Gaussian scatter """ w = [0.] * len(isos) logt0 = numarray.log10(age[0]) for m, a, feh in zip(weights, age, metallicity): logt = numarray.log10(a) sig = sigma + dsigmadlogt * (logt - logt0) for x in utils.frange(feh - 4, feh + 4, 0.1): w[getn(x, a, isos)] += m * utils.gauss(x, feh, sig) return w
def getisosweights_vgauss(weights,age,metallicity,isos,sigma,dsigmadlogt): """ same as getisoweights, but adds age-dependent Gaussian scatter """ w=[0.]*len(isos) logt0 = numarray.log10(age[0]) for m,a,feh in zip(weights,age,metallicity): logt = numarray.log10(a) sig = sigma+dsigmadlogt*(logt-logt0) for x in utils.frange(feh-4,feh+4,0.1): w[getn(x,a,isos)]+=m*utils.gauss(x,feh,sig) return w
def range(ode_method, ode, initial_val, start, end, step_size=0.01): values = [] old_val = initial_val for t in frange(start, end + 0.00001, step_size): values.append((t, old_val)) new_val = ode_method(ode, old_val, t, step_size) old_val = new_val return values
def __init__(self): self.names = ['vln', 'gtr'] self.vln = vln = Violin() self.gtr = gtr = AcousticGuitar() self.l = [vln, gtr] self.d = {} for name, inst in zip(self.names, self.l): inst.nickname = name self.d[name] = inst # lowest, highest notes ranges = [ ('G3', 'B6'), # Violin ('E2', 'G5') # Guitar ] for r, i in zip(ranges, self.l): i.lowest_note = Pitch(r[0]) i.highest_note = Pitch(r[1]) i.all_notes = list(frange(i.lowest_note.ps, i.highest_note.ps + 1)) i.all_notes_24 = list(frange(i.lowest_note.ps, i.highest_note.ps + 1, 0.5))
def __init__(self): self.names = ['vln', 'gtr'] self.vln = vln = Violin() self.gtr = gtr = AcousticGuitar() self.l = [vln, gtr] self.d = {} for name, inst in zip(self.names, self.l): inst.nickname = name self.d[name] = inst # lowest, highest notes ranges = [ ('G3', 'B6'), # Violin ('E2', 'G5') # Guitar ] for r, i in zip(ranges, self.l): i.lowest_note = Pitch(r[0]) i.highest_note = Pitch(r[1]) i.all_notes = list(frange(i.lowest_note.ps, i.highest_note.ps + 1)) i.all_notes_24 = list( frange(i.lowest_note.ps, i.highest_note.ps + 1, 0.5))
def readin(): """ read in the best fit: p,t,s,m,w,model,data,isos,isow = readin() parameters, time, sfr, metallicity, weights, model, data, ... """ log_tmax = Numeric.log10(13.7e9) data = iso.readfits(isodir + "/datarr.fits") isos = iso.readisos(isodir) t = utils.frange(8, log_tmax, 0.001) p = parameters() p.load() m, s, w, isow, model = compute_model(t, p, isos, data) return p, t, s, m, w, model, data, isos, isow
def readin(): """ read in the best fit: p,t,s,m,w,model,data,isos,isow = readin() parameters, time, sfr, metallicity, weights, model, data, ... """ log_tmax = Numeric.log10(13.7e9) data=iso.readfits(isodir+"/datarr.fits") isos = iso.readisos(isodir) t=utils.frange(8,log_tmax,0.001) p=parameters() p.load() m,s,w,isow,model = compute_model(t,p,isos,data) return p,t,s,m,w,model,data,isos,isow
def sumisos_gauss(mass,age,metallicity,fehlist,agelist,isos,sigma): """Faster version.""" isoweight = {} for k in isos.keys(): isoweight[k] = 0. for m,a,z in zip(mass,age,metallicity): feh = numarray.log10(z/SOLAR) for x in utils.frange(feh-4,feh+4,0.1): isoweight[getindices(x,a,fehlist,agelist)]+= utils.gauss(x,feh,sigma)*m summed_iso = 0.*isos[isos.keys()[0]] for k in isoweight.keys(): summed_iso+=isoweight[k]*isos[k] # p(isoweight) return summed_iso
def getisosweights_sgauss(weights,age,sfr,metallicity,isos,sigma,dsigmadlogt): """ Same as getisoweights, but adds sfr-dependent Gaussian scatter. **** CAUTION: not yet tested **** """ w=[0.]*len(isos) logt0 = numarray.log10(age[0]) for m,a,feh,s in zip(weights,age,metallicity,sfr): ss = numarray.maximum(sfr,1.e-5) logs = numarray.log10(ss) logt = numarray.log10(a) sig = sigma+dsigmadlogs*(logs) for x in utils.frange(feh-4,feh+4,0.1): w[getn(x,a,isos)]+=m*utils.gauss(x,feh,sig) return w
def getisosweights_sgauss(weights, age, sfr, metallicity, isos, sigma, dsigmadlogt): """ Same as getisoweights, but adds sfr-dependent Gaussian scatter. **** CAUTION: not yet tested **** """ w = [0.] * len(isos) logt0 = numarray.log10(age[0]) for m, a, feh, s in zip(weights, age, metallicity, sfr): ss = numarray.maximum(sfr, 1.e-5) logs = numarray.log10(ss) logt = numarray.log10(a) sig = sigma + dsigmadlogs * (logs) for x in utils.frange(feh - 4, feh + 4, 0.1): w[getn(x, a, isos)] += m * utils.gauss(x, feh, sig) return w
def Search(func, allSquaredDistances, n, h_ref, min_percent, max_percent, step_percent): h_min = min_percent * h_ref h_max = max_percent * h_ref h_step = step_percent * h_ref h_res = h_max minErr = func(allSquaredDistances, n, h_max) for h_test in utils.frange(h_min, h_max, h_step): err = func(allSquaredDistances, n, h_test) if (err < minErr): minErr = err h_res = h_test return h_res
def sumisos_gauss(mass, age, metallicity, fehlist, agelist, isos, sigma): """Faster version.""" isoweight = {} for k in isos.keys(): isoweight[k] = 0. for m, a, z in zip(mass, age, metallicity): feh = numarray.log10(z / SOLAR) for x in utils.frange(feh - 4, feh + 4, 0.1): isoweight[getindices(x, a, fehlist, agelist)] += utils.gauss(x, feh, sigma) * m summed_iso = 0. * isos[isos.keys()[0]] for k in isoweight.keys(): summed_iso += isoweight[k] * isos[k] # p(isoweight) return summed_iso
def choose_pitches(self, parts): for part in parts: for note in part['notes']: if note['pitch'] != 'rest': note_opts = self.song.note_opts[part['instrument_name']] # note_opts = self.piece.i.d[part['instrument_name']].all_notes note['pitch'] = random.choice(note_opts) # reset note_opts to a range within a 4th in either direction of the chosen note, # but making sure that all notes are in all_note_opts self.song.note_opts[part['instrument_name']] = [p for p in frange(note['pitch'] - 5, note['pitch'] + 6) if p in self.piece.i.d[part['instrument_name']].all_notes] harmonies = get_harmonies(parts) valid = all([validate_harmony(h) for h in harmonies]) if not valid: self.choose_pitches(parts)
def run_experiment2(args): """ Main entry for executing experiment2 and executing yielded files one by one """ threads = [] tcp_agent_pairs = OrderedDict({}) tcp_agent_pairs[('Reno', 'Reno')] = ('Agent/TCP/Reno', 'Agent/TCP/Reno') tcp_agent_pairs[('NewReno', 'Reno')] = ('Agent/TCP/Newreno', 'Agent/TCP/Reno') tcp_agent_pairs[('Vegas', 'Vegas')] = ('Agent/TCP/Vegas', 'Agent/TCP/Vegas') tcp_agent_pairs[('NewReno', 'Vegas')] = ('Agent/TCP/Newreno', 'Agent/TCP/Vegas') cbr_rates = frange(1.00, 10.00, args.granularity) for pair_names, agent_pair in tcp_agent_pairs.items(): for cbr_rate in cbr_rates: pair_name = '-'.join(pair_names) out_file = format_trace_file_name(args.out, pair_name, cbr_rate) nsargs = [ agent_pair[0], agent_pair[1], cbr_rate, out_file, args.cbr_start, args.cbr_stop, args.tcp1_start, args.tcp1_stop, args.tcp2_start, args.tcp2_stop, args.duration ] rc = ns('../ns2/experiment2.tcl', nsargs) nsargs.append(str(rc)) nsargs[1] = pair_name print ("experiment2, tcp_agents: %s, cbr_rate: %s, " + "out_to: %s, cbr_start: %s, cbr_stop: %s, " + "tcp1_start: %s, tcp1_stop: %s, tcp2_start: %s" + "tcp2_stop: %s, duration: %s, return code: %s") \ % tuple(map(lambda x: str(x), nsargs[1:])) kwargs = { 'cbr_rate': str(cbr_rate), 'agent1_name': pair_names[0], 'agent2_name': pair_names[1], } t = post_process(args.which, out_file, **kwargs) threads.append(t) # waits for all thread finish once get out of the loop wait_for(threads)
def run_experiment2(args): """ Main entry for executing experiment2 and executing yielded files one by one """ threads = [] tcp_agent_pairs = OrderedDict({}) tcp_agent_pairs[('Reno', 'Reno')] = ('Agent/TCP/Reno', 'Agent/TCP/Reno') tcp_agent_pairs[('NewReno', 'Reno')] = ('Agent/TCP/Newreno', 'Agent/TCP/Reno') tcp_agent_pairs[('Vegas', 'Vegas')] = ('Agent/TCP/Vegas', 'Agent/TCP/Vegas') tcp_agent_pairs[('NewReno', 'Vegas')] = ('Agent/TCP/Newreno', 'Agent/TCP/Vegas') cbr_rates = frange(1.00, 10.00, args.granularity) for pair_names, agent_pair in tcp_agent_pairs.items(): for cbr_rate in cbr_rates: pair_name = '-'.join(pair_names) out_file = format_trace_file_name(args.out, pair_name, cbr_rate) nsargs = [agent_pair[0], agent_pair[1], cbr_rate, out_file, args.cbr_start, args.cbr_stop, args.tcp1_start, args.tcp1_stop, args.tcp2_start, args.tcp2_stop, args.duration] rc = ns('../ns2/experiment2.tcl', nsargs) nsargs.append(str(rc)) nsargs[1] = pair_name print ("experiment2, tcp_agents: %s, cbr_rate: %s, " + "out_to: %s, cbr_start: %s, cbr_stop: %s, " + "tcp1_start: %s, tcp1_stop: %s, tcp2_start: %s" + "tcp2_stop: %s, duration: %s, return code: %s") \ % tuple(map(lambda x: str(x), nsargs[1:])) kwargs = { 'cbr_rate': str(cbr_rate), 'agent1_name': pair_names[0], 'agent2_name': pair_names[1], } t = post_process(args.which, out_file, **kwargs) threads.append(t) # waits for all thread finish once get out of the loop wait_for(threads)
def getBodies(self): bodies = [] for r in [.3]: for theta in frange(0, 2 * math.pi, .1): x = 0.5 + (r + .05) * math.cos(theta) y = 0.5 + (r + .05) * math.sin(theta) vx = 0.4 * math.sin(theta) vy = -0.4 * math.cos(theta) bodies.append( Body(mass=2, pos=Vector(x, y), vel=Vector(vx, vy), rad=0.01, value=-1)) good = random.sample(bodies, len(bodies) // 3) for g in good: g.color = (0, 255, 0) g.value = 1 return bodies
def parallelizeParameters(paramFile, original): parameters = [] if (paramFile.has_section('Parallel')): #Open the original parameter file with open (original, "r") as originalFile: pFile=originalFile.read() lists = [] replacements = [] #Read parallel section for metrics_par in paramFile.options('Parallel'): if metrics_par.count('#'): value = paramFile.get('Parallel' , metrics_par) #Parse range if value.count('..'): values = value.split('..') init = eval(values[0]) end = eval(values[1]) if len(values) > 2: step = eval(values[2]) else: step = 1 lists.append(u.frange(init, end, step)) #Parse list else: values = value.split(' ') lists.append(values) replacements.append('#' + metrics_par) #Generate all combinations combinations = list(itertools.product(*lists)) for comb in combinations: newParams = pFile for rep in xrange(len(replacements)): newParams = newParams.replace(replacements[rep], str(comb[rep])) newParParser = ConfigParser.RawConfigParser() newParParser.optionxform=str newParParser.readfp(StringIO.StringIO(newParams)) parameters.append(newParParser) else: parameters.append(paramFile) return parameters
def ornament_bridge(a, b, n=None, prev_duration=0.75, width=2): """Find notes that bridge the interval between a and b""" if n is None: # Choose the number of notes in the ornament if prev_duration >= 0.75: n = weighted_choice([1, 2, 3, 4, 5, 6], [3, 3, 4, 5, 4, 3]) else: n = weighted_choice([1, 2, 3], [3, 4, 5]) interval = b - a direction = 0 if interval > 0: direction = 1 if interval < 0: direction = -1 if direction == 0: option_groups = [range(int(a - width), int(a + width + 1))] * n else: offset_interval = float(interval) / n option_groups = [] for offset in list(frange(a, b, offset_interval)): middle = int(round(offset)) opts = range(middle - width, middle + width + 1) option_groups.append(opts) # Prevent the last note in the ornament from being the target note. if b in option_groups[-1]: option_groups[-1].remove(b) ornaments = [] for opts in option_groups: choice = choose(opts, ornaments) ornaments.append(choice) return ornaments
"""Writes all the information about the best fit to the file "protocol" """ import sys sys.path.append("/home/ondrej/py") import utils import iso from params import parameters import fit gauss = True isodir = "/home/ondrej/data/isochrones/696/halo" data = iso.readfits(isodir + "/datarr.fits") isos = iso.readisos(isodir) t = utils.frange(8, 10.25, 0.001) p = parameters() p.load() m = fit.metallicity(t, p) s = fit.sfr(t, p) w = utils.calculateweights(t, s) if gauss: isow = iso.getisosweights_gauss(w, 10.**t, m, isos, p.sigma) else: isow = iso.getisosweights(w, 10.**t, m, isos) model = iso.computeCMD(isow, isos) #model=isos[0][2]*0.0+1.0 model = utils.normalize(model, sum(data.flat)) #model=model/sum(model.flat)
def create_videos(trainer): """ Create videos for visualizing. Will generate a video for each sample, so cancel when you have enough videos. For this experiment, you need to have the images downloaded. """ if not trainer.args.use_cpu: torch.cuda.synchronize() # Switch to evaluate mode trainer.model.eval() len_audio = 20.48 # Only if target_spec_length = 2048 folder_name = os.path.join(trainer.args.results, 'results_video', trainer.args.name_checkpoint) os.makedirs(folder_name, exist_ok=True) with torch.no_grad(): for i, (image_input, audio_input, negatives, nframes, path, _) in enumerate(trainer.loaders['test']): v_init = trainer.z[int(path[0])] z_img = torch.FloatTensor(audio_input.size(0), v_init.shape[0]) for k in range(audio_input.size(0)): z_img[k, :] = trainer.z[int(path[k])] if not trainer.args.loading_image: image_input = trainer.generator.generate_images(z_img, intervention=None) image_input = utils.transform(image_input).detach() # compute output model_output = trainer.model(image_input, audio_input, []) image_output = model_output[0] audio_output = model_output[1] pooling_ratio = round(audio_input.size(3) / audio_output.size(3)) nframes.div_(pooling_ratio) fps = audio_output.size(3) / len_audio for bs in range(image_output.size(0)): try: target_writer = imageio.get_writer(folder_name + f'/output_video_{path[bs]}.mp4', fps=fps) matchmap = utils.compute_matchmap(image_output[bs], audio_output[bs][:, :, 0:nframes[bs]]).data.cpu().numpy().copy() wav = trainer.loaders['test'].dataset.load_audio_raw(path=path[bs]) scipy.io.wavfile.write(folder_name + f'/output_audio_{path[bs]}.mp3', 44100, wav.astype(np.int16)) matchmap = matchmap.transpose(2, 0, 1) # l, h, w matchmap = matchmap / matchmap.sum() matchmap_l, matchmap_h, matchmap_w = matchmap.shape k_ranges = utils.frange(np.max(matchmap) / 100, np.max(matchmap), np.max(matchmap) / 100) for k in k_ranges: binary_mask = matchmap > k map_temp = np.multiply(matchmap, binary_mask) if np.sum(map_temp) < 0.1: break smoothing_factor = 1 struct_element = [[[True]]] * smoothing_factor binary_mask = morph.binary_dilation(binary_mask, struct_element) # Temporal smoothing matchmap = np.multiply(matchmap, binary_mask) matchmap = (matchmap - np.min(matchmap)) / (np.max(matchmap) - np.min(matchmap)) image = trainer.loaders['test'].dataset.load_image_raw(path=path[bs]) for t in range(matchmap_l): mask_resize = np.array([cv2.resize(binary_mask[t, :, :].astype(float), (image.shape[1], image.shape[0]))] * 3).transpose(1, 2, 0) map_t = cv2.resize(matchmap[t, :, :], (image.shape[1], image.shape[0])) map_t = 1 - map_t map_t = 255 * map_t map_t = map_t.astype(np.uint8) map_t = cv2.applyColorMap(map_t, cv2.COLORMAP_JET) im_final = np.multiply((0.3 * image + 0.7 * map_t), mask_resize) + np.multiply(image, 1 - mask_resize) target_writer.append_data(im_final) target_writer.close() # -y means overwrite os.system('ffmpeg -y -i ' + folder_name + f'/output_video_{path[bs]}.mp4 -i ' + folder_name + f'/output_audio_{path[bs]}.mp3 -vf scale=1200:1200 -shortest -strict -2 ' '-c:v libx264 ' + folder_name + f'/video_{path[bs]}.mp4') except KeyboardInterrupt as e: print('you decided to finish!') finally: # Remove temporary files try: os.remove(folder_name + f'/output_video_{path[bs]}.mp4') except OSError: pass try: os.remove(folder_name + f'/output_audio_{path[bs]}.mp3') except OSError: pass return False
def choose_melody_pitches(self, notes, register, harmonies, start_with_rest): # print 'Choosing pitches' for h in harmonies: h['pitch_classes'] = [p % 12 for p in h['pitch']] set_start_end(notes) set_start_end(harmonies) # Pick a random pitch from the instrument's register on which to start previous_note_index = random.choice(range(int(len(register)))) prev = register[previous_note_index] previous_note = {'duration': 1.0, 'pitch': prev} pitch_history = [] first = True # print '-'*10 for note in notes: if first and start_with_rest: note['pitch'] = 'rest' first = False continue beats = list(frange(note['start'], note['start'] + note['duration'], .25)) note_harmonies = [] for b in beats: h = get_at(b, harmonies) h = h['pitch_classes'] if h not in note_harmonies: note_harmonies.append(h) if len(note_harmonies) == 1: pitch_options = self.get_pitch_options(note_harmonies[0], prev) else: pitch_options = [] c = Counter() for h in note_harmonies: for p in h: c[p] += 1 common = [] for p, count in c.most_common(): if count == len(note_harmonies): common.append(p) if len(common) > 0: pitch_options = self.get_pitch_options(common, prev) if len(pitch_options) == 0: ranked = [p for p, _ in c.most_common() if p not in common] for p in ranked: pitch_options = self.get_pitch_options([p], prev) if len(pitch_options) > 0: break if len(pitch_options) == 0: pitch_options = [prev - 2, prev - 1, prev + 1, prev + 2] note['pitch'] = random.choice(pitch_options) self.add_ornament(note, previous_note, note_harmonies, first) # print note prev = note['pitch'] previous_note = note pitch_history.append(note['pitch']) first = False
"""Writes all the information about the best fit to the file "protocol" """ import sys sys.path.append("/home/ondrej/py") import utils import iso from params import parameters import fit gauss=False isodir="/home/ondrej/data/isochrones/696/halo" data=iso.readfits(isodir+"/datarr.fits") isos = iso.readisos(isodir) t=utils.frange(8,10.25,0.001) p=parameters() p.load() m=fit.metallicity(t,p) s=fit.sfr(t,p) w=utils.calculateweights(t,s) if gauss: isow=iso.getisosweights_gauss(w,10.**t,m,isos,p.sigma) else: isow=iso.getisosweights(w,10.**t,m,isos) model=iso.computeCMD(isow,isos) #model=isos[0][2]*0.0+1.0 model=utils.normalize(model,sum(data.flat)) #model=model/sum(model.flat) def plot_residuals(d,m):
def makeSquiggles(self, image): """ actual calculations :param image: bitmap to squigglify :return: """ noOfLines = self.parent.noOfLines.value() height = image.height() width = image.width() amplitude = self.parent.strength.value() strokeWidth = self.parent.lineWidth.value() detail = self.parent.detail.value() invertColors = self.parent.invertColors.checkState() == Qt.Checked verticalSquiggles = self.parent.verticalSquiggles.checkState( ) == Qt.Checked maxBrightness = self.parent.maxBrightness.value() minBrightness = self.parent.minBrightness.value() self.removeOldGraphicsItems() group = QGraphicsItemGroup() finalRow = False minStepSize = self.parent.minStepSize.value() maxStepSize = self.parent.maxStepSize.value() # TODO: too much code duplication! path = QPainterPath() if not verticalSquiggles: scaledystep = max(1, height / noOfLines) for y in frange(0, height, scaledystep): if fabs(y - height) < 1e-3 or y >= height: finalRow = True x = 0 disturbance_direction = -1 prevX = 0 prevY = y while x < width: disturbance_direction = -disturbance_direction grayvalue = qGray(image.pixel(x, y)) if invertColors: grayvalue = 255 - grayvalue stepSize = Mapping.linexp(grayvalue, 0, 255, minStepSize, maxStepSize) stepSize = Mapping.linlin(stepSize, 1, 10, 1, 10 / detail) amplitudeSize = Mapping.linlin(grayvalue, 0, 255, amplitude, 0) if x == 0: path = QPainterPath() path.moveTo(x, y) x = prevX + stepSize newY = prevY + amplitudeSize * disturbance_direction if minBrightness <= grayvalue <= maxBrightness: path.quadTo((prevX + x) / 2, newY, x, prevY) else: path.moveTo(x, prevY) if x >= width: item = QGraphicsPathItem(path) pen = QPen() pen.setWidth(strokeWidth) item.setPen(pen) group.addToGroup(item) prevX = x prevY = y if finalRow: break else: scaledxstep = max(1, width / noOfLines) for x in frange(0, width, scaledxstep): if fabs(x - width) < 1e-3 or x >= width: finalRow = True y = 0 disturbance_direction = -1 prevX = x prevY = 0 while y < height: disturbance_direction = -disturbance_direction grayvalue = qGray(image.pixel(x, y)) if invertColors: grayvalue = 255 - grayvalue stepSize = Mapping.linexp(grayvalue, 0, 255, minStepSize, maxStepSize) stepSize = Mapping.linlin(stepSize, 1, 10, 1, 10 / detail) amplitudeSize = Mapping.linlin(grayvalue, 0, 255, amplitude, 0) if y == 0: path = QPainterPath() path.moveTo(x, y) y = prevY + stepSize newX = prevX + amplitudeSize * disturbance_direction if minBrightness <= grayvalue <= maxBrightness: path.quadTo(newX, (prevY + y) / 2, prevX, y) else: path.moveTo(x, prevY) if y >= height: item = QGraphicsPathItem(path) pen = QPen() pen.setWidth(strokeWidth) item.setPen(pen) group.addToGroup(item) prevX = x prevY = y if finalRow: break self.addNewGraphicsItems(group)
def objective(trial, ctf): learning_rate = trial.suggest_uniform('learning_rate', 0.0, 0.1) gamma = trial.suggest_uniform('gamma', 0.0, 0.1) dimensions = trial.suggest_int('dimensions', 750, 1000) x, y = calculate_fisher_discriminant(training_images_flat, training_labels, ctf) local_svm = SVMTree(x.shape[1], list(range(10)), learning_rate, dimensions=dimensions, gamma=gamma) return local_svm.train(x, y, 14) data = [] for ctf in frange(0.0, 1.0, 0.2): study = optuna.create_study(pruner=MedianPruner()) study.optimize(lambda trial: objective(trial, ctf), n_trials=25) best = study.best_params x, y = calculate_fisher_discriminant(training_images_flat, training_labels, ctf) svm = SVMTree(x.shape[1], list(range(10)), best['learning_rate'], dimensions=best['dimensions'], gamma=best['gamma']) t1 = time.time() svm.train(x, y, 14) t2 = time.time() adv_ex, bro_ex, grad_ex, peraccuracy = stage(test_images, test_labels,
def run(solvent_ratio): utils.Molecule.set_params('oplsaa4.prm') tail = utils.Molecule('hexanenitrile.arc') solvent = utils.Molecule('methane.arc') for a in tail.atoms: a.x, a.z = a.z, a.x directory = 'lammps' os.chdir(directory) n_steps = 1 for step in range(0,n_steps): atoms = [] bonds = [] angles = [] dihedrals = [] S = 3.5 box_size = [20,20,20] theta = 0; z = 0.0 print len(atoms)/len(tail.atoms) atom_count = len(atoms) solvent_spacing = [(max(solvent.atoms, key=lambda a:a.x).x-min(solvent.atoms, key=lambda a:a.x).x), (max(solvent.atoms, key=lambda a:a.y).y-min(solvent.atoms, key=lambda a:a.y).y), (max(solvent.atoms, key=lambda a:a.z).z-min(solvent.atoms, key=lambda a:a.z).z)] solvent_spacing = [x+1.0 for x in solvent_spacing] max_vdw_r = max(solvent.atoms, key=lambda a:a.type.vdw_r).type.vdw_r solvent_spacing = [max(x,max_vdw_r) for x in solvent_spacing] for x in utils.frange(-box_size[0]/2, box_size[0]/2-solvent_spacing[0], solvent_spacing[0]): for y in utils.frange(-box_size[1]/2, box_size[1]/2-solvent_spacing[1], solvent_spacing[1]): for z in utils.frange(-box_size[2]/2, box_size[2]/2-solvent_spacing[2], solvent_spacing[2]): try: for a in atoms[:atom_count]: if (a.x-x)**2 + (a.y-y)**2 + (a.z-z)**2 < max_vdw_r**2: raise Exception() solvent.add_to(x, y, z, atoms, bonds, angles, dihedrals) except: pass T = 94.0 P = 1.45 print (len(atoms)-atom_count)/len(solvent.atoms) filetypes.write_xyz('out', atoms) run_name = 'azoto_solv__'+str(int(step)) atom_types = dict( [(t.type,True) for t in atoms] ).keys() atom_type_numbers = dict( [(t,i+1) for i,t in enumerate(atom_types)] ) is_charged = True lammps.write_data_file_general(atoms, bonds, angles, dihedrals, box_size, run_name, atom_types=atom_types) os.system('cp ../'+sys.argv[0]+' '+run_name+'.py') f = open(run_name+'.in', 'w') f.write('units real\natom_style full #bonds, angles, dihedrals, impropers, charges\n') if is_charged: f.write('pair_style lj/cut/coul/long 8.0\n') else: f.write('pair_style lj/cut 8.0\n') if bonds: f.write('bond_style harmonic\n') if angles: f.write('angle_style harmonic\n') if dihedrals: f.write('dihedral_style opls\n') if is_charged: f.write('kspace_style pppm 1.0e-3\n') f.write('special_bonds lj/coul 0.0 0.0 0.5\nread_data '+run_name+'.data\n') f.write('''thermo 0 dump 1 all xyz 10000 '''+run_name+'''.xyz thermo_style custom etotal ke temp pe ebond eangle edihed evdwl ecoul elong press lx ly lz tpcpu thermo_modify line multi format float %14.6f minimize 0.0 1.0e-8 1000 100000 velocity all create '''+str(T)+''' 1 rot yes dist gaussian fix press all npt temp '''+str(T)+' '+str(T)+''' 100 aniso '''+str(P)+' '+str(P)+''' 500 timestep 4.0 neigh_modify check yes every 1 delay 0 run_style respa 4 2 2 2 inner 2 4.5 6.0 middle 3 7.0 8.0 outer 4 thermo 100 run 25000 unfix press fix dynamics all nvt temp '''+str(T)+' '+str(T)+''' 100 thermo_style custom epair pe etotal thermo 1 run 25000 ''') f.close() if True: #start multiple processes os.system('nohup ~/lammps/src/lmp_g++_no_cuda -in %s.in -log %s.log &> /dev/null &' % (run_name, run_name) ) print 'Running', run_name else: os.system('~/lammps/src/lmp_g++_no_cuda -in %s.in -log %s.log' % (run_name, run_name) ) sys.exit() os.chdir('..')
def CVL(fixes, lowerBound, upperBound, step, scaleFactor): """Return a list (with step number of items). Each item is a tuple of (variance, likelihood). Variances in the list are equally distributed between lowerBound to upperBound. The likelihood is based on Equation 7 in Horne, et al and I beleive it is a likelihood cross validation. fixes is a list of fixes where each fix is (time, x, y, locational_variance). fixes are assumed to be in chronological order, and time is a number. The units of time are unimportant, as the mobility variance varies with the units chosen. The algorithm is as follows: Assume a mobility variance, vm. For the first three fixes, pretend that fix2 is missing, and calculate the normal distribution at the observed location of fix2 based on the predicted (part way between fix1 and fix3) mean location of fix2. Do this again for fixes 2,3,4 then 3,4,5 etc until the last 3 fixes. The product of the normal distributions is the indicator of the likelihood of the assumed mobility variance. In Horne, et al (2007), only odd numbered fixes are considered independent. in the adehabitat code, each group of three is considered. This code uses the second approach.""" #print "In CVL(); len(fixes) =", len(fixes), "lowerBound =",lowerBound, "upperBound =",upperBound, "step =", step, "scaleFactor =", scaleFactor if len(fixes) < 3: raise ValueError, "Not enough fixes provided" results = [] for vm in utils.frange(lowerBound, (upperBound + step), step): #print "vm = ",vm likelihood = 1 for i in range(1, (len(fixes) - 1)): #times prevTime = fixes[i-1][0] thisTime = fixes[i][0] nextTime = fixes[i+1][0] #locations prevX = fixes[i-1][1] thisX = fixes[i][1] nextX = fixes[i+1][1] prevY = fixes[i-1][2] thisY = fixes[i][2] nextY = fixes[i+1][2] #locational Variance prevV = fixes[i-1][3] nextV = fixes[i+1][3] T = nextTime - prevTime #a = alphai = percent of path a = float(thisTime - prevTime) / T # (meanX, meanY) = mui(ti) = mean predicted location at time i meanX = prevX + a * (nextX - prevX) meanY = prevY + a * (nextY - prevY) # v = sigmai^2(ti) = total assumed brownian motion variance at time i v = T * a * (1 - a) * vm + (1-a)**2 * prevV + a**2 * nextV #distanceSquared = ((thisX - meanX)**2 + (thisY - meanY)**2) #print "thisX =",thisX,"thisY =",thisY,"meanX =",meanX,"meanY =",meanY, "v =", v #print "distanceSquared =",distanceSquared, "T =", T, "a =", a, "vm =", vm, "prevV =", prevV, "nextV =", nextV, # (thisX, thisY) = Zi = location at which to get value of PDF normal = Horne2dNormal(thisX, thisY, meanX, meanY, v) #print "normal =",normal # The normal is typically very small (the total area under the normal curve = 1.0) # the scale factor keeps the product from decaying to zero with a # large number of fixes. Since the likelihoods are relative, the # scale factor will not alter the relative likelihood of the Vm likelihood *= (normal * scaleFactor) #print "likelihood =",likelihood if likelihood == 0: break results.append((vm, likelihood)) return results
def CVL(fixes, lowerBound, upperBound, step, scaleFactor): """Return a list (with step number of items). Each item is a tuple of (variance, likelihood). Variances in the list are equally distributed between lowerBound to upperBound. The likelihood is based on Equation 7 in Horne, et al and I beleive it is a likelihood cross validation. fixes is a list of fixes where each fix is (time, x, y, locational_variance). fixes are assumed to be in chronological order, and time is a number. The units of time are unimportant, as the mobility variance varies with the units chosen. The algorithm is as follows: Assume a mobility variance, vm. For the first three fixes, pretend that fix2 is missing, and calculate the normal distribution at the observed location of fix2 based on the predicted (part way between fix1 and fix3) mean location of fix2. Do this again for fixes 2,3,4 then 3,4,5 etc until the last 3 fixes. The product of the normal distributions is the indicator of the likelihood of the assumed mobility variance. In Horne, et al (2007), only odd numbered fixes are considered independent. in the adehabitat code, each group of three is considered. This code uses the second approach.""" #print "In CVL(); len(fixes) =", len(fixes), "lowerBound =",lowerBound, "upperBound =",upperBound, "step =", step, "scaleFactor =", scaleFactor if len(fixes) < 3: raise ValueError, "Not enough fixes provided" results = [] for vm in utils.frange(lowerBound, (upperBound + step), step): #print "vm = ",vm likelihood = 1 for i in range(1, (len(fixes) - 1)): #times prevTime = fixes[i - 1][0] thisTime = fixes[i][0] nextTime = fixes[i + 1][0] #locations prevX = fixes[i - 1][1] thisX = fixes[i][1] nextX = fixes[i + 1][1] prevY = fixes[i - 1][2] thisY = fixes[i][2] nextY = fixes[i + 1][2] #locational Variance prevV = fixes[i - 1][3] nextV = fixes[i + 1][3] T = nextTime - prevTime #a = alphai = percent of path a = float(thisTime - prevTime) / T # (meanX, meanY) = mui(ti) = mean predicted location at time i meanX = prevX + a * (nextX - prevX) meanY = prevY + a * (nextY - prevY) # v = sigmai^2(ti) = total assumed brownian motion variance at time i v = T * a * (1 - a) * vm + (1 - a)**2 * prevV + a**2 * nextV #distanceSquared = ((thisX - meanX)**2 + (thisY - meanY)**2) #print "thisX =",thisX,"thisY =",thisY,"meanX =",meanX,"meanY =",meanY, "v =", v #print "distanceSquared =",distanceSquared, "T =", T, "a =", a, "vm =", vm, "prevV =", prevV, "nextV =", nextV, # (thisX, thisY) = Zi = location at which to get value of PDF normal = Horne2dNormal(thisX, thisY, meanX, meanY, v) #print "normal =",normal # The normal is typically very small (the total area under the normal curve = 1.0) # the scale factor keeps the product from decaying to zero with a # large number of fixes. Since the likelihoods are relative, the # scale factor will not alter the relative likelihood of the Vm likelihood *= (normal * scaleFactor) #print "likelihood =",likelihood if likelihood == 0: break results.append((vm, likelihood)) return results
def readCEData(parameters, variable): try: fileName = parameters.get(variable, 'input_files') field = parameters.get(variable, 'field') popType = parameters.get(variable, 'population_type') dims = parameters.getint(variable, 'dimensions') if popType != 'statistical' and popType != 'spatial': print 'ERROR: Population type ' + popType + ' for field ' + field + ' incorrect.' if dims < 1 or dims > 4: print 'ERROR: Number of dimensions ' + dims + ' for field ' + field + ' is incorrect. It should be from 1 up to 4.' stat_from = "" time_from = "" group_from = "" if parameters.has_option(variable, 'stat_from'): stat_from = eval(parameters.get(variable, 'stat_from')) stat_to = eval(parameters.get(variable, 'stat_to')) stat_step = 1 if parameters.has_option(variable, 'stat_step'): stat_step = eval(parameters.get(variable, 'stat_step')) if parameters.has_option(variable, 'time_from'): time_from = eval(parameters.get(variable, 'time_from')) time_to = eval(parameters.get(variable, 'stat_to')) time_step = 1 if parameters.has_option(variable, 'time_step'): time_step = eval(parameters.get(variable, 'time_step')) if parameters.has_option(variable, 'group_from'): group_from = eval(parameters.get(variable, 'group_from')) group_to = eval(parameters.get(variable, 'group_to')) group_step = 1 if parameters.has_option(variable, 'group_step'): group_step = eval(parameters.get(variable, 'group_step')) #Read actual dimensions in the field fName = fileName.replace('[P]', str(stat_from)).replace('[T]', str(time_from)).replace('[G]', str(group_from)) fileDims = getDataDims(fName, field) dimesionsToAdd = dims - fileDims if dims < fileDims: print 'ERROR: Variable ' + variable + ' has more dimensions than specified.' if dims == 1: if popType == 'spatial': print 'ERROR: Variable ' + variable + ' should have more than one dimension to use spatial population_type.' if dimesionsToAdd == 0: if popType == 'spatial': aux_init_data = readSpatialData(fileName, field, parameters, variable) if popType == 'statistical': aux_init_data = readField(fileName, field) if dimesionsToAdd == 1: if popType == 'spatial': if fileName.count('[T]') != 1: print 'ERROR: Spatial population for variable ' + variable + ' must have "[T]" wildcard.' try: time_from = eval(parameters.get(variable, 'time_from')) time_to = eval(parameters.get(variable, 'time_to')) initial = True i = 0 for index in u.frange(time_from, time_to + time_step, time_step): fName = fileName.replace('[T]', str(index)) tmp = readSpatialData(fName, field, parameters, variable) if initial: aux_init_data = np.ndarray(shape=(round((time_to - time_from)/time_step + 1), len(tmp)), dtype=object) initial = False aux_init_data[i, :] = tmp[:] i = i + 1 except ValueError as e: print 'ERROR: ', e except ConfigParser.NoOptionError: print 'ERROR: Error in ', variable, '. Spatial population must have time_from and time_to configurations.' if popType == 'statistical': if fileName.count('[P]') != 1: print 'ERROR: Statistical population for variable ' + variable + ' must have "[P]" wildcard.' try: stat_from = eval(parameters.get(variable, 'stat_from')) stat_to = eval(parameters.get(variable, 'stat_to')) stat_step = 1 if parameters.has_option(variable, 'stat_step'): stat_step = eval(parameters.get(variable, 'stat_step')) initial = True i = 0 for index_stat in u.frange(stat_from, stat_to + stat_step, stat_step): fName = fileName.replace('[P]', str(index_stat)) var = readField (fName, field) if initial: aux_init_data = np.ndarray(shape=(var.shape + (round((stat_to - stat_from)/stat_step + 1),)), dtype='float') initial = False aux_init_data[..., i] = var i = i + 1 except ValueError as e: import traceback print traceback.format_exc() print 'ERROR: ', e except ConfigParser.NoOptionError: print 'ERROR: Error in ', variable, '. Statistical population must have stat_from and stat_to configurations.' if dimesionsToAdd == 2: if popType == 'spatial': if fileName.count('[T]') != 1 or fileName.count('[G]') != 1: print 'ERROR: Spatial population for variable ' + variable + ' must have "[T]" and "[G]" wildcards.' try: group_from = eval(parameters.get(variable, 'group_from')) group_to = eval(parameters.get(variable, 'group_to')) group_step = 1 if parameters.has_option(variable, 'group_step'): group_step = eval(parameters.get(variable, 'group_step')) time_from = eval(parameters.get(variable, 'time_from')) time_to = eval(parameters.get(variable, 'time_to')) time_step = 1 if parameters.has_option(variable, 'time_step'): time_step = eval(parameters.get(variable, 'time_step')) initial = True i = 0 for index_group in u.frange(group_from, group_to + group_step, group_step): j = 0 for index_time in u.frange(time_from, time_to + time_step, time_step): fName = fileName.replace('[T]', str(index_time)).replace('[G]', str(index_group)) tmp = readSpatialData(fName, field, parameters, variable) if initial: aux_init_data = np.ndarray(shape=(round((group_to - group_from)/group_step + 1), round((time_to - time_from)/time_step + 1), len(tmp)), dtype=object) initial = False aux_init_data[i, j, :] = tmp[:] j = j + 1 i = i + 1 except ValueError as e: print 'ERROR: ', e except ConfigParser.NoOptionError: print 'ERROR: Error in ', variable, '. Spatial population must have group_from, group_to, time_from and time_to configurations.' if popType == 'statistical': if fileName.count('[P]') != 1 or fileName.count('[T]') != 1: print 'ERROR: Statistical population for variable ' + variable + ' must have "[P]" and "[T]" wildcards.' try: time_from = eval(parameters.get(variable, 'time_from')) time_to = eval(parameters.get(variable, 'time_to')) time_step = 1 if parameters.has_option(variable, 'time_step'): time_step = eval(parameters.get(variable, 'time_step')) stat_from = eval(parameters.get(variable, 'stat_from')) stat_to = eval(parameters.get(variable, 'stat_to')) stat_step = 1 if parameters.has_option(variable, 'stat_step'): stat_step = eval(parameters.get(variable, 'stat_step')) initial = True i = 0 for index_time in u.frange(time_from, time_to + time_step, time_step): j = 0 for index_stat in u.frange(stat_from, stat_to + stat_step, stat_step): fName = fileName.replace('[P]', str(index_stat)).replace('[T]', str(index_time)) var = readField (fName, field) if initial: aux_init_data = np.ndarray(shape=((round((time_to - time_from)/time_step + 1),) + var.shape + (round((stat_to - stat_from)/stat_step + 1),)), dtype='float') initial = False aux_init_data[i, ..., j] = var j = j + 1 i = i + 1 except ValueError as e: print 'ERROR: ', e except ConfigParser.NoOptionError: print 'ERROR: Error in ', variable, '. Statistical population must have time_from, time_to, stat_from and stat_to configurations.' if dimesionsToAdd == 3: if popType == 'spatial': print 'ERROR: Error in ', variable, '. Cannot add more than 2 extra dimensions to the spatial data provided.' if popType == 'statistical': if fileName.count('[P]') != 1 or fileName.count('[T]') != 1 or fileName.count('[G]') != 1: print 'ERROR: Statistical population for variable ' + variable + ' must have "[P]", "[G]" and "[T]" wildcards.' try: group_from = eval(parameters.get(variable, 'group_from')) group_to = eval(parameters.get(variable, 'group_to')) group_step = 1 if parameters.has_option(variable, 'group_step'): group_step = eval(parameters.get(variable, 'group_step')) time_from = eval(parameters.get(variable, 'time_from')) time_to = eval(parameters.get(variable, 'time_to')) time_step = 1 if parameters.has_option(variable, 'time_step'): time_step = eval(parameters.get(variable, 'time_step')) stat_from = eval(parameters.get(variable, 'stat_from')) stat_to = eval(parameters.get(variable, 'stat_to')) stat_step = 1 if parameters.has_option(variable, 'stat_step'): stat_step = eval(parameters.get(variable, 'stat_step')) initial = True i = 0 for index_group in u.frange(group_from, group_to + group_step, group_step): j = 0 for index_time in u.frange(time_from, time_to + time_step, time_step): k = 0 for index_stat in u.frange(stat_from, stat_to + stat_step, stat_step): fName = fileName.replace('[P]', str(index_stat)).replace('[T]', str(index_time)).replace('[G]', str(index_group)) var = readField (fName, field) if initial: aux_init_data = np.ndarray(shape=((round((group_to - group_from)/group_step + 1), round((time_to - time_from)/time_step + 1),) + var.shape + (round((stat_to - stat_from)/stat_step + 1),)), dtype='float') initial = False aux_init_data[i, j, ..., k] = var k = k + 1 j = j + 1 i = i + 1 except ValueError as e: print 'ERROR: ', e except ConfigParser.NoOptionError: print 'ERROR: Error in ', variable, '. Statistical population must have group_from, group_to, time_from, time_to, stat_from and stat_to configurations.' return aux_init_data except Exception as e: print 'ERROR: ', e
def readMeshlessData(parameters, variable): try: fileName = parameters.get(variable, 'input_files') field = parameters.get(variable, 'field') popType = parameters.get(variable, 'population_type') dims = parameters.getint(variable, 'dimensions') if popType != 'statistical' and popType != 'spatial': print 'ERROR: Population type ' + popType + ' for field ' + field + ' incorrect.' if dims < 2 or dims > 4: print 'ERROR: Number of dimensions ' + dims + ' for field ' + field + ' is incorrect. It should be 2, 3, or 4.' if dims == 2: if popType == 'spatial': return np.array(readSpatialData(fileName, field, parameters, variable)) if popType == 'statistical': if fileName.count('[P]') != 1: print 'ERROR: Statistical population for variable ' + variable + ' must have "[P]" wildcard.' try: stat_from = eval(parameters.get(variable, 'stat_from')) stat_to = eval(parameters.get(variable, 'stat_to')) stat_step = 1 if parameters.has_option(variable, 'stat_step'): stat_step = eval(parameters.get(variable, 'stat_step')) initial = True i = 0 for index_stat in u.frange(stat_from, stat_to + stat_step, stat_step): fName = fileName.replace('[P]', str(index_stat)) var = readField (fName, field) if initial: aux_init_data = np.ndarray(shape=(var.shape[0], round((stat_to - stat_from)/stat_step + 1)), dtype='float') initial = False aux_init_data[:, i] = np.resize(var, var.size) i = i + 1 except ValueError as e: print 'ERROR: ', e except ConfigParser.NoOptionError: print 'ERROR: Error in ', variable, '. Statistical population must have stat_from and stat_to configurations.' if dims == 3: if popType == 'spatial': if fileName.count('[T]') != 1: print 'ERROR: Spatial population for variable ' + variable + ' must have "[T]" wildcard.' try: time_from = eval(parameters.get(variable, 'time_from')) time_to = eval(parameters.get(variable, 'time_to')) time_step = 1 if parameters.has_option(variable, 'time_step'): time_step = eval(parameters.get(variable, 'time_step')) initial = True i = 0 for index in u.frange(time_from, time_to + time_step, time_step): fName = fileName.replace('[T]', str(index)) tmp = readSpatialData(fName, field, parameters, variable) if initial: aux_init_data = np.ndarray(shape=(round((time_to - time_from)/time_step + 1), len(tmp)), dtype=object) initial = False aux_init_data[i, :] = tmp[:] i = i + 1 except ValueError as e: print 'ERROR: ', e except ConfigParser.NoOptionError: print 'ERROR: Error in ', variable, '. Spatial population must have time_from and time_to configurations.' if popType == 'statistical': if fileName.count('[P]') != 1 or fileName.count('[T]') != 1: print 'ERROR: Statistical population for variable ' + variable + ' must have "[P]" and "[T]" wildcard.' try: stat_from = eval(parameters.get(variable, 'stat_from')) stat_to = eval(parameters.get(variable, 'stat_to')) stat_step = 1 if parameters.has_option(variable, 'stat_step'): stat_step = eval(parameters.get(variable, 'stat_step')) time_from = eval(parameters.get(variable, 'time_from')) time_to = eval(parameters.get(variable, 'time_to')) time_step = 1 if parameters.has_option(variable, 'time_step'): time_step = eval(parameters.get(variable, 'time_step')) initial = True i = 0 for index_time in u.frange(time_from, time_to + time_step, time_step): j = 0 for index_stat in u.frange(stat_from, stat_to + stat_step, stat_step): fName = fileName.replace('[P]', str(index_stat)).replace('[T]', str(index_time)) var = readField (fName, field) if initial: aux_init_data = np.ndarray(shape=(round((time_to - time_from)/time_step + 1), var.shape[0], round((stat_to - stat_from)/stat_step + 1)), dtype='float') initial = False aux_init_data[i, :, j] = np.resize(var, var.size) j = j + 1 i = i + 1 except ValueError as e: print 'ERROR: ', e except ConfigParser.NoOptionError: print 'ERROR: Error in ', variable, '. Statistical population must have time_from, time_to, stat_from and stat_to configurations.' if dims == 4: if popType == 'spatial': if fileName.count('[T]') != 1 or fileName.count('[G]') != 1: print 'ERROR: Spatial population for variable ' + variable + ' must have "[T]" and "[G]" wildcard.' try: group_from = eval(parameters.get(variable, 'group_from')) group_to = eval(parameters.get(variable, 'group_to')) group_step = 1 if parameters.has_option(variable, 'group_step'): group_step = eval(parameters.get(variable, 'group_step')) time_from = eval(parameters.get(variable, 'time_from')) time_to = eval(parameters.get(variable, 'time_to')) time_step = 1 if parameters.has_option(variable, 'time_step'): time_step = eval(parameters.get(variable, 'time_step')) initial = True i = 0 for index_group in u.frange(group_from, group_to + group_step, group_step): j = 0 for index_time in u.frange(time_from, time_to + time_step, time_step): fName = fileName.replace('[T]', str(index_time)).replace('[G]', str(index_group)) tmp = readSpatialData(fName, field, parameters, variable) if initial: aux_init_data = np.ndarray(shape=(round((group_to - group_from)/group_step + 1), round((time_to - time_from)/time_step + 1), len(tmp)), dtype=object) initial = False aux_init_data[i, j, :] = tmp[:] j = j + 1 i = i + 1 except ValueError as e: print 'ERROR: ', e except ConfigParser.NoOptionError: print 'ERROR: Error in ', variable, '. Spatial population must have group_from, group_to, time_from and time_to configurations.' if popType == 'statistical': if fileName.count('[P]') != 1 or fileName.count('[T]') != 1 or fileName.count('[G]') != 1: print 'ERROR: Statistical population for variable ' + variable + ' must have "[P]", "[G]" and "[T]" wildcard.' try: group_from = eval(parameters.get(variable, 'group_from')) group_to = eval(parameters.get(variable, 'group_to')) group_step = 1 if parameters.has_option(variable, 'group_step'): group_step = eval(parameters.get(variable, 'group_step')) stat_from = eval(parameters.get(variable, 'stat_from')) stat_to = eval(parameters.get(variable, 'stat_to')) stat_step = 1 if parameters.has_option(variable, 'stat_step'): stat_step = eval(parameters.get(variable, 'stat_step')) time_from = eval(parameters.get(variable, 'time_from')) time_to = eval(parameters.get(variable, 'time_to')) time_step = 1 if parameters.has_option(variable, 'time_step'): time_step = eval(parameters.get(variable, 'time_step')) initial = True i = 0 for index_group in u.frange(group_from, group_to + group_step, group_step): j = 0 for index_time in u.frange(time_from, time_to + time_step, time_step): k = 0 for index_stat in u.frange(stat_from, stat_to + stat_step, stat_step): fName = fileName.replace('[P]', str(index_stat)).replace('[T]', str(index_time)).replace('[G]', str(index_group)) var = readField (fName, field) if initial: aux_init_data = np.ndarray(shape=(round((group_to - group_from)/group_step + 1), round((time_to - time_from)/time_step + 1), var.shape[0], round((stat_to - stat_from)/stat_step + 1)), dtype='float') initial = False aux_init_data[i, j, :, k] = np.resize(var, var.size) k = k + 1 j = j + 1 i = i + 1 except ValueError as e: print 'ERROR: ', e except ConfigParser.NoOptionError: print 'ERROR: Error in ', variable, '. Statistical population must have stat_from and stat_to configurations.' return aux_init_data except Exception as e: print 'ERROR: ', e
def raycast(self, screen, numrays=5, angle=45): self.rays = [] for curangle in utils.frange(-angle, angle, angle / 2, inclusive=True): r, c = utils.ray(screen, self.pos, self.rot + utils.rad(curangle)) self.rays.append(Ray(r, c, self.pos))
self.running = False self.ticks -= self.STEADY_STATE_THRESHOLD # hack to make run() return the right val self.print("Controller reached steady state in {} ticks".format(self.ticks)) else: self.steady_state_ticks = 0 output = error * self.PROPORTIONAL_GAIN self.temperature_delta = output if __name__ == "__main__": # sim = PController(0.01) # sim.run() results = {} start = 0.001 step = 0.01 stop = 0.999 for gain in frange(start, stop, step): sim = PController(gain, verbose=False) results[gain] = sim.run() print("{:<10}|{:>8}".format("Gain", "Ticks")) for gain, ticks in sorted(results.items(), key=operator.itemgetter(0)): print("{:<10f}|{:>8}".format(gain, ticks)) gain, ticks = min(results.items(), key=operator.itemgetter(1)) print("Winner: {:f} at {} ticks".format(gain, ticks))
def readGraphData(parameters, variable): try: fieldType = parameters.get(variable, "field_type") fileName = parameters.get(variable, "input_files") field = parameters.get(variable, "field") popType = parameters.get(variable, "population_type") dims = parameters.getint(variable, "dimensions") if popType != "statistical" and popType != "spatial": print "ERROR: Population type " + popType + " for field " + field + " incorrect." if fieldType != "edge" and fieldType != "node": print "ERROR: Field type" + fieldType + " for field " + field + " incorrect." if dims < 2 or dims > 4: print "ERROR: Number of dimensions " + dims + " for field " + field + " is incorrect. It should be 2, 3, or 4." if dims == 2: if popType == "spatial": if fieldType == "edge": print "ERROR: Edge, " + field + ", population cannot be spatial." aux_init_data = readGraphFieldSpatial(fileName, field) if popType == "statistical": if fileName.count("[P]") != 1: print "ERROR: Statistical population for bidimensional variable " + variable + ' must have "[P]" wildcard. E.g: ./output_[P]/result.dot' try: stat_from = eval(parameters.get(variable, "stat_from")) stat_to = eval(parameters.get(variable, "stat_to")) stat_step = 1 if parameters.has_option(variable, "stat_step"): stat_step = eval(parameters.get(variable, "stat_step")) initial = True i = 0 for index in u.frange(stat_from, stat_to + stat_step, stat_step): print stat_from, index fName = fileName.replace("[P]", str(index)) g = readGraphField(fName, field, fieldType) if initial: aux_init_data = np.ndarray( shape=(len(g), round((stat_to - stat_from) / stat_step + 1)), dtype="float" ) initial = False aux_init_data[:, i] = g[:] i = i + 1 except ValueError as e: print "ERROR: ", e except ConfigParser.NoOptionError: print "ERROR: Error in ", variable, ". Bidimensional statistical population must have stat_from and stat_to configurations." if dims == 3: if popType == "spatial": if fileName.count("[T]") != 1: print "ERROR: Spatial population for tridimensional variable " + variable + ' must have "[T]" wildcard. E.g: ./output_[T]/result.dot' aux_init_data = [] if fieldType == "edge": print "ERROR: Edge, " + field + ", population cannot be spatial." try: time_from = eval(parameters.get(variable, "time_from")) time_to = eval(parameters.get(variable, "time_to")) time_step = 1 if parameters.has_option(variable, "time_step"): time_step = eval(parameters.get(variable, "time_step")) initial = True i = 0 for index in u.frange(time_from, time_to + time_step, time_step): fName = fileName.replace("[T]", str(index)) tmp = readGraphFieldSpatial(fName, field) if initial: aux_init_data = np.ndarray( shape=(round((time_to - time_from) / time_step + 1), len(tmp)), dtype=object ) initial = False aux_init_data[i, :] = tmp[:] i = i + 1 except ValueError as e: print "ERROR: ", e except ConfigParser.NoOptionError: print "ERROR: Error in ", variable, ". Tridimensional spatial population must have time_from and time_to configurations." if popType == "statistical": if fileName.count("[P]") != 1 or fileName.count("[T]") != 1: print "ERROR: Statistical population for tridimensional variable " + variable + ' must have "[P]" and "[T]" wildcards. E.g: ./output_[P]/result[T].dot' try: stat_from = eval(parameters.get(variable, "stat_from")) stat_to = eval(parameters.get(variable, "stat_to")) stat_step = 1 if parameters.has_option(variable, "stat_step"): stat_step = eval(parameters.get(variable, "stat_step")) time_from = eval(parameters.get(variable, "time_from")) time_to = eval(parameters.get(variable, "time_to")) time_step = 1 if parameters.has_option(variable, "time_step"): time_step = eval(parameters.get(variable, "time_step")) initial = True i = 0 for index_t in u.frange(time_from, time_to + time_step, time_step): j = 0 for index_s in u.frange(stat_from, stat_to + stat_step, stat_step): fName = fileName.replace("[P]", str(index_s)).replace("[T]", str(index_t)) g = readGraphField(fName, field, fieldType) if initial: aux_init_data = np.ndarray( shape=( round((time_to - time_from) / time_step + 1), len(g), round((stat_to - stat_from) / stat_step + 1), ), dtype="float", ) initial = False aux_init_data[i, :, j] = g[:] j = j + 1 i = i + 1 except ValueError as e: print "ERROR: ", e except ConfigParser.NoOptionError: print "ERROR: Error in ", variable, ". Tridimensional statistical population must have time_from, time_to, stat_from and stat_to configurations." if dims == 4: if popType == "spatial": if fileName.count("[T]") != 1 or fileName.count("[G]") != 1: print "ERROR: Spatial population for 4-dimensional variable " + variable + ' must have "[T]" and "[G]" wildcards. E.g: ./output_[T]_[G]/result.dot' aux_init_data = [] if fieldType == "edge": print "ERROR: Edge, " + field + ", population cannot be spatial." try: time_from = eval(parameters.get(variable, "time_from")) time_to = eval(parameters.get(variable, "time_to")) time_step = 1 if parameters.has_option(variable, "time_step"): time_step = eval(parameters.get(variable, "time_step")) group_from = eval(parameters.get(variable, "group_from")) group_to = eval(parameters.get(variable, "group_to")) group_step = 1 if parameters.has_option(variable, "group_step"): group_step = eval(parameters.get(variable, "group_step")) initial = True i = 0 for index_g in u.frange(group_from, group_to + group_step, group_step): j = 0 for index_t in u.frange(time_from, time_to + time_step, time_step): fName = fileName.replace("[T]", str(index_t)).replace("[G]", str(index_g)) tmp = readGraphFieldSpatial(fName, field) if initial: aux_init_data = np.ndarray( shape=( round((group_to - group_from) / group_step + 1), round((time_to - time_from) / time_step + 1), len(tmp), ), dtype=object, ) initial = False aux_init_data[i, j, :] = tmp[:] j = j + 1 i = i + 1 except ValueError as e: print "ERROR: ", e except ConfigParser.NoOptionError: print "ERROR: Error in ", variable, ". 4-dimensional spatial population must have group_from, group_to, time_from and time_to configurations." if popType == "statistical": if fileName.count("[P]") != 1 or fileName.count("[T]") != 1 or fileName.count("[G]") != 1: print "ERROR: Statistical population for 4-dimensional variable " + variable + ' must have "[P]" and "[T]" and "[G]" wildcards. E.g: ./output_[P]_[G]/result[T].dot' try: stat_from = eval(parameters.get(variable, "stat_from")) stat_to = eval(parameters.get(variable, "stat_to")) stat_step = 1 if parameters.has_option(variable, "stat_step"): stat_step = eval(parameters.get(variable, "stat_step")) time_from = eval(parameters.get(variable, "time_from")) time_to = eval(parameters.get(variable, "time_to")) time_step = 1 if parameters.has_option(variable, "time_step"): time_step = eval(parameters.get(variable, "time_step")) group_from = eval(parameters.get(variable, "group_from")) group_to = eval(parameters.get(variable, "group_to")) group_step = 1 if parameters.has_option(variable, "group_step"): group_step = eval(parameters.get(variable, "group_step")) initial = True i = 0 for index_g in u.frange(group_from, group_to + group_step, group_step): j = 0 for index_t in u.frange(time_from, time_to + time_step, time_step): k = 0 for index_s in u.frange(stat_from, stat_to + stat_step, stat_step): fName = ( fileName.replace("[P]", str(index_s)) .replace("[T]", str(index_t)) .replace("[G]", str(index_g)) ) g = readGraphField(fName, field, fieldType) if initial: aux_init_data = np.ndarray( shape=( round((group_to - group_from) / group_step + 1), round((time_to - time_from) / time_step + 1), var.shape[0], round((stat_to - stat_from) / stat_step + 1), ), dtype="float", ) initial = False aux_init_data[i, j, :, k] = g[:] k = k + 1 j = j + 1 i = i + 1 except ValueError as e: print "ERROR: ", e except ConfigParser.NoOptionError: print "ERROR: Error in ", variable, ". Tridimensional statistical population must have group_from, group_to, time_from, time_to, stat_from and stat_to configurations." return aux_init_data except Exception as e: print "ERROR: ", e
def test1(): for f in utils.frange(1, 2, 0.16): print f for f in utils.frange(3, 2, -0.16): print f
if __name__ == "__main__": # sim = PIController(0.01, 0.01) # sim.run() results = defaultdict(dict) pg_args = (0.0, 1.0, 0.05) ig_args = (-1.0, 1.0, 0.1) total_sims = ((pg_args[1] - pg_args[0]) / pg_args[2]) * ( (ig_args[1] - ig_args[0]) / ig_args[2]) print("Doing {} simulations...".format(total_sims)) sims = 0 for proportional_gain in frange(*pg_args): for integral_gain in frange(*ig_args): sim = PIController(proportional_gain, integral_gain, verbose=False) results[proportional_gain][integral_gain] = sim.run() sims += 1 if sims % 100 == 0: print("Done {} of {} ({} %)".format( sims, total_sims, round(100 * sims / total_sims, 0))) min_ticks = 1E6 min_pg = 0 min_ig = 0 for proportional_gain in results: for integral_gain in results[proportional_gain]: ticks = results[proportional_gain][integral_gain] if ticks < min_ticks:
def simul(isodir): """ Read in parameters, data and isochrones. Create callback functions for the optimization routine, one of which will return the log(likelihood) and the other of which will print the best-fit parameter values. Having done this, call the optimization routine to minimize log(L). """ log_tmax = math.log10(13.7e9) params=parameters() eps=0.01 #feh params.set("m0y" ,1.0, 0,2.5,True) params.set("m0cphi",-0.001, -pi/2+eps,0,True) params.set("m0cr" ,0.01, 0,2,True) params.set("m1y" ,-2.5, -2.5,0.9,True) params.set("m1cphi",1.67, pi/2+eps,pi,True) params.set("m1cr" ,1.06, 0,2,True) params.set("sigma" ,0.2, 0,1, True) params.set("dsigmadlogt" ,0.2, -1,1, True) #sfr params.set("s0x" ,8.0, 8.0,9.0,True) params.set("s0y" ,0.5, 0.0,1,True) params.set("s0tx" ,0.1, 0.,1.,True) params.set("s0ty" ,0.1, 0,1,True) params.set("s1tx" ,0.1, 0.,1.,True) params.set("s1ty" ,0.1, -1,1,True) params.set("s1x" ,0.5, 0,1,True) params.set("s1y" ,1.0, 0.0,1.0,False) params.set("s2x" ,log_tmax, 9.5,10.25,True) params.set("s2y" ,0.1, 0,1.0,True) params.set("s2tx" ,0.1, 0.,1.,True) params.set("s2ty" ,0.1, 0.,1.,True) if len(sys.argv) == 2: if sys.argv[1] == "start": #run with a param to start from the beginning params.save() params.load() if not params.pars.has_key('dsigmadlogt'): params.set('dsigmadlogt',0.,0,False) if not params.pars.has_key('dsigmadlogs'): # Hook for SFR-depenedent spread; not fully implemented params.set('dsigmadlogs',0.,0,False) if len(sys.argv) == 2: if sys.argv[1] == "nudge": #Tweak the values near their limits print "Nudging parameters near the limits" p1 = params.getl() utils.nudge(params) p2 = params.getl() for pp1,pp2 in zip(p1,p2): if pp1[1] != pp2[1]: print "%s %.8f -> %.8f" % (pp1[0],pp1[1],pp2[1]) data=iso.readfits(isodir+"/datarr.fits") isos = iso.readisos(isodir) t=utils.frange(8,log_tmax,0.001) def f(par): params.setvalues(par) p = params w=utils.calculateweights(t,sfr(t,params)) # isow=iso.getisosweights(w,10.**t,metallicity(t,params),isos) if p.sigma > 0.: if p.dsigmadlogt == 0.: isow=iso.getisosweights_gauss(w,10.**t,metallicity(t,p),isos,p.sigma) if p.dsigmadlogt != 0.: # print "Gaussian sigma, ds/dlogt ",p.sigma,p.dsigmadlogt isow=iso.getisosweights_vgauss(w,10.**t,metallicity(t,p),isos,p.sigma,p.dsigmadlogt) if p.dsigmadlogs != 0.: # Hook for SFR-depenedent spread; not fully implemented isow=iso.getisosweights_sgauss(w,10.**t,sfr(t,params),metallicity(t,p), isos,p.sigma,p.dsigmadlogs) else: isow=iso.getisosweights(w,10.**t,metallicity(t,p),isos) m=iso.computeCMD(isow,isos) m=utils.normalize(m,sum(data.flat)) return utils.loglikelihood(m,data) d = numarray.maximum(data,1e-20) llhC=sum( (d*numarray.log(d)).flat ) def b(par,value,iter): params.setvalues(par) params.save() print "henry:",value,"tom:",2.0*(value+llhC),"iter:",iter,time.ctime() sys.stdout.flush() optimization.minmax(optimization.fmin_simplex,f, params.getvalues(),params.min(),params.max(),b)
def buildMappedMultidimEPD( samples, sig_pvals, bkg_pvals, nbins = 30, nbins1D = 40, cuts = None, name='NDimEPD' ): from ROOT import TObjArray from ROOT import Array2D from ROOT import Array1D treeList = TObjArray( len( samples ) ) infoList = TObjArray( len( samples ) ) binning = Array2D( len(bkg_pvals), Array1D(nbins,0) ) varListString = '' sigme = '+'.join( ['%s'%(me) for me in sig_pvals] ) alpha = { } for ipval, pval in enumerate( bkg_pvals ): nonill = '(' + '&&'.join( ['(%s>0)'%(me) for me in sig_pvals ] ) + '&& (%s>0)'%(pval) + ')' samples.draw( 'alpha:'+pval, '', 'log((%s)/(%s))/log(10)'%(sigme,pval), 1000, utils.frange(-100,100,ndiv=1000), cuts = nonill ) hSumBkg = None for ismpl, smpl in enumerate( samples ): if smpl.get_stype() != 'bkg': continue if hSumBkg==None: hSumBkg = smpl['alpha:'+pval]( 'SumBkgAlpha' ) else: hSumBkg += smpl['alpha:'+pval] il, ir = -1, -1 for ib in xrange( 1, hSumBkg.GetNbinsX() + 2 ): if il < 0 and hSumBkg.GetBinContent(ib) > 0: il = ib if il > 0 and hSumBkg.GetBinContent(ib) > 0: ir = ib xlow, xhigh = hSumBkg.GetBinLowEdge(il-1), hSumBkg.GetBinLowEdge(ir+1) print xlow, xhigh formString = 'log( (%s) / (%s) )/log(10)'%( sigme, pval ) ## @FIXME what about nil p-values ?? for ibin in xrange( nbins + 1 ): binning[ipval][ibin] = ( (xhigh-xlow) / nbins * ibin + xlow ) hSumBkg.Delete() if len( varListString ) == '': varListString = formString else: varListString = ':'.join( (varListString, formString) ) if cuts == None or len(cuts) == 0: cuts = '0<1' for ismpl, smpl in enumerate( samples ): treeList.AddLast( smpl.get_tree() ) struct = ROOT.TSampleStruct( ) struct.Variable = varListString struct.Name = smpl.get_name() struct.Weight = '(%s * %0.5g * %0.5g)'%( smpl.get_weights(), smpl.get_scale(), abs(smpl.get_lumi()) ) struct.Selection = '( %s ) && ( %s )'%( cuts, smpl.get_cuts() ) struct.Type = smpl.get_stype() infoList.AddLast( struct ) mappedNDList = ROOT.getMappedNDim( treeList, infoList, binning, "ND" ) hists1D = [] for ismpl, smpl in enumerate( samples ): hists1D += [ ROOT.get1DMapped( mappedNDList[ismpl], len(varListString.split(':')), nbins, nbins1D ) ] smpl[name+'Mapped'] = histogram.pyhist1F( hists1D[-1] ) smpl.set_style() return mappedNDList
def test1(): for f in utils.frange(1,2,0.16): print f for f in utils.frange(3,2,-0.16): print f