import echidna.output.store as store
from echidna.fit.minimise import GridSearch
from echidna.errors.custom_errors import CompatibilityError
from echidna.core.config import GlobalFitConfig

import numpy
import logging
import collections
import os
import yaml
import copy


class Fit(object):
    """ Class to handle fitting.

    .. warning:: The :class:`Fit` initialisation will try to set
      atrributes using the values provided. If a value is not provided
      echidna will attempt to set the default. If this is not possible
      a warning will be raised and you will have to set this attribute
      manually before calling :meth:`fit`.

    Args:
      roi (dictionary): Region Of Interest you want to fit in. The format of
        roi is e.g. {"energy": (2.4, 2.6), "radial3": (0., 0.2)}
      test_statistic (:class:`echidna.limit.test_statistic.TestStatistic`): An
        appropriate class for calculating test statistics.
      fit_config (:class:`FitConfig`, optional): Config class for fit -
        usually loaded from file.
      data (:class:`echidna.core.spectra.Spectra`): Data spectrum you want to
        fit.
      fixed_background (dict, optional): Dictionary containing all fixed
        backgrounds, with :class:`echidna.core.spectra.Spectra` as keys
        and priors (float) as values.
      floating_backgrounds (list, optional): one
        :class:`echidna.core.spectra.Spectra` for each background to float.
      signal (:class:`echidna.core.spectra.Spectra`):
        A spectrum of the signal that you are fitting.
      shrink (bool, optional): If set to True (default),
        :meth:`shrink` method is called on all spectra shrinking them to
        the ROI.
      minimiser (:class:`echidna.limit.minimiser.Minimiser`, optional): Object
        to handle the minimisation.
      use_pre_made (bool, optional): Flag whether to load a pre-made spectrum
        for each systematic value, or apply convolutions on the fly.
      pre_made_dir (string, optional): Directory in which pre-made convolved
        spectra are stored.
      single_bin (bool, optional): Flag for a single bin fit (e.g. simple