Skip to content
Merged

Clean #676

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
62 changes: 62 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 10 additions & 7 deletions src/multiego/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
34 changes: 16 additions & 18 deletions src/multiego/arguments.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import os
import sys
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.
Expand Down Expand Up @@ -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"}
Expand All @@ -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):
Expand Down
10 changes: 4 additions & 6 deletions src/multiego/bonded.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/multiego/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +29,6 @@
import os
import pandas as pd
import parmed
import sys
import time
import warnings

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/multiego/ensemble_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 14 additions & 22 deletions src/multiego/io.py → src/multiego/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import os
import git
import time
import sys
import re
import json

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand All @@ -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()
Expand All @@ -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


Expand All @@ -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):
Expand Down
19 changes: 11 additions & 8 deletions src/multiego/lj.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
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
import itertools
import sys
import time


Expand Down Expand Up @@ -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
Expand All @@ -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])

Expand Down Expand Up @@ -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

Expand Down
Loading
Loading