Skip to content

Commit a71183b

Browse files
committed
Add mock support for ml workflow
1 parent 46b036f commit a71183b

File tree

7 files changed

+38
-20
lines changed

7 files changed

+38
-20
lines changed

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ filterwarnings = [
7171
"ignore:Could not find 2nd order Makov-Payne energy; applying first order only:UserWarning",
7272
"ignore:Martyna-Tuckerman corrections not applied for an aperiodic calculation; do this with caution:UserWarning",
7373
"ignore:Makov-Payne corrections are not being used; do this with caution for periodic systems:UserWarning",
74-
"ignore:eps_inf missing in input; it will default to 1.0. Proceed with caution for periodic systems:UserWarning",
74+
"ignore:`eps_inf` missing in input; it will default to 1.0. Proceed with caution for periodic systems:UserWarning",
7575
"ignore:Some of the pseudopotentials do not have `PP_PSWFC` blocks, which means a projected DOS calculation is not possible. Skipping...:UserWarning",
7676
"ignore:Neither a pseudopotential library nor a list of pseudopotentials was provided; defaulting to `sg15_v1.2`:UserWarning",
7777
"ignore:The screening parameters for a KI calculation with no empty states will converge instantly; to save computational time set alpha_numsteps == 1:UserWarning",
7878
"ignore:The screening parameters have been calculated but are not necessarily self-consistent. You may want to increase `alpha_numsteps` to obtain a more accurate result.:UserWarning",
7979
"ignore:This system is not cubic and will therefore not have a uniform dielectric tensor. However, the image-correction schemes that are currently implemented assume a uniform dielectric. Proceed with caution:UserWarning",
8080
'ignore:Small box parameters `nrb` not provided in input:UserWarning',
81+
'ignore:The computed screening parameter is greater than 1. Proceed with caution.:UserWarning',
82+
'ignore:Predicting screening parameters with machine-learning is an experimental feature; proceed with caution:UserWarning'
8183
]
8284
addopts = "--show-capture=no --capture=sys --strict-markers --tb=short -rfEs --basetemp=tests/tmp"
8385
markers = [

tests/benchmarks/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
!*.pkl
2+
!??-*/

tests/calculators/test_koopmans_cp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
from koopmans import settings, utils
1010
from koopmans.calculators import KoopmansCPCalculator
11-
from koopmans.calculators._koopmans_cp import allowed, good_fft
11+
from koopmans.calculators._koopmans_cp import (allowed,
12+
convert_flat_alphas_for_kcp,
13+
good_fft)
1214

1315

1416
def test_convert_flat_alphas_for_kcp():

tests/helpers/patches/_benchmark.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from koopmans.processes.wannier import ExtendProcess, MergeProcess
2222

2323
from ._utils import (benchmark_filename, find_subfiles_of_calc,
24-
metadata_filename)
24+
metadata_filename, recursively_find_files)
2525

2626

2727
def patch_calculator(c, monkeypatch):
@@ -84,6 +84,11 @@ def _run(self):
8484
filename.parent.mkdir(parents=True)
8585
write_pkl(self, filename)
8686

87+
# Copy over all files that are outputs of the process that need to be read
88+
for filepointer in recursively_find_files([o for _, o in self.outputs]):
89+
if filepointer.name in ['power_spectrum.npy']:
90+
shutil.copy(filepointer, benchmark_filename(self).parent / filepointer.name)
91+
8792
monkeypatch.setattr(p, '_run', _run)
8893

8994

tests/helpers/patches/_mock.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from koopmans.io import read_pkl
1212
from koopmans.utils import chdir, symlink
1313

14-
from ._utils import benchmark_filename, metadata_filename
14+
from ._utils import (benchmark_filename, metadata_filename,
15+
recursively_find_files)
1516

1617

1718
def write_mock_file(filename: Union[Path, str], written_by: str):
@@ -125,28 +126,18 @@ def generate_dos(self):
125126
monkeypatch.setattr(calc_class, 'generate_dos', generate_dos)
126127

127128

128-
def recursively_find_files(obj):
129-
if isinstance(obj, dict):
130-
for k, v in obj.items():
131-
yield from recursively_find_files(v)
132-
elif isinstance(obj, list):
133-
for v in obj:
134-
yield from recursively_find_files(v)
135-
elif isinstance(obj, FilePointer):
136-
yield obj.aspath()
137-
elif isinstance(obj, Path):
138-
yield obj
139-
return
140-
141-
142129
def mock_process_run(self):
143130
# Load the inputs from file
144131
bench_process = read_pkl(benchmark_filename(self))
145132
assert self.inputs == bench_process.inputs
146133
self.outputs = bench_process.outputs
147134

148135
for f in recursively_find_files([o for _, o in self.outputs]):
149-
write_mock_file(f, self.name)
136+
if f.name in ['power_spectrum.npy']:
137+
assert (benchmark_filename(self).parent / f.name).exists()
138+
shutil.copy(benchmark_filename(self).parent / f.name, f)
139+
else:
140+
write_mock_file(f, self.name)
150141

151142

152143
def monkeypatch_mock(monkeypatch):

tests/helpers/patches/_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from pathlib import Path
33
from typing import Set, Tuple
44

5+
from koopmans.files import FilePointer
6+
57

68
def benchmark_filename(obj) -> Path:
79
base_directory = Path(__file__).parents[3]
@@ -37,3 +39,17 @@ def find_subfiles_of_dir(base_dir: Path) -> Set[Tuple[Path, float]]:
3739
else:
3840
files = set([])
3941
return files
42+
43+
44+
def recursively_find_files(obj):
45+
if isinstance(obj, dict):
46+
for k, v in obj.items():
47+
yield from recursively_find_files(v)
48+
elif isinstance(obj, list):
49+
for v in obj:
50+
yield from recursively_find_files(v)
51+
elif isinstance(obj, FilePointer):
52+
yield obj.aspath()
53+
elif isinstance(obj, Path):
54+
yield obj
55+
return

tests/workflows/test_ml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@pytest.mark.parametrize('descriptor', ['orbital_density', 'self_hartree'])
1313
@pytest.mark.parametrize('estimator', ['mean', 'ridge_regression'])
1414
@pytest.mark.parametrize('occ_and_emp_together', [True, False])
15-
def test_ml_train_water(tmp_path, water_snapshots, descriptor, estimator, occ_and_emp_together):
15+
def test_ml_train_water(tmp_path, workflow_patch, water_snapshots, descriptor, estimator, occ_and_emp_together):
1616
with chdir(tmp_path):
1717
projs = ProjectionBlocks.fromlist([[{'site': 'O', 'ang_mtm': 'sp3'}], [{'site': 'H', 'ang_mtm': 's'}]], spins=[
1818
None, None], atoms=water_snapshots['atoms'])

0 commit comments

Comments
 (0)