Commit 6971c67b by Daniel Kullmann

### Started to write code for calculating average data

parent 48949c96
 ... ... @@ -2,6 +2,9 @@ import argparse import corona import datetime import json import copy ALL_KEYS = ['cases', 'deaths'] (data, all_countries, all_dates, population_data) = corona.load_corona_data() ... ... @@ -45,6 +48,57 @@ def make_rank_data(input_data): def get_rank(rank_data, country, key): return rank_data[key][country] def update_average(average_object, value): average_object["sum"] += value average_object["count"] += 1 AVERAGE_OVER_DAYS = 14 def calculate_moving_average(values, all_countries, all_dates): result = {} for country in all_countries: result[country] = {} # Accumulate values for average for date in all_dates: for num_days in range(0, AVERAGE_OVER_DAYS): average_date = date + datetime.timedelta(days=num_days) if average_date not in result[country]: result[country][average_date] = {} for key in ALL_KEYS: if average_date in values[country] and key in values[country][average_date]: value = values[country][average_date][key] if key not in result[country][average_date]: result[country][average_date][key] = {"sum": 0, "count": 0} update_average(result[country][average_date][key], value) # Calculate average for date in list(result[country].keys()): for (key, average_object) in result[country][date].items(): if average_object["count"] < AVERAGE_OVER_DAYS: del result[country][date] break else: result[country][date][key] = average_object["sum"]/average_object["count"] return result def calculate_and_merge_moving_averages(result, all_countries, all_dates, values, values_per_1m, accumulated_values, accumulated_values_per_1m): # "_ma" stands for "moving average" values_ma = calculate_moving_average(values, all_countries, all_dates) values_per_1m_ma = calculate_moving_average(values_per_1m, all_countries, all_dates) accumulated_values_ma = calculate_moving_average(accumulated_values, all_countries, all_dates) accumulated_values_per_1m_ma = calculate_moving_average(accumulated_values_per_1m, all_countries, all_dates) for country in all_countries: if country == "": continue for date in all_dates: date_string = date.isoformat() result[country][date_string]["new-cases-moving-average"] = get_value(values_ma, country, date, "cases") result[country][date_string]["new-deaths-moving-average"] = get_value(values_ma, country, date, "deaths") result[country][date_string]["new-cases-per-1m-moving-average"] = get_value(values_per_1m_ma, country, date, "cases") result[country][date_string]["new-deaths-per-1m-moving-average"] = get_value(values_per_1m_ma, country, date, "deaths") result[country][date_string]["accumulated-cases-moving-average"] = get_value(accumulated_values_ma, country, date, "cases") result[country][date_string]["accumulated-deaths-moving-average"] = get_value(accumulated_values_ma, country, date, "deaths") result[country][date_string]["accumulated-cases-per-1m-moving-average"] = get_value(accumulated_values_per_1m_ma, country, date, "cases") result[country][date_string]["accumulated-deaths-per-1m-moving-average"] = get_value(accumulated_values_per_1m_ma, country, date, "deaths") def merge_values(all_countries, all_dates, values, values_per_1m, accumulated_values, accumulated_values_per_1m): merged_data = dict() for country in all_countries: ... ... @@ -113,6 +167,8 @@ for country in data: "rank_accumulated_deaths_per_1m": rank_accumulated_deaths_per_1m, } calculate_and_merge_moving_averages(merged_data, list(all_country_data.keys()), all_dates, data, values_per_1m, accumulated_values, accumulated_values_per_1m) all_data = { "now": datetime.datetime.now().isoformat(), "countries": all_country_data, ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!