Skip to content

Commit 5f64982

Browse files
committed
chore: deprecate expansion functional utilities method.
1 parent 5ddd7b4 commit 5f64982

File tree

4 files changed

+215
-10
lines changed

4 files changed

+215
-10
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
**Added:**
2+
3+
* Added ``find_center`` method in ``expansion/shapeutils.py``
4+
* Added ``make_sphere`` method in ``expansion/makeellipsoid.py``
5+
* Added ``make_ellipsoid`` method in ``expansion/makeellipsoid.py``
6+
7+
**Changed:**
8+
9+
* <news item>
10+
11+
**Deprecated:**
12+
13+
* Deprecated ``findCenter`` method in ``expansion/shapeutils.py`` for removal in version 4.0.0
14+
* Deprecated ``makeSphere`` method in ``expansion/makeellipsoid.py`` for removal in version 4.0.0
15+
* Deprecated ``makeEllipsoid`` method in ``expansion/makeellipsoid.py`` for removal in version 4.0.0
16+
17+
**Removed:**
18+
19+
* <news item>
20+
21+
**Fixed:**
22+
23+
* <news item>
24+
25+
**Security:**
26+
27+
* <news item>

src/diffpy/structure/expansion/makeellipsoid.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,36 @@
1919
from numpy import array
2020

2121
from diffpy.structure import Structure
22-
from diffpy.structure.expansion.shapeutils import findCenter
22+
from diffpy.structure.expansion.shapeutils import find_center
23+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
24+
25+
base = "diffpy.structure"
26+
removal_version = "4.0.0"
27+
makeSphere_deprecation_msg = build_deprecation_message(
28+
base,
29+
"makeSphere",
30+
"make_sphere",
31+
removal_version,
32+
)
33+
makeEllipsoid_deprecation_msg = build_deprecation_message(
34+
base,
35+
"makeEllipsoid",
36+
"make_ellipsoid",
37+
removal_version,
38+
)
39+
40+
41+
@deprecated(makeSphere_deprecation_msg)
42+
def makeSphere(S, radius):
43+
"""This function has been deprecated and will be removed in version
44+
4.0.0.
45+
46+
Please use diffpy.structure.make_sphere instead.
47+
"""
48+
return make_sphere(S, radius)
2349

2450

25-
def makeSphere(S, radius):
51+
def make_sphere(S, radius):
2652
"""Create a spherical nanoparticle.
2753
2854
Parameters
@@ -37,10 +63,20 @@ def makeSphere(S, radius):
3763
Structure
3864
A new `Structure` instance.
3965
"""
40-
return makeEllipsoid(S, radius)
66+
return make_ellipsoid(S, radius)
4167

4268

69+
@deprecated(makeEllipsoid_deprecation_msg)
4370
def makeEllipsoid(S, a, b=None, c=None):
71+
"""This function has been deprecated and will be removed in version
72+
4.0.0.
73+
74+
Please use diffpy.structure.make_ellipsoid instead.
75+
"""
76+
return make_ellipsoid(S, a, b, c)
77+
78+
79+
def make_ellipsoid(S, a, b=None, c=None):
4480
"""Cut a `Structure` out of another one.
4581
4682
Parameters
@@ -78,7 +114,7 @@ def makeEllipsoid(S, a, b=None, c=None):
78114
lat = newS.lattice
79115

80116
# Find the central atom
81-
ncenter = findCenter(newS)
117+
ncenter = find_center(newS)
82118

83119
cxyz = lat.cartesian(newS[ncenter].xyz)
84120

@@ -111,17 +147,17 @@ def makeEllipsoid(S, a, b=None, c=None):
111147
datadir = "../../tests/testdata"
112148
S = Structure()
113149
S.read(os.path.join(datadir, "CdSe_bulk.stru"), "pdffit")
114-
newS = makeEllipsoid(S, 12)
150+
newS = make_ellipsoid(S, 12)
115151
newS.write("CdSe_d24.stru", "pdffit")
116-
newS = makeEllipsoid(S, 20, 10, 10)
152+
newS = make_ellipsoid(S, 20, 10, 10)
117153
newS.write("CdSe_a20_b10_c10.stru", "pdffit")
118-
newS = makeEllipsoid(S, 20, 15, 10)
154+
newS = make_ellipsoid(S, 20, 15, 10)
119155
newS.write("CdSe_a20_b15_c10.stru", "pdffit")
120156
S = Structure()
121157
S.read(os.path.join(datadir, "Ni.stru"), "pdffit")
122-
newS = makeEllipsoid(S, 10)
158+
newS = make_ellipsoid(S, 10)
123159
newS.write("Ni_d20.stru", "pdffit")
124-
newS = makeEllipsoid(S, 20, 4)
160+
newS = make_ellipsoid(S, 20, 4)
125161
newS.write("Ni_a20_b4_c20.stru", "pdffit")
126-
newS = makeEllipsoid(S, 20, 15, 10)
162+
newS = make_ellipsoid(S, 20, 15, 10)
127163
newS.write("Ni_a20_b15_c10.stru", "pdffit")

src/diffpy/structure/expansion/shapeutils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,30 @@
1212
# See LICENSE_DANSE.txt for license information.
1313
#
1414
##############################################################################
15+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
16+
17+
base = "diffpy.structure"
18+
removal_version = "4.0.0"
19+
findCenter_deprecation_msg = build_deprecation_message(
20+
base,
21+
"findCenter",
22+
"find_center",
23+
removal_version,
24+
)
1525
"""Utilities for making shapes."""
1626

1727

28+
@deprecated(findCenter_deprecation_msg)
1829
def findCenter(S):
30+
"""This function has been deprecated and will be removed in version
31+
4.0.0.
32+
33+
Please use diffpy.structure.find_center instead.
34+
"""
35+
return find_center(S)
36+
37+
38+
def find_center(S):
1939
"""Find the approximate center `Atom` of a `Structure`.
2040
2141
The center of the `Structure` is the `Atom` closest to ``(0.5, 0.5, 0.5)``.

tests/test_expansion.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env python
2+
##############################################################################
3+
#
4+
# diffpy.structure Complex Modeling Initiative
5+
# (c) 2016 Brookhaven Science Associates,
6+
# Brookhaven National Laboratory.
7+
# All rights reserved.
8+
#
9+
# File coded by: Pavol Juhas
10+
#
11+
# See AUTHORS.rst for a list of people who contributed.
12+
# See LICENSE.rst for license information.
13+
#
14+
##############################################################################
15+
"""Tests for the expansion module utilities."""
16+
import pytest
17+
18+
from diffpy.structure.atom import Atom
19+
from diffpy.structure.expansion.makeellipsoid import make_ellipsoid, make_sphere, makeEllipsoid, makeSphere
20+
from diffpy.structure.expansion.shapeutils import find_center, findCenter
21+
from diffpy.structure.lattice import Lattice
22+
from diffpy.structure.structure import Structure
23+
24+
25+
def test_findCenter():
26+
# C1: We have single atom, expect to return the index of the single atom.
27+
structure_1 = Structure([Atom("Ni", [0.8, 1.2, 0.9])], lattice=Lattice())
28+
expected = 0
29+
actual = findCenter(structure_1)
30+
assert actual == expected
31+
32+
# C2: We have multiple atoms.
33+
# Expect to find the index of the atom which has the closest distance to [0.5, 0.5, 0.5].
34+
# In this case it corresponds to atom "C".
35+
structure_2 = Structure(
36+
[Atom("Ni", [0.8, 1.2, 0.9]), Atom("C", [0.1, 0.2, 0.3])],
37+
lattice=Lattice(),
38+
)
39+
actual = findCenter(structure_2)
40+
expected = 1
41+
assert actual == expected
42+
43+
44+
def test_makeEllipsoid_and_makeSphere():
45+
structure = Structure(
46+
[
47+
Atom("Ni", [0.0, 0.0, 0.0]),
48+
Atom("C", [0.5, 0.5, 0.5]),
49+
Atom("O", [1.0, 0.0, 0.0]),
50+
Atom("H", [1.1, 0.0, 0.0]),
51+
],
52+
lattice=Lattice(1, 1, 1, 90, 90, 90),
53+
)
54+
# C1: set primary, secondary, and polar radius the same in makeEllipsoid, expect to be the same as makeSphere
55+
ellipsoid_1 = makeEllipsoid(structure, 1, 1, 1)
56+
sphere = makeSphere(structure, 1)
57+
assert [atom.element for atom in ellipsoid_1] == [atom.element for atom in sphere]
58+
assert [tuple(atom.xyz) for atom in ellipsoid_1] == [tuple(atom.xyz) for atom in sphere]
59+
# C2: set the radius to be 0.5, expect to exclude the atom that is too far from the center of ellipsoid.
60+
ellipsoid_2 = makeEllipsoid(structure, 0.5)
61+
actual = [(atom.element, tuple(atom.xyz)) for atom in ellipsoid_2]
62+
expected = [("C", (0.5, 0.5, 0.5))]
63+
assert actual == expected
64+
65+
66+
@pytest.mark.parametrize(
67+
"atom_params, expected",
68+
[
69+
pytest.param([Atom("Ni", [0.8, 1.2, 0.9])], 0),
70+
pytest.param([Atom("Ni", [0.8, 1.2, 0.9]), Atom("C", [0.1, 0.2, 0.3])], 1),
71+
],
72+
)
73+
def test_find_center(atom_params, expected):
74+
structure = Structure(atom_params, lattice=Lattice())
75+
actual = find_center(structure)
76+
assert actual == expected
77+
78+
79+
@pytest.mark.parametrize(
80+
("ellipsoid_args", "sphere_radius"),
81+
[
82+
((1, 1, 1), 1),
83+
((0.8, 0.8, 0.8), 0.8),
84+
((0.5, 0.5, 0.5), 0.5),
85+
],
86+
)
87+
def test_make_ellipsoid_equiv_to_make_sphere(ellipsoid_args, sphere_radius):
88+
structure = Structure(
89+
[
90+
Atom("Ni", [0.0, 0.0, 0.0]),
91+
Atom("C", [0.5, 0.5, 0.5]),
92+
Atom("O", [1.0, 0.0, 0.0]),
93+
Atom("H", [1.1, 0.0, 0.0]),
94+
],
95+
lattice=Lattice(1, 1, 1, 90, 90, 90),
96+
)
97+
98+
actual = [(atom.element, tuple(atom.xyz)) for atom in make_ellipsoid(structure, *ellipsoid_args)]
99+
expected = [(atom.element, tuple(atom.xyz)) for atom in make_sphere(structure, sphere_radius)]
100+
101+
assert actual == expected
102+
103+
104+
@pytest.mark.parametrize(
105+
("ellipsoid_args", "expected"),
106+
[
107+
((0.5,), [("C", (0.5, 0.5, 0.5))]),
108+
((0.4,), [("C", (0.5, 0.5, 0.5))]),
109+
],
110+
)
111+
def test_make_ellipsoid(ellipsoid_args, expected):
112+
structure = Structure(
113+
[
114+
Atom("Ni", [0.0, 0.0, 0.0]),
115+
Atom("C", [0.5, 0.5, 0.5]),
116+
Atom("O", [1.0, 0.0, 0.0]),
117+
Atom("H", [1.1, 0.0, 0.0]),
118+
],
119+
lattice=Lattice(1, 1, 1, 90, 90, 90),
120+
)
121+
actual = [(atom.element, tuple(atom.xyz)) for atom in make_ellipsoid(structure, *ellipsoid_args)]
122+
assert actual == expected

0 commit comments

Comments
 (0)