Skip to content

Commit 64aa1c7

Browse files
authored
Merge pull request #171 from stevenhua0320/deprecate-symmetryutilities-7
chore: deprecate symmetryutilities method
2 parents fd16b4c + f489b30 commit 64aa1c7

File tree

3 files changed

+245
-1
lines changed

3 files changed

+245
-1
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 ``position_formulas`` method in ``SymmetryConstraints`` class
4+
* Added ``position_formulas_pruned`` method in ``SymmetryConstraints`` class
5+
* Added ``u_formulas_pruned`` method in ``SymmetryConstraints`` class
6+
7+
**Changed:**
8+
9+
* <news item>
10+
11+
**Deprecated:**
12+
13+
* Deprecated ``positionFormulas`` method in ``SymmetryConstraints`` class for removal in version 4.0.0
14+
* Deprecated ``positionFormulasPruned`` method in ``SymmetryConstraints`` class for removal in version 4.0.0
15+
* Deprecated ``UFormulasPruned`` method in ``SymmetryConstraints`` class 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/symmetryutilities.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,24 @@ def prune_formula_dictionary(eqdict):
10741074
"u_formulas",
10751075
removal_version,
10761076
)
1077+
positionFormulas_deprecation_msg = build_deprecation_message(
1078+
symmetry_constraints,
1079+
"positionFormulas",
1080+
"position_formulas",
1081+
removal_version,
1082+
)
1083+
positionFormulasPruned_deprecation_msg = build_deprecation_message(
1084+
symmetry_constraints,
1085+
"positionFormulasPruned",
1086+
"position_formulas_pruned",
1087+
removal_version,
1088+
)
1089+
UFormulasPruned_deprecation_msg = build_deprecation_message(
1090+
symmetry_constraints,
1091+
"UFormulasPruned",
1092+
"u_formulas_pruned",
1093+
removal_version,
1094+
)
10771095

10781096

10791097
class SymmetryConstraints(object):
@@ -1243,7 +1261,16 @@ def pos_parm_values(self):
12431261
"""Return list of position parameters values."""
12441262
return [v for n, v in self.pospars]
12451263

1264+
@deprecated(posparValues_deprecation_msg)
12461265
def positionFormulas(self, xyzsymbols=None):
1266+
"""'diffpy.structure.SymmetryConstraints.positionFormulas' is
1267+
deprecated and will be removed in version 4.0.0.
1268+
1269+
Please use 'diffpy.structure.SymmetryConstraints.position_formulas' instead.
1270+
"""
1271+
return self.position_formulas(xyzsymbols)
1272+
1273+
def position_formulas(self, xyzsymbols=None):
12471274
"""List of position formulas with custom parameter symbols.
12481275
12491276
Parameters
@@ -1279,7 +1306,16 @@ def translatesymbol(matchobj):
12791306
rv.append(treqns)
12801307
return rv
12811308

1309+
@deprecated(positionFormulasPruned_deprecation_msg)
12821310
def positionFormulasPruned(self, xyzsymbols=None):
1311+
"""'diffpy.structure.SymmetryConstraints.positionFormulasPruned'
1312+
is deprecated and will be removed in version 4.0.0.
1313+
1314+
Please use 'diffpy.structure.SymmetryConstraints.position_formulas_pruned' instead.
1315+
"""
1316+
return self.position_formulas_pruned(xyzsymbols)
1317+
1318+
def position_formulas_pruned(self, xyzsymbols=None):
12831319
"""List of position formula dictionaries with constant items
12841320
removed.
12851321
@@ -1297,7 +1333,7 @@ def positionFormulasPruned(self, xyzsymbols=None):
12971333
list
12981334
List of coordinate formula dictionaries.
12991335
"""
1300-
rv = [prune_formula_dictionary(eqns) for eqns in self.positionFormulas(xyzsymbols)]
1336+
rv = [prune_formula_dictionary(eqns) for eqns in self.position_formulas(xyzsymbols)]
13011337
return rv
13021338

13031339
@deprecated(UparSymbols_deprecation_msg)
@@ -1376,7 +1412,17 @@ def translatesymbol(matchobj):
13761412
rv.append(treqns)
13771413
return rv
13781414

1415+
@deprecated(UFormulasPruned_deprecation_msg)
13791416
def UFormulasPruned(self, Usymbols=None):
1417+
"""'diffpy.structure.SymmetryConstraints.UFormulasPruned' is
1418+
deprecated and will be removed in version 4.0.0.
1419+
1420+
Please use 'diffpy.structure.SymmetryConstraints.u_formulas_pruned'
1421+
instead.
1422+
"""
1423+
return self.u_formulas_pruned(Usymbols)
1424+
1425+
def u_formulas_pruned(self, Usymbols=None):
13801426
"""List of atom displacement formula dictionaries with constant
13811427
items removed.
13821428

tests/test_symmetryutilities.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import pytest
2323

2424
from diffpy.structure.spacegroups import GetSpaceGroup
25+
from diffpy.structure.structureerrors import SymmetryError
2526
from diffpy.structure.symmetryutilities import (
2627
ExpandAsymmetricUnit,
2728
GeneratorSite,
@@ -960,6 +961,69 @@ def test_posparSymbols_and_posparValues(self):
960961
assert expected_symbols == actual_symbols
961962
assert expected_values == actual_values
962963

964+
def test_positionFormulas(self):
965+
sg225 = GetSpaceGroup(225)
966+
eau = ExpandAsymmetricUnit(sg225, [[0, 0, 0]])
967+
sc = SymmetryConstraints(sg225, eau.expandedpos)
968+
# C1: Simulate the "not enough symbols" branch
969+
sc.pospars = [("x1", 0.12), ("y1", 0.34), ("z1", 0.56)]
970+
sc.poseqns = [
971+
{"x": "x1", "y": "y1 +0.5", "z": "-z1 +0.25"},
972+
{"x": "-x1 +0.5", "y": "y1", "z": "z1 +0.5"},
973+
]
974+
with pytest.raises(SymmetryError):
975+
sc.positionFormulas(["x1"]) # too few custom symbols
976+
# C2: Normal case, does substitution of x0/y0/z0 tokens in formulas
977+
# Make pospars consistent with what positionFormulas expects to replace
978+
actual = sc.positionFormulas(["xA", "yA", "zA"])
979+
expected = [
980+
{"x": "xA", "y": "yA +0.5", "z": "-zA +0.25"},
981+
{"x": "-xA +0.5", "y": "yA", "z": "zA +0.5"},
982+
]
983+
assert actual == expected
984+
985+
def test_positionFormulasPruned(self):
986+
sg225 = GetSpaceGroup(225)
987+
eau = ExpandAsymmetricUnit(sg225, [[0, 0, 0]])
988+
sc = SymmetryConstraints(sg225, eau.expandedpos)
989+
# C1: Remove any key-value pairs with constant values
990+
sc.pospars = [("x1", 0.12), ("y1", 0.34), ("z1", 0.56)]
991+
sc.poseqns = [
992+
{"x": "x1", "y": "0.25", "z": "-z1 +0.5"},
993+
{"x": "0", "y": "y1 +0.5", "z": "0.125"},
994+
]
995+
actual = sc.positionFormulasPruned(["xA", "yA", "zA"])
996+
expected = [
997+
{"x": "xA", "z": "-zA +0.5"},
998+
{"y": "yA +0.5"},
999+
]
1000+
assert actual == expected
1001+
1002+
def test_UFormulasPruned(self):
1003+
"""Check SymmetryConstraints.UFormulasPruned()"""
1004+
u_formulas = {
1005+
"U11": "A",
1006+
"U22": "A",
1007+
"U33": "C",
1008+
"U12": "0.5*A",
1009+
"U13": "0",
1010+
"U23": "0",
1011+
}
1012+
expected = [
1013+
{
1014+
"U11": "A",
1015+
"U22": "A",
1016+
"U33": "C",
1017+
"U12": "0.5*A",
1018+
}
1019+
]
1020+
sg225 = GetSpaceGroup(225)
1021+
eau = ExpandAsymmetricUnit(sg225, [[0, 0, 0]])
1022+
sc = SymmetryConstraints(sg225, eau.expandedpos)
1023+
sc.Ueqns = [u_formulas]
1024+
actual = sc.UFormulasPruned(u_formulas)
1025+
assert actual == expected
1026+
9631027

9641028
# def test_UFormulas(self):
9651029
# """check SymmetryConstraints.UFormulas()
@@ -1113,5 +1177,112 @@ def test_pospar_symbols_and_pospar_values(params, expected_symbols, expected_val
11131177
assert actual_values == expected_values
11141178

11151179

1180+
@pytest.mark.parametrize(
1181+
"params",
1182+
[
1183+
pytest.param(["x1"]),
1184+
],
1185+
)
1186+
def test_position_formulas_raises_SymmetryError(params):
1187+
sg225 = GetSpaceGroup(225)
1188+
eau = ExpandAsymmetricUnit(sg225, [[0, 0, 0]])
1189+
sc = SymmetryConstraints(sg225, eau.expandedpos)
1190+
sc.pospars = [("x1", 0.12), ("y1", 0.34), ("z1", 0.56)]
1191+
sc.poseqns = [
1192+
{"x": "x1", "y": "y1 +0.5", "z": "-z1 +0.25"},
1193+
{"x": "-x1 +0.5", "y": "y1", "z": "z1 +0.5"},
1194+
]
1195+
# C1: Simulate the "not enough symbols" in position_formula
1196+
with pytest.raises(SymmetryError):
1197+
sc.position_formulas(params)
1198+
1199+
1200+
@pytest.mark.parametrize(
1201+
"params, expected",
1202+
[
1203+
pytest.param( # C2: Normal case, does substitution of x1/y1/z1 tokens in formulas
1204+
# Make pospars consistent with what position_formulas expects to replace
1205+
["xA", "yA", "zA"],
1206+
[
1207+
{"x": "xA", "y": "yA +0.5", "z": "-zA +0.25"},
1208+
{"x": "-xA +0.5", "y": "yA", "z": "zA +0.5"},
1209+
],
1210+
),
1211+
],
1212+
)
1213+
def test_position_formulas(params, expected):
1214+
sg225 = GetSpaceGroup(225)
1215+
eau = ExpandAsymmetricUnit(sg225, [[0, 0, 0]])
1216+
sc = SymmetryConstraints(sg225, eau.expandedpos)
1217+
sc.pospars = [("x1", 0.12), ("y1", 0.34), ("z1", 0.56)]
1218+
sc.poseqns = [
1219+
{"x": "x1", "y": "y1 +0.5", "z": "-z1 +0.25"},
1220+
{"x": "-x1 +0.5", "y": "y1", "z": "z1 +0.5"},
1221+
]
1222+
actual = sc.position_formulas(params)
1223+
assert actual == expected
1224+
1225+
1226+
@pytest.mark.parametrize(
1227+
"poseqns, expected",
1228+
[
1229+
pytest.param(
1230+
[
1231+
{"x": "x1", "y": "0.25", "z": "-z1 +0.5"},
1232+
{"x": "0", "y": "y1 +0.5", "z": "0.125"},
1233+
],
1234+
[
1235+
{"x": "xA", "z": "-zA +0.5"},
1236+
{"y": "yA +0.5"},
1237+
],
1238+
)
1239+
],
1240+
)
1241+
def test_position_formulas_pruned(poseqns, expected):
1242+
sg225 = GetSpaceGroup(225)
1243+
eau = ExpandAsymmetricUnit(sg225, [[0, 0, 0]])
1244+
sc = SymmetryConstraints(sg225, eau.expandedpos)
1245+
1246+
sc.pospars = [("x1", 0.12), ("y1", 0.34), ("z1", 0.56)]
1247+
sc.poseqns = poseqns
1248+
1249+
actual = sc.position_formulas_pruned(["xA", "yA", "zA"])
1250+
assert actual == expected
1251+
1252+
1253+
@pytest.mark.parametrize(
1254+
"u_formulas, expected",
1255+
[
1256+
pytest.param(
1257+
[
1258+
{
1259+
"U11": "A",
1260+
"U22": "A",
1261+
"U33": "C",
1262+
"U12": "0.5*A",
1263+
"U13": "0",
1264+
"U23": "0",
1265+
}
1266+
],
1267+
[
1268+
{
1269+
"U11": "A",
1270+
"U22": "A",
1271+
"U33": "C",
1272+
"U12": "0.5*A",
1273+
}
1274+
],
1275+
)
1276+
],
1277+
)
1278+
def test_u_formula_pruned(u_formulas, expected):
1279+
sg225 = GetSpaceGroup(225)
1280+
eau = ExpandAsymmetricUnit(sg225, [[0, 0, 0]])
1281+
sc = SymmetryConstraints(sg225, eau.expandedpos)
1282+
sc.Ueqns = u_formulas
1283+
actual = sc.u_formulas_pruned(u_formulas)
1284+
assert actual == expected
1285+
1286+
11161287
if __name__ == "__main__":
11171288
unittest.main()

0 commit comments

Comments
 (0)