a
    ߙfb2                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlZd dlm	Z
 d dlmZ d dlmZ d dlmZ ddlmZmZmZmZ ddlmZ g d	Zi Ze ZG d
d deZG dd de jdZG dd de jdZ dd Z!dS )    N)FunctionTypeMappingProxyType)UnifiedReadWriteMethod)classproperty)MetaData   )CosmologyFromFormatCosmologyReadCosmologyToFormatCosmologyWrite)	Parameter)	CosmologyCosmologyErrorFlatCosmologyMixinc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   5lib/python3.9/site-packages/astropy/cosmology/core.pyr   !   s   r   c                       s   e Zd ZdZe ZeeZee	Z
eeZeeZdZdZ fddZedddd Z fd	d
ZdddZedd ZddddZdd Zdd Zdd Zdd Zdd Z  ZS )r   a  Base-class for all Cosmologies.

    Parameters
    ----------
    *args
        Arguments into the cosmology; used by subclasses, not this base class.
    name : str or None (optional, keyword-only)
        The name of the cosmology.
    meta : dict or None (optional, keyword-only)
        Metadata for the cosmology, e.g., a reference.
    **kwargs
        Arguments into the cosmology; used by subclasses, not this base class.

    Notes
    -----
    Class instances are static -- you cannot (and should not) change the values
    of the parameters.  That is, all of the above attributes (except meta) are
    read only.

    For details on how to create performant custom subclasses, see the
    documentation on :ref:`astropy-cosmology-fast-integrals`.
    r   c                    sX  t    t| jj| jj| jj| jj| jjd}t	
|| j}| jj|_| j}|jtddgt|j  d}||_|| _g  g }| jD ]2}t| |}t|tr|jr||n | q| j D ]B\}}| v s|dst|tsq|jr||n | qʇ fdd| jj D }|   t | _| jt| | _| t | j!< d S )N)nameZargdefsZclosureclsr   
parameters_c                    s$   g | ]}| v r   |qS r   )popindex).0nr   r   r   
<listcomp>o   s   z/Cosmology.__init_subclass__.<locals>.<listcomp>)"super__init_subclass__r   __new____code____globals__r   __defaults____closure__	functoolsupdate_wrapper__init____kwdefaults___init_signaturereplaceinspectr   listr   valuesZ__signature____parameters__getattr
isinstanceZderivedappend__dict__items
startswithkeystuple__all_parameters___COSMOLOGY_CLASSESr   )r   newsigZderived_parametersr   pvZordered	__class__r   r   r    M   s8    

$





zCosmology.__init_subclass__T)Zlazyc                 C   s.   t | j}|jt|j dd d}|S )z*Initialization signature (without 'self').r   Nr   )r,   Z	signaturer(   r+   r-   r   r.   )r   r;   r   r   r   r*   z   s    zCosmology._init_signaturec                    s2   t  | }| jj|i |}|  |j|_|S N)r   r!   r*   bind_partialZapply_defaultsZ	arguments_init_arguments)r   argskwargsselfbar>   r   r   r!      s
    zCosmology.__new__Nc                 C   s   || _ | j|pi  d S r@   )_namemetaupdate)rE   r   rH   r   r   r   r(      s    zCosmology.__init__c                 C   s   | j S )z#The name of the Cosmology instance.)rG   rE   r   r   r   r      s    zCosmology.name)rH   c                K   s~   |du r|s| S | d| jdur*| jd nd i | j|p>i }i | jd|i|}| jjf i |}| j|ji |jS )a:  Returns a copy of this object with updated parameters, as specified.

        This cannot be used to change the type of the cosmology, so ``clone()``
        cannot be used to change between flat and non-flat cosmologies.

        Parameters
        ----------
        meta : mapping or None (optional, keyword-only)
            Metadata that will update the current metadata.
        **kwargs
            Cosmology parameter (and name) modifications.
            If any parameter is changed and a new name is not given, the name
            will be set to "[old name] (modified)".

        Returns
        -------
        newcosmo : `~astropy.cosmology.Cosmology` subclass instance
            A new instance of this class with updated parameters as specified.
            If no modifications are requested, then a reference to this object
            is returned instead of copy.

        Examples
        --------
        To make a copy of the ``Planck13`` cosmology with a different matter
        density (``Om0``), and a new name:

            >>> from astropy.cosmology import Planck13
            >>> newcosmo = Planck13.clone(name="Modified Planck 2013", Om0=0.35)

        If no name is specified, the new name will note the modification.

            >>> Planck13.clone(Om0=0.35).name
            'Planck13 (modified)'
        Nr   z (modified)rH   )	
setdefaultr   rH   rB   r*   rA   r?   rC   rD   )rE   rH   rD   Znew_metaZnew_initrF   r   r   r   clone   s    $zCosmology.clonec                 C   s6   |  |}|tu r&t|dr&| | }|tur2|S dS )a  Check equivalence between Cosmologies.

        Two cosmologies may be equivalent even if not the same class.
        For example, an instance of ``LambdaCDM`` might have :math:`\Omega_0=1`
        and :math:`\Omega_k=0` and therefore be flat, like ``FlatLambdaCDM``.

        Parameters
        ----------
        other : `~astropy.cosmology.Cosmology` subclass instance
            The object in which to compare.

        Returns
        -------
        bool
            True if cosmologies are equivalent, False otherwise.
        	__equiv__F)rM   NotImplementedhasattr)rE   otherZequivr   r   r   is_equivalent   s    

zCosmology.is_equivalentc                    sB    j j urtS tjt jko<t fddjD }|S )a  Cosmology equivalence. Use ``.is_equivalent()`` for actual check!

        Parameters
        ----------
        other : `~astropy.cosmology.Cosmology` subclass instance
            The object in which to compare.

        Returns
        -------
        bool or `NotImplemented`
            `NotImplemented` if 'other' is from a different class.
            `True` if 'other' is of the same class and has matching parameters
            and parameter values. `False` otherwise.
        c                 3   s(   | ] }t t|t |kV  qd S r@   )npallr0   r   krP   rE   r   r   	<genexpr>   s   z&Cosmology.__equiv__.<locals>.<genexpr>)r?   rN   setr8   rS   )rE   rP   Z	params_eqr   rV   r   rM      s    zCosmology.__equiv__c                 C   s.   |j | j urtS | |}| j|jk}|o,|S )a}  Check equality between Cosmologies.

        Checks the Parameters and immutable fields (i.e. not "meta").

        Parameters
        ----------
        other : `~astropy.cosmology.Cosmology` subclass instance
            The object in which to compare.

        Returns
        -------
        bool
            True if Parameters and names are the same, False otherwise.
        )r?   rN   rM   r   )rE   rP   Z
equivalentZname_eqr   r   r   __eq__  s
    
zCosmology.__eq__c                    sz   fddj D }fddj D  jj d}jd urR|dj d7 } fdd| D }|d	| d
 S )Nc                    s   i | ]}|t  |qS r   )r0   rT   rJ   r   r   
<dictcomp>      z&Cosmology.__repr__.<locals>.<dictcomp>c                    s   i | ]}|t  j|qS r   )r0   r?   rT   rJ   r   r   rZ      r[   (zname="z", c                 3   s6   | ].\}}|d  t ||dur& | jnd V  qdS )=N )formatformat_spec)r   rU   r=   )cpsr   r   rW   &  s   z%Cosmology.__repr__.<locals>.<genexpr>z, ))r/   r?   r   r   r4   join)rE   ZpsZnameleadZfmtpsr   )ra   rE   r   __repr__  s    

zCosmology.__repr__c                 K   s   | j dd|i|S )a  Return a `~astropy.table.Table` of type ``cls``.

        Parameters
        ----------
        cls : type
            Astropy ``Table`` class or subclass.
        copy : bool
            Ignored.
        **kwargs : dict, optional
            Additional keyword arguments. Passed to ``self.to_format()``.
            See ``Cosmology.to_format.help("astropy.table")`` for allowed kwargs.

        Returns
        -------
        `astropy.table.Table` or subclass instance
            Instance of type ``cls``.
        astropy.tabler   )re   )	to_format)rE   r   copyrD   r   r   r   __astropy_table__+  s    zCosmology.__astropy_table__)NN)r   r   r   __doc__r   rH   r   r   Zfrom_formatr
   rf   r	   readr   writer/   r8   r    r   r*   r!   r(   propertyr   rL   rQ   rM   rY   rd   rh   __classcell__r   r   r>   r   r   %   s*   -
	


9r   )	metaclassc                   @   s   e Zd ZdZdS )r   aZ  
    Mixin class for flat cosmologies. Do NOT instantiate directly.
    Note that all instances of ``FlatCosmologyMixin`` are flat, but not all
    flat cosmologies are instances of ``FlatCosmologyMixin``. As example,
    ``LambdaCDM`` **may** be flat (for the a specific set of parameter values),
    but ``FlatLambdaCDM`` **will** be flat.
    N)r   r   r   ri   r   r   r   r   r   @  s   r   c                 C   sh   ddl m} t|| rNdd l}ddlm} |d|  d|  d| t|| S tdt	d	| d
d S )Nr   )flrwr   )AstropyDeprecationWarningz`astropy.cosmology.core.zX` has been moved (since v5.0) and should be imported as ``from astropy.cosmology import z+``. In future this will raise an exception.zmodule z has no attribute .)
r^   ro   rO   warningsZastropy.utils.exceptionsrp   warnr0   AttributeErrorr   )attrro   rr   rp   r   r   r   __getattr__N  s    

rv   )"abcr&   r,   typesr   r   ZnumpyrR   Zastropy.unitsZunitsuZastropy.io.registryr   Zastropy.utils.decoratorsr   Zastropy.utils.metadatar   Zconnectr   r	   r
   r   Z	parameterr   __all__Z__doctest_requires__dictr9   	Exceptionr   ABCMetar   r   rv   r   r   r   r   <module>   s&   	  