Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/set-structure-matching-name.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**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:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
8 changes: 4 additions & 4 deletions src/diffpy/cmipdf/basepdfgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, "
Expand Down
6 changes: 3 additions & 3 deletions src/diffpy/cmipdf/debyepdfgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
24 changes: 16 additions & 8 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -118,30 +119,37 @@ 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
assert actual_managed_phase == expected_managed_phase
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)
Expand Down
Loading