コード例 #1
0
def test_profiler():
    p = Profiler()
    p = p.start("main")

    assert p.name() == "main"
    assert p.total() is None
    assert p.child_total() is None

    p = p.start("child1")

    assert p.name() == "child1"
    assert p.total() is None

    p = p.stop()

    assert p.name() == "main"
    assert p.child_total() >= 0

    p = p.start("child2")

    assert p.name() == "child2"
    assert p.total() is None

    p = p.start("grandchild")

    assert p.name() == "grandchild"
    assert p.total() is None

    p = p.stop()

    assert p.name() == "child2"
    assert p.child_total() >= 0

    p = p.stop()

    assert p.name() == "main"
    assert p.child_total() >= 0
    assert p.total() is None

    p = p.stop()

    assert p.name() is None
    assert p.child_total() >= 0

    print(p)
コード例 #2
0
def test_variables_with_repeats():
    from metawards.utils import Profiler
    p = Profiler()
    p = p.start("read")
    variables = VariableSets.read(params_with_repeats_csv)
    p = p.stop()

    p = p.start("loop")
    for variable in variables:
        assert variable.output_dir() != variable.fingerprint(
            include_index=True)

        o = "beta_%.1f_ill_%.2f" % (variable["beta[2]"],
                                    variable["too_ill_to_move[2]"])
        o = o.replace(".", "i")

        print(variable.output_dir(), o)

        assert variable.output_dir() == o
    p = p.stop()
    print(p)
コード例 #3
0
def test_ward_conversion():
    # load all of the parameters
    try:
        params = Parameters.load(parameters="march29")
    except Exception as e:
        print(f"Unable to load parameter files. Make sure that you have "
              f"cloned the MetaWardsData repository and have set the "
              f"environment variable METAWARDSDATA to point to the "
              f"local directory containing the repository, e.g. the "
              f"default is $HOME/GitHub/MetaWardsData")
        raise e

    params.set_input_files("2011Data")

    print("Building the network...")
    network = Network.build(params=params)

    profiler = Profiler()

    profiler = profiler.start("to_json")
    wards = network.to_wards(profiler=profiler)

    print(f"{wards.num_workers()} / {wards.num_players()}")

    _assert_equal(wards.num_workers(), network.work_population)
    _assert_equal(wards.num_players(), network.play_population)

    print(f"{wards.num_work_links()} / {wards.num_play_links()}")

    _assert_equal(wards.num_work_links(), network.nlinks)
    _assert_equal(wards.num_play_links(), network.nplay)

    print("Converting to data...")
    data = wards.to_data(profiler=profiler)

    print("Converting to json...")
    profiler = profiler.start("Convert to JSON")
    s = json.dumps(data)
    profiler = profiler.stop()

    profiler = profiler.stop()  #  end to_json

    print(f"Done - {len(s)/(1024*1024.0)} MB : {s[0:1024]}...")

    print(f"Converting from json...")
    profiler = profiler.start("Convert from JSON")

    profiler = profiler.start("from_json")
    data = json.loads(s)
    profiler = profiler.stop()

    wards2 = Wards.from_data(data, profiler=profiler)

    assert wards2 == wards

    network2 = Network.from_wards(wards2, profiler=profiler)

    profiler = profiler.stop()

    Console.print(profiler)

    Console.print("Validating equality - may take some time...")

    _assert_equal(network2.nnodes, network.nnodes)
    _assert_equal(network2.nlinks, network.nlinks)
    _assert_equal(network2.nplay, network.nplay)

    if network.info is None:
        assert network2.info is None

    _assert_equal(len(network.info), len(network2.info))

    Console.print(f"{len(network.info)}, {network.nnodes}")

    with Console.progress() as progress:
        task1 = progress.add_task("Validating info", total=len(network.info))
        task2 = progress.add_task("Validating nodes", total=network.nnodes)
        task3 = progress.add_task("Validating work", total=network.nlinks)
        task4 = progress.add_task("Validating play", total=network.nplay)

        for i in range(0, len(network.info)):
            assert network.info[i] == network2.info[i]
            progress.update(task1, advance=1)

        progress.update(task1, completed=len(network.info), force_update=True)

        for i in range(1, network.nnodes + 1):
            _assert_equal(network.nodes.label[i], network2.nodes.label[i])
            _assert_equal(network.nodes.begin_to[i],
                          network2.nodes.begin_to[i])
            _assert_equal(network.nodes.end_to[i], network2.nodes.end_to[i])
            _assert_equal(network.nodes.self_w[i], network2.nodes.self_w[i])
            _assert_equal(network.nodes.begin_p[i], network2.nodes.begin_p[i])
            _assert_equal(network.nodes.end_p[i], network2.nodes.end_p[i])
            _assert_equal(network.nodes.self_p[i], network2.nodes.self_p[i])
            _assert_equal(network.nodes.x[i], network2.nodes.x[i])
            _assert_equal(network.nodes.y[i], network2.nodes.y[i])
            progress.update(task2, advance=1)

        progress.update(task2, completed=network.nnodes, force_update=True)

        for i in range(1, network.nlinks + 1):
            _assert_equal(network.links.ifrom[i], network2.links.ifrom[i])
            _assert_equal(network.links.ito[i], network2.links.ito[i])
            _assert_equal(network.links.weight[i], network2.links.weight[i])
            _assert_equal(network.links.suscept[i], network2.links.suscept[i])
            progress.update(task3, advance=1)

        progress.update(task3, completed=network.nlinks, force_update=True)

        for i in range(1, network.nplay + 1):
            _assert_equal(network.play.ifrom[i], network2.play.ifrom[i])
            _assert_equal(network.play.ito[i], network2.play.ito[i])
            _assert_equal(network.play.weight[i], network2.play.weight[i])
            _assert_equal(network.play.suscept[i], network2.play.suscept[i])
            progress.update(task4, advance=1)

        progress.update(task4, completed=network.nplay, force_update=True)