Skip to content

Commit 495b394

Browse files
authored
Merge pull request #54 from diffpy/input
input
2 parents 44b2902 + 17df848 commit 495b394

File tree

4 files changed

+171
-17
lines changed

4 files changed

+171
-17
lines changed
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
1-
import os
21
from pathlib import Path
32

43
import pytest
54

65

76
@pytest.fixture
87
def user_filesystem(tmp_path):
9-
directory = Path(tmp_path)
10-
os.chdir(directory)
11-
12-
input_dir = Path(tmp_path).resolve() / "input_dir"
8+
base_dir = Path(tmp_path)
9+
input_dir = base_dir / "input_dir"
1310
input_dir.mkdir(parents=True, exist_ok=True)
1411

1512
chi_data = "dataformat = twotheta\n mode = xray\n # chi_Q chi_I\n 1 2\n 3 4\n 5 6\n 7 8\n"
1613
xy_data = "1 2\n 3 4\n 5 6\n 7 8"
17-
unreadable_data = "This is an unreadable file."
14+
unreadable_data = "This is a file with no data that is non-readable by " "LoadData"
1815
binary_data = b"\x00\x01\x02\x03\x04"
1916

20-
with open("good_data.chi", "w") as f:
17+
with open(base_dir / "good_data.chi", "w") as f:
2118
f.write(chi_data)
22-
with open("good_data.xy", "w") as f:
19+
with open(base_dir / "good_data.xy", "w") as f:
2320
f.write(xy_data)
24-
with open("good_data.txt", "w") as f:
21+
with open(base_dir / "good_data.txt", "w") as f:
2522
f.write(chi_data)
26-
with open("unreadable_file.txt", "w") as f:
23+
with open(base_dir / "unreadable_file.txt", "w") as f:
2724
f.write(unreadable_data)
28-
with open("binary.pkl", "wb") as f:
25+
with open(base_dir / "binary.pkl", "wb") as f:
2926
f.write(binary_data)
3027

31-
with open(os.path.join(input_dir, "good_data.chi"), "w") as f:
28+
with open(input_dir / "good_data.chi", "w") as f:
3229
f.write(chi_data)
33-
with open(os.path.join(input_dir, "good_data.xy"), "w") as f:
30+
with open(input_dir / "good_data.xy", "w") as f:
3431
f.write(xy_data)
35-
with open(os.path.join(input_dir, "good_data.txt"), "w") as f:
32+
with open(input_dir / "good_data.txt", "w") as f:
3633
f.write(chi_data)
37-
with open(os.path.join(input_dir, "unreadable_file.txt"), "w") as f:
34+
with open(input_dir / "unreadable_file.txt", "w") as f:
3835
f.write(unreadable_data)
39-
with open(os.path.join(input_dir, "binary.pkl"), "wb") as f:
36+
with open(input_dir / "binary.pkl", "wb") as f:
4037
f.write(binary_data)
4138

39+
with open(input_dir / "file_list.txt", "w") as f:
40+
f.write("good_data.chi \n good_data.xy \n good_data.txt \n missing_file.txt")
41+
with open(input_dir / "file_list_example2.txt", "w") as f:
42+
f.write("input_dir/good_data.chi \n")
43+
f.write("good_data.xy \n")
44+
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")
45+
4246
yield tmp_path
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
3+
import pytest
4+
5+
from diffpy.utils.parsers import loadData
6+
7+
8+
# Test that our readable and unreadable files are indeed readable and
9+
# unreadable by loadData (which is our definition of readable and unreadable)
10+
def test_loadData_with_input_files(user_filesystem):
11+
os.chdir(user_filesystem)
12+
xarray_chi, yarray_chi = loadData("good_data.chi", unpack=True)
13+
xarray_xy, yarray_xy = loadData("good_data.xy", unpack=True)
14+
xarray_txt, yarray_txt = loadData("good_data.txt", unpack=True)
15+
with pytest.raises(ValueError):
16+
xarray_txt, yarray_txt = loadData("unreadable_file.txt", unpack=True)
17+
with pytest.raises(ValueError):
18+
xarray_pkl, yarray_pkl = loadData("binary.pkl", unpack=True)

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,104 @@
1+
import os
12
import re
23
from pathlib import Path
34

45
import pytest
56

67
from diffpy.labpdfproc.labpdfprocapp import get_args
7-
from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_output_directory, set_wavelength
8+
from diffpy.labpdfproc.tools import (
9+
known_sources,
10+
load_user_metadata,
11+
set_input_lists,
12+
set_output_directory,
13+
set_wavelength,
14+
)
15+
16+
# Use cases can be found here: https://github.com/diffpy/diffpy.labpdfproc/issues/48
17+
18+
# This test covers existing single input file, directory, a file list, and multiple files
19+
# We store absolute path into input_directory and file names into input_file
20+
params_input = [
21+
(["good_data.chi"], ["good_data.chi"]), # single good file, same directory
22+
(["input_dir/good_data.chi"], ["input_dir/good_data.chi"]), # single good file, input directory
23+
( # glob current directory
24+
["."],
25+
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
26+
),
27+
( # glob input directory
28+
["./input_dir"],
29+
[
30+
"input_dir/good_data.chi",
31+
"input_dir/good_data.xy",
32+
"input_dir/good_data.txt",
33+
"input_dir/unreadable_file.txt",
34+
"input_dir/binary.pkl",
35+
],
36+
),
37+
( # glob list of input directories
38+
[".", "./input_dir"],
39+
[
40+
"./good_data.chi",
41+
"./good_data.xy",
42+
"./good_data.txt",
43+
"./unreadable_file.txt",
44+
"./binary.pkl",
45+
"input_dir/good_data.chi",
46+
"input_dir/good_data.xy",
47+
"input_dir/good_data.txt",
48+
"input_dir/unreadable_file.txt",
49+
"input_dir/binary.pkl",
50+
],
51+
),
52+
( # file_list.txt list of files provided
53+
["input_dir/file_list.txt"],
54+
["good_data.chi", "good_data.xy", "good_data.txt"],
55+
),
56+
( # file_list_example2.txt list of files provided in different directories
57+
["input_dir/file_list_example2.txt"],
58+
["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"],
59+
),
60+
]
61+
62+
63+
@pytest.mark.parametrize("inputs, expected", params_input)
64+
def test_set_input_lists(inputs, expected, user_filesystem):
65+
base_dir = Path(user_filesystem)
66+
os.chdir(base_dir)
67+
expected_paths = [base_dir.resolve() / expected_path for expected_path in expected]
68+
69+
cli_inputs = ["2.5"] + inputs
70+
actual_args = get_args(cli_inputs)
71+
actual_args = set_input_lists(actual_args)
72+
assert list(actual_args.input_paths).sort() == expected_paths.sort()
73+
74+
75+
# This test covers non-existing single input file or directory, in this case we raise an error with message
76+
params_input_bad = [
77+
(
78+
["non_existing_file.xy"],
79+
"Cannot find non_existing_file.xy. Please specify valid input file(s) or directories.",
80+
),
81+
(
82+
["./input_dir/non_existing_file.xy"],
83+
"Cannot find ./input_dir/non_existing_file.xy. Please specify valid input file(s) or directories.",
84+
),
85+
(["./non_existing_dir"], "Cannot find ./non_existing_dir. Please specify valid input file(s) or directories."),
86+
( # list of files provided (with missing files)
87+
["good_data.chi", "good_data.xy", "unreadable_file.txt", "missing_file.txt"],
88+
"Cannot find missing_file.txt. Please specify valid input file(s) or directories.",
89+
),
90+
]
91+
92+
93+
@pytest.mark.parametrize("inputs, msg", params_input_bad)
94+
def test_set_input_files_bad(inputs, msg, user_filesystem):
95+
base_dir = Path(user_filesystem)
96+
os.chdir(base_dir)
97+
cli_inputs = ["2.5"] + inputs
98+
actual_args = get_args(cli_inputs)
99+
with pytest.raises(FileNotFoundError, match=msg[0]):
100+
actual_args = set_input_lists(actual_args)
101+
8102

9103
params1 = [
10104
([], ["."]),
@@ -16,6 +110,7 @@
16110

17111
@pytest.mark.parametrize("inputs, expected", params1)
18112
def test_set_output_directory(inputs, expected, user_filesystem):
113+
os.chdir(user_filesystem)
19114
expected_output_directory = Path(user_filesystem) / expected[0]
20115
cli_inputs = ["2.5", "data.xy"] + inputs
21116
actual_args = get_args(cli_inputs)
@@ -26,6 +121,7 @@ def test_set_output_directory(inputs, expected, user_filesystem):
26121

27122

28123
def test_set_output_directory_bad(user_filesystem):
124+
os.chdir(user_filesystem)
29125
cli_inputs = ["2.5", "data.xy", "--output-directory", "good_data.chi"]
30126
actual_args = get_args(cli_inputs)
31127
with pytest.raises(FileExistsError):

src/diffpy/labpdfproc/tools.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,42 @@ def set_output_directory(args):
2828
return output_dir
2929

3030

31+
def set_input_lists(args):
32+
"""
33+
Set input directory and files.
34+
35+
It takes cli inputs, checks if they are files or directories and creates
36+
a list of files to be processed which is stored in the args Namespace.
37+
38+
Parameters
39+
----------
40+
args argparse.Namespace
41+
the arguments from the parser
42+
43+
Returns
44+
-------
45+
args argparse.Namespace
46+
47+
"""
48+
49+
input_paths = []
50+
for input in args.input:
51+
input_path = Path(input).resolve()
52+
if input_path.exists():
53+
if input_path.is_file():
54+
input_paths.append(input_path)
55+
elif input_path.is_dir():
56+
input_files = input_path.glob("*")
57+
input_files = [file.resolve() for file in input_files if file.is_file()]
58+
input_paths.extend(input_files)
59+
else:
60+
raise FileNotFoundError(f"Cannot find {input}. Please specify valid input file(s) or directories.")
61+
else:
62+
raise FileNotFoundError(f"Cannot find {input}")
63+
setattr(args, "input_paths", input_paths)
64+
return args
65+
66+
3167
def set_wavelength(args):
3268
"""
3369
Set the wavelength based on the given input arguments

0 commit comments

Comments
 (0)