diff --git a/news/set-structure-matching-name.rst b/news/set-structure-matching-name.rst new file mode 100644 index 0000000..ce07225 --- /dev/null +++ b/news/set-structure-matching-name.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* ``PDFGenerator.set_structure`` and ``DebyePDFGenerator.set_structure`` accept a ``name`` given with a ParameterSet when it matches the ParameterSet's own name, as diffpy.cmirietveld's ``set_structure`` does. Only a different name raises a ``ValueError``. + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/cmipdf/basepdfgenerator.py b/src/diffpy/cmipdf/basepdfgenerator.py index f81946b..64fc76f 100644 --- a/src/diffpy/cmipdf/basepdfgenerator.py +++ b/src/diffpy/cmipdf/basepdfgenerator.py @@ -280,8 +280,8 @@ def set_structure(self, structure, name=None, periodic=True): diffpy.cmistructure ParameterSet that adapts a structure. name : str, optional The name to give to the ParameterSet made for a structure object - or a path (default "phase"). It can't be given with a - ParameterSet. + or a path (default "phase"). With a ParameterSet, it must match + the ParameterSet's own name. periodic : bool, optional The structure should be treated as periodic (default True). Note that some structures do not support @@ -291,10 +291,10 @@ def set_structure(self, structure, name=None, periodic=True): Raises ------ ValueError - If a ``name`` is given with a ParameterSet. + If a ParameterSet is given with a ``name`` other than its own. """ if isinstance(structure, BaseStructureParSet): - if name is not None: + if name is not None and name != structure.name: raise ValueError( f"set_structure was given the name '{name}' with a " "ParameterSet, which keeps its own name, " diff --git a/src/diffpy/cmipdf/debyepdfgenerator.py b/src/diffpy/cmipdf/debyepdfgenerator.py index 03fb980..fb8c209 100644 --- a/src/diffpy/cmipdf/debyepdfgenerator.py +++ b/src/diffpy/cmipdf/debyepdfgenerator.py @@ -104,8 +104,8 @@ def set_structure(self, structure, name=None, periodic=False): diffpy.cmistructure ParameterSet that adapts a structure. name : str, optional The name to give to the ParameterSet made for a structure object - or a path (default "phase"). It can't be given with a - ParameterSet. + or a path (default "phase"). With a ParameterSet, it must match + the ParameterSet's own name. periodic : bool, optional The structure should be treated as periodic (default False). Note that some structures do not support @@ -115,7 +115,7 @@ def set_structure(self, structure, name=None, periodic=False): Raises ------ ValueError - If a ``name`` is given with a ParameterSet. + If a ParameterSet is given with a ``name`` other than its own. """ return BasePDFGenerator.set_structure(self, structure, name, periodic) diff --git a/tests/test_generators.py b/tests/test_generators.py index 75222f5..4b929a4 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -109,7 +109,8 @@ def test_set_structure_from_parset( @pytest.mark.parametrize( - "input_generator_class, expected_using_symmetry", + "input_generator_class, input_set_structure_kwargs, " + "expected_using_symmetry", [ # Test setting the structure from an existing ParameterSet with # set_structure, as a user does to share one structure between @@ -118,20 +119,27 @@ def test_set_structure_from_parset( # Expected: The generator manages that same ParameterSet under its # own name, ni, and it uses symmetry, as PDFGenerator treats a # structure as periodic by default. - (PDFGenerator, True), + (PDFGenerator, {}, True), # C2: A user gives the same kind of ParameterSet to a # DebyePDFGenerator. # Expected: The same, except that it does not use symmetry, as # DebyePDFGenerator treats a structure as not periodic by default. - (DebyePDFGenerator, False), + (DebyePDFGenerator, {}, False), + # C3: A user gives the ParameterSet named ni to a PDFGenerator, + # with the same name, ni. + # Expected: The same as without the name, since the names match. + (PDFGenerator, {"name": "ni"}, True), ], ) def test_set_structure_with_parameter_set( - ni_pdffit_structure, input_generator_class, expected_using_symmetry + ni_pdffit_structure, + input_generator_class, + input_set_structure_kwargs, + expected_using_symmetry, ): parameter_set = DiffpyStructureParSet("ni", ni_pdffit_structure) generator = input_generator_class() - generator.set_structure(parameter_set) + generator.set_structure(parameter_set, **input_set_structure_kwargs) actual_managed_phase = generator.get("ni") actual_using_symmetry = parameter_set.using_symmetry() expected_managed_phase = parameter_set @@ -139,9 +147,9 @@ def test_set_structure_with_parameter_set( assert actual_using_symmetry == expected_using_symmetry -def test_set_structure_with_parameter_set_and_name(ni_pdffit_structure): - # Test giving set_structure a name with a ParameterSet, which already - # has its own name +def test_set_structure_with_parameter_set_and_other_name(ni_pdffit_structure): + # Test giving set_structure a name with a ParameterSet that has another + # name of its own # Expected: A ValueError says the name can't be used and how to name # the ParameterSet instead. parameter_set = DiffpyStructureParSet("phase", ni_pdffit_structure)