def __init__(self, sam_file): self.header = '' self.references = [] self.lengths = [] self.read = ntup('blast', 'pos pnext rname rnext is_reverse mate_is_reverse is_read1 is_unmapped mate_is_unmapped line') self.sam = open(sam_file) line = self.sam.readline() lastline = None while line.startswith('@'): self.header += line if line.startswith('@SQ'): for i in line.split(): if i.startswith('SN:'): self.references.append(i[3:]) elif i.startswith('LN:'): self.lengths.append(int(i[3:])) lastline = line line = self.sam.readline() self.sam.seek(0) if not lastline is None: getit = True while getit: line = self.sam.readline() if line == lastline: getit = False
class Symbol(ntup('Symbol', 'name, value, section')): ''' A Symbol has a name, value, section and possibly other toolchain-specific information. The section is a toolchain-specific value and may be `None` if the toolchain does not support sections or an equivalent concept. ''' pass
class Area(ntup('Area', 'number, name, flags')): def isrelative(self): ''' Whether this is a relative or absolute area. Flags bit 3 is set for absolute, clear for relative. ''' return (self.flags & 0x08) == 0
def seeker_with_max_experiment(dimensions=4, rollouts=10, maxsamples=10000, allowederror=0.1, covariance_decay=0.992): # Per-experiment local variables: convergefailures = 0 convergesuccesses = 0 convergetotaltrials = 0 targets = [0] * rollouts convergestepss = [0] * rollouts convergesigmas = [0] * rollouts convergedistances = [0] * rollouts convergemaxdistances = [0] * rollouts success_p = [False] * rollouts for rollout in range(rollouts): # Choose a random ground-truth uniformly inside the d-ball. seek_target = get_target(dimensions) count = 0 yp = np.zeros(dimensions) # first guess is the origin maxdist = -1000000 totalbigdist = 0 totalsmldist = 0 dimdist = D_BALL_RADIUS ## usually 1.0 sigma = (dimdist**2) sigma_0 = sigma calcdistance = 0 while (distance.euclidean(seek_target, yp) > \ allowederror) and (count < maxsamples): # Generate new guesses centered at the current best guess yp. yp, calcdistance = sample_a_b(dimensions, yp, sigma, seek_target) count += 1 sigma *= covariance_decay # Maintain statistics for this rollout. if calcdistance < D_BALL_RADIUS: totalsmldist += 1 else: totalbigdist += 1 maxdist = max(maxdist, calcdistance) if count < maxsamples: success_p[rollout] = True targets[rollout] = seek_target convergestepss[rollout] = count convergesigmas[rollout] = sigma convergedistances[rollout] = calcdistance convergemaxdistances[rollout] = maxdist if (count >= maxsamples): convergefailures += 1 else: convergesuccesses += 1 convergetotaltrials += count vseeker = ntup('VogelsongSeeker', [ 'rollouts', 'failures', 'successes', 'targets', 'mean_trials_per_success', 'percentage_successful', 'big_distance_count', 'small_distance_count', 'original_sigma', 'covariance_decay_per_trial', 'convergestepss', 'convergesigmas', 'convergedistances', 'convergemaxdistances', 'success_flags' ]) average_trials_per_success = ((convergetotaltrials * 1.0 / convergesuccesses) if convergesuccesses > 0 else 0) percentage_successful = (100.0 * (rollouts - convergefailures) / rollouts) result = vseeker(rollouts, convergefailures, convergesuccesses, targets, average_trials_per_success, percentage_successful, totalbigdist, totalsmldist, sigma_0, covariance_decay, convergestepss, convergesigmas, convergedistances, convergemaxdistances, success_p) return result
class Record(ntup('Record', 'header, section, gran, addr, length data')): ''' A data record from a code file. This is used for both
class MemImage(list): ''' A memory image, usually loaded from an assembler or linker output file, consisting of an entrypoint and a list of `MemRecord` or similar data records, each with the memory address at which it starts and the data. The data records may contain additional information, but iterating over the sequence will always return ``(addr,data)`` tuples. This is a mutable ordered collection. The records are not necessarily in order of address, but `sorted()` will return the records ordered by address. ''' def __init__(self): self.entrypoint = None self.clear_cache() def clear_cache(self): ' Clear any cached values that have been calculated. ' self.startaddr = None self.endaddr = None # one past the last address with data self.contig_fill = None self.contig_data = None class OverlapError(ValueError): pass MemRecord = ntup('MemRecord', 'addr data') MemRecord.__docs__ = \ ''' A memory record, with an int starting address and sequence of byte values that start at that address. ''' def addrec(self, addr, data): self.clear_cache() self.append(MemImage.MemRecord(addr, data)) def __iter__(self): return (self.MemRecord(mr.addr, mr.data) for mr in super().__iter__()) def contiglen(self): ''' Return the number of bytes in this image covers from the lowest to highest address. This is the number of bytes that will be returned by `contigbytes()`. This does not check to see if the image has overlapping records. ''' if self.startaddr is not None and self.endaddr is not None: return self.endaddr - self.startaddr endaddr = None for mr in sorted(self): if len(mr.data) == 0: continue # Ignore empty records if self.startaddr is None: self.startaddr = mr.addr recend = mr.addr + len(mr.data) if endaddr is None or endaddr < recend: endaddr = recend self.endaddr = endaddr return self.contiglen() def contigbytes(self, fill=0xFF): ''' Return the binary contents of this image as a contiguous list of bytes from the lowest address to the highest, filling in unset areas with `fill`. If the image has any overlapping records a `MemOverlap` exception will be raised. (Possibly we should add a parameter to disable this check.) ''' if fill == self.contig_fill and self.contig_data is not None: return self.contig_data self.contig_fill = fill data = [None] * self.contiglen() for mr in self: start = mr.addr - self.startaddr sl = slice(start, start + len(mr.data)) for pos, val in enumerate(data[sl], mr.addr): if val is not None: raise self.OverlapError( 'Data overlap at location ${:04X}'.format(pos)) data[sl] = list(mr.data) self.contig_data = bytes(map(lambda x: fill if x is None else x, data)) return self.contig_data
# + 1.80146 E ^ (-10 t ^ 2) r00 = 1.80146 * np.exp(-10. * ((t - 0.)**2)) return 0 return r15 + r13 + r12 + r10 + r08 + r07 + r06 + r04 + r03 + r02 + r00 def random_disturbance_funcs(amplitude): return [ lambda _state, t: ((2 * amplitude * rnd.rand()) - amplitude) * np.exp( (-10 * (t - rnd.randint(0, 16))**2)) for _ in range(20) ] sim_constants_ntup = ntup('SimConstants', [ 'seed', 'state_dimensions', 'action_dimensions', 'duration_second', 'steps_per_second', 'delta_time', 'action_force_multiplier' ]) search_constants_ntup = ntup('SearchConstants', ['radius', 'decay']) command_screen_constants_ntup = ntup('CommandScreenConstants', ['width', 'height']) class GameState(object): """There is only one such object. Either pack up all the globals and put them in here or unpack this into globals to get rid of one level of indirection. TODO: Reconcile this singleton class with global variables.""" def __init__( self,