def test_subset_exc_inc() -> None: """Tests exclusion and inclusion filtering. Tests if for a subset of particles you get the proper subset of random numbers and whether you get the same random numbers by exclusion and inclusion if the class is initialized for a realization_id range. """ mnprng_gen_full = NamedPrng(mpurposes, mparticles) mnprng_gen_sbs1 = NamedPrng(mpurposes, mparticles) mnprng_gen_sbs2 = NamedPrng(mpurposes, mparticles) seed_args = ("quarks", "random_walk", range(0, 2)) arr_full = mnprng_gen_full.generate_it(Distr.UNI, seed_args) arr_sbs1 = mnprng_gen_sbs1.generate_it(Distr.UNI, seed_args, (remove_quarks, FStrat.EXC)) arr_sbs2 = mnprng_gen_sbs2.generate_it(Distr.UNI, seed_args, (quarks_subset, FStrat.INC)) # exclusion and inclusion is the same if the resulting set is the same assert arr_sbs1 == pytest.approx(arr_sbs2) # The subset random array of the original, full set's random array # is the same as # the full random array of the subset's random array. # Test it particle-wise (i.e. columnwise) for i, quark in enumerate(quarks_subset): assert arr_full[:, quarks_subset[quark]] == pytest.approx(arr_sbs1[:, i])
def test_unsupported_rnd_type() -> None: """Test for not supported random number type.""" with pytest.raises(NotImplementedError): mnprng = NamedPrng( mpurposes, # pylint: disable=unused-variable mparticles) mnprng.generate_it(Distr.STU, ["quarks", mpurposes[0], range(0, 2)])
def test_teefile() -> None: """Checks if teeing and reading back works. Create a set of random numbers then reads back from the teefile and checks if it the same for a realization_id range using filters including or excluding. While reading back, copies the random numbers to another teefile and checks if the copied file is the same. """ for only_used in [False, True]: for strategy in [FStrat.INC, FStrat.EXC]: tee_fname = "teefile_test_named_prng.dat" mnprng_save = NamedPrng(mpurposes, mparticles, exim_settings=(tee_fname, "", only_used)) seed_args = ["quarks", mpurposes[0], range(0, 2)] arr_save_r = mnprng_save.generate_it(Distr.UNI, seed_args, id_filter=(remove_quarks, strategy)) arr_save_n = mnprng_save.generate_it( (Distr.STN, (1, 3)), seed_args, id_filter=(remove_quarks, strategy)) del mnprng_save mnprng_load = NamedPrng(mpurposes, mparticles, exim_settings=("B" + tee_fname, tee_fname, only_used)) arr_load_r = mnprng_load.generate_it(Distr.UNI, seed_args, id_filter=(remove_quarks, strategy)) arr_load_n = mnprng_load.generate_it( (Distr.STN, (1, 3)), seed_args, id_filter=(remove_quarks, strategy)) assert filecmp.cmp(tee_fname, "B" + tee_fname) del mnprng_load os.remove(tee_fname) os.remove("B" + tee_fname) assert arr_load_r == pytest.approx(arr_save_r) assert arr_load_n == pytest.approx(arr_save_n)
def do_some_stuff(mnprng: NamedPrng) -> None: """Do some stuff with the NamedPrng. I will call it without and with _sourcefile defined to see if it gives the same result. """ for realization_id in range(2): for purpose in mpurposes: print("realization_id =", realization_id, "purpose =", purpose) # generate random numbers for quarks mnprng.init_prngs(realization_id) random_for_quarks = mnprng.generate(Distr.UNI, ("quarks", purpose)) for (key, value), rnd in zip(quarks.items(), random_for_quarks): print(key, value, rnd, sep="\t") # generate random numbers for a subset of quarks by excluding print( "-------- now for the restricted set of particles by excluding --------" ) mnprng.init_prngs(realization_id) # reset the prngs random_for_quarks_subset = mnprng.generate( Distr.UNI, ("quarks", purpose), id_filter=(remove_quarks, FStrat.EXC)) for (key, value), rnd in zip(quarks_subset.items(), random_for_quarks_subset): print(key, value, rnd, sep="\t") # generate random numbers for a subset of quarks by including print( "-------- now for the restricted set of particles by including --------" ) mnprng.init_prngs(realization_id) # reset the prngs random_for_quarks_subset_in = mnprng.generate( Distr.UNI, ("quarks", purpose), id_filter=(quarks_subset, FStrat.INC)) for (key, value), rnd in zip(quarks_subset.items(), random_for_quarks_subset_in): print(key, value, rnd, sep="\t") print( "========================================================================" ) print( '-- now for all realizations in 1 go for the purpose "random_walk" --' ) print(mnprng.generate_it(Distr.UNI, ["quarks", "random_walk", (0, 1, 2)]))
def test_generate_it_it() -> None: """Tests the iterable feature of the generate_it function. Generates random numbers for the IDs 0, 2, 4, 7, 8, 9 using 3 ways: - 1 by 1 by initializing and generating realization-wise and then joining, - by a list containing the values - by joining range(0,6,2) and range(7,10). """ for particle_type in [mparticles, uparticles]: ids = [0, 2, 4, 7, 8, 9] mnprng1by1 = NamedPrng(mpurposes, particle_type) mnprng_list = NamedPrng(mpurposes, particle_type) mnprng_rang = NamedPrng(mpurposes, particle_type) p_type = "quarks" purpose = mpurposes[0] # random_walk ret1by1 = numpy.ndarray(shape=(len(p_type), len(ids)), dtype=numpy.float64) retrang = numpy.ndarray(shape=(len(p_type), len(ids)), dtype=numpy.float64) for count, realization_id in enumerate(ids): mnprng1by1.init_prngs(realization_id) ret_col = mnprng1by1.generate(Distr.STN, [p_type, purpose]) ret1by1[count] = ret_col retlist = mnprng_list.generate_it(Distr.STN, (p_type, purpose, ids)) retrang[0:3] = mnprng_rang.generate_it( Distr.STN, (p_type, purpose, range(0, 6, 2))) retrang[3:6] = mnprng_rang.generate_it(Distr.STN, (p_type, purpose, range(7, 10))) assert pytest.approx(ret1by1) == retlist assert pytest.approx(retrang) == retlist
def test_same_case_after_pickle() -> None: """Tests if pickle works as expected. Exports particles and loads back, and tests if the generated random numbers for a subset is the same, for one realization_id for uniform and for a range for gaussian. """ # uniform, single realization ID mnprng_save = NamedPrng(mpurposes, mparticles) mnprng_save.init_prngs(realizations=0) generate_arg = ( Distr.UNI, # distribution ["quarks", mpurposes[0]], # particle type and purpose (remove_quarks, FStrat.EXC)) # filtering arr_save = mnprng_save.generate(*generate_arg) mparticles_fname = "test_same_case_after_pickle_random" mnprng_save.export_particles(mparticles_fname) del mnprng_save mnprng_load = NamedPrng(mpurposes, mparticles_fname) mnprng_load.init_prngs(realizations=0) arr_load = mnprng_load.generate(*generate_arg) del mnprng_load assert arr_save == pytest.approx(arr_load) if os.path.isfile(mparticles_fname): os.remove(mparticles_fname) # # gaussian, realization ID range seed_args = ["quarks", mpurposes[0], range(0, 2)] # distinguishable particles, filtering mnprng_save = NamedPrng(mpurposes, mparticles) mnprng_save.init_prngs(realizations=0) marr_save = mnprng_save.generate_it((Distr.STN, (1, 3)), seed_args, (None, remove_quarks)) mparticles_fname = "test_same_case_after_pickle_normal" mnprng_save.export_particles(mparticles_fname) del mnprng_save mnprng_load = NamedPrng(mpurposes, mparticles_fname) mnprng_load.init_prngs(realizations=0) marr_load = mnprng_load.generate_it((Distr.STN, (1, 3)), seed_args, (None, remove_quarks)) assert marr_save == pytest.approx(marr_load) if os.path.isfile(mparticles_fname): os.remove(mparticles_fname) # indistinguishable particles, no filtering unprng_save = NamedPrng(mpurposes, uparticles) unprng_save.init_prngs(realizations=0) uarr_save = unprng_save.generate_it((Distr.STN, (1, 3)), seed_args) uparticles_fname = "test_same_case_after_pickle_normal_indi" unprng_save.export_particles(uparticles_fname) del unprng_save unprng_load = NamedPrng(mpurposes, uparticles_fname) unprng_load.init_prngs(realizations=0) uarr_load = unprng_load.generate_it((Distr.STN, (1, 3)), seed_args) assert uarr_save == pytest.approx(uarr_load) if os.path.isfile(uparticles_fname): os.remove(uparticles_fname)
exim_settings=("tee.dat", "", True)) do_some_stuff(Mnprng_gen) del Mnprng_gen print(""" ################################################################### ########### Random numbers are read from a file ########### ###################################################################""") Mnprng_use = NamedPrng(mpurposes, mparticles, exim_settings=("", "tee.dat", True)) do_some_stuff(Mnprng_use) Mnprng_use.export_particles() del Mnprng_use Mnprng_stu = NamedPrng(mpurposes) print( "\nLoad back the particles and generate random numbers for quarks for random_walk." ) print(Mnprng_stu.generate_it(Distr.UNI, ["quarks", "random_walk", (0, 1, 2)])) os.remove("dict_of_particles.pickle") print(f"\nPrint out {len(range(0,4))} times random number for quarks.", f"quarks are {len(quarks)} long") ind_particles = NamedPrng(mpurposes, uparticles) rnd_array = ind_particles.generate_it(Distr.UNI, ("quarks", "random_walk", range(0, 4))) print(rnd_array)