def test_list_to_from_meminfo(self): """ Exercise listobject.{_as_meminfo, _from_meminfo} """ @njit def boxer(): l = listobject.new_list(int32) for i in range(10, 20): l.append(i) return listobject._as_meminfo(l) lsttype = types.ListType(int32) @njit def unboxer(mi): l = listobject._from_meminfo(mi, lsttype) return l[0], l[1], l[2], l[3], l[4], l[5], l[6], l[7], l[8], l[9] mi = boxer() self.assertEqual(mi.refcount, 1) received = list(unboxer(mi)) expected = list(range(10, 20)) self.assertEqual(received, expected)
def test_list_as_item_in_list(self): nested_type = types.ListType(types.int32) @njit def foo(): la = List.empty_list(nested_type) lb = List.empty_list(types.int32) lb.append(1) la.append(lb) return la expected = foo.py_func() got = foo() self.assertEqual(expected, got)
def test_objmode_typed_list(self): ret_type = types.ListType(types.int64) @njit def test4(): with objmode(res=ret_type): res = [1, 2] return res with self.assertRaises(TypeError) as raises: test4() # Note: in python3.6, the Generic[T] on typedlist is causing it to # format differently. self.assertRegex( str(raises.exception), (r"can't unbox a <class 'list'> " r"as a (<class ')?numba.typed.typedlist.List('>)?"), )
def test_instcombine_effect(self): # Without instcombine running ahead of refprune, the IR has refops that # are trivially prunable (same BB) but the arguments are obfuscated # through aliases etc. The follow case triggers this situation as the # typed.List has a structproxy call for computing `len` and getting the # base pointer for use in iteration. def sum_sqrt_list(lst): acc = 0.0 for item in lst: acc += np.sqrt(item) return acc llvm_ir = self.gen_ir(sum_sqrt_list, (types.ListType(types.float64), ), fastmath=True) self.assertIn("vector.body", llvm_ir) self.assertIn("llvm.loop.isvectorized", llvm_ir)
def sdc_indexes_build_map_positions_ovld(self): indexer_dtype = self.dtype indexer_value_type = types.ListType(types.int64) def sdc_indexes_build_map_positions_impl(self): indexer_map = Dict.empty(indexer_dtype, indexer_value_type) for i in range(len(self)): val = self[i] index_list = indexer_map.get(val, None) if index_list is None: indexer_map[val] = List.empty_list(types.int64) indexer_map[val].append(i) else: index_list.append(i) return indexer_map return sdc_indexes_build_map_positions_impl
def _make_list(typingctx, itemty, ptr): """Make a list struct with the given *ptr* Parameters ---------- itemty: Type Type of the item. ptr : llvm pointer value Points to the list object. """ list_ty = types.ListType(itemty.instance_type) def codegen(context, builder, signature, args): [_, ptr] = args ctor = cgutils.create_struct_proxy(list_ty) lstruct = ctor(context, builder) lstruct.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_list_type.as_pointer()) builder.store(ptr, data_pointer) lstruct.meminfo = meminfo return lstruct._getvalue() sig = list_ty(itemty, ptr) return sig, codegen
import numpy as np from numba.experimental import jitclass from numba import njit, types, typed, prange import z_helper as h import time from numba.core.errors import NumbaTypeSafetyWarning import warnings warnings.simplefilter('ignore', category=NumbaTypeSafetyWarning) spec = [ ("layer_sizes", types.ListType(types.int64)), ("layer_activations", types.ListType( types.FunctionType(types.float64[:, ::1](types.float64[:, ::1], types.boolean)))), ("weights", types.ListType(types.float64[:, ::1])), ("biases", types.ListType(types.float64[:, ::1])), ("layer_outputs", types.ListType(types.float64[:, ::1])), ("learning_rate", types.float64), ] @jitclass(spec) class NeuralNetwork: def __init__(self, layer_sizes, layer_activations, weights, biases, layer_outputs, learning_rate): self.layer_sizes = layer_sizes self.layer_activations = layer_activations self.weights = weights
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 def step_aproximation(t, time_array, data_array):
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"""
def typer(): return types.ListType(types.undefined)
def _initialise_list(self, item): lsttype = types.ListType(typeof(item)) self._list_type, self._opaque = self._parse_arg(lsttype)
from numba.experimental import jitclass node_type = Node.class_type.instance_type spec = OrderedDict() spec['layer_heights'] = float64[:] spec['layer_densities'] = float64[:] spec['layer_temperatures'] = float64[:] spec['layer_liquid_water_content'] = float64[:] spec['layer_ice_fraction'] = optional(float64[:]) spec['number_nodes'] = intp spec['new_snow_height'] = float64 spec['new_snow_timestamp'] = float64 spec['old_snow_timestamp'] = float64 spec['grid'] = types.ListType(node_type) @jitclass(spec) class Grid: def __init__(self, layer_heights, layer_densities, layer_temperatures, layer_liquid_water_content, layer_ice_fraction=None, new_snow_height=None, new_snow_timestamp=None, old_snow_timestamp=None): """ The Grid-class controls the numerical mesh.
a = 0.0003102 T_inf = 11.9433 T_sup = 40 else: return mean_rate = a * self.T * (self.T - T_inf) * np.sqrt(T_sup - self.T) mean = 0.2789 * density**0.2789 / mean_rate self.stage_length = np.random.gamma(mean * 10, 0.1) # In[22]: mosquito_type = typeof(Mosquito('egg', 25, 0)) mosquito_list_type = typeof([Mosquito('egg', 25, 0)]) specs = [('egg_numbers', types.ListType(int64)), ('larva_numbers', types.ListType(int64)), ('pupa_numbers', types.ListType(int64)), ('naive_numbers', types.ListType(int64)), ('adult_numbers', types.ListType(int64)), ('total_numbers', types.ListType(int64)), ('Temps', types.ListType(float64)), ('T', float64), ('mosquitos', mosquito_list_type)] @jitclass(specs) class Population: def __init__(self, egg_no, larva_no, pupa_no, naive_no, adult_no, Temps): self.egg_numbers = typed.List([egg_no]) self.larva_numbers = typed.List([larva_no]) self.pupa_numbers = typed.List([pupa_no])
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): with open(filename1) as file: data = json.load(file) tojitclass = typed.Dict() for i in data: temp = typed.Dict() for j in data[i]: temp[j] = data[i][j] tojitclass[i] = temp if filename2:
""" import array import sys import numpy import pysam import argparse 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