Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce stricter checks (ruff, mypy, and pylint) #55

Merged
merged 27 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
168470a
better typing and fixed a minor bug for cubegen
mcocdawc Dec 10, 2024
3e833ed
explicitly fail when `lo_method == "iao" and iao_val_core`
mcocdawc Dec 10, 2024
6309fcb
kbe should use the molbe be_func and be_func_parallel
mcocdawc Dec 10, 2024
c3a7280
fixed mypy of scratch manager
mcocdawc Dec 10, 2024
9bf111f
fixed molbe_io_fcidump_test
mcocdawc Dec 10, 2024
3645ae1
improved mypy testing
mcocdawc Dec 10, 2024
8d74c81
fixed a wrong import
mcocdawc Dec 10, 2024
a8f687f
more explicit blacklist for tests
mcocdawc Dec 10, 2024
ca5d111
enable the test for mypy
mcocdawc Dec 10, 2024
06296dc
moved write_cube to shared.io
mcocdawc Dec 11, 2024
5b9c13e
fixed import in test
mcocdawc Dec 11, 2024
8540049
bumped up python versions
mcocdawc Dec 11, 2024
fbbcef8
changed quemb unittest to install pytest earlier
mcocdawc Dec 11, 2024
1cfdd0b
actually also install pytest for test :-D
mcocdawc Dec 11, 2024
fce077b
fixed typing issues in tests
mcocdawc Dec 11, 2024
9eccd27
do explicit blacklist in mypy.ini
mcocdawc Dec 11, 2024
64d696e
refactored kbe update_heff
mcocdawc Dec 11, 2024
b8a6782
updated doc and removed some unnecessary commands
mcocdawc Dec 12, 2024
0769f9a
enforce checks for `ruff check` and `pylint`
mcocdawc Dec 12, 2024
133e01c
fixed comments in build yaml files
mcocdawc Dec 12, 2024
d2b9d50
made mypy testing even more beautiful
mcocdawc Dec 12, 2024
2f55fba
fixed small type error in os.environ.get
mcocdawc Dec 12, 2024
03048d1
fixed unsorted imports
mcocdawc Dec 13, 2024
7ff5b35
enable ruff check and redirect stderr in pre-commit hook
mcocdawc Dec 13, 2024
b799cce
do the mypy testing with 3.10
mcocdawc Dec 13, 2024
5f2f573
separated requirements clearly out
mcocdawc Dec 13, 2024
6140dbe
deleted superfluos return
mcocdawc Dec 13, 2024
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
9 changes: 7 additions & 2 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ if [ "`git diff --check --cached | wc -c`" -gt 0 ]; then
fi


if ! command -v ruff 2>&1 >/dev/null; then
if ! command -v ruff >/dev/null 2>&1; then
>&2 echo "Please install ruff via 'pip install ruff'."
exit 1
else
if ! ruff format --diff `git rev-parse --show-toplevel`; then
if ! ruff format --diff `git rev-parse --show-toplevel` >/dev/null 2>&1; then
>&2 echo "There are problems with PEP8-compliance."
>&2 echo 'You can check yourself by executing "ruff format --diff `git rev-parse --show-toplevel`"'
exit 1
fi
if ! ruff check `git rev-parse --show-toplevel` >/dev/null 2>&1; then
>&2 echo "There are problems in the code."
>&2 echo 'You can check yourself by executing "ruff check `git rev-parse --show-toplevel`"'
exit 1
fi
fi


Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.9
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.9"
python-version: "3.10"
cache: 'pip'
- name: Install dependencies
run: |
Expand Down
17 changes: 7 additions & 10 deletions .github/workflows/quemb_unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.12"]
python-version: ["3.10"]


steps:
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:

- name: Install dependencies
run: |
pip install --upgrade --upgrade-strategy eager ruff pylint mypy scipy-stubs
pip install -r tests/static_analysis_requirements.txt
pip install .


Expand All @@ -58,9 +58,8 @@ jobs:


- name: Static analysis with ruff
# for the moment we want to report always report success
run: |
ruff check . || true
ruff check .


- name: Static analysis with pylint
Expand All @@ -69,12 +68,12 @@ jobs:
# TODO: if they add it to ruff as well https://github.com/astral-sh/ruff/issues/9103
# remove pylint.
run: |
pylint --disable=all --enable=E0401,R0401,E0611 . || true # for the moment we want to report always report success
pylint --disable=all --enable=E0401,R0401,E0611 .


- name: Static analysis with mypy
run: |
mypy tests/ example/ src/ || true # for the moment we want to report always report success
mypy tests/ example/ src/



Expand All @@ -83,7 +82,7 @@ jobs:
needs: analysis
strategy:
matrix:
python-version: ["3.9", "3.12"]
python-version: ["3.10", "3.13"]

steps:
- uses: actions/checkout@v4
Expand All @@ -106,14 +105,12 @@ jobs:

- name: Install dependencies
run: |
pip install --upgrade --upgrade-strategy eager pytest
if [ -f requirements.txt ]; then pip install --upgrade --upgrade-strategy eager -r requirements.txt; fi
pip install -r tests/test_requirements.txt
pip install git+https://github.com/pyscf/dmrgscf
PYSCFHOME=$(pip show pyscf-dmrgscf | grep 'Location' | tr ' ' '\n' | tail -n 1)
wget https://raw.githubusercontent.com/pyscf/dmrgscf/master/pyscf/dmrgscf/settings.py.example
mv settings.py.example ${PYSCFHOME}/pyscf/dmrgscf/settings.py
pip install .
echo ${{ github.workspace }} > $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")/quemb.pth


- name: Test with pytest
Expand Down
49 changes: 23 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,32 @@ processors.

### Prerequisites

- Python 3.6 or higher
- Python `>=3.10`
- PySCF library
- Numpy
- Scipy
- libDMET (required for periodic BE)
- [Wannier90](https://github.com/wannier-developers/wannier90)<sup>&&</sup> (to use Wannier functions)

<sup>&&</sup>Wannier90 code is interfaced via [libDMET](https://github.com/gkclab/libdmet_preview) in QuEmb</sub>

### Steps

1. Clone the repository:
```bash
git clone https://github.com/oimeitei/quemb.git
cd quemb

2. Install QuEmb using one of the following approaches:
```bash
pip install .
```
or simply add `path/to/quemd` to `PYTHONPATH`
```bash
export PYTHONPATH=/path/to/quemb:$PYTHONPATH
```

For conda (or virtual environment) installations, after creating your environment, specify the path to mol-be source as a path file, as in:
```bash
echo path/to/quemb > $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")/quemb.pth
```
- [libDMET](https://github.com/gkclab/libdmet_preview) (required for periodic BE)
- [Wannier90](https://github.com/wannier-developers/wannier90)<sup>##</sup> (to use Wannier functions)

<sup>##</sup> `Wannier90` code is optional and only necessary to use Wannier functions in periodic code. </sub>

The required dependencies, with the exception of the optional `Wannier90`,
mcocdawc marked this conversation as resolved.
Show resolved Hide resolved
are automatically installed by `pip`.

### Installation

One can just `pip install` directly from the Github repository:
```bash
pip install git+https://https://github.com/troyvvgroup/quemb
```

Alternatively one can manually clone and install as in:
```bash
git clone https://https://github.com/troyvvgroup/quemb
cd quemb
pip install .
```



## Basic Usage
Expand Down
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# the additional (!) requirements for building the doc
# the dependencies of `quemb` itself are given in the setup.py
sphinx_rtd_theme
sphinx_autodoc_typehints
5 changes: 5 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ References
optimize
solvers
misc



.. role:: bash(code)
:language: bash
39 changes: 17 additions & 22 deletions docs/source/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,34 @@ Installation
Prerequisites
-------------

* Python 3.6 or higher
* Python >=10 or higher
* PySCF library
* Numpy
* Scipy
* libDMET :sup:`##` (required for periodic BE)
* `Wannier90 <https://github.com/wannier-developers/wannier90>`_ :sup:`&&` (to use Wannier functions)
* `libDMET <https://github.com/gkclab/libdmet_preview>`__ (required for periodic BE)
* `Wannier90 <https://github.com/wannier-developers/wannier90>`_ :sup:`##`

| :sup:`##` The modified version of `libDMET <https://github.com/gkclab/libdmet_preview>`_ available at `here <https://github.com/oimeitei/libdmet_preview>`_ is recommended to run periodic BE using QuEmb.
| :sup:`&&` Wannier90 code is interfaced via `libDMET <https://github.com/gkclab/libdmet_preview>`_ in QuEmb
| :sup:`##` :code:`Wannier90` code is optional and only necessary to use Wannier functions in periodic code.

The required dependencies, with the exception of the optional :code:`Wannier90`,
are automatically installed by :bash:`pip`.

Obtain the source code
----------------------
Clone the Github repository::

git clone https://github.com/oimeitei/quemb.git
Installation
-------------

One can just :bash:`pip install` directly from the Github repository

pip install
-----------
.. code-block:: bash

::
pip install git+https://https://github.com/troyvvgroup/quemb

pip install .

Add to ``PYTHONPATH``
---------------------
Simply add ``path/to/quemb`` to ``PYTHONPATH``
::

export PYTHONPATH=/path/to/quemb:$PYTHONPATH
Alternatively one can manually clone and install as in

Conda or virtual environment
----------------------------
For conda (or virtual environment) installations, after creating your environment, specify the path to mol-be source as a path file, as in::
.. code-block:: bash

echo path/to/quemb > $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")/quemb.pth
git clone https://https://github.com/troyvvgroup/quemb
cd quemb
pip install .
3 changes: 3 additions & 0 deletions example/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# the __init__.py file only exists so that we can
# have more fine-grained control over which file we want to test how
# in mypy.
3 changes: 2 additions & 1 deletion example/molbe_io_fcidump.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Illustrates how fcidump file containing fragment hamiltonian
# can be generated using be2fcidump

from quemb.molbe import BE, be_var, fragpart
from quemb.molbe import BE, fragpart
from quemb.molbe.misc import be2fcidump, libint2pyscf
from quemb.shared import be_var

be_var.PRINT_LEVEL = 3

Expand Down
18 changes: 14 additions & 4 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
disallow_untyped_defs = True
check_untyped_defs = True

[mypy-quemb.molbe.*]

# explicitly blacklist files, this means we can easily add in stricter type checks
# by removing them from the blacklist
[mypy-quemb.molbe.ube,quemb.molbe.be_parallel,quemb.molbe.autofrag,quemb.molbe.eri_onthefly,quemb.molbe.fragment,quemb.molbe.helper,quemb.molbe.lchain,quemb.molbe.lo,quemb.molbe.mbe,quemb.molbe.misc,quemb.molbe._opt,quemb.molbe.pfrag,quemb.molbe.solver]
disallow_untyped_defs = False
check_untyped_defs = False

[mypy-quemb.kbe.autofrag,quemb.kbe.chain,quemb.kbe.fragment,quemb.kbe.helper,quemb.kbe.__init__,quemb.kbe.lo_k,quemb.kbe.lo,quemb.kbe.misc,quemb.kbe.pbe,quemb.kbe.pfrag,quemb.kbe.solver]
disallow_untyped_defs = False
check_untyped_defs = False

[mypy-quemb.kbe.*]
[mypy-quemb.shared.external.ccsd_rdm,quemb.shared.external.cphf_utils,quemb.shared.external.cpmp2_utils,quemb.shared.external.__init__,quemb.shared.external.jac_utils,quemb.shared.external.lo_helper,quemb.shared.external.optqn,quemb.shared.external.uccsd_eri,quemb.shared.external.unrestricted_utils]
disallow_untyped_defs = False
check_untyped_defs = False

[mypy-quemb.shared.external.*]
[mypy-tests.chem_dm_kBE_test,tests.chempot_molBE_test,tests.dm_molBE_test,tests.dmrg_molBE_test,tests.eri_onthefly_test,tests.hf-in-hf_BE_test,tests.kbe_polyacetylene_test,tests.molbe_h8_test,tests.molbe_io_fcidump_test,tests.molbe_octane_get_rdms_test,tests.molbe_oneshot_rbe_hcore_test,tests.molbe_oneshot_rbe_qmmm-fromchk_test,tests.ube-oneshot_test]
disallow_untyped_defs = False
check_untyped_defs = False

[mypy-example.kbe_polyacetylene,example.molbe_dmrg_block2,example.molbe_h8_chemical_potential,example.molbe_h8_density_matching,example.molbe_hexene_oneshot_uccsd,example.molbe_io_fcidump,example.molbe_octane_get_rdms,example.molbe_octane,example.molbe_oneshot_rbe_hcore,example.molbe_oneshot_rbe_qmmm-fromchk,example.molbe_oneshot_ube_qmmm,example.molbe_ppp]
disallow_untyped_defs = False
check_untyped_defs = False

Expand All @@ -29,4 +39,4 @@
ignore_missing_imports = True

[mypy-h5py.*]
ignore_missing_imports = True
ignore_missing_imports = True
6 changes: 0 additions & 6 deletions requirements.txt

This file was deleted.

3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
license="Apache 2.0",
packages=find_packages("src"),
package_dir={"": "src"},
python_requires=">=3.7",
python_requires=">=3.10",
install_requires=[
"numpy>=1.22.0",
"scipy>=1.7.0",
"pyscf>=2.0.0",
"matplotlib",
"libdmet @ git+https://github.com/gkclab/libdmet_preview.git",
"attrs",
"typing_extensions",
],
)
2 changes: 1 addition & 1 deletion src/quemb/kbe/lo.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def localize(
Whether to perform Wannier localization in the IAO space
"""
if lo_method == "iao" and iao_val_core:
raise NotImplementedError("This does not work. Contact Developers.")
raise NotImplementedError("iao_val_core and lo_method='iao' not supported.")

if lo_method == "lowdin":
# Lowdin orthogonalization with k-points
Expand Down
3 changes: 1 addition & 2 deletions src/quemb/kbe/pbe.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
from quemb.kbe.misc import print_energy, storePBE
from quemb.kbe.pfrag import Frags
from quemb.molbe._opt import BEOPT
from quemb.molbe.helper import get_eri, get_scfObj, get_veff
from quemb.molbe.be_parallel import be_func_parallel
from quemb.molbe.helper import get_eri, get_scfObj, get_veff
from quemb.molbe.solver import be_func

from quemb.shared import be_var
from quemb.shared.external.optqn import (
get_be_error_jacobian as _ext_get_be_error_jacobian,
Expand Down
41 changes: 13 additions & 28 deletions src/quemb/kbe/pfrag.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,49 +353,34 @@ def update_heff(
self,
u,
cout=None,
return_heff=False,
no_chempot=False,
do_chempot=True,
only_chem=False,
):
"""Update the effective Hamiltonian for the fragment."""

heff_ = numpy.zeros_like(self.h1)

if cout is None:
cout = self.udim

if not no_chempot:
if do_chempot:
for i, fi in enumerate(self.fsites):
if not any(i in sublist for sublist in self.edge_idx):
heff_[i, i] -= u[-1]

if only_chem:
self.heff = heff_
if return_heff:
if cout is None:
return heff_
else:
return (cout, heff_)
return cout

for idx, i in enumerate(self.edge_idx):
for j in range(len(i)):
for k in range(len(i)):
if j > k:
continue

heff_[i[j], i[k]] = u[cout]
heff_[i[k], i[j]] = u[cout]

cout += 1
return
mcocdawc marked this conversation as resolved.
Show resolved Hide resolved
else:
for idx, i in enumerate(self.edge_idx):
for j in range(len(i)):
for k in range(len(i)):
if j > k:
continue
heff_[i[j], i[k]] = u[cout]
heff_[i[k], i[j]] = u[cout]
cout += 1

self.heff = heff_
if return_heff:
if cout is None:
return heff_
else:
return (cout, heff_)
return cout
self.heff = heff_

def set_udim(self, cout):
for i in self.edge_idx:
Expand Down
Loading
Loading