def test_bad_filenames() -> None: """Checks if exim settings exceptions are handled properly.""" wrong_filename = "very/bad:filename?here*" with pytest.raises(OSError): mnprng = NamedPrng(mpurposes, wrong_filename) with pytest.raises(OSError): mnprng = NamedPrng(mpurposes, mparticles, (wrong_filename, None, False)) with pytest.raises(OSError): mnprng = NamedPrng(mpurposes, mparticles, (None, wrong_filename, False)) with pytest.raises(OSError): mnprng = NamedPrng(mpurposes, mparticles) mnprng.export_particles(wrong_filename)
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)
###################################################################""") Mnprng_gen = NamedPrng(mpurposes, mparticles, 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,