• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CCPBioSim / CodeEntropy / 14059657073

25 Mar 2025 12:31PM UTC coverage: 38.633% (+8.9%) from 29.725%
14059657073

push

github

web-flow
Merge pull request #73 from CCPBioSim/34-implement-python-logging

Implement Python Logging

137 of 211 new or added lines in 9 files covered. (64.93%)

5 existing lines in 1 file now uncovered.

243 of 629 relevant lines covered (38.63%)

1.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.11
/CodeEntropy/config/arg_config_manager.py
1
import argparse
3✔
2
import logging
3✔
3
import os
3✔
4

5
import yaml
3✔
6

7
# Set up logger
8
logger = logging.getLogger(__name__)
3✔
9

10
arg_map = {
3✔
11
    "top_traj_file": {
12
        "type": str,
13
        "nargs": "+",
14
        "help": "Path to Structure/topology file followed by Trajectory file(s)",
15
        "default": [],
16
    },
17
    "selection_string": {
18
        "type": str,
19
        "help": "Selection string for CodeEntropy",
20
        "default": "all",
21
    },
22
    "start": {
23
        "type": int,
24
        "help": "Start analysing the trajectory from this frame index",
25
        "default": 0,
26
    },
27
    "end": {
28
        "type": int,
29
        "help": "Stop analysing the trajectory at this frame index",
30
        "default": -1,
31
    },
32
    "step": {
33
        "type": int,
34
        "help": "Interval between two consecutive frames to be read index",
35
        "default": 1,
36
    },
37
    "bin_width": {
38
        "type": int,
39
        "help": "Bin width in degrees for making the histogram",
40
        "default": 30,
41
    },
42
    "temperature": {
43
        "type": float,
44
        "help": "Temperature for entropy calculation (K)",
45
        "default": 298.0,
46
    },
47
    "verbose": {
48
        "action": "store_true",
49
        "help": "Enable verbose output",
50
    },
51
    "thread": {"type": int, "help": "How many multiprocess to use", "default": 1},
52
    "outfile": {
53
        "type": str,
54
        "help": "Name of the file where the output will be written",
55
        "default": "outfile.json",
56
    },
57
    "mout": {
58
        "type": str,
59
        "help": "Name of the file where certain matrices will be written",
60
        "default": None,
61
    },
62
    "force_partitioning": {"type": float, "help": "Force partitioning", "default": 0.5},
63
    "waterEntropy": {"type": bool, "help": "Calculate water entropy", "default": False},
64
}
65

66

67
class ConfigManager:
3✔
68
    def __init__(self):
3✔
69
        self.arg_map = arg_map
3✔
70

71
    def load_config(self, file_path):
3✔
72
        """Load YAML configuration file."""
73
        if not os.path.exists(file_path):
3✔
NEW
74
            raise FileNotFoundError(f"Configuration file '{file_path}' not found.")
×
75

76
        with open(file_path, "r") as file:
3✔
77
            config = yaml.safe_load(file)
3✔
78

79
            # If YAML content is empty, return an empty dictionary
80
            if config is None:
3✔
81
                config = {}
3✔
82

83
        return config
3✔
84

85
    def setup_argparse(self):
3✔
86
        """Setup argument parsing dynamically based on arg_map."""
87
        parser = argparse.ArgumentParser(
3✔
88
            description="CodeEntropy: Entropy calculation with MCC method."
89
        )
90

91
        for arg, properties in self.arg_map.items():
3✔
92
            kwargs = {key: properties[key] for key in properties if key != "help"}
3✔
93
            parser.add_argument(f"--{arg}", **kwargs, help=properties.get("help"))
3✔
94

95
        return parser
3✔
96

97
    def merge_configs(self, args, run_config):
3✔
98
        """Merge CLI arguments with YAML configuration and adjust logging level."""
99
        if run_config is None:
3✔
NEW
100
            run_config = {}
×
101

102
        if not isinstance(run_config, dict):
3✔
103
            raise TypeError("run_config must be a dictionary or None.")
3✔
104

105
        # Step 1: Merge YAML configuration into args
106
        for key, value in run_config.items():
3✔
107
            if getattr(args, key, None) is None:
3✔
108
                setattr(args, key, value)
3✔
109

110
        # Step 2: Set default values for any missing arguments from `arg_map`
111
        for key, params in self.arg_map.items():
3✔
112
            if getattr(args, key, None) is None:
3✔
113
                setattr(args, key, params.get("default"))
3✔
114

115
        # Step 3: Override with CLI values if provided
116
        for key in self.arg_map.keys():
3✔
117
            cli_value = getattr(args, key, None)
3✔
118
            if cli_value is not None:
3✔
119
                run_config[key] = cli_value
3✔
120

121
        # Adjust logging level based on 'verbose' flag
122
        if getattr(args, "verbose", False):
3✔
NEW
123
            logger.setLevel(logging.DEBUG)
×
NEW
124
            for handler in logger.handlers:
×
NEW
125
                handler.setLevel(logging.DEBUG)
×
NEW
126
            logger.debug("Verbose mode enabled. Logger set to DEBUG level.")
×
127
        else:
128
            logger.setLevel(logging.INFO)
3✔
129
            for handler in logger.handlers:
3✔
NEW
130
                handler.setLevel(logging.INFO)
×
131

132
        return args
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc