From cecaa70e07e6480e504ff584671ff3e53e32f0ce Mon Sep 17 00:00:00 2001 From: Carlo Camilloni Date: Wed, 15 Apr 2026 23:36:25 +0200 Subject: [PATCH 1/4] raise error instead than sysexit --- src/multiego/_run.py | 15 +++++++++------ src/multiego/arguments.py | 32 +++++++++++++++----------------- src/multiego/bonded.py | 10 ++++------ src/multiego/contacts.py | 4 +--- src/multiego/io.py | 36 ++++++++++++++---------------------- src/multiego/lj.py | 17 ++++++++++------- tests/test_multiego.py | 10 +++++----- 7 files changed, 58 insertions(+), 66 deletions(-) diff --git a/src/multiego/_run.py b/src/multiego/_run.py index ad3e094c..0b812991 100644 --- a/src/multiego/_run.py +++ b/src/multiego/_run.py @@ -72,12 +72,7 @@ def meGO_parsing(root_dir): arguments.validate_args(args) - custom_dict = {} - if args.custom_dict: - custom_dict = io.parse_json(args.custom_dict) - if custom_dict is None: - print("ERROR: Custom dictionary was parsed, but the dictionary is empty") - sys.exit() + custom_dict = io.parse_json(args.custom_dict) if args.custom_dict else {} if args.symmetry_file: args.symmetry = io.read_symmetry_file(args.symmetry_file) @@ -104,6 +99,14 @@ def main(root_dir): """ bt = time.time() generate_face.print_welcome() + try: + _main(root_dir, bt) + except (ValueError, RuntimeError, FileNotFoundError) as e: + _term.error(str(e)) + sys.exit(1) + + +def _main(root_dir, bt): args, meGO_ensembles, custom_dict = meGO_parsing(root_dir) st = time.time() diff --git a/src/multiego/arguments.py b/src/multiego/arguments.py index 68ab10f0..b9741699 100644 --- a/src/multiego/arguments.py +++ b/src/multiego/arguments.py @@ -1,5 +1,4 @@ import os -import sys import yaml import argparse from . import io @@ -225,20 +224,22 @@ def read_arguments(args, args_dict, args_dict_global, args_dict_single_reference def validate_args(args): """ - Validates parsed and fully resolved arguments, exiting with a clear - message on any error. Should be called after read_arguments. + Validates parsed and fully resolved arguments. Should be called after read_arguments. Parameters ---------- args : argparse.Namespace Arguments as returned by read_arguments. + + Raises + ------ + ValueError + On any invalid or missing argument. """ if not args.system: - print("ERROR: No system name found! Please provide a system name.") - sys.exit() + raise ValueError("No system name found! Please provide a system name.") if not args.egos: - print("ERROR: No egos mode found! Please provide an egos mode.") - sys.exit() + raise ValueError("No egos mode found! Please provide an egos mode.") for ref in args.input_refs: required_keys = {"matrix", "epsilon", "train", "reference"} @@ -249,26 +250,23 @@ def validate_args(args): empty_required_keys = [key for key in required_keys if not ref[key]] if empty_required_keys: if "train" in empty_required_keys: - print("ERROR: No training simulations found! Please provide a list of training simulations.") - sys.exit() + raise ValueError("No training simulations found! Please provide a list of training simulations.") if "epsilon" in empty_required_keys: - print("ERROR: No epsilon value found! Please provide an epsilon value.") - sys.exit() + raise ValueError("No epsilon value found! Please provide an epsilon value.") raise ValueError(f"Empty values for required keys in {ref}.\nMissing {empty_required_keys}") if ref["epsilon"] < config.epsilon_min: - print(f"ERROR: --epsilon ({ref['epsilon']}) must be greater-equal than epsilon_min ({config.epsilon_min})") - sys.exit() + raise ValueError( + f"--epsilon ({ref['epsilon']}) must be greater-equal than epsilon_min ({config.epsilon_min})" + ) if args.symmetry_file and args.symmetry: - print("ERROR: Both symmetry file and symmetry list provided. Please provide only one.") - sys.exit() + raise ValueError("Both symmetry file and symmetry list provided. Please provide only one.") if args.custom_c12 is not None: custom_c12_dict = io.read_custom_c12_parameters(args.custom_c12) if custom_c12_dict is None or custom_c12_dict.empty: - print("ERROR: Custom c12 parameter file was parsed, but the dictionary is empty") - sys.exit() + raise ValueError("Custom c12 parameter file was parsed, but the dictionary is empty") def read_config(file, args_dict): diff --git a/src/multiego/bonded.py b/src/multiego/bonded.py index 01ab5a95..5d326d4a 100644 --- a/src/multiego/bonded.py +++ b/src/multiego/bonded.py @@ -5,7 +5,6 @@ import networkx as nx import numpy as np from itertools import combinations -import sys def compute_bond_distances(reduced_topology, bond_pair, max_distance=config.max_bond_separation): @@ -128,7 +127,7 @@ def create_pairs_14_dataframe(atomtype1, atomtype2, c6=0.0, shift=0, prefactor=N if prefactor is None and constant is None: raise ValueError("Neither prefactor nor constant has been set.") - _empty = pd.DataFrame(columns=["ai", "aj", "func", "c6", "c12", "probability", "rc_probability", "source"]) + _empty = pd.DataFrame(columns=["ai", "aj", "func", "c6", "c12", "source"]) at1 = atomtype1[["number", "resnum", "c12"]].copy() at2 = atomtype2[["number", "resnum", "c12"]].copy() @@ -351,11 +350,10 @@ def generate_14_data(meGO_ensemble): nonprotein_c12 = [] for test in meGO_ensemble.user_pairs[molecule].type: if test is None: - print( - "\nERROR: you have 1-4 pairs defined in your reference topology without the associated C6/C12 values" + raise RuntimeError( + "1-4 pairs defined in your reference topology without the associated C6/C12 values.\n" + "User-provided 1-4 pairs need to define also the C6/C12." ) - print(" user provided 1-4 pairs need to define also the C6/C12\n") - sys.exit() nonprotein_c12.append(float(test.epsilon) * 4.184) pairs["func"] = 1 diff --git a/src/multiego/contacts.py b/src/multiego/contacts.py index 754b0cd1..f68cbee4 100644 --- a/src/multiego/contacts.py +++ b/src/multiego/contacts.py @@ -29,7 +29,6 @@ import os import pandas as pd import parmed -import sys import time import warnings @@ -868,11 +867,10 @@ def init_meGO_matrices(ensemble, args, custom_dict): difference_set = comparison_set.difference(reference_set) if difference_set: - _term.error( + raise RuntimeError( f"The following atomtypes are not converted:\n {difference_set}\n" f' You MUST add them in "from_ff_to_multiego" dictionary to properly merge all the contacts.' ) - sys.exit() return ensemble, { "reference_matrices": reference_contact_matrices, diff --git a/src/multiego/io.py b/src/multiego/io.py index 929fc3aa..bab0584e 100644 --- a/src/multiego/io.py +++ b/src/multiego/io.py @@ -5,7 +5,6 @@ import os import git import time -import sys import re import json @@ -492,8 +491,7 @@ def get_outdir_name(output_dir, explicit_name, egos): while os.path.exists(f"{output_dir}/{out}_{index}"): index += 1 if index > 100: - _term.error(f"too many directories in {output_dir}") - sys.exit() + raise RuntimeError(f"too many directories in {output_dir}") output_dir = f"{output_dir}/{out}_{index}" return output_dir @@ -748,8 +746,7 @@ def check_files_existence(args): def read_intra_file(file_path): if not os.path.exists(file_path): - _term.error(f"File {file_path} does not exist.") - sys.exit() + raise FileNotFoundError(f"File {file_path} does not exist.") names = [] epsilons = [] @@ -764,8 +761,7 @@ def read_intra_file(file_path): def read_inter_file(file_path): if not os.path.exists(file_path): - _term.error(f"File {file_path} does not exist.") - sys.exit() + raise FileNotFoundError(f"File {file_path} does not exist.") with open(file_path, "r") as file: lines = file.readlines() @@ -776,19 +772,17 @@ def read_inter_file(file_path): # Check that the names are consistent on rows and columns (avoid mistakes) if np.any(names_row != names_col): - _term.error( + raise ValueError( f"the names are inconsistent in the inter epsilon matrix:\n" f" Rows: {names_row}\n" f" Columns: {names_col}\n" f" Please fix to avoid silly mistakes." ) - sys.exit() epsilons = [line.split()[1:] for line in lines[1:]] epsilons = np.array(epsilons, dtype=float) if np.any(epsilons != epsilons.T): - _term.error(f"the matrix of inter epsilon must be symmetric, check the input file {file_path}") - sys.exit() + raise ValueError(f"the matrix of inter epsilon must be symmetric, check the input file {file_path}") return names_row, epsilons @@ -797,18 +791,16 @@ def read_custom_c12_parameters(file): def parse_json(file_path): - if file_path: - try: - with open(file_path, "r") as file: - custom_dict = json.load(file) - if not isinstance(custom_dict, dict): - raise ValueError("Error in reading the custom dictionary: Invalid dictionary format") - return custom_dict - except (json.JSONDecodeError, ValueError) as e: - _term.error(f"Error in reading the custom dictionary: {e}") - sys.exit() - else: + if not file_path: return {} + try: + with open(file_path, "r") as file: + custom_dict = json.load(file) + if not isinstance(custom_dict, dict): + raise ValueError("Invalid dictionary format") + return custom_dict + except (json.JSONDecodeError, ValueError) as e: + raise ValueError(f"Error reading custom dictionary {file_path}: {e}") from e def sort_LJ(meGO_ensemble, meGO_LJ): diff --git a/src/multiego/lj.py b/src/multiego/lj.py index b93f46aa..6dc752d1 100644 --- a/src/multiego/lj.py +++ b/src/multiego/lj.py @@ -8,7 +8,6 @@ import numpy as np import pandas as pd import itertools -import sys import time @@ -278,12 +277,14 @@ def init_LJ_datasets(meGO_ensemble, matrices, args): ) if (np.abs(temp_merged["rc_cutoff"] - temp_merged["cutoff"])).max() > 0: - print( + detail = ( temp_merged[["ai", "aj", "rc_ai", "rc_aj", "source", "rc_source", "cutoff", "rc_cutoff", "same_chain"]] .loc[(np.abs(temp_merged["rc_cutoff"] - temp_merged["cutoff"]) > 0)] .to_string() ) - sys.exit("ERROR: Inconsistent cutoff values between the TRAINING and corresponding REFERENCE input data") + raise RuntimeError( + f"Inconsistent cutoff values between the TRAINING and corresponding REFERENCE input data\n{detail}" + ) # Restrict the check to contacts that appear in both matrices: the outer # merge leaves rc_same_chain = NaN for training contacts that have no @@ -298,8 +299,10 @@ def init_LJ_datasets(meGO_ensemble, matrices, args): ) if mismatched.any(): diff_indices = temp_merged.index[mismatched].tolist() - print(f"Difference found at indices: {diff_indices}") - sys.exit("ERROR: You are pairing intra and inter molecular training and reference data") + raise RuntimeError( + f"You are pairing intra and inter molecular training and reference data\n" + f"Mismatched indices: {diff_indices}" + ) chunks.append(temp_merged[td_fields]) @@ -448,12 +451,12 @@ def init_LJ_datasets(meGO_ensemble, matrices, args): train_dataset = train_dataset.loc[train_dataset["rep"] > 0.0] if (np.abs(train_dataset["cutoff"] - 1.45 * train_dataset["rep"] ** (1 / 12))).max() > 1e-5: - print( + detail = ( train_dataset[["ai", "aj", "source", "same_chain", "cutoff", "rep"]] .loc[(np.abs(train_dataset["cutoff"] - 1.45 * train_dataset["rep"] ** (1 / 12)) > 1e-5)] .to_string() ) - sys.exit("ERROR: Inconsistent cutoff and C12 repulsive values") + raise RuntimeError(f"Inconsistent cutoff and C12 repulsive values\n{detail}") return train_dataset diff --git a/tests/test_multiego.py b/tests/test_multiego.py index 61ac6387..cb7b01a8 100644 --- a/tests/test_multiego.py +++ b/tests/test_multiego.py @@ -549,18 +549,18 @@ def test_valid_args_pass(self): self.validate(_make_args(system="GB1", egos="production", input_refs=[self._valid_ref()])) def test_missing_system_exits(self): - with pytest.raises(SystemExit): + with pytest.raises(ValueError, match="No system name found"): self.validate(_make_args(system="", egos="production", input_refs=[])) def test_missing_egos_exits(self): - with pytest.raises(SystemExit): + with pytest.raises(ValueError, match="No egos mode found"): self.validate(_make_args(system="GB1", egos=None, input_refs=[])) def test_epsilon_below_epsilon_min_exits(self): - """ref epsilon below config.epsilon_min (0.07) must trigger sys.exit.""" + """ref epsilon below config.epsilon_min (0.07) must raise ValueError.""" ref = self._valid_ref() ref["epsilon"] = 0.01 # below the default config.epsilon_min of 0.07 - with pytest.raises(SystemExit): + with pytest.raises(ValueError, match="epsilon_min"): self.validate(_make_args(system="GB1", egos="production", input_refs=[ref])) def test_missing_required_ref_key_raises(self): @@ -569,7 +569,7 @@ def test_missing_required_ref_key_raises(self): self.validate(_make_args(system="GB1", egos="production", input_refs=[bad_ref])) def test_symmetry_conflict_exits(self): - with pytest.raises(SystemExit): + with pytest.raises(ValueError, match="symmetry"): self.validate( _make_args( system="GB1", From 10663c965325dd50392758b13f89b87ef05b33f5 Mon Sep 17 00:00:00 2001 From: Carlo Camilloni Date: Thu, 16 Apr 2026 10:00:03 +0200 Subject: [PATCH 2/4] codeql and file rename --- .github/workflows/codeql.yml | 62 +++++++++++++++++++++++++++++++ src/multiego/_run.py | 2 +- src/multiego/arguments.py | 2 +- src/multiego/contacts.py | 2 +- src/multiego/ensemble_data.py | 2 +- src/multiego/{io.py => fileio.py} | 0 src/multiego/lj.py | 4 +- tests/conftest.py | 4 +- tests/test_make_mat_unit.py | 4 +- tests/test_multiego.py | 2 +- 10 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/codeql.yml rename src/multiego/{io.py => fileio.py} (100%) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 00000000..d9d9a427 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,62 @@ +name: CodeQL + +on: + push: + paths: + - 'tools/cmdata/**' + pull_request: + paths: + - 'tools/cmdata/**' + schedule: + - cron: '0 4 * * 1' + +permissions: + contents: read + security-events: write + actions: write + +jobs: + analyze-cpp: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: c-cpp + # Disable default queries that expect Python/JS; use the standard C++ suite. + queries: security-and-quality + + - name: Get GROMACS commit hash + id: gmx-hash + run: | + echo "hash=$(git ls-remote https://github.com/multi-ego/gromacs.git refs/heads/release-2025 | cut -f1)" >> $GITHUB_OUTPUT + + - name: Cache GROMACS installation + id: cache-gromacs + uses: actions/cache@v4 + with: + path: /usr/local/gromacs + key: gromacs-release-2025-${{ steps.gmx-hash.outputs.hash }} + + - name: Build GROMACS + if: steps.cache-gromacs.outputs.cache-hit != 'true' + run: | + git clone --depth 1 -b release-2025 https://github.com/multi-ego/gromacs.git + cd gromacs; mkdir build; cd build + cmake .. -DGMX_FFT_LIBRARY=fftpack -DBUILD_TESTING=OFF -DGMX_INSTALL_LEGACY_API=ON -DGMX_IMD=OFF + make -j2 + sudo make install + + - name: Build cmdata + run: | + cd tools/cmdata; mkdir build; cd build + cmake .. + make + + - name: Perform CodeQL analysis + uses: github/codeql-action/analyze@v3 + with: + category: c-cpp diff --git a/src/multiego/_run.py b/src/multiego/_run.py index 0b812991..a4b72121 100644 --- a/src/multiego/_run.py +++ b/src/multiego/_run.py @@ -13,7 +13,7 @@ from multiego import arguments from multiego import contacts from multiego import generate_face -from multiego import io +from multiego import fileio as io from multiego import lj from multiego import mg from multiego import pairs diff --git a/src/multiego/arguments.py b/src/multiego/arguments.py index b9741699..f81efada 100644 --- a/src/multiego/arguments.py +++ b/src/multiego/arguments.py @@ -1,7 +1,7 @@ import os import yaml import argparse -from . import io +from . import fileio as io from .model_config import config # Arguments shared between the single-reference CLI mode and the config-file (global) mode. diff --git a/src/multiego/contacts.py b/src/multiego/contacts.py index f68cbee4..828e50a7 100644 --- a/src/multiego/contacts.py +++ b/src/multiego/contacts.py @@ -19,7 +19,7 @@ Private helpers are prefixed with ``_`` and are not part of the public API. """ -from . import io +from . import fileio as io from . import type_definitions from . import _term from .model_config import config diff --git a/src/multiego/ensemble_data.py b/src/multiego/ensemble_data.py index ffd5e291..2787c862 100644 --- a/src/multiego/ensemble_data.py +++ b/src/multiego/ensemble_data.py @@ -24,7 +24,7 @@ import pandas as pd import parmed -from . import io +from . import fileio as io from . import topology as _topology from . import type_definitions from ._parmed_compat import gmxlib_in_topo_dir diff --git a/src/multiego/io.py b/src/multiego/fileio.py similarity index 100% rename from src/multiego/io.py rename to src/multiego/fileio.py diff --git a/src/multiego/lj.py b/src/multiego/lj.py index 6dc752d1..9ccec12f 100644 --- a/src/multiego/lj.py +++ b/src/multiego/lj.py @@ -3,7 +3,7 @@ from . import bonded from . import _term from .model_config import config -from . import io +from . import fileio as io import numpy as np import pandas as pd @@ -113,7 +113,7 @@ def set_sig_epsilon(meGO_LJ, parameters): & (meGO_LJ["rc_probability"] > meGO_LJ["md_threshold"]) ) meGO_LJ.loc[condition, "epsilon"] = (-meGO_LJ["rep"] * (meGO_LJ["distance"] / meGO_LJ["rc_distance"]) ** 12).clip( - lower=-20 * meGO_LJ["rep"], upper=-0.05 * meGO_LJ["rep"] + lower=-2 * meGO_LJ["rep"], upper=-0.05 * meGO_LJ["rep"] )[condition] meGO_LJ.loc[condition, "learned"] = 1 diff --git a/tests/conftest.py b/tests/conftest.py index 48e67705..11db104d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -57,8 +57,8 @@ def stub_deps(): MeGOEnsemble=type("MeGOEnsemble", (), {}), ) - # multiego.io — no attributes needed at import time - stubs["multiego.io"] = _install_stub("multiego.io") + # multiego.fileio — no attributes needed at import time + stubs["multiego.fileio"] = _install_stub("multiego.fileio") # parmed — third-party, may not be installed in all test environments stubs["parmed"] = _install_stub("parmed") diff --git a/tests/test_make_mat_unit.py b/tests/test_make_mat_unit.py index 630bc454..78045c91 100644 --- a/tests/test_make_mat_unit.py +++ b/tests/test_make_mat_unit.py @@ -73,8 +73,8 @@ def _numpy_logsumexp(a, b=None, **_kw): _bonded = _stub("multiego.bonded") _multiego.bonded = _bonded -_io_stub = _stub("multiego.io") -_multiego.io = _io_stub +_io_stub = _stub("multiego.fileio") +_multiego.fileio = _io_stub # --------------------------------------------------------------------------- # Import the module under test via file path diff --git a/tests/test_multiego.py b/tests/test_multiego.py index cb7b01a8..7c9b8602 100644 --- a/tests/test_multiego.py +++ b/tests/test_multiego.py @@ -535,7 +535,7 @@ class TestValidateArgs: @pytest.fixture(autouse=True) def load_module(self, stub_deps): # Give the io stub the specific attribute validate_args needs - stub_deps["multiego.io"].read_custom_c12_parameters = lambda path: pd.DataFrame( + stub_deps["multiego.fileio"].read_custom_c12_parameters = lambda path: pd.DataFrame( {"name": ["CH2"], "c12": [1e-5]} ) from multiego.arguments import validate_args From 837f650f0bd355624a940463bad3bd9a3d49758a Mon Sep 17 00:00:00 2001 From: Carlo Camilloni Date: Thu, 16 Apr 2026 10:03:47 +0200 Subject: [PATCH 3/4] fix --- tools/make_mat/make_mat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/make_mat/make_mat.py b/tools/make_mat/make_mat.py index 4d445331..b68cc9fa 100644 --- a/tools/make_mat/make_mat.py +++ b/tools/make_mat/make_mat.py @@ -17,7 +17,7 @@ from multiego import bonded from multiego import type_definitions -from multiego import io +from multiego import fileio as io _DEFAULT_C12 = { type_definitions.gromos_atp.name[i]: type_definitions.gromos_atp.rc_c12[i] From 32a9e448aebda456212a8675781903ebc3ee8469 Mon Sep 17 00:00:00 2001 From: Carlo Camilloni Date: Thu, 16 Apr 2026 10:06:13 +0200 Subject: [PATCH 4/4] revert an lj change that will be applied later --- src/multiego/lj.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/multiego/lj.py b/src/multiego/lj.py index 9ccec12f..311f7d87 100644 --- a/src/multiego/lj.py +++ b/src/multiego/lj.py @@ -113,7 +113,7 @@ def set_sig_epsilon(meGO_LJ, parameters): & (meGO_LJ["rc_probability"] > meGO_LJ["md_threshold"]) ) meGO_LJ.loc[condition, "epsilon"] = (-meGO_LJ["rep"] * (meGO_LJ["distance"] / meGO_LJ["rc_distance"]) ** 12).clip( - lower=-2 * meGO_LJ["rep"], upper=-0.05 * meGO_LJ["rep"] + lower=-20 * meGO_LJ["rep"], upper=-0.05 * meGO_LJ["rep"] )[condition] meGO_LJ.loc[condition, "learned"] = 1