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, 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
Esempio n. 3
0
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