def test_dict_of_dict_npm(self): inner_dict_ty = types.DictType(types.intp, types.intp) @njit def inner_numba_dict(): d = Dict.empty( key_type=types.intp, value_type=types.intp, ) return d @njit 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 d = foo(100) ct = 0 for k, dd in d.items(): ct += 1 self.assertEqual(len(dd), k + 1) for kk, vv in dd.items(): self.assertEqual(kk, vv) self.assertEqual(ct, 100)
def test_dict_to_from_meminfo(self): """ Exercise dictobject.{_as_meminfo, _from_meminfo} """ @njit def make_content(nelem): for i in range(nelem): yield i, i + (i + 1) / 100 @njit def boxer(nelem): d = dictobject.new_dict(int32, float64) for k, v in make_content(nelem): d[k] = v return dictobject._as_meminfo(d) dcttype = types.DictType(int32, float64) @njit def unboxer(mi): d = dictobject._from_meminfo(mi, dcttype) return list(d.items()) mi = boxer(10) self.assertEqual(mi.refcount, 1) got = unboxer(mi) expected = list(make_content.py_func(10)) self.assertEqual(got, expected)
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 test_objmode_typed_dict(self): ret_type = types.DictType(types.unicode_type, types.int64) @njit def test4(): with objmode(res=ret_type): res = {'A': 1, 'B': 2} return res with self.assertRaises(TypeError) as raises: test4() self.assertIn( ("can't unbox a <class 'dict'> " "as a <class 'numba.typed.typeddict.Dict'>"), str(raises.exception), )
def _make_dict(typingctx, keyty, valty, ptr): """Make a dictionary struct with the given *ptr* Parameters ---------- keyty, valty: Type Type of the key and value, respectively. ptr : llvm pointer value Points to the dictionary object. """ dict_ty = types.DictType(keyty.instance_type, valty.instance_type) def codegen(context, builder, signature, args): [_, _, ptr] = args ctor = cgutils.create_struct_proxy(dict_ty) dstruct = ctor(context, builder) dstruct.data = ptr alloc_size = context.get_abi_sizeof( context.get_value_type(types.voidptr), ) dtor = _imp_dtor(context, builder.module) meminfo = context.nrt.meminfo_alloc_dtor( builder, context.get_constant(types.uintp, alloc_size), dtor, ) data_pointer = context.nrt.meminfo_data(builder, meminfo) data_pointer = builder.bitcast(data_pointer, ll_dict_type.as_pointer()) builder.store(ptr, data_pointer) dstruct.meminfo = meminfo return dstruct._getvalue() sig = dict_ty(keyty, valty, ptr) return sig, codegen
def typer(): return types.DictType(types.undefined, types.undefined)
def _initialise_dict(self, key, value): dcttype = types.DictType(typeof(key), typeof(value)) self._dict_type, self._opaque = self._parse_arg(dcttype)
from numba import int32, float64, boolean, int64, njit, types, typed import numpy as np # key and value types kv_ty = (types.unicode_type, float64) numba_model_spec = [ ('start_time', int32), ('number_of_timesteps', int32), ('deriv_idx', int64[:]), ('state_idx', int64[:]), ('init_vars', float64[:]), ('global_vars', float64[:]), ('historian_ix', int64), ('historian_data', float64[:, :]), ('path_variables', types.DictType(*kv_ty)), ('path_keys', types.ListType(types.unicode_type)), ('historian_max_size', int64), ('in_memory_history_correction', boolean), ('external_mappings_time', float64[:, :]), ('number_of_external_mappings', int64), ('external_mappings_numpy', float64[:, :, :]), ('external_df_idx', int64[:, :]), ('approximation_type', boolean[:]), ('is_external_data', boolean), ('max_external_t', int64), ('external_idx', int64[:]), ] @njit
import numpy as numpy from numba import jitclass from numba import types, typed obj_meta = (types.string, types.string) obj_vars = (types.string, types.int64) spec = [ ('bld_meta', types.DictType(*obj_meta)), ('bld_vars', types.DictType(*obj_vars)), ('end_uses', types.ListType(types.string)) ] @jitclass(spec) class BuildingNumba(object): def __init__(self): self.bld_meta = typed.Dict.empty(*obj_meta) self.bld_vars = typed.Dict.empty(*obj_vars) self.end_uses = typed.List.empty_list(types.string) def parse_building_meta(bld_obj, bld_numba): """Parse string type meta fields into numba class""" # will assume that all string type fields are meta data for key, val in bld_obj.items(): if isinstance(val, str): bld_numba.bld_meta[key] = val return bld_numba def parse_building_vars(bld_obj, bld_numba): """Parse numeric variables used for building calculation"""
h = np.bitwise_xor(h, ztb[row, col]) return h @jit(nopython=True) def hyper_hash(k, w, b): hash_values = [] for flip in (0, 1): for rot in (0, 1, 2, 3): hash_values.append(zhash(k, w, b, flip, rot)) hash_array = np.array(hash_values) return hash_array tt_ab = Dict.empty(key_type=types.uint64, value_type=types.DictType(types.unicode_type, types.float64)) # transposition_table = dict() tt_best_moves = {} @jit(nopython=True) def tt_lookup(k, w, b, tt): h = zhash(k, w, b) if h in tt: return tt[h] else: return None @jit(nopython=True) def tt_set(k, w, b, entry, tt):
# coding=utf-8 from numba import jitclass, float32 from numba import jitclass from numba import types from numba.typed import Dict @jitclass([('d', types.DictType(types.intp, types.float64))]) class DictWrapper(object): def __init__(self): d = Dict() d[1] = 1.2 self.d = d dw = DictWrapper() print(dw.d) def main(): if __name__ == '__main__': main()
masses = weights*m1 else: masses = np.repeat(total_mass/r_res,r_res) _orbital_inclination = 1.8 #https://arxiv.org/abs/1704.02444 I _loanode = 77 #https://arxiv.org/abs/1704.02444 big Omega _eccentricity = 0.1 #DES/SSBN07 classification e roids = np.array([]) for i in range(len(radii)): asteroid = planet(mass=masses[i],radius=0,eccentricity=_eccentricity,loanode=_loanode,orbital_inclination=_orbital_inclination, smaxis=radii[i]) roids = np.append(roids,asteroid) setattr(self,"asteroids_array",roids) ''' #specify specs dict_kv_ty = (types.unicode_type, types.float64) dicttype = types.DictType(*dict_kv_ty) kv_ty2 = (types.unicode_type, types.float64[:]) dicttype2 = types.DictType(*kv_ty2) with open('data.json') as file: dicts = [i for i in json.load(file)] if not "planet9" in dicts: dicts.append("planet9") spec = [(i, dicttype) for i in dicts] spec += [('asteroids', types.ListType(dicttype))] d = typed.Dict.empty(key_type=types.unicode_type, value_type=dicttype) spec += [('rawdata', typeof(d))] spec += [('asteroid_attributes', dicttype2)] def create_pdh(filename1, filename2=False):
import logging import time logger = logging.getLogger() from numba import njit, types, typed, typeof from numba.experimental import jitclass # http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#change-of-jitclass-location # The MinimizerIndexer jitclass requires explicit signatures for the container attributes # CITATION: https://stackoverflow.com/questions/56463147/how-to-specify-the-string-data-type-when-using-numba mm_kv_types = (types.unicode_type, types.ListType(types.int64)) mo_kv_types = (types.unicode_type, types.int64) # The clas spec tells numba how to pre-compile the instance attributes miSpec = [('targetString', types.unicode_type), ('w', types.int64), ('k', types.int64), ('t', types.int64), ('minimizerMap', types.DictType(*mm_kv_types)), ('minmerOccurrences', types.DictType(*mo_kv_types))] # Decorates a class for just-in-time pre-compiling by Numba @jitclass(miSpec) class MinimizerIndexer(object): """ Simple minimizer based substring-indexer. Please read: https://doi.org/10.1093/bioinformatics/bth408 Related to idea of min-hash index and other "sketch" methods. """ def __init__(self, targetString, w, k, t): """ The target string is a string/array of form "[ACGT]*".