Exemplo n.º 1
0
def test_option_susceptibility():
    args = parse_args(
        ["--popsize", "A=500", "B=300", "--susceptibility", "A=0.8"])
    params = Params(args)
    assert sorted(list(params.groups.keys())) == ["A", "B"]

    assert params.susceptibility_multiplier_A == 0.8
    with pytest.raises(AttributeError):
        params.susceptibility_multiplier_B

    # group does not exist
    with pytest.raises(ValueError):
        args = parse_args(
            ["--popsize", "A=500", "B=300", "--susceptibility", "C=0.8"])
        params = Params(args)

    # not float
    with pytest.raises(ValueError):
        args = parse_args(
            ["--popsize", "A=500", "B=300", "--susceptibility", "A=1f"])
        params = Params(args)

    args = parse_args(
        ["--popsize", "A=500", "B=300", "--susceptibility", "0.8"])
    params = Params(args)
Exemplo n.º 2
0
    def summarize_model(self, simu_args, args):
        print(f"\nPlugin {self}:")

        model = Model(Params(simu_args))
        for asym_carriers in (0, 1, None):
            if asym_carriers is not None:
                model.params.set("prop_asym_carriers", "loc", asym_carriers)
                model.params.set("prop_asym_carriers", "scale", 0)
            else:
                model = Model(Params(simu_args))

            # average test sensitivity
            sensitivities7 = []
            sensitivities20 = []
            with open(os.devnull, "w") as logger:
                logger.id = 1

                for i in range(10000):
                    model.draw_prop_asym_carriers()
                    ind = Individual(id="0",
                                     susceptibility=1,
                                     model=model,
                                     logger=logger)
                    ind.infect(0, by=None, leadtime=0)

                    for i in range(20):
                        test_lod = (args.sensitivity[1]
                                    if len(args.sensitivity) == 2 else 0)

                        lod_sensitivity = ind.test_sensitivity(i, test_lod)
                        if lod_sensitivity == 0:
                            continue
                        #
                        sensitivity = lod_sensitivity * args.sensitivity[0]
                        if i <= 7:
                            sensitivities7.append(sensitivity)
                        else:
                            sensitivities20.append(sensitivity)
            print(
                f"\nTest sensitivity (for {model.params.prop_asym_carriers*100:.1f}% asymptomatic carriers)"
            )
            print(
                f"    <= 7 days:     {pd.Series(sensitivities7).mean() * 100:.1f}%"
            )
            print(
                f"    > 7 days:      {pd.Series(sensitivities20).mean() * 100:.1f}%"
            )
            print(
                f"    all:           {pd.Series(sensitivities7 + sensitivities20).mean() * 100:.1f}%"
            )
Exemplo n.º 3
0
def test_option_popsize():
    args = parse_args(["--popsize", "500"])
    params = Params(args)
    assert list(params.groups.keys()) == [""]

    args = parse_args(["--popsize", "A=500", "B=300"])
    params = Params(args)
    assert sorted(list(params.groups.keys())) == ["A", "B"]

    args = parse_args(["--popsize", "A=500", "B=300"])
    params = Params(args)
    assert sorted(list(params.groups.keys())) == ["A", "B"]

    with pytest.raises(ValueError):
        args = parse_args(["--popsize", "500", "300"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(["--popsize", "50A"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(["--popsize", "all=100"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(["--popsize", "500", "A=300", "A=200"])
        params = Params(args)
Exemplo n.º 4
0
def test_option_prop_asym_carriers():
    args = parse_args(["--prop-asym-carriers", "0.1"])
    params = Params(args)
    assert params.prop_asym_carriers_loc == 0.1
    assert params.prop_asym_carriers_scale == 0.0

    args = parse_args(["--prop-asym-carriers", "0.1", "0.3"])
    params = Params(args)
    assert params.prop_asym_carriers_loc == 0.2

    # not a float number
    with pytest.raises(ValueError):
        args = parse_args(
            ["--popsize", "A=10", "B=10", "--prop-asym-carriers", "1.2f"])
        params = Params(args)

    # not incremental
    with pytest.raises(ValueError):
        args = parse_args([
            "--popsize", "A=10", "B=10", "--prop-asym-carriers", "1.5", "0.5"
        ])
        params = Params(args)

    # more numbers
    with pytest.raises(ValueError):
        args = parse_args([
            "--popsize", "A=10", "B=10", "--prop-asym-carriers", "1.5", "2.5",
            "3.5"
        ])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(
            ["--popsize", "A=10", "B=10", "--prop-asym-carriers", "C=1.2"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(
            ["--popsize", "A=10", "B=10", "--prop-asym-carriers", "A=-1.2"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(
            ["--popsize", "A=10", "B=10", "--prop-asym-carriers", "A=1.2f"])
        params = Params(args)

    args = parse_args(
        ["--popsize", "A=10", "B=10", "--prop-asym-carriers", "A=1.2"])
    params = Params(args)
    assert params.prop_asym_carriers_multiplier_A == 1.2
Exemplo n.º 5
0
def test_option_infectors():
    args = parse_args(["--infectors", "10"])
    params = Params(args)

    args = parse_args(
        ["--popsize", "A=10", "B=10", "--infectors", "A_1", "B_2"])
    params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(["--popsize", "A=10", "B=10", "--infectors", "1"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(
            ["--popsize", "A=10", "B=10", "--infectors", "C_1", "C_2"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(["--popsize", "A=10", "B=10", "--infectors", "A_10"])
        params = Params(args)
Exemplo n.º 6
0
def test_option_symptomatic_r0():
    args = parse_args(["--symptomatic-r0", "1.0"])
    params = Params(args)
    assert params.symptomatic_r0_loc == 1.0
    assert params.symptomatic_r0_scale == 0

    args = parse_args(["--symptomatic-r0", "1.2", "2.0"])
    params = Params(args)
    assert params.symptomatic_r0_loc == (1.2 + 2.0) / 2

    args = parse_args(
        ["--popsize", "A=500", "B=300", "--symptomatic-r0", "1.5", "A=0.8"])
    params = Params(args)
    assert sorted(list(params.groups.keys())) == ["A", "B"]
    assert params.symptomatic_r0_loc == 1.5
    assert params.symptomatic_r0_scale == 0
    assert params.symptomatic_r0_multiplier_A == 0.8

    # not float number
    with pytest.raises(ValueError):
        args = parse_args(["--symptomatic-r0", "1.0f"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(["--symptomatic-r0", "0.8a", "1.0"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(["--symptomatic-r0", "0.8", "1.0f"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(["--popsize", "A=200", "--symptomatic-r0", "A=8b"])
        params = Params(args)

    # more numbers
    with pytest.raises(ValueError):
        args = parse_args(["--symptomatic-r0", "0.8", "1.0", "2.0"])
        params = Params(args)
Exemplo n.º 7
0
def test_option_inclubation_period():
    with pytest.raises(ValueError):
        args = parse_args(["--incubation-period", "1.0"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(["--incubation-period", "1.0", "2.0"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(["--incubation-period", "uniform", "1.0", "2.0"])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args(
            ["--popsize", "A=100", "B=200", "--incubation-period", "C=0.8"])
        params = Params(args)

    args = parse_args(["--incubation-period", "normal", "1.2", "2.0"])
    params = Params(args)
    assert params.incubation_period_loc == 1.2
    assert params.incubation_period_scale == 2.0

    args = parse_args(["--incubation-period", "lognormal", "1.", "2.5"])
    params = Params(args)
    assert params.incubation_period_mean == 1.0
    assert params.incubation_period_sigma == 2.5

    args = parse_args(
        ["--popsize", "A=500", "B=300", "--incubation-period", "A=0.8"])
    params = Params(args)
    assert sorted(list(params.groups.keys())) == ["A", "B"]
    assert params.incubation_period_multiplier_A == 0.8

    args = parse_args([
        "--popsize",
        "A=500",
        "B=300",
        "--incubation-period",
        "normal",
        "5",
        "2",
        "B=0.95",
        "A=1.13",
    ])
    params = Params(args)
    assert sorted(list(params.groups.keys())) == ["A", "B"]
    assert params.incubation_period_loc == 5.0
    assert params.incubation_period_scale == 2.0
    assert params.incubation_period_multiplier_A == 1.13
    assert params.incubation_period_multiplier_B == 0.95

    with pytest.raises(ValueError):
        args = parse_args([
            "--popsize",
            "A=500",
            "B=300",
            "--incubation-period",
            "normal",
            "5",
            "2.3p",
        ])
        params = Params(args)

    with pytest.raises(ValueError):
        args = parse_args([
            "--popsize",
            "A=500",
            "B=300",
            "--incubation-period",
            "normal",
            "5",
            "2.3p",
            "A=1.4p",
        ])
        params = Params(args)

    # negative multiplier
    with pytest.raises(ValueError):
        args = parse_args(
            ["--popsize", "A=500", "B=300", "--incubation-period", "A=-1"])
        params = Params(args)

    # non-float multiplier
    with pytest.raises(ValueError):
        args = parse_args(
            ["--popsize", "A=500", "B=300", "--incubation-period", "A=1f"])
        params = Params(args)
Exemplo n.º 8
0
def default_model():
    return Model(Params())
Exemplo n.º 9
0
def params():
    return Params()