U
    PhB                     @  s   d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	 dddgZ
ddddddddZdddddZdddddZdS )    )annotationsN)
BundleAlgo)algo_from_picklealgo_to_pickle)AlgoKeysimport_bundle_algo_historyexport_bundle_algo_historyget_name_from_algo_id.Tstrz
str | Noneboollist)output_foldertemplate_pathonly_trainedreturnc                 C  s   g }t t| D ]}tj| |}tj|s2qtj|d}tj|sNqt||d\}}|t	j
d}	|	dkrz| }	W n tk
r   Y nX |	dk	}
|r|
s|s|t	j|t	j|t	j
|	t	j|
i q|S )a  
    import the history of the bundleAlgo objects as a list of algo dicts.
    each algo_dict has keys name (folder name), algo (bundleAlgo), is_trained (bool),

    Args:
        output_folder: the root path of the algorithms templates.
        template_path: the algorithm_template. It must contain algo.py in the follow path:
            ``{algorithm_templates_dir}/{network}/scripts/algo.py``.
        only_trained: only read the algo history if the algo is trained.
    zalgo_object.pklr   N)sortedoslistdirpathjoinisdirisfiler   getr   SCORE	get_scoreBaseExceptionappendIDALGO
IS_TRAINED)r   r   r   historynameZ
write_pathobj_filenamealgoalgo_meta_databest_metric
is_trained r)   O/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/auto3dseg/utils.pyr      s*    zlist[dict[str, BundleAlgo]]None)r"   r   c                 C  s&   | D ]}|t j }t||jd qdS )z
    Save all the BundleAlgo in the history to algo_object.pkl in each individual folder

    Args:
        history: a List of Bundle. Typically, the history can be obtained from BundleGen get_history method
    r   N)r   r    r   r   )r"   	algo_dictr%   r)   r)   r*   r   D   s    
)idr   c                 C  s   |  dd S )z
    Get the name of Algo from the identifier of the Algo.

    Args:
        id: identifier which follows a convention of "name_fold_other".

    Returns:
        name of the Algo.
    _r   )split)r-   r)   r)   r*   r	   P   s    
)r
   NT)
__future__r   r   monai.apps.auto3dseg.bundle_genr   monai.auto3dsegr   r   monai.utils.enumsr   __all__r   r   r	   r)   r)   r)   r*   <module>   s   
     -