U
    md                  
   @   sb   d Z ddlmZ ddlZddlmZ ddlm  m	Z	 ej
dddddddddf
ddZd	d
 ZdS )z@
Authors:    Josef Perktold, Skipper Seabold, Denis A. Engemann
    )lrangeN)rainbowbbestc                 K   s  ddl m} t|\}}|p(t|dd}d|j|f }|pFt| dd}|pVt|dd}|| || d }}t| d t	rd	d
 t
| D }tt|}t| tt||} |t| ||d}|ddg| }t|d  }|
dkrdg| n|
}
|	dkrdg| n|	}	|dkr2t|n|}t|
|krLtdt|	|krbtdt||krxtd|dks|dkrt|dD ]R\}\}}t	|d jd }|j|d |d f|| |	| ||
| d| qn|dks|dkrbt|dD ]L\}\}}t	|d jd }|j|d |d f|| ||
| d| qn~|dksv|dkrt|dD ]L\}\}}t	|d jd }|j|d |d f|| ||	| d| qntd| |j||d |d t||gr|| || |S )a  
    Interaction plot for factor level statistics.

    Note. If categorial factors are supplied levels will be internally
    recoded to integers. This ensures matplotlib compatibility. Uses
    a DataFrame to calculate an `aggregate` statistic for each level of the
    factor or group given by `trace`.

    Parameters
    ----------
    x : array_like
        The `x` factor levels constitute the x-axis. If a `pandas.Series` is
        given its name will be used in `xlabel` if `xlabel` is None.
    trace : array_like
        The `trace` factor levels will be drawn as lines in the plot.
        If `trace` is a `pandas.Series` its name will be used as the
        `legendtitle` if `legendtitle` is None.
    response : array_like
        The reponse or dependent variable. If a `pandas.Series` is given
        its name will be used in `ylabel` if `ylabel` is None.
    func : function
        Anything accepted by `pandas.DataFrame.aggregate`. This is applied to
        the response variable grouped by the trace levels.
    ax : axes, optional
        Matplotlib axes instance
    plottype : str {'line', 'scatter', 'both'}, optional
        The type of plot to return. Can be 'l', 's', or 'b'
    xlabel : str, optional
        Label to use for `x`. Default is 'X'. If `x` is a `pandas.Series` it
        will use the series names.
    ylabel : str, optional
        Label to use for `response`. Default is 'func of response'. If
        `response` is a `pandas.Series` it will use the series names.
    colors : list, optional
        If given, must have length == number of levels in trace.
    markers : list, optional
        If given, must have length == number of levels in trace
    linestyles : list, optional
        If given, must have length == number of levels in trace.
    legendloc : {None, str, int}
        Location passed to the legend command.
    legendtitle : {None, str}
        Title of the legend.
    **kwargs
        These will be passed to the plot command used either plot or scatter.
        If you want to control the overall plotting options, use kwargs.

    Returns
    -------
    Figure
        The figure given by `ax.figure` or a new instance.

    Examples
    --------
    >>> import numpy as np
    >>> np.random.seed(12345)
    >>> weight = np.random.randint(1,4,size=60)
    >>> duration = np.random.randint(1,3,size=60)
    >>> days = np.log(np.random.randint(1,30, size=60))
    >>> fig = interaction_plot(weight, duration, days,
    ...             colors=['red','blue'], markers=['D','^'], ms=10)
    >>> import matplotlib.pyplot as plt
    >>> plt.show()

    .. plot::

       import numpy as np
       from statsmodels.graphics.factorplots import interaction_plot
       np.random.seed(12345)
       weight = np.random.randint(1,4,size=60)
       duration = np.random.randint(1,3,size=60)
       days = np.log(np.random.randint(1,30, size=60))
       fig = interaction_plot(weight, duration, days,
                   colors=['red','blue'], markers=['D','^'], ms=10)
       import matplotlib.pyplot as plt
       #plt.show()
    r   )	DataFramenameresponsez%s of %sXTraceNc                 S   s   g | ]}|qS  r   ).0lr   r   Y/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/graphics/factorplots.py
<listcomp>l   s     z$interaction_plot.<locals>.<listcomp>)xtracer   r   r   -.z(Must be a linestyle for each trace levelz%Must be a marker for each trace levelz$Must be a color for each trace levelZbothr   )colormarkerlabel	linestyleliner   )r   r   r   scatters)r   r   r   zPlot type %s not understood)loctitleg?)pandasr   utilsZcreate_mpl_axgetattr__name__Z
set_ylabelZ
set_xlabel
isinstancestrnpuniquer   len_recodedictzipgroupbyZ	aggregateZreset_indexr   
ValueError	enumeratevaluesZplotr   ZlegendZmarginsallZ
set_xticksZset_xticklabels)r   r   r   funcZaxZplottypeZxlabelZylabelcolorsmarkersZ
linestylesZ	legendlocZlegendtitlekwargsr   ZfigZresponse_nameZx_valuesZx_levelsdataZ	plot_dataZn_traceir,   groupr   r   r   r   interaction_plot   sx    R

   


r5   c                 C   s   ddl m} d}d}t| |r0| j}| j}| j} | jjtj	tj
fkrNtdnt|tsbtdnrt| tt| k stdnHtj| jd td}| D ]\}}||| |k< q|r||||d}|S dS )	a8   Recode categorial data to int factor.

    Parameters
    ----------
    x : array_like
        array like object supporting with numpy array methods of categorially
        coded data.
    levels : dict
        mapping of labels to integer-codings

    Returns
    -------
    out : instance numpy.ndarray
    r   )SeriesNz<This is not a categorial factor. Array of str type required.z4This is not a valid value for levels. Dict required.z)The levels do not match the array values.)dtype)r   index)r   r6   r!   r   r8   r,   r7   typer#   Zstr_Zobject_r*   r'   r$   listkeysr-   emptyshapeintitems)r   Zlevelsr6   r   r8   outlevelZcodingr   r   r   r&      s&    



 
r&   )__doc__Zstatsmodels.compat.pythonr   numpyr#   Zstatsmodels.graphics.plottoolsr   Zstatsmodels.graphics.utilsZgraphicsr   Zmeanr5   r&   r   r   r   r   <module>   s        
 