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

__all__ = ['log', 'models_supported', 'models_chart_enabled', 'models_chart_only', 'models_no_lags', 'validate_inputs',
           'validate_dicts', 'run_model_dict']

# Cell

import logging
import time

from .ks import do_ks_dict
from ..data.preprocessing import preprocess_data, make_diffs, do_fill_none
from ..data.utils import get_chart_map

log = logging.getLogger(__name__)

models_supported = ['ks']
models_chart_enabled = []
models_chart_only = []
models_no_lags = ['ks']

# Cell


def validate_inputs(model, model_level, n_lags):
    if model not in models_chart_enabled and model_level == 'chart':
        model_level = 'dim'
    if model in models_no_lags:
        n_lags = 0
    if model in models_chart_only:
        model_level = 'chart'
    return model, model_level, n_lags



# Cell


def validate_dicts(dict_baseline, dict_highlight):
    if len(dict_baseline) == 0:
        raise ValueError(f'looks like dict_baseline is empty')
    if len(dict_highlight) == 0:
        raise ValueError(f'looks like dict_highlight is empty')



# Cell


def run_model_dict(model, dict_baseline, dict_highlight, n_lags=0, model_errors='ignore', model_level='dim', return_times=False, return_meta=False, preprocess=True, validate=True, diff=True, fill_none=True):

    if return_times:
        time_start = time.time()
    model, model_level, n_lags = validate_inputs(model, model_level, n_lags)

    chart_map_raw = get_chart_map(dict_baseline, dict_highlight)
    log.debug(f'chart_map_raw={chart_map_raw}')

    if validate:
        validate_dicts(dict_baseline, dict_highlight)

    if fill_none:
        dict_baseline = do_fill_none(dict_baseline)
        dict_highlight = do_fill_none(dict_highlight)

    if diff:
        dict_baseline = make_diffs(dict_baseline)
        dict_highlight = make_diffs(dict_highlight)

    if preprocess:
        chart_map_final, chart_map_removed = preprocess_data(dict_baseline, dict_highlight)
        n_charts_removed = len([c for c in chart_map_final if chart_map_final[c]==[]])
        n_dims_removed = len([f'{c}|{d}' for c in chart_map_removed for d in chart_map_removed[c]])
    else:
        chart_map_final = chart_map_raw
        n_charts_removed = 0
        n_dims_removed = 0

    n_charts_raw = len(chart_map_raw)
    n_dims_raw = len([f'{c}|{d}' for c in chart_map_raw for d in chart_map_raw[c]])
    n_charts = len(chart_map_final)
    n_dims = len([f'{c}|{d}' for c in chart_map_final for d in chart_map_final[c]])

    log.debug(f'n_charts_raw={n_charts_raw}')
    log.debug(f'n_dims_raw={n_dims_raw}')
    log.debug(f'n_charts={n_charts}')
    log.debug(f'n_dims={n_dims}')
    log.debug(f'n_charts_removed={n_charts_removed}')
    log.debug(f'n_dims_removed={n_dims_removed}')

    if model == 'ks':
        results = do_ks_dict(dict_baseline, dict_highlight, chart_map_final)
    else:
        raise ValueError(f"unknown model '{model}'")

    if return_times:
        results['times'] = {'secs_total': round(time.time() - time_start, 2)}

    if return_meta:
        results['meta'] = {'model': model, 'model_level': model_level, 'n_lags': n_lags}

    log.debug(f"results_summary={results['summary']}")

    results['summary']['n_charts_raw'] = n_charts_raw
    results['summary']['n_dims_raw'] = n_dims_raw
    results['summary']['n_charts'] = n_charts
    results['summary']['n_dims'] = n_dims
    results['summary']['n_charts_removed'] = n_charts_removed
    results['summary']['n_dims_removed'] = n_dims_removed
    if n_dims > 0 and results['summary']['fit_success'] > 0 :
        results['summary']['success_rate'] = round(results['summary']['fit_success'] / n_dims, 2)
    else:
        results['summary']['success_rate'] = 0.00
    results['summary']['removal_rate'] = round(n_dims_removed / n_dims_raw, 2)

    return results

