def main():
    args = CLIArgs.read_args(sys.argv[1:])
    if args.command == CLIArgs.CONFIRMED_COMMAND:
        charts = BaseCharts(csv_confirmed)
        map_builder = MapCharts(csv_confirmed)
    elif args.command == CLIArgs.RECOVERED_COMMAND:
        charts = BaseCharts(cvs_recovered)
        map_builder = MapCharts(cvs_recovered)
    elif args.command == CLIArgs.DEATHS_COMMAND:
        charts = BaseCharts(cvs_death)
        map_builder = MapCharts(cvs_death)

    if not args.output:
        filename = os.path.join(output_dir, "chart-{}.html".format(args.command))
    else:
        filename = args.output

    current_date = today
    date_str = DateHelper.date_to_str(current_date)
    while not charts.has_coloumn(date_str):
        date_str = DateHelper.previous_day_str(date_str)
    yday_str = DateHelper.previous_day_str(date_str)

    # Total of contagions in the last day
    chart_tot_per_day = charts.get_histogram_per_day(date_str)
    chart_increment_last = charts.get_histogram_increment_last_day(date_str)
    chart_lines_incr = charts.get_chart_increment_per_day(date_str,
                                                          title=f"Day by day increment of {args.command} patients")

    map_chart = map_builder.get_map(date_str, f"Worldwide map of {args.command} patients")

    chart_lines_tot = charts.get_chart_total_per_day(date_str, title="Total in top 10 countries")

    chart_row = alt.hconcat(chart_tot_per_day, chart_increment_last)
    chart = alt.vconcat(map_chart, chart_lines_tot, chart_lines_incr, chart_row)

    if args.format == OutputFormat.html:
        if not os.path.exists(output_dir):
            os.mkdir(output_dir)
        chart.save(filename)
        print("{} generated successfully !". format(filename))
    elif args.format == OutputFormat.json:
        if not os.path.exists(output_dir):
            os.mkdir(output_dir)
        chart.save(filename)
        print("{} generated successfully !". format(filename))
    else:
        chart.serve()
示例#2
0
 def show_image_from_data(data):
     if os.name == 'posix':
         img = Image.open(StringIO(data))
         img.show()
     else:
         print "deprecated method for windows system!"
         tmp_file = 'tmp_%s.jpg' % DateHelper.get_current_timestamp()
         ImageHelper.save_image_file(data, tmp_file)
         ImageHelper.show_from_file(tmp_file)
         SystemHelper.delete(tmp_file)
    def get_histogram_increment_last_day(self, day_str):
        chart_data = self._data
        col_name_increments = "increments"
        chart_data[col_name_increments] = self._data[day_str] - self._data[DateHelper.previous_day_str(day_str)]
        chart_data = self._data[[self.col_name_countries, col_name_increments]]
        chart_data = chart_data.groupby(self.col_name_countries, as_index=False).agg({col_name_increments: "sum"})
        chart_data = chart_data.sort_values(by=[col_name_increments], ascending=False)
        chart_data = chart_data[:self.num_countries]

        chart = alt.Chart(chart_data).mark_bar().encode(
            x=alt.X(self.col_name_countries, sort='-x'),
            y=alt.Y(col_name_increments, axis=alt.Axis(format='N', title='daily increments on {}'.format(day_str)))
        ).interactive()
        return chart
 def _read_today_data(self):
     self.last_read_date = DateHelper.get_current_date()
     self._read_last_date_file()
 def _is_date_change(self):
     now_date = DateHelper.get_current_date()
     if self.last_read_date is not None and self.last_read_date != now_date:
         return True
     return False
    def get_chart_increment_per_day(self, day_str, num_previous_days=30, title=""):
        col_name_date = "date"
        col_name_value = "increments per day"

        # Get the top affected countries
        top_countries_data = self._data.groupby(self.col_name_countries, as_index=False).agg({day_str: "sum"})
        top_countries = top_countries_data.sort_values(
            by=[day_str], ascending=False
        )[:self.num_countries][self.col_name_countries]

        # print(top_countries)
        # Filter only the top countries data
        chart_data = self._data[self._data["Country/Region"].isin(top_countries.values)]
        # print(chart_data)

        data_1 = pandas.DataFrame()
        current_date_str = day_str
        for x in range(num_previous_days):
            previous_day = DateHelper.previous_day_str(current_date_str)

            data_tmp = pandas.DataFrame()
            data_tmp[self.col_name_countries] = chart_data[self.col_name_countries]
            data_tmp[col_name_date] = pandas.to_datetime(current_date_str, format='%m/%d/%y', errors='ignore')
            data_tmp[col_name_value] = chart_data[current_date_str] - chart_data[previous_day]
            data_tmp = data_tmp.groupby([self.col_name_countries, col_name_date], as_index=False).agg(
                {col_name_value: "sum"})

            data_1 = pandas.concat([data_1, data_tmp], ignore_index=True)
            current_date_str = previous_day

        nearest = alt.selection(type='single', nearest=True, on='mouseover',
                                fields=['date'], empty='none')
        line = alt.Chart(data_1).mark_line().encode(
            y=alt.Y("{}:Q".format(col_name_value)),
            x=alt.X('{}:T'.format(col_name_date), axis=alt.Axis(title='Date'.upper(), format="%d/%m/%Y")),
            color=alt.Color('{}'.format(self.col_name_countries), scale=alt.Scale(scheme='category20c'),
                            legend=alt.Legend(orient='right')),
            tooltip='{}'.format(self.col_name_countries),
        )

        selectors = alt.Chart(data_1).mark_point().encode(
            x=alt.X('{}:T'.format(col_name_date), axis=alt.Axis(title='Date'.upper(), format="%d/%m/%Y")),
            opacity=alt.value(0),
        ).add_selection(
            nearest
        )

        # Draw points on the line, and highlight based on selection
        points = line.mark_point().encode(
            opacity=alt.condition(nearest, alt.value(1), alt.value(0))
        )

        # Draw text labels near the points, and highlight based on selection
        text = line.mark_text(align='left', dx=5, dy=-5).encode(
            text=alt.condition(nearest, '{}'.format(self.col_name_countries), alt.value(' '))
        )

        # Draw a rule at the location of the selection
        rules = alt.Chart(data_1).mark_rule(color='gray').encode(
            x=alt.X('{}:T'.format(col_name_date)),
        ).transform_filter(
            nearest
        ).properties(
            title=title
        )

        chart = alt.layer(
            line, selectors, points, rules, text
        ).properties(
            width=800, height=500
        ).interactive()

        return chart