o
    zi
                     @   sZ   d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	Z	G dd dZ
dS )zS
Useful class for Experiment tracking, and ensuring code is
saved alongside files.
    N)Pathc                   @   s\   e Zd ZdZ		ddedefddZdd	 Zd
d Zedd Z	dd fde
jfddZdS )
ExperimentaZ  This class contains utilities for managing experiments.
    It is a context manager, that when you enter it, changes
    your directory to a specified experiment folder (which
    optionally can have an automatically generated experiment
    name, or a specified one), and changes the CUDA device used
    to the specified device (or devices).

    Parameters
    ----------
    exp_directory : str
        Folder where all experiments are saved, by default "runs/".
    exp_name : str, optional
        Name of the experiment, by default uses the current time, date, and
        hostname to save.
    runs/Nexp_directoryexp_namec                 C   sd   |d u r|   }t|| }|jddd || _|| _ttd	d
 | _td | _d S )NTparentsexist_okz+git ls-tree --full-tree --name-only -r HEADzutf-8.)generate_exp_namer   mkdirexp_dirr   
subprocesscheck_outputshlexsplitdecode
splitlinesgit_tracked_filesabsoluteparent_directory)selfr   r   r    r   L/home/ubuntu/.local/lib/python3.10/site-packages/audiotools/ml/experiment.py__init__!   s   zExperiment.__init__c                 C   s   t  | _t | j | S N)osgetcwdprev_dirchdirr   )r   r   r   r   	__enter__6   s   
zExperiment.__enter__c                 C   s   t | j d S r   )r   r   r   )r   exc_type	exc_value	tracebackr   r   r   __exit__;   s   zExperiment.__exit__c                  C   s&   t j  d} |  dt  }|S )zGenerates a random experiment name based on the date
        and a randomly generated adjective-noun tuple.

        Returns
        -------
        str
            Randomly generated experiment name.
        z%y%m%d-)datetimenowstrftime
randomnameget_name)datenamer   r   r   r   >   s   
zExperiment.generate_exp_namec                 C   s   dS )NTr   )fr   r   r   <lambda>L   s    zExperiment.<lambda>	filter_fnc                 C   s>   | j D ]}||rt|jjddd t| j| | qdS )a  Captures a full snapshot of all the files tracked by git at the time
        the experiment is run. It also captures the diff against the committed
        code as a separate file.

        Parameters
        ----------
        filter_fn : typing.Callable, optional
            Function that can be used to exclude some files
            from the snapshot, by default accepts all files
        Tr   N)r   r   parentr   shutilcopyfiler   )r   r/   r-   r   r   r   snapshotL   s   
zExperiment.snapshot)r   N)__name__
__module____qualname____doc__strr   r    r$   staticmethodr   typingCallabler3   r   r   r   r   r      s    

r   )r7   r&   r   r   r1   r   r:   pathlibr   r)   r   r   r   r   r   <module>   s    