Skip to content

Commit 441d31d

Browse files
committed
fix(tests/dpa_adapt): update tests for removed conditions= param and numb_fparam key
- test_conditions.py: replace conditions= kwarg in fit()/predict() calls with fparam_dim=1 constructor param + fparam.npy files, matching the new auto-read behavior introduced in d3c55ee - rename test_predict_unexpected_conditions_raises to test_predict_with_unexpected_fparam_does_not_raise and invert the assertion (silently ignored rather than raising) - fix error match string from "fit with conditions" to "fit with fparam" - test_fparam.py: fix test_trainer_fparam_dim_injected_in_fitting_net to check fn["numb_fparam"] instead of fn["fparam_dim"] (deepmd config key name)
1 parent 159db76 commit 441d31d

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

source/tests/dpa_adapt/test_conditions.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,43 +132,50 @@ def test_fit_with_conditions_changes_feature_dim(self, tmp_path):
132132
system = tmp_path / "sys"
133133
system.mkdir()
134134
_make_npy_system(system, n_frames=4)
135+
np.save(system / "set.000" / "fparam.npy", np.zeros((4, 1)))
135136

136137
with (
137138
patch.object(
138139
DPAFineTuner, "_load_descriptor_model", _mock_load_descriptor_model
139140
),
140141
patch.object(DPAFineTuner, "_extract_features", _mock_extract_features),
141142
):
142-
ft = DPAFineTuner(pretrained="fake.pt", predictor="linear")
143-
cond = {"T": np.array([300.0, 400.0, 500.0, 600.0])}
144-
ft.fit(str(system), target_key="energy", conditions=cond)
143+
ft = DPAFineTuner(pretrained="fake.pt", predictor="linear", fparam_dim=1)
144+
ft.fit(str(system), target_key="energy")
145145

146146
# The pipeline's first step (StandardScaler) reveals the input dim
147147
scaler = ft.predictor.named_steps["standardscaler"]
148148
assert scaler.n_features_in_ == FEAT_DIM + 1
149149

150150
def test_predict_missing_conditions_raises(self, tmp_path):
151-
system = tmp_path / "sys"
152-
system.mkdir()
153-
_make_npy_system(system, n_frames=4)
151+
system_fit = tmp_path / "sys_fit"
152+
system_fit.mkdir()
153+
_make_npy_system(system_fit, n_frames=4)
154+
np.save(system_fit / "set.000" / "fparam.npy", np.zeros((4, 1)))
155+
156+
system_predict = tmp_path / "sys_predict"
157+
system_predict.mkdir()
158+
_make_npy_system(system_predict, n_frames=4)
159+
# No fparam.npy here — should trigger DPAConditionError on predict
154160

155161
with (
156162
patch.object(
157163
DPAFineTuner, "_load_descriptor_model", _mock_load_descriptor_model
158164
),
159165
patch.object(DPAFineTuner, "_extract_features", _mock_extract_features),
160166
):
161-
ft = DPAFineTuner(pretrained="fake.pt", predictor="linear")
162-
cond = {"T": np.array([300.0, 400.0, 500.0, 600.0])}
163-
ft.fit(str(system), target_key="energy", conditions=cond)
167+
ft = DPAFineTuner(pretrained="fake.pt", predictor="linear", fparam_dim=1)
168+
ft.fit(str(system_fit), target_key="energy")
164169

165-
with pytest.raises(DPAConditionError, match="fit with conditions"):
166-
ft.predict(str(system))
170+
with pytest.raises(DPAConditionError, match="fit with fparam"):
171+
ft.predict(str(system_predict))
167172

168-
def test_predict_unexpected_conditions_raises(self, tmp_path):
173+
def test_predict_with_unexpected_fparam_does_not_raise(self, tmp_path):
169174
system = tmp_path / "sys"
170175
system.mkdir()
171176
_make_npy_system(system, n_frames=4)
177+
# fparam.npy present even though model was NOT trained with fparam_dim
178+
np.save(system / "set.000" / "fparam.npy", np.zeros((4, 1)))
172179

173180
with (
174181
patch.object(
@@ -179,30 +186,30 @@ def test_predict_unexpected_conditions_raises(self, tmp_path):
179186
ft = DPAFineTuner(pretrained="fake.pt", predictor="linear")
180187
ft.fit(str(system), target_key="energy")
181188

182-
with pytest.raises(DPAConditionError, match="fit without conditions"):
183-
ft.predict(
184-
str(system), conditions={"T": np.array([1.0, 2.0, 3.0, 4.0])}
185-
)
189+
# fparam.npy is silently ignored when model was fitted without fparam_dim
190+
result = ft.predict(str(system))
191+
192+
assert result.predictions.shape == (4, 1)
186193

187194
def test_freeze_load_with_conditions(self, tmp_path):
188195
system = tmp_path / "sys"
189196
system.mkdir()
190197
_make_npy_system(system, n_frames=4)
198+
np.save(system / "set.000" / "fparam.npy", np.zeros((4, 1)))
191199

192200
with (
193201
patch.object(
194202
DPAFineTuner, "_load_descriptor_model", _mock_load_descriptor_model
195203
),
196204
patch.object(DPAFineTuner, "_extract_features", _mock_extract_features),
197205
):
198-
ft = DPAFineTuner(pretrained="fake.pt", predictor="linear")
199-
cond = {"T": np.array([300.0, 400.0, 500.0, 600.0])}
200-
ft.fit(str(system), target_key="energy", conditions=cond)
206+
ft = DPAFineTuner(pretrained="fake.pt", predictor="linear", fparam_dim=1)
207+
ft.fit(str(system), target_key="energy")
201208

202209
frozen = ft.freeze(str(tmp_path / "model.pth"))
203210

204211
pred = DPAPredictor(frozen)
205-
result = pred.predict(str(system), conditions=cond)
212+
result = pred.predict(str(system))
206213

207214
assert result.predictions.shape == (4, 1)
208215

source/tests/dpa_adapt/test_fparam.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ def test_trainer_fparam_dim_non_int_raises():
7272

7373

7474
def test_trainer_fparam_dim_injected_in_fitting_net():
75-
"""DPATrainer(fparam_dim=3)._build_fitting_net() includes fparam_dim=3."""
75+
"""DPATrainer(fparam_dim=3)._build_fitting_net() includes numb_fparam=3."""
7676
t = _make_dummy_trainer(fparam_dim=3)
7777
fn = t._build_fitting_net()
78-
assert fn["fparam_dim"] == 3
78+
assert fn["numb_fparam"] == 3
7979

8080

8181
def test_trainer_fparam_dim_zero_not_injected():

0 commit comments

Comments
 (0)