Exemplo n.º 1
0
def create_report_file(campaign_obj, report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        timestep = Outbreak_Start_Day
        new_infection = report_data_obj[KEY_NEW_INFECTIONS][timestep]
        statistical_population = report_data_obj[KEY_STATISTICAL_POPULATION][timestep]
        initial_effect = campaign_obj[KEY_INITIAL_EFFECT]
        demographic_coverage_v = campaign_obj[KEY_DEMOGRAPHIC_COVERAGE_V]
        demographic_coverage_o = campaign_obj[KEY_DEMOGRAPHIC_COVERAGE_O]
        immunity = initial_effect * demographic_coverage_v
        expected_new_infection = statistical_population * (1.0 - immunity) * demographic_coverage_o
        tolerance = 0.0 if expected_new_infection == 0.0 else 2e-2 * statistical_population
        if math.fabs(new_infection - expected_new_infection) > tolerance:
            success = False
            outfile.write("BAD: At time step {0}, new infections are {1} as reported, expected {2}.\n".format(
                timestep, new_infection, expected_new_infection))
        outfile.write(sft.format_success_msg(success))
    sft.plot_data_unsorted(new_infection,expected_new_infection,
                           label1= "Actual", label2 = "Expected",
                           ylabel="new infection", xlabel="red: actual data, blue: expected data",
                           title = "Actual new infection vs. expected new infection",
                           category = 'New_infection',show = True )
    if debug:
        print "SUMMARY: Success={0}\n".format(success)
    return success
Exemplo n.º 2
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        immunity = calc_immunity(debug)
        new_infections = []
        expected_new_infections = []
        timestep = Outbreak_Start_Day
        for i in range(len(KEY_NEW_INFECTIONS_GROUP)):
            new_infection = report_data_obj[KEY_NEW_INFECTIONS_GROUP[i]][timestep]
            statistical_population = report_data_obj[KEY_STATISTICAL_POPULATION_GROUP[i]][timestep]
            expected_new_infection = statistical_population * (1.0 - immunity[i])
            tolerance = 0.0 if expected_new_infection == 0.0 else 2e-2 * statistical_population
            if math.fabs(new_infection - expected_new_infection) > tolerance:
                success = False
                outfile.write("BAD: At time step {0}, {1} has {2} reported, expected {3}.\n".format(
                    timestep, KEY_NEW_INFECTIONS_GROUP[i], new_infection, expected_new_infection))
            new_infections.append(new_infection)
            expected_new_infections.append(expected_new_infection)
        outfile.write(sft.format_success_msg(success))
    sft.plot_data_unsorted(new_infections,expected_new_infections,
                           label1= "Actual", label2 = "Expected",
                           xlabel= "0: VaccineOnly, 1: 2Vaccines, 2: 3Vaccines, 3: 5Vaccines",ylabel="new infection",
                           title = "Actual new infection vs. expected new infection",
                           category = 'New_infections',show = True )
    if debug:
        print "SUMMARY: Success={0}\n".format(success)
    return success
Exemplo n.º 3
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        immunity = calc_immunity(debug)
        new_infections = []
        expected_new_infections = []
        timestep = Outbreak_Start_Day
        if not report_data_obj:
            success = False
            outfile.write("BAD: There is no data in the PropertyReport report")
        else:
            for i in range(Number_Repetitions):
                for j in range(len(KEY_NEW_INFECTIONS_GROUP)):
                    new_infection = report_data_obj[KEY_NEW_INFECTIONS_GROUP[j]][timestep]
                    statistical_population = report_data_obj[KEY_STATISTICAL_POPULATION_GROUP[j]][timestep]
                    expected_new_infection = statistical_population * (1.0 - immunity[j][i])
                    tolerance = 0.0 if expected_new_infection == 0.0 else 2e-2 * statistical_population
                    if math.fabs(new_infection - expected_new_infection) > tolerance:
                        success = False
                        outfile.write("BAD: At time step {0}, {1} has {2} reported, expected {3}.\n".format(
                            timestep, KEY_NEW_INFECTIONS_GROUP[j], new_infection, expected_new_infection))
                    new_infections.append(new_infection)
                    expected_new_infections.append(expected_new_infection)
                timestep += Timesteps_Between_Repetitions
        outfile.write(sft.format_success_msg(success))
    sft.plot_data_unsorted(new_infections,expected_new_infections,
                           label1= "Actual", label2 = "Expected",
                           xlabel= "group: 0-4 outbreak 1, 5-9 outbreak 2",ylabel="new infection",
                           title = "Actual new infection vs. expected new infection",
                           category = 'New_infections',show = True )
    if debug:
        print "SUMMARY: Success={0}\n".format(success)
    return success
Exemplo n.º 4
0
def create_report_file(param_obj, report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        new_infection_portions = calc_expected_new_infection_portion(param_obj, debug)
        new_infections = []
        expected_new_infections = []
        # skip the first outbreak, which gives the natual immunity
        timestep = Outbreak_Start_Day + Timesteps_Between_Repetitions
        for i in range(len(KEY_NEW_INFECTIONS_GROUP)):
            new_infection = report_data_obj[KEY_NEW_INFECTIONS_GROUP[i]][timestep]
            statistical_population = report_data_obj[KEY_STATISTICAL_POPULATION_GROUP[i]][timestep]
            expected_new_infection = statistical_population * (new_infection_portions[i])
            tolerance = 0.0 if expected_new_infection == 0.0 else 2e-2 * statistical_population
            if math.fabs(new_infection - expected_new_infection) > tolerance:
                success = False
                outfile.write("BAD: At time step {0}, {1} has {2} reported, expected {3}.\n".format(
                    timestep, KEY_NEW_INFECTIONS_GROUP[i], new_infection, expected_new_infection))
            new_infections.append(new_infection)
            expected_new_infections.append(expected_new_infection)
        outfile.write(sft.format_success_msg(success))
    sft.plot_data_unsorted(new_infections,expected_new_infections,
                           label1= "Actual", label2 = "Expected",
                           xlabel= "0: Control group, 1: Test group",ylabel="new infection",
                           title = "Actual new infection vs. expected new infection",
                           category = 'New_infections',show = True )
    if debug:
        print "SUMMARY: Success={0}\n".format(success)
    return success
def create_report_file(expected_infections_obj, actual_new_infections,
                       outbreak_day, report_name, debug):
    mean_expected = expected_infections_obj[ExpectedInfectionsKeys.MEAN]
    min_expected = expected_infections_obj[ExpectedInfectionsKeys.MIN]
    max_expected = expected_infections_obj[ExpectedInfectionsKeys.MAX]
    tolerance = expected_infections_obj[ExpectedInfectionsKeys.TOLERANCE]
    with open(report_name, 'a') as outfile:
        success = True
        if math.fabs(actual_new_infections - mean_expected) > tolerance:
            success = False
            outfile.write(("BAD: At time step {0}, new infections are {1} as reported, " +
                           "expected between {2} and {3}.\n").format(
                              outbreak_day, actual_new_infections,
                              min_expected, max_expected))
        else:
            outfile.write(("GOOD: At time step {0}, new infections are {1} as reported, " +
                           "expected between {2} and {3}.\n").format(
                              outbreak_day, actual_new_infections,
                              min_expected, max_expected))
        outfile.write(sft.format_success_msg(success))
    sft.plot_data_unsorted(actual_new_infections, mean_expected,
                           label1= "Actual", label2 = "Expected",
                           ylabel="new infection", xlabel="red: actual data, blue: expected data",
                           title = "Actual new infection vs. expected new infection",
                           category = 'New_infection',show = True )
    if debug:
        print( "SUMMARY: Success={0}\n".format(success) )
    return success
Exemplo n.º 6
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True

        timestep = Outbreak_Start_Day
        tb_effect = prime
        baseline = 2 # group 3_Control is the baseline
        test = 3 # group 4_Test is the test group
        # first outbreak
        pre_new_infection_baseline = report_data_obj[KEY_NEW_INFECTIONS_GROUP[baseline]][timestep]
        pre_new_infection_test= report_data_obj[KEY_NEW_INFECTIONS_GROUP[test]][timestep]
        # second outbreak
        timestep +=  Timesteps_Between_Repetitions
        new_infection_baseline = report_data_obj[KEY_NEW_INFECTIONS_GROUP[baseline]][timestep]
        statistical_population_baseline = report_data_obj[KEY_STATISTICAL_POPULATION_GROUP[baseline]][timestep]
        new_infection_test= report_data_obj[KEY_NEW_INFECTIONS_GROUP[test]][timestep]
        statistical_population_test = report_data_obj[KEY_STATISTICAL_POPULATION_GROUP[test]][timestep]
        # because expected_new_infection_test / ((1.0 - tb_effect)* statistical_population_test)= new_infection_baseline / statistical_population_baseline, so
        expected_new_infection_test = (1.0 - tb_effect) * new_infection_baseline * statistical_population_test / statistical_population_baseline
        tolerance = 0.0 if expected_new_infection_test == 0.0 else 2e-2 * statistical_population_test
        if math.fabs(new_infection_test - expected_new_infection_test) > tolerance:
            success = False
            outfile.write("BAD: At time step {0}, {1} reported new infections in Group 4_Test, expected {2}.\n".format(
                timestep, new_infection_test, expected_new_infection_test))

        sft.plot_data_unsorted([pre_new_infection_baseline, new_infection_baseline],[pre_new_infection_test, new_infection_test],
                               label1= "control_group", label2 = "test_group",
                               xlabel= "0: first outbreak, 1: second outbreak",ylabel="new infection",
                               title = "control vs. test",
                               category = 'New_infections',show = True )

        outfile.write(sft.format_success_msg(success))
        if debug:
            print "SUMMARY: Success={0}\n".format(success)
        return success
Exemplo n.º 7
0
def create_report_file(campaign_obj, report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        timestep = Outbreak_Start_Day
        new_infection = report_data_obj[KEY_NEW_INFECTIONS][timestep]
        statistical_population = report_data_obj[KEY_STATISTICAL_POPULATION][
            timestep]
        initial_effect = campaign_obj[KEY_INITIAL_EFFECT]
        demographic_coverage_v = campaign_obj[KEY_DEMOGRAPHIC_COVERAGE_V]
        demographic_coverage_o = campaign_obj[KEY_DEMOGRAPHIC_COVERAGE_O]
        immunity = initial_effect * demographic_coverage_v
        expected_new_infection = statistical_population * (
            1.0 - immunity) * demographic_coverage_o
        tolerance = 0.0 if expected_new_infection == 0.0 else 2e-2 * statistical_population
        if math.fabs(new_infection - expected_new_infection) > tolerance:
            success = False
            outfile.write(
                "BAD: At time step {0}, new infections are {1} as reported, expected {2}.\n"
                .format(timestep, new_infection, expected_new_infection))
        outfile.write(sft.format_success_msg(success))
    sft.plot_data_unsorted(
        new_infection,
        expected_new_infection,
        label1="Actual",
        label2="Expected",
        ylabel="new infection",
        xlabel="red: actual data, blue: expected data",
        title="Actual new infection vs. expected new infection",
        category='New_infection',
        show=True)
    if debug:
        print "SUMMARY: Success={0}\n".format(success)
    return success
Exemplo n.º 8
0
def calculate_infectiousness(new_infections, index, simulation_timestep,
                             total_timesteps, base_infectivity, baseline,
                             delay, rate, debug):
    infectiousness_ind = [0] * (int(total_timesteps / simulation_timestep) + 1)
    timestep = index * simulation_timestep
    for i in np.arange(timestep, total_timesteps, simulation_timestep):
        x = int(i / simulation_timestep)
        if i < delay:
            infectiousness_ind[x] = base_infectivity * baseline
        else:
            infectiousness_ind[x] = base_infectivity * (1.0 - (
                (1.0 - baseline) * math.exp((delay - i) / rate)))
    if index == 1 or timestep == 1:
        dtk_sft.plot_data_unsorted(
            infectiousness_ind,
            title="infectiousness for new infections at time step {0}".format(
                timestep),
            xlabel="time step / simulation_timestep{0}".format(
                simulation_timestep),
            ylabel="Exponential_Delay: {0} days, Exponential_Rate: {1} ".
            format(delay, rate),
            label1="infectiousness",
            label2="None",
            category="infectiousness_for_new_infections_at_time_step_{0}".
            format(timestep),
            show=True,
            line=True)
    infectiousness_ind = map(lambda x: x * new_infections, infectiousness_ind)
    return infectiousness_ind
def create_report_file(param_obj, output_df, report_df, report_name, debug):
    total_timesteps = int(param_obj[KEY_TOTAL_TIMESTEPS])
    simulation_timestep = float(param_obj[KEY_SIMULATION_TIMESTEP])
    base_infectivity = float(param_obj[KEY_BASE_INFECTIVITY])
    baseline = float(param_obj[KEY_INFECTIVITY_EXPONENTIAL_BASELINE])
    delay = float(param_obj[KEY_INFECTIVITY_EXPONENTIAL_DELAY])
    rate = float(param_obj[KEY_INFECTIVITY_EXPONENTIAL_RATE])
    infected = output_df[KEY_INFECTED]
    infectiousness = output_df[KEY_INFECTIOUSNESS]
    statpop = output_df[KEY_STAT_POP]
    new_infections = report_df[KEY_NEW_INFECTIONS]
    cumulative_infections = report_df[KEY_CUMULATIVE_INFECTIONS]
    dtk_sft.plot_data_unsorted(new_infections, cumulative_infections,
                               label1="new infections", label2="cumulative infections",
                               title="Exponential_Delay: {0} days, Exponential_Rate: {1} ".format(delay, rate),
                               xlabel="time step / simulation_timestep{0}".format(simulation_timestep), ylabel=None,
                               category='New_infections_vs_cumulative_infections',
                               show=True, line = True)

    with open(report_name, "w") as outfile:
        expected_infectiousness = [0]*(int(total_timesteps/simulation_timestep) + 1)
        pre_infected = int(infected[0])
        for index in range(1, len(infected)):
            new_infected = int(infected[index]) - pre_infected
            pre_infected = int(infected[index])
            if new_infected:
                new_expected_infectiousness = calculate_infectiousness(new_infected, index,
                                                                       simulation_timestep, total_timesteps,
                                                                       base_infectivity, baseline,
                                                                       delay, rate, debug)
                expected_infectiousness = map(sum, zip(expected_infectiousness, new_expected_infectiousness))

        success = True
        actual_infectiousness_all = []
        calc_infectiousness_all = []
        for index in range(len(infectiousness)):
            timestep = index * simulation_timestep
            actual_infectiousness = float(infectiousness[index])
            calc_infectiousness = expected_infectiousness[index] / float(statpop[index])
            actual_infectiousness_all.append(actual_infectiousness)
            calc_infectiousness_all.append(calc_infectiousness)
            tolerance = 0 if calc_infectiousness == 0 else 3e-2 * calc_infectiousness
            if math.fabs(actual_infectiousness - calc_infectiousness) > tolerance:
                success = False
                outfile.write("BAD: actual infectiousness at time step {0} is {1}, expected {2}.\n".format(timestep, actual_infectiousness, calc_infectiousness))
        if debug:
            with open("actual_vs_calc_infectiousness.txt", "w") as file:
                for i in range(len(actual_infectiousness_all)):
                    file.write("Time Step: {0}, actual infectiousnes: {1},"
                               " expected_infectiousness: {2}.\n".format(i*simulation_timestep, actual_infectiousness_all[i], calc_infectiousness_all[i]))
        dtk_sft.plot_data_unsorted(actual_infectiousness_all, calc_infectiousness_all,
                                   label1="actual infectiousness", label2="calc infectiousness",
                                   title="Exponential_Delay: {0} days, Exponential_Rate: {1} ".format(delay, rate),
                                   xlabel="time step / simulation_timestep{0}".format(simulation_timestep), ylabel="Infectiousness",
                                   category='Infectiousness',
                                   show=True, line = True)
        outfile.write(dtk_sft.format_success_msg(success))
        return success
Exemplo n.º 10
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        timestep = Outbreak_Start_Day
        effects = calc_effect(debug)
        new_infections = []
        new_disease_deaths = []
        expected_new_disease_deaths = []
        actual_effects = []
        pre_disease_death = 0

        for i in range(len(Interventions)):
            new_infection = report_data_obj[KEY_NEW_INFECTIONS][timestep]
            disease_death = report_data_obj[KEY_DISEASE_DEATHS][timestep]
            new_disease_death = disease_death - pre_disease_death
            effect = effects[i]
            expected_new_disease_death = (1.0 - effect) * new_infection
            tolerance = 0.0 if expected_new_disease_death == 0.0 else 3e-2 * new_infection
            actual_effect = 1.0 - new_disease_death/float(new_infection) if new_infection != 0 else 0.0
            if math.fabs(new_disease_death - expected_new_disease_death) > tolerance:
                success = False
                outfile.write("BAD: At time step {0}, outbreak {1}, {2} reported new disease death, expected {3}.\n".format(
                    timestep, Interventions[i], new_disease_death, expected_new_disease_death))
                outfile.write("actual MortalityBlocking effect is {0}, expected {1}.\n".format(actual_effect, effect))
            new_disease_deaths.append(new_disease_death)
            expected_new_disease_deaths.append(expected_new_disease_death)
            actual_effects.append(actual_effect)
            new_infections.append(new_infection)
            timestep += Timesteps_Between_Repetitions
            pre_disease_death = disease_death
        sft.plot_data_unsorted(new_disease_deaths,expected_new_disease_deaths,
                               label1= "Actual", label2 = "Expected",
                               xlabel= "outbreak",ylabel="disease death",
                               title = "Actual disease death vs. expected disease death",
                               category = 'Disease_death',show = True )
        sft.plot_data_unsorted(new_disease_deaths,new_infections,
                               label1= "death", label2 = "infection",
                               xlabel= "outbreak",ylabel="population",
                               title = "Actual disease death vs. new infections",
                               category = 'disease_death_vs_new_infections',show = True )
        if debug:
            with open("New_disease_death.txt", "w") as file:
                for i in range(len(new_disease_deaths)):
                    file.write("{0}, {1}.\n".format(new_disease_deaths[i],expected_new_disease_deaths[i]))
            with open("Effects.txt", "w") as file:
                for i in range(len(actual_effects)):
                    file.write("{0}, {1}.\n".format(actual_effects[i],effects[i]))

        outfile.write(sft.format_success_msg(success))
        if debug:
            print "SUMMARY: Success={0}\n".format(success)
        return success
Exemplo n.º 11
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        timestep = Outbreak_Start_Day
        effect = prime
        new_infections = []
        statistical_populations = []
        disease_deaths = []
        for i in range(len(KEY_NEW_INFECTIONS_GROUP)):
            new_infection = report_data_obj[KEY_NEW_INFECTIONS_GROUP[i]][timestep]
            statistical_population = report_data_obj[KEY_STATISTICAL_POPULATION_GROUP[i]][timestep]
            disease_death = report_data_obj[KEY_DISEASE_DEATHS_GROUP[i]][timestep + i/2] # disease death in the last two groups happen 1 day later than the first two groups.
            new_infections.append(new_infection)
            statistical_populations.append(statistical_population)
            disease_deaths.append(disease_death)
        # test acquisition blocking
        new_infection_seed_test = new_infections[1]
        statistical_population_seed_test = statistical_populations[1]
        expected_new_infection_seed_test = statistical_population_seed_test * (1.0 - effect)
        tolerance_1 = 0.0 if expected_new_infection_seed_test == 0.0 else 2e-2 * statistical_population_seed_test
        if math.fabs(new_infection_seed_test - expected_new_infection_seed_test) > tolerance_1:
            success = False
            outfile.write("BAD: At time step {0}, {1} reported new infections in Group 2_Seed_Test, expected {2}.\n".format(
                timestep, new_infection_seed_test, expected_new_infection_seed_test))
        # test transmission blocking
        new_infection_seed_control = new_infections[0]
        new_infection_control = new_infections[2]
        new_infection_test = new_infections[3]
        expected_new_infection_test = (1.0 - effect) * new_infection_control * new_infection_seed_test/float(new_infection_seed_control)
        statistical_population_test = statistical_populations[3]
        tolerance_2 = 0.0 if expected_new_infection_test == 0.0 else 2e-2 * statistical_population_test
        if math.fabs(new_infection_test - expected_new_infection_test) > tolerance_2:
            success = False
            outfile.write("BAD: At time step {0}, {1} reported new infections in Group 4_Test, expected {2}.\n".format(
                timestep, new_infectio_test, expected_new_infection_test))
        #test mortality blocking
        disease_death_seed_test = disease_deaths[1]
        expected_disease_death_seed_test = new_infection_seed_test * (1.0 - effect)
        tolerance_3 = 0.0 if expected_disease_death_seed_test == 0.0 else 2e-2 * new_infection_seed_test
        if math.fabs(disease_death_seed_test - expected_disease_death_seed_test) > tolerance_3:
            success = False
            outfile.write("BAD: At time step {0}, {1} reported disease deaths in Group 2_Seed_Test, expected {2}.\n".format(
                timestep, disease_death_seed_test, expected_disease_death_seed_test))
        outfile.write(sft.format_success_msg(success))
    sft.plot_data_unsorted(new_infections,disease_deaths,
                           label1= "new_infections", label2 = "disease_death",
                           xlabel= "0:1_Seed_Control, 1:2_Seed_Test, 2:3_Control, 4:3_Test",
                           title = "new_infections vs. disease_death",
                           category = 'New_infections_vs_disease_death',show = True )
    if debug:
        print "SUMMARY: Success={0}\n".format(success)
    return success
Exemplo n.º 12
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True

        timestep = Outbreak_Start_Day
        tb_effect = prime
        baseline = 2  # group 3_Control is the baseline
        test = 3  # group 4_Test is the test group
        # first outbreak
        pre_new_infection_baseline = report_data_obj[
            KEY_NEW_INFECTIONS_GROUP[baseline]][timestep]
        pre_new_infection_test = report_data_obj[
            KEY_NEW_INFECTIONS_GROUP[test]][timestep]
        # second outbreak
        timestep += Timesteps_Between_Repetitions
        new_infection_baseline = report_data_obj[
            KEY_NEW_INFECTIONS_GROUP[baseline]][timestep]
        statistical_population_baseline = report_data_obj[
            KEY_STATISTICAL_POPULATION_GROUP[baseline]][timestep]
        new_infection_test = report_data_obj[
            KEY_NEW_INFECTIONS_GROUP[test]][timestep]
        statistical_population_test = report_data_obj[
            KEY_STATISTICAL_POPULATION_GROUP[test]][timestep]
        # because expected_new_infection_test / ((1.0 - tb_effect)* statistical_population_test)= new_infection_baseline / statistical_population_baseline, so
        expected_new_infection_test = (
            1.0 - tb_effect
        ) * new_infection_baseline * statistical_population_test / statistical_population_baseline
        tolerance = 0.0 if expected_new_infection_test == 0.0 else 2e-2 * statistical_population_test
        if math.fabs(new_infection_test -
                     expected_new_infection_test) > tolerance:
            success = False
            outfile.write(
                "BAD: At time step {0}, {1} reported new infections in Group 4_Test, expected {2}.\n"
                .format(timestep, new_infection_test,
                        expected_new_infection_test))

        sft.plot_data_unsorted(
            [pre_new_infection_baseline, new_infection_baseline],
            [pre_new_infection_test, new_infection_test],
            label1="control_group",
            label2="test_group",
            xlabel="0: first outbreak, 1: second outbreak",
            ylabel="new infection",
            title="control vs. test",
            category='New_infections',
            show=True)

        outfile.write(sft.format_success_msg(success))
        if debug:
            print "SUMMARY: Success={0}\n".format(success)
        return success
Exemplo n.º 13
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        timestep = Outbreak_Start_Day
        tb_effects = calc_tb_effect(debug)
        tb_effect_baseline = float(tb_effects[0]) # use the number of new infection from the 1st outbreak as a baseline
        new_infection_baseline = report_data_obj[KEY_NEW_INFECTIONS_GROUP[1]][timestep]
        statistical_population = report_data_obj[KEY_STATISTICAL_POPULATION_GROUP[1]][timestep] # no any death
        new_infections = []
        expected_new_infections = []
        new_infections.append(new_infection_baseline)
        expected_new_infections.append(new_infection_baseline)
        actual_tb_effects = []
        actual_tb_effects.append(tb_effect_baseline)

        for i in range(1, len(Interventions)): # no need to test the 1st outbreak
            timestep += Timesteps_Between_Repetitions
            new_infection = report_data_obj[KEY_NEW_INFECTIONS_GROUP[1]][timestep]
            tb_effect = tb_effects[i]
            # because expected_new_infection / (1.0 - tb_effect) = new_infection_baseline / (1.0- tb_effect_baseline), so
            expected_new_infection = (1.0 - tb_effect) * new_infection_baseline / (1.0- tb_effect_baseline)
            tolerance = 0.0 if expected_new_infection == 0.0 else 2e-2 * statistical_population
            actual_tb_effect = 1.0 - (new_infection * (1.0 - tb_effect_baseline) / new_infection_baseline)
            if math.fabs(new_infection - expected_new_infection) > tolerance:
                success = False
                outfile.write("BAD: At time step {0}, outbreak {1}, {2} reported new infections, expected {3}.\n".format(
                    timestep, Interventions[i], new_infection, expected_new_infection))
                outfile.write("actual TransmissionBlocking effect is {0}, expected {1}.\n".format(actual_tb_effect, tb_effect))
            new_infections.append(new_infection)
            expected_new_infections.append(expected_new_infection)
            actual_tb_effects.append(actual_tb_effect)
        sft.plot_data_unsorted(new_infections,expected_new_infections,
                               label1= "Actual", label2 = "Expected",
                               xlabel= "outbreak",ylabel="new infection",
                               title = "Actual new infection vs. expected new infection",
                               category = 'New_infections',show = True )
        if debug:
            with open("New_infections.txt", "w") as file:
                for i in range(len(new_infections)):
                    file.write("{0}, {1}.\n".format(new_infections[i],expected_new_infections[i]))
            with open("Effects.txt", "w") as file:
                for i in range(len(actual_tb_effects)):
                    file.write("{0}, {1}.\n".format(actual_tb_effects[i],tb_effects[i]))

        outfile.write(sft.format_success_msg(success))
        if debug:
            print "SUMMARY: Success={0}\n".format(success)
        return success
Exemplo n.º 14
0
def application(output_folder="output",
                stdout_filename="test.txt",
                config_filename="config.json",
                campaign_filename="campaign.json",
                demographics_filename="demographics_100_overlay.json",
                insetchart_name="InsetChart.json",
                report_name=sft.sft_output_filename,
                debug=False):
    if debug:
        print "output_folder: " + output_folder
        print "stdout_filename: " + stdout_filename + "\n"
        print "config_filename: " + config_filename + "\n"
        print "campaign_filename: " + campaign_filename + "\n"
        print "demographics_filename: " + demographics_filename + "\n"
        print "insetchart_name: " + insetchart_name + "\n"
        print "report_name: " + report_name + "\n"
        print "debug: " + str(debug) + "\n"

    sft.wait_for_done()

    param_obj = ips.load_emod_parameters(config_filename, debug)
    campaign_obj = ips.load_campaign_file(campaign_filename, debug)
    demographics_obj = ips.load_demographics_file(demographics_filename, debug)
    report_data_obj = ips.parse_json_report(output_folder, insetchart_name,
                                            debug)

    sft.plot_data_unsorted(report_data_obj[KEY_NEW_INFECTIONS],
                           title="new infections",
                           label1="New Infections",
                           label2="NA",
                           xlabel="time steps",
                           ylabel="new infection",
                           category='New_infections',
                           show=True)
    sft.plot_data_unsorted(report_data_obj[KEY_STATISTICAL_POPULATION],
                           title="Statistical Population",
                           label1="Statistical Population",
                           label2="NA",
                           xlabel="time steps",
                           ylabel="Statistical Population",
                           category='Statistical_popupation',
                           show=True,
                           line=True)

    ips.create_report_file(param_obj, campaign_obj, demographics_obj,
                           report_data_obj, report_name, debug)
def create_report_file(param_obj, output_df, report_df, report_name, debug):
    total_timesteps = int(param_obj[KEY_TOTAL_TIMESTEPS])
    simulation_timestep = float(param_obj[KEY_SIMULATION_TIMESTEP])
    base_infectivity = float(param_obj[KEY_BASE_INFECTIVITY])
    amplitude = float(param_obj[KEY_AMPLITUDE])
    start_time = float(param_obj[KEY_START_TIME])
    end_time = float(param_obj[KEY_END_TIME])
    infected = output_df[KEY_INFECTED]
    infectiousness = output_df[KEY_INFECTIOUSNESS]
    statpop = output_df[KEY_STAT_POP]
    new_infections = report_df[KEY_NEW_INFECTIONS]
    if debug:
        dtk_sft.plot_data_unsorted(new_infections, label1="new infections", label2="NA",
                                   title="Start_time: {0} day, End_time: {1} day".format(start_time, end_time),
                                   xlabel="Time_Step_{0}_Days".format(simulation_timestep), ylabel=None,
                                   category='New_infections',
                                   show=True, line = True)
    with open(report_name, "w") as outfile:
        expected_infectiousness = []
        for index in range(len(infected)):
            infected_pop = int(infected[index])
            expected_infectiousness.append(calculate_infectiousness(infected_pop, index, simulation_timestep,
                                                                    start_time, end_time,
                                                                    base_infectivity, amplitude, debug))
        success = True
        actual_infectiousness_all = []
        calc_infectiousness_all = []
        for index in range(len(infectiousness)):
            timestep = index * simulation_timestep
            actual_infectiousness = float(infectiousness[index])
            calc_infectiousness = expected_infectiousness[index] / float(statpop[index])
            actual_infectiousness_all.append(actual_infectiousness)
            calc_infectiousness_all.append(calc_infectiousness)
            tolerance = 0 if calc_infectiousness == 0 else 5e-2 * calc_infectiousness
            if  math.fabs(actual_infectiousness - calc_infectiousness) > tolerance:
                success = False
                outfile.write("BAD: actual infectiousness at time step {0} is {1}, expected {2}.\n".format(timestep, actual_infectiousness, calc_infectiousness))
        outfile.write(dtk_sft.format_success_msg(success))
    dtk_sft.plot_data_unsorted(actual_infectiousness_all, calc_infectiousness_all,
                               label1="actual infectiousness", label2="calc infectiousness",
                               title="Start_time: {0} day, End_time: {1} day".format(start_time, end_time),
                               xlabel="Time_Step_{0}_Days".format(simulation_timestep), ylabel="Infectiousness",
                               category='Infectiousness',
                               show=True, line = True)
    return success
def calculate_infectiousness(new_infections, index, simulation_timestep, total_timesteps, base_infectivity, baseline, delay, rate, debug):
    infectiousness_ind = [0]*(int(total_timesteps/simulation_timestep) + 1)
    timestep = index * simulation_timestep
    for i in np.arange(timestep, total_timesteps, simulation_timestep):
        x = int(i/simulation_timestep)
        if i < delay:
            infectiousness_ind[x] = base_infectivity * baseline
        else:
            infectiousness_ind[x] = base_infectivity * (1.0 - ((1.0 - baseline)* math.exp((delay -i)/rate)))
    if index == 1 or timestep == 1:
        dtk_sft.plot_data_unsorted(infectiousness_ind,
                                   title="infectiousness for new infections at time step {0}".format(timestep),
                                   xlabel="time step / simulation_timestep{0}".format(simulation_timestep),
                                   ylabel="Exponential_Delay: {0} days, Exponential_Rate: {1} ".format(delay, rate),
                                   label1="infectiousness",
                                   label2="None",
                                   category="infectiousness_for_new_infections_at_time_step_{0}".format(timestep),
                                   show=True, line = True)
    infectiousness_ind = map(lambda x : x * new_infections, infectiousness_ind)
    return infectiousness_ind
Exemplo n.º 17
0
def application( output_folder="output", stdout_filename="test.txt",
                 config_filename="config.json",campaign_filename="campaign.json",
                 demographics_filename = "demographics_100_overlay.json",
                 insetchart_name="InsetChart.json",
                 report_name=sft.sft_output_filename,
                 debug=False):
    if debug:
        print "output_folder: " + output_folder
        print "stdout_filename: " + stdout_filename+ "\n"
        print "config_filename: " + config_filename + "\n"
        print "campaign_filename: " + campaign_filename + "\n"
        print "demographics_filename: " + demographics_filename + "\n"
        print "insetchart_name: " + insetchart_name + "\n"
        print "report_name: " + report_name + "\n"
        print "debug: " + str(debug) + "\n"

    sft.wait_for_done()

    param_obj = ips.load_emod_parameters(config_filename, debug)
    campaign_obj =  ips.load_campaign_file(campaign_filename, debug)
    demographics_obj =  ips.load_demographics_file(demographics_filename, debug)
    report_data_obj =  ips.parse_json_report(output_folder, insetchart_name, debug)

    sft.plot_data_unsorted(report_data_obj[KEY_NEW_INFECTIONS],
                           title="new infections",
                           label1= "New Infections",
                           label2 = "NA",
                           xlabel="time steps", ylabel="new infection",
                           category = 'New_infections',
                           show = True )
    sft.plot_data_unsorted(report_data_obj[KEY_STATISTICAL_POPULATION],
                           title="Statistical Population",
                           label1= "Statistical Population",
                           label2 = "NA",
                           xlabel = "time steps", ylabel="Statistical Population",
                           category = 'Statistical_popupation',
                           show = True, line = True)

    ips.create_report_file(param_obj, campaign_obj, demographics_obj, report_data_obj, report_name, debug)
Exemplo n.º 18
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        timestep = Outbreak_Start_Day
        effect_a = Prime_Acquire
        effect_t = Prime_Transmit
        effect_m = Prime_Mortality
        new_infections = []
        statistical_populations = []
        new_disease_deaths = []
        for x in range(Number_Repetitions):
            num_group = len(KEY_NEW_INFECTIONS_GROUP)
            for i in range(num_group):
                new_infection = report_data_obj[
                    KEY_NEW_INFECTIONS_GROUP[i]][timestep]
                statistical_population = report_data_obj[
                    KEY_STATISTICAL_POPULATION_GROUP[i]][timestep]
                pre_disease_death = report_data_obj[KEY_DISEASE_DEATHS_GROUP[i]][
                    timestep + i / 2 -
                    1]  # disease death in the last 2 groups happen 1 day later than the first 2 groups.
                disease_death = report_data_obj[KEY_DISEASE_DEATHS_GROUP[i]][
                    timestep + i / 2]
                new_disease_death = disease_death - pre_disease_death
                new_infections.append(new_infection)
                statistical_populations.append(statistical_population)
                new_disease_deaths.append(new_disease_death)
            # test acquisition blocking
            new_infection_seed_test = new_infections[1 + x * num_group]
            statistical_population_seed_test = statistical_populations[
                1 + x * num_group]
            expected_new_infection_seed_test = statistical_population_seed_test * (
                1.0 - effect_a) * Outbreak_Demographic_Coverage
            tolerance_1 = 0.0 if expected_new_infection_seed_test == 0.0 else 2e-2 * statistical_population_seed_test
            if math.fabs(new_infection_seed_test -
                         expected_new_infection_seed_test) > tolerance_1:
                success = False
                outfile.write(
                    "BAD: At time step {0}, {1} reported new infections in Group 2_Seed_Test, expected {2}.\n"
                    .format(timestep, new_infection_seed_test,
                            expected_new_infection_seed_test))
            # test transmission blocking
            new_infection_seed_control = new_infections[0 + x * num_group]
            new_infection_control = new_infections[2 + x * num_group]
            new_infection_test = new_infections[3 + x * num_group]
            expected_new_infection_test = (
                1.0 - effect_t
            ) * new_infection_control * new_infection_seed_test / float(
                new_infection_seed_control)
            statistical_population_test = statistical_populations[3]
            tolerance_2 = 0.0 if expected_new_infection_test == 0.0 else 2e-2 * statistical_population_test
            if math.fabs(new_infection_test -
                         expected_new_infection_test) > tolerance_2:
                success = False
                outfile.write(
                    "BAD: At time step {0}, {1} reported new infections in Group 4_Test, expected {2}.\n"
                    .format(timestep, new_infection_test,
                            expected_new_infection_test))
            #test mortality blocking
            disease_death_seed_test = new_disease_deaths[1 + x * num_group]
            expected_disease_death_seed_test = new_infection_seed_test * (
                1.0 - effect_m)
            tolerance_3 = 0.0 if expected_disease_death_seed_test == 0.0 else 2e-2 * new_infection_seed_test
            if math.fabs(disease_death_seed_test -
                         expected_disease_death_seed_test) > tolerance_3:
                success = False
                outfile.write(
                    "BAD: At time step {0}, {1} reported disease deaths in Group 2_Seed_Test, expected {2}.\n"
                    .format(timestep, disease_death_seed_test,
                            expected_disease_death_seed_test))
            timestep += Timesteps_Between_Repetitions
            effect_a = effect_a + (1.0 - effect_a) * Boost_Acquire
            effect_t = effect_t + (1.0 - effect_t) * Boost_Transmit
            effect_m = effect_m + (1.0 - effect_m) * Boost_Mortality
        outfile.write(sft.format_success_msg(success))
    sft.plot_data_unsorted(
        new_infections,
        new_disease_deaths,
        label1="new_infections",
        label2="disease_death",
        xlabel="0&4:Seed_Control, 1&5:Seed_Test, 2&6:Control, 4&7:Test",
        title="new_infections vs. new_disease_death",
        category='New_infections_vs_new_disease_death',
        show=True)
    if debug:
        print "SUMMARY: Success={0}\n".format(success)
    return success
Exemplo n.º 19
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        timestep = Outbreak_Start_Day
        effect_a = Prime_Acquire
        effect_t = Prime_Transmit
        effect_m = Prime_Mortality
        new_infections = []
        statistical_populations = []
        new_disease_deaths = []
        for x in range(Number_Repetitions):
            num_group = len(KEY_NEW_INFECTIONS_GROUP)
            for i in range(num_group):
                new_infection = report_data_obj[KEY_NEW_INFECTIONS_GROUP[i]][timestep]
                statistical_population = report_data_obj[KEY_STATISTICAL_POPULATION_GROUP[i]][timestep]
                pre_disease_death = report_data_obj[KEY_DISEASE_DEATHS_GROUP[i]][timestep + i/2 - 1] # disease death in the last 2 groups happen 1 day later than the first 2 groups.
                disease_death = report_data_obj[KEY_DISEASE_DEATHS_GROUP[i]][timestep + i/2]
                new_disease_death = disease_death - pre_disease_death
                new_infections.append(new_infection)
                statistical_populations.append(statistical_population)
                new_disease_deaths.append(new_disease_death)
            # test acquisition blocking
            new_infection_seed_test = new_infections[1 + x * num_group]
            statistical_population_seed_test = statistical_populations[1 + x * num_group]
            expected_new_infection_seed_test = statistical_population_seed_test * (1.0 - effect_a) * Outbreak_Demographic_Coverage
            tolerance_1 = 0.0 if expected_new_infection_seed_test == 0.0 else 2e-2 * statistical_population_seed_test
            if math.fabs(new_infection_seed_test - expected_new_infection_seed_test) > tolerance_1:
                success = False
                outfile.write("BAD: At time step {0}, {1} reported new infections in Group 2_Seed_Test, expected {2}.\n".format(
                    timestep, new_infection_seed_test, expected_new_infection_seed_test))
            # test transmission blocking
            new_infection_seed_control = new_infections[0 + x * num_group]
            new_infection_control = new_infections[2 + x * num_group]
            new_infection_test = new_infections[3+ x * num_group]
            expected_new_infection_test = (1.0 - effect_t) * new_infection_control * new_infection_seed_test/float(new_infection_seed_control)
            statistical_population_test = statistical_populations[3]
            tolerance_2 = 0.0 if expected_new_infection_test == 0.0 else 2e-2 * statistical_population_test
            if math.fabs(new_infection_test - expected_new_infection_test) > tolerance_2:
                success = False
                outfile.write("BAD: At time step {0}, {1} reported new infections in Group 4_Test, expected {2}.\n".format(
                    timestep, new_infection_test, expected_new_infection_test))
            #test mortality blocking
            disease_death_seed_test = new_disease_deaths[1 + x * num_group]
            expected_disease_death_seed_test = new_infection_seed_test * (1.0 - effect_m)
            tolerance_3 = 0.0 if expected_disease_death_seed_test == 0.0 else 2e-2 * new_infection_seed_test
            if math.fabs(disease_death_seed_test - expected_disease_death_seed_test) > tolerance_3:
                success = False
                outfile.write("BAD: At time step {0}, {1} reported disease deaths in Group 2_Seed_Test, expected {2}.\n".format(
                    timestep, disease_death_seed_test, expected_disease_death_seed_test))
            timestep += Timesteps_Between_Repetitions
            effect_a = effect_a + (1.0 - effect_a) * Boost_Acquire
            effect_t = effect_t + (1.0 - effect_t) * Boost_Transmit
            effect_m = effect_m + (1.0 - effect_m) * Boost_Mortality
        outfile.write(sft.format_success_msg(success))
    sft.plot_data_unsorted(new_infections,new_disease_deaths,
                           label1= "new_infections", label2 = "disease_death",
                           xlabel= "0&4:Seed_Control, 1&5:Seed_Test, 2&6:Control, 4&7:Test",
                           title = "new_infections vs. new_disease_death",
                           category = 'New_infections_vs_new_disease_death',show = True )
    if debug:
        print "SUMMARY: Success={0}\n".format(success)
    return success
Exemplo n.º 20
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        timestep = Outbreak_Start_Day
        tb_effects = calc_tb_effect(debug)
        tb_effect_baseline = float(
            tb_effects[0]
        )  # use the number of new infection from the 1st outbreak as a baseline
        new_infection_baseline = report_data_obj[
            KEY_NEW_INFECTIONS_GROUP[1]][timestep]
        statistical_population = report_data_obj[
            KEY_STATISTICAL_POPULATION_GROUP[1]][timestep]  # no any death
        new_infections = []
        expected_new_infections = []
        new_infections.append(new_infection_baseline)
        expected_new_infections.append(new_infection_baseline)
        actual_tb_effects = []
        actual_tb_effects.append(tb_effect_baseline)

        for i in range(1,
                       len(Interventions)):  # no need to test the 1st outbreak
            timestep += Timesteps_Between_Repetitions
            new_infection = report_data_obj[
                KEY_NEW_INFECTIONS_GROUP[1]][timestep]
            tb_effect = tb_effects[i]
            # because expected_new_infection / (1.0 - tb_effect) = new_infection_baseline / (1.0- tb_effect_baseline), so
            expected_new_infection = (1.0 -
                                      tb_effect) * new_infection_baseline / (
                                          1.0 - tb_effect_baseline)
            tolerance = 0.0 if expected_new_infection == 0.0 else 2e-2 * statistical_population
            actual_tb_effect = 1.0 - (new_infection *
                                      (1.0 - tb_effect_baseline) /
                                      new_infection_baseline)
            if math.fabs(new_infection - expected_new_infection) > tolerance:
                success = False
                outfile.write(
                    "BAD: At time step {0}, outbreak {1}, {2} reported new infections, expected {3}.\n"
                    .format(timestep, Interventions[i], new_infection,
                            expected_new_infection))
                outfile.write(
                    "actual TransmissionBlocking effect is {0}, expected {1}.\n"
                    .format(actual_tb_effect, tb_effect))
            new_infections.append(new_infection)
            expected_new_infections.append(expected_new_infection)
            actual_tb_effects.append(actual_tb_effect)
        sft.plot_data_unsorted(
            new_infections,
            expected_new_infections,
            label1="Actual",
            label2="Expected",
            xlabel="outbreak",
            ylabel="new infection",
            title="Actual new infection vs. expected new infection",
            category='New_infections',
            show=True)
        if debug:
            with open("New_infections.txt", "w") as file:
                for i in range(len(new_infections)):
                    file.write("{0}, {1}.\n".format(
                        new_infections[i], expected_new_infections[i]))
            with open("Effects.txt", "w") as file:
                for i in range(len(actual_tb_effects)):
                    file.write("{0}, {1}.\n".format(actual_tb_effects[i],
                                                    tb_effects[i]))

        outfile.write(sft.format_success_msg(success))
        if debug:
            print "SUMMARY: Success={0}\n".format(success)
        return success
Exemplo n.º 21
0
def create_report_file(param_obj, output_df, report_df, report_name, debug):
    total_timesteps = int(param_obj[KEY_TOTAL_TIMESTEPS])
    simulation_timestep = float(param_obj[KEY_SIMULATION_TIMESTEP])
    base_infectivity = float(param_obj[KEY_BASE_INFECTIVITY])
    baseline = float(param_obj[KEY_INFECTIVITY_EXPONENTIAL_BASELINE])
    delay = float(param_obj[KEY_INFECTIVITY_EXPONENTIAL_DELAY])
    rate = float(param_obj[KEY_INFECTIVITY_EXPONENTIAL_RATE])
    infected = output_df[KEY_INFECTED]
    infectiousness = output_df[KEY_INFECTIOUSNESS]
    statpop = output_df[KEY_STAT_POP]
    new_infections = report_df[KEY_NEW_INFECTIONS]
    cumulative_infections = report_df[KEY_CUMULATIVE_INFECTIONS]
    dtk_sft.plot_data_unsorted(
        new_infections,
        cumulative_infections,
        label1="new infections",
        label2="cumulative infections",
        title="Exponential_Delay: {0} days, Exponential_Rate: {1} ".format(
            delay, rate),
        xlabel="time step / simulation_timestep{0}".format(
            simulation_timestep),
        ylabel=None,
        category='New_infections_vs_cumulative_infections',
        show=True,
        line=True)

    with open(report_name, "w") as outfile:
        expected_infectiousness = [0] * (
            int(total_timesteps / simulation_timestep) + 1)
        pre_infected = int(infected[0])
        for index in range(1, len(infected)):
            new_infected = int(infected[index]) - pre_infected
            pre_infected = int(infected[index])
            if new_infected:
                new_expected_infectiousness = calculate_infectiousness(
                    new_infected, index, simulation_timestep, total_timesteps,
                    base_infectivity, baseline, delay, rate, debug)
                expected_infectiousness = map(
                    sum,
                    zip(expected_infectiousness, new_expected_infectiousness))

        success = True
        actual_infectiousness_all = []
        calc_infectiousness_all = []
        for index in range(len(infectiousness)):
            timestep = index * simulation_timestep
            actual_infectiousness = float(infectiousness[index])
            calc_infectiousness = expected_infectiousness[index] / float(
                statpop[index])
            actual_infectiousness_all.append(actual_infectiousness)
            calc_infectiousness_all.append(calc_infectiousness)
            tolerance = 0 if calc_infectiousness == 0 else 3e-2 * calc_infectiousness
            if math.fabs(actual_infectiousness -
                         calc_infectiousness) > tolerance:
                success = False
                outfile.write(
                    "BAD: actual infectiousness at time step {0} is {1}, expected {2}.\n"
                    .format(timestep, actual_infectiousness,
                            calc_infectiousness))
        if debug:
            with open("actual_vs_calc_infectiousness.txt", "w") as file:
                for i in range(len(actual_infectiousness_all)):
                    file.write("Time Step: {0}, actual infectiousnes: {1},"
                               " expected_infectiousness: {2}.\n".format(
                                   i * simulation_timestep,
                                   actual_infectiousness_all[i],
                                   calc_infectiousness_all[i]))
        dtk_sft.plot_data_unsorted(
            actual_infectiousness_all,
            calc_infectiousness_all,
            label1="actual infectiousness",
            label2="calc infectiousness",
            title="Exponential_Delay: {0} days, Exponential_Rate: {1} ".format(
                delay, rate),
            xlabel="time step / simulation_timestep{0}".format(
                simulation_timestep),
            ylabel="Infectiousness",
            category='Infectiousness',
            show=True,
            line=True)
        outfile.write(dtk_sft.format_success_msg(success))
        return success
Exemplo n.º 22
0
def create_report_file(report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        timestep = Outbreak_Start_Day
        effect = prime
        new_infections = []
        statistical_populations = []
        disease_deaths = []
        for i in range(len(KEY_NEW_INFECTIONS_GROUP)):
            new_infection = report_data_obj[
                KEY_NEW_INFECTIONS_GROUP[i]][timestep]
            statistical_population = report_data_obj[
                KEY_STATISTICAL_POPULATION_GROUP[i]][timestep]
            disease_death = report_data_obj[KEY_DISEASE_DEATHS_GROUP[i]][
                timestep + i /
                2]  # disease death in the last two groups happen 1 day later than the first two groups.
            new_infections.append(new_infection)
            statistical_populations.append(statistical_population)
            disease_deaths.append(disease_death)
        # test acquisition blocking
        new_infection_seed_test = new_infections[1]
        statistical_population_seed_test = statistical_populations[1]
        expected_new_infection_seed_test = statistical_population_seed_test * (
            1.0 - effect)
        tolerance_1 = 0.0 if expected_new_infection_seed_test == 0.0 else 2e-2 * statistical_population_seed_test
        if math.fabs(new_infection_seed_test -
                     expected_new_infection_seed_test) > tolerance_1:
            success = False
            outfile.write(
                "BAD: At time step {0}, {1} reported new infections in Group 2_Seed_Test, expected {2}.\n"
                .format(timestep, new_infection_seed_test,
                        expected_new_infection_seed_test))
        # test transmission blocking
        new_infection_seed_control = new_infections[0]
        new_infection_control = new_infections[2]
        new_infection_test = new_infections[3]
        expected_new_infection_test = (
            1.0 -
            effect) * new_infection_control * new_infection_seed_test / float(
                new_infection_seed_control)
        statistical_population_test = statistical_populations[3]
        tolerance_2 = 0.0 if expected_new_infection_test == 0.0 else 2e-2 * statistical_population_test
        if math.fabs(new_infection_test -
                     expected_new_infection_test) > tolerance_2:
            success = False
            outfile.write(
                "BAD: At time step {0}, {1} reported new infections in Group 4_Test, expected {2}.\n"
                .format(timestep, new_infectio_test,
                        expected_new_infection_test))
        #test mortality blocking
        disease_death_seed_test = disease_deaths[1]
        expected_disease_death_seed_test = new_infection_seed_test * (1.0 -
                                                                      effect)
        tolerance_3 = 0.0 if expected_disease_death_seed_test == 0.0 else 2e-2 * new_infection_seed_test
        if math.fabs(disease_death_seed_test -
                     expected_disease_death_seed_test) > tolerance_3:
            success = False
            outfile.write(
                "BAD: At time step {0}, {1} reported disease deaths in Group 2_Seed_Test, expected {2}.\n"
                .format(timestep, disease_death_seed_test,
                        expected_disease_death_seed_test))
        outfile.write(sft.format_success_msg(success))
    sft.plot_data_unsorted(
        new_infections,
        disease_deaths,
        label1="new_infections",
        label2="disease_death",
        xlabel="0:1_Seed_Control, 1:2_Seed_Test, 2:3_Control, 4:3_Test",
        title="new_infections vs. disease_death",
        category='New_infections_vs_disease_death',
        show=True)
    if debug:
        print "SUMMARY: Success={0}\n".format(success)
    return success
Exemplo n.º 23
0
def create_report_file(param_obj, output_df, report_df, report_name, debug):
    total_timesteps = int(param_obj[KEY_TOTAL_TIMESTEPS])
    simulation_timestep = float(param_obj[KEY_SIMULATION_TIMESTEP])
    base_infectivity = float(param_obj[KEY_BASE_INFECTIVITY])
    amplitude = float(param_obj[KEY_AMPLITUDE])
    start_time = float(param_obj[KEY_START_TIME])
    end_time = float(param_obj[KEY_END_TIME])
    infected = output_df[KEY_INFECTED]
    infectiousness = output_df[KEY_INFECTIOUSNESS]
    statpop = output_df[KEY_STAT_POP]
    new_infections = report_df[KEY_NEW_INFECTIONS]
    if debug:
        dtk_sft.plot_data_unsorted(
            new_infections,
            label1="new infections",
            label2="NA",
            title="Start_time: {0} day, End_time: {1} day".format(
                start_time, end_time),
            xlabel="Time_Step_{0}_Days".format(simulation_timestep),
            ylabel=None,
            category='New_infections',
            show=True,
            line=True)
    with open(report_name, "w") as outfile:
        expected_infectiousness = []
        for index in range(len(infected)):
            infected_pop = int(infected[index])
            expected_infectiousness.append(
                calculate_infectiousness(infected_pop, index,
                                         simulation_timestep, start_time,
                                         end_time, base_infectivity, amplitude,
                                         debug))
        success = True
        actual_infectiousness_all = []
        calc_infectiousness_all = []
        for index in range(len(infectiousness)):
            timestep = index * simulation_timestep
            actual_infectiousness = float(infectiousness[index])
            calc_infectiousness = expected_infectiousness[index] / float(
                statpop[index])
            actual_infectiousness_all.append(actual_infectiousness)
            calc_infectiousness_all.append(calc_infectiousness)
            tolerance = 0 if calc_infectiousness == 0 else 5e-2 * calc_infectiousness
            if math.fabs(actual_infectiousness -
                         calc_infectiousness) > tolerance:
                success = False
                outfile.write(
                    "BAD: actual infectiousness at time step {0} is {1}, expected {2}.\n"
                    .format(timestep, actual_infectiousness,
                            calc_infectiousness))
        outfile.write(dtk_sft.format_success_msg(success))
    dtk_sft.plot_data_unsorted(
        actual_infectiousness_all,
        calc_infectiousness_all,
        label1="actual infectiousness",
        label2="calc infectiousness",
        title="Start_time: {0} day, End_time: {1} day".format(
            start_time, end_time),
        xlabel="Time_Step_{0}_Days".format(simulation_timestep),
        ylabel="Infectiousness",
        category='Infectiousness',
        show=True,
        line=True)
    return success