Example #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
Example #2
0
def create_report_file(active_TB_treatments, relapses,
                       tb_drug_relapse_rate_hiv, drug_start_timestep,
                       report_name):
    with open(report_name, "w") as outfile:
        print(str(relapses), str(tb_drug_relapse_rate_hiv))
        success = True
        if sum(active_TB_treatments) == 0 or sum(relapses) == 0:
            success = False
            outfile.write(sft.sft_no_test_data)
        for x in range(drug_start_timestep + 1, len(active_TB_treatments) - 1):
            active_TB_treatment = int(active_TB_treatments[x])
            relapse = relapses[x + 1]
            result = sft.test_binomial_99ci(relapse,
                                            active_TB_treatment,
                                            tb_drug_relapse_rate_hiv,
                                            outfile,
                                            category="time step {}".format(x +
                                                                           1))
            if not result:
                success = False
                outfile.write(
                    "BAD: test fails for rate = {0} at time step {1}.\n".
                    format(tb_drug_relapse_rate_hiv, x + 1))

        outfile.write(sft.format_success_msg(success))
        return success
Example #3
0
def create_report_file(param_obj, stdout_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        config_name = param_obj[KEY_CONFIG_NAME]
        outfile.write("Config_name = {}\n".format(config_name))
        success = True
        cd4_progressor_multiplier = param_obj[KEY_CD4_PROGRESSION_MULTIPLIER][0]  # all values should be the same
        fast_progressor_fraction = param_obj[KEY_FAST_PROGRESSOR_FRACTION]
        results = []
        for key, value in stdout_data_obj[0].items():
            if key > 2 and value[1] > 35:  # ignoring the smaller latent #s because there's not enough data for the test
                result = dtk_sft.test_binomial_95ci(value[0], value[1],
                                                    fast_progressor_fraction * cd4_progressor_multiplier, outfile,
                                                    "Time {} :{} new exogenous infections for {} latent infections."
                                                     "\n".format(key, value[0], value[1]))
                results.append(result)
        if not results:
            outfile.write("No results.\n")
            success = False
        elif results.count(False) > 2:  # not enough data for a binomial test on the binomial results,
            success = False           # so, the closest we can come is "less than two False is good".
            outfile.write("{} out of {} tests failed.\n".format(results.count(False), len(results)))
        outfile.write(dtk_sft.format_success_msg(success))
        dtk_sft.plot_data(stdout_data_obj[1], title="Exogenous infections for tracked time steps",
                          category="exogenous_infections_tb_hiv_art")

    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
Example #5
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
Example #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
Example #7
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
Example #8
0
def create_report_file(param_obj, report_data_obj, campaign_obj, report_name,
                       debug):
    with open(report_name, "w") as outfile:
        config_name = param_obj[KEY_CONFIG_NAME]
        outfile.write("Config_name = {}\n".format(config_name))
        success = True
        new_infections = report_data_obj[KEY_NEW_INFECTIONS]
        start_day = campaign_obj[KEY_START_DAY]
        tb_mdr_fitness_multiplier = param_obj[KEY_TB_MDR_FITNESS_MULTIPLIER]
        if tb_mdr_fitness_multiplier:
            success = False
            outfile.write(
                "BAD: TB_MDR_Fitness_Multiplier must set to 0 in this test, it's {} now, please fix"
                "the test.\n".format(tb_mdr_fitness_multiplier))
        if not len(new_infections):
            success = False
            outfile.write(sft.sft_no_test_data)
        if len(new_infections) < start_day + 1:
            success = False
            outfile.write(
                "BAD: Outbreak start day is longer than simulation duration, please extend the duration.\n"
            )
        for i in range(int(start_day) + 1, len(new_infections)):
            if new_infections[i] > 0:
                success = False
                outfile.write(
                    "BAD: Expected no new TB infection at timestep {0}, but found {1} new infections from"
                    "insetchart.json.\n".format(i, new_infections[i]))
        outfile.write(sft.format_success_msg(success))
    if debug:
        print("SUMMARY: Success={0}\n".format(success))
    return success
Example #9
0
def create_report_file(param_obj, report_data_obj, campaign_obj, report_name,
                       debug):
    with open(report_name, "w") as outfile:
        config_name = param_obj[KEY_CONFIG_NAME]
        outfile.write(
            "Config_name = {}\nInfect all individuals with MDR TB at time= 1. "
            "Check that all infected individuals have MDR TB and not DS TB.\n".
            format(config_name))
        success = True
        mdr_prevalence = report_data_obj[KEY_MDR_TB_Prevalence]
        start_day = campaign_obj[KEY_START_DAY]
        if not len(mdr_prevalence):
            success = False
            outfile.write(sft.sft_no_test_data)
        if len(mdr_prevalence) < start_day + 1:
            success = False
            outfile.write(
                "BAD: Outbreak start day is longer than simulation duration, please extend the duration.\n"
            )
        if mdr_prevalence[int(start_day)] != 1:
            success = False
            outfile.write(
                "BAD: Expected all individuals infected with MDR TB at timestep {0}, but only {1} proportion "
                "of the population has MDR TB.\n".format(
                    start_day, mdr_prevalence[int(start_day)]))
        outfile.write(sft.format_success_msg(success))
    if debug:
        print("SUMMARY: Success={0}\n".format(success))
    return success
Example #10
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
Example #11
0
def application( report_file ):
    #pdb.set_trace()
    #print( "Post-processing: " + report_file ) 
    lines = []
    with open( "test.txt" ) as logfile:
        for line in logfile:
            if re.search( "Initializing Typhoid immunity object", line ) and re.search( "age=0.000000", line ):
                lines.append( line )

    cdj = json.loads( open( "config.json" ).read() )["parameters"]
    #c1 = cdj["Infectiousness_Asymptomatic_Naive_1"]
    success = True
    with open( sft.sft_output_filename, "w" ) as report_file:
        if len( lines ) == 0:
            success = False
            report_file.write( "Found no data matching test case.\n" )
        else:
            for line in lines:
                immunity = float( get_val( " immunity modifier", line ) )
                if immunity != 1.000:
                    success = False
                    report_file.write( "BAD: immunity for newborn={0} instead of 1.0\n".format( immunity ) )
                    
        if success:
            report_file.write( sft.format_success_msg( success ) )
            os.remove( "test.txt" )
Example #12
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
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
def application( report_file ):
    #pdb.set_trace()
    #print( "Post-processing: " + report_file )

    cdj = json.loads( open( "config.json" ).read() )["parameters"]
    tsf = cdj["Typhoid_Symptomatic_Fraction"]
    start_time=cdj["Start_Time"]
    lines = []
    timestep=start_time
    with open( "test.txt" ) as logfile:
        for line in logfile:
            if re.search("Update\(\): Time:",line):
                #calculate time step
                timestep+=1
            if re.search( "Infection stage transition", line ):
                #append time step and all Infection stage transition to list
                line="TimeStep: "+str(timestep)+ " " + line
                lines.append( line )

    success = True
    with open( sft.sft_output_filename, "w" ) as report_file:
        if len( lines ) == 0:
            success = False
            report_file.write( "Found no data matching test case.\n" )
        else:
            subcount = 0
            acutecount = 0
            for line in lines:
                if (((not re.search("->SubClinical:", line))  and (not re.search("->Acute:", line))) and re.search(", Prepatent->", line)) or (re.search("->SubClinical:", line) or re.search("->Acute:", line)) and not re.search(", Prepatent->", line):
                    success = False
                    ind_id=get_val("Individual=", line)
                    current_infection_stage=get_char_after("->", line)
                    previous_infection_stage=get_char_before("->", line)
                    report_file.write("BAD: individuals {0} went to {1} state from {2} state, expected Prepatent->Acute or Prepatent->SubClinical.\n".format(ind_id,current_infection_stage, previous_infection_stage))
                elif re.search(", Prepatent->Acute:", line):
                    #count # of cases: from Prepatent to Acute
                    acutecount+=1
                elif re.search(", Prepatent->SubClinical:", line):
                    #count # of cases: from Prepatent to Subclinical
                    subcount += 1
            if subcount+acutecount == 0:
                success = False
                report_file.write("Found no individual exits Prepatent state in log.\n")
            else:
                # for cases that binormal confidence interval will not work
                if tsf < 1e-2 or tsf >0.99:
                    actual_tsf=acutecount/float(subcount+acutecount)
                    if math.fabs( actual_tsf - tsf)>5e-2 :
                        success = False
                        report_file.write("BAD: Proportion of prepatent cases that become acute vs. subclinical is {0} instead of {1}. Actual Acute case = {2} vs. Actual SubClinical case = {3}.\n".format(actual_tsf,tsf, acutecount, subcount))
                #use binormal 95% confidence interval
                elif not sft.test_binormal_95ci(acutecount, subcount+acutecount, tsf ,report_file, 'PrePatent to Acute transition'):
                    success = False


        if success:
            report_file.write( sft.format_success_msg( success ) )
            os.remove( "test.txt" )
def application( report_file ):
    #pdb.set_trace()
    #print( "Post-processing: " + report_file )

    cdj = json.loads( open( "config.json" ).read() )["parameters"]
    start_time=cdj["Start_Time"]
    timestep=start_time
    lines=[]
    count_chronic=0
    count_recovered=0

    with open( "test.txt" ) as logfile:
        for line in logfile:
            if re.search("Update\(\): Time:",line):
                #calculate time step
                timestep+=1
            if re.search( "just went chronic", line ):
                #append time step and all Infection stage transition to list
                line="TimeStep: "+str(timestep)+ " " + line
                lines.append( line )
                count_chronic += 1
            if re.search("just recovered", line):
                # append time step and all Infection stage transition to list
                line = "TimeStep: " + str(timestep) + " " + line
                lines.append(line)
                count_recovered += 1

    success = True
    with open( sft.sft_output_filename, "w" ) as report_file:
        if len( lines ) == 0 :
            success = False
            report_file.write( "Found no data matching test case.\n" )
        else:
            for line in lines:
                age = float(get_val(" age ", line))
                sex = "female" if (re.search("sex 1", line) or re.search("sex Female", line)) else "male"
                previous_infection_stage = get_char("from ", line)
                if count_chronic==0:
                    success = False
                    report_file.write("Found no Chronic case in data.\n")
                if count_recovered==0:
                    success = False
                    report_file.write("Found no Recovered case in data.\n")
                if (not re.search("from subclinical", line)) and (not re.search("from acute", line)) and (not re.search("from Subclinical", line)) and (not re.search("from Acute", line)) and (not re.search("from SubClinical", line)):
                    success = False
                    if re.search("just went chronic", line):
                        report_file.write("BAD: individual age {0}, sex {1} went to Chronic state from {2} state, expected Acute state or SubClinical state.\n".format(age,sex, previous_infection_stage))
                    else:
                        ind_id=get_val("Individual ", line)
                        report_file.write("BAD: individual {0} age {1}, sex {2} went to Susceptible state from {3} state, expected Acute state or SubClinical state.\n".format(ind_id, age,sex, previous_infection_stage))
        if success:
            report_file.write( sft.format_success_msg( success ) )
            os.remove( "test.txt" )
Example #16
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
Example #17
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
Example #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
        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
Example #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
        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
Example #20
0
def create_report_file(param_obj, actual_infections, inset_days, report_name):
    success = True
    error_tolerance = 2.5e-2
    with open(report_name, "w") as outfile:
        # Test HIV infections
        test_tstep_hiv = param_obj[CampaignKeys.Start_Day][0] + 1
        actual_hiv_infections = actual_infections[0]
        expected_hiv_infections_fraction = param_obj[CampaignKeys.Coverage][0]
        actual_hiv_infection_fraction = 1.0 * actual_hiv_infections / inset_days[
            test_tstep_hiv]
        outfile.write("Total HIV infections fraction expected: {0} \n".format(
            expected_hiv_infections_fraction))
        outfile.write(
            "Total HIV infections: {0} \n".format(actual_hiv_infections))
        outfile.write("Total HIV infection fraction: {0} \n".format(
            actual_hiv_infection_fraction))
        total_error_hiv = math.fabs(expected_hiv_infections_fraction -
                                    actual_hiv_infection_fraction)

        if total_error_hiv > error_tolerance:
            success = False
            outfile.write(
                "BAD: Total error is {0} for HIV, error tolerance is {1}\n".
                format(total_error_hiv, error_tolerance))

        # Test TB infections
        test_tstep_tb = param_obj[CampaignKeys.Start_Day][1] + 1
        actual_tb_infections = actual_infections[1]
        expected_tb_infections_fraction = param_obj[CampaignKeys.Coverage][1]
        actual_tb_infection_fraction = 1.0 * actual_tb_infections / inset_days[
            test_tstep_tb]
        outfile.write("Total TB infections fraction expected: {0} \n".format(
            expected_tb_infections_fraction))
        outfile.write(
            "Total TB infections: {0} \n".format(actual_tb_infections))
        outfile.write("Total TB infection fraction: {0} \n".format(
            actual_tb_infection_fraction))
        total_error_tb = math.fabs(expected_tb_infections_fraction -
                                   actual_tb_infection_fraction)
        success = True

        if total_error_tb > error_tolerance:
            success = False
            outfile.write(
                "BAD: Total error is {0} for TB, error tolerance is {1}\n".
                format(total_error_tb, error_tolerance))

        outfile.write(dtk_sft.format_success_msg(success))
def application(report_file):
    #pdb.set_trace()
    #print( "Post-processing: " + report_file )
    cdj = json.loads(open("config.json").read())["parameters"]
    tcer = float(cdj["Typhoid_Contact_Exposure_Rate"])
    teer = float(cdj["Typhoid_Environmental_Exposure_Rate"])
    start_time = cdj["Start_Time"]

    timestep = start_time
    num_exposures_contact = []
    num_exposures_enviro = []
    with open("test.txt") as logfile:
        route = None
        for line in logfile:
            if re.search("Exposing ", line) and re.search(
                    "route 'environment'", line):
                # collect # of exposures for route contact
                num_exp_e = get_val("num_exposures=", line)
                num_exposures_enviro.append(num_exp_e)
            if re.search("Exposing ", line) and re.search(
                    "route 'contact'", line):
                # collect # of exposures for route environment
                num_exp_c = get_val("num_exposures=", line)
                num_exposures_contact.append(num_exp_c)

    success = True
    with open(sft.sft_output_filename, "w") as report_file:
        if len(num_exposures_enviro) == 0:
            success = False
            report_file.write(
                "Found no individual exposed from route environment.\n")
        elif not sft.test_poisson(num_exposures_enviro, teer, report_file,
                                  'environment'):
            success = False
        if len(num_exposures_contact) == 0:
            success = False
            report_file.write(
                "Found no individual exposed from route contact.\n")
        elif not sft.test_poisson(num_exposures_contact, tcer, report_file,
                                  'contact'):
            success = False

        if success:
            report_file.write(sft.format_success_msg(success))
            os.remove("test.txt")
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 application(report_file):
    """
    Parse this line from test.txt:
    00:00:00 [0] [V] [IndividualTyphoid] amplification calculated as 0.997059: day of year=1, start=360.000000, end=365.000000, ramp_up=30.000000, ramp_down=170.000000, cutoff=160.000000. 
    """
    #print( "Post-processing: " + report_file )
    lines = []
    with open("test.txt") as logfile:
        for line in logfile:
            if re.search("Initializing Typhoid immunity object",
                         line) and re.search("age=0.000000", line):
                lines.append(line)

    cdj = json.loads(open("config.json").read())["parameters"]
    rud = cdj["Typhoid_Environmental_Ramp_Up_Duration"]
    rdd = cdj["Typhoid_Environmental_Ramp_Down_Duration"]
    erd = cdj["Typhoid_Environmental_Ramp_Duration"]  # e.g., 80
    ecd = cdj["Typhoid_Environmental_Cutoff_Days"]  # e.g., 160
    eps = cdj["Typhoid_Environmental_Peak_Start"]  # e.g., 360

    success = True
    with open(sft.sft_output_filename, "w") as report_file:
        if len(lines) == 0:
            success = False
            report_file.write("Found no data matching test case.\n")
        else:
            for line in lines:
                immunity = float(get_val(" immunity modifier", line))
                if immunity != 1.000:
                    success = False
                    report_file.write(
                        "BAD: immunity for newborn={0} instead of 1.0\n".
                        format(immunity))

        if success:
            report_file.write(sft.format_success_msg(success))
            os.remove("test.txt")
Example #24
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
Example #25
0
def application(report_file):
    #pdb.set_trace()
    #print( "Post-processing: " + report_file )

    cdj = json.loads(open("config.json").read())["parameters"]
    start_time = cdj["Start_Time"]

    timestep = start_time
    lines = []

    with open("test.txt") as logfile:
        for line in logfile:
            if re.search("Update\(\): Time:", line):
                #calculate time step
                timestep += 1
            if re.search("just went chronic", line) and re.search(
                    "from acute", line):
                #append time step and all Infection stage transition to list
                line = "TimeStep: " + str(timestep) + " " + line
                lines.append(line)
            if re.search("just recovered", line) and re.search(
                    "from acute", line):
                # append time step and all Infection stage transition to list
                line = "TimeStep: " + str(timestep) + " " + line
                lines.append(line)

    # 4*10 list to store the count for cases [0][]: Chr_male, [1][]: Chr_female, [2][]: Sus_male. [3][]: Sus_female
    count = [[0] * 9 for _ in range(4)]

    tcpm = cdj["Typhoid_Carrier_Probability_Male"]
    tcpf = cdj["Typhoid_Carrier_Probability_Female"]

    gpag_male = [0.0, 0.0, 0.045, 0.134, 0.167, 0.198, 0.247, 0.435, 0.4]
    gpag_female = [0.0, 0.097, 0.234, 0.431, 0.517, 0.60, 0.692, 0.692, 0.555]

    success = True
    with open(sft.sft_output_filename, "w") as report_file:
        if len(lines) == 0:
            success = False
            report_file.write("Found no data matching test case.\n")
        else:
            for line in lines:
                age = float(get_val(" age ", line))
                sex = "female" if (re.search("sex 1", line) or re.search(
                    "sex Female", line)) else "male"
                if re.search("just went chronic", line):
                    # to Chronic
                    #  python 2.7 the (int / int) operator is integer division
                    i = int(age) / 10
                    # for age > 80, put them into the last age group
                    if i > 8: i = 8
                    if sex == "male":
                        count[0][i] += 1
                    else:
                        count[1][i] += 1
                else:
                    # to Susceptible
                    # python 2.7 the (int / int) operator is integer division
                    i = int(age) / 10
                    # for age > 80, put them into the last age group
                    if i > 8: i = 8
                    if sex == "male":
                        count[2][i] += 1
                    else:
                        count[3][i] += 1
            # calculate theoretic probability of becoming a Chronic carrier in two 1*9 lists
            theoretic_p_male = [x * tcpm for x in gpag_male]
            theoretic_p_female = [x * tcpf for x in gpag_female]
            # calculate actual probability of becoming a Chronic carrier in two 1*9 lists
            actual_p_male = [
                x / float(x + y) if (x + y) != 0 else -1
                for x, y in zip(count[0], count[2])
            ]
            actual_p_female = [
                x / float(x + y) if (x + y) != 0 else -1
                for x, y in zip(count[1], count[3])
            ]

            for x in range(0, 9):
                age = [
                    "0-9", "10-19", "20-29", "30-39", "40-49", "50-59",
                    "60-69", "70-79", "80+"
                ]
                # calculate the total chronic cases and sample sizes for Male and Female
                actual_chr_count_male = count[0][x]
                actual_count_male = count[0][x] + count[2][x]
                actual_chr_count_female = count[1][x]
                actual_count_female = count[1][x] + count[3][x]
                # Male
                category = 'sex: Male, age: ' + age[x]
                if actual_count_male == 0:
                    success = False
                    report_file.write(
                        "Found no male in age group {0} went to Chronic state or was recovered from Acute state.\n"
                        .format(age[x]))
                elif theoretic_p_male[x] < 5e-2 or theoretic_p_male[x] > 0.95:
                    # for cases that binormal confidence interval will not work: prob close to 0 or 1
                    if math.fabs(actual_p_male[x] -
                                 theoretic_p_male[x]) > 5e-2:
                        success = False
                        report_file.write(
                            "BAD: Proportion of {0} Acute cases that become Chronic is {1}, expected {2}.\n"
                            .format(category, actual_p_male[x],
                                    theoretic_p_male[x]))
                elif not sft.test_binormal_95ci(
                        actual_chr_count_male, actual_count_male,
                        theoretic_p_male[x], report_file, category):
                    success = False

                #Female
                category = 'sex: Female, age: ' + age[x]
                if actual_count_female == 0:
                    success = False
                    report_file.write(
                        "Found no female in age group {0} went to Chronic state or was recovered from Acute state.\n"
                        .format(age[x]))
                elif theoretic_p_female[x] < 5e-2 or theoretic_p_female[
                        x] > 0.95:
                    # for cases that binormal confidence interval will not work: prob close to 0 or 1
                    if math.fabs(actual_p_female[x] -
                                 theoretic_p_female[x]) > 5e-2:
                        success = False
                        report_file.write(
                            "BAD: Proportion of {0} Acute cases that become Chronic is {1}, expected {2}.\n"
                            .format(category, actual_p_female[x],
                                    theoretic_p_female[x]))
                elif not sft.test_binormal_95ci(
                        actual_chr_count_female, actual_count_female,
                        theoretic_p_female[x], report_file, category):
                    success = False
        if success:
            report_file.write(sft.format_success_msg(success))
Example #26
0
def create_report_file(param_obj, output_dict, report_name, debug):
    with open(report_name, "w") as outfile:
        config_name = param_obj[KEY_CONFIG_NAME]
        outfile.write("Config_name = {}\n".format(config_name))
        success = True
        death_rate = param_obj[KEY_DEATH_RATE]
        simulation_duration = param_obj[KEY_DURATION]
        if not len(output_dict):
            success = False
            outfile.write(dtk_sft.sft_no_test_data)
        actual_timer = []
        outfile.write(
            "collecting the actual timestep between active and death:\n")
        for id in output_dict:
            death_time = timer = active_time = None
            if KEY_DEATH in output_dict[id]:
                death_time = output_dict[id][KEY_DEATH]
            if KEY_SYMPTOMATIC in output_dict[id]:
                active_time = output_dict[id][KEY_SYMPTOMATIC][0]
            if active_time:
                if death_time:  # some individual may not die yet at the end of the simulation
                    actual_timer.append(death_time - active_time)
                else:
                    outfile.write(
                        "Individual {0} moved to symptomatic active at timestep {1} and is not dead yet at "
                        "the end of simulation (duration = {2}).\n".format(
                            id, active_time, simulation_duration))
            else:
                success = False
                outfile.write(
                    "BAD: individual {0} died before entering active symptomatic state.\n"
                    .format(id))
        if not len(actual_timer):
            success = False
            outfile.write(
                "BAD: There is no death in this test, please fix the test.\n")

        outfile.write(
            "Running ks test for death time and numpy exponential distribution: \n"
        )
        size = len(actual_timer)
        scale = 1.0 / death_rate
        dist_exponential_np = np.random.exponential(scale, size)
        dtk_sft.plot_data_sorted(
            actual_timer,
            dist2=np.array(dist_exponential_np),
            label1="death timer",
            label2="numpy exponential",
            title="exponential rate = {}".format(death_rate),
            xlabel="data point",
            ylabel="death timer",
            category='Death_timer',
            show=True,
            line=False,
            overlap=True)
        result = dtk_sft.test_exponential(actual_timer,
                                          p1=death_rate,
                                          report_file=outfile,
                                          integers=True,
                                          roundup=True,
                                          round_nearest=False)
        outfile.write(
            "ks test result is {0}, exponential rate = {1}, # of data point = {2}.\n"
            .format(result, death_rate, size))
        if not result:
            success = False
            outfile.write(
                "BAD: test exponential for death timer failed with death rate = {}.\n"
                .format(death_rate))

        outfile.write(dtk_sft.format_success_msg(success))
    if debug:
        print("SUMMARY: Success={0}\n".format(success))
    return success
Example #27
0
def create_report_file(param_obj, campaign_obj, report_data_obj, report_name,
                       debug):
    with open(report_name, "w") as outfile:
        config_name = param_obj[KEY_CONFIG_NAME]
        outfile.write("Config_name = {}\n".format(config_name))
        success = True
        base_infectivity = param_obj[KEY_BASE_INFECTIVITY]
        start_day = campaign_obj[KEY_START_DAY]
        new_infection = report_data_obj[KEY_NEW_INFECTION]
        immunity_acquisition_factor = param_obj[
            KEY_IMMUNITY_ACQUISITION_FACTOR]
        # calculate expected number of infections for a time period of 3 months:
        number_of_month = 3
        expected_new_infection = base_infectivity * dtk_sft.DAYS_IN_MONTH * number_of_month * immunity_acquisition_factor
        expected = [expected_new_infection
                    ] * (dtk_sft.MONTHS_IN_YEAR // number_of_month)
        # group new infections for every 3 months:
        value_to_test = []
        if len(new_infection) < start_day + dtk_sft.DAYS_IN_YEAR:
            success = False
            outfile.write(
                "BAD: the simulation duration is too short, please make sure it's at least {} days.\n"
                .format(start_day + dtk_sft.DAYS_IN_YEAR))
        outfile.write(
            "running chi-squared test for expected new infections for {0} {1}-months time bins: \n"
            "base_infectivity = {2}, immunity_acquisition_factor = {3}.\n".
            format(dtk_sft.MONTHS_IN_YEAR // number_of_month, number_of_month,
                   base_infectivity, immunity_acquisition_factor))
        actual_new_infection = 0
        i = 0
        for t in range(start_day, len(new_infection)):
            actual_new_infection += new_infection[t]
            i += 1
            if not i % (number_of_month * dtk_sft.DAYS_IN_MONTH):
                value_to_test.append(actual_new_infection)
                actual_new_infection = 0
        dtk_sft.plot_data(
            value_to_test,
            dist2=expected,
            label1="actual_new_infections",
            label2="expected_new_infection",
            title="actual vs. expected new infection for every {} months".
            format(number_of_month),
            xlabel="every {} months".format(number_of_month),
            ylabel="# of new infections",
            category='actual_vs_expected_new_infections',
            show=True,
            line=True)
        result = dtk_sft.test_multinomial(dist=value_to_test,
                                          proportions=expected,
                                          report_file=outfile,
                                          prob_flag=False)

        if not result:
            success = False
            outfile.write(
                "BAD: The Chi-squared test for number of new infections in every {} months failed.\n"
                .format(number_of_month))
        else:
            outfile.write(
                "GOOD: The Chi-squared test for number of new infections in every {} months passed.\n"
                .format(number_of_month))
        outfile.write(dtk_sft.format_success_msg(success))

    if debug:
        print("SUMMARY: Success={0}\n".format(success))
    return success
def create_report_file(param_obj, campaign_obj, demographics_obj,
                       report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        total_timesteps = param_obj[KEY_TOTAL_TIMESTEPS]
        start_timestep = param_obj[KEY_START_TIME]
        initial_population = demographics_obj[KEY_INITIAL_POPULATION]
        rates = campaign_obj[KEY_CAMPAIGN_DIP]
        durations = campaign_obj[KEY_CAMPAIGN_DURATIONS]
        if not report_data_obj:  # todo: maybe use try
            success = False
            outfile.write("BAD: There is no data in the InsetChart report")
        else:
            new_infections = report_data_obj[KEY_NEW_INFECTIONS]
            statistical_population = report_data_obj[
                KEY_STATISTICAL_POPULATION]

            length = len(rates)
            start_duration = start_timestep
            new_infections_dict = {}
            calculate_new_population = initial_population
            for i in range(length):
                rate = rates[i]
                duration = durations[i]
                calculate_new_population = rate * duration + calculate_new_population
                end_duration = duration + start_duration
                if rate not in new_infections_dict:
                    new_infections_dict[rate] = []
                for j in range(start_duration + 1, end_duration + 1):
                    if j < total_timesteps + start_timestep:
                        new_infections_dict[rate].append(new_infections[j])
                        j += 1
                    else:
                        break
                if end_duration > total_timesteps + start_timestep:
                    calculate_new_population -= rate * (
                        end_duration - total_timesteps - start_timestep)
                    break
                start_duration = end_duration
            if end_duration < total_timesteps + start_timestep:
                rate = 0.0
                if rate not in new_infections_dict:
                    new_infections_dict[rate] = []
                for j in range(end_duration + 1, len(new_infections)):
                    new_infections_dict[rate].append(new_infections[j])
            with open("new_infections_parsed.json", "w") as file:
                json.dump(new_infections_dict, file, indent=4)

            # test statistical population channel
            diff_population = math.fabs(calculate_new_population -
                                        statistical_population[-1])
            if debug:
                print( "calculated population is {0}, statistical population " \
                      "from InsetChart is {1}.".format(calculate_new_population,
                                                       statistical_population[-1]) )
            error_tolerance = math.fabs(calculate_new_population -
                                        initial_population) * 0.1
            if debug:
                print("diff_population is {0}, error_tolerance is {1}".format(
                    diff_population, error_tolerance))
            if diff_population > error_tolerance:
                success = False
                outfile.write(
                    "BAD: statistical population is {0}, expected about {1}.\n"
                    .format(statistical_population[-1],
                            calculate_new_population))

            # test poisson distribution for new infections
            for rate in new_infections_dict:
                dist = new_infections_dict[rate]
                title = "rate = " + str(rate)
                result = sft.test_poisson(dist,
                                          rate,
                                          route=title,
                                          report_file=outfile,
                                          normal_approximation=False)
                # print result, rate, len(dist)
                if not result:
                    success = False
                    outfile.write(
                        "BAD: ks poisson test for {0} is {1}.\n".format(
                            title, result))
                numpy_distro = np.random.poisson(rate, len(dist))
                sft.plot_data(
                    dist,
                    numpy_distro,
                    title="new infections for {}".format(title),
                    label1="new infection from model, {}".format(title),
                    label2="Poisson distro from numpy",
                    xlabel="data points",
                    ylabel="new infection",
                    category="plot_data_{0}".format(title),
                    show=True)
                sft.plot_probability(
                    dist,
                    numpy_distro,
                    title="probability mass function for {}".format(title),
                    label1="new infection probability from model",
                    label2="new infection probability from numpy distro",
                    category="plot_probability_{0}".format(title),
                    show=True)

        outfile.write(sft.format_success_msg(success))
        if debug:
            print("SUMMARY: Success={0}\n".format(success))
        return success
def create_report_file(param_obj, campaign_obj, demographics_obj, report_data_obj, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        total_timesteps = param_obj[KEY_TOTAL_TIMESTEPS]
        start_timestep = param_obj[KEY_START_TIME]
        initial_population = demographics_obj[KEY_INITIAL_POPULATION]
        rates = campaign_obj[KEY_CAMPAIGN_DIP]
        durations = campaign_obj[KEY_CAMPAIGN_DURATIONS]
        if not report_data_obj: # todo: maybe use try
            success = False
            outfile.write("BAD: There is no data in the InsetChart report")
        else:
            new_infections = report_data_obj[KEY_NEW_INFECTIONS]
            statistical_population = report_data_obj[KEY_STATISTICAL_POPULATION]

            length = len(rates)
            start_duration = start_timestep
            new_infections_dict = {}
            calculate_new_population = initial_population
            for i in range(length):
                rate = rates[i]
                duration = durations[i]
                calculate_new_population = rate * duration + calculate_new_population
                end_duration = duration + start_duration
                if rate not in new_infections_dict:
                    new_infections_dict[rate] = []
                for j in range(start_duration + 1, end_duration + 1):
                    if j < total_timesteps + start_timestep:
                        new_infections_dict[rate].append(new_infections[j])
                        j += 1
                    else:
                        break
                if end_duration > total_timesteps + start_timestep:
                    calculate_new_population -= rate * (end_duration - total_timesteps - start_timestep)
                    break
                start_duration = end_duration
            if end_duration < total_timesteps + start_timestep:
                rate = 0.0
                if rate not in new_infections_dict:
                    new_infections_dict[rate] = []
                for j in range(end_duration + 1, len(new_infections)):
                    new_infections_dict[rate].append(new_infections[j])
            with open("new_infections_parsed.json","w") as file:
                json.dump(new_infections_dict, file, indent = 4)

            # test statistical population channel
            diff_population = math.fabs(calculate_new_population - statistical_population[-1])
            if debug:
                print "calculated population is {0}, statistical population " \
                      "from InsetChart is {1}.".format(calculate_new_population,
                                                       statistical_population[-1])
            error_tolerance = math.fabs(calculate_new_population - initial_population)* 0.1
            if debug:
                print "diff_population is {0}, error_tolerance is {1}".format(diff_population, error_tolerance)
            if diff_population  > error_tolerance:
                success = False
                outfile.write("BAD: statistical population is {0}, expected about {1}.\n".format(statistical_population[-1], calculate_new_population))

            # test poisson distribution for new infections
            for rate in new_infections_dict:
                dist = new_infections_dict[rate]
                title = "rate = " + str(rate)
                result = sft.test_poisson(dist, rate, route = title, report_file = outfile, normal_approximation = False)
                # print result, rate, len(dist)
                if not result:
                    success = False
                    outfile.write("BAD: ks poisson test for {0} is {1}.\n".format(title, result))
                numpy_distro = np.random.poisson(rate, len(dist))
                sft.plot_data(dist, sorted(numpy_distro),
                              title="new infections for {}".format(title),
                              label1="new infection from model, {}".format(title),
                              label2="Poisson distro from numpy",
                              xlabel="data points", ylabel="new infection",
                              category="plot_data_{0}".format(title), show=True)
                sft.plot_probability(dist, numpy_distro,
                                     title="probability mass function for {}".format(title),
                                     label1="new infection probability from model",
                                     label2="new infection probability from numpy distro",
                                     category="plot_probability_{0}".format(title), show=True)

        outfile.write(sft.format_success_msg(success))
        if debug:
            print "SUMMARY: Success={0}\n".format(success)
        return success
def application( report_file ):
    #pdb.set_trace()
    #print( "Post-processing: " + report_file )
    cdj = json.loads( open( "config.json" ).read() )["parameters"]
    ncdr = cdj["Node_Contagion_Decay_Rate"]
    start_time = cdj["Start_Time"]
    isj = json.loads(open("output/InsetChart.json").read())["Channels"]
    ecp = isj["Environmental Contagion Population"]["Data"]

    timestep = start_time
    lines=[]
    cum_all=[]
    cum=0
    Statpop=[]
    pop = 0
    adding_cp_log = []
    adding_cp = 0
    with open( "test.txt" ) as logfile:
        route=None
        for line in logfile:
            # collect all lines of
            if re.search("Update\(\): Time:",line):
                #calculate time step
                timestep+=1
                #lines.append(line)
                #append the accumulated shedding and reset the counter at the end of each time step.
                #data for timestep 1 is stored in cum_all[1]
                cum_all.append(cum)
                # environmental cp decay
                cum *= math.exp(-1*ncdr)
                adding_cp_log.append(adding_cp)
            if re.search("\[StrainAwareTransmissionGroups\] Adding ", line) and re.search("route:1", line):
                # append time step and total shedding line to lists
                #line = "TimeStep: " + str(timestep) + " " + line
                #lines.append(line)
                adding_cp = float(get_val("Adding ",line))
            if re.search("Exposing ", line) and re.search("route 'environment'", line):
                # append time step and Exposing line to lists
                line = "TimeStep: " + str(timestep) + " " + line
                lines.append(line)
                # get shedding of contact route and add it to accumulated shedding
            if re.search("depositing", line) and re.search("route environment", line):
                shedding= float(get_val("depositing ", line))
                cum += shedding
            if re.search("scaled by ", line) and re.search("route:1", line):
                # get population
                pop = float(get_val("scaled by ",line))
                Statpop.append(pop)


    success = True
    with open( sft.sft_output_filename, "w" ) as report_file:
        if len(lines ) == 0:
            success = False
            report_file.write("Found no data matching test case.\n" )
        else:
            for line in lines:
                if re.search("Exposing", line):
                    fEnvironment = float(get_val("fEnvironment=", line))
                    timestep = int(get_val("TimeStep: ", line))
                    expected_cp = cum_all[timestep-start_time] / Statpop[timestep -start_time+ 1]
                    if math.fabs(fEnvironment-expected_cp) >1e-2 :
                        success = False
                        ind_id = get_val("inividual ", line)
                        report_file.write("BAD: Individual {0} is exposed on route environment with contagion population = {1} at time {2} StatPop = {3}, expected {4}.\n".format(ind_id,fEnvironment, timestep, Statpop[timestep+1],expected_cp))
            for x in range(1, len(cum_all)):
                #report_file.write("timestep={0}, StatPop = {1}, cum_all= {2}, adding_cp_log = {3}.\n".format(x,Statpop[x+1],cum_all[x], adding_cp_log[x]))
                if math.fabs(cum_all[x] - adding_cp_log[x])>1e-2 :
                    success = False
                    report_file.write(
                        "BAD: At time {0}, the accumulated shedding is {1} from log, expected {2}.\n".format(timestep, adding_cp_log[x], cum_all[x]))
                expected_cp = cum_all[x] / Statpop[x+ 1]
                if math.fabs(expected_cp - ecp[x-1])>1e-2 :
                    success = False
                    report_file.write(
                        "BAD: At time {0}, the accumulated shedding is {1} from InsetChart.json, expected {2}.\n".format(timestep, ecp[x-1], expected_cp))
        if success:
              report_file.write(sft.format_success_msg(success))
              os.remove( "test.txt" )
Example #31
0
def create_report_file(param_obj, output_dict, reporter_df, report_name,
                       debug):
    with open(report_name, "w") as outfile:
        config_name = param_obj[Config.config_name]
        outfile.write("Config_name = {}\n".format(config_name))
        success = True
        simulation_duration = param_obj[Config.duration]
        timestep = param_obj[Config.simulation_timestep]
        if not len(output_dict):
            success = False
            outfile.write(dtk_sft.sft_no_test_data)
        else:
            outfile.write(
                "Group the incidence by year and get the sum for all age bins:\n"
            )
            # the year column becomes the index of the groupby_df
            groupby_df = reporter_df.groupby(ReportColumn.year).sum()

            if debug:
                with open("DEBUG_groupby_dataframe.csv", "w") as groupby_file:
                    groupby_df.to_csv(groupby_file, header=True)

            expected_max_time_step = math.floor(
                simulation_duration / timestep) * timestep
            if simulation_duration <= 180 or expected_max_time_step <= 180:
                success = False
                outfile.write(
                    "BAD: the simulation duration is too short, please increase the duration.\n"
                )

            elif not groupby_df[ReportColumn.incidence].sum():
                success = False
                outfile.write(
                    "BAD: there in no TB incidence in the test, please check the test.\n"
                )

            else:
                outfile.write(
                    "Testing the incidence count with log_valid for all year buckets:\n"
                )
                i = incidence_count = 0
                years = groupby_df.index.values
                incidence_counts = []

                for t in output_dict:
                    if i < len(years):
                        year = years[i]
                        if t <= round(year * dtk_sft.DAYS_IN_YEAR):
                            incidence_count += output_dict[t]
                        else:
                            reporter_sum = int(
                                groupby_df[groupby_df.index == year][
                                    ReportColumn.incidence])
                            incidence_counts.append(incidence_count)
                            if incidence_count != reporter_sum:
                                success = False
                                outfile.write(
                                    "BAD: in year {0} the incidence count get from reporter is {1}, while test.txt reports"
                                    " {2} cases.\n".format(
                                        year, reporter_sum, incidence_count))
                            incidence_count = output_dict[t]
                            i += 1
                    else:
                        break

                dtk_sft.plot_data(incidence_counts,
                                  dist2=np.array(
                                      groupby_df[ReportColumn.incidence]),
                                  label1="reporter",
                                  label2="log_valid",
                                  title="incidence",
                                  xlabel="every half year",
                                  ylabel="incidence",
                                  category='incidence',
                                  show=True,
                                  line=False,
                                  alpha=0.8)

                outfile.write(
                    "Testing whether the time step in log mathces the simulation duration:\n"
                )
                max_time_step = max(output_dict.keys())
                if max_time_step != expected_max_time_step:
                    success = False
                    outfile.write(
                        "BAD: the last time step in simulation is {0}, expected {1}."
                        "\n".format(max_time_step, expected_max_time_step))

                outfile.write(
                    "Testing whether the reporter year matches the simulation duration:\n"
                )
                if i != len(years):
                    success = False
                    outfile.write(
                        "BAD: the reporter has data up to year {0} but the simulation duration is {1}, we are expecting "
                        "not more than year {2} from reporter.\n".format(
                            max(years), simulation_duration,
                            math.floor(simulation_duration /
                                       dtk_sft.DAYS_IN_YEAR)))
                    outfile.write("i={0}, len(years)={1}\n".format(
                        i, len(years)))
                if simulation_duration > round(
                        max(years) * dtk_sft.DAYS_IN_YEAR) + 180:
                    success = False
                    outfile.write(
                        "BAD: the reporter has data up to year {0} but the simulation duration is {1}, we are expecting "
                        "data after year {0} from reporter.\n".format(
                            max(years), simulation_duration))

        outfile.write(dtk_sft.format_success_msg(success))

        if debug:
            print("SUMMARY: Success={0}\n".format(success))
        return success
def application( report_file ):
    #pdb.set_trace()
    #print( "Post-processing: " + report_file )

    # get params from config.json
    cdj = json.loads( open( "config.json" ).read() )["parameters"]
    start_time=cdj["Start_Time"]
    simulation_duration = cdj["Simulation_Duration"]
    lines = []
    timestep=start_time

    with open( "test.txt" ) as logfile:
        for line in logfile:
            if re.search("Update\(\): Time:",line):
                #calculate time step
                timestep+=1
            if re.search( "depositing", line ):
                line = "TimeStep: " + str(timestep) + " " + line
                lines.append( line )
            if re.search("AcquireNewInfection:", line):
                line = "TimeStep: " + str(timestep) + " " + line
                lines.append(line)
            if re.search("state_to_report", line):
                line = "TimeStep: " + str(timestep) + " " + line
                lines.append(line)
    with open ("Ye.txt", "w") as report_file:
        for line in lines:
            if re.search("ndividual 5 ",line) or re.search("individual=5 ", line):
                report_file.write(line)

    with open ("Ye_all.txt", "w") as report_file:
        for line in lines:
            report_file.write(line)

    success = True
    dict_depositing = {}
    dict_infection ={}

    with open( sft.sft_output_filename, "w" ) as report_file:
        if len( lines ) == 0:
            success = False
            report_file.write( "Found no data matching test case.\n" )
        else:
            for i in range(0, len(lines)):
                timestep = get_val("TimeStep: ", lines[i])
                Ind_id = None
                state = None
                if re.search("depositing",lines[i]):
                    Ind_id = get_val("Individual ", lines[i])
                    state = get_char("in state ", lines[i])
                    route = get_char("to route ", lines[i])
                    key = "Time  "+ str(timestep) + " individual " + str(Ind_id) + " route " + str(route)
                    if dict_depositing.has_key(key):
                        success = False
                        report_file.write("BAD: At time {0} individual {1} is depositing to route {2} more than once.\n".format(timestep, Ind_id, route))
                    else:
                        dict_depositing[key]=state
                elif re.search("AcquireNewInfection:",lines[i]):
                    Ind_id=get_val("individual=", lines[i])
                    state = "PRE"
                    if not re.search("route=0", lines[i]):
                        # if the infection is caused by contact or environment route which happen after the shedding
                        # the state is use for next step
                        timestep = int(timestep) + 1
                    key = "Time  " + str(timestep) + " individual " + str(Ind_id)
                    if timestep !=start_time + simulation_duration:
                        #the last time step has no shedding information
                        if dict_infection.has_key(key):
                            if state != dict_infection.get(key):
                                success = False
                                report_file.write("BAD: At time {0} individual {1} is reported to be in state {2} and {3}\n".format(timestep,Ind_id, state, dict_infection.get(key)))
                        else:
                            dict_infection[key]=state
                elif re.search("state_to_report", lines[i]):
                    Ind_id=get_val("individual ",lines[i])
                    state=get_char("= ",lines[i])
                    if state =='SUS':
                        # skip for susceptiable state
                        continue
                    # this state is use for next time step
                    timestep = int(timestep) + 1
                    key = "Time  "+ str(timestep) + " individual " + str(Ind_id)
                    if timestep !=start_time + simulation_duration:
                        #the last time step has no shedding information
                        if dict_infection.has_key(key):
                            if state != dict_infection.get(key):
                                success = False
                                report_file.write("BAD: At time {0} individual {1} is reported to be in state {2} and {3}\n".format(timestep,Ind_id, state, dict_infection.get(key)))
                        else:
                            dict_infection[key] = state
    # with open("dict_d.txt", "w") as report_file:
    #     for key, value in dict_depositing.iteritems():
    #         report_file.write("{0}: {1}\n".format(key,value))
    # with open("dict_i.txt", "w") as report_file:
    #     for key, value in dict_infection.iteritems():
    #         report_file.write("{0}: {1}\n".format(key, value))


            for key in dict_infection:
#                print key + "\n"
                key_1 = key + " route contact"
                key_2 = key + " route environmental"
                state = dict_infection.get(key)
                if dict_depositing.has_key(key_1):
                    depositing_state = dict_depositing.get(key_1)
                    if depositing_state != state:
                        success = False
                        report_file.write("BAD: {0} is depositing in state {1}, expected {2}.\n".format(key_1, depositing_state,state))
                    dict_depositing.pop(key_1, None)
                else:
                    success = False
                    report_file.write("BAD: {0} is in infection state {1} but it's not depositing in route contact.\n".format(key,state))
                if dict_depositing.has_key(key_2):
                    depositing_state = dict_depositing.get(key_2)
                    if depositing_state != state:
                        success = False
                        report_file.write("BAD: {0} is depositing in state {1}, expected {2}.\n".format(key_2, depositing_state, state))
                    dict_depositing.pop(key_2, None)
                else:
                    success = False
                    report_file.write("BAD: {0} is in infection state {1} but it's not depositing in route environment.\n".format(key,state))
            if len(dict_depositing) != 0:
                success = False
                report_file.write("BAD: Some individuals are depositing while they are not reported to be in Infection state. Please see \"depositing.txt\" for details\n")
                with open("depositing.txt", "w") as file:
                    for key, value in dict_depositing.iteritems():
                        file.write("{0} is depositing as state {1}.\n".format(key, value))

            if success:
                report_file.write( sft.format_success_msg( success ) )
                os.remove( "test.txt" )
Example #33
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
def application(report_file):
    #pdb.set_trace()
    #print( "Post-processing: " + report_file )

    cdj = json.loads(open("config.json").read())["parameters"]
    start_time = cdj["Start_Time"]

    timestep = start_time
    lines = []

    with open("test.txt") as logfile:
        for line in logfile:
            if re.search("Update\(\): Time:", line):
                #calculate time step
                timestep += 1
            if re.search("just went chronic", line) and re.search(
                    "from acute", line):
                #append time step and all Infection stage transition to list
                line = "TimeStep: " + str(timestep) + " " + line
                lines.append(line)
            if re.search("just recovered", line) and re.search(
                    "from acute", line):
                # append time step and all Infection stage transition to list
                line = "TimeStep: " + str(timestep) + " " + line
                lines.append(line)

    # 4*10 list to store the count for cases [0][]: Chr_male, [1][]: Chr_female, [2][]: Sus_male. [3][]: Sus_female
    count = [[0] * 9 for _ in range(4)]

    tcpm = cdj["Typhoid_Carrier_Probability_Male"]
    tcpf = cdj["Typhoid_Carrier_Probability_Female"]

    gpag_male = [0.0, 0.0, 0.045, 0.134, 0.167, 0.198, 0.247, 0.435, 0.4]
    gpag_female = [0.0, 0.097, 0.234, 0.431, 0.517, 0.60, 0.692, 0.692, 0.555]

    success = True
    with open(sft.sft_output_filename, "w") as report_file:
        if len(lines) == 0:
            success = False
            report_file.write("Found no data matching test case.\n")
        else:
            for line in lines:
                age = float(get_val(" age ", line))
                sex = "female" if (re.search("sex 1", line) or re.search(
                    "sex Female", line)) else "male"
                if re.search("just went chronic", line):
                    # to Chronic
                    #  python 2.7 the (int / int) operator is integer division
                    i = int(age) / 10
                    # for age > 80, put them into the last age group
                    if i > 8: i = 8
                    if sex == "male":
                        count[0][i] += 1
                    else:
                        count[1][i] += 1
                else:
                    # to Susceptible
                    # python 2.7 the (int / int) operator is integer division
                    i = int(age) / 10
                    # for age > 80, put them into the last age group
                    if i > 8: i = 8
                    if sex == "male":
                        count[2][i] += 1
                    else:
                        count[3][i] += 1
            # calculate theoretic probability of becoming a Chronic carrier in two 1*9 lists
            theoretic_p_male = [x * tcpm for x in gpag_male]
            theoretic_p_female = [x * tcpf for x in gpag_female]
            # calculate actual probability of becoming a Chronic carrier in two 1*9 lists
            actual_p_male = [
                x / float(x + y) if (x + y) != 0 else -1
                for x, y in zip(count[0], count[2])
            ]
            actual_p_female = [
                x / float(x + y) if (x + y) != 0 else -1
                for x, y in zip(count[1], count[3])
            ]
            # calculate tolerance from theoretic and actual probabilities and store then in two 1*9 list
            #tolerance_male = [math.fabs(x-y)/y if y !=0 else x for x, y in zip(actual_p_male, theoretic_p_male)]
            #tolerance_female = [math.fabs(x-y)/y if y !=0 else x for x, y in zip(actual_p_female, theoretic_p_female)]
            for x in range(5, 5):
                age = [
                    "0-9", "10-19", "20-29", "30-39", "40-49", "50-59",
                    "60-69", "70-79", "80+"
                ]
                # calculate the total chronic cases and sample sizes for Male and Female
                actual_chr_count_male = count[0][x]
                actual_count_male = count[0][x] + count[2][x]
                actual_chr_count_female = count[1][x]
                actual_count_female = count[1][x] + count[3][x]
                # calculate the mean and  standard deviation for binormal distribution
                sd_male = math.sqrt(theoretic_p_male[x] *
                                    (1 - theoretic_p_male[x]) *
                                    actual_count_male)
                mean_male = actual_count_male * theoretic_p_male[x]
                sd_female = math.sqrt(theoretic_p_female[x] *
                                      (1 - theoretic_p_female[x]) *
                                      actual_count_female)
                mean_female = actual_count_female * theoretic_p_female[x]
                # 99.73% confidence interval
                lower_bound_male = mean_male - 3 * sd_male
                upper_bound_male = mean_male + 3 * sd_male
                lower_bound_female = mean_female - 3 * sd_female
                upper_bound_female = mean_female + 3 * sd_female

                # Male
                sex = "male"
                if actual_count_male == 0:
                    success = False
                    report_file.write(
                        "Found no male in age group {0} went to Chronic state or was recovered from Acute state.\n"
                        .format(age[x]))
                elif mean_male != 0 and (mean_male <= 5 or
                                         actual_count_male - mean_male <= 5):
                    success = False
                    report_file.write(
                        "There is not enough sample size in age group {0}, sex {1}: mean = {2}, sample size - mean = {3}.\n"
                        .format(age[x], sex, mean_male,
                                actual_count_male - mean_male))
                elif actual_chr_count_male < lower_bound_male or actual_chr_count_male > upper_bound_male:
                    success = False
                    report_file.write(
                        "BAD: The probability of becoming a chronic carrier from Acute stage for individual age group {0}, sex {1} is {2}, expected {3}. The {1} Chronic cases is {4}, expected 99.73% confidence interval ( {5}, {6}).\n"
                        .format(age[x], sex, actual_p_male[x],
                                theoretic_p_male[x], actual_chr_count_male,
                                lower_bound_male, upper_bound_male))
                #Female
                sex = "female"
                if actual_count_female == 0:
                    success = False
                    report_file.write(
                        "Found no female in age group {0} went to Chronic state or was recovered from Acute state.\n"
                        .format(age[x]))
                elif mean_female != 0 and (
                        mean_female <= 5
                        or actual_count_female - mean_female <= 5):
                    success = False
                    report_file.write(
                        "There is not enough sample size in age group {0}, sex {1}: mean = {2}, sample size - mean = {3}.\n"
                        .format(age[x], sex, mean_female,
                                actual_count_female - mean_female))
                elif actual_chr_count_female < lower_bound_female or actual_chr_count_female > upper_bound_female:
                    success = False
                    report_file.write(
                        "BAD: The probability of becoming a chronic carrier from Acute stage for individual age group {0}, sex {1} is {2}, expected {3}. The {1} Chronic cases is {4}, expected 99.73% confidence interval ( {5}, {6}).\n"
                        .format(age[x], sex, actual_p_female[x],
                                theoretic_p_female[x], actual_chr_count_female,
                                lower_bound_female, upper_bound_female))

        if success:
            report_file.write(sft.format_success_msg(success))
Example #35
0
def create_report_file(param_obj, output_df, report_name, debug=False):
    """
    Do the actual validation as follows and create the actual reports:
    1) validating each individual's mod_acquire value using the function calculate_initial_mod_acquire(),
    2) examine the set of immune failage values collectively - binary type only. These values are assigned according
        to a random process, so it's possible to identify threshold values from that process

    :param param_obj: parameter object(read from config.json)
    :param output_df: output dataframe from output file parsing (stdout/test.txt)
    :param report_name: report file name to write to disk
    :param debug:
    :return: auccess
    """
    simulation_duration = int(param_obj[KEY_TOTAL_TIMESTEPS])
    simulation_timestep = float(param_obj[KEY_SIMULATION_TIMESTEP])
    enable_birth = param_obj[KEY_ENABLE_BIRTH]
    base_infectivity = float(param_obj[KEY_BASE_INFECTIVITY])
    susceptibility_type = param_obj[KEY_SUSCEPTIBILITY_TYPE]
    maternal_protection_type = param_obj[KEY_MATERNAL_PROTECTION_TYPE]
    linear_slope = None
    linear_susZero = None
    sigmoid_halfMaxAge = None
    sigmoid_steepFac = None
    sigmoid_susinit = None

    with open(report_name, "w") as outfile:
        outfile.write(
            "Simulation parmaters: simulation_duration={0}, simulation_timestep={1}, enable_birth={2}, base_infectivity={3}\n"
            .format(simulation_duration, simulation_timestep, enable_birth,
                    base_infectivity))
        outfile.write("maternal_protection_type = {0}:\n".format(
            maternal_protection_type))
        outfile.write(
            "susceptibility_type = {0}:\n".format(susceptibility_type))

        if param_obj[KEY_MATERNAL_PROTECTION_TYPE] == "LINEAR":
            linear_slope = float(param_obj[KEY_MATERNAL_LINEAR_SLOPE])
            linear_susZero = float(param_obj[KEY_MATERNAL_LINEAR_SUSZERO])
            outfile.write(
                "It's a linear type with linear_slope:{0} and linear_susZero:{1}\n"
                .format(linear_slope, linear_susZero))
        elif param_obj[KEY_MATERNAL_PROTECTION_TYPE] == "SIGMOID":
            sigmoid_halfMaxAge = param_obj[KEY_MATERNAL_SIGMOID_HALFMAXAGE]
            sigmoid_steepFac = param_obj[KEY_MATERNAL_SIGMOID_STEEPFAC]
            sigmoid_susinit = param_obj[KEY_MATERNAL_SIGMOID_SUSINIT]
            outfile.write(
                "It's a sigmoid type with half_max_age:{0}, steepFac:{1}, susInit:{2}\n"
                .format(sigmoid_halfMaxAge, sigmoid_steepFac, sigmoid_susinit))
        elif param_obj[KEY_MATERNAL_PROTECTION_TYPE] == "NONE":
            outfile.write("It's a NONE type \n")

        success = True

        # mod_acquire check
        for index, row in output_df.iterrows():
            current_individual = row
            if debug:
                print("working on individual " +
                      str(current_individual[KEY_INDIVIDUAL_ID]))
            expected_mod_aqcuire = calculate_initial_mod_acquire(
                current_individual[KEY_INDIVIDUAL_AGE], susceptibility_type,
                maternal_protection_type, linear_slope, linear_susZero,
                sigmoid_steepFac, sigmoid_halfMaxAge, sigmoid_susinit,
                current_individual.immune_failage)
            if (not np.isclose(expected_mod_aqcuire,
                               current_individual[KEY_INDIVIDUAL_MOD_ACQUIRE],
                               0.0001)):
                outfile.write(
                    "BAD: actual mod_acquire for individual {0} at time step {1} is {2}, expected {3}.\n"
                    .format(current_individual[KEY_INDIVIDUAL_ID],
                            current_individual[KEY_SIMULATION_TIMESTEP],
                            current_individual[KEY_INDIVIDUAL_MOD_ACQUIRE],
                            expected_mod_aqcuire))
                success = False

        # failage threshold check, only operate on BINARY enumerations
        if (maternal_protection_type == "BINARY"):
            threshold_set, threshold_vals = reference_failage_thresholds(
                susceptibility_type, maternal_protection_type, linear_slope,
                linear_susZero, sigmoid_steepFac, sigmoid_halfMaxAge,
                sigmoid_susinit, False)
            # Ensure each individual is in the DataFrame only once.
            if (len(output_df) == len(output_df[KEY_INDIVIDUAL_ID].unique())):
                # Extract vector of immune_failage values
                mfvals = output_df[KEY_INDIVIDUAL_IMMUNE_FAILAGE].values
                if debug:
                    print("")
                # Test threshold values
                for ts, tv in zip(threshold_set, threshold_vals):
                    # The quality of the match depends on the number of individuals;
                    # for ~10k individuals, a 2% match is a good level
                    if (not np.isclose(
                        (mfvals < tv).sum() / mfvals.size, ts, atol=0.02)):
                        outfile.write("BAD threshold value check:\n")
                        outfile.write("threshold_set:" + str(ts) + "\n")
                        outfile.write("threshold_vals:" + str(tv) + "\n")
                        outfile.write("mfvals: " + str(mfvals) + "\n")
                        success = False

        outfile.write(dtk_sft.format_success_msg(success))

    return success
Example #36
0
def create_report_file(param_obj, output_dict, report_name, debug):
    with open(report_name, "w") as outfile:
        config_name = param_obj[KEY_CONFIG_NAME]
        outfile.write("Config_name = {}\n".format(config_name))
        success = True
        slow_progressor_rate = param_obj[KEY_SLOW_PROGRESSOR_RATE]
        latent_cure_rate = param_obj[KEY_LATENT_CURE_RATE]
        presymptomatic_cure_rate = param_obj[KEY_PRESYMPTOMATIC_CURE_RATE]
        presymptomatic_rate = param_obj[KEY_PRESYMPTOMATIC_RATE]
        base_infectivity = param_obj[KEY_BASE_INFECTIVITY]
        simulation_duration = param_obj[KEY_DURATION]
        if not len(output_dict):
            success = False
            outfile.write(dtk_sft.sft_no_test_data)

        outfile.write("checking test conditions: \n")
        dist_exponential_np_slow = np.random.exponential(
            1 / slow_progressor_rate, 100)
        if min(dist_exponential_np_slow) < simulation_duration:
            success = False
            outfile.write(
                "BAD: expected a small {0} to avoid moving individual to active disease state, got {1} from config.json. Please "
                "fix the test.\n".format(KEY_SLOW_PROGRESSOR_RATE,
                                         slow_progressor_rate))
        dist_exponential_np_latent_cure = np.random.exponential(
            1 / latent_cure_rate, 100)
        if min(dist_exponential_np_latent_cure) < simulation_duration:
            success = False
            outfile.write(
                "BAD: expected a small {0} to avoid Latent to Cleared state transition(all Latent state will progress to "
                "PreSymptomatic), got {1} from config.json. Please fix the test.\n"
                .format(KEY_LATENT_CURE_RATE, latent_cure_rate))
        dist_exponential_np_presymptomatic = np.random.exponential(
            1 / presymptomatic_rate, 100)
        if min(dist_exponential_np_presymptomatic) < simulation_duration:
            success = False
            outfile.write(
                "BAD: expected a small {0} to avoid PreSymptomatic to Symptomatic state transition(all PreSymptomatic "
                "state will progress to Cleared), got {1} from config.json. Please fix the test.\n"
                .format(KEY_PRESYMPTOMATIC_RATE, presymptomatic_rate))
        if base_infectivity:
            success = False
            outfile.write(
                "BAD: expected {0} = 0 to look only at progression, got {1} from config.json. Please fix"
                "the test.\n".format(KEY_BASE_INFECTIVITY, base_infectivity))
        outfile.write("conditional check result is {}.\n".format(success))

        actual_timer = []
        internal_timer = []
        outfile.write(
            "collecting the actual timestep between PreSymptomatic and Cleared:\n"
        )
        outfile.write(
            "checking if the internal timer matches the PreSymptomatic to Cleared duration:\n"
        )
        for id in output_dict:
            cleared_time = presymptomatic_time = timer = None
            if KEY_CLEARED in output_dict[id]:
                cleared_time = output_dict[id][KEY_CLEARED][0]
                timer = output_dict[id][KEY_CLEARED][1]
                internal_timer.append(timer)
            if KEY_PRESYMPTOMATIC in output_dict[id]:
                presymptomatic_time = output_dict[id][KEY_PRESYMPTOMATIC][0]
            if presymptomatic_time:
                if cleared_time:  # some individual may not move to cleared state at the end of the simulation
                    actual_timer.append(cleared_time - presymptomatic_time)
                    if cleared_time - presymptomatic_time != math.ceil(timer):
                        success = False
                        outfile.write(
                            "BAD: individual {0} has internal timer = {1} but the actual timer is {2} (enter "
                            "PreSymptomatic state at timestep {3}, moved to Cleared state at "
                            "timestep {4}).\n".format(
                                id, timer, cleared_time - presymptomatic_time,
                                presymptomatic_time, cleared_time))
                else:
                    outfile.write(
                        "Individual {0} moved to PreSymptomatic state at timestep {1} and is not cleared yet at the "
                        "end of simulation (duration = {2})."
                        "\n".format(id, presymptomatic_time,
                                    simulation_duration))
            else:
                success = False
                outfile.write(
                    "BAD: individual {0} moved to cleared state at timerstep {1} before entering "
                    "PreSymptomatic state.\n".format(id, cleared_time))

        if not len(actual_timer):
            success = False
            outfile.write(
                "BAD: There is no PreSymptomatic to cleared transition in this test, please fix the test.\n"
            )

        outfile.write(
            "Running ks test for PreSymptomatic to cleared internal timer and numpy exponential distribution: \n"
        )
        size = len(internal_timer)
        scale = 1.0 / presymptomatic_cure_rate
        dist_exponential_np = np.random.exponential(scale, size)
        dtk_sft.plot_data_sorted(
            internal_timer,
            dist2=np.array(dist_exponential_np),
            label1="PreSymptomatic to cleared duration",
            label2="numpy exponential",
            title="exponential rate = {}".format(presymptomatic_cure_rate),
            xlabel="data point",
            ylabel="PreSymptomatic to cleared duration",
            category='PreSymptomatic_to_cleared_duration',
            show=True,
            line=True,
            overlap=True)
        result = dtk_sft.test_exponential(internal_timer,
                                          p1=presymptomatic_cure_rate,
                                          report_file=outfile,
                                          integers=False,
                                          roundup=False,
                                          round_nearest=False)
        outfile.write(
            "ks test result is {0}, exponential rate = {1}, # of data point = {2}.\n"
            .format(result, presymptomatic_cure_rate, size))
        if not result:
            success = False
            outfile.write(
                "BAD: test exponential for PreSymptomatic to cleared duration failed with {0} "
                "= {1}.\n".format(KEY_PRESYMPTOMATIC_CURE_RATE,
                                  presymptomatic_cure_rate))
        else:
            outfile.write(
                "GOOD: test exponential for PreSymptomatic to cleared duration passed with {0} "
                "= {1}.\n".format(KEY_PRESYMPTOMATIC_CURE_RATE,
                                  presymptomatic_cure_rate))

        outfile.write(dtk_sft.format_success_msg(success))
    if debug:
        print("SUMMARY: Success={0}\n".format(success))
    return success
Example #37
0
def application(report_file):
    #pdb.set_trace()
    #print( "Post-processing: " + report_file )

    cdj = json.loads(open("config.json").read())["parameters"]
    tsf = cdj["Typhoid_Symptomatic_Fraction"]
    start_time = cdj["Start_Time"]
    isj = json.loads(open("output/InsetChart.json").read())["Channels"]
    nibrc = isj["New Infections By Route (CONTACT)"]["Data"]
    nibre = isj["New Infections By Route (ENVIRONMENT)"]["Data"]

    timestep = start_time
    count_new_infection = 0
    count_Outbreak = 0
    #count_Outbreak_int = 0
    count_contact = 0
    count_enviro = 0
    new_infection = []
    new_Outbreak = []
    #new_Outbreak_int =[]
    new_contact = []
    new_enviro = []
    lines = []

    #length = os.path.getsize("test.txt")

    with open("test.txt") as logfile:
        for num, line in enumerate(logfile):
            if re.search("Update\(\): Time:", line):
                #calculate time step and collect counters
                timestep += 1
                new_infection.append(count_new_infection)
                new_Outbreak.append(count_Outbreak)
                #new_Outbreak_int.append(count_Outbreak_int)
                new_contact.append(count_contact)
                new_enviro.append(count_enviro)
                # reset all counters at each time step
                count_new_infection = 0
                count_Outbreak = 0
                #count_Outbreak_int =0
                count_contact = 0
                count_enviro = 0
            if re.search("Calculated prepatent duration", line):
                count_new_infection += 1
                line = "line: " + str(num) + " TimeStep: " + str(
                    timestep) + " " + line
                lines.append(line)
            if re.search("AcquireNewInfection:", line) and re.search(
                    "route=0", line):
                #print line
                print("New outbreak infection.")
                count_Outbreak += 1
                line = "line: " + str(num) + " TimeStep: " + str(
                    timestep) + " " + line
                lines.append(line)
            if re.search("AcquireNewInfection:", line) and re.search(
                    "route=1", line):
                #print line
                print("New contact infection.")
                count_contact += 1
                line = "line: " + str(num) + " TimeStep: " + str(
                    timestep) + " " + line
                lines.append(line)
            if re.search("AcquireNewInfection:", line) and re.search(
                    "route=2", line):
                #print line
                print("New enviro infection.")
                count_enviro += 1
                line = "line: " + str(num) + " TimeStep: " + str(
                    timestep) + " " + line
                lines.append(line)
            # if re.search( "\'OutbreakIndividual\' interventions", line ):
            #     #print line
            #     count_Outbreak_int += 1
            #     line = "line: "+ str(num) + " TimeStep: " + str(timestep) + " " + line
            #     lines.append(line)

    success = True
    error_log = []
    with open(sft.sft_output_filename, "w") as report_file:
        if len(new_infection) == 0 or len(lines) == 0:
            success = False
            report_file.write("Found no data matching test case.\n")
        else:
            for x in range(0, len(new_infection) - 1):
                new_infection_log = new_infection[x + 1]
                new_outbreak_log = new_Outbreak[x + 1]
                #new_Outbreak_int_log = new_Outbreak_int[x+1]
                new_contact_log = new_contact[x + 1]
                new_enviro_log = new_enviro[x + 1]
                total_log = new_outbreak_log + new_enviro_log + new_contact_log
                new_infection_contact_output = nibrc[x]
                new_infection_environment_output = nibre[x]
                if new_infection_log != total_log:
                    success = False
                    report_file.write(
                        "BAD: At time {0}: new prepatent case = {1}, expected {2}(new infection by route (contact) = {3}, new infection by route (environment) = {4}, new infection by Outbreak = {5}).\n"
                        .format(x + start_time + 1, new_infection_log,
                                total_log, new_contact_log, new_enviro_log,
                                new_outbreak_log))
                if new_contact_log != new_infection_contact_output:
                    success = False
                    report_file.write(
                        "BAD: At time {0}: new infection by contact route is {1} from Stdout while it's {2} from InsetChart ).\n"
                        .format(x + start_time + 1, new_contact_log,
                                new_infection_contact_output))
                if new_enviro_log != new_infection_environment_output:
                    success = False
                    report_file.write(
                        "BAD: At time {0}: new infection by environment route is {1} from Stdout while it's {2} from InsetChart ).\n"
                        .format(x + start_time + 1, new_enviro_log,
                                new_infection_environment_output))
                # if new_outbreak_log != new_Outbreak_int_log:
                #     success = False
                #     report_file.write(
                #         "BAD: At time {0}: {1} \'OutbreakIndividual\' interventions is gave out but only {2} received it.\n".format(
                #             x + start_time + 1, new_Outbreak_int_log, new_outbreak_log))
        for i in range(0, len(lines)):
            line = lines[i]
            if re.search("AcquireNewInfection:", line) and re.search(
                    "route=0", line):
                next_line = lines[i + 1]
                if not re.search("doseTracking = Low", next_line):
                    error_log.append(line)
                    error_log.append(next_line)
            if re.search("AcquireNewInfection:", line) and re.search(
                    "route=1", line):
                next_line = lines[i + 1]
                if not re.search("doseTracking = High", next_line):
                    error_log.append(line)
                    error_log.append(next_line)
        if len(error_log) != 0:
            success = False
            report_file.write(
                "BAD: Some infected individuals didn't fall in the right doseTracking categories. Expected: route 0 - doseTracking = Low and route 1 - doseTracking = High. Please see the details in \"categogy_error_log.txt\".\n"
            )
            with open("category_error_log.txt", "w") as log:
                for line in error_log:
                    log.write(line)

        if success:
            report_file.write(sft.format_success_msg(success))
            os.remove("test.txt")
Example #38
0
def create_report_file(param_obj, multipliers, infectiousness, report_name, debug):
    with open(report_name, "w") as outfile:
        success = True
        if not multipliers:
            outfile.write(sft.sft_no_test_data)
        sigma = param_obj[Param_keys.LOGNORMAL_SCALE]
        base_infectivity = param_obj[Param_keys.BASE_INFECTIVITY]
        if sigma > 0:
            mu = - sigma**2 / 2.0
            # test log_normal distribution
            success = sft.test_lognorm(multipliers,mu=mu, sigma=sigma,report_file=outfile,round = False)

            # test mean_l = 1
            mean_l = np.mean(multipliers)
            mean_infectiousness = np.mean(infectiousness)
            outfile.write("mean of the multipliers is {}, expected 1.0.\n".format(mean_l))
            outfile.write("mean of the Infectiousness is {0}, while base infectivity is {1}.\n".format(mean_infectiousness,
                                                                                           base_infectivity))

            tolerance  = 2e-2
            if math.fabs(mean_l - 1.0) > tolerance:
                outfile.write("BAD: mean of the multipliers is {}, expected 1.0.\n".format(mean_l))
                success = False
            # plotting
            size = len(multipliers)
            outfile.write("size is {}\n".format(size))
            scale = math.exp(mu)
            dist_lognormal = stats.lognorm.rvs(sigma, 0, scale, size)
            sft.plot_data(multipliers, dist_lognormal,
                          label1="Emod", label2="Scipy",
                          ylabel="Multiplier", xlabel="data point",
                          category="Emod_vs_Scipy",
                          title="Emod_vs_Scipy, sigma = {}".format(sigma),
                          show=True)
            sft.plot_probability(multipliers, dist_lognormal,
                                 precision=1, label1="Emod", label2="Scipy",
                                 category="Probability_mass_function_Emod_vs_Scipy",
                                 title="Emod_vs_Scipy, sigma = {}".format(sigma),
                                 show=True)
            sft.plot_cdf(multipliers,dist_lognormal,label1="Emod", label2="Scipy",
                                 category="cdf",
                                 title="cdf, sigma = {}".format(sigma),
                                 show=True, line = False)
            if debug:
                with open("scipy_data.txt", "w") as file:
                    for n in sorted(dist_lognormal):
                        file.write(str(n) + "\n")
                with open("emod_data.txt", "w") as file:
                    for n in sorted(multipliers):
                        file.write(str(n) + "\n")
        else:
            # sigma = 0, this feature is disabled
            for multiplier in multipliers:
                if multiplier != 1.0:
                    success = False
                    outfile.write("BAD: multiplier is {0} when {1} set to {2}, expected 1.0.\n".format(multiplier, Param_keys.LOGNORMAL_SCALE, sigma))
            # plotting
            sft.plot_data(multipliers, label1="Multiplier", label2="NA",
                          category="Multiplier", title="Multiplier_Sigma={}".format(sigma),
                          ylabel="Multiplier", xlabel="data point",
                          show=True)
            sft.plot_data(infectiousness, label1="Infectiousness",
                          label2="NA",category="Infectiousness",
                          title="Infectiousness_Sigma={0}_BaseInfectivity={1}".format(sigma,base_infectivity),
                          ylabel="Infectiousness",xlabel="data point",
                          show=True)
        outfile.write(sft.format_success_msg(success))

    if debug:
        print "SUMMARY: Success={0}\n".format(success)
    return success
Example #39
0
def create_report_file(param_obj, output_dict, report_name, debug):
    with open(report_name, "w") as outfile:
        config_name = param_obj[KEY_CONFIG_NAME]
        outfile.write("Config_name = {}\n".format(config_name))
        success = True
        fast_progressor_rate = param_obj[KEY_FAST_PROGRESSOR_RATE]
        latent_cure_rate = param_obj[KEY_LATENT_CURE_RATE]
        child_fast_fraction = param_obj[KEY_CHILD_FRACTION]
        adult_fast_fraction = param_obj[KEY_ADULT_FRACTION]
        simulation_duration = param_obj[KEY_DURATION]
        if not len(output_dict):
            success = False
            outfile.write(dtk_sft.sft_no_test_data)

        outfile.write("checking test conditions: \n")
        if not child_fast_fraction or not adult_fast_fraction:
            success = False
            outfile.write(
                "BAD: expected {0} and {1} = 1, got {2} and {3} from config.json. "
                "Please fix the test.\n".format(KEY_CHILD_FRACTION,
                                                KEY_ADULT_FRACTION,
                                                child_fast_fraction,
                                                adult_fast_fraction))
        dist_exponential_np_fast = np.random.exponential(
            1 / fast_progressor_rate, 100)
        if min(dist_exponential_np_fast) < simulation_duration:
            success = False
            outfile.write(
                "BAD: expected a small {0} to avoid moving individual to active disease state, got {1} from config.json. Please "
                "fix the test.\n".format(KEY_FAST_PROGRESSOR_RATE,
                                         fast_progressor_rate))
        outfile.write("conditional check result is {}.\n".format(success))

        actual_timer = []
        outfile.write(
            "collecting the actual timestep between latent and cleared:\n")
        for id in output_dict:
            cleared_time = presymptomatic_time = latent_time = None
            if KEY_CLEARED in output_dict[id]:
                cleared_time = output_dict[id][KEY_CLEARED]
            if KEY_LATENT in output_dict[id]:
                latent_time = output_dict[id][KEY_LATENT]
            if KEY_PRESYMPTOMATIC in output_dict[id]:
                presymptomatic_time = output_dict[id][KEY_PRESYMPTOMATIC]
            if latent_time:
                if cleared_time:  # some individual may not move to cleared state at the end of the simulation
                    actual_timer.append(cleared_time - latent_time)
                else:
                    outfile.write(
                        "Individual {0} moved to latent state at timestep {1} and is not cleared yet at the "
                        "end of simulation (duration = {2})."
                        "\n".format(id, latent_time, simulation_duration))
            else:
                success = False
                outfile.write(
                    "BAD: individual {0} moved to cleared state at timerstep {1} before entering "
                    "latent state.\n".format(id, cleared_time))
            if presymptomatic_time:
                success = False
                outfile.write(
                    "BAD: individual {0} moved to presymptomatic at timestep {1}, expected no active disease"
                    " in this simulation, please double check the config.\n".
                    format(id, presymptomatic_time))
        if not len(actual_timer):
            success = False
            outfile.write(
                "BAD: There is no latent to cleared transition in this test, please fix the test.\n"
            )

        outfile.write(
            "Running ks test for latent to cleared duration and numpy exponential distribution: \n"
        )
        size = len(actual_timer)
        scale = 1.0 / latent_cure_rate
        dist_exponential_np = np.random.exponential(scale, size)
        dtk_sft.plot_data_sorted(
            actual_timer,
            dist2=np.array(dist_exponential_np),
            label1="latent to cleared duration",
            label2="numpy exponential",
            title="exponential rate = {}".format(latent_cure_rate),
            xlabel="data point",
            ylabel="latent to cleared duration",
            category='latent_to_cleared_duration',
            show=True,
            line=False,
            overlap=True)
        result = dtk_sft.test_exponential(actual_timer,
                                          p1=latent_cure_rate,
                                          report_file=outfile,
                                          integers=True,
                                          roundup=True,
                                          round_nearest=False)
        outfile.write(
            "ks test result is {0}, exponential rate = {1}, # of data point = {2}.\n"
            .format(result, latent_cure_rate, size))
        if not result:
            success = False
            outfile.write(
                "BAD: test exponential for latent to cleared duration failed with {0} "
                "= {1}.\n".format(KEY_LATENT_CURE_RATE, latent_cure_rate))
        else:
            outfile.write(
                "GOOD: test exponential for latent to cleared duration passed with {0} "
                "= {1}.\n".format(KEY_LATENT_CURE_RATE, latent_cure_rate))

        outfile.write(dtk_sft.format_success_msg(success))
    if debug:
        print("SUMMARY: Success={0}\n".format(success))
    return success
Example #40
0
def create_report_file(param_obj, campaign_obj, output_dict, report_dict,
                       report_name, debug):
    with open(report_name, "w") as outfile:
        config_name = param_obj[KEY_CONFIG_NAME]
        outfile.write("Config_name = {}\n".format(config_name))
        success = True
        specificity = campaign_obj[KEY_BASE_SPECIFICITY]
        treatment_fraction = campaign_obj[KEY_TREATMENT_FRACTION]
        prob = (1.0 - specificity) * treatment_fraction
        binomial_test_count = 0
        positive = []
        total = []
        if not len(report_dict):
            success = False
            outfile.write(dtk_sft.sft_no_test_data)
        for t in report_dict:
            num_success = report_dict[t][KEY_POSITIVE]
            num_trials = report_dict[t][KEY_NEGATIVE] + num_success
            positive.append(num_success)
            total.append(num_trials)
            if num_trials * prob < 5 or num_trials * (1 - prob) < 5:
                outfile.write(
                    "At timestep {0}, there is not enough sample size : mean = {1}, sample size - mean = {2}"
                    ".\n".format(t, num_trials * prob,
                                 num_trials * (1 - prob)))
            else:
                result = dtk_sft.test_binomial_95ci(num_success,
                                                    num_trials,
                                                    prob,
                                                    report_file=outfile,
                                                    category="TB positive")
                outfile.write(
                    "At timestep {0}, the binomial 95% test result is {1}.\n".
                    format(t, result))
                binomial_test_count += 1
                if not result:
                    success = False
        if not binomial_test_count:
            success = False
            outfile.write(
                "BAD: There is not enough sample size for binomial test in every time step, please fix the test.\n"
            )
        dtk_sft.plot_data(
            positive,
            dist2=total,
            label1="TBTestPositive",
            label2="Total tested",
            title="Test positive vs. total, positive proportion = {}".format(
                prob),
            xlabel="time step",
            ylabel="# of individuals",
            category='Test_positive_vs_total',
            show=True,
            line=False)
        # When Treatment_fraction is set to 1, the report should match debug log. Here is the test for it:
        for t in output_dict:
            log_positive = output_dict[t][KEY_POSITIVE]
            log_negative = output_dict[t][KEY_NEGATIVE]
            match_error = 0
            if log_negative or log_positive:
                report_positive = report_dict[t][KEY_POSITIVE]
                report_negative = report_dict[t][KEY_NEGATIVE]
                if report_positive != log_positive:
                    match_error += 1
                    success = False
                    outfile.write(
                        "BAD: at time step {0} the TBTestPositive is {1} from ReportEventRecorder.csv and {2} from"
                        "debug logging.\n".format(t, report_positive,
                                                  log_positive))
                if report_negative != log_negative:
                    match_error += 1
                    success = False
                    outfile.write(
                        "BAD: at time step {0} the TBTestNegative is {1} from ReportEventRecorder.csv and {2} from"
                        "debug logging.\n".format(t, report_negative,
                                                  log_negative))
            else:
                if t in report_dict:
                    report_positive = report_dict[t][KEY_POSITIVE]
                    report_negative = report_dict[t][KEY_NEGATIVE]
                    match_error += 1
                    success = False
                    outfile.write(
                        "BAD: at time step {0} the TBTestPositive and TBTestNegative are {1} and {2} from "
                        "ReportEventRecorder.csv and {2} and {3} from debug logging. They should be matched\n"
                        "".format(t, report_positive, report_negative,
                                  log_positive, log_negative))
        if not match_error:
            outfile.write(
                "GOOD: The ReportEventRecorder.csv matches the debug logging.\n"
            )
        else:
            outfile.write(
                "BAD: The ReportEventRecorder.csv doesn't match the debug logging.\n"
            )

        outfile.write(dtk_sft.format_success_msg(success))
    if debug:
        print("SUMMARY: Success={0}\n".format(success))
    return success