Skip to content

Commit 358df45

Browse files
committed
chore:deprecate method in and add test for methods
1 parent 42d3828 commit 358df45

File tree

3 files changed

+96
-9
lines changed

3 files changed

+96
-9
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
**Added:**
2+
3+
* Added ``position_difference`` method in ``symmetryutilities.py``
4+
* Added ``nearest_site_index`` method in ``symmetryutilities.py``
5+
* Added ``_find_invariants`` method in ``symmetryutilities.py``
6+
7+
**Changed:**
8+
9+
* <news item>
10+
11+
**Deprecated:**
12+
13+
* Deprecated ``positionDifference`` method in ``symmetryutilities.py`` for removal in version 4.0.0
14+
* Deprecated ``nearestSiteIndex`` method in ``symmetryutilities.py`` for removal in version 4.0.0
15+
16+
**Removed:**
17+
18+
* <news item>
19+
20+
**Fixed:**
21+
22+
* <news item>
23+
24+
**Security:**
25+
26+
* <news item>

src/diffpy/structure/symmetryutilities.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@
4848
"is_constant_formula",
4949
removal_version,
5050
)
51+
positionDifference_deprecation_msg = build_deprecation_message(
52+
base,
53+
"positionDifference",
54+
"position_difference",
55+
removal_version,
56+
)
57+
nearestSiteIndex_deprecation_msg = build_deprecation_message(
58+
base,
59+
"nearestSiteIndex",
60+
"nearest_site_index",
61+
removal_version,
62+
)
5163

5264
# Constants ------------------------------------------------------------------
5365

@@ -223,7 +235,17 @@ def __call__(self, xyz):
223235
# End of class _Position2Tuple
224236

225237

238+
@deprecated(positionDifference_deprecation_msg)
226239
def positionDifference(xyz0, xyz1):
240+
"""'diffpy.structure.positionDifference' is deprecated and will be
241+
removed in version 4.0.0.
242+
243+
Please use 'diffpy.structure.position_difference' instead.
244+
"""
245+
return position_difference(xyz0, xyz1)
246+
247+
248+
def position_difference(xyz0, xyz1):
227249
"""Smallest difference between two coordinates in periodic lattice.
228250
229251
Parameters
@@ -245,7 +267,18 @@ def positionDifference(xyz0, xyz1):
245267
return dxyz
246268

247269

270+
@deprecated(nearestSiteIndex_deprecation_msg)
248271
def nearestSiteIndex(sites, xyz):
272+
"""'diffpy.structure.nearestSiteIndex' is deprecated and will be
273+
removed in version 4.0.0.
274+
275+
Please use 'diffpy.structure.nearest_site_index' instead.
276+
"""
277+
# we use box distance to be consistent with _Position2Tuple conversion
278+
return nearest_site_index(sites, xyz)
279+
280+
281+
def nearest_site_index(sites, xyz):
249282
"""Index of the nearest site to a specified position.
250283
251284
Parameters
@@ -261,7 +294,7 @@ def nearestSiteIndex(sites, xyz):
261294
Index of the nearest site.
262295
"""
263296
# we use box distance to be consistent with _Position2Tuple conversion
264-
dbox = positionDifference(sites, xyz).max(axis=1)
297+
dbox = position_difference(sites, xyz).max(axis=1)
265298
nearindex = numpy.argmin(dbox)
266299
return nearindex
267300

@@ -282,7 +315,7 @@ def equalPositions(xyz0, xyz1, eps):
282315
``True`` when two coordinates are closer than `eps`.
283316
"""
284317
# we use box distance to be consistent with _Position2Tuple conversion
285-
dxyz = positionDifference(xyz0, xyz1)
318+
dxyz = position_difference(xyz0, xyz1)
286319
return numpy.all(dxyz <= eps)
287320

288321

@@ -323,7 +356,7 @@ def expandPosition(spacegroup, xyz, sgoffset=[0, 0, 0], eps=None):
323356
site_symops[tpl] = []
324357
# double check if there is any position nearby
325358
if positions:
326-
nearpos = positions[nearestSiteIndex(positions, pos)]
359+
nearpos = positions[nearest_site_index(positions, pos)]
327360
# is it an equivalent position?
328361
if equalPositions(nearpos, pos, eps):
329362
# tpl should map to the same list as nearpos
@@ -352,7 +385,7 @@ def nullSpace(A):
352385
return null_space
353386

354387

355-
def _findInvariants(symops):
388+
def _find_invariants(symops):
356389
"""Find a list of symmetry operations which contains identity.
357390
358391
Parameters
@@ -495,7 +528,7 @@ def __init__(
495528
self.Uparameters = []
496529
# fill in the values
497530
sites, ops, mult = expandPosition(spacegroup, xyz, sgoffset, eps)
498-
invariants = _findInvariants(ops)
531+
invariants = _find_invariants(ops)
499532
# shift self.xyz exactly to the special position
500533
if mult > 1:
501534
xyzdups = numpy.array([op(xyz + self.sgoffset) - self.sgoffset for op in invariants])
@@ -506,7 +539,7 @@ def __init__(
506539
self.xyz = xyz + dxyz
507540
self.xyz[numpy.fabs(self.xyz) < self.eps] = 0.0
508541
sites, ops, mult = expandPosition(spacegroup, self.xyz, self.sgoffset, eps)
509-
invariants = _findInvariants(ops)
542+
invariants = _find_invariants(ops)
510543
# self.xyz, sites, ops are all adjusted here
511544
self.eqxyz = sites
512545
self.symops = ops
@@ -681,7 +714,7 @@ def positionFormula(self, pos, xyzsymbols=("x", "y", "z")):
681714
``-x``, ``z +0.5``, ``0.25``.
682715
"""
683716
# find pos in eqxyz
684-
idx = nearestSiteIndex(self.eqxyz, pos)
717+
idx = nearest_site_index(self.eqxyz, pos)
685718
eqpos = self.eqxyz[idx]
686719
if not equalPositions(eqpos, pos, self.eps):
687720
return {}
@@ -733,7 +766,7 @@ def UFormula(self, pos, Usymbols=stdUsymbols):
733766
pos is not equivalent to generator.
734767
"""
735768
# find pos in eqxyz
736-
idx = nearestSiteIndex(self.eqxyz, pos)
769+
idx = nearest_site_index(self.eqxyz, pos)
737770
eqpos = self.eqxyz[idx]
738771
if not equalPositions(eqpos, pos, self.eps):
739772
return {}
@@ -772,7 +805,7 @@ def eqIndex(self, pos):
772805
int
773806
Index of the nearest generator equivalent site.
774807
"""
775-
return nearestSiteIndex(self.eqxyz, pos)
808+
return nearest_site_index(self.eqxyz, pos)
776809

777810

778811
# End of class GeneratorSite

tests/test_symmetryutilities.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
is_space_group_latt_parms,
3232
isconstantFormula,
3333
isSpaceGroupLatPar,
34+
nearest_site_index,
35+
nearestSiteIndex,
36+
position_difference,
37+
positionDifference,
3438
pruneFormulaDictionary,
3539
)
3640

@@ -99,6 +103,30 @@ def test_sgtbx_spacegroup_aliases(self):
99103
self.assertIs(GetSpaceGroup("Ia3d"), GetSpaceGroup("I a -3 d"))
100104
return
101105

106+
def test_positionDifference(self):
107+
"""Check positionDifference in normal and boundary cases."""
108+
self.assertTrue(numpy.allclose(positionDifference([0.1, 0.9, 0.2], [0.9, 0.1, 0.8]), [0.2, 0.2, 0.4]))
109+
self.assertTrue(numpy.allclose(positionDifference([1.2, -0.1, 2.75], [0.1, 0.4, 0.25]), [0.1, 0.5, 0.5]))
110+
return
111+
112+
def test_position_difference(self):
113+
"""Check positionDifference in normal and boundary cases."""
114+
self.assertTrue(numpy.allclose(position_difference([0.1, 0.9, 0.2], [0.8, 0.1, 0.8]), [0.3, 0.2, 0.4]))
115+
self.assertTrue(numpy.allclose(position_difference([1.2, -0.1, 2.75], [0.1, 0.4, 0.25]), [0.1, 0.5, 0.5]))
116+
return
117+
118+
def test_nearestSiteIndex(self):
119+
"""Check nearestSiteIndex with single and multiple sites."""
120+
self.assertEqual(nearestSiteIndex([[0.1, 0.9, 0.2], [0.8, 0.1, 0.8]], [0.8, 0.1, 0.8]), 1)
121+
self.assertEqual(nearestSiteIndex([[1.2, -0.1, 2.75]], [0.7, 0.4, 0.25]), 0)
122+
return
123+
124+
def test_nearest_site_index(self):
125+
"""Check nearestSiteIndex with single and multiple sites."""
126+
self.assertEqual(nearest_site_index([[0.1, 0.9, 0.2], [0.8, 0.1, 0.8]], [0.8, 0.1, 0.8]), 1)
127+
self.assertEqual(nearest_site_index([[1.2, -0.1, 2.75]], [0.7, 0.4, 0.25]), 0)
128+
return
129+
102130
def test_expandPosition(self):
103131
"""Check expandPosition()"""
104132
# ok again Ni example

0 commit comments

Comments
 (0)