|
| 1 | +import copy |
1 | 2 | import json |
2 | 3 | import re |
3 | 4 | import unittest |
4 | 5 | from pathlib import Path |
5 | 6 |
|
| 7 | +from dargs.dargs import ( |
| 8 | + ArgumentKeyError, |
| 9 | + ArgumentTypeError, |
| 10 | + ArgumentValueError, |
| 11 | +) |
| 12 | + |
6 | 13 | from dpgen.generator.arginfo import run_jdata_arginfo |
7 | 14 | from dpgen.remote.decide_machine import convert_mdata |
8 | 15 | from dpgen.util import normalize |
@@ -88,6 +95,72 @@ def test_linked_parameter_examples_use_current_schema(self): |
88 | 95 | self.assertIsInstance(normalized, dict) |
89 | 96 | self.assertTrue(normalized) |
90 | 97 |
|
| 98 | + def test_documented_parameter_schema_invariants(self): |
| 99 | + repository_root = Path(__file__).resolve().parents[1] |
| 100 | + example = ( |
| 101 | + repository_root |
| 102 | + / "examples" |
| 103 | + / "run" |
| 104 | + / "dp2.x-lammps-cp2k" |
| 105 | + / "param_CH4_deepmd-kit-2.0.1.json" |
| 106 | + ) |
| 107 | + parameter_data = json.loads(example.read_text()) |
| 108 | + normalized = normalize( |
| 109 | + run_jdata_arginfo(), |
| 110 | + copy.deepcopy(parameter_data), |
| 111 | + strict_check=False, |
| 112 | + ) |
| 113 | + |
| 114 | + self.assertEqual(normalized["model_devi_engine"], "lammps") |
| 115 | + self.assertEqual(normalized["train_backend"], "tensorflow") |
| 116 | + |
| 117 | + without_mass_map = copy.deepcopy(parameter_data) |
| 118 | + without_mass_map.pop("mass_map", None) |
| 119 | + normalized_without_mass_map = normalize( |
| 120 | + run_jdata_arginfo(), |
| 121 | + without_mass_map, |
| 122 | + strict_check=False, |
| 123 | + ) |
| 124 | + self.assertEqual(normalized_without_mass_map["mass_map"], "auto") |
| 125 | + |
| 126 | + invalid_fp_style = copy.deepcopy(parameter_data) |
| 127 | + invalid_fp_style["fp_style"] = "none" |
| 128 | + with self.assertRaises(ArgumentValueError): |
| 129 | + normalize( |
| 130 | + run_jdata_arginfo(), |
| 131 | + invalid_fp_style, |
| 132 | + strict_check=False, |
| 133 | + ) |
| 134 | + |
| 135 | + missing_model_devi_skip = copy.deepcopy(parameter_data) |
| 136 | + missing_model_devi_skip.pop("model_devi_skip") |
| 137 | + with self.assertRaises(ArgumentKeyError): |
| 138 | + normalize( |
| 139 | + run_jdata_arginfo(), |
| 140 | + missing_model_devi_skip, |
| 141 | + strict_check=False, |
| 142 | + ) |
| 143 | + |
| 144 | + flat_sys_configs = copy.deepcopy(parameter_data) |
| 145 | + flat_sys_configs["sys_configs"] = ["POSCAR"] |
| 146 | + with self.assertRaises(ArgumentTypeError): |
| 147 | + normalize( |
| 148 | + run_jdata_arginfo(), |
| 149 | + flat_sys_configs, |
| 150 | + strict_check=False, |
| 151 | + ) |
| 152 | + |
| 153 | + for backend in ("tensorflow", "pytorch"): |
| 154 | + with self.subTest(backend=backend): |
| 155 | + backend_data = copy.deepcopy(parameter_data) |
| 156 | + backend_data["train_backend"] = backend |
| 157 | + normalized_backend = normalize( |
| 158 | + run_jdata_arginfo(), |
| 159 | + backend_data, |
| 160 | + strict_check=False, |
| 161 | + ) |
| 162 | + self.assertEqual(normalized_backend["train_backend"], backend) |
| 163 | + |
91 | 164 | def test_linked_machine_examples_use_current_schema(self): |
92 | 165 | repository_root = Path(__file__).resolve().parents[1] |
93 | 166 | examples_root = repository_root / "examples" / "machine" |
|
0 commit comments