Skip to content

Commit 2cd123e

Browse files
authored
Merge pull request #184 from Sparks29032/bugfix
Bug (unintentional feature) fixes
2 parents 4914488 + 426671e commit 2cd123e

24 files changed

+108
-80
lines changed

doc/source/api/diffpy.morph.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ Subpackages
2020
Submodules
2121
----------
2222

23-
diffpy.morph.pdfplot module
24-
^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
diffpy.morph.plot module
24+
^^^^^^^^^^^^^^^^^^^^^^^^
2525

26-
.. automodule:: diffpy.morph.pdfplot
26+
.. automodule:: diffpy.morph.plot
2727
:members:
2828
:undoc-members:
2929
:show-inheritance:

news/bugfix.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* Swap colors for morph and target. Morph is now blue and target red.
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* Multiple morphs/targets used to break given multiple subdirectories.
20+
* Fixed the Rw calculation. Previously returned an analogue to chi square.
21+
22+
**Security:**
23+
24+
* <news item>

src/diffpy/morph/morphapp.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import diffpy.morph.morph_helpers as helpers
2222
import diffpy.morph.morph_io as io
2323
import diffpy.morph.morphs as morphs
24-
import diffpy.morph.pdfplot as pdfplot
24+
import diffpy.morph.plot as plot
2525
import diffpy.morph.refine as refine
2626
import diffpy.morph.tools as tools
2727
from diffpy.morph import __save_morph_as__
@@ -588,22 +588,22 @@ def single_morph(parser, opts, pargs, stdout_flag=True):
588588
parser.custom_error(save_fail_message)
589589

590590
if opts.plot:
591-
pairlist = [chain.xy_morph_out, chain.xy_target_out]
592-
labels = [pargs[0], pargs[1]] # Default is to use file names
591+
pairlist = [chain.xy_target_out, chain.xy_morph_out]
592+
labels = [pargs[1], pargs[0]] # Default is to use file names
593593

594594
# If user chooses labels
595595
if opts.mlabel is not None:
596-
labels[0] = opts.mlabel
596+
labels[1] = opts.mlabel
597597
if opts.tlabel is not None:
598-
labels[1] = opts.tlabel
598+
labels[0] = opts.tlabel
599599

600600
# Plot extent defaults to calculation extent
601601
pmin = opts.pmin if opts.pmin is not None else opts.rmin
602602
pmax = opts.pmax if opts.pmax is not None else opts.rmax
603603
maglim = opts.maglim
604604
mag = opts.mag
605605
l_width = opts.lwidth
606-
pdfplot.comparePDFs(
606+
plot.compare_funcs(
607607
pairlist,
608608
labels,
609609
rmin=pmin,
@@ -644,9 +644,12 @@ def multiple_targets(parser, opts, pargs, stdout_flag=True):
644644

645645
# Get list of files from target directory
646646
target_list = list(target_directory.iterdir())
647+
to_remove = []
647648
for target in target_list:
648649
if target.is_dir():
649-
target_list.remove(target)
650+
to_remove.append(target)
651+
for target in to_remove:
652+
target_list.remove(target)
650653

651654
# Do not morph morph_file against itself if it is in the same directory
652655
if morph_file in target_list:
@@ -782,13 +785,9 @@ def multiple_targets(parser, opts, pargs, stdout_flag=True):
782785
else:
783786
try:
784787
if field_list is not None:
785-
pdfplot.plot_param(
786-
field_list, param_list, param_name, field
787-
)
788+
plot.plot_param(field_list, param_list, param_name, field)
788789
else:
789-
pdfplot.plot_param(
790-
target_file_names, param_list, param_name
791-
)
790+
plot.plot_param(target_file_names, param_list, param_name)
792791
# Can occur for non-refined plotting parameters
793792
# i.e. --smear is not selected as an option, but smear is the
794793
# plotting parameter
@@ -828,9 +827,12 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True):
828827

829828
# Get list of files from morph directory
830829
morph_list = list(morph_directory.iterdir())
830+
to_remove = []
831831
for morph in morph_list:
832832
if morph.is_dir():
833-
morph_list.remove(morph)
833+
to_remove.append(morph)
834+
for morph in to_remove:
835+
morph_list.remove(morph)
834836

835837
# Do not morph target_file against itself if it is in the same directory
836838
if target_file in morph_list:
@@ -967,13 +969,9 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True):
967969
else:
968970
try:
969971
if field_list is not None:
970-
pdfplot.plot_param(
971-
field_list, param_list, param_name, field
972-
)
972+
plot.plot_param(field_list, param_list, param_name, field)
973973
else:
974-
pdfplot.plot_param(
975-
morph_file_names, param_list, param_name
976-
)
974+
plot.plot_param(morph_file_names, param_list, param_name)
977975
# Can occur for non-refined plotting parameters
978976
# i.e. --smear is not selected as an option, but smear is the
979977
# plotting parameter

src/diffpy/morph/pdfplot.py renamed to src/diffpy/morph/plot.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See LICENSE.txt for license information.
1313
#
1414
##############################################################################
15-
"""Collection of plotting functions for PDFs."""
15+
"""Collection of plotting functions (originally specifically) for PDFs."""
1616

1717
import matplotlib.pyplot as plt
1818
import numpy
@@ -23,8 +23,8 @@
2323

2424
# FIXME - make this return the figure object in the future, so several views
2525
# can be composed.
26-
def plotPDFs(pairlist, labels=None, offset="auto", rmin=None, rmax=None):
27-
"""Plots several PDFs on top of one another.
26+
def plot_funcs(pairlist, labels=None, offset="auto", rmin=None, rmax=None):
27+
"""Plots several functions g(r) on top of one another.
2828
2929
Parameters
3030
----------
@@ -34,15 +34,15 @@ def plotPDFs(pairlist, labels=None, offset="auto", rmin=None, rmax=None):
3434
Iterable of names for the pairs. If this is not the same length as
3535
the pairlist, a legend will not be shown (default []).
3636
offset
37-
Offset to place between plots. PDFs will be sequentially shifted in
38-
the y-direction by the offset. If offset is 'auto' (default), the
37+
Offset to place between plots. Functions will be sequentially shifted
38+
in the y-direction by the offset. If offset is 'auto' (default), the
3939
optimal offset will be determined automatically.
4040
rmin
4141
The minimum r-value to plot. If this is None (default), the lower
42-
bound of the PDF is not altered.
42+
bound of the function is not altered.
4343
rmax
4444
The maximum r-value to plot. If this is None (default), the upper
45-
bound of the PDF is not altered.
45+
bound of the function is not altered.
4646
"""
4747
if labels is None:
4848
labels = []
@@ -68,7 +68,7 @@ def plotPDFs(pairlist, labels=None, offset="auto", rmin=None, rmax=None):
6868
return
6969

7070

71-
def comparePDFs(
71+
def compare_funcs(
7272
pairlist,
7373
labels=None,
7474
rmin=None,
@@ -80,10 +80,10 @@ def comparePDFs(
8080
legend=True,
8181
l_width=1.5,
8282
):
83-
"""Plot two PDFs on top of each other and difference curve.
83+
"""Plot two functions g(r) on top of each other and difference curve.
8484
85-
The second PDF will be shown as blue circles below and the first as a red
86-
line. The difference curve will be in green and offset for clarity.
85+
The second function will be shown as blue circles below and the first as
86+
a red line. The difference curve will be in green and offset for clarity.
8787
8888
Parameters
8989
----------
@@ -94,10 +94,10 @@ def comparePDFs(
9494
the pairlist, a legend will not be shown (default []).
9595
rmin
9696
The minimum r-value to plot. If this is None (default), the lower
97-
bound of the PDF is not altered.
97+
bound of the function is not altered.
9898
rmax
9999
The maximum r-value to plot. If this is None (default), the upper
100-
bound of the PDF is not altered.
100+
bound of the function is not altered.
101101
show
102102
Show the plot (default True)
103103
maglim
@@ -158,7 +158,7 @@ def comparePDFs(
158158
offset = -1.1 * (ymax - ymin)
159159

160160
# Scale the x-limit based on the r-extent of the signal. This gives a nice
161-
# density of PDF peaks.
161+
# density of function peaks.
162162
rlim = rvmax - rvmin
163163
scale = rlim / 25.0
164164
# Set a reasonable minimum of .8 and maximum of 1
@@ -305,21 +305,21 @@ def plot_param(target_labels, param_list, param_name=None, field=None):
305305
return
306306

307307

308-
def truncatePDFs(r, gr, rmin=None, rmax=None):
309-
"""Truncate a PDF to specified bounds.
308+
def truncate_func(r, gr, rmin=None, rmax=None):
309+
"""Truncate a function g(r) to specified bounds.
310310
311311
Parameters
312312
----------
313313
r
314-
r-values of the PDF.
314+
The r-values of the function g(r).
315315
gr
316-
PDF g(r) values.
316+
Function g(r) values.
317317
rmin
318318
The minimum r-value. If this is None (default), the lower bound of
319-
the PDF is not altered.
319+
the function is not altered.
320320
rmax
321321
The maximum r-value. If this is None (default), the upper bound of
322-
the PDF is not altered.
322+
the function is not altered.
323323
324324
Returns
325325
-------
@@ -340,7 +340,7 @@ def truncatePDFs(r, gr, rmin=None, rmax=None):
340340

341341

342342
def _find_offset(pairlist):
343-
"""Find an optimal offset between PDFs."""
343+
"""Find an optimal offset between functions."""
344344
maxlist = [max(p[1]) for p in pairlist]
345345
minlist = [min(p[1]) for p in pairlist]
346346
difflist = numpy.subtract(maxlist[:-1], minlist[1:])

src/diffpy/morph/tools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ def getRw(chain):
9393
# Make sure we put these on the proper grid
9494
x_morph, y_morph, x_target, y_target = chain.xyallout
9595
diff = y_target - y_morph
96-
rw = numpy.dot(diff, diff)
97-
rw /= numpy.dot(y_target, y_target)
98-
rw = rw**0.5
96+
rw = numpy.dot(x_morph * diff, diff)
97+
rw /= numpy.dot(x_morph * y_morph, y_morph)
98+
rw = rw
9999
return rw
100100

101101

tests/test_morphapp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
nickel_PDF = testdata_dir.joinpath("nickel_ss0.01.cgr")
1919
serial_JSON = testdata_dir.joinpath("testsequence_serialfile.json")
2020

21-
testsaving_dir = testdata_dir.joinpath("testsaving")
21+
testsaving_dir = testsequence_dir.joinpath("testsaving")
2222
test_saving_succinct = testsaving_dir.joinpath("succinct")
2323
test_saving_verbose = testsaving_dir.joinpath("verbose")
2424
tssf = testdata_dir.joinpath("testsequence_serialfile.json")

tests/test_morphio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
testdata_dir = tests_dir.joinpath("testdata")
2222
testsequence_dir = testdata_dir.joinpath("testsequence")
2323

24-
testsaving_dir = testdata_dir.joinpath("testsaving")
24+
testsaving_dir = testsequence_dir.joinpath("testsaving")
2525
test_saving_succinct = testsaving_dir.joinpath("succinct")
2626
test_saving_verbose = testsaving_dir.joinpath("verbose")
2727
tssf = testdata_dir.joinpath("testsequence_serialfile.json")

tests/test_tools.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,16 @@ def test_nn_value(self, setup):
6060
pytest.approx(tools.nn_value(-value, name=None), abs(-value))
6161

6262
def test_field_sort(self, setup):
63-
sequence_files = [*os.listdir(testsequence_dir)]
63+
sequence_files = [file for file in Path(testsequence_dir).iterdir()]
64+
to_remove = []
65+
for file in sequence_files:
66+
if file.is_dir():
67+
to_remove.append(file)
68+
for d in to_remove:
69+
sequence_files.remove(d)
6470
absolute_sf = []
6571
for file in sequence_files:
66-
absolute_sf.append(os.path.join(testsequence_dir, file))
72+
absolute_sf.append(Path(testsequence_dir) / file.name)
6773

6874
# Fisher-Yates randomization
6975
import random
@@ -87,8 +93,8 @@ def test_field_sort(self, setup):
8793

8894
# Temperature sort should produce same result as alphanumerical if
8995
# leading character is removed
90-
sequence_files.sort(key=lambda entry: entry[2:])
91-
assert sequence_files == sorted_sequence
96+
sequence_files.sort(key=lambda entry: entry.name[2:])
97+
assert [file.name for file in sequence_files] == sorted_sequence
9298

9399
# Check temperatures are correct
94100
assert fvs == [174, 180, 186, 192, 198, 204, 210]
@@ -103,7 +109,7 @@ def test_field_sort(self, setup):
103109

104110
# Reversed sort should match alphanumerical sort
105111
sequence_files.sort()
106-
assert sequence_files == reversed_sequence
112+
assert [file.name for file in sequence_files] == reversed_sequence
107113

108114
# Check we get the same sequence when we load header information from
109115
# a serial file
@@ -116,7 +122,7 @@ def test_field_sort(self, setup):
116122
metadata_sequence = []
117123
for path in metadata_path_sequence:
118124
metadata_sequence.append(path.name)
119-
assert sequence_files == metadata_sequence
125+
assert [file.name for file in sequence_files] == metadata_sequence
120126

121127
# Check error thrown when field does not exist
122128
with pytest.raises(KeyError):

tests/testdata/testsaving/succinct/Morph_Reference_Table.txt renamed to tests/testdata/testsequence/testsaving/succinct/Morph_Reference_Table.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
# vshift = None
1111

1212
# Labels: [Target] [Temperature] [Pearson] [Rw]
13-
f_180K.gr 180.0 0.999810 0.020141
14-
e_186K.gr 186.0 0.999424 0.034859
15-
d_192K.gr 192.0 0.998077 0.062392
16-
c_198K.gr 198.0 0.994409 0.105918
17-
b_204K.gr 204.0 0.993160 0.117595
18-
a_210K.gr 210.0 0.992111 0.127100
13+
f_180K.gr 180.0 0.999810 0.000674
14+
e_186K.gr 186.0 0.999424 0.002047
15+
d_192K.gr 192.0 0.998077 0.008405
16+
c_198K.gr 198.0 0.994409 0.026768
17+
b_204K.gr 204.0 0.993160 0.031685
18+
a_210K.gr 210.0 0.992111 0.035072

0 commit comments

Comments
 (0)