def main():
    # Log status
    logger.info("Loading local data files")
    test_results = read_json_from_file(TEST_RESULTS_PATH)
    hospitalization = read_json_from_file(HOSPITALIZATION_PATH)
    deaths = read_json_from_file(DEATHS_PATH)
    manual_data = read_json_from_file(MANUAL_DATA_PATH)

    # Log status
    logger.info("Calculating main statistics")

    # Create date ranges for charts
    case_dates = pd.date_range(start=DATE_SETTINGS["firstCaseDate"],
                               end=YESTERDAY_YMD)

    # Set recovered, deceased, hospitalized and ICU time-series
    hospital = get_hospital_data(hospitalization,
                                 DATE_SETTINGS["firstCaseDate"])
    recovered = hospital["discharged"]
    manual_data["deceased"].update(deaths)
    deceased = list(manual_data["deceased"].values())
    hospitalized = hospital["activeHospitalizations"]
    # TODO: Based on cross-checking with the hospitalization data publishedby TEHIK, the data listed
    #       in the manual_data.json file with the field name "intensive" appears to show the number
    #       of patients on ventilation. We should fix the terminology and make sure that the intensive
    #       and on ventilation statistics are being calculated correctly.
    intensive = list(
        get_in_intensive_data(hospitalization,
                              manual_data["intensive"]).values())
    on_ventilation = list(get_on_ventilation_data(hospitalization).values())

    # Get data for each chart
    logger.info("Calculating data for charts")
    cumulative_cases_chart_data = get_cumulative_cases_chart_data(
        test_results,
        recovered,
        deceased,
        hospitalized,
        intensive,
        on_ventilation,
        case_dates,
    )

    del cumulative_cases_chart_data["active100k"]

    # Create dictionary for final JSON
    logger.info("Compiling final JSON")
    final_json = {
        "updatedOn": TODAY_DMYHM,
        "caseDates": [str(x.date()) for x in case_dates],
        "dataCumulativeCasesChart": cumulative_cases_chart_data,
    }

    # Dump JSON output
    save_as_json(CUMULATIVE_CASES_PATH, final_json)

    # Log finish time
    logger.info("Finished update process")
def main():
    # Log status
    logger.info("Starting data update process")

    test_results = read_json_from_file(TEST_RESULTS_PATH)
    test_locations = read_json_from_file(TEST_LOCATIONS_PATH)
    hospitalization = read_json_from_file(HOSPITALIZATION_PATH)
    vaccination = read_json_from_file(VACCINATIONS_PATH)

    # Load locally-stored data
    logger.info("Loading local data files")
    deaths = read_json_from_file(DEATHS_PATH)
    manual_data = read_json_from_file(MANUAL_DATA_PATH)

    # Log status
    logger.info("Calculating main statistics")

    # Statsbar
    # Find count of confirmed cases
    n_confirmed_cases = np.sum(
        [res["ResultValue"] == "P" for res in test_results])

    # Find total number of tests
    n_tests_administered = len(test_results)

    # Create date ranges for charts
    # dates1 = pd.date_range(start=DATE_SETTINGS["dates1_start"], end=yesterday)
    case_dates = pd.date_range(start=DATE_SETTINGS["firstCaseDate"],
                               end=YESTERDAY_YMD)
    vaccination_dates = pd.date_range(
        start=DATE_SETTINGS["vaccinationStartDate"], end=YESTERDAY_YMD)

    # Set recovered, deceased, hospitalized and ICU time-series
    hospital = get_hospital_data(hospitalization,
                                 DATE_SETTINGS["firstCaseDate"])
    recovered = hospital["discharged"]
    manual_data["deceased"].update(deaths)
    deceased = list(manual_data["deceased"].values())
    hospitalized = hospital["activehospitalizations"]
    # TODO: Based on cross-checking with the hospitalization data publishedby TEHIK, the data listed
    #       in the manual_data.json file with the field name "intensive" appears to show the number
    #       of patients on ventilation. We should fix the terminology and make sure that the intensive
    #       and on ventilation statistics are being calculated correctly.
    intensive = list(
        get_in_intensive_data(hospitalization,
                              manual_data["intensive"]).values())
    on_ventilation = list(get_on_ventilation_data(hospitalization).values())

    n_deaths = deceased[-1]
    n_deaths_change = int(deceased[-1]) - int(deceased[-2])

    # Get data for each chart
    logger.info("Calculating data for charts")
    infections_by_county = get_infection_count_by_county(
        test_results, COUNTY_MAPPING)
    infections_by_county_10000 = get_infections_data_by_count_10000(
        infections_by_county, COUNTY_POPULATION)
    tests_pop_ratio = get_test_data_pop_ratio(infections_by_county_10000)
    county_by_day = get_county_by_day(test_results, case_dates, COUNTY_MAPPING,
                                      COUNTY_POPULATION)
    confirmed_cases_by_county = get_confirmed_cases_by_county(
        test_results, COUNTY_MAPPING)
    cumulative_cases_chart_data = get_cumulative_cases_chart_data(
        test_results,
        recovered,
        deceased,
        hospitalized,
        intensive,
        on_ventilation,
        case_dates,
    )
    new_cases_per_day_chart_data = get_new_cases_per_day_chart_data(
        cumulative_cases_chart_data)
    cumulative_tests_chart_data = get_cumulative_tests_chart_data(
        test_results, case_dates)
    tests_per_day_chart_data = get_tests_per_day_chart_data(
        test_results, case_dates)
    positive_test_by_age_chart_data = get_positive_tests_by_age_chart_data(
        test_results)
    positive_negative_chart_data = get_positive_negative_chart_data(
        test_results, COUNTY_MAPPING)
    vaccinated_people_chart_data = get_vaccinated_people_chart_data(
        vaccination, vaccination_dates)
    county_daily_active = get_county_daily_active(test_results, case_dates,
                                                  COUNTY_MAPPING,
                                                  COUNTY_POPULATION)
    n_active_cases = cumulative_cases_chart_data["active"][-1]
    n_active_cases_change = (cumulative_cases_chart_data["active"][-1] -
                             cumulative_cases_chart_data["active"][-2])
    active_infections_by_county = [{
        "MNIMI": k,
        "sequence": v,
        "drilldown": k
    } for k, v in county_daily_active["countyByDayActive"].items()]
    active_infections_by_county_100k = [[
        k, round(v[-1] / COUNTY_POPULATION[k] * 100000, 2)
    ] for k, v in county_daily_active["countyByDayActive"].items()]
    municipalities_data = get_municipality_data(test_locations, COUNTY_MAPPING)
    per_100k = cumulative_cases_chart_data["active100k"][-1]

    # Calculate vaccination data
    logger.info("Calculating vaccination data")
    last_day_vaccination_data = [
        x for x in vaccination if x["MeasurementType"] == "Vaccinated"
    ][-1]
    last_day_completed_vaccination_data = [
        x for x in vaccination if x["MeasurementType"] == "FullyVaccinated"
    ][-1]
    # TODO: Doses administered
    # last_day_doses_administered_data = [x for x in json_vaccination if x['MeasurementType'] == 'DosesAdministered'][-1]
    completed_vaccination_number_total = last_day_completed_vaccination_data[
        "TotalCount"]
    completed_vaccination_number_last_day = last_day_completed_vaccination_data[
        "DailyCount"]
    all_vaccination_number_total = last_day_vaccination_data["TotalCount"]
    all_vaccination_number_last_day = last_day_vaccination_data["DailyCount"]
    vaccination_number_total = (all_vaccination_number_total -
                                completed_vaccination_number_total)
    vaccination_number_last_day = (all_vaccination_number_last_day -
                                   completed_vaccination_number_last_day)
    fully_vaccinated_from_total_vaccinated_percentage = round(
        completed_vaccination_number_total * 100 /
        (all_vaccination_number_total), 2)

    # Create dictionary for final JSON
    logger.info("Compiling final JSON")
    final_json = {
        "updatedOn":
        TODAY_DMYHM,
        "confirmedCasesNumber":
        str(n_confirmed_cases),
        # TODO: For consistency, we should include the change in the number of confirmed cases as well.
        "hospitalizedNumber":
        str(hospital["activehospitalizations"][-1]),
        "hospitalChanged":
        str(hospital["activehospitalizations"][-1] -
            hospital["activehospitalizations"][-2]),
        "deceasedNumber":
        str(n_deaths),
        "deceasedChanged":
        str(n_deaths_change),
        "recoveredNumber":
        str(hospital["discharged"][-1]),
        "recoveredChanged":
        str(hospital["discharged"][-1] - hospital["discharged"][-2]),
        "testsAdministeredNumber":
        str(n_tests_administered),
        # TODO: For consistency, we should include the change in the number of tests as well.
        "activeCasesNumber":
        str(n_active_cases),
        "activeChanged":
        str(n_active_cases_change),
        "perHundred":
        str(per_100k),  # TODO: This should be given a clearer name.
        # TODO: I can't find anywhere in the app where "dates1" is used. Is it needed? Commented out for now.
        # "dates1": [str(x.date()) for x in dates1],
        "caseDates": [str(x.date()) for x in case_dates],
        "vaccinationDates": [str(x.date()) for x in vaccination_dates],
        "counties":
        COUNTIES_INCL_UNKNOWN,
        "age_groups":
        AGE_GROUPS,
        "dataInfectionsByCounty":
        infections_by_county,
        "dataInfectionsByCounty10000":
        infections_by_county_10000,
        "dataActiveInfectionsByCounty100k":
        active_infections_by_county_100k,
        "dataActiveInfectionsByCounty":
        active_infections_by_county,
        "dataTestsPopRatio":
        tests_pop_ratio,
        "countyByDay":
        county_by_day,
        "dataCountyDailyActive":
        county_daily_active,
        "dataConfirmedCasesByCounties":
        confirmed_cases_by_county,
        "dataCumulativeCasesChart":
        cumulative_cases_chart_data,
        "dataNewCasesPerDayChart":
        new_cases_per_day_chart_data,
        "dataCumulativeTestsChart":
        cumulative_tests_chart_data,
        "dataTestsPerDayChart":
        tests_per_day_chart_data,
        "dataPositiveTestsByAgeChart":
        positive_test_by_age_chart_data,
        "dataPositiveNegativeChart":
        positive_negative_chart_data,
        "dataVaccinatedPeopleChart":
        vaccinated_people_chart_data,
        "dataMunicipalities":
        municipalities_data,
        "hospital":
        hospital,  # TODO: Rename this to make it clearer what data it contains.
        "vaccinationNumberTotal":
        vaccination_number_total,
        "vaccinationNumberLastDay":
        vaccination_number_last_day,
        "completedVaccinationNumberTotal":
        completed_vaccination_number_total,
        "completedVaccinationNumberLastDay":
        completed_vaccination_number_last_day,
        "allVaccinationNumberTotal":
        all_vaccination_number_total,
        "allVaccinationNumberLastDay":
        all_vaccination_number_last_day,
        "allVaccinationFromPopulationPercentage":
        last_day_vaccination_data["PopulationCoverage"],
        "completelyVaccinatedFromTotalVaccinatedPercentage":
        fully_vaccinated_from_total_vaccinated_percentage,
    }

    # Dump JSON output
    save_as_json(OUTPUT_PATH, final_json)

    # Log finish time
    logger.info("Finished update process")
Example #3
0
def main():
    # Log status
    logger.info("Loading local data files")
    test_results = read_json_from_file(TEST_RESULTS_PATH)
    hospitalization = read_json_from_file(HOSPITALIZATION_PATH)
    vaccination = read_json_from_file(VACCINATIONS_PATH)
    deaths = read_json_from_file(DEATHS_PATH)
    manual_data = read_json_from_file(MANUAL_DATA_PATH)

    # Log status
    logger.info("Calculating main statistics")

    # Statsbar
    # Find count of confirmed cases
    n_confirmed_cases = np.sum(
        [res["ResultValue"] == "P" for res in test_results])

    # Find total number of tests
    n_tests_administered = len(test_results)

    # Create date ranges for charts
    case_dates = pd.date_range(start=DATE_SETTINGS["firstCaseDate"],
                               end=YESTERDAY_YMD)

    # Set recovered, deceased, hospitalized and ICU time-series
    hospital = get_hospital_data(hospitalization,
                                 DATE_SETTINGS["firstCaseDate"])
    recovered = hospital["discharged"]
    manual_data["deceased"].update(deaths)
    deceased = list(manual_data["deceased"].values())
    hospitalized = hospital["activeHospitalizations"]
    # TODO: Based on cross-checking with the hospitalization data publishedby TEHIK, the data listed
    #       in the manual_data.json file with the field name "intensive" appears to show the number
    #       of patients on ventilation. We should fix the terminology and make sure that the intensive
    #       and on ventilation statistics are being calculated correctly.
    intensive = list(
        get_in_intensive_data(hospitalization,
                              manual_data["intensive"]).values())
    on_ventilation = list(get_on_ventilation_data(hospitalization).values())

    n_deaths = deceased[-1]
    n_deaths_change = int(deceased[-1]) - int(deceased[-2])

    # Get data for each chart
    logger.info("Calculating data for charts")
    tests_per_day_chart_data = get_tests_per_day_chart_data(
        test_results, case_dates)
    cumulative_cases_chart_data = get_cumulative_cases_chart_data(
        test_results,
        recovered,
        deceased,
        hospitalized,
        intensive,
        on_ventilation,
        case_dates,
        test_per_day_chart_data=tests_per_day_chart_data,
    )
    new_cases_per_day_chart_data = get_new_cases_per_day_chart_data(
        cumulative_cases_chart_data)
    cumulative_tests_chart_data = get_cumulative_tests_chart_data(
        test_results, case_dates)

    n_active_cases = cumulative_cases_chart_data["active"][-1]
    n_active_cases_change = (cumulative_cases_chart_data["active"][-1] -
                             cumulative_cases_chart_data["active"][-2])

    # Calculate vaccination data
    logger.info("Calculating vaccination data")
    last_day_vaccination_data = [
        x for x in vaccination if x["MeasurementType"] == "Vaccinated"
    ][-1]
    last_day_completed_vaccination_data = [
        x for x in vaccination if x["MeasurementType"] == "FullyVaccinated"
    ][-1]
    # TODO: Doses administered
    completed_vaccination_number_total = last_day_completed_vaccination_data[
        "TotalCount"]
    completed_vaccination_number_last_day = last_day_completed_vaccination_data[
        "DailyCount"]
    all_vaccination_number_total = last_day_vaccination_data["TotalCount"]
    all_vaccination_number_last_day = last_day_vaccination_data["DailyCount"]
    vaccination_number_total = (all_vaccination_number_total -
                                completed_vaccination_number_total)
    vaccination_number_last_day = (all_vaccination_number_last_day -
                                   completed_vaccination_number_last_day)
    fully_vaccinated_from_total_vaccinated_percentage = round(
        completed_vaccination_number_total * 100 /
        (all_vaccination_number_total), 2)
    raw_active_100k = (cumulative_cases_chart_data["active"][-1] -
                       cumulative_cases_chart_data["active"][-2])
    raw_confirmed_changed = new_cases_per_day_chart_data["confirmedCases"][-1]
    raw_per_hundred_changed = (cumulative_cases_chart_data["active100k"][-1] -
                               cumulative_cases_chart_data["active100k"][-2])
    confirmed_changed = new_cases_per_day_chart_data["confirmedCases"][-1]
    tests_changed = (cumulative_tests_chart_data["testsAdministered"][-1] -
                     cumulative_tests_chart_data["testsAdministered"][-2])

    partially_immunized_total = all_vaccination_number_total + n_confirmed_cases
    partially_immunized_pct = round(
        (float(partially_immunized_total) / float(POPULATION) * 100), 2)

    # Create dictionary for final JSON
    logger.info("Compiling final JSON")
    final_json = {
        "updatedOn":
        TODAY_DMYHM,
        "activeCasesNumber":
        str(n_active_cases),
        "activeChanged":
        str(n_active_cases_change),
        "allVaccinationFromPopulationPercentage":
        last_day_vaccination_data["PopulationCoverage"],
        "allVaccinationNumberLastDay":
        all_vaccination_number_last_day,
        "allVaccinationNumberTotal":
        all_vaccination_number_total,
        "completedVaccinationNumberLastDay":
        completed_vaccination_number_last_day,
        "completedVaccinationNumberTotal":
        completed_vaccination_number_total,
        "completelyVaccinatedFromTotalVaccinatedPercentage":
        fully_vaccinated_from_total_vaccinated_percentage,
        "confirmedCasesNumber":
        str(n_confirmed_cases),
        "confirmedChanged":
        confirmed_changed,
        "deceasedChanged":
        str(n_deaths_change),
        "deceasedNumber":
        str(n_deaths),
        "hospitalizedChanged":
        str(hospital["activeHospitalizations"][-1] -
            hospital["activeHospitalizations"][-2]),
        "hospitalizedNumber":
        hospital["activeHospitalizations"][-1],
        "partiallyImmunized":
        partially_immunized_total,
        "partiallyImmunizedPercentage":
        partially_immunized_pct,
        "perHundred":
        cumulative_cases_chart_data["active100k"][-1],
        "positiveTestAverage14Percent":
        tests_per_day_chart_data["positiveTestAverage14Percent"],
        "rawActiveChanged":
        raw_active_100k,
        "rawConfirmedChanged":
        raw_confirmed_changed,
        "rawPerHundredChanged":
        raw_per_hundred_changed,
        "recoveredChanged":
        str(hospital["discharged"][-1] - hospital["discharged"][-2]),
        "recoveredNumber":
        hospital["discharged"][-1],
        "testsAdministeredNumber":
        str(n_tests_administered),
        "testsChanged":
        tests_changed,
        "vaccinationNumberLastDay":
        vaccination_number_last_day,
        "vaccinationNumberTotal":
        vaccination_number_total,
    }

    # Dump JSON output
    save_as_json(STATS_BAR_PATH, final_json)

    # Log finish time
    logger.info("Finished update process")