Exemple #1
0
    def test_default_args_starargs_and_keyonly(self):
        spec = [('x', int32), ('y', int32), ('z', int32),
                ('args', types.UniTuple(int32, 2)), ('a', int32)]

        with self.assertRaises(errors.UnsupportedError) as raises:
            jitclass(spec)(TestClass2)

        msg = "VAR_POSITIONAL argument type unsupported"
        self.assertIn(msg, str(raises.exception))
Exemple #2
0
    def test_default_args_starargs_and_keyonly(self):
        spec = [('x', int32),
                ('y', int32),
                ('z', int32),
                ('args', types.UniTuple(int32, 2)),
                ('a', int32)]

        with self.assertRaises(errors.UnsupportedError) as raises:
            jitclass(spec)(TestClass2)

        msg = "VAR_POSITIONAL argument type unsupported"
        self.assertIn(msg, str(raises.exception))
Exemple #3
0
    def test_globals(self):
        class Mine(object):
            constant = 123

            def __init__(self):
                pass

        with self.assertRaises(TypeError) as raises:
            jitclass(())(Mine)

        self.assertEqual(str(raises.exception),
                         "class members are not yet supported: constant")
    def test_import_warnings(self):
        class Test:
            def __init__(self):
                pass

        with warnings.catch_warnings(record=True) as ws:
            numba.experimental.jitclass([])(Test)
            self.assertEqual(len(ws), 0)

            numba.jitclass([])(Test)
            self.assertEqual(len(ws), 1)
            self.assertIs(ws[0].category, errors.NumbaDeprecationWarning)
            self.assertIn("numba.experimental.jitclass", ws[0].message.msg)
Exemple #5
0
    def test_globals(self):

        class Mine(object):
            constant = 123

            def __init__(self):
                pass

        with self.assertRaises(TypeError) as raises:
            jitclass(())(Mine)

        self.assertEqual(str(raises.exception),
                         "class members are not yet supported: constant")
Exemple #6
0
 def make_jitclass_element(self):
     spec = [
         ('many', types.float64[:]),
         ('scalar', types.float64),
     ]
     JCItem = jitclass(spec)(Item)
     return JCItem
Exemple #7
0
    def test_default_args_keyonly(self):
        spec = [('x', int32), ('y', int32), ('z', int32), ('a', int32)]

        TestClass = jitclass(spec)(TestClass1)

        tc = TestClass(2, 3)
        self.assertEqual(tc.x, 2)
        self.assertEqual(tc.y, 3)
        self.assertEqual(tc.z, 1)
        self.assertEqual(tc.a, 5)

        tc = TestClass(y=4, x=2, a=42, z=100)
        self.assertEqual(tc.x, 2)
        self.assertEqual(tc.y, 4)
        self.assertEqual(tc.z, 100)
        self.assertEqual(tc.a, 42)

        tc = TestClass(y=4, x=2, a=42)
        self.assertEqual(tc.x, 2)
        self.assertEqual(tc.y, 4)
        self.assertEqual(tc.z, 1)
        self.assertEqual(tc.a, 42)

        tc = TestClass(y=4, x=2)
        self.assertEqual(tc.x, 2)
        self.assertEqual(tc.y, 4)
        self.assertEqual(tc.z, 1)
        self.assertEqual(tc.a, 5)
Exemple #8
0
    def test_spec_errors(self):
        spec1 = [('x', int), ('y', float32[:])]
        spec2 = [(1, int32), ('y', float32[:])]

        class Test(object):
            def __init__(self):
                pass

        with self.assertRaises(TypeError) as raises:
            jitclass(spec1)(Test)
        self.assertIn("spec values should be Numba type instances",
                      str(raises.exception))
        with self.assertRaises(TypeError) as raises:
            jitclass(spec2)(Test)
        self.assertEqual(str(raises.exception),
                         "spec keys should be strings, got 1")
def fjit(fun):
    'just-in-time compile a function by wrapping it in a singleton class'
    
    from numba import jitclass
    import time
    
    # the function is jitted first
    jitted_fun = njit(fun)

    # Generate a random class name like 'Singleton_Sat_Jan__2_18_08_32_2016'
    classname = 'Singleton_' + time.asctime().replace(' ','_').replace(':','_')
    
    # programmatically create a class equivalent to :
    # class Singleton_Sat_Jan__2_18_08_32_2016:
    #     def __init__(self): pass
    #     def value(self, x): return fj(x)
    
    def __init__(self): pass
    def value(self, x): return jitted_fun(x)
    SingletonClass = type(classname, (object,), {'__init__': __init__, 'value': value})
    
    # jit compile the class
    # spec is [] since we don't store attributes
    spec = []
    sc = jitclass(spec)(SingletonClass)
    
    # return a unique instance of the class
    return sc()
Exemple #10
0
 def make_jitclass_element(self):
     spec = [
         ('many', types.float64[:]),
         ('scalar', types.float64),
     ]
     JCItem = jitclass(spec)(Item)
     return JCItem
Exemple #11
0
    def test_default_args_keyonly(self):
        spec = [('x', int32),
                ('y', int32),
                ('z', int32),
                ('a', int32)]

        TestClass = jitclass(spec)(TestClass1)

        tc = TestClass(2, 3)
        self.assertEqual(tc.x, 2)
        self.assertEqual(tc.y, 3)
        self.assertEqual(tc.z, 1)
        self.assertEqual(tc.a, 5)

        tc = TestClass(y=4, x=2, a=42, z=100)
        self.assertEqual(tc.x, 2)
        self.assertEqual(tc.y, 4)
        self.assertEqual(tc.z, 100)
        self.assertEqual(tc.a, 42)

        tc = TestClass(y=4, x=2, a=42)
        self.assertEqual(tc.x, 2)
        self.assertEqual(tc.y, 4)
        self.assertEqual(tc.z, 1)
        self.assertEqual(tc.a, 42)

        tc = TestClass(y=4, x=2)
        self.assertEqual(tc.x, 2)
        self.assertEqual(tc.y, 4)
        self.assertEqual(tc.z, 1)
        self.assertEqual(tc.a, 5)
Exemple #12
0
    def test_spec_errors(self):
        spec1 = [('x', int), ('y', float32[:])]
        spec2 = [(1, int32), ('y', float32[:])]

        class Test(object):
            def __init__(self):
                pass

        with self.assertRaises(TypeError) as raises:
            jitclass(spec1)(Test)
        self.assertIn("spec values should be Numba type instances",
                      str(raises.exception))
        with self.assertRaises(TypeError) as raises:
            jitclass(spec2)(Test)
        self.assertEqual(str(raises.exception),
                         "spec keys should be strings, got 1")
Exemple #13
0
    def test_user_deleter_error(self):
        class Foo(object):
            def __init__(self):
                pass

            @property
            def value(self):
                return 1

            @value.deleter
            def value(self):
                pass

        with self.assertRaises(TypeError) as raises:
            jitclass([])(Foo)
        self.assertEqual(str(raises.exception),
                         "deleter is not supported: value")
Exemple #14
0
    def test_user_deleter_error(self):
        class Foo(object):
            def __init__(self):
                pass

            @property
            def value(self):
                return 1

            @value.deleter
            def value(self):
                pass

        with self.assertRaises(TypeError) as raises:
            jitclass([])(Foo)
        self.assertEqual(str(raises.exception),
                         "deleter is not supported: value")
Exemple #15
0
    def test_name_shadowing_error(self):
        class Foo(object):
            def __init__(self):
                pass

            @property
            def my_property(self):
                pass

            def my_method(self):
                pass

        with self.assertRaises(NameError) as raises:
            jitclass([('my_property', int32)])(Foo)
        self.assertEqual(str(raises.exception), 'name shadowing: my_property')

        with self.assertRaises(NameError) as raises:
            jitclass([('my_method', int32)])(Foo)
        self.assertEqual(str(raises.exception), 'name shadowing: my_method')
Exemple #16
0
    def test_jitclass(self):
        with override_config('DISABLE_JIT', True):
            with forbid_codegen():
                SimpleJITClass = jitclass(simple_class_spec)(SimpleClass)

                obj = SimpleJITClass()
                self.assertPreciseEqual(obj.h, 5)

                cfunc = jit(nopython=True)(simple_class_user)
                self.assertPreciseEqual(cfunc(obj), 5)
Exemple #17
0
    def test_name_shadowing_error(self):
        class Foo(object):
            def __init__(self):
                pass

            @property
            def my_property(self):
                pass

            def my_method(self):
                pass

        with self.assertRaises(NameError) as raises:
            jitclass([('my_property', int32)])(Foo)
        self.assertEqual(str(raises.exception), 'name shadowing: my_property')

        with self.assertRaises(NameError) as raises:
            jitclass([('my_method', int32)])(Foo)
        self.assertEqual(str(raises.exception), 'name shadowing: my_method')
    def test_jitclass(self):
        with override_config('DISABLE_JIT', True):
            with forbid_codegen():
                SimpleJITClass = jitclass(simple_class_spec)(SimpleClass)

                obj = SimpleJITClass()
                self.assertPreciseEqual(obj.h, 5)

                cfunc = jit(nopython=True)(simple_class_user)
                self.assertPreciseEqual(cfunc(obj), 5)
Exemple #19
0
    def test_annotations(self):
        """
        Methods with annotations should compile fine (issue #1911).
        """
        from .annotation_usecases import AnnotatedClass

        spec = {'x': int32}
        cls = jitclass(spec)(AnnotatedClass)

        obj = cls(5)
        self.assertEqual(obj.x, 5)
        self.assertEqual(obj.add(2), 7)
Exemple #20
0
    def test_annotations(self):
        """
        Methods with annotations should compile fine (issue #1911).
        """
        from .annotation_usecases import AnnotatedClass

        spec = {'x': int32}
        cls = jitclass(spec)(AnnotatedClass)

        obj = cls(5)
        self.assertEqual(obj.x, 5)
        self.assertEqual(obj.add(2), 7)
Exemple #21
0
def _make_jitclass_for_type(base, nbtype):
	"""
	Apply jitclass to one of the base classes with the signature for the given
	array data type.
	"""

	spec = [
		('array', nbtype[:]),
		('arraysize', nb.intp),
		('size', nb.intp),
		('upper', nb.boolean),
		('diag_val', nbtype)
	]

	return nb.jitclass(spec)(base)
Exemple #22
0
 def create_tree(self, subpattern, i, j, k, parent, plen):
     pNode = numba.jitclass(spec)(ParallelNode)
     pNode.start = i
     pNode.end = j
     left = math.ceil((k + 1) / 2.0)
     pNode.parent = parent
     pNode.error = k
     pNode.pattern = subpattern
     if k == 0:
         self.leaves.append(pNode)
     else:
         lk = math.floor((left * k) / (k + 1))
         self.create_tree(subpattern[:left * plen], i, i + left * plen - 1,
                          lk, pNode, plen)
         rk = math.floor(((k + 1 - left) * k) / (k + 1))
         self.create_tree(subpattern[left * plen:], i + left * plen, j, rk,
                          pNode, plen)
Exemple #23
0
def get_shuffle_meta_class(arr_t):
    count_arr_typ = types.Array(types.int32, 1, 'C')
    if isinstance(arr_t, types.Array):
        spec = [
            ('send_counts', count_arr_typ),
            ('recv_counts', count_arr_typ),
            ('send_buff', arr_t),
            ('out_arr', arr_t),
            ('n_send', types.intp),
            ('n_out', types.intp),
            ('send_disp', count_arr_typ),
            ('recv_disp', count_arr_typ),
            ('tmp_offset', count_arr_typ),
            ('send_counts_char', types.none),
            ('recv_counts_char', types.none),
            ('send_arr_lens', types.none),
            ('send_arr_chars', types.none),
            ('send_disp_char', types.none),
            ('recv_disp_char', types.none),
            ('tmp_offset_char', types.none),
            ('send_arr_chars_arr', types.none),
        ]
    else:
        spec = [
            ('send_counts', count_arr_typ),
            ('recv_counts', count_arr_typ),
            ('send_buff', types.none),
            ('out_arr', arr_t),
            ('n_send', types.intp),
            ('n_out', types.intp),
            ('send_disp', count_arr_typ),
            ('recv_disp', count_arr_typ),
            ('tmp_offset', count_arr_typ),
            ('send_counts_char', count_arr_typ),
            ('recv_counts_char', count_arr_typ),
            ('send_arr_lens', types.Array(types.uint32, 1, 'C')),
            ('send_arr_chars', types.voidptr),
            ('send_disp_char', count_arr_typ),
            ('recv_disp_char', count_arr_typ),
            ('tmp_offset_char', count_arr_typ),
            ('send_arr_chars_arr', types.Array(types.uint8, 1, 'C')),
        ]

    ShuffleMetaCL = numba.jitclass(spec)(ShuffleMeta)
    return ShuffleMetaCL
Exemple #24
0
def new_numba(N):
    import numba
    spec = [
        ('N', numba.int32),
        ('D', numba.float64),
        ('vz', numba.float64),
        ('k', numba.float64),
        ('Cf', numba.float64),
        ('z0', numba.float64),
        ('zf', numba.float64),
        ('h', numba.float64),
    ]
    Numba_PFR = numba.jitclass(spec)(pfr)
    jitted_pfr_model = numba.jit(model_pfr_shared_direct, nopython=True)
    numba_pfr = Numba_PFR(N)
    numba_args = setup_base(N)
    numba_args[3] = numba_pfr
    dasslcy.solve(jitted_pfr_model, *numba_args, share_res=1)
    return [jitted_pfr_model] + numba_args
Exemple #25
0
def test():  # pragma: no cover
    import time
    #SortStateCL = SortState #numba.jitclass(spec)(SortState)
    # warm up
    t1 = time.time()
    T = np.ones(3)
    data = (
        np.arange(3),
        np.ones(3),
    )
    spec = [
        ('key_arrs', numba.types.Tuple((numba.float64[::1], ))),
        ('aLength', numba.intp),
        ('minGallop', numba.intp),
        ('tmpLength', numba.intp),
        ('tmp', numba.types.Tuple((numba.float64[::1], ))),
        ('stackSize', numba.intp),
        ('runBase', numba.int64[:]),
        ('runLen', numba.int64[:]),
        ('data', numba.typeof(data)),
        ('tmp_data', numba.typeof(data)),
    ]
    SortStateCL = numba.jitclass(spec)(SortState)
    sortState = SortStateCL((T, ), 3, data)
    sort(sortState, (T, ), 0, 3, data)
    print("compile time", time.time() - t1)
    n = 210000
    np.random.seed(2)
    data = (np.arange(n), np.random.ranf(n))
    A = np.random.ranf(n)
    df = pd.DataFrame({'A': A, 'B': data[0], 'C': data[1]})
    t1 = time.time()
    #B = np.sort(A)
    df2 = df.sort_values('A', inplace=False)
    t2 = time.time()
    sortState = SortStateCL((A, ), n, data)
    sort(sortState, (A, ), 0, n, data)
    print("HPAT", time.time() - t2, "Numpy", t2 - t1)
    # print(df2.B)
    # print(data)
    np.testing.assert_almost_equal(data[0], df2.B.values)
    np.testing.assert_almost_equal(data[1], df2.C.values)
Exemple #26
0
def get_local_sort_func(key_typ, data_tup_typ):
    sort_state_spec = [
        ('key_arrs', to_string_list_typ(key_typ)),
        ('aLength', numba.intp),
        ('minGallop', numba.intp),
        ('tmpLength', numba.intp),
        ('tmp', to_string_list_typ(key_typ)),
        ('stackSize', numba.intp),
        ('runBase', numba.int64[:]),
        ('runLen', numba.int64[:]),
        ('data', to_string_list_typ(data_tup_typ)),
        ('tmp_data', to_string_list_typ(data_tup_typ)),
    ]
    SortStateCL = numba.jitclass(sort_state_spec)(hpat.timsort.SortState)

    # XXX: make sure function is not using old SortState
    local_sort.__globals__['SortState'] = SortStateCL
    _local_sort_f = numba.njit(local_sort)
    _local_sort_f.compile(signature(types.none, key_typ, data_tup_typ))
    return _local_sort_f
Exemple #27
0
 def make_jitclass_container(self):
     spec = {
         'data': types.List(dtype=types.List(types.float64[::1])),
     }
     JCContainer = jitclass(spec)(Container)
     return JCContainer
Exemple #28
0
from numba import jitclass, float32


# 2D point class with constructor
class Point2D(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return '(x=' + str(self.x) + ', y=' + str(self.y) + ')\n'


# Here we are creating the instance of the class Point2D
p1 = Point2D(10.0, 10.0)
print(p1)  # Printing invokes __str__

# Here we are creating the instance of Point2D which is compiled by Numba
compiled_class = jitclass([('x', float32),
                           ('y', float32)])(Point2D)  # Compiling in this line
p2 = compiled_class(10.0, 10.0)  # Creating the instance of compiled class
print(p2)  # Invocation of __str__ is not implemented in Numba
print('(x=' + str(p2.x) + ', y=' + str(p2.y) +
      ')')  # Workaround to print representation of the object
Exemple #29
0
def Vector(numba_type):
    """Generates an instance of a dynamically resized vector numba jitclass."""
    
    if numba_type in Vector._saved_type:
        return Vector._saved_type[numba_type]
    
    class _Vector:
        """Dynamically sized arrays in nopython mode."""
        
        def __init__(self, n):
            """Initialize with space enough to hold n garbage values."""
            self.n = n
            self.m = n
            self.full_arr = np.empty(self.n, dtype=numba_type)
        
        @property
        def size(self):
            """The number of valid values."""
            return self.n
        
        @property
        def arr(self):
            """Return the subarray."""
            return self.full_arr[:self.n]
        
        @property
        def last(self):
            """The last element in the array."""
            if self.n:
                return self.full_arr[self.n-1]
            else:
                raise IndexError(
                    "This numbavec has no elements: cannot return 'last'.")
        
        @property
        def first(self):
            """The first element in the array."""
            if self.n:
                return self.full_arr[0]
            else:
                raise IndexError(
                    "This numbavec has no elements: cannot return 'first'.")
        
        def clear(self):
            """Remove all elements from the array."""
            self.n = 0
            return self
        
        def extend(self, other):
            """Add the contents of a numpy array to the end of this Vector.
            
            Arguments
            ---------
            other : 1d array
                The values to add to the end.
            """
            n_required = self.size + other.size
            self.reserve(n_required)
            self.full_arr[self.size:n_required] = other
            self.n = n_required
            return self
        
        def append(self, val):
            """Add a value to the end of the Vector, expanding it if necessary."""
            if self.n == self.m:
                self._expand()
            self.full_arr[self.n] = val
            self.n += 1
            return self
        
        def reserve(self, n):
            """Reserve a n elements in the underlying array.
            
            Arguments
            ---------
            n : int
                The number of elements to reserve
            
            Reserving n elements ensures no resize overhead when appending up
            to size n-1 .
            """
            if n > self.m:  # Only change size if we are
                temp = np.empty(int(n), dtype=numba_type)
                temp[:self.n] = self.arr
                self.full_arr = temp
                self.m = n
            return self
        
        def consolidate(self):
            """Remove unused memory from the array."""
            if self.n < self.m:
                self.full_arr = self.arr.copy()
                self.m = self.n
            return self
        
        def __array__(self):
            """Array inteface for Numpy compatibility."""
            return self.full_arr[:self.n]
        
        def _expand(self):
            """Internal function that handles the resizing of the array."""
            self.m = int(self.m * _EXPANSION_CONSTANT_) + 1
            temp = np.empty(self.m, dtype=numba_type)
            temp[:self.n] = self.full_arr[:self.n]
            self.full_arr = temp
        
        def set_to(self, arr):
            """Make this vector point to another array of values.
            
            Arguments
            ---------
            arr : 1d array
                Array to set this vector to. After this operation, self.arr
                will be equal to arr. The dtype of this array must be the 
                same dtype as used to create the vector. Cannot be a readonly
                vector.
            """
            self.full_arr = arr
            self.n = self.m = arr.size
        
        def set_to_copy(self, arr):
            """Set this vector to an array, copying the underlying input.
            
            Arguments
            ---------
            arr : 1d array
                Array to set this vector to. After this operation, self.arr
                will be equal to arr. The dtype of this array must be the 
                same dtype as used to create the vector.
            """
            self.full_arr = arr.copy()
            self.n = self.m = arr.size
    
    
    if numba_type not in Vector._saved_type:
        spec = [("n", numba.uint64),
                ("m", numba.uint64),
                ("full_arr", numba_type[:])]
        Vector._saved_type[numba_type] = numba.jitclass(spec)(_Vector)
    
    return Vector._saved_type[numba_type]
Exemple #30
0
def entry_point(argv):

    args = {'-i': '', '-k': '50', '-n': '1000000', '-r': '', '-d': ''}
    N = len(argv)

    for i in xrange(1, N):
        k = argv[i]
        if k in args:
            v = argv[i + 1]
            args[k] = v
        elif k[:2] in args and len(k) > 2:
            args[k[:2]] = k[2:]
        else:
            continue

    # bkt, the breakpoint
    qry, kmer, Ns, bkt, dbs = args['-i'], int(args['-k']), int(eval(
        args['-n'])), args['-r'], args['-d']
    if not qry:
        seq = 'ACCCATCGGGCTAAACCCCCCCCCCGATCGATCGAC'
        #seq = 'AAAAAAAAAAGAAAAAAAAAATAAAAAAAAAACAAAAAAAAAA'
        seq = 'AAAACCCCAATACCCCATAACCCC'
        kmer = 4
        a0 = [(k2n_(seq[elem:elem + kmer]), seq[elem - 1:elem],
               seq[elem + kmer:elem + kmer + 1])
              for elem in xrange(len(seq) - kmer + 1)]
        a1 = [elem[:] for elem in seq2ns_(seq, kmer)]
        print(a0 == a1)
        print(a0[-105:])
        print(a1[-105:])

        # test
        try:
            N = int(eval(sys.argv[1]))
        except:
            N = 10**7

        mkey = 5
        print('test', N)

        # the spec for jitclass
        spec = {}
        spec['capacity'] = nb.int64
        spec['load'] = nb.float32
        spec['size'] = nb.int64
        spec['ksize'] = nb.int64
        spec['ktype'] = nb.uint64
        spec['keys'] = spec['ktype'][:]
        spec['vtype'] = nb.uint32
        spec['values'] = spec['vtype'][:]
        spec['counts'] = nb.uint8[:]

        oakht_jit = jitclass(spec)(oakht)
        clf = oakht_jit(capacity=N,
                        ksize=mkey,
                        ktype=spec['ktype'],
                        vtype=spec['vtype'])
        #clf = oakht_jit(capacity=int(N * 1.75), ksize=mkey, ktype=spec['ktype'], vtype=spec['vtype'])

        print('initial finish')

        @njit
        def oa_test(N, k, clf):
            x = np.random.randint(0, N, N)
            for i in range(N - k + 1):
                clf.push(x[i:i + k], i)
                #clf.push(np.random.randint(0, N, k), i)

            flag = 0
            for i in range(N - k + 1):
                #clf.push(x[i:i+k], i)
                #clf.push(np.random.randint(0, N, k), i)
                if not clf.has_key(x[i:i + k]):
                    flag += 1

            print('x err', flag)

            # check random generated array
            flag = 0
            y = np.random.randint(0, N, N)
            for i in range(N - k + 1):
                if clf.has_key(y[i:i + k]):
                    flag += 1

            print('y err', flag)

            #for key in clf.iterkeys():
            #    print('key is', key)

        print('numba version')
        oa_test(N, mkey, clf)
        raise SystemExit()

    if dbs:
        #print('recover from', bkt)
        # convert sequence to path and build the graph
        dct = seq2graph(qry,
                        kmer=kmer,
                        bits=5,
                        Ns=Ns,
                        saved=dbs,
                        hashfunc=oamkht)
    else:
        # build the rdbg, convert sequence to path, and build the graph
        #dct = seq2dbg(qry, kmer, 5, Ns, rec=bkt)
        kmer_dict = seq2rdbg(qry, kmer, 5, Ns, rec=bkt)
        #for i in kmer_dict:
        #    print('kmer dict size', qry, len(kmer_dict), kmer, n2k_(i, kmer, 5))
        #    if bkt:
        #        print('bkt is', bkt)
        dct = seq2graph(qry,
                        kmer=kmer,
                        bits=5,
                        Ns=Ns,
                        kmer_dict=kmer_dict,
                        hashfunc=oamkht)

    return 0
Exemple #31
0
    def init(self, inputset, parameters):

        # base class 'init (...)' method
        Model.init(self, inputset, parameters)

        # get initial time and make sure it is a float
        self.t = numpy.float64(inputset)

        # store inputset and parameters
        self.parameters = parameters

        # create a precipitation instance
        if self.numbify:
            self.h = numba.jitclass(Precipitation_numba_spec)(Precipitation)(
                *self.precipitation_coefficients)
        else:
            self.h = Precipitation(*self.precipitation_coefficients)

        # generate random initial values for \xi and y using the observed (uncertain) initial values and the associated error model
        initial = self.initial.draw(self.rng)
        if self.verbosity >= 2:
            print('Drawing initial values:')
            print(initial)

        # generate a random initial value for \xi with observed value as the mean and parameter r'\sigma_\xi' as the spread
        xi0 = initial[r'$\xi$']

        # determine tau
        if self.tau is not None:
            tau = self.tau
        else:
            tau = numpy.float64(self.parameters[r'$\tau$'])

        # create and initialize OU process
        if self.numbify:
            self.ou = numba.jitclass(OrnsteinUhlenbeck_numba_spec)(
                OrnsteinUhlenbeck)(tau)
        else:
            self.ou = OrnsteinUhlenbeck(tau)
        self.ou.init(self.t, numpy.float64(xi0))

        # initialize wastewater process
        zeta = numpy.array(
            [-self.parameters[r'$-\zeta_1$'], -self.parameters[r'$-\zeta_2$']],
            dtype=numpy.float64)
        chi = numpy.array(
            [-self.parameters[r'$-\chi_1$'], self.parameters[r'$\chi_2$']],
            dtype=numpy.float64)
        if self.numbify:
            self.w = numba.jitclass(Wastewater_numba_spec)(Wastewater)(zeta,
                                                                       chi)
        else:
            self.w = Wastewater(zeta, chi)

        # generate a random initial value for y with observed value as the mean and parameter r'\sigma_y' as the spread
        y0 = initial['y']
        if self.verbosity >= 2:
            print('y0: ', y0)

        # compute initial S from initial discharge
        self.S = numpy.float64(self.parameters['K'] *
                               (y0 - self.w.evaluate(self.t)))

        # initialize the the right hand side 'dS' of the ODE and its args
        if self.numbify:
            self.dS = numba.njit(dS)
        else:
            self.dS = dS
        self.dS_args = (self.parameters['A'], self.h, self.ou,
                        self.parameters[r'$x_{gw}$'], self.parameters['K'])

        # set integrator
        integrate = None
        if self.integrator == 'FE':
            integrate = integrate_FE
        if self.integrator == 'RKSSP':
            integrate = integrate_RKSSP
        assert integrate, ' :: ERROR: The requested integrator is not implemented.'
        if self.numbify:
            self.integrate = numba.njit(integrate)
        else:
            self.integrate = integrate
Exemple #32
0
 def create_my_class(value):
     cls = jitclass([('value', typeof(value))])(MyClass)
     return cls(value)
import numpy as np
from numba import jit, njit, jitclass
import gate

spec = [
    ('representation', 

@jitclass(spec)
class TensorGate(gate.Gate):
    '''Implements Gate using numpy tensors under the hood.'''

    def __init__(self, gate_matrix, sites, N, name=None):
        if any([x >= N for x in sites]):
            raise ValueError('Acts on site outside qubit range')
        self.dim = int(len(gate_matrix.shape)/2)
        gate.Gate.__init__(self, gate_matrix, sites, N, name)
	
    @njit()
    def compose_with(self, other):
        '''Uses np.tensordot to multiply with another tensor.

        This multiplication process involves summing over the correct indices to match how matrix
        multiplication should work. Furthermore, the process requires reordering the tensor
        components afterwards so the tensor continues to act on the proper sites.'''

        sums1 = []
        sums2 = []
        sites1 = self.sites.copy()
        sites2 = other.sites.copy()

        index = {}
Exemple #34
0
 def make_jitclass_container(self):
     spec = {
         'data': types.List(dtype=types.List(types.float64[::1])),
     }
     JCContainer = jitclass(spec)(Container)
     return JCContainer
Exemple #35
0
def data_alloc_shuffle_metadata_overload(data_t, n_pes_t, is_contig_t):
    count = data_t.count
    spec_null = [
        ('send_counts', types.none),
        ('recv_counts', types.none),
        ('send_buff', types.none),
        ('out_arr', types.none),
        ('n_send', types.none),
        ('n_out', types.none),
        ('send_disp', types.none),
        ('recv_disp', types.none),
        ('tmp_offset', types.none),
        ('send_counts_char', types.none),
        ('recv_counts_char', types.none),
        ('send_arr_lens', types.none),
        ('send_arr_chars', types.none),
        ('send_disp_char', types.none),
        ('recv_disp_char', types.none),
        ('tmp_offset_char', types.none),
        ('send_arr_chars_arr', types.none),
    ]
    count_arr_typ = types.Array(types.int32, 1, 'C')
    spec_str = [
        ('send_counts', types.none),
        ('recv_counts', types.none),
        ('send_buff', types.none),
        ('out_arr', string_array_type),
        ('n_send', types.none),
        ('n_out', types.none),
        ('send_disp', types.none),
        ('recv_disp', types.none),
        ('tmp_offset', types.none),
        ('send_counts_char', count_arr_typ),
        ('recv_counts_char', count_arr_typ),
        ('send_arr_lens', types.Array(types.uint32, 1, 'C')),
        ('send_arr_chars', types.voidptr),
        ('send_disp_char', count_arr_typ),
        ('recv_disp_char', count_arr_typ),
        ('tmp_offset_char', count_arr_typ),
        ('send_arr_chars_arr', types.Array(types.uint8, 1, 'C')),
    ]
    ShuffleMetaStr = numba.jitclass(spec_str)(ShuffleMeta)

    glbls = {
        'ShuffleMetaStr': ShuffleMetaStr,
        'np': np,
        'get_data_ptr': get_data_ptr,
        'num_total_chars': num_total_chars,
        'get_ctypes_ptr': get_ctypes_ptr
    }
    for i, typ in enumerate(data_t.types):
        if isinstance(typ, types.Array):
            spec_null[2] = ('send_buff', typ)
            spec_null[3] = ('out_arr', typ)
            ShuffleMetaCL = numba.jitclass(spec_null.copy())(ShuffleMeta)
            glbls['ShuffleMeta_{}'.format(i)] = ShuffleMetaCL

    func_text = "def f(data, n_pes, is_contig):\n"
    for i in range(count):
        typ = data_t.types[i]
        func_text += "  arr = data[{}]\n".format(i)
        if isinstance(typ, types.Array):
            func_text += "  send_buff = arr\n"
            func_text += (
                "  meta_{} = ShuffleMeta_{}(None, None, send_buff, arr, None, None, None,"
                " None, None, None, None, None, None, None, None, None, None)\n"
            ).format(i, i)
        else:
            assert typ == string_array_type
            func_text += "  send_counts_char = np.zeros(n_pes, np.int32)\n"
            func_text += "  recv_counts_char = np.empty(n_pes, np.int32)\n"
            func_text += "  send_arr_lens = np.empty(1, np.uint32)\n"
            func_text += "  if is_contig:\n"
            func_text += "    send_arr_lens = np.empty(len(arr), np.uint32)\n"
            func_text += "  send_arr_chars = get_ctypes_ptr(get_data_ptr(arr))\n"
            func_text += "  tmp_offset_char = send_counts_char\n"
            func_text += "  send_arr_chars_arr = np.empty(1, np.uint8)\n"
            func_text += "  if not is_contig:\n"
            func_text += "    tmp_offset_char = np.zeros(n_pes, np.int32)\n"
            func_text += (
                "  meta_{} = ShuffleMetaStr(None, None, None, arr, None, None,"
                "None, None, None, send_counts_char, recv_counts_char, send_arr_lens,"
                " send_arr_chars, send_counts_char, recv_counts_char, tmp_offset_char, send_arr_chars_arr)\n"
            ).format(i)
    func_text += "  return ({}{})\n".format(
        ','.join(['meta_{}'.format(i) for i in range(count)]),
        "," if count == 1 else "")

    loc_vars = {}
    exec(func_text, glbls, loc_vars)
    alloc_impl = loc_vars['f']
    return alloc_impl
Exemple #36
0
    def write_many(self, b, count):
        self.data[self.loc:self.loc + count] = b
        self.loc += count

    def tell(self):
        return self.loc

    def so_far(self):
        """ In write mode, the data we have gathered until now
        """
        return self.data[:self.loc]


spec8 = [('data', numba.uint8[:]), ('loc', numba.int64), ('len', numba.int64)]
Numpy8 = numba.jitclass(spec8)(NumpyIO)
spec32 = [('data', numba.uint32[:]), ('loc', numba.int64),
          ('len', numba.int64)]
Numpy32 = numba.jitclass(spec32)(NumpyIO)


def _assemble_objects(assign, defi, rep, val, dic, d, null, null_val,
                      max_defi):
    """Dremel-assembly of arrays of values into lists

    Parameters
    ----------
    assign: array dtype O
        To insert lists into
    defi: int array
        Definition levels, max 3
Exemple #37
0
 def create_my_class(value):
     cls = jitclass([('value', typeof(value))])(MyClass)
     return cls(value)
Exemple #38
0
    def write_many(self, b, count):
        self.data[self.loc:self.loc + count] = b
        self.loc += count

    def tell(self):
        return self.loc

    def so_far(self):
        """ In write mode, the data we have gathered until now
        """
        return self.data[:self.loc]


spec8 = [('data', numba.uint8[:]), ('loc', numba.int64), ('len', numba.int64)]
Numpy8 = jitclass(spec8)(NumpyIO)
spec32 = [('data', numba.uint32[:]), ('loc', numba.int64),
          ('len', numba.int64)]
Numpy32 = jitclass(spec32)(NumpyIO)


def _assemble_objects(assign, defi, rep, val, dic, d, null, null_val, max_defi,
                      prev_i):
    """Dremel-assembly of arrays of values into lists

    Parameters
    ----------
    assign: array dtype O
        To insert lists into
    defi: int array
        Definition levels, max 3
Exemple #39
0
        else:
            return self.values[sp:ep]

    def rowinds(self):
        ris = np.zeros(self.nnz, np.intc)
        for i in range(self.nrows):
            sp, ep = self.row_extent(i)
            ris[sp:ep] = i
        return ris


_CSR64 = type('_CSR64', _CSR.__bases__, dict(_CSR.__dict__))
_CSR = jitclass({
    'nrows': n.intc,
    'ncols': n.intc,
    'nnz': n.intc,
    'rowptrs': n.intc[::1],
    'colinds': n.intc[::1],
    'values': n.float64[::1]
})(_CSR)
_CSR64 = jitclass({
    'nrows': n.intc,
    'ncols': n.intc,
    'nnz': n.int64,
    'rowptrs': n.int64[::1],
    'colinds': n.intc[::1],
    'values': n.float64[::1]
})(_CSR64)


class CSR:
    """
Exemple #40
0
import numba

import bampy.bam as bam
import bampy.bam.packed_cigar as packed_cigar
import bampy.bam.packed_sequence as packed_sequence
import bampy.bam.tag as tag
import bampy.reference as reference

Tag = numba.jitclass({
    '_header': tag.TagHeader,
    '_buffer': memoryview,
})(bam.Tag)

"""Reference = numba.jitclass({
    'name' : str,
    'length' : int,
    'index' : int,
    '_optional' : dict,
})(reference.Reference)"""

PackedCIGAR = numba.jitclass({
    'buffer': memoryview,
})(packed_cigar.PackedCIGAR)

PackedSequence = numba.jitclass({
    'buffer': memoryview,
    '_length': int,
})(packed_sequence.PackedSequence)

Record = numba.jitclass({
    '_header': bam.record.RecordHeader,
Exemple #41
0
        self.loc += 1

    def write_many(self, b, count):
        self.data[self.loc:self.loc+count] = b
        self.loc += count

    def tell(self):
        return self.loc

    def so_far(self):
        """ In write mode, the data we have gathered until now
        """
        return self.data[:self.loc]

spec8 = [('data', numba.uint8[:]), ('loc', numba.int64), ('len', numba.int64)]
Numpy8 = numba.jitclass(spec8)(NumpyIO)
spec32 = [('data', numba.uint32[:]), ('loc', numba.int64), ('len', numba.int64)]
Numpy32 = numba.jitclass(spec32)(NumpyIO)


def _assemble_objects(assign, defi, rep, val, dic, d, null, null_val, max_defi, prev_i):
    """Dremel-assembly of arrays of values into lists

    Parameters
    ----------
    assign: array dtype O
        To insert lists into
    defi: int array
        Definition levels, max 3
    rep: int array
        Repetition levels, max 1
Exemple #42
0
import numba

import bampy.bgzf as bgzf

SubField = numba.jitclass({
    'header': bgzf.block.SubFieldHeader,
    'data': bytearray,
})(bgzf.block.SubField)

Block = numba.jitclass({
    '_header': bgzf.block.Header,
    '_trailer': bgzf.block.Trailer,
    'extra_fields': SubField[:],
    'size': int,
    'flags': bgzf.block.BlockFlags,
})(bgzf.Block)