U
    Ph|                  	   @  s   d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	m
Z
 d dlZd dlmZ d dlmZ dZdd
dddddddddZddddddddZdS )    )annotationsN)MappingSequence)IOAny)get_config_values)JITMetadataKeyszmetadata.jsonTFztorch.nn.Modulezstr | IO[Any]boolzMapping[str, Any] | NonezMapping[str, bytes] | NoneNone)jit_objfilename_prefix_or_streaminclude_config_valsappend_timestampmeta_valuesmore_extra_filesreturnc                 C  s   t j  }i }|r2|t  |  |tjj< |dk	rD|| t	
|}t| i}	|dk	rl|	| t|trtj|\}
}|dkrd}|r||
 d| }n|
| }tj| ||	 dS )a.  
    Save the JIT object (script or trace produced object) `jit_obj` to the given file or stream with metadata
    included as a JSON file. The Torchscript format is a zip file which can contain extra file data which is used
    here as a mechanism for storing metadata about the network being saved. The data in `meta_values` should be
    compatible with conversion to JSON using the standard library function `dumps`. The intent is this metadata will
    include information about the network applicable to some use case, such as describing the input and output format,
    a network name and version, a plain language description of what the network does, and other relevant scientific
    information. Clients can use this information to determine automatically how to use the network, and users can
    read what the network does and keep track of versions.

    Examples::

        net = torch.jit.script(monai.networks.nets.UNet(2, 1, 1, [8, 16], [2]))

        meta = {
            "name": "Test UNet",
            "used_for": "demonstration purposes",
            "input_dims": 2,
            "output_dims": 2
        }

        # save the Torchscript bundle with the above dictionary stored as an extra file
        save_net_with_metadata(m, "test", meta_values=meta)

        # load the network back, `loaded_meta` has same data as `meta` plus version information
        loaded_net, loaded_meta, _ = load_net_with_metadata("test.ts")


    Args:
        jit_obj: object to save, should be generated by `script` or `trace`.
        filename_prefix_or_stream: filename or file-like stream object, if filename has no extension it becomes `.ts`.
        include_config_vals: if True, MONAI, Pytorch, and Numpy versions are included in metadata.
        append_timestamp: if True, a timestamp for "now" is appended to the file's name before the extension.
        meta_values: metadata values to store with the object, not limited just to keys in `JITMetadataKeys`.
        more_extra_files: other extra file data items to include in bundle, see `_extra_files` of `torch.jit.save`.
    N z.tsz_%Y%m%d%H%M%S)datetimenowupdater   
astimezone	isoformatr   	TIMESTAMPvaluejsondumpsMETADATA_FILENAMEencode
isinstancestrospathsplitextstrftimetorchjitsave)r   r   r   r   r   r   r   metadict	json_dataextra_filesZfilename_no_extext r+   Q/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/data/torchscript_utils.pysave_net_with_metadata   s&    -




r-   r+   ztorch.device | NonezSequence[str]z"tuple[torch.nn.Module, dict, dict])r   map_locationr   r   c                 C  sb   dd |D }d|t < tj| ||}t| }t |krJ|t  }|t = nd}t|}|||fS )a]  
    Load the module object from the given Torchscript filename or stream, and convert the stored JSON metadata
    back to a dict object. This will produce an empty dict if the metadata file is not present.

    Args:
        filename_prefix_or_stream: filename or file-like stream object.
        map_location: network map location as in `torch.jit.load`.
        more_extra_files: other extra file data names to load from bundle, see `_extra_files` of `torch.jit.load`.
    Returns:
        Triple containing loaded object, metadata dict, and extra files dict containing other file data if present
    c                 S  s   i | ]
}|d qS )r   r+   ).0fr+   r+   r,   
<dictcomp>w   s      z*load_net_with_metadata.<locals>.<dictcomp>r   z{})r   r$   r%   loaddictitemsr   loads)r   r.   r   r)   r   r(   Zjson_data_dictr+   r+   r,   load_net_with_metadatag   s    
r6   )TFNN)Nr+   )
__future__r   r   r   r    collections.abcr   r   typingr   r   r$   monai.configr   monai.utilsr   r   r-   r6   r+   r+   r+   r,   <module>   s"       M  