def astar_search(graph: Graph): open_nodes = bintrees.AVLTree() fg_values = bintrees.AVLTree() # Create the start and end node end_node = NodeInGraph(graph.end_config, None) start_node = NodeInGraph(graph.start_config, None) start_node.g = 0 start_node.h = start_node.compute_heuristic(end_node) start_node.f = start_node.g + start_node.h open_nodes[start_node] = 0 # start_node's g value fg_values[start_node.state] = (start_node.f, start_node.g) max_states = 0 while len(open_nodes) > 0: max_states = max(max_states, open_nodes.__len__()) current_node = open_nodes.pop_min()[0] # Check if we reached the goal and return the path if current_node is None: print("Error: current node is None.") exit(0) if current_node.state == end_node.state: print("Total number of states ever visited: " + str(fg_values.__len__())) print( "Maximum number of states in the open set at a certain point: " + str(max_states)) return current_node.get_path() # Get neighbours neighbours = graph.expand(current_node) for next_node in neighbours: next_node.g = current_node.g + 1 next_node.h = next_node.compute_heuristic(end_node) next_node.f = next_node.g + next_node.h if fg_values.__contains__(next_node.state) == False: fg_values[next_node.state] = (next_node.f, next_node.g ) # Add it to fg_values open_nodes[next_node] = next_node.g # Add it to the open set elif next_node.g < fg_values[next_node.state][1]: # Make an artificial node to see if the current state exists in the open set # We don't want the same state to exist in the open set with different f values # We only want to keep the instance with the smallest f value aux_node = NodeInGraph(next_node.state, None) aux_node.f = fg_values[next_node.state][0] fg_values[next_node.state] = (next_node.f, next_node.g) if open_nodes.__contains__(aux_node) == False: open_nodes[next_node] = next_node.g else: open_nodes.remove(aux_node) open_nodes[next_node] = next_node.g return None
def astar_search(graph : Graph, heuristic, file_descriptor): open_nodes = bintrees.AVLTree() fg_values = bintrees.AVLTree() # Create the start and end node end_node = NodeInGraph(graph.end_config, None) start_node = NodeInGraph(graph.start_config, None) start_node.g = 0 start_node.h = heuristic(start_node) start_node.f = start_node.g + start_node.h open_nodes[start_node] = 0 # start node's g value fg_values[start_node.state] = (start_node.f, start_node.g) max_states = 0 while len(open_nodes) > 0: max_states = max(max_states, len(open_nodes)) current_node = open_nodes.pop_min()[0] # Check for the node being None if current_node is None: print('Error: current node is None.') sys.exit(0) # Check if we reached the goal state and return the path if current_node.state == end_node.state: file_descriptor.write('Total number of states ever visited: ' + str(len(fg_values)) + '\n') file_descriptor.write('Maximum number of states in the open set at any certain point: ' + str(max_states) + '\n') file_descriptor.write('Moves used: ' + str(current_node.moves_used) + '\n') return current_node.get_path() # Get neighbours neighbours = graph.expand(current_node) for next_node in neighbours: next_node.g = current_node.g + 1 next_node.h = heuristic(next_node) next_node.f = next_node.g + next_node.h if fg_values.__contains__(next_node.state) == False: fg_values[next_node.state] = (next_node.f, next_node.g) # Add it to fg_values open_nodes[next_node] = next_node.g # Add it to the open set elif next_node.g < fg_values[next_node.state][1]: # Make an artificial node to see if the current state exists in the open set # We don't want the same state to exist in the open set with different f values # We only want to keep the instance with the smallest f value aux_node = NodeInGraph(next_node.state, None) aux_node.f = fg_values[next_node.state][0] fg_values[next_node.state] = (next_node.f, next_node.g) if open_nodes.__contains__(aux_node) == False: open_nodes[next_node] = next_node.g else: open_nodes.remove(aux_node) open_nodes[next_node] = next_node.g file_descriptor.write('No solutions found. Total number of states is ' + str(max_states) + '.\n') return None
def __init__(self): self.nodes = {} self.roots = bintrees.AVLTree() # root indexes self.rec = bintrees.AVLTree() # arg rec parents nodes self.coal = bintrees.AVLTree() # arg CA parent node self.num_ancestral_recomb = 0 self.num_nonancestral_recomb = 0 self.branch_length = 0 self.nextname = 1 # next node index self.available_names = SortedSet()
def men(): points = [(0,0),(1,2),(2,2),(3,0),(1,3)] #points = [(0,0),(2,0),(1,1)] H1 = bintrees.AVLTree() H2 = bintrees.AVLTree() for p in points: addpoint(p,H1,H2) print "->",H1 print H2 print "-------------------------------"
def clean(self): """ Borra todos los elementos de la gráfica. Lineas y circulos """ self.points = None self.H1 = bintrees.AVLTree() self.H2 = bintrees.AVLTree() self.ax.cla() self.ax.autoscale(False) self.ax.set_xlim(-20,20) self.ax.set_ylim(-20,20) self.fig.canvas.draw()
def __init__(self): self.fig = plt.figure() self.ax = self.fig.add_subplot(111, aspect=1) self.ax.autoscale(False) self.ax.set_xlim(-20,20) self.ax.set_ylim(-20,20) self.H1 = bintrees.AVLTree() self.H2 = bintrees.AVLTree() self.points = None self.newpoint = None # Controlador de Eventos self.cid_release_button = self.fig.canvas.mpl_connect('button_release_event', self.on_release)
def __init__(self, data1, data2, *args, **kwargs): super(InterpolatedDataLogger, self).__init__(*args, **kwargs) (self._event1, self._field1) = data1 (self._event2, self._field2) = data2 self._x_timestamps = [] self._x_values = [] self._x_needs_interpolation = dict() self._x_received_values = bintrees.AVLTree() self._y_timestamps = [] self._y_values = [] self._y_needs_interpolation = dict() self._y_received_values = bintrees.AVLTree()
def __init__(self, n, m, r, max_segments=100): self.n = n self.m = m self.r = r self.max_segments = max_segments self.segment_stack = [] self.segments = [None for j in range(self.max_segments + 1)] for j in range(self.max_segments): s = Segment(j + 1) self.segments[j + 1] = s self.segment_stack.append(s) # We'd like to use an AVLTree here for P but the API doesn't quite # do what we need. Lists are inefficient here and should not be # used in a real implementation. self.P = [None for j in range(n)] self.C = [] self.L = FenwickTree(self.max_segments) self.S = bintrees.AVLTree() for j in range(n): x = self.alloc_segment(0, m, j + 1) self.L.set_value(x.index, m - 1) self.P[j] = x self.S[0] = n self.S[m] = -1 self.t = 0 self.w = n + 1 self.num_ca_events = 0 self.num_re_events = 0
def __init__(self): self.left = None self.right = None self.node = None self.prev = None self.next = None self.samples = bintrees.AVLTree()
def load_binary_tree(): tree = bintrees.AVLTree() for i in range(MAX_LOAD): tree.insert(i, True) return tree
def __init__(self, ts_full): self.ts_full = ts_full self.tables = ts_full.tables self.edges_dict = collections.defaultdict(list) for edge in self.tables.edges: self.edges_dict[edge.parent].append(edge) self.n = ts_full.sample_size self.seq_length = ts_full.sequence_length self.mutation_map = self.map_mutation( ) #[[SNP]] where index is ts_node # print("selfmutations", self.mutation_map) self.arg = argbook.ARG() self.arg.nextname = self.n self.parent_nodes = collections.defaultdict(list) for k in range(self.n): node = self.arg.alloc_node(k, 0) #index, time, samples = bintrees.AVLTree() samples.__setitem__(k, k) s = self.arg.alloc_segment(0, math.ceil(self.seq_length), node, samples) node.first_segment = s self.add_mutation(node) node = self.arg.add(node) x = self.arg.alloc_segment(0, math.ceil(self.seq_length), node, samples) self.parent_nodes[k] = x
def _loadFromFile(self): if os.path.isfile(self.filename): with open(self.filename, 'rb') as f: self.tree = pickle.load(f) else: # print("Loading Empty Tree") self.tree = bintrees.AVLTree()
def verify(self): """ Checks that the state of the simulator is consistent. """ q = 0 for pop_index, pop in enumerate(self.P): for u in pop: assert u.prev is None left = u.left right = u.left while u is not None: assert u.population == pop_index assert u.left <= u.right if u.prev is not None: s = u.right - u.prev.right else: s = u.right - u.left - 1 if self.model != 'dtwf': assert s == self.L.get_frequency(u.index) right = u.right v = u.next if v is not None: assert v.prev == u if u.right > v.left: print("ERROR", u, v) assert u.right <= v.left u = v q += right - left - 1 if self.model != 'dtwf': assert q == self.L.get_total() assert self.S[self.m] == -1 # Check the ancestry tracking. A = bintrees.AVLTree() A[0] = 0 A[self.m] = -1 for pop in self.P: for u in pop: while u is not None: if u.left not in A: k = A.floor_key(u.left) A[u.left] = A[k] if u.right not in A: k = A.floor_key(u.right) A[u.right] = A[k] k = u.left while k < u.right: A[k] += 1 k = A.succ_key(k) u = u.next # Now, defrag A j = 0 k = 0 while k < self.m: k = A.succ_key(j) if A[j] == A[k]: del A[k] else: j = k assert list(A.items()) == list(self.S.items())
def get_seg_variants(self, data): '''TODO: implement efficiently''' assert self is not None seg_variants = bintrees.AVLTree() for item in data.keys(): if self.contains(item): seg_variants[item] = item return seg_variants
def get_variants(self, data): '''get all snps of data that are in self TODO: find an efficient way ''' seg_variants = bintrees.AVLTree() for item in data.keys(): if self.contains(item): seg_variants[item] = item return seg_variants
def __init__(self, index): self.left_child = None self.right_child = None self.left_parent = None self.right_parent = None self.first_segment = None self.snps = bintrees.AVLTree() self.time = None self.breakpoint = None self.index = index
def solve1(x): ordered = bintrees.AVLTree() res = 0 for k in range(len(x)): xk, hk = x[k] to_add = True x1 = xk for x0, h0 in ordered.item_slice(0, xk, reverse=True): if covers(x0, h0, xk, hk): to_add = False break elif covers(xk, hk, x0, h0): x1 = x0 pass else: break x2 = xk for x0, h0 in ordered[xk:].items(): if covers(x0, h0, xk, hk): to_add = False break elif covers(xk, hk, x0, h0): x2 = x0 pass else: break del ordered[x1:x2+1] if to_add: ordered.insert(xk, hk) x_cur, h_cur = ordered.min_item() square = h_cur * h_cur * 0.5 x_last, h_last = x_cur, h_cur for x_cur, h_cur in ordered[x_last+1:].items(): dx = x_cur - x_last if dx >= h_cur + h_last: square += (h_cur * h_cur + h_last * h_last) * 0.5 else: h0 = min(h_cur, h_last) dh = abs(h_cur - h_last) square += (h_cur + h_last) * 0.5 * dh square += (2 * h0 - (dx - dh)*0.5) * (dx - dh) * 0.5 x_last, h_last = x_cur, h_cur square += h_cur * h_cur * 0.5 res += square return res
def get_variants(self, data): '''get all snps in data lay in the self segments TODO: an efficient way it is not efficient ot loop over data SNPs for each segment ''' node_variants = bintrees.AVLTree() seg = self.first_segment while seg is not None: for item in data.keys(): if seg.contains(item): node_variants[item] = item seg = seg.next return node_variants
def dtwf_generation(self): """ Evolves one generation of a Wright Fisher population """ for pop_idx, pop in enumerate(self.P): ## Cluster haploid inds by parent cur_inds = pop.get_ind_range(self.t) offspring = bintrees.AVLTree() for i in range(pop.get_num_ancestors() - 1, -1, -1): ## Popping every ancestor every generation is inefficient. ## In the C implementation we store a pointer to the ## ancestor so we can pop only if we need to merge anc = pop.remove(i) parent = np.random.choice(cur_inds) if parent not in offspring: offspring[parent] = [] offspring[parent].append(anc) ## Draw recombinations in children and sort segments by ## inheritance direction for children in offspring.values(): H = [[], []] for child in children: segs_pair = self.dtwf_recombine(child) ## Collect segments inherited from the same individual for i, seg in enumerate(segs_pair): if seg is None: continue assert seg.prev is None heapq.heappush(H[i], (seg.left, seg)) ## Merge segments for h in H: self.merge_ancestors(h, pop_idx) ## Migration events happen at the rates in the matrix. for j in range(len(self.P)): source_size = self.P[j].get_num_ancestors() for k in range(len(self.P)): if j == k: continue mig_rate = source_size * self.migration_matrix[j][k] num_migs = min(source_size, np.random.poisson(mig_rate)) for _ in range(num_migs): mig_source = j mig_dest = k self.migration_event(mig_source, mig_dest)
def alloc_segment(self, left=None, right=None, node=None, samples=bintrees.AVLTree(), prev=None, next=None): """ alloc a new segment """ s = Segment() s.left = left s.right = right s.node = node s.samples = samples s.next = next s.prev = prev return s
def alloc_node(self, index=None, time=None, left_child=None, right_child=None): """ alloc a new Node """ node = Node(index) node.time = time node.first_segment = None node.left_child = left_child node.right_child = right_child node.left_parent = None node.right_parent = None node.breakpoint = None node.snps = bintrees.AVLTree() return node
def stack_dcm_files(dic): # the dictionary like '/home/zhoul0a/CT_slices_for_patient_alice/' # return a 3D np array with shape [Rows, Columns, Num_Slices], and the resolution of each axis: (0.625, 0.625, 0.9) dcm_file_names = os.listdir(dic) num_slices = len(dcm_file_names) first_slice = load_dicom(dic+dcm_file_names[0])[0] first_content = pydicom.read_file(dic+dcm_file_names[0]) resolutions = first_content.PixelSpacing resolutions.append(first_content.SliceThickness) print('the resolution for x, y, z in mm:', resolutions) rows, columns = first_slice.shape tree_instance = bintrees.AVLTree() array_3d = np.zeros([rows, columns, num_slices], 'int32') for file in dcm_file_names: data_array, slice_id = load_dicom(dic+file) assert not tree_instance.__contains__(slice_id) tree_instance.insert(slice_id, slice_id) array_3d[:, :, num_slices - slice_id] = data_array print('the array corresponds to a volume of:', rows*resolutions[0], columns*resolutions[1], num_slices*resolutions[2]) return array_3d, resolutions
def get_arg_genotype(ts_full): ''' get the genotypes from ts and implements a discretising function, which rounds upwards to the nearest int, and store in a dict where keys are SNP positions and values are the sequences for which the allele is derived. ANDing if the results if 2 or more variants end up at the same integer position. ''' simple_ts = ts_full.simplify() new_data = {} derived = 1 genotypes = None position = 0 for v in simple_ts.variants(): if int(math.ceil(v.position)) != position: genotypes = v.genotypes position = int(math.ceil(v.position)) else: raise NameError("Not two SNPs can have identical genomic position") # genotypes = np.logical_and(genotypes, v.genotypes) b = bintrees.AVLTree() b.update({k: k for k in list(np.where(genotypes == derived)[0])}) new_data[math.ceil(position)] = b return new_data
def audio_to_words(filename, word_confidence_threshold=0.98, speaker_confidence_threshold=0.3): transcript = _watson_transcript(filename) speakers = bintrees.AVLTree( {v['from']: v for v in transcript['speaker_labels']}) for r in transcript['results']: timestamps = r['alternatives'][0]['timestamps'] confidences = r['alternatives'][0]['word_confidence'] for (w, ta, tb), (w_, c) in zip(timestamps, confidences): assert w == w_ from_ts = _float_to_ts(ta) to_ts = _float_to_ts(tb) speaker_label = speakers.floor_item(ta)[1] if _float_to_ts(speaker_label['to']) < from_ts: speaker_confidence = 0 speaker = -1 else: speaker = speaker_label['speaker'] speaker_confidence = speaker_label['confidence'] if c > word_confidence_threshold and speaker_confidence > speaker_confidence_threshold: yield w.lower().strip( '.'), from_ts, to_ts, speaker, speaker_confidence
def __init__(self, sample_size, num_loci, recombination_rate, migration_matrix, sample_configuration, population_growth_rates, population_sizes, population_growth_rate_changes, population_size_changes, migration_matrix_element_changes, bottlenecks, max_segments=100): # Must be a square matrix. N = len(migration_matrix) assert len(sample_configuration) == N assert len(population_growth_rates) == N assert len(population_sizes) == N for j in range(N): assert N == len(migration_matrix[j]) assert migration_matrix[j][j] == 0 assert sum(sample_configuration) == sample_size self.n = sample_size self.m = num_loci self.r = recombination_rate self.migration_matrix = migration_matrix self.max_segments = max_segments self.segment_stack = [] self.segments = [None for j in range(self.max_segments + 1)] for j in range(self.max_segments): s = Segment(j + 1) self.segments[j + 1] = s self.segment_stack.append(s) self.P = [Population(id_) for id_ in range(N)] self.C = [] self.L = FenwickTree(self.max_segments) self.S = bintrees.AVLTree() j = 0 for pop_index in range(N): sample_size = sample_configuration[pop_index] self.P[pop_index].set_start_size(population_sizes[pop_index]) self.P[pop_index].set_growth_rate( population_growth_rates[pop_index], 0) for k in range(sample_size): x = self.alloc_segment(0, self.m, j, pop_index) self.L.set_value(x.index, self.m - 1) self.P[pop_index].add(x) j += 1 self.S[0] = self.n self.S[self.m] = -1 self.t = 0 self.w = self.n self.num_ca_events = 0 self.num_re_events = 0 self.modifier_events = [(sys.float_info.max, None, None)] for time, pop_id, new_size in population_size_changes: self.modifier_events.append( (time, self.change_population_size, (int(pop_id), new_size))) for time, pop_id, new_rate in population_growth_rate_changes: self.modifier_events.append( (time, self.change_population_growth_rate, (int(pop_id), new_rate, time))) for time, pop_i, pop_j, new_rate in migration_matrix_element_changes: self.modifier_events.append( (time, self.change_migration_matrix_element, (int(pop_i), int(pop_j), new_rate))) for time, pop_id, intensity in bottlenecks: self.modifier_events.append( (time, self.bottleneck_event, (int(pop_id), intensity))) self.modifier_events.sort()
def _copy_args(self): return {'storage': bintrees.AVLTree(self._storage)}
def __init__(self, *args, **kwargs): storage = kwargs.pop("storage", None) super(TreePage, self).__init__(*args, **kwargs) self._storage = bintrees.AVLTree() if storage is None else storage
def __init__(self, backref, *args, **kwargs): self._backref = backref self._avltree = bintrees.AVLTree() super(FunctionDict, self).__init__(*args, **kwargs)
def __init__(self, root): # super init TODO self._tree = bintrees.AVLTree([(root.interval, root)])
def solve(x): ordered = bintrees.AVLTree() ordered[-MX] = (0,0) ordered[+MX] = (0,0) res = 0 square = 0 for k in range(len(x)): xk, hk = x[k] # print(xk, hk) to_add = True x1 = xk for x0, (h0, s0) in ordered.item_slice(0, xk, reverse=True): if covers(x0, h0, xk, hk): to_add = False break elif covers(xk, hk, x0, h0): x1 = x0 pass else: break x2 = xk for x0, (h0, s0) in ordered[xk:].items(): if covers(x0, h0, xk, hk): to_add = False break elif covers(xk, hk, x0, h0): x2 = x0 pass else: break if x2 == xk: x2 = ordered.ceiling_key(x2) else: x2 = ordered.succ_key(x2) print('range:', x1, x2) s = 0 for x0, (h0, s0) in ordered[x1:x2].items(): s += s0 square -= s del ordered[x1:x2] if to_add: ordered.insert(xk, (hk, 0)) x1 = ordered.floor_key(x1) x_last, (h_last, _) = ordered.prev_item(x1) for x_cur, (h_cur, s_cur) in ordered[x1:x2].items(): dx = x_cur - x_last s_new = shape_square(h_last, h_cur, dx) print('square: ', h_last, h_cur, dx, ':', s_new) square += -s_cur + s_new ordered[x_cur] = (h_cur, s_new) x_last, h_last = x_cur, h_cur print(square) print(ordered) res += square return res