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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 d d	lmZ d d
lmZ d dlmZmZmZ d dlmZmZmZmZ d dlm Z  d dl!m"Z"m#Z#m$Z$ ddgZ%ee&dZ'G dd deZ(G dd de(Z)G dd de(Z*dS )    )annotationsN)ABCabstractmethod)Sequence)copy)
fileConfig)Path)Any)
get_logger)ConfigParser)InferPropertiesMetaPropertiesTrainProperties)DEFAULT_EXP_MGMT_SETTINGSEXPR_KEY
ID_REF_KEY
ID_SEP_KEY)PathLike)BundlePropertyBundlePropertyConfigensure_tupleBundleWorkflowConfigWorkflow)module_namec                      s   e Zd ZU dZdZded< dZded< 				d4d5ddZed6ddZ	ed6ddZ
ed6ddZed7dd Zed8d"d#Zd$d% Z fd&d'Zd(d) Zd*d+ Zd9d:d/d0Zd;d2d3Z  ZS )<r   a}  
    Base class for the workflow specification in bundle, it can be a training, evaluation or inference workflow.
    It defines the basic interfaces for the bundle workflow behavior: `initialize`, `run`, `finalize`, etc.
    And also provides the interface to get / set public properties to interact with a bundle workflow.

    Args:
        workflow_type: specifies the workflow type: "train" or "training" for a training workflow,
            or "infer", "inference", "eval", "evaluation" for a inference workflow,
            other unsupported string will raise a ValueError.
            default to `None` for only using meta properties.
        properties_path: the path to the JSON file of properties. If `workflow_type` is specified, properties will be
            loaded from the file based on the provided `workflow_type` and meta. If no `workflow_type` is specified,
            properties will default to loading from "meta". If `properties_path` is None, default properties
            will be sourced from "monai/bundle/properties.py" based on the workflow_type:
            For a training workflow, properties load from `TrainProperties` and `MetaProperties`.
            For a inference workflow, properties load from `InferProperties` and `MetaProperties`.
            For workflow_type = None : only `MetaProperties` will be loaded.
        meta_file: filepath of the metadata file, if this is a list of file paths, their contents will be merged in order.
        logging_file: config file for `logging` module in the program. for more details:
            https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig.

    traintrainingtuplesupported_train_typeinfer	inferenceevalZ
evaluationsupported_infer_typeNworkflow_type
str | Noneproperties_pathPathLike | None	meta_filestr | Sequence[str] | Nonelogging_filec           	      C  s  |d ur!t j|std| dtd| d t|dd |d urVt|tr;t j|s;t	d| d d }t|t
rV|D ]}t j|sUt	d| d d }qB|d urv| | jv rdd}n| | jv rnd	}ntd
| d|d urt|}| std| dt|o}z<t|}i | _|d ur||v r|| | _d|v r| j|d  n|d u rd|v r|d | _td ntd W n* ty } z
t| d| |d }~w tjy } ztd| |d }~ww W d    n	1 sw   Y  n2|dkri tt| _n%|d	kr"i tt| _n|d u r2tt| _td ntd
| d|| _|| _d S )N%Cannot find the logging config file: .,Setting logging properties based on config: FZdisable_existing_loggersz&Cannot find the metadata config file: zC. Please see: https://docs.monai.io/en/stable/mb_specification.htmlr   r    zUnsupported workflow type: 'z'.zProperty file z does not exist.metazONo workflow type specified, default to load meta properties from property file.z>No 'meta' key found in properties while workflow_type is None.z not found in property file z'Error decoding JSON from property file zKNo workflow type and property file specified, default to 'meta' properties.)ospathisfileFileNotFoundErrorloggerinfor   
isinstancestrerrorlistlowerr   r#   
ValueErrorr   is_fileopenjsonload
propertiesupdatewarningKeyErrorJSONDecodeErrorr   r   r   r   r$   r(   )	selfr$   r&   r(   r*   f	json_filer@   e rI   X/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/bundle/workflows.py__init__@   s   













zBundleWorkflow.__init__argsr	   kwargsreturnc                 O     t  )A
        Initialize the bundle workflow before running.

        NotImplementedErrorrE   rL   rM   rI   rI   rJ   
initialize      zBundleWorkflow.initializec                 O  rO   )zZ
        Run the bundle workflow, it can be a training, evaluation or inference.

        rQ   rS   rI   rI   rJ   run   rU   zBundleWorkflow.runc                 O  rO   )F
        Finalize step after the running of bundle workflow.

        rQ   rS   rI   rI   rJ   finalize   rU   zBundleWorkflow.finalizenamer7   propertydictc                 C  rO   )a  
        With specified property name and information, get the expected property value.

        Args:
            name: the name of target property.
            property: other information for the target property, defined in `TrainProperties` or `InferProperties`.

        rQ   )rE   rY   rZ   rI   rI   rJ   _get_property   s   
zBundleWorkflow._get_propertyvaluec                 C  rO   )I  
        With specified property name and information, set value for the expected property.

        Args:
            name: the name of target property.
            property: other information for the target property, defined in `TrainProperties` or `InferProperties`.
            value: value to set for the property.

        rQ   rE   rY   rZ   r]   rI   rI   rJ   _set_property   s   zBundleWorkflow._set_propertyc                 C  s2   | j d ur|| j v r| j|| j | dS | |S )NrY   rZ   )r@   r\   __getattribute__)rE   rY   rI   rI   rJ   __getattr__   s   
zBundleWorkflow.__getattr__c                   sH   |dkr| j d ur|| j v r| j|| j | |d d S t || d S )Nr@   )rY   rZ   r]   )r@   r`   super__setattr__)rE   rY   r]   	__class__rI   rJ   re      s   zBundleWorkflow.__setattr__c                 C     | j S )zP
        Get the workflow type, it can be `None`, "train", or "infer".

        )r$   rE   rI   rI   rJ   get_workflow_type      z BundleWorkflow.get_workflow_typec                 C  rh   )z%
        Get the meta file.

        )r(   ri   rI   rI   rJ   get_meta_file   rk   zBundleWorkflow.get_meta_filerequireddescNonec                 C  sF   | j du ri | _ || j v rtd| d tj|tj|i| j |< dS )a  
        Besides the default predefined properties, some 3rd party applications may need the bundle
        definition to provide additional properties for the specific use cases, if the bundle can't
        provide the property, means it can't work with the application.
        This utility adds the property for the application requirements check and access.

        Args:
            name: the name of target property.
            required: whether the property is "must-have".
            desc: descriptions for the property.
        Nz
property 'z7' already exists in the properties list, overriding it.)r@   r4   rB   r   DESCREQUIRED)rE   rY   rm   rn   rI   rI   rJ   add_property   s
   

zBundleWorkflow.add_propertylist[str] | Nonec                   s&    j du rdS  fdd j  D S )z
        Check whether the required properties are existing in the bundle workflow.
        If no workflow type specified, return None, otherwise, return a list of required but missing properties.

        Nc                   s,   g | ]\}}| tjd rt |s|qS )F)getr   rq   hasattr).0npri   rI   rJ   
<listcomp>   s   , z3BundleWorkflow.check_properties.<locals>.<listcomp>)r@   itemsri   rI   ri   rJ   check_properties   s   
zBundleWorkflow.check_properties)NNNN)r$   r%   r&   r'   r(   r)   r*   r%   rL   r	   rM   r	   rN   r	   rY   r7   rZ   r[   rN   r	   rY   r7   rZ   r[   r]   r	   rN   r	   N)rY   r7   rm   r7   rn   r%   rN   ro   rN   rs   )__name__
__module____qualname____doc__r   __annotations__r#   rK   r   rT   rV   rX   r\   r`   rc   re   rj   rl   rr   r{   __classcell__rI   rI   rf   rJ   r   %   s2   
 Kc                      sf   e Zd ZU dZdZded< dZded< 					d"d# fddZd$ddZd%ddZ	d&d d!Z
  ZS )'PythonicWorkflowai
  
    Base class for the pythonic workflow specification in bundle, it can be a training, evaluation or inference workflow.
    It defines the basic interfaces for the bundle workflow behavior: `initialize`, `finalize`, etc.
    This also provides the interface to get / set public properties to interact with a bundle workflow through
    defined `get_<property>` accessor methods or directly defining members of the object.
    For how to set the properties, users can define the `_set_<property>` methods or directly set the members of the object.
    The `initialize` method is called to set up the workflow before running. This method sets up internal state
    and prepares properties. If properties are modified after the workflow has been initialized, `self._is_initialized`
    is set to `False`. Before running the workflow again, `initialize` should be called to ensure that the workflow is
    properly set up with the new property values.

    Args:
        workflow_type: specifies the workflow type: "train" or "training" for a training workflow,
            or "infer", "inference", "eval", "evaluation" for a inference workflow,
            other unsupported string will raise a ValueError.
            default to `None` for only using meta properties.
        workflow: specifies the workflow type: "train" or "training" for a training workflow,
            or "infer", "inference", "eval", "evaluation" for a inference workflow,
            other unsupported string will raise a ValueError.
            default to `None` for common workflow.
        properties_path: the path to the JSON file of properties. If `workflow_type` is specified, properties will be
            loaded from the file based on the provided `workflow_type` and meta. If no `workflow_type` is specified,
            properties will default to loading from "meta". If `properties_path` is None, default properties
            will be sourced from "monai/bundle/properties.py" based on the workflow_type:
            For a training workflow, properties load from `TrainProperties` and `MetaProperties`.
            For a inference workflow, properties load from `InferProperties` and `MetaProperties`.
            For workflow_type = None : only `MetaProperties` will be loaded.
        config_file: path to the config file, typically used to store hyperparameters.
        meta_file: filepath of the metadata file, if this is a list of file paths, their contents will be merged in order.
        logging_file: config file for `logging` module in the program. for more details:
            https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig.

    r   r   r   r   r#   Nr$   r%   r&   r'   config_filer)   r(   r*   overrider	   c                   s   |d u rt tt d n|}t j||||d i | _i | _t | _	|d ur/| j	j
|d | jd ur<| j	j| jd | j	j|d d| _d S )Nmetadata.json)r$   r&   r(   r*   rF   pairsF)r7   r   r0   getcwdrd   rK   _props_vals_set_props_valsr   parserread_configr(   	read_metarA   _is_initialized)rE   r$   r&   r   r(   r*   r   rf   rI   rJ   rK     s    	

zPythonicWorkflow.__init__rL   rM   rN   c                 O  s   i | _ d| _dS )z@
        Initialize the bundle workflow before running.
        TN)r   r   rS   rI   rI   rJ   rT   2  s   
zPythonicWorkflow.initializerY   r7   rZ   r[   c              
   C  s   | j stdd}|| jv r| j| }|S || jv r!| j| }|S || jj| jj v r>| j|dt	j
d}| j| }|S zt| d|  }W n# tyl } z|tj rbtd| d| d|W Y d}~nd}~ww || j|< |S )a  
        With specified property name and information, get the expected property value.
        If the property is already generated, return from the bucket directly.
        If user explicitly set the property, return it directly.
        Otherwise, generate the expected property as a class private property with prefix "_".

        Args:
            name: the name of target property.
            property: other information for the target property, defined in `TrainProperties` or `InferProperties`.
        z:Please execute 'initialize' before getting any properties.Nget_zunsupported property 'zG' is required in the bundle properties,need to implement a method 'get_z' to provide the property.)r   RuntimeErrorr   r   r   configmeta_keyr@   rt   r   IDgetattrAttributeErrorr   rq   r;   )rE   rY   rZ   r]   idrH   rI   rI   rJ   r\   9  s:   






zPythonicWorkflow._get_propertyr]   c                 C  s   || j |< d| _dS )a  
        With specified property name and information, set value for the expected property.
        Stores user-reset initialized objects that should not be re-initialized and marks the workflow as not initialized.

        Args:
            name: the name of target property.
            property: other information for the target property, defined in `TrainProperties` or `InferProperties`.
            value: value to set for the property.

        FN)r   r   r_   rI   rI   rJ   r`   Z  s   

zPythonicWorkflow._set_property)NNNNN)r$   r%   r&   r'   r   r)   r(   r)   r*   r%   r   r	   r|   r}   r~   )r   r   r   r   r   r   r#   rK   rT   r\   r`   r   rI   rI   rf   rJ   r      s   
 "

!r   c                      s   e Zd ZdZ								dBdC fddZdDddZdDdd ZdDd!d"ZdE fd$d%ZdFd*d+Z	dGd.d/Z
dGd0d1ZdHd3d4Z	dIdJ fd8d9ZdKd;d<ZedLd@dAZ  ZS )Mr   ad  
    Specification for the config-based bundle workflow.
    Standardized the `initialize`, `run`, `finalize` behavior in a config-based training, evaluation, or inference.
    Before `run`, we add bundle root directory to Python search directories automatically.
    For more information: https://docs.monai.io/en/latest/mb_specification.html.

    Args:
        config_file: filepath of the config file, if this is a list of file paths, their contents will be merged in order.
        meta_file: filepath of the metadata file, if this is a list of file paths, their contents will be merged in order.
            If None, default to "configs/metadata.json", which is commonly used for bundles in MONAI model zoo.
        logging_file: config file for `logging` module in the program. for more details:
            https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig.
            If None, default to "configs/logging.conf", which is commonly used for bundles in MONAI model zoo.
            If False, the logging logic for the bundle will not be modified.
        init_id: ID name of the expected config expression to initialize before running, default to "initialize".
            allow a config to have no `initialize` logic and the ID.
        run_id: ID name of the expected config expression to run, default to "run".
            to run the config, the target config must contain this ID.
        final_id: ID name of the expected config expression to finalize after running, default to "finalize".
            allow a config to have no `finalize` logic and the ID.
        tracking: if not None, enable the experiment tracking at runtime with optionally configurable and extensible.
            if "mlflow", will add `MLFlowHandler` to the parsed bundle with default tracking settings,
            if other string, treat it as file path to load the tracking settings.
            if `dict`, treat it as tracking settings.
            will patch the target config content with `tracking handlers` and the top-level items of `configs`.
            for detailed usage examples, please check the tutorial:
            https://github.com/Project-MONAI/tutorials/blob/main/experiment_management/bundle_integrate_mlflow.ipynb.
        workflow_type: specifies the workflow type: "train" or "training" for a training workflow,
            or "infer", "inference", "eval", "evaluation" for a inference workflow,
            other unsupported string will raise a ValueError.
            default to `None` for common workflow.
        properties_path: the path to the JSON file of properties. If `workflow_type` is specified, properties will be
            loaded from the file based on the provided `workflow_type` and meta. If no `workflow_type` is specified,
            properties will default to loading from "train". If `properties_path` is None, default properties
            will be sourced from "monai/bundle/properties.py" based on the workflow_type:
            For a training workflow, properties load from `TrainProperties` and `MetaProperties`.
            For a inference workflow, properties load from `InferProperties` and `MetaProperties`.
            For workflow_type = None : only `MetaProperties` will be loaded.
        override: id-value pairs to override or add the corresponding config content.
            e.g. ``--net#input_chns 42``, ``--net %/data/other.json#net_arg``

    NrT   rV   rX   r   r   str | Sequence[str]r(   r)   r*   str | bool | Noneinit_idr7   run_idfinal_idtrackingstr | dict | Noner$   r%   r&   r'   r   r	   rN   ro   c
                   s  |d ur6t |}t|d j}|D ]#}t|}|j|kr(td| d| d | s4td| dqntd}|d u rDt|d n|}t j	|||	d	 || _
|d u r]t| j
d
 n|}|du rmtd| d n2tj|s|t| j
d
 krtd| d ntd| dtt|dd td| d t | _| jj|d | jd ur| jj| jd | jj|
d || _|| _|| _|d urt|tr|tv rt| }nt|}| j| j|d d| _d S )Nr   zNot all config files are in z2. If logging_file and meta_file arenot specified, z3 will be used as the default config root directory.zCannot find the config file: r,   configsr   )r$   r(   r&   zlogging.confFzLogging file is set to z, skipping logging.zDefault logging file in z" does not exist, skipping logging.r+   r.   r-   r   r   )r   settings)r   r   parentr4   rB   r<   r3   r7   rd   rK   config_root_pathr0   r1   r2   r   r5   r   r   r   r(   r   rA   r   r   r   r6   r   load_config_filespatch_bundle_trackingr   )rE   r   r(   r*   r   r   r   r   r$   r&   r   Z_config_filesr   Z_config_fileZ	settings_rf   rI   rJ   rK     sT   





zConfigWorkflow.__init__c                 C  s"   | j jdd d| _| j| jdS )rP   T)resetr   )r   parser   	_run_exprr   ri   rI   rI   rJ   rT     s   zConfigWorkflow.initializec                 C  sX   | j jdkr
| j jn| j }tjdt| | j| jvr%t	d| j d| j
| jdS )z
        Run the bundle workflow, it can be a training, evaluation or inference.
        Before run, we add bundle root directory to Python search directories automatically.

        r      zrun ID 'z#' doesn't exist in the config file.r   )r   rY   r   sysr1   insertr7   r   r   r;   r   )rE   Z_bundle_root_pathrI   rI   rJ   rV     s   zConfigWorkflow.runc                 C  s   | j | jdS )rW   r   )r   r   ri   rI   rI   rJ   rX     s   zConfigWorkflow.finalizers   c                   s   t   }| jdu rtd dS |rtd|  g }| j D ]\}}|tjds9| j	||ds9|
| q"|rDtd|  |durM|| |S )aD  
        Check whether the required properties are existing in the bundle workflow.
        If the optional properties have reference in the config, will also check whether the properties are existing.
        If no workflow type specified, return None, otherwise, return a list of required but missing properties.

        Nz5No available properties had been set, skipping check.zBLoaded bundle does not contain the following required properties: Fra   zGLoaded bundle defines the following optional properties with wrong ID: )rd   r{   r@   r4   rB   rz   rt   r   rq   _check_optional_idappendextend)rE   retZwrong_propsrw   rx   rf   rI   rJ   r{     s    




zConfigWorkflow.check_propertiesr   rM   r[   	list[Any]c                 K  s   g }|| j v rMt| j | tr9tt| j | D ]}| t | }|| j j|fi | | j j	| q|S || j j|fi | | j j	| |S )z
        Evaluate the expression or expression list given by `id`. The resolved values from the evaluations are not stored,
        allowing this to be evaluated repeatedly (eg. in streaming applications) without restarting the hosting process.
        )
r   r6   r9   rangelenr   r   get_parsed_contentref_resolverremove_resolved_content)rE   r   rM   r   isub_idrI   rI   rJ   r   	  s   
zConfigWorkflow._run_exprrY   rZ   c                 C  s@   |t j }|| jvr|tjdsd S td| d| d|S )NFz
Property 'z' with config ID 'z' not in the config.)r   r   r   rt   r   rq   rC   rE   rY   rZ   prop_idrI   rI   rJ   _get_prop_id  s   

zConfigWorkflow._get_prop_idc                 C  s4   | j std| ||}|dur| jj|dS dS )a  
        With specified property name and information, get the parsed property value from config.

        Args:
            name: the name of target property.
            property: other information for the target property, defined in `TrainProperties` or `InferProperties`.

        z>Please execute 'initialize' before getting any parsed content.Nr   )r   r   r   r   r   r   rI   rI   rJ   r\   $  s   	zConfigWorkflow._get_propertyr]   c                 C  s8   |  ||}|dur|| j|< d| _| jj  dS dS )r^   NF)r   r   r   r   r   )rE   rY   rZ   r]   r   rI   rI   rJ   r`   2  s   

zConfigWorkflow._set_propertyrm   	config_idrn   c                   s&   t  j|||d || j| tj< dS )ao  
        Besides the default predefined properties, some 3rd party applications may need the bundle
        definition to provide additional properties for the specific use cases, if the bundle can't
        provide the property, means it can't work with the application.
        This utility adds the property for the application requirements check and access.

        Args:
            name: the name of target property.
            required: whether the property is "must-have".
            config_id: the config ID of target property in the bundle definition.
            desc: descriptions for the property.

        )rY   rm   rn   N)rd   rr   r@   r   r   )rE   rY   rm   r   rn   rf   rI   rJ   rr   C  s   zConfigWorkflow.add_propertyboolc                 C  s   | tjd}| tjd}|du rdS d}|dv r<dt d| jv r;| jdt d D ]}|d dkr:| |d}q,n| j |d}|durT|tsT|t| krTdS dS )	a  
        If an optional property has reference in the config, check whether the property is existing.
        If `ValidationHandler` is defined for a training workflow, will check whether the optional properties
        "evaluator" and "val_interval" are existing.

        Args:
            name: the name of target property.
            property: other information for the target property, defined in `TrainProperties` or `InferProperties`.

        NT)	evaluatorval_intervalr   handlers_target_ZValidationHandlerF)	rt   r   r   REF_IDr   r   
startswithr   r   )rE   rY   rZ   r   ref_idrefhrI   rI   rJ   r   V  s   z!ConfigWorkflow._check_optional_idr   r   r   c                 C  s8  |d   D ]B\}}||d v r@| |d | d }|dur?| |d | d }|du r:|g||dkr7dnd< q|| q|| vrH|| |< qd	td
 d}| dd}|rt|trd| vrlt t d| d< | d d| d | d< t	j
| d|}t|jjddd | |  | dS d| d< dS )a,  
        Patch the loaded bundle config with a new handler logic to enable experiment tracking features.

        Args:
            parser: loaded config content to patch the handler.
            settings: settings for the experiment tracking, should follow the pattern of default settings.

        r   handlers_idr   Nr   trainertrain_handlersval_handlersZconfig_z%Y%m%d_%H%M%Sz.jsonsave_execute_configT
output_dirzbundle_root + '/eval'z + '/')parentsexist_ok)rz   rt   r   timestrftimer6   r   r   r   r0   r1   joinr   r   r   mkdirexport_config_file)r   r   kvenginer   default_namefilepathrI   rI   rJ   r   t  s.   


z$ConfigWorkflow.patch_bundle_tracking)NNrT   rV   rX   Nr   N)r   r   r(   r)   r*   r   r   r7   r   r7   r   r7   r   r   r$   r%   r&   r'   r   r	   rN   ro   )rN   r	   r   )r   r7   rM   r[   rN   r   r}   )rY   r7   rZ   r[   r]   r	   rN   ro   r   )
rY   r7   rm   r7   r   r7   rn   r%   rN   ro   )rY   r7   rZ   r[   rN   r   )r   r   r   r[   rN   ro   )r   r   r   r   rK   rT   rV   rX   r{   r   r   r\   r`   rr   r   staticmethodr   r   rI   rI   rf   rJ   r   i  s0    .
=





	

)+
__future__r   r>   r0   r   r   abcr   r   collections.abcr   r   Zlogging.configr   pathlibr   typingr	   monai.apps.utilsr
   monai.bundle.config_parserr   Zmonai.bundle.propertiesr   r   r   monai.bundle.utilsr   r   r   r   monai.configr   monai.utilsr   r   r   __all__r   r4   r   r   r   rI   rI   rI   rJ   <module>   s.   
 Ov