Skip to content

Commit a7d43a1

Browse files
authored
Merge pull request #163 from stevenhua0320/deprecate-lattice
chore: deprecate setLatPar and setLatBase method
2 parents 3041bc2 + 74683b6 commit a7d43a1

File tree

7 files changed

+148
-25
lines changed

7 files changed

+148
-25
lines changed

news/deprecate-lattice.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**Added:**
2+
3+
* Added ``set_latt_parms`` method into ``Lattice`` class
4+
* Added ``set_new_latt_base_vec`` method into ``Lattice`` class
5+
6+
**Changed:**
7+
8+
* <news item>
9+
10+
**Deprecated:**
11+
12+
* Deprecated ``setLatPar`` method in ``Lattice`` class for removal in version 4.0.0
13+
* Deprecated ``setLatBase`` method in ``Lattice`` class for removal in version 4.0.0
14+
15+
**Removed:**
16+
17+
* <news item>
18+
19+
**Fixed:**
20+
21+
* <news item>
22+
23+
**Security:**
24+
25+
* <news item>

src/diffpy/structure/expansion/supercell_mod.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def supercell(S, mno):
8282
newS.__setitem__(slice(None), newAtoms, copy=False)
8383

8484
# take care of lattice parameters
85-
newS.lattice.setLatPar(a=mno[0] * S.lattice.a, b=mno[1] * S.lattice.b, c=mno[2] * S.lattice.c)
85+
newS.lattice.set_latt_parms(a=mno[0] * S.lattice.a, b=mno[1] * S.lattice.b, c=mno[2] * S.lattice.c)
8686
return newS
8787

8888

src/diffpy/structure/lattice.py

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@
2727
import numpy.linalg as numalg
2828

2929
from diffpy.structure.structureerrors import LatticeError
30+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
31+
32+
base = "diffpy.structure.Lattice"
33+
removal_version = "4.0.0"
34+
setLatPar_deprecation_msg = build_deprecation_message(
35+
base,
36+
"setLatPar",
37+
"set_latt_parms",
38+
removal_version,
39+
)
40+
setLatBase_deprecation_msg = build_deprecation_message(
41+
base,
42+
"setLatBase",
43+
"set_new_latt_base_vec",
44+
removal_version,
45+
)
3046

3147
# Helper Functions -----------------------------------------------------------
3248

@@ -168,37 +184,37 @@ class Lattice(object):
168184

169185
a = property(
170186
lambda self: self._a,
171-
lambda self, value: self.setLatPar(a=value),
187+
lambda self, value: self.set_latt_parms(a=value),
172188
doc="The unit cell length *a*.",
173189
)
174190

175191
b = property(
176192
lambda self: self._b,
177-
lambda self, value: self.setLatPar(b=value),
193+
lambda self, value: self.set_latt_parms(b=value),
178194
doc="The unit cell length *b*.",
179195
)
180196

181197
c = property(
182198
lambda self: self._c,
183-
lambda self, value: self.setLatPar(c=value),
199+
lambda self, value: self.set_latt_parms(c=value),
184200
doc="The unit cell length *c*.",
185201
)
186202

187203
alpha = property(
188204
lambda self: self._alpha,
189-
lambda self, value: self.setLatPar(alpha=value),
205+
lambda self, value: self.set_latt_parms(alpha=value),
190206
doc="The cell angle *alpha* in degrees.",
191207
)
192208

193209
beta = property(
194210
lambda self: self._beta,
195-
lambda self, value: self.setLatPar(beta=value),
211+
lambda self, value: self.set_latt_parms(beta=value),
196212
doc="The cell angle *beta* in degrees.",
197213
)
198214

199215
gamma = property(
200216
lambda self: self._gamma,
201-
lambda self, value: self.setLatPar(gamma=value),
217+
lambda self, value: self.set_latt_parms(gamma=value),
202218
doc="The cell angle *gamma* in degrees.",
203219
)
204220

@@ -323,12 +339,12 @@ def __init__(
323339
# work out argument variants
324340
# Lattice()
325341
if not argset:
326-
self.setLatPar(1.0, 1.0, 1.0, 90.0, 90.0, 90.0, baserot)
342+
self.set_latt_parms(1.0, 1.0, 1.0, 90.0, 90.0, 90.0, baserot)
327343
# Lattice(base=abc)
328344
elif base is not None:
329345
if len(argset) > 1:
330346
raise ValueError("'base' must be the only argument.")
331-
self.setLatBase(base)
347+
self.set_new_latt_base_vec(base)
332348
# Lattice(lat)
333349
elif isinstance(a, Lattice):
334350
if len(argset) > 1:
@@ -339,10 +355,10 @@ def __init__(
339355
abcabg = ("a", "b", "c", "alpha", "beta", "gamma")
340356
if not argset.issuperset(abcabg):
341357
raise ValueError("Provide all 6 cell parameters.")
342-
self.setLatPar(a, b, c, alpha, beta, gamma, baserot=baserot)
358+
self.set_latt_parms(a, b, c, alpha, beta, gamma, baserot=baserot)
343359
return
344360

345-
def setLatPar(
361+
def set_latt_parms(
346362
self,
347363
a=None,
348364
b=None,
@@ -441,7 +457,34 @@ def setLatPar(
441457
self.isotropicunit = _isotropicunit(self.recnormbase)
442458
return
443459

460+
@deprecated(setLatPar_deprecation_msg)
461+
def setLatPar(
462+
self,
463+
a=None,
464+
b=None,
465+
c=None,
466+
alpha=None,
467+
beta=None,
468+
gamma=None,
469+
baserot=None,
470+
):
471+
"""This function has been deprecated and will be removed in
472+
version 4.0.0.
473+
474+
Please use diffpy.structure.Lattice.set_lat_par instead.
475+
"""
476+
return self.set_latt_parms(a, b, c, alpha, beta, gamma, baserot)
477+
478+
@deprecated(setLatBase_deprecation_msg)
444479
def setLatBase(self, base):
480+
"""This function has been deprecated and will be removed in
481+
version 4.0.0.
482+
483+
Please use diffpy.structure.Lattice.set_lat_base instead.
484+
"""
485+
return self.set_new_latt_base_vec(base)
486+
487+
def set_new_latt_base_vec(self, base):
445488
"""Set new base vectors for this lattice.
446489
447490
This updates the cell lengths and cell angles according to the

src/diffpy/structure/parsers/p_discus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def _parse_cell(self, words):
201201
words = self.line.replace(",", " ").split()
202202
latpars = [float(w) for w in words[1:7]]
203203
try:
204-
self.stru.lattice.setLatPar(*latpars)
204+
self.stru.lattice.set_latt_parms(*latpars)
205205
except ZeroDivisionError:
206206
emsg = "%d: Invalid lattice parameters - zero cell volume" % self.nl
207207
raise StructureFormatError(emsg)

src/diffpy/structure/parsers/p_pdb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def parseLines(self, lines):
157157
alpha = float(line[33:40])
158158
beta = float(line[40:47])
159159
gamma = float(line[47:54])
160-
stru.lattice.setLatPar(a, b, c, alpha, beta, gamma)
160+
stru.lattice.set_latt_parms(a, b, c, alpha, beta, gamma)
161161
scale = numpy.transpose(stru.lattice.recbase)
162162
elif record == "SCALE1":
163163
sc = numpy.zeros((3, 3), dtype=float)
@@ -171,7 +171,7 @@ def parseLines(self, lines):
171171
scaleU[2] = float(line[45:55])
172172
base = numpy.transpose(numpy.linalg.inv(sc))
173173
abcABGcryst = numpy.array(stru.lattice.abcABG())
174-
stru.lattice.setLatBase(base)
174+
stru.lattice.set_new_latt_base_vec(base)
175175
abcABGscale = numpy.array(stru.lattice.abcABG())
176176
reldiff = numpy.fabs(1.0 - abcABGscale / abcABGcryst)
177177
if not numpy.all(reldiff < 1.0e-4):

src/diffpy/structure/parsers/p_xcfg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def parseLines(self, lines):
253253
emsg = ("%d: auxiliary fields are " "not consistent with entry_count") % p_nl
254254
raise StructureFormatError(emsg)
255255
# define proper lattice
256-
stru.lattice.setLatBase(xcfg_H0)
256+
stru.lattice.set_new_latt_base_vec(xcfg_H0)
257257
# here we are inside the data block
258258
p_element = None
259259
for line in ilines:

tests/test_lattice.py

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test___init__(self):
4444
self.assertRaises(ValueError, Lattice, 1, 2, 3)
4545
self.assertRaises(ValueError, Lattice, 1, 2, 3, 80, 90)
4646
L0 = self.lattice
47-
L0.setLatBase(L0.cartesian([[1, 1, 0], [0, 1, 1], [1, 0, 1]]))
47+
L0.set_new_latt_base_vec(L0.cartesian([[1, 1, 0], [0, 1, 1], [1, 0, 1]]))
4848
L1 = Lattice(L0)
4949
self.assertTrue(numpy.array_equal(L0.base, L1.base))
5050
L2 = Lattice(base=L0.base)
@@ -77,6 +77,28 @@ def cosd(x):
7777
self.assertAlmostEqual(cosd(120.0), dot(base[0], base[1]) / (1 * 2), self.places)
7878
return
7979

80+
def test_set_lat_par(self):
81+
"""Check calculation of standard unit cell vectors."""
82+
from math import cos, radians, sqrt
83+
84+
from numpy import dot
85+
86+
def norm(x):
87+
return sqrt(sum([xi**2 for xi in x]))
88+
89+
def cosd(x):
90+
return cos(radians(x))
91+
92+
self.lattice.set_latt_parms(1.0, 2.0, 3.0, 80, 100, 120)
93+
base = self.lattice.base
94+
self.assertAlmostEqual(1.0, norm(base[0]), self.places)
95+
self.assertAlmostEqual(2.0, norm(base[1]), self.places)
96+
self.assertAlmostEqual(3.0, norm(base[2]), self.places)
97+
self.assertAlmostEqual(cosd(80.0), dot(base[1], base[2]) / (2 * 3), self.places)
98+
self.assertAlmostEqual(cosd(100.0), dot(base[0], base[2]) / (1 * 3), self.places)
99+
self.assertAlmostEqual(cosd(120.0), dot(base[0], base[1]) / (1 * 2), self.places)
100+
return
101+
80102
def test_latpar_properties(self):
81103
"""Check assignment to a, b, c, alpha, beta, gamma."""
82104
lat = self.lattice
@@ -152,9 +174,9 @@ def test_setLatBase(self):
152174
self.assertAlmostEqual(detR0, 1.0, self.places)
153175
# try if rotation matrix works
154176
self.assertEqual(numpy.all(base == self.lattice.base), True)
155-
self.lattice.setLatPar(alpha=44, beta=66, gamma=88)
177+
self.lattice.set_latt_parms(alpha=44, beta=66, gamma=88)
156178
self.assertNotEqual(numpy.all(base == self.lattice.base), True)
157-
self.lattice.setLatPar(alpha=60, beta=60, gamma=60)
179+
self.lattice.set_latt_parms(alpha=60, beta=60, gamma=60)
158180
self.assertTrue(numpy.allclose(base[0], self.lattice.base[0]))
159181
self.assertTrue(numpy.allclose(base[1], self.lattice.base[1]))
160182
self.assertTrue(numpy.allclose(base[2], self.lattice.base[2]))
@@ -171,6 +193,39 @@ def test_setLatBase(self):
171193
)
172194
return
173195

196+
def test_set_lat_base(self):
197+
"""Check calculation of unit cell rotation."""
198+
base = numpy.array([[1.0, 1.0, 0.0], [0.0, 1.0, 1.0], [1.0, 0.0, 1.0]])
199+
self.lattice.set_new_latt_base_vec(base)
200+
self.assertAlmostEqual(self.lattice.a, numpy.sqrt(2.0), self.places)
201+
self.assertAlmostEqual(self.lattice.b, numpy.sqrt(2.0), self.places)
202+
self.assertAlmostEqual(self.lattice.c, numpy.sqrt(2.0), self.places)
203+
self.assertAlmostEqual(self.lattice.alpha, 60.0, self.places)
204+
self.assertAlmostEqual(self.lattice.beta, 60.0, self.places)
205+
self.assertAlmostEqual(self.lattice.gamma, 60.0, self.places)
206+
detR0 = numalg.det(self.lattice.baserot)
207+
self.assertAlmostEqual(detR0, 1.0, self.places)
208+
# try if rotation matrix works
209+
self.assertEqual(numpy.all(base == self.lattice.base), True)
210+
self.lattice.set_latt_parms(alpha=44, beta=66, gamma=88)
211+
self.assertNotEqual(numpy.all(base == self.lattice.base), True)
212+
self.lattice.set_latt_parms(alpha=60, beta=60, gamma=60)
213+
self.assertTrue(numpy.allclose(base[0], self.lattice.base[0]))
214+
self.assertTrue(numpy.allclose(base[1], self.lattice.base[1]))
215+
self.assertTrue(numpy.allclose(base[2], self.lattice.base[2]))
216+
# try base checking
217+
self.assertRaises(
218+
LatticeError,
219+
self.lattice.set_new_latt_base_vec,
220+
[[1, 0, 0], [1, 0, 0], [0, 0, 1]],
221+
)
222+
self.assertRaises(
223+
LatticeError,
224+
self.lattice.set_new_latt_base_vec,
225+
[[1, 0, 0], [0, 0, 1], [0, 1, 0]],
226+
)
227+
return
228+
174229
def test_reciprocal(self):
175230
"""Check calculation of reciprocal lattice."""
176231
r1 = self.lattice.reciprocal()
@@ -185,7 +240,7 @@ def test_reciprocal(self):
185240
def test_dot(self):
186241
"""Check dot product of lattice vectors."""
187242
L = self.lattice
188-
L.setLatPar(gamma=120)
243+
L.set_latt_parms(gamma=120)
189244
self.assertAlmostEqual(-0.5, L.dot([1, 0, 0], [0, 1, 0]), self.places)
190245
va5 = numpy.tile([1.0, 0.0, 0.0], (5, 1))
191246
vb5 = numpy.tile([0.0, 1.0, 0.0], (5, 1))
@@ -199,14 +254,14 @@ def test_norm(self):
199254
self.assertEqual(1, self.lattice.norm([1, 0, 0]))
200255
u = numpy.array([[3, 4, 0], [1, 1, 1]])
201256
self.assertTrue(numpy.allclose([5, 3**0.5], self.lattice.norm(u)))
202-
self.lattice.setLatPar(gamma=120)
257+
self.lattice.set_latt_parms(gamma=120)
203258
self.assertAlmostEqual(1, self.lattice.norm([1, 1, 0]), self.places)
204259
return
205260

206261
def test_rnorm(self):
207262
"""Check norm of a reciprocal vector."""
208263
L = self.lattice
209-
L.setLatPar(1, 1.5, 2.3, 80, 95, 115)
264+
L.set_latt_parms(1, 1.5, 2.3, 80, 95, 115)
210265
r = L.reciprocal()
211266
hkl = [0.5, 0.3, 0.2]
212267
self.assertAlmostEqual(r.norm(hkl), L.rnorm(hkl), self.places)
@@ -217,7 +272,7 @@ def test_rnorm(self):
217272
def test_dist(self):
218273
"""Check dist function for distance between lattice points."""
219274
L = self.lattice
220-
L.setLatPar(1, 1.5, 2.3, 80, 95, 115)
275+
L.set_latt_parms(1, 1.5, 2.3, 80, 95, 115)
221276
u = [0.1, 0.3, 0.7]
222277
v = [0.3, 0.7, 0.7]
223278
d0 = numalg.norm(L.cartesian(numpy.array(u) - v))
@@ -235,7 +290,7 @@ def test_angle(self):
235290
from math import acos, degrees
236291

237292
L = self.lattice
238-
L.setLatPar(1, 1.5, 2.3, 80, 95, 115)
293+
L.set_latt_parms(1, 1.5, 2.3, 80, 95, 115)
239294
u = [0.1, 0.3, 0.7]
240295
v = [0.3, 0.7, 0.7]
241296
uc = L.cartesian(u)
@@ -254,12 +309,12 @@ def test_repr(self):
254309
"""Check string representation of this lattice."""
255310
r = repr(self.lattice)
256311
self.assertEqual(r, "Lattice()")
257-
self.lattice.setLatPar(1, 2, 3, 10, 20, 30)
312+
self.lattice.set_latt_parms(1, 2, 3, 10, 20, 30)
258313
r = repr(self.lattice)
259314
r0 = "Lattice(a=1, b=2, c=3, alpha=10, beta=20, gamma=30)"
260315
self.assertEqual(r, r0)
261316
base = [[1.0, 1.0, 0.0], [0.0, 2.0, 2.0], [3.0, 0.0, 3.0]]
262-
self.lattice.setLatBase(base)
317+
self.lattice.set_new_latt_base_vec(base)
263318
r = repr(self.lattice)
264319
self.assertEqual(r, "Lattice(base=%r)" % self.lattice.base)
265320

0 commit comments

Comments
 (0)