Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions CodeEntropy/config/arg_config_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import glob
import logging
import os

Expand Down Expand Up @@ -77,16 +78,26 @@ def __init__(self):
self.arg_map = arg_map

def load_config(self, file_path):
"""Load YAML configuration file."""
if not os.path.exists(file_path):
raise FileNotFoundError(f"Configuration file '{file_path}' not found.")
"""Load YAML configuration file from the given directory."""
yaml_files = glob.glob(os.path.join(file_path, "*.yaml"))

with open(file_path, "r") as file:
config = yaml.safe_load(file)

# If YAML content is empty, return an empty dictionary
if config is None:
config = {}
if not yaml_files:
logger.warning(
f"No YAML configuration files found in directory: {file_path}. "
"Expected a file with extension '.yaml'. "
"Proceeding with default configuration: {'run1': {}}."
)
return {"run1": {}}

try:
with open(yaml_files[0], "r") as file:
config = yaml.safe_load(file)
logger.info(f"Loaded configuration from: {yaml_files[0]}")
if config is None:
config = {"run1": {}}
except Exception as e:
logger.error(f"Failed to load config file: {e}")
config = {"run1": {}}

return config

Expand Down
7 changes: 2 additions & 5 deletions CodeEntropy/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,9 @@ def run_entropy_workflow(self):
try:
logger = self._logging_config.setup_logging()

config = self._config_manager.load_config("config.yaml")
if config is None:
raise ValueError(
"No configuration file found, and no CLI arguments were provided."
)
current_directory = os.getcwd()

config = self._config_manager.load_config(current_directory)
parser = self._config_manager.setup_argparse()
args, _ = parser.parse_known_args()
args.output_file = os.path.join(self.folder, args.output_file)
Expand Down
15 changes: 0 additions & 15 deletions config.yaml

This file was deleted.

52 changes: 36 additions & 16 deletions tests/test_CodeEntropy/test_arg_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,50 @@ def setup_file(self, mock_file):
"water_entropy: False"
).return_value

@patch("builtins.open", new_callable=mock_open)
@patch("os.path.exists", return_value=True)
def test_load_config(self, mock_exists, mock_file):
@patch("builtins.open")
@patch("glob.glob", return_value=["config.yaml"])
def test_load_config(self, mock_glob, mock_file):
"""
Test loading a valid configuration file.
"""
arg_config = ConfigManager()
# Setup the mock file content
self.setup_file(mock_file)
config = arg_config.load_config(self.config_file)

arg_config = ConfigManager()
config = arg_config.load_config("/some/path")

self.assertIn("run1", config)
self.assertEqual(
config["run1"]["top_traj_file"], ["/path/to/tpr", "/path/to/trr"]
)
self.assertEqual(config["run1"]["selection_string"], "all")
self.assertEqual(config["run1"]["start"], 0)
self.assertEqual(config["run1"]["end"], -1)
self.assertEqual(config["run1"]["step"], 1)
self.assertEqual(config["run1"]["bin_width"], 30)
self.assertEqual(config["run1"]["tempra"], 298.0)
self.assertFalse(config["run1"]["verbose"])
self.assertEqual(config["run1"]["thread"], 1)
self.assertEqual(config["run1"]["output_file"], "output_file.json")
self.assertEqual(config["run1"]["force_partitioning"], 0.5)
self.assertFalse(config["run1"]["water_entropy"])

@patch("glob.glob", return_value=[])
def test_load_config_no_yaml_files(self, mock_glob):
arg_config = ConfigManager()
config = arg_config.load_config("/some/path")
self.assertEqual(config, {"run1": {}})

@patch("builtins.open", side_effect=FileNotFoundError)
def test_load_config_file_not_found(self, mock_file):
@patch("glob.glob", return_value=["config.yaml"])
def test_load_config_file_not_found(self, mock_glob, mock_open):
"""
Test loading a configuration file that does not exist.
Test loading a configuration file that exists but cannot be opened.
Should return default config instead of raising an error.
"""
arg_config = ConfigManager()
with self.assertRaises(FileNotFoundError):
arg_config.load_config(self.config_file)
config = arg_config.load_config("/some/path")
self.assertEqual(config, {"run1": {}})

@patch.object(ConfigManager, "load_config", return_value=None)
def test_no_cli_no_yaml(self, mock_load_config):
Expand Down Expand Up @@ -480,19 +502,17 @@ def test_edge_case_argument_values(self, mock_args):
self.assertEqual(args.end, -10)

@patch("builtins.open", new_callable=mock_open, read_data="--- \n")
@patch("os.path.exists", return_value=True)
def test_empty_yaml_config(self, mock_exists, mock_file):
@patch("glob.glob", return_value=["config.yaml"])
def test_empty_yaml_config(self, mock_glob, mock_file):
"""
Test behavior when an empty YAML file is provided.
Should use defaults or raise an appropriate error.
Should return default config {'run1': {}}.
"""

arg_config = ConfigManager()

config = arg_config.load_config(self.config_file)
config = arg_config.load_config("/some/path")

self.assertIsInstance(config, dict)
self.assertEqual(config, {})
self.assertEqual(config, {"run1": {}})

def test_input_parameters_validation_all_valid(self):
"""Test that input_parameters_validation passes with all valid inputs."""
Expand Down