Skip to content

Commit 322b75e

Browse files
authored
Restructure morphpy for easier maintainence (#235)
1 parent 742643c commit 322b75e

File tree

3 files changed

+129
-53
lines changed

3 files changed

+129
-53
lines changed

news/restructure_morphpy.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* For diffpy.morph developers: both morphpy functions now have shared code in a separate function for easier maintenance.
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/morph/morphpy.py

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,57 @@ def get_args(parser, params, kwargs):
1919
return opts, pargs
2020

2121

22+
def __get_morph_opts__(parser, scale, stretch, smear, plot, **kwargs):
23+
# Check for Python-specific options
24+
python_morphs = ["funcy"]
25+
pymorphs = {}
26+
for pmorph in python_morphs:
27+
if pmorph in kwargs:
28+
pmorph_value = kwargs.pop(pmorph)
29+
pymorphs.update({pmorph: pmorph_value})
30+
31+
# Special handling of parameters with dashes
32+
kwargs_copy = kwargs.copy()
33+
kwargs = {}
34+
for key in kwargs_copy.keys():
35+
new_key = key
36+
if "_" in key:
37+
new_key = key.replace("_", "-")
38+
kwargs.update({new_key: kwargs_copy[key]})
39+
40+
# Special handling of store_true and store_false parameters
41+
opts_storing_values = [
42+
"verbose",
43+
"pearson",
44+
"addpearson",
45+
"apply",
46+
"reverse",
47+
]
48+
opts_to_ignore = ["multiple-morphs", "multiple-targets"]
49+
for opt in opts_storing_values:
50+
if opt in kwargs:
51+
# Remove if user sets false in params
52+
if not kwargs[opt]:
53+
kwargs.pop(opt)
54+
for opt in opts_to_ignore:
55+
if opt in kwargs:
56+
kwargs.pop(opt)
57+
58+
# Wrap the CLI
59+
params = {
60+
"scale": scale,
61+
"stretch": stretch,
62+
"smear": smear,
63+
"noplot": True if not plot else None,
64+
}
65+
opts, _ = get_args(parser, params, kwargs)
66+
67+
if not len(pymorphs) > 0:
68+
pymorphs = None
69+
70+
return opts, pymorphs
71+
72+
2273
# Take in file names as input.
2374
def morph(
2475
morph_file,
@@ -58,38 +109,12 @@ def morph(
58109
Function after morph where morph_table[:,0] is the abscissa and
59110
morph_table[:,1] is the ordinate.
60111
"""
61-
62-
# Check for Python-specific morphs
63-
python_morphs = ["funcy"]
64-
pymorphs = {}
65-
for pmorph in python_morphs:
66-
if pmorph in kwargs:
67-
pmorph_value = kwargs.pop(pmorph)
68-
pymorphs.update({pmorph: pmorph_value})
69-
70-
# Special handling of parameters with dashes
71-
kwargs_copy = kwargs.copy()
72-
kwargs = {}
73-
for key in kwargs_copy.keys():
74-
new_key = key
75-
if "_" in key:
76-
new_key = key.replace("_", "-")
77-
kwargs.update({new_key: kwargs_copy[key]})
78-
79-
# Wrap the CLI
80-
parser = create_option_parser()
81-
params = {
82-
"scale": scale,
83-
"stretch": stretch,
84-
"smear": smear,
85-
"noplot": True if not plot else None,
86-
}
87-
opts, _ = get_args(parser, params, kwargs)
88-
89112
pargs = [morph_file, target_file]
113+
parser = create_option_parser()
114+
opts, pymorphs = __get_morph_opts__(
115+
parser, scale, stretch, smear, plot, **kwargs
116+
)
90117

91-
if not len(pymorphs) > 0:
92-
pymorphs = None
93118
return single_morph(
94119
parser,
95120
opts,
@@ -139,36 +164,18 @@ def morph_arrays(
139164
Function after morph where morph_table[:,0] is the abscissa and
140165
morph_table[:,1] is the ordinate.
141166
"""
142-
# Check for Python-specific morphs
143-
python_morphs = ["funcy"]
144-
pymorphs = {}
145-
for pmorph in python_morphs:
146-
if pmorph in kwargs:
147-
pmorph_value = kwargs.pop(pmorph)
148-
pymorphs.update({pmorph: pmorph_value})
149-
150-
# Wrap the CLI
151-
parser = create_option_parser()
152-
params = {
153-
"scale": scale,
154-
"stretch": stretch,
155-
"smear": smear,
156-
"noplot": True if not plot else None,
157-
}
158-
opts, _ = get_args(parser, params, kwargs)
159-
160167
morph_table = np.array(morph_table)
161168
target_table = np.array(target_table)
162-
163169
x_morph = morph_table[:, 0]
164170
y_morph = morph_table[:, 1]
165171
x_target = target_table[:, 0]
166172
y_target = target_table[:, 1]
167-
168173
pargs = ["Morph", "Target", x_morph, y_morph, x_target, y_target]
174+
parser = create_option_parser()
175+
opts, pymorphs = __get_morph_opts__(
176+
parser, scale, stretch, smear, plot, **kwargs
177+
)
169178

170-
if not len(pymorphs) > 0:
171-
pymorphs = None
172179
return single_morph(
173180
parser,
174181
opts,

tests/test_morphpy.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77

88
from diffpy.morph.morphapp import create_option_parser, single_morph
9-
from diffpy.morph.morphpy import morph, morph_arrays
9+
from diffpy.morph.morphpy import __get_morph_opts__, morph, morph_arrays
1010
from diffpy.morph.tools import getRw
1111

1212
thisfile = locals().get("__file__", "file.py")
@@ -68,6 +68,52 @@ def setup_morph(self):
6868
)
6969
return
7070

71+
def test_morph_opts(self, setup_morph):
72+
kwargs = {
73+
"verbose": False,
74+
"pearson": False,
75+
"addpearson": False,
76+
"apply": False,
77+
"reverse": False,
78+
"multiple_morphs": False,
79+
"multiple_targets": False,
80+
}
81+
kwargs_copy = kwargs.copy()
82+
opts, _ = __get_morph_opts__(
83+
self.parser, scale=1, stretch=0, smear=0, plot=False, **kwargs_copy
84+
)
85+
# Special set true/false operations should be removed
86+
# when their input value is False
87+
for opt in kwargs:
88+
if opt == "apply":
89+
assert getattr(opts, "refine")
90+
else:
91+
assert getattr(opts, opt) is None or not getattr(opts, opt)
92+
93+
kwargs = {
94+
"verbose": True,
95+
"pearson": True,
96+
"addpearson": True,
97+
"apply": True,
98+
"reverse": True,
99+
"multiple_morphs": True,
100+
"multiple_targets": True,
101+
}
102+
kwargs_copy = kwargs.copy()
103+
opts, _ = __get_morph_opts__(
104+
self.parser, scale=1, stretch=0, smear=0, plot=False, **kwargs_copy
105+
)
106+
for opt in kwargs:
107+
if opt == "apply":
108+
assert not getattr(opts, "refine")
109+
# These options are not enabled in morphpy
110+
elif opt == "multiple_morphs" or opt == "multiple_targets":
111+
assert getattr(opts, opt) is None or not getattr(opts, opt)
112+
# Special set true/false operations should NOT be removed
113+
# when their input value is True
114+
else:
115+
assert getattr(opts, opt)
116+
71117
def test_morph(self, setup_morph):
72118
morph_results = {}
73119
morph_file = self.testfiles[0]

0 commit comments

Comments
 (0)