|
24 | 24 |
|
25 | 25 | from diffpy.srfit.exceptions import SrFitError |
26 | 26 | from diffpy.srfit.fitbase import ProfileParser |
| 27 | +from diffpy.srfit.fitbase.parameter import Parameter |
| 28 | +from diffpy.srfit.fitbase.recipeorganizer import RecipeContainer |
27 | 29 | from diffpy.srfit.pdf import PDFContribution, PDFGenerator, PDFParser |
28 | 30 |
|
29 | 31 | # ---------------------------------------------------------------------------- |
@@ -321,3 +323,114 @@ def test_pickling( |
321 | 323 |
|
322 | 324 | if __name__ == "__main__": |
323 | 325 | unittest.main() |
| 326 | + |
| 327 | + |
| 328 | +def _make_iterpars_tree(): |
| 329 | + """Build a small hierarchy for iterPars tests.""" |
| 330 | + root = RecipeContainer("root") |
| 331 | + root._containers = {} |
| 332 | + root._manage(root._containers) |
| 333 | + |
| 334 | + root_biso = Parameter("Biso", 10) |
| 335 | + root._add_object(root_biso, root._parameters) |
| 336 | + |
| 337 | + ni0 = RecipeContainer("Ni0") |
| 338 | + ni0_biso = Parameter("Biso", 20) |
| 339 | + ni0_uiso = Parameter("Uiso", 30) |
| 340 | + ni0._add_object(ni0_biso, ni0._parameters) |
| 341 | + ni0._add_object(ni0_uiso, ni0._parameters) |
| 342 | + |
| 343 | + ni1 = RecipeContainer("Ni1") |
| 344 | + ni1_biso = Parameter("Biso", 40) |
| 345 | + ni1._add_object(ni1_biso, ni1._parameters) |
| 346 | + |
| 347 | + o0 = RecipeContainer("O0") |
| 348 | + o0_biso = Parameter("Biso", 50) |
| 349 | + o0._add_object(o0_biso, o0._parameters) |
| 350 | + |
| 351 | + root._add_object(ni0, root._containers) |
| 352 | + root._add_object(ni1, root._containers) |
| 353 | + root._add_object(o0, root._containers) |
| 354 | + |
| 355 | + return { |
| 356 | + "root": root, |
| 357 | + "root_biso": root_biso, |
| 358 | + "ni0": ni0, |
| 359 | + "ni0_biso": ni0_biso, |
| 360 | + "ni0_uiso": ni0_uiso, |
| 361 | + "ni1": ni1, |
| 362 | + "ni1_biso": ni1_biso, |
| 363 | + "o0": o0, |
| 364 | + "o0_biso": o0_biso, |
| 365 | + } |
| 366 | + |
| 367 | + |
| 368 | +@pytest.mark.parametrize( |
| 369 | + ("pattern", "kwargs", "expected_values"), |
| 370 | + [ |
| 371 | + # C1: Match leaf parameter names without fullnames. |
| 372 | + # Expected: all Biso parameters in the hierarchy are returned. |
| 373 | + (r"^Biso$", {}, [10, 20, 40, 50]), |
| 374 | + # C2: Match hierarchical names without fullnames. |
| 375 | + # Expected: no leaf names match the hierarchical pattern. |
| 376 | + (r"^Ni\d+\.Biso$", {}, []), |
| 377 | + # C3: Match hierarchical names with fullnames enabled. |
| 378 | + # Expected: matching Ni Biso parameters are returned. |
| 379 | + (r"^Ni\d+\.Biso$", {"fullnames": True}, [20, 40]), |
| 380 | + # C4: Match one hierarchical Uiso name. |
| 381 | + # Expected: only Ni0.Uiso is returned. |
| 382 | + (r"^Ni0\.Uiso$", {"fullnames": True}, [30]), |
| 383 | + # C5: Match one hierarchical Biso name outside Ni containers. |
| 384 | + # Expected: only O0.Biso is returned. |
| 385 | + (r"^O0\.Biso$", {"fullnames": True}, [50]), |
| 386 | + # C6: Disable recursion while matching child fullnames. |
| 387 | + # Expected: no child parameters are returned. |
| 388 | + (r"^Ni\d+\.Biso$", {"fullnames": True, "recurse": False}, []), |
| 389 | + # C7: Disable recursion while matching root fullname. |
| 390 | + # Expected: only the root-level Biso parameter is returned. |
| 391 | + (r"^Biso$", {"fullnames": True, "recurse": False}, [10]), |
| 392 | + ], |
| 393 | +) |
| 394 | +def test_iterpars_fullname_matching(pattern, kwargs, expected_values): |
| 395 | + """Verify leaf-name and fullname matching in |
| 396 | + iterate_over_parameters.""" |
| 397 | + objs = _make_iterpars_tree() |
| 398 | + root = objs["root"] |
| 399 | + |
| 400 | + actual_values = [ |
| 401 | + parameter.value |
| 402 | + for parameter in root.iterate_over_parameters(pattern, **kwargs) |
| 403 | + ] |
| 404 | + |
| 405 | + assert actual_values == expected_values |
| 406 | + |
| 407 | + |
| 408 | +@pytest.mark.parametrize( |
| 409 | + ("pattern", "expected_name"), |
| 410 | + [ |
| 411 | + # C1: Match Biso relative to the called Ni0 container. |
| 412 | + # Expected: Ni0.Biso is returned without the Ni0 prefix. |
| 413 | + (r"^Biso$", ["Biso"]), |
| 414 | + # C2: Match Uiso relative to the called Ni0 container. |
| 415 | + # Expected: Ni0.Uiso is returned without the Ni0 prefix. |
| 416 | + (r"^Uiso$", ["Uiso"]), |
| 417 | + # C3: Match with the parent container prefix from inside Ni0. |
| 418 | + # Expected: no parameter is returned because fullnames are relative |
| 419 | + # to the container on which iterate_over_parameters is called. |
| 420 | + (r"^Ni0\.Biso$", []), |
| 421 | + ], |
| 422 | +) |
| 423 | +def test_iterpars_fullnames_are_relative_to_called_container( |
| 424 | + pattern, |
| 425 | + expected_name, |
| 426 | +): |
| 427 | + """Verify fullname matching is relative to the called container.""" |
| 428 | + objs = _make_iterpars_tree() |
| 429 | + ni0 = objs["ni0"] |
| 430 | + |
| 431 | + actual_name = [ |
| 432 | + parameter.name |
| 433 | + for parameter in ni0.iterate_over_parameters(pattern, fullnames=True) |
| 434 | + ] |
| 435 | + |
| 436 | + assert actual_name == expected_name |
0 commit comments