FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

chore: deprecated addNewAtom by stevenhua0320 · Pull Request #157 · diffpy/diffpy.structure · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (9) .rst  (1) .txt  (2) All 3 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 23 additions & 0 deletions news/deprecate-addNewItem.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Added `diffpy.structure.Structure.add_new_atom` in replace of `addNewAtom`

**Changed:**

* <news item>

**Deprecated:**

* Deprecated `diffpy.structure.Structure.addNewAtom` method for removal in version 4.0.0

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
1 change: 1 addition & 0 deletions requirements/conda.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numpy
pycifrw
diffpy.utils
1 change: 1 addition & 0 deletions requirements/pip.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numpy
pycifrw
diffpy.utils
2 changes: 1 addition & 1 deletion src/diffpy/structure/parsers/p_cif.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def _parse_atom_site_label(self, block):
if curlabel == "?":
continue
self.labelindex[curlabel] = len(self.stru)
self.stru.addNewAtom()
self.stru.add_new_atom()
a = self.stru.getLastAtom()
for fset, val in zip(prop_setters, values):
fset(a, val)
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/structure/parsers/p_discus.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def _parse_atom(self, words):
element = words[0][0:1].upper() + words[0][1:].lower()
xyz = [float(w) for w in words[1:4]]
Biso = float(words[4])
self.stru.addNewAtom(element, xyz)
self.stru.add_new_atom(element, xyz)
a = self.stru.getLastAtom()
a.Bisoequiv = Biso
return
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/structure/parsers/p_pdb.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def parseLines(self, lines):
# get element from the first 2 characters of name
element = line[12:14].strip()
element = element[0].upper() + element[1:].lower()
stru.addNewAtom(element, occupancy=occupancy, label=name)
stru.add_new_atom(element, occupancy=occupancy, label=name)
last_atom = stru.getLastAtom()
last_atom.xyz_cartn = rc
last_atom.Uisoequiv = uiso
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/structure/parsers/p_pdffit.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def parseLines(self, lines):
element = wl1[0][0].upper() + wl1[0][1:].lower()
xyz = [float(w) for w in wl1[1:4]]
occ = float(wl1[4])
stru.addNewAtom(element, xyz=xyz, occupancy=occ)
stru.add_new_atom(element, xyz=xyz, occupancy=occ)
a = stru.getLastAtom()
p_nl += 1
wl2 = next(ilines).split()
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/structure/parsers/p_rawxyz.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def parseLines(self, lines):
xyz = [float(f) for f in fields[x_idx : x_idx + 3]]
if len(xyz) == 2:
xyz.append(0.0)
stru.addNewAtom(element, xyz=xyz)
stru.add_new_atom(element, xyz=xyz)
except ValueError:
emsg = "%d: invalid number" % p_nl
exc_type, exc_value, exc_traceback = sys.exc_info()
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/structure/parsers/p_xcfg.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def parseLines(self, lines):
elif len(words) == xcfg_entry_count and p_element is not None:
fields = [float(w) for w in words]
xyz = [xcfg_A * xi for xi in fields[:3]]
stru.addNewAtom(p_element, xyz=xyz)
stru.add_new_atom(p_element, xyz=xyz)
a = stru[-1]
_assign_auxiliaries(
a,
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/structure/parsers/p_xyz.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def parseLines(self, lines):
element = fields[0]
element = element[0].upper() + element[1:].lower()
xyz = [float(f) for f in fields[1:4]]
stru.addNewAtom(element, xyz=xyz)
stru.add_new_atom(element, xyz=xyz)
except ValueError:
exc_type, exc_value, exc_traceback = sys.exc_info()
emsg = "%d: invalid number format" % p_nl
Expand Down
38 changes: 36 additions & 2 deletions src/diffpy/structure/structure.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,26 @@
"""This module defines class `Structure`."""

import copy as copymod
import warnings

import numpy

from diffpy.structure.atom import Atom
from diffpy.structure.lattice import Lattice
from diffpy.structure.utils import _linkAtomAttribute, atomBareSymbol, isiterable
from diffpy.utils._deprecator import build_deprecation_message, deprecated

# ----------------------------------------------------------------------------

base = "diffpy.structure.Structure"
removal_version = "4.0.0"
addNewAtom_deprecation_msg = build_deprecation_message(
base,
"addNewAtom",
"add_new_atom",
removal_version,
)


class Structure(list):
"""Define group of atoms in a specified lattice. Structure --> group
Expand Down Expand Up @@ -145,17 +156,40 @@ def __str__(self):
s_atoms = "\n".join([str(a) for a in self])
return s_lattice + "\n" + s_atoms

@deprecated(addNewAtom_deprecation_msg)
def addNewAtom(self, *args, **kwargs):
"""This function has been deprecated and will be removed in
version 4.0.0.

Please use diffpy.structure.Structure.add_new_atom instead.
"""
self.add_new_atom(*args, **kwargs)
return

def add_new_atom(self, *args, **kwargs):
"""Add new `Atom` instance to the end of this `Structure`.

Parameters
----------
*args, **kwargs :
See `Atom` class constructor.

Raises

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think this is undesirable behavior. Let's allow the atom to be added. We could raise a warning. But I think it is on the user to get this how they want it. So I would be inclined to simply allow it.

------
UserWarning
If an atom with the same element/type and coordinates already exists.
"""
kwargs["lattice"] = self.lattice
a = Atom(*args, **kwargs)
self.append(a, copy=False)
atom = Atom(*args, **kwargs)
for existing in self:
if existing.element == atom.element and numpy.allclose(existing.xyz, atom.xyz):
warnings.warn(
f"Duplicate atom {atom.element} already exists at {atom.xyz!r}",
category=UserWarning,
stacklevel=2,
)
break
self.append(atom, copy=False)
return

def getLastAtom(self):
Expand Down
69 changes: 69 additions & 0 deletions tests/test_structure.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ def test___copy__(self):
# """check Structure.getLastAtom()"""
# return

def test_addNewAtom(self):
"""Duplicate test for the deprecated addNewAtom method.

Remove this test in version 4.0.0
"""
s_lat = Lattice()
structure = Structure(lattice=s_lat)

length = len(structure)
structure.addNewAtom(atype="C", xyz=[0.1, 0.2, 0.3])
expected = len(structure)
actual = length + 1
assert expected == actual
atom_object = structure[-1]
assert atom_object.element == "C"
assert numpy.allclose(atom_object.xyz, [0.1, 0.2, 0.3])

def test_assignUniqueLabels(self):
"""Check Structure.assignUniqueLabels()"""
self.assertEqual("", "".join([a.label for a in self.stru]))
Expand Down Expand Up @@ -601,7 +618,59 @@ def test_pickling(self):

# End of class TestStructure


# ----------------------------------------------------------------------------
@pytest.mark.parametrize(
"existing, atype, xyz, expected_len, expected_element, expected_xyz",
[
# Case 1: valid atom added to an empty structure.
# Expect the atom list length to go from 0 to 1.
# Expect the atom attributes are successfully loaded.
(
None,
"C",
[0.1, 0.2, 0.3],
1,
"C",
[0.1, 0.2, 0.3],
),
# Case 2: valid atom added to existing atom list.
# Expect the atom list length to go from 1 to 2.
# Expect the atom attributes are successfully loaded.
(
[Atom("C", [0, 0, 0])],
"Ni",
[0.8, 1.2, 0.9],
2,
"Ni",
[0.8, 1.2, 0.9],
),
],
)
def test_add_new_atom(existing, atype, xyz, expected_len, expected_element, expected_xyz):
"""Check Structure.add_new_item()"""
structure = Structure(existing, lattice=Lattice())
structure.add_new_atom(atype=atype, xyz=xyz)
actual_length = len(structure)
assert expected_len == actual_length
atom_object = structure[-1]
assert atom_object.element == expected_element
assert numpy.allclose(atom_object.xyz, expected_xyz)


def test_add_new_atom_duplicate():
# Case 3: duplicated atom added to the existing atom list.
# Expect the atom to be added and gives a UserWarning.
structure = Structure(
[Atom("C", [0.1, 0.2, 0.3]), Atom("Ni", [0.8, 1.2, 0.9])],
lattice=Lattice(),
)
with pytest.warns(UserWarning):
structure.add_new_atom(atype="C", xyz=[0.1, 0.2, 0.3])
assert len(structure) == 3
assert structure[-1].element == "C"
assert numpy.allclose(structure[-1].xyz, [0.1, 0.2, 0.3])


if __name__ == "__main__":
unittest.main()
Loading

Back | FazBrowse Home | New Git URL