Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert version.py, add tests
  • Loading branch information
Andrew Yang
Andrew Yang committed Jul 5, 2023
commit 69c154436c0ce734960489ad14071f2c31bd219d
4 changes: 2 additions & 2 deletions diffpy/pdfmorph/tests/test_morphsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
class TestMorphSequence(unittest.TestCase):
def setUp(self):
self.parser = create_option_parser()
filenames = ["174K.gr", "180K.gr", "186K.gr", "192K.gr", "198K.gr", "204K.gr", "210K.gr"]
(self.opts, pargs) = self.parser.parse_args(["--scale", "1", "--stretch", "0", "-n"])
filenames = ["g_174K.gr", "f_180K.gr", "e_186K.gr", "d_192K.gr", "c_198K.gr", "b_204K.gr", "a_210K.gr"]
(self.opts, pargs) = self.parser.parse_args(["--scale", "1", "--stretch", "0", "-n", "--temperature"])
self.testfiles = []
for filename in filenames:
self.testfiles.append(testsequence_dir.joinpath(filename))
Expand Down
39 changes: 37 additions & 2 deletions diffpy/pdfmorph/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import unittest
import numpy

from pathlib import Path

# useful variables
thisfile = locals().get('__file__', 'file.py')
tests_dir = os.path.dirname(os.path.abspath(thisfile))
testdata_dir = os.path.join(tests_dir, 'testdata')
testsequence_dir = os.path.join(testdata_dir, 'testsequence')

import diffpy.pdfmorph.tools as tools

Expand Down Expand Up @@ -41,10 +44,42 @@ def test_estimateScale(self):
return

def test_nn_value(self):
pass
import random

# Values with 6 and 7 decimals (limit of assertAlmostEqual)
test_values = [10.0000001, 10.00000001, 0.9999999, 0.99999999]

# Random values
for i in range(100):
test_values.append(random.uniform(0, 65535))

# Check positive and negative
for value in test_values:
self.assertAlmostEqual(tools.nn_value(value, name=None), abs(value))
self.assertAlmostEqual(tools.nn_value(-value, name=None), abs(-value))

def test_temperature_sort(self):
pass
sequence_files = [*os.listdir(testsequence_dir)]

# Fisher-Yates randomization
import random
length = len(sequence_files)
for i in range(length - 1, 0, -1):
j = random.randint(0, i)
sequence_files[i], sequence_files[j] = sequence_files[j], sequence_files[i]

# Prepare and run through temperature_sort
path_sequence = []
for file in sequence_files:
path_sequence.append(Path(file))
sorted_path_sequence = tools.temperature_sort(path_sequence)
sorted_sequence = []
for path in sorted_path_sequence:
sorted_sequence.append(path.name)

# Temperature sort should produce same result as alphanumerical if leading character is removed
sequence_files.sort(key=lambda entry: entry[2:])
assert sequence_files == sorted_sequence


# End of class TestRoutines
Expand Down
9 changes: 5 additions & 4 deletions diffpy/pdfmorph/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ def nn_value(val, name):


def temperature_sort(filenames):
# Sort files (in list filenames) ending in _#K.gr/_#K.cgr by # rather than alphabetical
# Sort files (whose paths are contained in filenames) ending in _###K.gr/_###K.cgr by ###
for idx in range(len(filenames)):
s_index = filenames[idx].rfind("_") # Start of temperature value
e_index = filenames[idx].rfind("K") # End of temperature value
temp = float(filenames[s_index + 1, e_index])
filename = filenames[idx].name
s_index = filename.rfind("_") # Start of temperature value
e_index = filename.rfind("K") # End of temperature value
temp = float(filename[s_index + 1: e_index])
filenames[idx] = [filenames[idx], temp]
filenames.sort(key=lambda entry: entry[1])
return [entry[0] for entry in filenames]
5 changes: 3 additions & 2 deletions diffpy/pdfmorph/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"""Definition of __version__ and __date__ for diffpy.pdfmorph.
"""


# obtain version information
from importlib_metadata import version
from pkg_resources import get_distribution

__version__ = version('diffpy.pdfmorph')
__version__ = get_distribution('diffpy.pdfmorph').version

# we assume that tag_date was used and __version__ ends in YYYYMMDD
__date__ = __version__[-8:-4] + '-' + __version__[-4:-2] + '-' + __version__[-2:]
Expand Down