def test_copy_from_dict(self): expect = {k: float(v) for k, v in zip(range(10), range(10, 20))} nbd = Dict.empty(int32, float64) for k, v in expect.items(): nbd[k] = v got = dict(nbd) self.assertEqual(got, expect)
def test_dict_of_dict_int_keyval(self): def inner_numba_dict(): d = Dict.empty( key_type=types.intp, value_type=types.intp, ) return d d = Dict.empty( key_type=types.intp, value_type=types.DictType(types.intp, types.intp), ) def usecase(d, make_inner_dict): for i in range(100): mid = make_inner_dict() for j in range(i + 1): mid[j] = j * 10000 d[i] = mid return d got = usecase(d, inner_numba_dict) expect = usecase({}, dict) self.assertIsInstance(expect, dict) self.assertEqual(dict(got), expect) # Delete items for where in [12, 3, 6, 8, 10]: del got[where] del expect[where] self.assertEqual(dict(got), expect)
def assert_disallow_key(self, ty): msg = '{} as key is forbidded'.format(ty) self.assert_disallow(msg, lambda: Dict.empty(ty, types.intp)) @njit def foo(): Dict.empty(ty, types.intp) self.assert_disallow(msg, foo)
def foo(): d = Dict.empty( key_type=types.unicode_type, value_type=types.int32, ) d["123"] = 123 d["321"] = 321 return d
def foo(): d = Dict.empty( key_type=types.int32, value_type=types.unicode_type, ) d[123] = "123" d[321] = "321" return d
def assert_disallow_value(self, ty): msg = '{} as value is forbidded'.format(ty) self.assert_disallow(msg, lambda: Dict.empty(types.intp, ty)) @njit def foo(): Dict.empty(types.intp, ty) self.assert_disallow(msg, foo)
def foo(): # Make dictionary d = Dict.empty( key_type=types.unicode_type, value_type=float_array, ) # Fill the dictionary d["posx"] = np.arange(3).astype(np.float64) d["posy"] = np.arange(3, 6).astype(np.float64) return d
def test_delitem(self): d = Dict.empty(types.int64, types.unicode_type) d[1] = 'apple' @njit def foo(x, k): del x[1] foo(d, 1) self.assertEqual(len(d), 0) self.assertFalse(d)
def foo(count): d = Dict.empty( key_type=types.intp, value_type=inner_dict_ty, ) for i in range(count): d[i] = inner_numba_dict() for j in range(i + 1): d[i][j] = j return d
def test_basic(self): d = Dict.empty(int32, float32) # len self.assertEqual(len(d), 0) # setitems d[1] = 1 d[2] = 2.3 d[3] = 3.4 self.assertEqual(len(d), 3) # keys self.assertEqual(list(d.keys()), [1, 2, 3]) # values for x, y in zip(list(d.values()), [1, 2.3, 3.4]): self.assertAlmostEqual(x, y, places=4) # getitem self.assertAlmostEqual(d[1], 1) self.assertAlmostEqual(d[2], 2.3, places=4) self.assertAlmostEqual(d[3], 3.4, places=4) # deltiem del d[2] self.assertEqual(len(d), 2) # get self.assertIsNone(d.get(2)) # setdefault d.setdefault(2, 100) d.setdefault(3, 200) self.assertEqual(d[2], 100) self.assertAlmostEqual(d[3], 3.4, places=4) # update d.update({4: 5, 5: 6}) self.assertAlmostEqual(d[4], 5) self.assertAlmostEqual(d[5], 6) # contains self.assertTrue(4 in d) # items pyd = dict(d.items()) self.assertEqual(len(pyd), len(d)) # pop self.assertAlmostEqual(d.pop(4), 5) # popitem nelem = len(d) k, v = d.popitem() self.assertEqual(len(d), nelem - 1) self.assertTrue(k not in d) # __eq__ & copy copied = d.copy() self.assertEqual(copied, d) self.assertEqual(list(copied.items()), list(d.items()))
def check_stringify(self, strfn, prefix=False): nbd = Dict.empty(int32, int32) d = {} nbd[1] = 2 d[1] = 2 checker = self.assertIn if prefix else self.assertEqual checker(strfn(d), strfn(nbd)) nbd[2] = 3 d[2] = 3 checker(strfn(d), strfn(nbd)) for i in range(10, 20): nbd[i] = i + 1 d[i] = i + 1 checker(strfn(d), strfn(nbd)) if prefix: self.assertTrue(strfn(nbd).startswith('DictType'))
def test_getitem_return_type(self): # Dict.__getitem__ must return non-optional type. d = Dict.empty(types.int64, types.int64[:]) d[1] = np.arange(10, dtype=np.int64) @njit def foo(d): d[1] += 100 return d[1] foo(d) # Return type is an array, not optional retty = foo.nopython_signatures[0].return_type self.assertIsInstance(retty, types.Array) self.assertNotIsInstance(retty, types.Optional) # Value is correctly updated self.assertPreciseEqual(d[1], np.arange(10, dtype=np.int64) + 100)
def test_str_key_array_value(self): np.random.seed(123) d = Dict.empty( key_type=types.unicode_type, value_type=types.float64[:], ) expect = [] expect.append(np.random.random(10)) d['mass'] = expect[-1] expect.append(np.random.random(20)) d['velocity'] = expect[-1] for i in range(100): expect.append(np.random.random(i)) d[str(i)] = expect[-1] self.assertEqual(len(d), len(expect)) self.assertPreciseEqual(d['mass'], expect[0]) self.assertPreciseEqual(d['velocity'], expect[1]) # Ordering is kept for got, exp in zip(d.values(), expect): self.assertPreciseEqual(got, exp) # Try deleting self.assertTrue('mass' in d) self.assertTrue('velocity' in d) del d['mass'] self.assertFalse('mass' in d) del d['velocity'] self.assertFalse('velocity' in d) del expect[0:2] for i in range(90): k, v = d.popitem() w = expect.pop() self.assertPreciseEqual(v, w) # Trigger a resize expect.append(np.random.random(10)) d["last"] = expect[-1] # Ordering is kept for got, exp in zip(d.values(), expect): self.assertPreciseEqual(got, exp)
def _create_dom_channel_lookup(self): if HAVE_NUMBA: from numba.typed import Dict from numba import types data = Dict.empty( key_type=types.i8, value_type=types.float64[:, :] ) else: data = {} for pmt in self.detector.pmts: if pmt.dom_id not in data: data[pmt.dom_id] = np.zeros((31, 9)) data[pmt.dom_id][pmt.channel_id] = np.asarray([ pmt.pos_x, pmt.pos_y, pmt.pos_z, pmt.dir_x, pmt.dir_y, pmt.dir_z, pmt.t0, pmt.du, pmt.floor ], dtype=np.float64) self._calib_by_dom_and_channel = data if HAVE_NUMBA: self._lookup_tables = [(dom, cal) for dom, cal in data.items()]
def _create_pmt_id_lookup(self): if HAVE_NUMBA: from numba.typed import Dict from numba import types data = Dict.empty(key_type=types.i8, value_type=types.float64[:]) else: data = {} for pmt in self.detector.pmts: data[pmt.pmt_id] = np.asarray([ pmt.pos_x, pmt.pos_y, pmt.pos_z, pmt.dir_x, pmt.dir_y, pmt.dir_z, pmt.t0, pmt.du, pmt.floor, ], dtype=np.float64) self._calib_by_pmt_id = data
def ex_typed_dict_from_cpython(): # magictoken.ex_typed_dict_from_cpython.begin import numpy as np from numba import njit from numba import types from numba.typed import Dict # The Dict.empty() constructs a typed dictionary. # The key and value typed must be explicitly declared. d = Dict.empty( key_type=types.unicode_type, value_type=types.float64[:], ) # The typed-dict can be used from the interpreter. d['posx'] = np.asarray([1, 0.5, 2], dtype='f8') d['posy'] = np.asarray([1.5, 3.5, 2], dtype='f8') d['velx'] = np.asarray([0.5, 0, 0.7], dtype='f8') d['vely'] = np.asarray([0.2, -0.2, 0.1], dtype='f8') # Here's a function that expects a typed-dict as the argument @njit def move(d): # inplace operations on the arrays d['posx'] += d['velx'] d['posy'] += d['vely'] print('posx: ', d['posx']) # Out: posx: [1. 0.5 2. ] print('posy: ', d['posy']) # Out: posy: [1.5 3.5 2. ] # Call move(d) to inplace update the arrays in the typed-dict. move(d) print('posx: ', d['posx']) # Out: posx: [1.5 0.5 2.7] print('posy: ', d['posy']) # Out: posy: [1.7 3.3 2.1] # magictoken.ex_typed_dict_from_cpython.end # Test np.testing.assert_array_equal(d['posx'], [1.5, 0.5, 2.7]) np.testing.assert_array_equal(d['posy'], [1.7, 3.3, 2.1])
[1, _, 3, _, _, _, 4, _, _, 2, _, _, 2, _, _, _, _, _, _, 4, _, _, _, 1], [1, _, 3, _, _, _, 2, _, _, 2, 2, _, 2, _, _, _, 4, _, _, _, _, 4, _, 1], [1, _, _, 3, _, _, 2, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 1], [1, _, 3, _, _, _, _, _, _, _, 3, _, _, 3, 3, _, _, _, _, 3, 3, _, _, 1], [1, _, 3, _, _, _, 3, 3, _, 3, _, _, _, 3, 3, _, _, _, _, 2, 3, _, _, 1], [1, _, _, _, _, 3, _, 3, _, _, 3, _, _, _, _, _, _, _, _, _, _, _, _, 1], [1, _, 4, _, 3, _, _, _, _, 3, _, _, 2, _, _, _, _, _, _, _, _, 2, _, 1], [1, _, _, _, _, _, 4, _, _, _, _, _, 2, 2, _, _, _, _, _, _, 2, 2, _, 1], [1, _, _, 4, _, _, _, _, 4, _, _, _, _, 2, 2, 2, 2, 2, 2, 2, 2, _, _, 1], [1, _, _, _, _, _, _, _, _, _, 4, _, _, _, _, _, _, _, _, _, _, _, _, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] WORLD_WIDTH, WORLD_HEIGHT = max([len(i) for i in matrix_map ]) * TILE, len(matrix_map) * TILE world_map = Dict.empty(key_type=types.UniTuple(int32, 2), value_type=int32) mini_map = set() collision_walls = [] for j, row in enumerate(matrix_map): for i, char in enumerate(row): if char: mini_map.add((i * MAP_TILE, j * MAP_TILE)) collision_walls.append(pygame.Rect(i * TILE, j * TILE, TILE, TILE)) if char == 1: world_map[(i * TILE, j * TILE)] = 1 elif char == 2: world_map[(i * TILE, j * TILE)] = 2 elif char == 3: world_map[(i * TILE, j * TILE)] = 3 elif char == 4: world_map[(i * TILE, j * TILE)] = 4
def producer(): d = Dict.empty(int32, float64) d[1] = 1.23 return d
def inner_numba_dict(): d = Dict.empty( key_type=types.intp, value_type=types.intp, ) return d
def get_continuation_values(self, period): """Get continuation values. The function takes the expected value functions from the previous periods and then uses the indices of child states to put these expected value functions in the correct format. If period is equal to self.n_periods - 1 the function returns arrays of zeros since we are in terminal states. Otherwise we retrieve expected value functions for next period and call :func:`_get_continuation_values` to assign continuation values to all choices within a period. (The object `subset_expected_value_functions` is required because we need a Numba typed dict but the function :meth:`StateSpace.get_attribute_from_period` just returns a normal dict) Returns ------- continuation_values : numba.typed.Dict The continuation values for each dense key in a :class:`numpy.ndarray`. See also -------- _get_continuation_values A more theoretical explanation can be found here: See :ref:`get continuation values <get_continuation_values>`. """ if period == self.n_periods - 1: shapes = self.get_attribute_from_period("base_draws_sol", period) states = self.get_attribute_from_period("dense_key_to_core_indices", period) continuation_values = { key: np.zeros((states[key].shape[0], shapes[key].shape[1])) for key in shapes } else: child_indices = self.get_attribute_from_period("child_indices", period) expected_value_functions = self.get_attribute_from_period( "expected_value_functions", period + 1 ) subset_expected_value_functions = Dict.empty( key_type=nb.types.int64, value_type=nb.types.float64[:] ) for key, value in expected_value_functions.items(): subset_expected_value_functions[key] = value transit_choice_sets = ( "transit_key_to_choice_set" if hasattr(self, "transit_key_to_choice_set") else "dense_key_to_choice_set" ) continuation_values = _get_continuation_values( self.get_attribute_from_period("dense_key_to_complex", period), self.get_attribute_from_period(transit_choice_sets, period), self.get_attribute_from_period("dense_key_to_core_indices", period), child_indices, self.core_key_and_dense_index_to_dense_key, bypass={"expected_value_functions": subset_expected_value_functions}, ) if len(self.optim_paras["exogenous_processes"]) > 0: continuation_values = weight_continuation_values( self.get_attribute_from_period("dense_key_to_complex", period), self.options, bypass={ "continuation_values": continuation_values, "transit_key_to_choice_set": self.get_attribute_from_period( transit_choice_sets, period ), }, ) return continuation_values
def foo(): d = Dict() d[123] = 321 return d
def call_ctor(): return Dict.empty(kt, vt)
def _read_block_dense( self, block_idx, tile_block_size, min_per_file, max_per_file, open_files, slices, ranges, scheme_indices, shape_prods, out_decoded, r_n_d, sig_dims, ds_shape, need_clear, native_dtype, corrections, ): """ Reads a block of tiles, starting at `block_idx`, having a size of `tile_block_size` read range entries. """ # phase 1: read buffers = Dict() # this list manages the lifetime of the ManagedBuffer instances; # after `buf_ref` goes out of scope, the buffers are returned to # the buffer pool, so make sure that this matches with the usage # of the buffers! buf_ref = [] for fileno in min_per_file.keys(): fh = open_files[fileno] # add align_to to allow for alignment cut: align_to = fh.get_blocksize() read_size = max_per_file[fileno] - min_per_file[fileno] + align_to # ManagedBuffer gives us memory in 4k blocks, so the size is 4k aligned mb = ManagedBuffer(self._buffer_pool, read_size, alignment=fh.get_blocksize()) arr = np.frombuffer(mb.buf, dtype=np.uint8) buf_ref.append(mb) seek_pos = min_per_file[fileno] alignment = 0 # seek_pos needs to be aligned to 4k block size, too: if seek_pos % align_to != 0: alignment = seek_pos % align_to seek_pos = align_to * (seek_pos // align_to) fh.seek(seek_pos) read_result = fh.readinto(arr) # read may be truncated, if the buffer is larger than the file; we # truncate the buffer, too, to make sure we don't use any # uninitialized values. Also cut off `alignment` bytes at the beginning, # which were read to make O_DIRECT happy: buffers[fileno] = read_result[alignment:] # phase 2: decode tiles from the data that was read for idx in range(block_idx, block_idx + tile_block_size): origin = slices[idx, 0] shape = slices[idx, 1] tile_ranges = ranges[idx] scheme_idx = scheme_indices[idx] out_cut = out_decoded[:shape_prods[idx]].reshape((shape[0], -1)) data = r_n_d( idx, buffers, sig_dims, tile_ranges, out_cut, native_dtype, do_zero=need_clear, origin=origin, shape=shape, ds_shape=ds_shape, offsets=min_per_file, ) tile_slice = Slice( origin=origin, shape=Shape(shape, sig_dims=sig_dims) ) data = data.reshape(shape) self.preprocess(data, tile_slice, corrections) yield DataTile( data, tile_slice=tile_slice, scheme_idx=scheme_idx, )
def graph_maps_from_nX( networkX_graph: nx.Graph ) -> Tuple[tuple, np.ndarray, np.ndarray, Dict]: ''' Strategic decisions because of too many edge cases: - decided to not discard disconnected components to avoid unintended consequences - no internal simplification - use prior methods or tools to clean or simplify the graph before calling this method - length and angle now set automatically inside this method because in and out bearing are set here regardless. - returns node_data, edge_data, a map from nodes to edges ''' if not isinstance(networkX_graph, nx.Graph): raise TypeError('This method requires an undirected networkX graph.') logger.info('Preparing node and edge arrays from networkX graph.') g_copy = networkX_graph.copy() logger.info('Preparing graph') total_out_degrees = 0 for n in tqdm(g_copy.nodes(), disable=checks.quiet_mode): # writing node identifier to 'labels' in case conversion to integers method interferes with order g_copy.nodes[n]['label'] = n # sum edges for nb in g_copy.neighbors(n): total_out_degrees += 1 logger.info('Generating data arrays') # convert the nodes to sequential - this permits implicit indices with benefits to speed and structure g_copy = nx.convert_node_labels_to_integers(g_copy, 0) # prepare the node and edge maps node_uids = [] # float - for consistency - requires higher accuracy for x, y work node_data = np.full((g_copy.number_of_nodes(), 4), np.nan, dtype=np.float64) # float - allows for nan and inf - float32 should be ample... edge_data = np.full((total_out_degrees, 7), np.nan, dtype=np.float32) # nodes have a one-to-many mapping to edges node_edge_map = Dict.empty(key_type=types.int64, value_type=types.int64[:]) edge_idx = 0 # populate the nodes for n, d in tqdm(g_copy.nodes(data=True), disable=checks.quiet_mode): # label # don't cast to string because otherwise correspondence between original and round-trip graph indices is lost node_uids.append(d['label']) # cast to int for indexing node_idx = int(n) # NODE MAP INDEX POSITION 0 = x coordinate if 'x' not in d: raise KeyError( f'Encountered node missing "x" coordinate attribute at node {n}.' ) node_data[node_idx][0] = d['x'] # NODE MAP INDEX POSITION 1 = y coordinate if 'y' not in d: raise KeyError( f'Encountered node missing "y" coordinate attribute at node {n}.' ) node_data[node_idx][1] = d['y'] # NODE MAP INDEX POSITION 2 = live or not if 'live' in d: node_data[node_idx][2] = d['live'] else: node_data[node_idx][2] = True # NODE MAP INDEX POSITION 3 = ghosted if 'ghosted' in d: node_data[node_idx][3] = d['ghosted'] else: node_data[node_idx][3] = False # build edges out_edges = [] for nb in g_copy.neighbors(n): # add the new edge index to the node's out edges out_edges.append(edge_idx) # EDGE MAP INDEX POSITION 0 = start node edge_data[edge_idx][0] = node_idx # EDGE MAP INDEX POSITION 1 = end node edge_data[edge_idx][1] = nb # get edge data edge = g_copy[node_idx][nb] # EDGE MAP INDEX POSITION 2 = length if not 'geom' in edge: raise KeyError( f'No edge geom found for edge {node_idx}-{nb}: ' f'Please add an edge "geom" attribute consisting of a shapely LineString.' f'Simple (straight) geometries can be inferred automatically through use of the nX_simple_geoms() method.' ) line_geom = edge['geom'] if line_geom.type != 'LineString': raise TypeError( f'Expecting LineString geometry but found {line_geom.type} geometry for edge {node_idx}-{nb}.' ) # cannot have zero or negative length - division by zero l = line_geom.length if not np.isfinite(l) or l <= 0: raise ValueError( f'Length attribute {l} for edge {node_idx}-{nb} must be a finite positive value.' ) edge_data[edge_idx][2] = l # EDGE MAP INDEX POSITION 3 = angle_sum # check geom coordinates directionality (for bearings at index 5 / 6) # flip if facing backwards direction s_x, s_y = node_data[node_idx][:2] if not np.allclose( (s_x, s_y), line_geom.coords[0][:2], atol=0.001, rtol=0): line_geom = geometry.LineString(line_geom.coords[::-1]) e_x, e_y = (g_copy.nodes[nb]['x'], g_copy.nodes[nb]['y']) # double check that coordinates now face the forwards direction if not np.allclose((s_x, s_y), line_geom.coords[0][:2]) or \ not np.allclose((e_x, e_y), line_geom.coords[-1][:2], atol=0.001, rtol=0): raise ValueError( f'Edge geometry endpoint coordinate mismatch for edge {node_idx}-{nb}' ) # iterate the coordinates and calculate the angular change angle_sum = 0 for c in range(len(line_geom.coords) - 2): x_1, y_1 = line_geom.coords[c][:2] x_2, y_2 = line_geom.coords[c + 1][:2] x_3, y_3 = line_geom.coords[c + 2][:2] # arctan2 is y / x order a_1 = np.rad2deg(np.arctan2(y_2 - y_1, x_2 - x_1)) a_2 = np.rad2deg(np.arctan2(y_3 - y_2, x_3 - x_2)) angle_sum += np.abs((a_2 - a_1 + 180) % 360 - 180) # alternative # A = np.array(merged_line.coords[c + 1]) - np.array(merged_line.coords[c]) # B = np.array(merged_line.coords[c + 2]) - np.array(merged_line.coords[c + 1]) # angle = np.abs(np.degrees(np.math.atan2(np.linalg.det([A, B]), np.dot(A, B)))) if not np.isfinite(angle_sum) or angle_sum < 0: raise ValueError( f'Angle-sum attribute {angle_sum} for edge {node_idx}-{nb} must be a finite positive value.' ) edge_data[edge_idx][3] = angle_sum # EDGE MAP INDEX POSITION 4 = imp_factor # if imp_factor is set explicitly, then use if 'imp_factor' in edge: # cannot have imp_factor less than zero (but == 0 is OK) imp_factor = edge['imp_factor'] if not (np.isfinite(imp_factor) or np.isinf(imp_factor)) or imp_factor < 0: raise ValueError( f'Impedance factor: {imp_factor} for edge {node_idx}-{nb} must be a finite positive value or positive infinity.' ) edge_data[edge_idx][4] = imp_factor else: # fallback imp_factor of 1 edge_data[edge_idx][4] = 1 # EDGE MAP INDEX POSITION 5 - in bearing x_1, y_1 = line_geom.coords[0][:2] x_2, y_2 = line_geom.coords[1][:2] edge_data[edge_idx][5] = np.rad2deg( np.arctan2(y_2 - y_1, x_2 - x_1)) # EDGE MAP INDEX POSITION 6 - out bearing x_1, y_1 = line_geom.coords[-2][:2] x_2, y_2 = line_geom.coords[-1][:2] edge_data[edge_idx][6] = np.rad2deg( np.arctan2(y_2 - y_1, x_2 - x_1)) # increment the edge_idx edge_idx += 1 # add the node to the node_edge_map node_edge_map[node_idx] = np.array(out_edges, dtype='int64') return tuple(node_uids), node_data, edge_data, node_edge_map
def calculate_descr(img, mirrored=False): if mirrored: img = cv2.flip(img, 1) height = img.shape[0] width = img.shape[1] height_divided_by_2 = height // 2 width_divided_by_2 = width // 2 kps = AKAZE.detect(img, None) if kps is None: return None kps = sorted(kps, key=lambda x: x.response, reverse=True) descriptors_count = [0, 0, 0, 0] keypoints = [] _keypoints = np.zeros((256, 2)) keypoints_neighbors = Dict.empty(key_type=types.float64, value_type=types.int64) for keypoint in kps: keypoint_x, keypoint_y = keypoint.pt if len(keypoints) != 0: skip_keypoint = check_distance(keypoint_x, keypoint_y, _keypoints, keypoints_neighbors) if skip_keypoint: continue if sum(descriptors_count) == 64 * 4: break if descriptors_count[ 0] < 64 and 0 < keypoint_y < height_divided_by_2 and 0 < keypoint_x < width_divided_by_2: keypoints.append(keypoint) _keypoints[len(keypoints) - 1][0] = keypoint.pt[0] _keypoints[len(keypoints) - 1][1] = keypoint.pt[1] descriptors_count[0] += 1 continue if descriptors_count[ 1] < 64 and 0 < keypoint_y < height_divided_by_2 and width_divided_by_2 < keypoint_x < width: keypoints.append(keypoint) _keypoints[len(keypoints) - 1][0] = keypoint.pt[0] _keypoints[len(keypoints) - 1][1] = keypoint.pt[1] descriptors_count[1] += 1 continue if descriptors_count[ 2] < 64 and height_divided_by_2 < keypoint_y < height and 0 < keypoint_x < width_divided_by_2: keypoints.append(keypoint) _keypoints[len(keypoints) - 1][0] = keypoint.pt[0] _keypoints[len(keypoints) - 1][1] = keypoint.pt[1] descriptors_count[2] += 1 continue if descriptors_count[ 3] < 64 and height_divided_by_2 < keypoint_y < height and 0 < width_divided_by_2 < keypoint_x < width: keypoints.append(keypoint) _keypoints[len(keypoints) - 1][0] = keypoint.pt[0] _keypoints[len(keypoints) - 1][1] = keypoint.pt[1] descriptors_count[3] += 1 continue _, desc1 = AKAZE.compute(img, keypoints) return desc1
def make_dict(): return Dict.empty(kt, vt)
def __init__(self): self.d = Dict.empty(key_type=int32, value_type=int32)
def foo(k, v, w): d = Dict() d[k] = v d[k] = w return d
def newVmap(X, typ): d = Dict.empty(typ, i8) for x in X: d[x] = 0 return d
def foo(k1, v1, k2): d = Dict() d[k1] = v1 return d, d[k2]
def foo(): Dict.empty(types.intp, ty)
from numba.typed import Dict from numba import types, njit import numpy as np # Using Python dictionary d = dict() d[1] = [1.1, 1.2] d[2] = [2.2] d[3] = [3.3, 3.4, 3.5] print(d) # Using Numba dictionary is more efficient for passing into compiled region due to lower unboxing overheads # The Dict.empty() constructs a typed dictionary. # The key and value typed must be explicitly declared. nd = Dict.empty(key_type=types.int64, value_type=types.float64[:]) # Assigning key-values using Numpy arrays nd[1] = np.asarray([1.1, 1.2], dtype='f8') nd[2] = np.asarray([2.2], dtype='f8') nd[3] = np.asarray([3.3, 3.4, 3.5], dtype='f8') print(nd) @njit def foo(nd): nd[4] = np.asarray([4.4, 4.5], dtype='f8') return nd print(foo(nd))
def foo(k, v): d = Dict() d[k] = v return d
def to_typed_dict_nonterm_rules(untyped_d): typed_d = Dict.empty(key_type=types.int64, value_type=types.int64[:]) for nonterm, rules in untyped_d.items(): np_rules = np.array([hash(rule) for rule in rules], dtype=np.int64) typed_d[nonterm] = np_rules return typed_d
def foo(k, h, v): d = Dict() d[k] = v d[h] = v return d
def foo(k, v): d = Dict() if k: d[k] = v return d
def foo(ks, vs): d = Dict() for k, v in zip(ks, vs): d[k] = v return d
def foo(): d = Dict() return d
def foo(x): d = Dict() d[0] = x d[1] = Bag(101) return d
def computeStepOne(self, label_start, output_path): # correct block size if smaller than specified in param file, which happens for blocks on the boundary if self.labels_in.shape[0] > param.max_bs_z or self.labels_in.shape[ 1] > param.max_bs_y or self.labels_in.shape[2] > param.max_bs_x: raise ValueError( "Block is larger than specified in param file! - aborting") if self.labels_in.shape[0] < param.max_bs_z or self.labels_in.shape[ 1] < param.max_bs_y or self.labels_in.shape[2] < param.max_bs_x: # labels_extended = np.zeros((max_bs_z,max_bs_y,max_bs_x), dtype=np.uint64) # labels_extended[:self.labels_in.shape[0], :self.labels_in.shape[1], :self.labels_in.shape[2]] = self.labels_in.copy() pad_z = param.max_bs_z - self.labels_in.shape[0] pad_y = param.max_bs_y - self.labels_in.shape[1] pad_x = param.max_bs_x - self.labels_in.shape[2] self.labels_in = np.pad(self.labels_in, ((0, pad_z), (0, pad_y), (0, pad_x)), 'constant', constant_values=0) if self.labels_in.shape[ 0] != param.max_bs_z or self.labels_in.shape[ 1] != param.max_bs_y or self.labels_in.shape[ 2] != param.max_bs_x: raise ValueError( "Attempted to pad labels_in, unknown error, shape is:" + str(self.labels_in.shape) + " - aborting") start_time_cc_labels = time.time() # compute connected component labels cc_labels, n_comp = computeConnectedComp6(self.labels_in, label_start, param.max_labels_block) del self.labels_in output_name = "cc_labels" output_folder = blockFolderPath(output_path, self.bz, self.by, self.bx) #save output and slices of walls makeFolder(output_folder) writeData(output_folder + output_name, cc_labels) writeData(output_folder + "zMinWall", cc_labels[0, :, :]) writeData(output_folder + "zMaxWall", cc_labels[-1, :, :]) writeData(output_folder + "yMinWall", cc_labels[:, 0, :]) writeData(output_folder + "yMaxWall", cc_labels[:, -1, :]) writeData(output_folder + "xMinWall", cc_labels[:, :, 0]) writeData(output_folder + "xMaxWall", cc_labels[:, :, -1]) self.time_cc_labels = time.time() - start_time_cc_labels start_time_AdjLabelLocal = time.time() # find the set of adjacent labels, both inside the volume and the ones connected to the local border neighbor_label_set_inside_local, neighbor_label_set_border_local = findAdjLabelSetLocal( cc_labels) self.time_AdjLabelLocal = time.time() - start_time_AdjLabelLocal if param.compute_statistics: n_points = Dict.empty(key_type=types.float64, value_type=types.float64) self.points_per_component = get_points_per_component( cc_labels, n_points) del cc_labels start_time_assoc_labels = time.time() # for identification of local wholes that do not cross the border, unify both sets and write a dict of the corresponding neighbors for each component neighbor_label_set = neighbor_label_set_inside_local.union( neighbor_label_set_border_local) neighbor_label_dict = writeNeighborLabelDict( neighbor_label_dict=False, neighbor_label_set=neighbor_label_set.copy()) # create a set of undtermined components, at this stage all components in the block and find associated labels of components that can be identified already undetermined_local = set(neighbor_label_dict.keys()) associated_label_local = Dict.empty(key_type=types.int64, value_type=types.int64) associated_label_local, undetermined_local, isHole, isNotHole = findAssociatedLabels( neighbor_label_dict=neighbor_label_dict, undetermined=undetermined_local, associated_label=associated_label_local) self.time_assoc_labels = time.time() - start_time_assoc_labels del neighbor_label_set, neighbor_label_set_border_local, neighbor_label_dict self.n_comp = n_comp self.n_Holes = len(isHole) self.n_NotHoles = len(isNotHole) # remove alrady detected hole components from neighbor label set local and write the according neighbor label dict of components that are not yet determined self.size_label_set_inside = len(neighbor_label_set_inside_local) neighbor_label_set_inside_local_reduced = removeDetComp( neighbor_label_set_inside_local, isHole, isNotHole) if param.compute_statistics: self.hole_components = isHole self.undetermined = undetermined_local neighbor_label_dict_reduced = writeNeighborLabelDict( neighbor_label_dict=False, neighbor_label_set=neighbor_label_set_inside_local_reduced.copy()) self.size_label_set_inside_reduced = len( neighbor_label_set_inside_local_reduced) # write components that are needed for later steps to files output_folder = blockFolderPath(output_path, self.bz, self.by, self.bx) start_time_writepickle = time.time() dumpNumbaDictToFile(associated_label_local, "associated_label_local", output_folder, "") dumpToFile(undetermined_local, "undetermined_local", output_folder, "") dumpToFile(neighbor_label_dict_reduced, "neighbor_label_dict_reduced", output_folder, "") self.time_writepickle = time.time() - start_time_writepickle