From 230240d17e444b6c42b3396680e00f6265cd51de Mon Sep 17 00:00:00 2001
From: Jakub Dylag <jjd1c23@soton.ac.uk>
Date: Thu, 10 Apr 2025 08:44:38 +0100
Subject: [PATCH] (feat) flag to stop phen diff from check config.yml if it
 doesn't exist

---
 acmc/main.py |  8 +++++++-
 acmc/phen.py | 57 ++++++++++++++++++++++++++++------------------------
 2 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/acmc/main.py b/acmc/main.py
index 4b7ac6c..5f796b0 100644
--- a/acmc/main.py
+++ b/acmc/main.py
@@ -78,7 +78,7 @@ def _phen_copy(args: argparse.Namespace):
 
 def _phen_diff(args: argparse.Namespace):
     """Handle the `phen diff` command."""
-    phen.diff(args.phen_dir, args.version, args.old_phen_dir, args.old_version)
+    phen.diff(args.phen_dir, args.version, args.old_phen_dir, args.old_version, args.not_check_config)
 
 
 def main():
@@ -323,6 +323,12 @@ def main():
         required=True,
         help="Old phenotype version to compare with the changed version",
     )
+    phen_diff_parser.add_argument(
+        "--not-check-config",
+        action='store_true',
+        default=False,
+        help="(Optional) Prevent loading and comparing config file, in the case where one does not exist",
+    )   
     phen_diff_parser.set_defaults(func=_phen_diff)
 
     # Parse arguments
diff --git a/acmc/phen.py b/acmc/phen.py
index abea461..052e1a0 100644
--- a/acmc/phen.py
+++ b/acmc/phen.py
@@ -1375,37 +1375,42 @@ def diff_phen(
     old_phen_path: Path,
     old_version: str,
     report_path: Path,
+    not_check_config:bool,
 ):
     """Compare the differences between two versions of a phenotype"""
 
-    # validate phenotypes
-    _logger.debug(f"Validating for diff old path: {str(old_phen_path.resolve())}")
-    validate(str(old_phen_path.resolve()))
-    _logger.debug(f"Validating for diff new path: {str(new_phen_path.resolve())}")
-    validate(str(new_phen_path.resolve()))
-
-    # get old and new config
-    old_config_path = old_phen_path / CONFIG_FILE
-    with old_config_path.open("r") as file:
-        old_config = yaml.safe_load(file)
-    new_config_path = new_phen_path / CONFIG_FILE
-    with new_config_path.open("r") as file:
-        new_config = yaml.safe_load(file)
-
     # write report heading
     report = f"# Phenotype Comparison Report\n"
-    report += f"## Original phenotype\n"
-    report += f"  - {old_config['phenotype']['omop']['vocabulary_id']}\n"
-    report += f"  - {old_version}\n"
-    report += f"  - {str(old_phen_path.resolve())}\n"
-    report += f"## Changed phenotype:\n"
-    report += f"  - {new_config['phenotype']['omop']['vocabulary_id']}\n"
-    report += f"  - {new_version}\n"
-    report += f"  - {str(new_phen_path.resolve())}\n"
 
     # Step 1: check differences configuration files
-    # Convert list of dicts into a dict: {name: file}
-    report += diff_config(old_config, new_config)
+    if not not_check_config:
+        # validate phenotypes
+        _logger.debug(f"Validating for diff old path: {str(old_phen_path.resolve())}")
+        validate(str(old_phen_path.resolve()))
+        _logger.debug(f"Validating for diff new path: {str(new_phen_path.resolve())}")
+        validate(str(new_phen_path.resolve()))
+
+        # get old and new config
+        old_config_path = old_phen_path / CONFIG_FILE
+        with old_config_path.open("r") as file:
+            old_config = yaml.safe_load(file)
+        new_config_path = new_phen_path / CONFIG_FILE
+        with new_config_path.open("r") as file:
+            new_config = yaml.safe_load(file)
+
+        # write report
+        report += f"## Original phenotype\n"
+        report += f"  - {old_config['phenotype']['omop']['vocabulary_id']}\n"
+        report += f"  - {old_version}\n"
+        report += f"  - {str(old_phen_path.resolve())}\n"
+        report += f"## Changed phenotype:\n"
+        report += f"  - {new_config['phenotype']['omop']['vocabulary_id']}\n"
+        report += f"  - {new_version}\n"
+        report += f"  - {str(new_phen_path.resolve())}\n"
+
+        
+        # Convert list of dicts into a dict: {name: file}
+        report += diff_config(old_config, new_config)
 
     # Step 2: check differences between map files
     # List files from output directories
@@ -1422,7 +1427,7 @@ def diff_phen(
     _logger.info(f"Phenotypes diff'd successfully")
 
 
-def diff(phen_dir: str, version: str, old_phen_dir: str, old_version: str):
+def diff(phen_dir: str, version: str, old_phen_dir: str, old_version: str, not_check_config:bool):
     # make tmp directory .acmc
     timestamp = time.strftime("%Y%m%d_%H%M%S")
     temp_dir = Path(f".acmc/diff_{timestamp}")
@@ -1483,7 +1488,7 @@ def diff(phen_dir: str, version: str, old_phen_dir: str, old_version: str):
         report_filename = f"{version}_{old_version}_diff.md"
         report_path = changed_phen_path / report_filename
         # diff old with new
-        diff_phen(changed_path, version, old_path, old_version, report_path)
+        diff_phen(changed_path, version, old_path, old_version, report_path, not_check_config)
 
     finally:
         # clean up tmp directory
-- 
GitLab