Skip to content

Commit

Permalink
Merge pull request #104 from MolarVerse/dev
Browse files Browse the repository at this point in the history
Merge dev into main for release v1.2
  • Loading branch information
97gamjak authored Jun 11, 2024
2 parents 7ca25d9 + 8915353 commit b4ab7da
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 32 deletions.
Binary file modified .github/.pylint_cache/PQAnalysis_1.stats
Binary file not shown.
9 changes: 3 additions & 6 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,17 @@ jobs:
python .github/scripts/parse_pylint.py pylint_output.txt | tee comment.txt
fi
#check if the first line of comment.txt rates higher than 9.75 with following format
#Your code has been rated at 9.86/10 (previous run: 7.63/10, +2.22)
- name: Check if pylint score is higher than 9.75
id: check_pylint_score
run: |
head -n3 comment.txt | tail -n 1
head -n3 comment.txt | tail -n 1 | awk '{print $7}'
head -n3 comment.txt | tail -n 1 | awk '{print $7}' | cut -d '/' -f 1
score=$(head -n3 comment.txt | tail -n 1 | awk '{print $7}' | cut -d '/' -f 1)
if (( $(echo "$score > 9.5" | bc -l) )); then
echo "Pylint score is higher than 9.5 and is $score"
if (( $(echo "$score > 9.75" | bc -l) )); then
echo "Pylint score is higher than 9.75 and is $score"
else
echo "Pylint score is lower than 9.5 and is $score"
echo "Pylint score is lower than 9.75 and is $score"
exit 1
fi
shell: bash
Expand Down
1 change: 0 additions & 1 deletion PQAnalysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# BEARTYPE SETUP #
##################

# TODO: change the default level to "RELEASE" after all changes are implemented
__beartype_default_level__ = "RELEASE"
__beartype_level__ = os.getenv(
"PQANALYSIS_BEARTYPE_LEVEL", __beartype_default_level__
Expand Down
1 change: 0 additions & 1 deletion PQAnalysis/atomic_system/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
of atoms is equal to the number of positions.
"""

import sys
import numpy as np

from beartype.typing import Any
Expand Down
4 changes: 2 additions & 2 deletions PQAnalysis/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def distance(
pos1: Np1DNumberArray | Np2DNumberArray,
pos2: Np1DNumberArray | Np2DNumberArray,
cell: _cell.Cell = _cell.Cell(),
**kwargs
**kwargs # pylint: disable=unused-argument
) -> PositiveReal | Np1DNumberArray | Np2DNumberArray:
"""
Returns the distances between all combinations of two position arrays.
Expand Down Expand Up @@ -66,7 +66,7 @@ def distance(
cell : Cell, optional
The unit cell of the system. Default is Cell().
**kwargs
Arbitrary keyword arguments.
Additional keyword arguments.
Returns
-------
Expand Down
1 change: 1 addition & 0 deletions PQAnalysis/io/gen_file/gen_file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def _read_header(self,
"""
n_atoms, periodicity = header[0].split()
n_atoms = int(n_atoms)
is_periodic = False # Default value

if periodicity.lower() == "c":
is_periodic = False
Expand Down
3 changes: 3 additions & 0 deletions PQAnalysis/io/input_file_reader/input_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def parse(self) -> "InputDictionary":
InputDictionary: InputDictionary
The parsed input file dictionary.
"""

grammar_file = None # to avoid linter warning

if self.input_format == InputFileFormat.PQANALYSIS:
grammar_file = "inputGrammar.lark"
elif self.input_format in [InputFileFormat.PQ, InputFileFormat.QMCFC]:
Expand Down
2 changes: 0 additions & 2 deletions PQAnalysis/io/input_file_reader/pq/output_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
A module containing a Mixin class for output files of a PQ input file.
"""

from PQAnalysis.exceptions import PQKeyError



class _OutputFileMixin:
Expand Down
18 changes: 12 additions & 6 deletions PQAnalysis/io/nep/nep_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ def write_from_files(

self.is_validation = False

file_to_write = None # to avoid linting warning

if j in train_indices:
self.n_train_frames += 1
file_to_write = self.train_file
Expand All @@ -339,7 +341,11 @@ def write_from_files(
)

self.write_from_atomic_system(
system, file_to_write, use_forces, use_stress, use_virial
system,
file_to_write,
use_forces,
use_stress,
use_virial,
)

if self.is_validation:
Expand Down Expand Up @@ -437,12 +443,16 @@ def _setup_frame_splitting_for_training(
exception=NEPError
)

n_train = 0.0
n_validation = 0.0
n_test = 0.0

if total_ratios is not None:
ratios = total_ratios.split(":")
if len(ratios) == 2:
n_train = float(ratios[0])
n_test = float(ratios[1])
n_validation = 0
n_validation = 0.0
elif len(ratios) == 3:
n_train = float(ratios[0])
n_test = float(ratios[1])
Expand All @@ -456,10 +466,6 @@ def _setup_frame_splitting_for_training(
),
exception=NEPError
)
else:
n_train = 0.0
n_validation = 0.0
n_test = 0.0

sum_frames = n_train + n_test + n_validation

Expand Down
30 changes: 30 additions & 0 deletions PQAnalysis/io/topology_file/topology_file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ def _parse_bonds(self, block: List[str]) -> List[Bond]:

line, comment = self._get_data_line_comment(line)

index = None # to avoid linter warning
target_index = None # to avoid linter warning
bond_type = None # to avoid linter warning
is_linker = None # to avoid linter warning

if len(line.split()) == 4:
index, target_index, bond_type, _ = line.split()
is_linker = True
Expand Down Expand Up @@ -263,6 +268,12 @@ def _parse_angles(self, block: List[str]) -> List[Angle]:

line, comment = self._get_data_line_comment(line)

index1 = None # to avoid linter warning
index2 = None # to avoid linter warning
index3 = None # to avoid linter warning
angle_type = None # to avoid linter warning
is_linker = None # to avoid linter warning

if len(line.split()) == 5:
index1, index2, index3, angle_type, _ = line.split()
is_linker = True
Expand Down Expand Up @@ -318,6 +329,13 @@ def _parse_dihedrals(self, block: List[str]) -> List[Dihedral]:

line, comment = self._get_data_line_comment(line)

index1 = None # to avoid linter warning
index2 = None # to avoid linter warning
index3 = None # to avoid linter warning
index4 = None # to avoid linter warning
dihedral_type = None # to avoid linter warning
is_linker = None # to avoid linter warning

if len(line.split()) == 6:
index1, index2, index3, index4, dihedral_type, _ = line.split()
is_linker = True
Expand Down Expand Up @@ -374,6 +392,13 @@ def _parse_impropers(self, block: List[str]) -> List[Dihedral]:

line, comment = self._get_data_line_comment(line)

index1 = None # to avoid linter warning
index2 = None # to avoid linter warning
index3 = None # to avoid linter warning
index4 = None # to avoid linter warning
dihedral_type = None # to avoid linter warning
is_linker = None # to avoid linter warning

if len(line.split()) == 6:
index1, index2, index3, index4, dihedral_type, _ = line.split()
is_linker = True
Expand Down Expand Up @@ -431,6 +456,11 @@ def _parse_shake(self, block: List[str]) -> List[Bond]:

line, comment = self._get_data_line_comment(line)

index = None # to avoid linter warning
target_index = None # to avoid linter warning
distance = None # to avoid linter warning
is_linker = None # to avoid linter warning

if len(line.split()) == 4:
index, target_index, distance, _ = line.split()
is_linker = True
Expand Down
2 changes: 2 additions & 0 deletions PQAnalysis/io/traj_file/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.so
process_lines.c
16 changes: 6 additions & 10 deletions PQAnalysis/io/traj_file/frame_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from PQAnalysis import __package_name__

from .exceptions import FrameReaderError
from .process_lines import process_lines_with_atoms # pylint: disable=import-error



Expand Down Expand Up @@ -98,7 +99,7 @@ def read(

# This should never happen - only for safety
self.logger.error(
f'Invalid TrajectoryFormat given.{traj_format=}',
f'Invalid TrajectoryFormat given. {traj_format=}',
exception=FrameReaderError
)

Expand Down Expand Up @@ -315,6 +316,9 @@ def _read_header_line(self, header_line: str) -> Tuple[int, Cell]:
If the header line is not valid. Either it contains too many or too few values.
"""

n_atoms = 0 # default value
cell = None # default value

header_line = header_line.split()

if len(header_line) == 4:
Expand Down Expand Up @@ -364,15 +368,7 @@ def _read_xyz(
If the given string does not contain the correct number of lines.
"""
try:
# Pre-allocate xyz and atoms
xyz = np.empty((n_atoms, 3), dtype=np.float32)
atoms = [None] * n_atoms

# Fill xyz and atoms in a single loop
for i, line in enumerate(splitted_frame_string[2:2 + n_atoms]):
split_line = line.split()
atoms[i] = split_line[0]
xyz[i] = split_line[1:4]
atoms, xyz = process_lines_with_atoms(splitted_frame_string[2:], n_atoms)

return xyz, atoms
except ValueError:
Expand Down
94 changes: 94 additions & 0 deletions PQAnalysis/io/traj_file/process_lines.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
This module contains the functions that are used to parse the body of
xyz formatted files. The functions are written in Cython and are compiled
to C code.
"""

import numpy as np
cimport numpy as np
from libc.stdio cimport sscanf

def process_lines_with_atoms(input, int n_atoms):
"""
Function to parse the body of an xyz file. The function expects a list of
strings, where each string is a line in the file. The function will parse
the lines and return a tuple with two elements. The first element is a list
of strings, where each string is the atom type of the corresponding atom in
the xyz file. The second element is a numpy array with the xyz coordinates
of the atoms in the file.
Parameters
----------
input : list
A list of strings, where each string is a line in the xyz file.
n_atoms : int
The number of atoms in the xyz file.
Returns
-------
tuple
A tuple with two elements. The first element is a list of strings, where
each string is the atom type of the corresponding atom in the xyz file.
The second element is a numpy array with the xyz coordinates of the atoms
in the file.
"""

cdef np.ndarray[np.float32_t, ndim=2] xyz = np.zeros((n_atoms, 3), dtype=np.float32)
cdef list atoms = [None] * n_atoms
cdef char[5] atom
cdef float x, y, z
cdef int ret

for i in range(n_atoms):
line = input[i]
line_str = line.encode('utf-8')

ret = sscanf(line_str, "%s %f %f %f", atom, &x, &y, &z)
if ret != 4:
raise ValueError("Could not parse line")

xyz[i, 0] = x
xyz[i, 1] = y
xyz[i, 2] = z

atoms[i] = atom.decode('utf-8')

return atoms, xyz

def process_lines(input, int n_atoms):
"""
Function to parse the body of an xyz file. The function expects a list of
strings, where each string is a line in the file. The function will parse
the lines and return a numpy array with the xyz coordinates of the atoms
in the file.
Parameters
----------
input : list
A list of strings, where each string is a line in the xyz file.
n_atoms : int
The number of atoms in the xyz file.
Returns
-------
numpy.ndarray
A numpy array with the xyz coordinates of the atoms in the file.
"""

cdef np.ndarray[np.float32_t, ndim=2] xyz = np.zeros((n_atoms, 3), dtype=np.float32)
cdef float x, y, z
cdef int ret

for i in range(n_atoms):
line = input[i]
line_str = line.encode('utf-8')

ret = sscanf(line_str, "%*s %f %f %f", &x, &y, &z)
if ret != 3:
raise ValueError("Could not parse line")

xyz[i, 0] = x
xyz[i, 1] = y
xyz[i, 2] = z

return xyz
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=42", "setuptools_scm>=8"]
requires = ["setuptools>=42", "setuptools_scm>=8", "wheel", "numpy", "Cython"]
build-backend = "setuptools.build_meta"

[project]
Expand Down Expand Up @@ -32,16 +32,19 @@ dependencies = [
"argcomplete",
"yapf",
"rich-argparse",
"Cython",
]

[project.optional-dependencies]
test = [
"pytest",
"coverage",
"pytest-cov",
"pytest-runner",
"pylint",
"pylint-django",
"coverage",
"docstr-coverage",
"setuptools",
]
docs = [
"sphinx",
Expand Down
14 changes: 12 additions & 2 deletions pytest.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
export PQANALYSIS_BEARTYPE_LEVEL=DEBUG
python -m pytest $@
#check if $@ is empty
if [ -z "$@" ]; then
python setup.py pytest
else
python setup.py pytest --addopts $@
fi
if [ $? -ne 0 ]; then
exit 1
fi

export PQANALYSIS_BEARTYPE_LEVEL=RELEASE
python -m pytest $@
#check if $@ is empty
if [ -z "$@" ]; then
python setup.py pytest
else
python setup.py pytest --addopts $@
fi
Loading

0 comments on commit b4ab7da

Please sign in to comment.