B
    Z                 @   sH  d Z ddlmZ ddlmZ ddlZG dd deZe	dkrDe
dZed	ZxeD ]Zeeeee  qVW ejd
dd ejdddd ddlZxeD ]Zee eee eejekZesee jedksejee eejedddZesed esdd eee  D Zeseed qW ee  dS )zaSave a set of numpy arrays to a python module file that can be imported

Author : Josef Perktold
    )print_function)iterkeysNc               @   s*   e Zd ZdZdd Zd
ddZdd	 ZdS )HoldItzClass to write numpy arrays into a python module

    Calling save on the instance of this class write all attributes of the
    instance into a module file. For details see the save method.

    c             C   s
   || _ d S )N)name)selfr    r   <lib/python3.7/site-packages/statsmodels/tools/dump2module.py__init__   s    zHoldIt.__init__NTc          
   C   s   t  }tddddd}|r&|| t jf | |dkrJdd | jD }|rdd	g}	|rh|	d
 ng }	|r|	d| j  d| j }
nd}
|dk	r|	d|
|f  x*|D ]"}|	d|
|tt	| |f  qW |	
ddg |dk	rt|dd|	 t jf | || _|| _|| _|	S )a  write attributes of this instance to python module given by filename

        Parameters
        ----------
        what : list or None
            list of attributes that are added to the module. If None (default)
            then all attributes in __dict__ that do not start with an underline
            will be saved.
        filename : string
            specifies filename with path. If the file does not exist, it will be
            created. If the file is already exists, then the new data will be
            appended to the file.
        header : bool
            If true, then the imports of the module and the class definition are
            written before writing the data.
        useinstance : bool
            If true, then the data in the module are attached to an instance of a
            holder class. If false, then each array will be saved as separate
            variable.
        comment : string
            If comment is not empty then this string will be attached as a
            description comment to the data instance in the saved module.
        print_options : dict or None
            The print_options for the numpy arrays will be updated with this.
            see notes

        Notes
        -----
        The content of an numpy array are written using repr, which can be
        controlled with the np.set_printoptions. The numpy default is updated
        with:  precision=20, linewidth=100, nanstr='nan', infstr='inf'

        This should provide enough precision for double floating point numbers.
        If one array has more than 1000 elements, then threshold should be
        overwritten by the user, see keyword argument print_options.
           d   naninf)	precision	linewidthZnanstrZinfstrNc             s   s   | ]}|d  dkr|V  qdS )r   _Nr   ).0ir   r   r   	<genexpr>B   s    zHoldIt.save.<locals>.<genexpr>z;import numpy as np
from numpy import array, rec, inf, nan

z class Holder(object):
    pass

z%s = Holder()z%s. z%scomment = '%s'z	%s%s = %sza+
)npZget_printoptionsdictupdateset_printoptions__dict__appendr   reprgetattrextendfilewritejoin	_filename_useinstance_what)r   ZwhatfilenameheaderZuseinstancecommentZprint_optionsZprint_opt_oldZ	print_optZtxtprefixxr   r   r   save   s:    '

"zHoldIt.savec       	         s   t | jdd}| js tdt|| jg }g }x| jD ] t }t }t	| j
|k}|s| j
jtdkstj||ddd}|sddl}|d	  t |sڇ fd
dt  
 D }|s|  |  q<W t|dk||fS )a  load the saved module and verify the data

        This tries several ways of comparing the saved and the attached data,
        but might not work for all possible data structures.

        Returns
        -------
        all_correct : bool
            true if no differences are found, for floating point numbers
            rtol=1e-16, atol=1e-16 is used to determine equality (allclose)
        correctli : list
            list of attribute names that compare as equal
        incorrectli : list
            list of attribute names that did not compare as equal, either
            because they differ or because the comparison does not handle the
            data structure correctly

        z.pyr   z2currently only implemented whenuseinstance is trueobjectgؗҜ<)rtolatolr   Nzinexact precision in c                s6   g | ].}t    | ttj  | kqS r   )r   allitemr   testsavevar_results)r   k)ddatar   r   
<listcomp>   s   z!HoldIt.verify.<locals>.<listcomp>)
__import__r"   replacer#   NotImplementedErrorr   r   r$   r   r.   r/   dtypeallclosewarningswarnRuntimeWarningr   r   len)	r   moduleZ	correctliZincorrectliZ	self_itemZ
saved_itemcorrectr;   correlemr   )r3   r4   r   verify_   s.    



zHoldIt.verify)NNTTNN)__name__
__module____qualname____doc__r	   r*   rB   r   r   r   r   r   	   s
    
Kr   __main__zE:\Josef\eclipsegworkspace\statsmodels-josef-experimental-030\dist\statsmodels-0.3.0dev_with_Winhelp_a2\statsmodels-0.3.0dev\scikits\statsmodels\tsa\vector_ar\tests\results\vars_results.npzr1   x   r   )r   r   ztestsave.pyTz-VAR test data converted from vars_results.npz)r%   r&   r'   r+   gؗҜ<)r,   r-   zinexact precisionc             C   s6   g | ].}t tt  | ttjt | kqS r   )r   r.   r4   r3   r/   r   r0   r1   )r   r2   r   r   r   r5      s   r5   Zwrong)rF   Z
__future__r   Zstatsmodels.compat.pythonr   Znumpyr   r+   r   rC   loadr4   Zres_varr3   setattrr   r*   r0   printr.   r   r1   r@   r9   r:   r/   rA   rB   r   r   r   r   <module>   s8    




 