def _numba_update_nested_dict(self, nested): d = typed.Dict() for key, dct in nested.items(): d2 = typed.Dict() d2.update(dct) d[key] = d2 return d
def test_ex_initial_value_dict_compile_time_consts(self): with captured_stdout(): # magictoken.test_ex_initial_value_dict_compile_time_consts.begin from numba import njit, literally from numba.extending import overload # overload this function def specialize(x): pass @overload(specialize) def ol_specialize(x): iv = x.initial_value if iv is None: return lambda x: literally(x) # Force literal dispatch assert iv == {'a': 1, 'b': 2, 'c': 3} # INITIAL VALUE return lambda x: literally(x) @njit def foo(): d = {'a': 1, 'b': 2, 'c': 3} d['c'] = 20 # no impact on .initial_value d['d'] = 30 # no impact on .initial_value return specialize(d) result = foo() print(result) # {a: 1, b: 2, c: 20, d: 30} # NOT INITIAL VALUE! # magictoken.test_ex_initial_value_dict_compile_time_consts.end expected = typed.Dict() for k, v in {'a': 1, 'b': 2, 'c': 20, 'd': 30}.items(): expected[k] = v self.assertEqual(result, expected)
def dict_usecase(arr): d = typed.Dict() for i in range(arr.size): d[i] = arr[i] out = np.zeros_like(arr) for k, v in d.items(): out[k] = k * v return out
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: with open(filename2) as file: data = json.load(file) for i in data: temp = typed.Dict() for j in data[i]: temp[j] = float(data[i][j]) tojitclass[i] = temp return JitPDH(tojitclass)
def create_dict(seed=0): random.seed(seed) d = random_dict(random.randint(0, 10), random.randint(0, 10)) d[tuple((np.str_("ciao"), np.str_("ciao")))] = 9 d["numba_list"] = typed.List() d["numba_dict"] = typed.Dict() d["callable"] = create_dict d["date"] = date(1994, 12, 12) d["none"] = None return d
def test_calculate_spectrum(): x = np.linspace(-1, 20, 1000, dtype=np.float32) parameter_values = [0., 0.2, 1., 0.1, 1.] lookup = typed.Dict() lookup['eped'] = 0 lookup['eped_sigma'] = 1 lookup['spe'] = 2 lookup['spe_sigma'] = 3 lookup['lambda_'] = 4 y = calculate_spectrum(x, lookup, *parameter_values) np.testing.assert_allclose(np.trapz(y, x), 1, rtol=1e-3)
def test_MyStructType_in_dict_mixed_type_error(self): self.disable_leak_check() td = typed.Dict() td['a'] = MyStruct(1, 2.3) self.assertEqual(td['a'].values, 1) self.assertEqual(td['a'].counter, 2.3) # ERROR: store different types with self.assertRaisesRegex(errors.TypingError, r"Cannot cast numba.MyStructType"): # because first field is not a float; # the second field is now an integer. td['b'] = MyStruct(2.3, 1)
def test_MyStructType_in_dict(self): td = typed.Dict() td['a'] = MyStruct(1, 2.3) self.assertEqual(td['a'].values, 1) self.assertEqual(td['a'].counter, 2.3) # overwrite td['a'] = MyStruct(2, 3.3) self.assertEqual(td['a'].values, 2) self.assertEqual(td['a'].counter, 3.3) # mutate td['a'].values += 10 self.assertEqual(td['a'].values, 12) # changed self.assertEqual(td['a'].counter, 3.3) # unchanged # insert td['b'] = MyStruct(4, 5.6)
def add_kuiperbelt(self, total_mass, r_res, range_min, range_max, hom_mode=False): """Creates an array of planet objects for the euler lagrange method hom_mode False divides their mass evenly over orbits, True divides it homogeneously in the radial direction.""" if r_res != 0: radii = np.linspace(range_min, range_max, r_res) if hom_mode: m1 = total_mass * range_min / np.sum(radii) weights = radii / range_max masses = weights * m1 else: masses = np.repeat( np.array([total_mass / r_res], dtype=np.float64), r_res) _orbital_inclination = 1.8 #https://arxiv.org/abs/1704.02444 _loanode = 77 #https://arxiv.org/abs/1704.02444 _eccentricity = 0.1 #DES/SSBN07 classification _argperiapsis = 270 #Nergens op gebaseerd self.asteroid_attributes['mass'] = np.append( self.asteroid_attributes['mass'], masses) self.asteroid_attributes['smaxis'] = np.append( self.asteroid_attributes['smaxis'], radii) self.asteroid_attributes['eccentricity'] = np.append( self.asteroid_attributes['eccentricity'], np.repeat(np.array([_eccentricity], dtype=np.float64), r_res)) self.asteroid_attributes['loanode'] = np.append( self.asteroid_attributes['loanode'], np.repeat(np.array([_loanode], dtype=np.float64), r_res)) self.asteroid_attributes['orbital inclination'] = np.append( self.asteroid_attributes['orbital inclination'], np.repeat(np.array([_orbital_inclination], dtype=np.float64), r_res)) self.asteroid_attributes['argperiapsis'] = np.append( self.asteroid_attributes['argperiapsis'], np.repeat(np.array([_argperiapsis], dtype=np.float64), r_res)) for i in range(len(radii)): asteroid = typed.Dict() asteroid['mass'] = masses[i] asteroid['eccentricity'] = _eccentricity asteroid['loanode'] = _loanode asteroid['argperiapsis'] = _argperiapsis asteroid['orbital inclination'] = _orbital_inclination asteroid['smaxis'] = radii[i] self.asteroids.append(asteroid)
def typed_dict(d): nd = typed.Dict() for k, v in d.items(): nd[k] = v return nd