U Ph`@sddlmZddlZddlZddlZddlZddlmZmZddl m Z ddl m Z ddl m Z ddlmZmZddlmZdd lmZdd lmZmZmZdd lmZmZmZmZdd lmZdd l m!Z!m"Z"m#Z#m$Z$m%Z%ddgZ&ee'dZ(GdddeZ)Gddde)Z*dS)) annotationsN)ABCabstractmethod)copy) fileConfig)Path)AnySequence) get_logger) ConfigParser)InferPropertiesMetaPropertiesTrainProperties)DEFAULT_EXP_MGMT_SETTINGSEXPR_KEY ID_REF_KEY ID_SEP_KEY)PathLike)BundlePropertyBundlePropertyConfigdeprecated_argdeprecated_arg_default ensure_tupleBundleWorkflowConfigWorkflow) module_namecseZdZUdZdZded<dZded<eddd d d d d4ddddddddZe ddddddZ e ddddddZ e ddddddZ e dddddd Z e ddddd!d"d#Zd$d%Zfd&d'Zd(d)Zd*d+Zd5dddd,d-d.d/Zd0d1d2d3ZZS)6ra 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 common workflow. 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. 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. )traintrainingtuplesupported_train_type)infer inferenceevalZ evaluationsupported_infer_typeworkflow1.21.5 workflow_type#please use `workflow_type` instead.sinceremovednew_name msg_suffixN str | NonePathLike | Nonestr | Sequence[str] | None)r'r$properties_path meta_file logging_filec Cs|dk rBtj|s$td|dtd|dt|dd|dk rt|trvtj|svt d|dd}t|t r|D]&}tj|st d|dd}q|dk r|n|}|dkr|dkrt t |_ d|_||_dS|dk rBt|}|std|d t|}t||_ W5QRXd|_||_dS||jkrdtt |_ d |_n2||jkrtt |_ d |_ntd |d ||_dS)N%Cannot find the logging config file: .,Setting logging properties based on config: FZdisable_existing_loggersz&Cannot find the metadata config file: zC. Please see: https://docs.monai.io/en/stable/mb_specification.htmlzProperty file z does not exist.rr zUnsupported workflow type: 'z'.)ospathisfileFileNotFoundErrorloggerinfor isinstancestrerrorlistrr propertiesr'r2ris_file ValueErroropenjsonloadlowerrrr#r )selfr'r$r1r2r3f json_filerLK/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/bundle/workflows.py__init__=sR            zBundleWorkflow.__init__r)argskwargsreturncOs tdS)A Initialize the bundle workflow before running. NNotImplementedErrorrIrOrPrLrLrM initialize|szBundleWorkflow.initializecOs tdS)zZ Run the bundle workflow, it can be a training, evaluation or inference. NrSrUrLrLrMrunszBundleWorkflow.runcOs tdS)F Finalize step after the running of bundle workflow. NrSrUrLrLrMfinalizeszBundleWorkflow.finalizer?dictnamepropertyrQcCs tdS)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`. NrS)rIr\r]rLrLrM _get_propertys zBundleWorkflow._get_propertyr\r]valuerQcCs tdS)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. NrS)rIr\r]r`rLrLrM _set_propertys zBundleWorkflow._set_propertycCs6|jdk r(||jkr(|j||j|dS||SdS)Nr\r])rBr^__getattribute__)rIr\rLrLrM __getattr__szBundleWorkflow.__getattr__csF|dkr4|jdk r4||jkr4|j||j||dnt||dS)NrB)r\r]r`)rBrbsuper __setattr__)rIr\r` __class__rLrMrgszBundleWorkflow.__setattr__cCs|jS)zP Get the workflow type, it can be `None`, "train", or "infer". )r'rIrLrLrMget_workflow_typesz BundleWorkflow.get_workflow_typecCs|jS)z% Get the meta file. )r2rjrLrLrM get_meta_fileszBundleWorkflow.get_meta_fileNone)r\requireddescrQcCsF|jdkri|_||jkr,td|dtj|tj|i|j|<dS)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.)rBr<warnrDESCREQUIRED)rIr\rnrorLrLrM add_propertys  zBundleWorkflow.add_propertylist[str] | NonerQcs&jdkrdSfddjDS)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. Ncs,g|]$\}}|tjdrt|s|qS)F)getrrrhasattr).0nprjrLrM s z3BundleWorkflow.check_properties..)rBitemsrjrLrjrMcheck_propertiess zBundleWorkflow.check_properties)NNNNN)N)__name__ __module__ __qualname____doc__r__annotations__r#rrNrrVrWrYr^rbrergrkrlrsr} __classcell__rLrLrhrMr$s@    8   cs,eZdZdZeddddddeddd dd d d;ddddddddddddd fdd ZddddZddddZddd d!Z d"dfd#d$ Z dd%dd&d'd(Z dd%dd)d*d+Z dd%dd)d,d-Z dd%ddd.d/d0Zdrload_config_filespatch_bundle_tracking_is_initialized)rIrr2r3rrrrr'r$r1rZ _config_filesrZ _config_fileZ settings_rhrLrMrNsN     zConfigWorkflow.__init__rucCs"|jjddd|_|j|jdS)rRT)resetid)rparser _run_exprrrjrLrLrMrVVszConfigWorkflow.initializecCsX|jjdkr|jjn|j}tjdt||j|jkrJt d|jd|j |jdS)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. rzrun ID 'z#' doesn't exist in the config file.r) rr\rsysr9insertr?rrrDr)rIZ_bundle_root_pathrLrLrMrW`s  zConfigWorkflow.runcCs|j|jdS)rXr)rrrjrLrLrMrYnszConfigWorkflow.finalizertcst}|jdkr"tddS|r6td|g}|jD].\}}|tjdsD|j ||dsD| |qD|rtd||dk r| ||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: FrczGLoaded bundle defines the following optional properties with wrong ID: ) rfr}rBr<rpr|rvrrr_check_optional_idappendextend)rIretZ wrong_propsryrzrhrLrMr}us     zConfigWorkflow.check_propertiesrZ)rrPrQcKs||jkr|jj|f|SdS)N)rget_parsed_content)rIrrPrLrLrMrszConfigWorkflow._run_exprr[cCs@|tj}||jkr<|tjds&dStd|d|d|S)NFz Property 'z' with config ID 'z' not in the config.)rIDrrvrrrKeyErrorrIr\r]prop_idrLrLrM _get_prop_ids   zConfigWorkflow._get_prop_idcCs4|jstd|||}|dk r0|jj|dSdS)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 RuntimeErrorrrrrrLrLrMr^s  zConfigWorkflow._get_propertyr_cCs4|||}|dk r0||j|<d|_|jjdS)raNF)rrr ref_resolverr)rIr\r]r`rrLrLrMrbs  zConfigWorkflow._set_property)r\rn config_idrorQcs&tj|||d||j|tj<dS)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. )r\rnroN)rfrsrBrr)rIr\rnrrorhrLrMrsszConfigWorkflow.add_propertyboolcCs|tjd}|tjd}|dkr(dSd}|dkrxdtd|jkr|jdtdD]}|ddkrX||d}qXn|j|d}|dk r|ts|t|krdSdS) 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_intervalrhandlers_target_ZValidationHandlerF) rvrrREF_IDrr startswithrr)rIr\r]rref_idrefhrLrLrMrs  z!ConfigWorkflow._check_optional_idr )rrrQcCs:|dD]\}}||dkr||d|d}|dk r||d|d}|dkrt|g||dkrndnd<q||q ||kr |||<q d td d }|d d }|r.t|trd|krttd|d<|dd|d|d <t j | d|}t |jjd d d|||nd|d <dS)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_idrNrtrainertrain_handlers val_handlersZconfig_z %Y%m%d_%H%M%Sz.jsonsave_execute_configT output_dirzbundle_root + '/eval'z + '/')parentsexist_ok)r|rvrtimestrftimer>rrrr8r9joinrrrmkdirexport_config_file)rrkvenginer default_namefilepathrLrLrMrs*      z$ConfigWorkflow.patch_bundle_tracking) NNrVrWrYNNNN)N)r~rrrrrrNrVrWrYr}rrr^rbrsr staticmethodrrrLrLrhrMrs@).@  )+ __future__rrFr8rrabcrrrZlogging.configrpathlibrtypingrr monai.apps.utilsr monai.bundle.config_parserr Zmonai.bundle.propertiesr r rmonai.bundle.utilsrrrr monai.configr monai.utilsrrrrr__all__r~r<rrrLrLrLrM s(        A