Ejemplo n.º 1
0
def test_diff_or_identical_cases() -> None:
    """Tests if same or diff. numbers are generated for same or diff. cases.

    Tests if for different realization_id and for different
    purpose you get different random numbers,
    but for the same realization_id and same purpose
    you get the same random numbers.
    """

    realization_ids = [1, 2]
    # pylint: disable=too-many-nested-blocks
    for particle_type in [mparticles, uparticles]:
        for mparticle in particle_type:
            for realization_id1 in realization_ids:
                for mpurpose1 in mpurposes:
                    for realization_id2 in realization_ids:
                        for mpurpose2 in mpurposes:
                            mnprng_gen1 = NamedPrng(mpurposes, mparticles)
                            mnprng_gen2 = NamedPrng(mpurposes, mparticles)
                            mnprng_gen1.init_prngs(realization_id1)
                            mnprng_gen2.init_prngs(realization_id2)
                            arr1 = mnprng_gen1.generate(
                                Distr.UNI, [mparticle, mpurpose1])
                            arr2 = mnprng_gen2.generate(
                                Distr.UNI, [mparticle, mpurpose2])

                            if (realization_id1 == realization_id2
                                    and mpurpose1 == mpurpose2):
                                assert pytest.approx(arr1) == arr2
                            else:
                                assert pytest.approx(arr1) != arr2
Ejemplo n.º 2
0
def test_trigger_bare_str_ptype_purpose_warning() -> None:
    """Trigger the logging warning for implicit realization values."""
    mnprng = NamedPrng(mpurposes, mparticles)
    mnprng.init_prngs([1, 2], "quarks", "random_walk")
    prng_explicit = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 1])

    mnprng.init_prngs([1, 2], ["quarks"], ["random_walk"])
    prng_implicit = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 1])

    assert numpy.equal(prng_explicit, prng_implicit).all()
Ejemplo n.º 3
0
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)]))
Ejemplo n.º 4
0
def test_erase() -> None:
    """Test if deleting the engines removes the engines."""
    mnprng = NamedPrng(mpurposes, mparticles)
    mnprng.init_prngs(0)
    mnprng.generate(Distr.UNI, ("quarks", "random_walk"))

    mnprng.clear_prngs()
    assert isinstance(mnprng._engines, dict)  # pylint: disable=protected-access
    assert len(mnprng._engines) == 0  # pylint: disable=protected-access

    with pytest.raises(IndexError):
        mnprng.generate(Distr.UNI, ("quarks", "random_walk"))
Ejemplo n.º 5
0
def test_seed_shift() -> None:
    """Test if the realization shift works.

    I don't know why anybody would use the seed shift,
    it has no physical meaning."""
    mnprng_1 = NamedPrng(mpurposes, mparticles)
    mnprng_1.init_prngs(10)
    result_1 = mnprng_1.generate(Distr.UNI, ("quarks", "random_walk"))

    mnprng_2 = NamedPrng(mpurposes, mparticles, seed_logic=(100, 10, 0, 10))
    mnprng_2.init_prngs(0)
    result_2 = mnprng_2.generate(Distr.UNI, ("quarks", "random_walk"))

    assert numpy.equal(result_1, result_2).all()
Ejemplo n.º 6
0
def test_range_inited_with_single() -> None:
    """What happens if multiple realizations are initialized but only for a single one is used."""
    mnprng = NamedPrng(mpurposes, mparticles)
    mnprng.init_prngs([1, 2], ["quarks"], ["random_walk"])
    rnds12_1 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 1])
    rnds12_2 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 2])

    mnprng.init_prngs(1, ["quarks"], ["random_walk"])
    rnds1_1 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 1])

    mnprng.init_prngs(2, ["quarks"], ["random_walk"])
    rnds2_2 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 2])

    assert numpy.equal(rnds12_1, rnds1_1).all()
    assert numpy.equal(rnds12_2, rnds2_2).all()
Ejemplo n.º 7
0
def test_normal() -> None:
    """Tests if normal distribution is the same as before.

    Tests the first few random numbers from the seed 0
    if matches a previously generated sequence for normal distribution
    with 3 different mean and std.
    """

    param_pairs = [(mean, std) for mean in [-1, 0, 1] for std in [0.1, 1, 10]]

    for particle_type in [mparticles, uparticles]:
        ret_arrs = numpy.ndarray((9, 6), dtype=numpy.float64)

        for i, param_pair in enumerate(param_pairs):
            mnprng_gen = NamedPrng(mpurposes, particle_type)

            mnprng_gen.init_prngs(0, ["quarks"], ["random_walk"])

            ret_arrs[i] = mnprng_gen.generate((Distr.STN, param_pair),
                                              ["quarks", "random_walk"])

        orig_arr = numpy.array(
            [[
                -1.0625151089e+00, -1.0596945414e+00, -9.8609030314e-01,
                -1.0276130879e+00, -9.9928650089e-01, -1.0060799379e+00
            ],
             [
                 -1.6251510894e+00, -1.5969454139e+00, -8.6090303136e-01,
                 -1.2761308794e+00, -9.9286500888e-01, -1.0607993786e+00
             ],
             [
                 -7.2515108944e+00, -6.9694541388e+00, 3.9096968635e-01,
                 -3.7613087942e+00, -9.2865008877e-01, -1.6079937856e+00
             ],
             [
                 -6.2515108944e-02, -5.9694541388e-02, 1.3909696864e-02,
                 -2.7613087942e-02, 7.1349911225e-04, -6.0799378562e-03
             ],
             [
                 -6.2515108944e-01, -5.9694541388e-01, 1.3909696864e-01,
                 -2.7613087942e-01, 7.1349911225e-03, -6.0799378562e-02
             ],
             [
                 -6.2515108944e+00, -5.9694541388e+00, 1.3909696864e+00,
                 -2.7613087942e+00, 7.1349911225e-02, -6.0799378562e-01
             ],
             [
                 9.3748489106e-01, 9.4030545861e-01, 1.0139096969e+00,
                 9.7238691206e-01, 1.0007134991e+00, 9.9392006214e-01
             ],
             [
                 3.7484891056e-01, 4.0305458612e-01, 1.1390969686e+00,
                 7.2386912058e-01, 1.0071349911e+00, 9.3920062144e-01
             ],
             [
                 -5.2515108944e+00, -4.9694541388e+00, 2.3909696864e+00,
                 -1.7613087942e+00, 1.0713499112e+00, 3.9200621438e-01
             ]])

        assert pytest.approx(ret_arrs) == orig_arr
Ejemplo n.º 8
0
def test_uniform() -> None:
    """Tests if uniform distribution is the same as before.

    Tests the class if it generates the same sequence of random numbers
    as it generated in a controlled environment long time ago.
    """

    for particles_type in [uparticles, mparticles]:
        mnprng_gen = NamedPrng(mpurposes, particles_type)
        mnprng_gen.init_prngs(0, ["quarks"], ["random_walk"])
        arr = mnprng_gen.generate(Distr.UNI, ["quarks", "random_walk"])

        orig_arr = numpy.array([
            0.47932306384132817, 0.2864961735272108, 0.022216695186381585,
            0.5453879896772311, 0.0012294157898979918, 0.5108467779409858
        ])

        assert pytest.approx(arr) == orig_arr
Ejemplo n.º 9
0
def test_compare_generate_3d_generate_it() -> None:
    """Compares the 3d init+gen method with the generate_it."""
    id_filter = (remove_quarks, FStrat.EXC)
    real_range = [0, 2]

    mnprng1 = NamedPrng(mpurposes, mparticles)
    mnprng1.init_prngs(real_range, purposes=["random_walk"])

    mnprng2 = NamedPrng(mpurposes, mparticles)
    out2 = mnprng2.generate_r_t(Distr.UNI,
                                ["quarks", "random_walk", real_range], (0, 3),
                                id_filter)

    for time in range(0, 3):
        out1 = mnprng1.generate(Distr.UNI,
                                ["quarks", "random_walk", real_range],
                                id_filter)
        for i, _ in enumerate(real_range):
            assert (out1[i] == out2[i][time]).all()
Ejemplo n.º 10
0
def test_independent_ptype_real_time() -> None:
    """Test if prngs are indeed generation order-independent."""
    mnprng = NamedPrng(mpurposes, mparticles)
    mnprng.init_prngs([1, 2], ["quarks", "atoms"], "random_walk")
    a_1 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 1])
    a_2 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 2])
    a_3 = mnprng.generate(Distr.UNI, ["atoms", "random_walk", 1])
    a_4 = mnprng.generate(Distr.UNI, ["atoms", "random_walk", 2])
    a_1_2 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 1])

    mnprng.init_prngs([1, 2], ["quarks", "atoms"], "random_walk")
    b_1 = mnprng.generate(Distr.UNI, ["atoms", "random_walk", 2])
    b_2 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 1])
    b_3 = mnprng.generate(Distr.UNI, ["atoms", "random_walk", 1])
    b_4 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 2])
    b_2_2 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", 1])

    assert numpy.equal(a_1, b_2).all()
    assert numpy.equal(a_2, b_4).all()
    assert numpy.equal(a_3, b_3).all()
    assert numpy.equal(a_4, b_1).all()
    assert numpy.equal(a_1_2, b_2_2).all()
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
def test_generate_same_3d() -> None:
    """Initializes engines in 3D and generates prns.

    Engines are initialized for realizations, ptype and purpose,
    and generates random number for the same setup and checks if
    the corresponding values are the same and the unrelated values
    are different.
    """
    id_filter = (remove_quarks, FStrat.EXC)

    mnprng = NamedPrng(mpurposes, mparticles)
    reals = [0, 1, 2]
    mnprng.init_prngs(reals, purposes=["random_walk"])

    out1_t0 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", reals],
                              id_filter)
    out1_t1 = mnprng.generate(Distr.UNI, ["quarks", "random_walk", reals],
                              id_filter)

    mnprng2 = NamedPrng(mpurposes, mparticles)
    mnprng2.init_prngs(reals, purposes=["random_walk"])

    out2_t0 = mnprng2.generate(Distr.UNI, ["quarks", "random_walk", reals],
                               id_filter)
    out2_t1 = mnprng2.generate(Distr.UNI, ["quarks", "random_walk", reals],
                               id_filter)

    reals3 = [3, 4, 5]
    mnprng3 = NamedPrng(mpurposes, mparticles)
    mnprng3.init_prngs(reals3, purposes=["random_walk"])

    out3_t0 = mnprng3.generate(Distr.UNI, ["quarks", "random_walk", reals3],
                               id_filter)
    out3_t1 = mnprng3.generate(Distr.UNI, ["quarks", "random_walk", reals3],
                               id_filter)

    assert (out1_t0 == out2_t0).all()
    assert (out1_t1 == out2_t1).all()
    assert (out3_t0 != out2_t0).any()
    assert (out3_t1 != out2_t1).any()
    assert (out3_t0 != out2_t1).any()
Ejemplo n.º 13
0
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)