o
    $i|'                     @  s  d dl m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m	Z	 d dl
mZ ed\ZZg dZdZd	Zd
ZdZdZe	 Zdddied eed dd  dd  ed i ddddi i ddZdgddddddd d!d"d#d$d%d&d$d%d'd$d%d(d$d)d*gd+d,d-d.d/d0d1d2d3d d4d5d6id"d7d8d9d:d;d8d9d<gd+d=d>d?d@dAidBgdCd)dDdAdEdFdGdHdIgdJZdKdLdMdNdOdMdPdQdMdRZedSdTdUdd9dVdWdXdYdZd[d\d9d9d]d^d9d_dWdXdYdZd[d3d`dWdXdYdZd[d\d3d9dadb	dcZddeiZdedfiZdvdmdnZdwdtduZdS )x    )annotationsN)Any)get_config_values)optional_importyaml)
ID_REF_KEY
ID_SEP_KEYEXPR_KEY	MACRO_KEY	MERGE_KEYDEFAULT_MLFLOW_SETTINGSDEFAULT_EXP_MGMT_SETTINGS@z::$%+z0.0.1zInitial versionMONAIPytorchaNumpyz"Describe what the network predictszQA longer description of what the network does, use context, inputs, outputs, etc.zYour Name HerezCopyright (c) Your Name Here)inputsoutputs)version	changelogZmonai_versionZpytorch_versionnumpy_versionrequired_packages_versiontaskdescriptionauthors	copyrightZnetwork_data_formatz$import globz?$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')z"$@bundle_root + '/models/model.pt'z/workspace/dataz2$list(sorted(glob.glob(@dataset_dir + '/*.jpeg')))z???   )_target_spatial_dimsz$@network_def.to(@device)Compose
LoadImagedimage)r!   keysEnsureChannelFirstdScaleIntensitydEnsureTypedz@device)r!   r&   device)r!   
transformsDatasetz"$[{'image': i} for i in @datalist]z@preprocessing)r!   data	transform
DataLoaderz@dataset   F)r!   dataset
batch_sizeshufflenum_workersr!   ZSimpleInfererActivationsdpredT)r!   r&   softmaxAsDiscreted)r!   r&   argmaxZCheckpointLoaderz$not os.path.exists(@ckpt_path)z
@ckpt_pathmodelz@network)r!   
_disabled_	load_path	load_dictZSupervisedEvaluatorz@dataloaderz@infererz@postprocessingz	@handlers)r!   r*   Zval_data_loadernetworkinfererpostprocessingZval_handlersz$@evaluator.run())Zimportsr*   	ckpt_pathdataset_dirdatalistZnetwork_defr>   preprocessingr1   
dataloaderr?   r@   handlers	evaluatorZ
evaluatingztrain#trainerztrain#handlers)idrF   zvalidate#evaluatorzvalidate#handlersrG   rF   )trainer	validatorrG   z$@bundle_root + '/eval'z1$monai.utils.path_to_uri(@output_dir) + '/mlruns'Zmonai_experimentz}$torch.distributed.is_available()                 and torch.distributed.is_initialized() and torch.distributed.get_rank() > 0ZMLFlowHandlerz@is_not_rank0z@tracking_uriz@experiment_namez	@run_namez@save_execute_config
train_lossz1$monai.handlers.from_engine(['loss'], first=True))r!   r;   tracking_uriexperiment_namerun_name	artifactsiteration_logZ	epoch_logtag_nameZoutput_transformclose_on_complete)r!   r;   rL   rM   rN   rP   )r!   r;   rL   rM   rN   rO   rP   rR   )	
output_dirrL   rM   rN   Zsave_execute_configZis_not_rank0rI   rJ   rG   )Zhandlers_idconfigsmlflowZoptional_packages_versionr   bundle_pathstrconfig_namesload_kw_argsr   returnc                 O  s  ddl m} | }tj| std|  dtj| r[g }|jddtj| ddi| |D ]}tj| d|}tj|sItd| d|	| q1|j
dd|i| |S tjtj| \}}	t| d	}
|
 }| d
}||v r| d}n
| d}| d}t|
|}|j|d |D ]@}|| }||vrtd| d|
|}i }| drtj|fi |}n| drtj|fi |}|j
|d q|S )a  
    Load the metadata and nominated configuration files from a MONAI bundle without loading the network itself.

    This function will load the information from the bundle, which can be a directory or a zip file containing a
    directory or a Torchscript bundle, and return the parser object with the information. This saves having to load
    the model if only the information is wanted, and can work on any sort of bundle format.

    Args:
        bundle_path: path to the bundle directory or zip file
        config_names: names of configuration files with extensions to load, should not be full paths but just name+ext
        load_kw_args: keyword arguments to pass to the ConfigParser object when loading

    Returns:
        ConfigParser object containing the parsed information
    r   )ConfigParserz#Cannot find bundle file/directory ''frT   zmetadata.jsonzCannot find config file 'rz/configs/metadata.jsonz	/configs/z/extra/metadata.jsonz/extra/)r]   json)r   ZymlN )monai.bundle.config_parserr[   ospathexists
ValueErrorisdirZ	read_metajoinappendread_configsplitextbasenamezipfileZipFilenamelistr_   loadsreadlowerendswithr   	safe_load)rV   rX   rY   r[   parserZ	conf_datacnameZcpathname_archive	all_filesZzip_meta_nameprefixZ	meta_jsonZ
full_cnameZardatacdatar`   r`   T/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/bundle/utils.pyload_bundle_config   sH    $



r}   args
dict | AnykvNonec              	   C  s   | tr^	 |dd }|| v rLt|tr%t| | tr%| | | dS t|tr:t| | tr:| | | dS ttd| dt| d| dt	
d| d| d	 || |< dS || |< dS )
zQ
    Update the `args` dict-like object with the key/value pair `k` and `v`.
    r0   Nz%config must be dict or list for key `z`, but got z: .zCan't merge entry ['z'], 'z*' is not in target dict - copying instead.)
startswithr   
isinstancedictupdatelistextendre   typewarningswarn)r~   r   r   rH   r`   r`   r|   merge_kv   s   
$r   )rV   rW   rX   rW   rY   r   rZ   r   )r~   r   r   rW   r   r   rZ   r   )
__future__r   r_   rb   r   rl   typingr   monai.config.deviceconfigr   monai.utilsr   r   rw   __all__r   r   r	   r
   r   Z_conf_valuesrW   splitZDEFAULT_METADATAZDEFAULT_INFERENCEZDEFAULT_HANDLERS_IDr   r   ZDEPRECATED_ID_MAPPINGr}   r   r`   r`   r`   r|   <module>   s   


	

			6
4
K