<commit_before>"""This module contains the Model class in Pastas.

"""

from collections import OrderedDict
from itertools import combinations
from logging import getLogger
from os import getlogin

import numpy as np
from pandas import date_range, Series, Timedelta, DataFrame, Timestamp

from .decorators import get_stressmodel
from .io.base import dump, _load_model
from .modelstats import Statistics
from .noisemodels import NoiseModel
from .plots import Plotting
from .solver import LeastSquares
from .stressmodels import Constant
from .timeseries import TimeSeries
from .utils import _get_dt, _get_time_offset, get_sample, \
    frequency_is_supported, validate_name
from .version import __version__


class Model:
    """Class that initiates a Pastas time series model.

    Parameters
    ----------
    oseries: pandas.Series or pastas.TimeSeries
        pandas Series object containing the dependent time series. The
        observation can be non-equidistant.
    constant: bool, optional
        Add a constant to the model (Default=True).
    noisemodel: bool, optional
        Add the default noisemodel to the model. A custom noisemodel can be
        added later in the modelling process as well.
    name: str, optional
        String with the name of the model, used in plotting and saving.
    metadata: dict, optional
        Dictionary containing metadata of the oseries, passed on the to
        oseries when creating a pastas TimeSeries object. hence,
        ml.oseries.metadata will give you the metadata.

    Returns
    -------
    ml: pastas.model.Model
        Pastas Model instance, the base object in Pastas.

    Examples
    --------
    A minimal working example of the Model class is shown below:

    >>> oseries = pd.Series([1,2,1], index=pd.to_datetime(range(3), unit="D"))
    >>> ml = Model(oseries)

    """

    def __init__(self, oseries, constant=True, noisemodel=True, name=None,
                 metadata=None):

        self.logger = getLogger(__name__)

        # Construct the different model components
        self.oseries = TimeSeries(oseries, settings="oseries",
                                  metadata=metadata)

        if name is None and self.oseries.name is not None:
            name = self.oseries.name
        else:
            name = 'Observations'
        self.name = validate_name(name)

        self.parameters = DataFrame(
            columns=["initial", "name", "optimal", "pmin", "pmax", "vary",
                     "stderr"])

        # Define the model components
        self.stressmodels = OrderedDict()
        self.constant = None
        self.transform = None
        self.noisemodel = None

        # Default solve/simulation settings
        self.settings = {
            "tmin": None,
            "tmax": None,
            "freq": "D",
            "warmup": Timedelta(3650, "D"),
            "time_offset": Timedelta(0),
            "noise": noisemodel,
            "solver": None,
            "fit_constant": True,
        }

        if constant:
            constant = Constant(initial=self.oseries.series.mean(),
                                name="constant")
            self.add_constant(constant)<commit_msg>fix: use provided model name if given<commit_after>"""This module contains the Model class in Pastas.

"""

from collections import OrderedDict
from itertools import combinations
from logging import getLogger
from os import getlogin

import numpy as np
from pandas import date_range, Series, Timedelta, DataFrame, Timestamp

from .decorators import get_stressmodel
from .io.base import dump, _load_model
from .modelstats import Statistics
from .noisemodels import NoiseModel
from .plots import Plotting
from .solver import LeastSquares
from .stressmodels import Constant
from .timeseries import TimeSeries
from .utils import _get_dt, _get_time_offset, get_sample, \
    frequency_is_supported, validate_name
from .version import __version__


class Model:
    """Class that initiates a Pastas time series model.

    Parameters
    ----------
    oseries: pandas.Series or pastas.TimeSeries
        pandas Series object containing the dependent time series. The
        observation can be non-equidistant.
    constant: bool, optional
        Add a constant to the model (Default=True).
    noisemodel: bool, optional
        Add the default noisemodel to the model. A custom noisemodel can be
        added later in the modelling process as well.
    name: str, optional
        String with the name of the model, used in plotting and saving.
    metadata: dict, optional
        Dictionary containing metadata of the oseries, passed on the to
        oseries when creating a pastas TimeSeries object. hence,
        ml.oseries.metadata will give you the metadata.

    Returns
    -------
    ml: pastas.model.Model
        Pastas Model instance, the base object in Pastas.

    Examples
    --------
    A minimal working example of the Model class is shown below:

    >>> oseries = pd.Series([1,2,1], index=pd.to_datetime(range(3), unit="D"))
    >>> ml = Model(oseries)

    """

    def __init__(self, oseries, constant=True, noisemodel=True, name=None,
                 metadata=None):

        self.logger = getLogger(__name__)

        # Construct the different model components
        self.oseries = TimeSeries(oseries, settings="oseries",
                                  metadata=metadata)

        if name is None and self.oseries.name is not None:
            name = self.oseries.name
        elif name is None and self.oseries.name is None:
            name = 'Observations'
        self.name = validate_name(name)

        self.parameters = DataFrame(
            columns=["initial", "name", "optimal", "pmin", "pmax", "vary",
                     "stderr"])

        # Define the model components
        self.stressmodels = OrderedDict()
        self.constant = None
        self.transform = None
        self.noisemodel = None

        # Default solve/simulation settings
        self.settings = {
            "tmin": None,
            "tmax": None,
            "freq": "D",
            "warmup": Timedelta(3650, "D"),
            "time_offset": Timedelta(0),
            "noise": noisemodel,
            "solver": None,
            "fit_constant": True,
        }

        if constant:
            constant = Constant(initial=self.oseries.series.mean(),
                                name="constant")
            self.add_constant(constant)
{'fraction_non_alphanumeric': 0.08370725669193872, 'fraction_numerical': 0.00278508432616432, 'mean_word_length': 2.34229576008273, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6387954', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1678, 'n_words': 602}