Skip to content

Commit 5c3eed7

Browse files
authored
Merge pull request #59 from yucongalicechen/input_dir4
implement wildcard pattern for input
2 parents c45022d + bfa3d8e commit 5c3eed7

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from diffpy.labpdfproc.functions import apply_corr, compute_cve
55
from diffpy.labpdfproc.tools import (
6-
expand_list_file,
76
known_sources,
87
load_user_metadata,
98
set_input_lists,
@@ -29,7 +28,9 @@ def get_args(override_cli_inputs=None):
2928
"'.' (load everything in the current directory), 'data' (load"
3029
"everything in the folder ./data), 'data/file_list.txt' (load"
3130
" the list of files contained in the text-file called "
32-
"file_list.txt that can be found in the folder ./data).",
31+
"file_list.txt that can be found in the folder ./data), "
32+
"'./*.chi', 'data/*.chi' (load all files with extension .chi in the "
33+
"folder ./data).",
3334
)
3435
p.add_argument(
3536
"-a",
@@ -95,13 +96,12 @@ def get_args(override_cli_inputs=None):
9596

9697
def main():
9798
args = get_args()
98-
args = expand_list_file(args)
9999
args = set_input_lists(args)
100100
args.output_directory = set_output_directory(args)
101101
args.wavelength = set_wavelength(args)
102102
args = load_user_metadata(args)
103103

104-
for filepath in args.input_directory:
104+
for filepath in args.input_paths:
105105
outfilestem = filepath.stem + "_corrected"
106106
corrfilestem = filepath.stem + "_cve"
107107
outfile = args.output_directory / (outfilestem + ".chi")
@@ -119,13 +119,13 @@ def main():
119119
)
120120

121121
input_pattern = Diffraction_object(wavelength=args.wavelength)
122-
xarray, yarray = loadData(args.input_file, unpack=True)
122+
xarray, yarray = loadData(filepath, unpack=True)
123123
input_pattern.insert_scattering_quantity(
124124
xarray,
125125
yarray,
126126
"tth",
127127
scat_quantity="x-ray",
128-
name=str(args.input_file),
128+
name=filepath.stem,
129129
metadata={"muD": args.mud, "anode_type": args.anode_type},
130130
)
131131

src/diffpy/labpdfproc/tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def user_filesystem(tmp_path):
3939
with open(input_dir / "file_list.txt", "w") as f:
4040
f.write("good_data.chi \n good_data.xy \n good_data.txt \n missing_file.txt")
4141
with open(input_dir / "file_list_example2.txt", "w") as f:
42+
f.write("input_dir/*.txt \n")
4243
f.write("input_dir/good_data.chi \n")
4344
f.write("good_data.xy \n")
4445
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from diffpy.labpdfproc.labpdfprocapp import get_args
88
from diffpy.labpdfproc.tools import (
9-
expand_list_file,
109
known_sources,
1110
load_user_metadata,
1211
set_input_lists,
@@ -50,9 +49,21 @@
5049
"input_dir/binary.pkl",
5150
],
5251
),
53-
( # file_list_example2.txt list of files provided in different directories
52+
( # file_list_example2.txt list of files provided in different directories with wildcard
5453
["input_dir/file_list_example2.txt"],
55-
["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"],
54+
["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt", "input_dir/unreadable_file.txt"],
55+
),
56+
( # wildcard pattern, matching files with .chi extension in the same directory
57+
["./*.chi"],
58+
["good_data.chi"],
59+
),
60+
( # wildcard pattern, matching files with .chi extension in the input directory
61+
["input_dir/*.chi"],
62+
["input_dir/good_data.chi"],
63+
),
64+
( # wildcard pattern, matching files starting with good_data
65+
["good_data*"],
66+
["good_data.chi", "good_data.xy", "good_data.txt"],
5667
),
5768
]
5869

@@ -65,7 +76,6 @@ def test_set_input_lists(inputs, expected, user_filesystem):
6576

6677
cli_inputs = ["2.5"] + inputs
6778
actual_args = get_args(cli_inputs)
68-
actual_args = expand_list_file(actual_args)
6979
actual_args = set_input_lists(actual_args)
7080
assert sorted(actual_args.input_paths) == sorted(expected_paths)
7181

@@ -98,7 +108,6 @@ def test_set_input_files_bad(inputs, msg, user_filesystem):
98108
os.chdir(base_dir)
99109
cli_inputs = ["2.5"] + inputs
100110
actual_args = get_args(cli_inputs)
101-
actual_args = expand_list_file(actual_args)
102111
with pytest.raises(FileNotFoundError, match=msg[0]):
103112
actual_args = set_input_lists(actual_args)
104113

src/diffpy/labpdfproc/tools.py

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

3030

31-
def expand_list_file(args):
31+
def _expand_user_input(args):
3232
"""
33-
Expands the list of inputs by adding files from file lists and removing the file list.
33+
Expands the list of inputs by adding files from file lists and wildcards.
3434
3535
Parameters
3636
----------
@@ -48,6 +48,11 @@ def expand_list_file(args):
4848
file_inputs = [input_name.strip() for input_name in f.readlines()]
4949
args.input.extend(file_inputs)
5050
args.input.remove(file_list_input)
51+
wildcard_inputs = [input_name for input_name in args.input if "*" in input_name]
52+
for wildcard_input in wildcard_inputs:
53+
input_files = [str(file) for file in Path(".").glob(wildcard_input) if "file_list" not in file.name]
54+
args.input.extend(input_files)
55+
args.input.remove(wildcard_input)
5156
return args
5257

5358

@@ -70,6 +75,7 @@ def set_input_lists(args):
7075
"""
7176

7277
input_paths = []
78+
args = _expand_user_input(args)
7379
for input_name in args.input:
7480
input_path = Path(input_name).resolve()
7581
if input_path.exists():
@@ -86,7 +92,7 @@ def set_input_lists(args):
8692
f"Cannot find {input_name}. Please specify valid input file(s) or directories."
8793
)
8894
else:
89-
raise FileNotFoundError(f"Cannot find {input_name}")
95+
raise FileNotFoundError(f"Cannot find {input_name}.")
9096
setattr(args, "input_paths", list(set(input_paths)))
9197
return args
9298

0 commit comments

Comments
 (0)