Пример #1
0
    def resample_batch(self, batch: DataFrame,
                       resample_config: dict) -> DataFrame:
        if not str2bool(get_recursive_config(resample_config, 'enabled')):
            return batch

        target_sampling_rate = get_recursive_config(resample_config,
                                                    'target_sampling_rate')
        method = get_recursive_config(resample_config, 'method')
        resampled_batch = batch.resample(target_sampling_rate)

        if method == 'ffill':
            return resampled_batch.ffill()
        elif method == 'bfill':
            return resampled_batch.bfill()
        elif method == 'mean':
            interpolation_method = get_recursive_config(
                resample_config, 'interpolation_method')

            return resampled_batch.mean().interpolate(
                method=interpolation_method)
        elif method == 'fill_value':
            fill_value = get_recursive_config(resample_config, 'fill_value')

            return resampled_batch.asfreq(fill_value=fill_value)
        elif method == 'interpolate':
            interpolation_method = get_recursive_config(
                resample_config, 'interpolation_method')

            return resampled_batch.interpolate(method=interpolation_method)
        else:
            return resampled_batch
Пример #2
0
 def get_config(self, *args, **kwargs):
     return get_recursive_config(self.config, *args, **kwargs)
Пример #3
0
    def destroy(self) -> None:
        super().destroy()
        register_matplotlib_converters()

        for figure_config in self.get_config('figures'):
            plt.rcParams.update({'font.size': get_recursive_config(figure_config, 'font_size', default=12)})

            fig, ax = plt.subplots()
            x_axis_formatter = get_recursive_config(figure_config, 'x_axis_formatter', default='datetime')
            start_datetime = get_recursive_config(figure_config, 'start_datetime', default=None)
            end_datetime = get_recursive_config(figure_config, 'end_datetime', default=None)

            for plot_config in figure_config['plots']:
                if not plot_config['column'] in self.accumulated_data_frame:
                    continue

                sanitized_column = self.accumulated_data_frame.loc[start_datetime:end_datetime][plot_config['column']].dropna()
                plot_type = get_recursive_config(plot_config, 'type', default='line')

                if 'label' in plot_config:
                    label = plot_config['label'] if plot_config['label'] != 'None' else None
                else:
                    label = plot_config['column']

                if plot_type == 'line':
                    ax.plot(
                        sanitized_column.index if x_axis_formatter == 'datetime' else list(map(lambda x: x.value, sanitized_column.index)),
                        sanitized_column.values,
                        color=self.get_color(get_recursive_config(plot_config, 'color', default=colors['blue'])),
                        label=label,
                        linestyle=get_recursive_config(plot_config, 'linestyle', default='solid'),
                        alpha=get_recursive_config(plot_config, 'alpha', default=1),
                        marker=get_recursive_config(plot_config, 'marker', default=None)
                    )
                elif plot_type == 'histogram':
                    histogram_range = None
                    range_min = get_recursive_config(plot_config, 'range', 'min', default=None)
                    range_max = get_recursive_config(plot_config, 'range', 'max', default=None)

                    if range_min or range_max:
                        histogram_range = [range_min, range_max]

                    ax.hist(
                        sanitized_column.values,
                        color=self.get_color(get_recursive_config(plot_config, 'color', default=colors['blue'])),
                        bins=get_recursive_config(plot_config, 'bins', default=40),
                        label=label,
                        histtype=get_recursive_config(plot_config, 'histtype', default='bar'),
                        alpha=get_recursive_config(plot_config, 'alpha', default=1),
                        range=histogram_range,
                    )

            if 'vline' in figure_config:
                for vline_config in figure_config['vline']:
                    color = self.get_color(get_recursive_config(vline_config, 'color', default=colors['red']))
                    linestyle = get_recursive_config(vline_config, 'linestyle', default='solid')
                    label = get_recursive_config(vline_config, 'label', default=None)

                    plt.axvline(x=vline_config['x'], color=color, linestyle=linestyle, label=label)

            if 'hline' in figure_config:
                for hline_config in figure_config['hline']:
                    color = self.get_color(get_recursive_config(hline_config, 'color', default=colors['red']))
                    linestyle = get_recursive_config(hline_config, 'linestyle', default='solid')
                    label = get_recursive_config(hline_config, 'label', default=None)

                    plt.axvline(y=hline_config['x'], color=color, linestyle=linestyle, label=label)

            if 'xlabel' in figure_config:
                ax.set_xlabel(figure_config['xlabel'], labelpad=10)

            if 'ylabel' in figure_config:
                ax.set_ylabel(figure_config['ylabel'], labelpad=10)

            if 'title' in figure_config:
                ax.set_title(figure_config['title'])

            if 'ylim' in figure_config:
                ax.set_ylim([figure_config['ylim']['min'], figure_config['ylim']['max']])

            if 'xlim' in figure_config:
                ax.set_xlim([figure_config['xlim']['min'], figure_config['xlim']['max']])

            plt.rcParams.update({'font.size': get_recursive_config(figure_config, 'font_size', default=12)})
            plt.tight_layout(0)
            plt.legend(loc=get_recursive_config(figure_config, 'legend_location', default='best'))
            fig.autofmt_xdate()

            if self.save_to_image:
                image_save_path = self.get_save_path_from_figure_config(figure_config, self.format)
                fig.savefig(image_save_path, format=self.format, dpi=self.dpi)

            if self.save_to_pickle:
                pickle_save_path = self.get_save_path_from_figure_config(figure_config, 'pickle')

                with open(str(pickle_save_path), 'wb') as pickle_file:
                    pickle.dump(fig, pickle_file)

        if self.show_plots:
            plt.show()