|
| 1 | +import pickle |
| 2 | +from logging import INFO |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import hydra |
| 7 | +from omegaconf import DictConfig |
| 8 | + |
| 9 | +from examples.training.multi_table import run_training |
| 10 | +from midst_toolkit.common.config import GeneralConfig, MatchingConfig, SamplingConfig |
| 11 | +from midst_toolkit.common.logger import TOOLKIT_LOGGER, log |
| 12 | +from midst_toolkit.models.clavaddpm.data_loaders import load_tables |
| 13 | +from midst_toolkit.models.clavaddpm.enumerations import Relation |
| 14 | +from midst_toolkit.models.clavaddpm.synthesizer import clava_synthesizing |
| 15 | + |
| 16 | + |
| 17 | +# Preventing some excessive logging |
| 18 | +TOOLKIT_LOGGER.setLevel(INFO) |
| 19 | + |
| 20 | + |
| 21 | +@hydra.main(config_path=".", config_name="config", version_base=None) |
| 22 | +def main(config: DictConfig) -> None: |
| 23 | + """ |
| 24 | + Run the synthesizing pipeline for a multi-table diffusion model. |
| 25 | +
|
| 26 | + It will load the config and then data from the `config.base_data_dir` folder, |
| 27 | + train the model, synthesize the data and save the results in the |
| 28 | + `config.results_dir` folder. |
| 29 | +
|
| 30 | + It will first look for a pre-trained model in the `config.results_dir` folder. |
| 31 | + If it doesn't find one, it will train a new model from scratch. |
| 32 | +
|
| 33 | + Args: |
| 34 | + config: Training and synthesizing configuration as an OmegaConf DictConfig object. |
| 35 | + """ |
| 36 | + log(INFO, f"Checking for a pre-trained model in {config.results_dir}...") |
| 37 | + |
| 38 | + _, relation_order, _ = load_tables(Path(config.base_data_dir)) |
| 39 | + |
| 40 | + model_file_paths: dict[Relation, dict[str, Any]] = {} |
| 41 | + for relation in relation_order: |
| 42 | + model_file_path = Path(config.results_dir) / "models" / f"{relation[0]}_{relation[1]}_ckpt.pkl" |
| 43 | + model_file_paths[relation] = { |
| 44 | + "file_path": model_file_path, |
| 45 | + "exists": model_file_path.exists(), |
| 46 | + } |
| 47 | + |
| 48 | + clustering_results_file = Path(config.results_dir) / "cluster_ckpt.pkl" |
| 49 | + |
| 50 | + if all(result["exists"] for result in model_file_paths.values()) and clustering_results_file.exists(): |
| 51 | + log(INFO, f"Found previous results in {config.results_dir}. Skipping training.") |
| 52 | + else: |
| 53 | + log(INFO, "Not all previous results found. Training a new model from scratch.") |
| 54 | + log(INFO, f"Summary of results: {model_file_paths}") |
| 55 | + log(INFO, f"Clustering results file: {clustering_results_file} exists? {clustering_results_file.exists()}") |
| 56 | + run_training.main(config) |
| 57 | + |
| 58 | + log(INFO, "Loading models...") |
| 59 | + |
| 60 | + models = {} |
| 61 | + for relation in relation_order: |
| 62 | + with open(model_file_paths[relation]["file_path"], "rb") as f: |
| 63 | + models[relation] = pickle.load(f) |
| 64 | + |
| 65 | + with open(clustering_results_file, "rb") as f: |
| 66 | + clustering_result = pickle.load(f) |
| 67 | + |
| 68 | + tables = clustering_result["tables"] |
| 69 | + all_group_lengths_prob_dicts = clustering_result["all_group_lengths_prob_dicts"] |
| 70 | + |
| 71 | + log(INFO, "Synthesizing data...") |
| 72 | + |
| 73 | + clava_synthesizing( |
| 74 | + tables, |
| 75 | + relation_order, |
| 76 | + Path(config.results_dir), |
| 77 | + models, |
| 78 | + GeneralConfig(**config.general_config), |
| 79 | + SamplingConfig(**config.sampling_config), |
| 80 | + MatchingConfig(**config.matching_config), |
| 81 | + all_group_lengths_prob_dicts, |
| 82 | + ) |
| 83 | + |
| 84 | + log(INFO, "Data synthesized successfully.") |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments