# AUTOGENERATED! DO NOT EDIT! File to edit: notebooks/data.preprocessing.ipynb (unless otherwise specified).

__all__ = ['log', 'make_chart_df', 'preprocess_data', 'make_diffs', 'do_fill_none']

# Cell

import logging

import pandas as pd
from .utils import get_chart_map

log = logging.getLogger(__name__)

# Cell


def make_chart_df(dict_baseline, dict_highlight, chart):
    if chart in dict_baseline:
        df_chart_baseline = pd.DataFrame().from_dict(dict_baseline[chart])
    else:
        df_chart_baseline = pd.DataFrame()
    #df_chart_baseline['window'] = 'baseline'
    if chart in dict_highlight:
        df_chart_highlight = pd.DataFrame().from_dict(dict_highlight[chart])
    else:
        df_chart_highlight = pd.DataFrame()
    #df_chart_highlight['window'] = 'highlight'
    df_chart = pd.concat([df_chart_baseline,df_chart_highlight])
    return df_chart



# Cell


def preprocess_data(dict_baseline, dict_highlight):
    chart_map = get_chart_map(dict_baseline, dict_highlight)
    chart_map_filtered = {}
    chart_map_removed = {}
    for chart in chart_map:
        df = make_chart_df(dict_baseline, dict_highlight, chart)
        dims_filtered = []
        dims_removed = []
        for dim in df.columns:
            if len(df[dim].unique()) > 1:
                dims_filtered.append(dim)
            else:
                dims_removed.append(dim)
        chart_map_filtered[chart] = dims_filtered
        chart_map_removed[chart] = dims_removed
    return chart_map_filtered, chart_map_removed



# Cell


def make_diffs(dict_data):
    dict_diffs = {}
    for c in dict_data:
        dict_dims = {}
        for d in dict_data[c]:
            data = dict_data[c][d]
            data_diff = [x1 - x0 for x0, x1 in zip(data, data[1:]) if x0 is not None if x1 is not None]
            dict_dims[d] = data_diff
        dict_diffs[c] = dict_dims
    return dict_diffs



# Cell


def do_fill_none(dict_data, fill_with=0):
    dict_out = {}
    for c in dict_data:
        dict_dims = {}
        for d in dict_data[c]:
            data = [fill_with if x==None else x for x in dict_data[c][d]]
            dict_dims[d] = data
        dict_out[c] = dict_dims
    return dict_out

