o
    zi                  	   @   s   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZm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 G dd deeZG dd deZdejfdee dee de
ee gef defddZdS )z.Abstract base class used to build new loggers.    N)ABC)defaultdict)AnyCallableDictMappingOptionalSequence)override)Logger)_DummyExperiment)rank_zero_experiment)ModelCheckpointc                   @   s8   e Zd ZdZdeddfddZedee fddZ	dS )	r   z"Base class for experiment loggers.checkpoint_callbackreturnNc                 C      dS )zCalled after model checkpoint callback saves a new checkpoint.

        Args:
            checkpoint_callback: the model checkpoint callback instance

        N )selfr   r   r   T/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/loggers/logger.pyafter_save_checkpoint"   s   zLogger.after_save_checkpointc                 C   r   )zvReturn the root directory where experiment logs get saved, or `None` if the logger does not save data
        locally.Nr   r   r   r   r   save_dir+      zLogger.save_dir)
__name__
__module____qualname____doc__r   r   propertyr   strr   r   r   r   r   r      s
    	r   c                       s   e Zd ZdZd fddZedefddZede	d	e	ddfd
dZ
ede	d	e	ddfddZeedefddZeedefddZdedd fddZdedefddZ  ZS )DummyLoggerzDummy logger for internal use.

    It is useful if we want to disable user's logger for a feature, but still ensure that user code can run

    r   Nc                    s   t    t | _d S N)super__init__DummyExperiment_experimentr   	__class__r   r   r"   9   s   
zDummyLogger.__init__c                 C   s   | j S )z9Return the experiment object associated with this logger.)r$   r   r   r   r   
experiment=   s   zDummyLogger.experimentargskwargsc                 O      d S r    r   r   r(   r)   r   r   r   log_metricsB      zDummyLogger.log_metricsc                 O   r*   r    r   r+   r   r   r   log_hyperparamsF   r-   zDummyLogger.log_hyperparamsc                 C   r   )zReturn the experiment name. r   r   r   r   r   nameJ   r   zDummyLogger.namec                 C   r   )zReturn the experiment version.r/   r   r   r   r   r   versionP   r   zDummyLogger.versionidxc                 C   s   | S r    r   )r   r2   r   r   r   __getitem__V   r-   zDummyLogger.__getitem__r0   c                 C   s   dt dt ddfdd}|S )zUAllows the DummyLogger to be called with arbitrary methods, to avoid AttributeErrors.r(   r)   r   Nc                  _   r*   r    r   )r(   r)   r   r   r   method]   s   z'DummyLogger.__getattr__.<locals>.method)r   )r   r0   r4   r   r   r   __getattr__Z   s   zDummyLogger.__getattr__)r   N)r   r   r   r   r"   r   r#   r'   r
   r   r,   r.   r   r0   r1   intr3   r   r5   __classcell__r   r   r%   r   r   2   s"    r   dictsagg_key_funcsdefault_funcr   c                    s   |pi }t ttjdd | D }tt}|D ]- | }dd  fdd| D D }t|d tr=t	|||| < q|p@||| < qt|S )a#  Merge a sequence with dictionaries into one dictionary by aggregating the same keys with some given function.

    Args:
        dicts:
            Sequence of dictionaries to be merged.
        agg_key_funcs:
            Mapping from key name to function. This function will aggregate a
            list of values, obtained from the same key of all dictionaries.
            If some key has no specified aggregation function, the default one
            will be used. Default is: ``None`` (all keys will be aggregated by the
            default function).
        default_func:
            Default function to aggregate keys, which are not presented in the
            `agg_key_funcs` map.

    Returns:
        Dictionary with merged values.

    Examples:
        >>> import pprint
        >>> d1 = {'a': 1.7, 'b': 2.0, 'c': 1, 'd': {'d1': 1, 'd3': 3}}
        >>> d2 = {'a': 1.1, 'b': 2.2, 'v': 1, 'd': {'d1': 2, 'd2': 3}}
        >>> d3 = {'a': 1.1, 'v': 2.3, 'd': {'d3': 3, 'd4': {'d5': 1}}}
        >>> dflt_func = min
        >>> agg_funcs = {'a': statistics.mean, 'v': max, 'd': {'d1': sum}}
        >>> pprint.pprint(merge_dicts([d1, d2, d3], agg_funcs, dflt_func))
        {'a': 1.3,
         'b': 2.0,
         'c': 1,
         'd': {'d1': 3, 'd2': 3, 'd3': 3, 'd4': {'d5': 1}},
         'v': 2.3}

    c                 S   s   g | ]}t | qS r   )setkeys).0dr   r   r   
<listcomp>       zmerge_dicts.<locals>.<listcomp>c                 S   s   g | ]}|d ur|qS r    r   )r=   vr   r   r   r?      r@   c                    s   g | ]}|  qS r   )get)r=   d_inkr   r   r?      s    r   )
list	functoolsreduceoperatoror_r   dictrB   
isinstancemerge_dicts)r8   r9   r:   r<   d_outfnvalues_to_aggr   rD   r   rM   d   s   &
rM   )r   rG   rI   
statisticsabcr   collectionsr   typingr   r   r   r   r   r	   typing_extensionsr
   lightning_fabric.loggersr   FabricLoggerlightning_fabric.loggers.loggerr   r#   r   ,pytorch_lightning.callbacks.model_checkpointr   r   meanfloatrM   r   r   r   r   <module>   s2    4