diff --git a/cmflib/cmf_ray_logger.py b/cmflib/cmf_ray_logger.py new file mode 100644 index 00000000..c0eda132 --- /dev/null +++ b/cmflib/cmf_ray_logger.py @@ -0,0 +1,60 @@ +from ray import tune +from ray.tune import Callback +from cmflib import cmf + +class CmfRayLogger(Callback): + #id_count = 1 + + def __init__(self, pipeline_name, file_path, pipeline_stage): + """ + pipeline_name: The name of the CMF Pipelibe + file_path: The path to metadata file + pipeline_stage: The name for the stage of cmf_pipeline + """ + self.pipeline_name = pipeline_name + self.file_path = file_path + self.pipeline_stage = pipeline_stage + self.cmf_obj = {} + + def on_trial_start(self, iteration, trials, trial, **info): + trial_id = trial.trial_id + print(f"CMF Logging Started for Trial {trial_id}") + self.cmf_obj[trial_id] = cmf.Cmf(filepath = self.file_path, pipeline_name = self.pipeline_name) + _ = self.cmf_obj[trial_id].create_context(pipeline_stage = self.pipeline_stage) + _ = self.cmf_obj[trial_id].create_execution(execution_type=f"Trial_{trial_id}", + create_new_execution = False) + #self.execution_id[trial_id] = CmfRayLogger.id_count + #CmfRayLogger.id_count+=1 + + def on_trial_result(self, iteration, trials, trial, result, **info): + trial_id = trial.trial_id + trial_config = trial.config + trial_result = trial.last_result + curr_res = result + print(f'In Trial Results') + print(f'Trial_ID: {trial_id}') + print(f'curr_results is {curr_res}') + print(f'Logging results to CMF with metric name Trial_{trial_id}_metrics') + #if trial_id in self.execution_id: + # _ = self.metawriter.update_execution(int(self.execution_id[trial_id])) + _ = self.cmf_obj[trial_id].log_metric(metrics_name = f"Trial_{trial_id}_metrics", + custom_properties = {'Output': curr_res}) + + + def on_trial_complete(self, iteration, trials, trial, **info): + trial_id = trial.trial_id + trial_config = trial.config + trial_result = trial.last_result + + print(f"Trial {trial_id} completed, Commiting to CMF: with name Trial_{trial_id}_metrics") + print() + #if trial_id in self.execution_id: + # _ = self.metawriter.update_execution(int(self.execution_id[trial_id])) + _ = self.cmf_obj[trial_id].commit_metrics(f"Trial_{trial_id}_metrics") + + def on_trial_error(self, iteration, trials, trial, **info): + trial_id = trial.trial_id + trial_config = trial.config + trial_result = trial.last_result + + print(f"An error occured with Trial {trial_id}, Not commiting anything to cmf") \ No newline at end of file diff --git a/docs/api/public/cmf_ray_logger.md b/docs/api/public/cmf_ray_logger.md new file mode 100644 index 00000000..4447e28a --- /dev/null +++ b/docs/api/public/cmf_ray_logger.md @@ -0,0 +1,77 @@ +# cmflib.cmf_ray_logger.CmfRayLogger + + +# CmfRayLogger User Guide + +## Overview +The `CmfRayLogger` class is designed to log Ray Tune metrics for the CMF (Common Metadata Framework). It tracks the performance and outputs of trials during the tuning process, directly linking metrics to stages of your CMF pipeline. + +## Requirements +- Ensure both `cmf` and `raytune` are installed on your system to use the `CmfRayLogger`. + +## Installation +To use `CmfRayLogger`, import it in your Python script: + +```python +from cmf import cmf_ray_logger +``` + +## Usage + +### Initialization + +Create an instance of CmfRayLogger by providing the following parameters: + +* pipeline_name: A string representing the name of the CMF pipeline. +* file_path: The file path to the metadata file associated with the CMF pipeline. +* pipeline_stage: The name of the current stage of the CMF pipeline. + +Example of instantiation: +```python +logger = cmf_ray_logger.CmfRayLogger(pipeline_name, file_path, pipeline_stage) +``` + +## Integration with Ray Tune + +After initializing the logger, it should be passed to Ray Tune’s `tune.run` method via the `callbacks` parameter. This setup allows `CmfRayLogger` to log metrics for each trial based on the pipeline configuration and trial execution details. + +```Python +from ray import tune + +# Example configuration for Ray Tune +config = { + # Your configuration details +} + +tune.run( + , + config=config, + callbacks=[logger] +) +``` + +## Output +During each trial, `CmfRayLogger` will automatically create a CMF object with attributes set as `pipeline_name`, `pipeline_stage`, and the CMF execution as `trial_id`. It captures the trial's output and logs it under the metric key `'Output'`. + +## Example +Here is a complete example of how to use `CmfRayLogger` with Ray Tune: + +```Python +from cmf import cmf_ray_logger +from ray import tune + +# Initialize the logger +logger = cmf_ray_logger.CmfRayLogger("ExamplePipeline", "/path/to/metadata.json", "Stage1") + +# Configuration for tuning +config = { + # Configuration details +} + +# Execute the tuning process +tune.run( + , + config=config, + callbacks=[logger] +) +``` \ No newline at end of file