def getNewAVL(seq): if has_fast_tree_support(): from bintrees import FastAVLTree return FastAVLTree(seq) else: from bintrees import AVLTree return AVLTree(seq)
def reset(self): """ Resets all info. :return: None """ self.clients = {} self.sorted_clients = FastAVLTree()
def __init__(self): # AVL trees are used as a main structure due its optimal performance features for this purpose self._participants = FastAVLTree() self._order_owners = FastAVLTree() # Assigning ID -> Owner self._asks = FastAVLTree() # MIN Heap self._bids = FastAVLTree() # MAX Heap self._price_ids = FastAVLTree() # Assigning ID -> Price self._total_ask_size = 0 # For monitoring purpose self._total_bid_size = 0 # For monitoring purpose self._last_order_id = 0 # Increases with each order processed self._cleared_orders_count = 0 # For monitoring purpose self._total_volume_trad = 0 # For monitoring purpose self._total_volume_pending = 0 # For monitoring purpose
def scan_1D(result, d, self_edges=False, weight=False, bound=None, handling=None, memory=False): # recover sim parameters N = len(result[0][0]) # "N" = N + 1 D = len(result[0]) P = len(result) G = [] # list of graphs # Each time for t in range(N): # copy previous matrix or start fresh as appropriate if t is 0 or memory is False: G.append(nx.Graph()) else: G.append(G[t - 1].copy()) G[t].add_nodes_from(range(P)) idx = FastAVLTree() # insert all points not out of bounds for i in range(P): I = result[i][0][t] if I < 999999999.0: idx.insert(I, i) for i in range(P): I = result[i][0][t] if I < 999999999.0: minimum = I - d maximum = I + d # get all results within range hits = [v for (k, v) in idx.item_slice(minimum, maximum)] if handling is "Torus" and maximum > bound: hits += [ v for (k, v) in idx.item_slice(0, maximum % bound) ] if handling is "Torus" and minimum < 0: hits += [ v for (k, v) in idx.item_slice(minimum % bound, bound) ] for j in hits: if self_edges or i != j: G[t].add_edge(i, j, weight=1) # add something to handle weight case here stdout.write(".") stdout.flush() print("") return G
def __init__(self): # Clients that have reported score # <key> :<value> -> <client_id> : <client> # # where: # # <client_id> (int) : Id that uniquelly specifies the client. # <client> (Client) : Information of the client (including score) # self.clients = {} # AVL tree in which each node is a list of Client instances (i.e., all the clients with the same score) # Allows to access sorted info in O(log(N)) self.sorted_clients = FastAVLTree()
def _submit_lmt(self, side, size, price, pi_d): """ Submits LMT order to book """ # Assign order ID order_id = self._get_order_id() # Pending volume monitoring self._total_volume_pending += size self._price_ids.insert(order_id, (price, side)) # Keep track of participant orders, book will be asked for sure if pi_d not in self._participants: self._participants.insert(pi_d, [order_id]) else: owner_trades = self._participants.get(pi_d, []) owner_trades.append(order_id) self._order_owners.insert(order_id, pi_d) # Assign to right (correct) side if side == 'ask': self._total_ask_size += size ask_level = self._asks.get(price, FastAVLTree()) ask_level.insert(order_id, size) if price not in self._asks: self._asks.insert(price, ask_level) else: # bid self._total_bid_size += size bid_level = self._bids.get(price, FastAVLTree()) bid_level.insert(order_id, size) if price not in self._bids: self._bids.insert(price, bid_level) return order_id
# n * log n solution from bintrees import FastAVLTree # read in the first line of input k, n = [int(x) for x in raw_input().split(' ')] # read in the second line of input A = [int(x) for x in raw_input().split(' ')] assert len(A) == n # check all the subsequences and keep the length best_sequence_length = 0 min_tree = FastAVLTree() max_tree = FastAVLTree() def ok(): if max_tree.is_empty(): return True else: difference = max_tree.max_item()[0][0] \ - min_tree.min_item()[0][0] return difference <= k start, end = 0, 0 while end < len(A): # extend the sequence until violation reached. while end < len(A) and ok():
def __init__(self, csvfile=None, parser=MaxMindGeoLiteCSVParser()): treelist = parser.parse(csvfile) self.tree = FastAVLTree(treelist) # Done with the list, remove it since it can be rather large del treelist
def __init__(self) -> None: self._tree = FastAVLTree( ) # Map ints to tuples (val, Union[end, InfinityMarker])
def get_ROIs(path, delta_mz=0.005, required_points=15, dropped_points=3, progress_callback=None): ''' :param path: path to mzml file :param delta_mz: :param required_points: :param dropped_points: can be zero points :param pbar: an pyQt5 progress bar to visualize :return: ROIs - a list of ROI objects found in current file ''' # read all scans in mzML file run = pymzml.run.Reader(path) scans = [] for scan in run: if scan.ms_level == 1: scans.append(scan) ROIs = [] # completed ROIs process_ROIs = FastAVLTree() # processed ROIs # initialize a processed data number = 1 # number of processed scan init_scan = scans[0] start_time = init_scan.scan_time[0] min_mz = max(init_scan.mz) max_mz = min(init_scan.mz) for mz, i in zip(init_scan.mz, init_scan.i): if i != 0: process_ROIs[mz] = ProcessROI([1, 1], [start_time, start_time], [i], [mz], mz) min_mz = min(min_mz, mz) max_mz = max(max_mz, mz) for scan in tqdm(scans): if number == 1: # already processed scan number += 1 continue # expand ROI for n, mz in enumerate(scan.mz): if scan.i[n] != 0: ceiling_mz, ceiling_item = None, None floor_mz, floor_item = None, None if mz < max_mz: _, ceiling_item = process_ROIs.ceiling_item(mz) ceiling_mz = ceiling_item.mzmean if mz > min_mz: _, floor_item = process_ROIs.floor_item(mz) floor_mz = floor_item.mzmean # choose closest if ceiling_mz is None and floor_mz is None: time = scan.scan_time[0] process_ROIs[mz] = ProcessROI([number, number], [time, time], [scan.i[n]], [mz], mz) continue elif ceiling_mz is None: closest_mz, closest_item = floor_mz, floor_item elif floor_mz is None: closest_mz, closest_item = ceiling_mz, ceiling_item else: if ceiling_mz - mz > mz - floor_mz: closest_mz, closest_item = floor_mz, floor_item else: closest_mz, closest_item = ceiling_mz, ceiling_item if abs(closest_item.mzmean - mz) < delta_mz: roi = closest_item if roi.scan[1] == number: # ROIs is already extended (two peaks in one mz window) roi.mzmean = (roi.mzmean * roi.points + mz) / (roi.points + 1) roi.points += 1 roi.mz[-1] = (roi.i[-1]*roi.mz[-1] + scan.i[n]*mz) / (roi.i[-1] + scan.i[n]) roi.i[-1] = (roi.i[-1] + scan.i[n]) else: roi.mzmean = (roi.mzmean * roi.points + mz) / (roi.points + 1) roi.points += 1 roi.mz.append(mz) roi.i.append(scan.i[n]) roi.scan[1] = number # show that we extended the roi roi.rt[1] = scan.scan_time[0] else: time = scan.scan_time[0] process_ROIs[mz] = ProcessROI([number, number], [time, time], [scan.i[n]], [mz], mz) # Check and cleanup to_delete = [] for mz, roi in process_ROIs.items(): if roi.scan[1] < number <= roi.scan[1] + dropped_points: # insert 'zero' in the end roi.mz.append(roi.mzmean) roi.i.append(0) elif roi.scan[1] != number: to_delete.append(mz) if roi.points >= required_points: ROIs.append(ROI( roi.scan, roi.rt, roi.i, roi.mz, roi.mzmean )) process_ROIs.remove_items(to_delete) try: min_mz, _ = process_ROIs.min_item() max_mz, _ = process_ROIs.max_item() except ValueError: min_mz = float('inf') max_mz = 0 number += 1 if progress_callback is not None and not number % 10: progress_callback.emit(int(number * 100 / len(scans))) # add final rois for mz, roi in process_ROIs.items(): if roi.points >= required_points: for n in range(dropped_points - (number - 1 - roi.scan[1])): # insert 'zero' in the end roi.mz.append(roi.mzmean) roi.i.append(0) ROIs.append(ROI( roi.scan, roi.rt, roi.i, roi.mz, roi.mzmean )) # expand constructed roi for roi in ROIs: for n in range(dropped_points): # insert in the begin roi.i.insert(0, 0) roi.mz.insert(0, roi.mzmean) # change scan numbers (necessary for future matching) roi.scan = (roi.scan[0] - dropped_points, roi.scan[1] + dropped_points) assert roi.scan[1] - roi.scan[0] == len(roi.i) - 1 return ROIs
def find_voronoi_seeds(simple_vertices, simple_faces, complex_vertices, complex_faces, log_file, cutoff_angle=(np.pi / 2)): ''' Finds those points on the complex mesh that correspond best to the simple mesh (taking into accound euclidian distance and direction of normals) while forcing a one-to-one mapping ''' from bintrees import FastAVLTree import scipy.spatial as spatial from utils import log # calculate normals for simple and complex vertices simple_normals = calculate_normals(simple_vertices, simple_faces) complex_normals = calculate_normals(complex_vertices, complex_faces) # prepare array to store seeds voronoi_seed_idx = np.zeros( (simple_vertices.shape[0], ), dtype='int64') - 1 missing = np.where(voronoi_seed_idx == -1)[0].shape[0] # initialize with all vertices and small number of neighbours remaining_idxs = range(simple_vertices.shape[0]) neighbours = 100 while missing > 0: log(log_file, 'producing nearest neighbours k=%i' % (neighbours)) # find nearest neighbours of simple vertices on complex mesh using kdtree inaccuracy, mapping = spatial.KDTree(complex_vertices).query( simple_vertices[remaining_idxs], k=neighbours) # create tidy long-format lists simple_idxs = np.asarray([ neighbours * [simple_idx] for simple_idx in remaining_idxs ]).flatten() candidate_idxs = mapping.flatten() diff_euclid = inaccuracy.flatten() # for each vertex pair calculate the angle between their normals diff_normals, _ = compare_normals(simple_normals[simple_idxs], complex_normals[candidate_idxs]) log(log_file, 'candidates %i' % (diff_normals.shape[0])) # remove those pairs that have an angle / distance above cutoff #mask = np.unique(np.concatenate((np.where(diff_euclid>cutoff_euclid)[0], np.where(diff_normals>cutoff_rad)[0]))) mask = np.unique(np.where(diff_normals > cutoff_angle)[0]) diff_normals = np.delete(diff_normals, mask) diff_euclid = np.delete(diff_euclid, mask) simple_idxs = np.delete(simple_idxs, mask) candidate_idxs = np.delete(candidate_idxs, mask) log(log_file, 'remaining candidates %i' % (diff_normals.shape[0])) # calculate scores for each vertex pair scores = (diff_normals - np.mean(diff_normals)) + (diff_euclid - np.mean(diff_euclid)) log(log_file, 'producing tree') # make a binary search tree from the scores and vertex pairs, # organisation is key: score, values: tuple(simple_vertex, candiate_complex_vertex) tree = FastAVLTree(zip(scores, zip(simple_idxs, candidate_idxs))) while tree.count > 0: min_item = tree.pop_min() simple_idx = min_item[1][0] candidate_idx = min_item[1][1] if (voronoi_seed_idx[simple_idx] == -1): if candidate_idx not in voronoi_seed_idx: voronoi_seed_idx[simple_idx] = candidate_idx else: pass else: pass missing = np.where(voronoi_seed_idx == -1)[0].shape[0] if missing == 0: break # if the tree is empty, but there are still seeds missing, increase the number of nearest neighbours # and repeat procedure, but only for those simple vertices that have not been matched yet log(log_file, 'missing %i' % (missing)) remaining_idxs = np.where(voronoi_seed_idx == -1)[0] neighbours *= 5 return voronoi_seed_idx, inaccuracy, log_file
def competetive_fast_marching(vertices, graph, seeds): ''' Label all vertices on highres mesh to the closest seed vertex using a balanced binary search tree ''' import numpy as np import sys from bintrees import FastAVLTree # make a labelling container to be filled with the search tree # first column are the vertex indices of the complex mesh # second column are the labels from the simple mesh # (-1 for all but the corresponding points for now) labels = np.zeros((vertices.shape[0], 2), dtype='int64') - 1 labels[:, 0] = range(vertices.shape[0]) for i in range(seeds.shape[0]): labels[seeds[i]][1] = i # initiate AVLTree for binary search tree = FastAVLTree() # organisation of the tree will be # key: edge length; value: tuple of vertices (source, target) # add all neighbours of the voronoi seeds for v in seeds: add_neighbours(v, 0, graph, labels, tree) # Competetive fast marching starting from voronoi seeds printcount = 0 while tree.count > 0: printcount += 1 # pdb.set_trace() # pop the item with minimum edge length min_item = tree.pop_min() length = min_item[0] source = min_item[1][0] target = min_item[1][1] # if target no label yet (but source does!), assign label of source if labels[target][1] == -1: if labels[source][1] == -1: sys.exit('Source has no label, something went wrong!') else: # assign label of source to target labels[target][1] = labels[source][1] # test if labelling is complete if any(labels[:, 1] == -1): # if not, add neighbours of target to tree add_neighbours(target, length, graph, labels, tree) else: break # if the target already has a label the item is just popped out of the # tree and nothing else happens else: pass # for monitoring the progress if np.mod(printcount, 100) == 0.0: print 'tree ' + str(tree.count) print 'labels ' + str(np.where(labels[:, 1] == -1)[0].shape[0]) return labels
def get_tree(dico, size): tree = FastAVLTree() for i in range(size): tree.insert(dico[i],None)
def __init__(self): self.avl = FastAVLTree()
def construct(src=None): return FastAVLTree(src)
def __init__(self, price_level_type, **kwargs): super(AVLTreePriceLevels, self).__init__(price_level_type, **kwargs) self.price_levels = FastAVLTree(), FastAVLTree()