Skip to content

Commit ed8641e

Browse files
authored
Merge pull request #155 from Sparks29032/morphshift
Add shifting morph
2 parents c7b1f60 + 641b5e7 commit ed8641e

File tree

13 files changed

+144
-9
lines changed

13 files changed

+144
-9
lines changed

Diff for: news/morphshift.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* Shifting morph for vertical and horizontal shifts.
4+
5+
**Changed:**
6+
7+
* <news item>
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>

Diff for: src/diffpy/pdfmorph/morphs/morphshift.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,19 @@ class MorphShift(Morph):
4747

4848
def morph(self, x_morph, y_morph, x_target, y_target):
4949
"""Apply the shifts."""
50+
try:
51+
hshift = self.hshift
52+
except AttributeError:
53+
hshift = 0
54+
try:
55+
vshift = self.vshift
56+
except AttributeError:
57+
vshift = 0
58+
5059
Morph.morph(self, x_morph, y_morph, x_target, y_target)
51-
r = self.x_morph_in - self.hshift
60+
r = self.x_morph_in - hshift
5261
self.y_morph_out = numpy.interp(r, self.x_morph_in, self.y_morph_in)
53-
self.y_morph_out += self.vshift
62+
self.y_morph_out += vshift
5463
return self.xyallout
5564

5665

Diff for: src/diffpy/pdfmorph/pdfmorphapp.py

+40-7
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,43 @@ def custom_error(self, msg):
121121
help="""Exclude a manipulation from refinement by name. This can
122122
appear multiple times.""",
123123
)
124-
group.add_option("--scale", type="float", metavar="SCALE", help="Apply scale factor SCALE.")
125124
group.add_option(
126-
"--smear",
125+
"--scale",
127126
type="float",
128-
metavar="SMEAR",
129-
help="Smear peaks with a Gaussian of width SMEAR.",
127+
metavar="SCALE",
128+
help="Apply scale factor SCALE.",
130129
)
131130
group.add_option(
132131
"--stretch",
133132
type="float",
134133
metavar="STRETCH",
135134
help="Stretch PDF by a fraction STRETCH.",
136135
)
136+
group.add_option(
137+
"--smear",
138+
type="float",
139+
metavar="SMEAR",
140+
help="Smear peaks with a Gaussian of width SMEAR.",
141+
)
137142
group.add_option(
138143
"--slope",
139144
type="float",
140145
dest="baselineslope",
141146
help="""Slope of the baseline. This is used when applying the smear
142147
factor. It will be estimated if not provided.""",
143148
)
149+
group.add_option(
150+
"--hshift",
151+
type="float",
152+
metavar="HSHIFT",
153+
help="Shift the PDF horizontally by HSHIFT to the right.",
154+
)
155+
group.add_option(
156+
"--vshift",
157+
type="float",
158+
metavar="VSHIFT",
159+
help="Shift the PDF vertically by VSHIFT upward.",
160+
)
144161
group.add_option(
145162
"--qdamp",
146163
type="float",
@@ -318,6 +335,8 @@ def single_morph(parser, opts, pargs, stdout_flag=True):
318335
scale_in = "None"
319336
stretch_in = "None"
320337
smear_in = "None"
338+
hshift_in = "None"
339+
vshift_in = "None"
321340
config = {}
322341
config["rmin"] = opts.rmin
323342
config["rmax"] = opts.rmax
@@ -336,22 +355,33 @@ def single_morph(parser, opts, pargs, stdout_flag=True):
336355
if opts.scale is not None:
337356
scale_in = opts.scale
338357
chain.append(morphs.MorphScale())
339-
config["scale"] = opts.scale
358+
config["scale"] = scale_in
340359
refpars.append("scale")
341360
# Stretch
342361
if opts.stretch is not None:
343362
stretch_in = opts.stretch
344363
chain.append(morphs.MorphStretch())
345-
config["stretch"] = opts.stretch
364+
config["stretch"] = stretch_in
346365
refpars.append("stretch")
366+
# Shift
367+
if opts.hshift is not None or opts.vshift is not None:
368+
chain.append(morphs.MorphShift())
369+
if opts.hshift is not None:
370+
hshift_in = opts.hshift
371+
config["hshift"] = hshift_in
372+
refpars.append("hshift")
373+
if opts.vshift is not None:
374+
vshift_in = opts.vshift
375+
config["vshift"] = vshift_in
376+
refpars.append("vshift")
347377
# Smear
348378
if opts.smear is not None:
349379
smear_in = opts.smear
350380
chain.append(helpers.TransformXtalPDFtoRDF())
351381
chain.append(morphs.MorphSmear())
352382
chain.append(helpers.TransformXtalRDFtoPDF())
353383
refpars.append("smear")
354-
config["smear"] = opts.smear
384+
config["smear"] = smear_in
355385
# Set baselineslope if not given
356386
config["baselineslope"] = opts.baselineslope
357387
if opts.baselineslope is None:
@@ -432,6 +462,7 @@ def single_morph(parser, opts, pargs, stdout_flag=True):
432462

433463
# Input morph parameters
434464
morph_inputs = {"scale": scale_in, "stretch": stretch_in, "smear": smear_in}
465+
morph_inputs.update({"hshift": hshift_in, "vshift": vshift_in})
435466

436467
# Output morph parameters
437468
morph_results = dict(config.items())
@@ -580,6 +611,7 @@ def multiple_targets(parser, opts, pargs, stdout_flag=True):
580611
target_file_names.append(key)
581612

582613
morph_inputs = {"scale": opts.scale, "stretch": opts.stretch, "smear": opts.smear}
614+
morph_inputs.update({"hshift": opts.hshift, "vshift": opts.vshift})
583615

584616
try:
585617
# Print summary of morphs to terminal and to file (if requested)
@@ -724,6 +756,7 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True):
724756
morph_file_names.append(key)
725757

726758
morph_inputs = {"scale": opts.scale, "stretch": opts.stretch, "smear": opts.smear}
759+
morph_inputs.update({"hshift": opts.hshift, "vshift": opts.vshift})
727760

728761
try:
729762
# Print summary of morphs to terminal and to file (if requested)

Diff for: tests/test_morphshift.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
2+
3+
4+
import os
5+
6+
import numpy
7+
import pytest
8+
9+
from diffpy.pdfmorph.morphs.morphshift import MorphShift
10+
11+
# useful variables
12+
thisfile = locals().get("__file__", "file.py")
13+
tests_dir = os.path.dirname(os.path.abspath(thisfile))
14+
# testdata_dir = os.path.join(tests_dir, 'testdata')
15+
16+
17+
class TestMorphShift:
18+
@pytest.fixture
19+
def setup(self):
20+
self.hshift = 2.0
21+
self.vshift = 3.0
22+
23+
# Original dataset goes from 0.1 to 5.0
24+
self.x_morph = numpy.arange(0.01, 5 + self.hshift, 0.01)
25+
self.y_morph = numpy.arange(0.01, 5 + self.hshift, 0.01)
26+
27+
# New dataset is moved to the right by 2.0 and upward by 3.0
28+
self.x_target = numpy.arange(0.01 + self.hshift, 5 + self.hshift, 0.01)
29+
self.y_target = numpy.arange(0.01 + self.vshift, 5 + self.vshift, 0.01)
30+
return
31+
32+
def test_morph(self, setup):
33+
"""check MorphScale.morph()"""
34+
config = {"hshift": self.hshift, "vshift": self.vshift}
35+
morph = MorphShift(config)
36+
37+
x_morph, y_morph, x_target, y_target = morph(self.x_morph, self.y_morph, self.x_target, self.y_target)
38+
39+
# Only care about the shifted data past the shift
40+
# Everything to left of shift is outside our input data domain
41+
assert numpy.allclose(y_morph[x_morph > self.hshift], y_target)
42+
assert numpy.allclose(self.x_target, x_target)
43+
assert numpy.allclose(self.y_target, y_target)
44+
return
45+
46+
47+
# End of class TestMorphScale
48+
49+
if __name__ == "__main__":
50+
TestMorphShift()
51+
52+
# End of file

Diff for: tests/testdata/testsaving/succinct/Morph_Reference_Table.txt

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# scale = None
77
# stretch = None
88
# smear = None
9+
# hshift = None
10+
# vshift = None
911

1012
# Labels: [Target] [Temperature] [Pearson] [Rw]
1113
f_180K.gr 180.0 0.999810 0.020141

Diff for: tests/testdata/testsaving/verbose/Morph_Reference_Table.txt

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# scale = None
77
# stretch = None
88
# smear = None
9+
# hshift = None
10+
# vshift = None
911

1012
# Target: f_180K.gr
1113
# Optimized morphing parameters:

Diff for: tests/testdata/testsaving/verbose/Morphs/mwt_a.cgr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# scale = None
66
# stretch = None
77
# smear = None
8+
# hshift = None
9+
# vshift = None
810

911
# Optimized morphing parameters:
1012
# rmin = 0.000000

Diff for: tests/testdata/testsaving/verbose/Morphs/mwt_b.cgr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# scale = None
66
# stretch = None
77
# smear = None
8+
# hshift = None
9+
# vshift = None
810

911
# Optimized morphing parameters:
1012
# rmin = 0.000000

Diff for: tests/testdata/testsaving/verbose/Morphs/mwt_c.cgr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# scale = None
66
# stretch = None
77
# smear = None
8+
# hshift = None
9+
# vshift = None
810

911
# Optimized morphing parameters:
1012
# rmin = 0.000000

Diff for: tests/testdata/testsaving/verbose/Morphs/mwt_d.cgr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# scale = None
66
# stretch = None
77
# smear = None
8+
# hshift = None
9+
# vshift = None
810

911
# Optimized morphing parameters:
1012
# rmin = 0.000000

Diff for: tests/testdata/testsaving/verbose/Morphs/mwt_e.cgr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# scale = None
66
# stretch = None
77
# smear = None
8+
# hshift = None
9+
# vshift = None
810

911
# Optimized morphing parameters:
1012
# rmin = 0.000000

Diff for: tests/testdata/testsaving/verbose/Morphs/mwt_f.cgr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# scale = None
66
# stretch = None
77
# smear = None
8+
# hshift = None
9+
# vshift = None
810

911
# Optimized morphing parameters:
1012
# rmin = 0.000000

Diff for: tests/testdata/testsaving/verbose/single_verbose_morph.cgr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# scale = None
66
# stretch = None
77
# smear = None
8+
# hshift = None
9+
# vshift = None
810

911
# Optimized morphing parameters:
1012
# rmin = 0.000000

0 commit comments

Comments
 (0)