Skip to content

Commit fad4f07

Browse files
committed
Add tests
1 parent 76d1bd1 commit fad4f07

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/diffpy/pdfmorph/morphs/morphshift.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ class MorphShift(Morph):
4848
def morph(self, x_morph, y_morph, x_target, y_target):
4949
"""Apply the shifts."""
5050
try:
51-
self.hshift
51+
hshift = self.hshift
5252
except AttributeError:
53-
self.hshift = 0
53+
hshift = 0
5454
try:
55-
self.vshift
55+
vshift = self.vshift
5656
except AttributeError:
57-
self.vshift = 0
57+
vshift = 0
5858

5959
Morph.morph(self, x_morph, y_morph, x_target, y_target)
60-
r = self.x_morph_in - self.hshift
60+
r = self.x_morph_in - hshift
6161
self.y_morph_out = numpy.interp(r, self.x_morph_in, self.y_morph_in)
62-
self.y_morph_out += self.vshift
62+
self.y_morph_out += vshift
6363
return self.xyallout
6464

6565

tests/test_morphshift.py

Lines changed: 52 additions & 0 deletions
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

0 commit comments

Comments
 (0)