def _random_tile_coordinates(self, slide: Slide) -> CoordinatePair: """Return 0-level Coordinates of a tile picked at random within the box. Parameters ---------- slide : Slide Slide from which calculate the coordinates. Needed to calculate the box. Returns ------- CoordinatePair Random tile Coordinates at level 0 """ box_mask_lvl = self.box_mask_lvl(slide) tile_w_lvl, tile_h_lvl = self.tile_size x_ul_lvl = np.random.choice(sparse.where(box_mask_lvl)[1]) y_ul_lvl = np.random.choice(sparse.where(box_mask_lvl)[0]) x_br_lvl = x_ul_lvl + tile_w_lvl y_br_lvl = y_ul_lvl + tile_h_lvl tile_wsi_coords = scale_coordinates( reference_coords=CoordinatePair(x_ul_lvl, y_ul_lvl, x_br_lvl, y_br_lvl), reference_size=slide.level_dimensions(level=self.level), target_size=slide.level_dimensions(level=0), ) return tile_wsi_coords
def _genTables(self): "Generate the multiplication tables." self.bladeTupMap = generate_blade_tup_map(self.bladeTupList) self.bitmap_to_linear_map = generate_bitmap_to_linear_index_map(self.bladeTupList, self.firstIdx) self.linear_map_to_bitmap = np.zeros(len(self.bladeTupMap), dtype=int) for bitmap, linear in enumerate(self.bitmap_to_linear_map): self.linear_map_to_bitmap[linear] = int(bitmap) self.gmt, imt_prod_mask, omt_prod_mask, lcmt_prod_mask = construct_tables( np.array(self.gradeList), self.linear_map_to_bitmap, self.bitmap_to_linear_map, self.sig ) self.omt = sparse.where(omt_prod_mask, self.gmt, self.gmt.dtype.type(0)) self.imt = sparse.where(imt_prod_mask, self.gmt, self.gmt.dtype.type(0)) self.lcmt = sparse.where(lcmt_prod_mask, self.gmt, self.gmt.dtype.type(0)) # This generates the functions that will perform the various products self.gmt_func = get_mult_function(self.gmt, self.gradeList) self.imt_func = get_mult_function(self.imt, self.gradeList) self.omt_func = get_mult_function(self.omt, self.gradeList) self.lcmt_func = get_mult_function(self.lcmt, self.gradeList) self.inv_func = get_leftLaInv(self.gmt, self.gradeList) # these are probably not useful, but someone might want them self.imt_prod_mask = imt_prod_mask self.omt_prod_mask = omt_prod_mask self.lcmt_prod_mask = lcmt_prod_mask
def get_mult_function(mt: sparse.COO, gradeList, grades_a=None, grades_b=None, filter_mask=None): ''' Returns a function that implements the mult_table on two input multivectors ''' if (filter_mask is None) and (grades_a is not None) and (grades_b is not None): # If not specified explicitly, we can specify sparseness by grade filter_mask = np.zeros(mt.nnz, dtype=bool) k_list, _, m_list = mt.coords for i in range(len(filter_mask)): if gradeList[k_list[i]] in grades_a: if gradeList[m_list[i]] in grades_b: filter_mask[i] = 1 filter_mask = sparse.COO(coords=mt.coords, data=filter_mask, shape=mt.shape) if filter_mask is not None: # We can pass the sparse filter mask directly mt = sparse.where(filter_mask, mt, mt.dtype.type(0)) return _get_mult_function(mt) else: return _get_mult_function_runtime_sparse(mt)
def sparsesort(numbers, coords): X, Y = coords matrix = np.zeros((X, Y)) for i in range(numbers): rx, ry = random.randint(0, X - 1), random.randint(0, Y - 1) matrix[rx, ry] = random.random() print() print(r'Sorting {} random numbers in a sparse ("zero-filled") {} x {} matrix:'.\ format(numbers,X,Y)) print(matrix) print() sparse_matrix = sparse.COO(matrix) where = sparse.where(sparse_matrix) smd = sparse_matrix.data matrix_dict = dict(zip(smd, zip(*where))) matrix_sorted = collections.OrderedDict(sorted(matrix_dict.items())) mitems = matrix_sorted.items() print('sorting results:') k = 1 for i, j in mitems: print('item #{}: {} @ coordinate {}'.format(k, i, j)) k += 1 print()
def _genTables(self): "Generate the multiplication tables." self.gmt, imt_prod_mask, omt_prod_mask, lcmt_prod_mask = construct_tables( self._basis_blade_order, self.sig) self.omt = sparse.where(omt_prod_mask, self.gmt, self.gmt.dtype.type(0)) self.imt = sparse.where(imt_prod_mask, self.gmt, self.gmt.dtype.type(0)) self.lcmt = sparse.where(lcmt_prod_mask, self.gmt, self.gmt.dtype.type(0)) # This generates the functions that will perform the various products self.gmt_func = get_mult_function(self.gmt, self.gradeList) self.imt_func = get_mult_function(self.imt, self.gradeList) self.omt_func = get_mult_function(self.omt, self.gradeList) self.lcmt_func = get_mult_function(self.lcmt, self.gradeList) # these are probably not useful, but someone might want them self.imt_prod_mask = imt_prod_mask self.omt_prod_mask = omt_prod_mask self.lcmt_prod_mask = lcmt_prod_mask
def _truncate_sparse(arr, abs=None, rel=None): assert (abs is not None) != (rel is not None) abs_arr = np.absolute(arr) cutoff = abs if abs is not None else rel * abs_arr.max() return sparse.where(abs_arr < cutoff, 0, arr)