forked from diffpy/diffpy.labpdfproc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
156 lines (122 loc) · 4.54 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import glob
import os
from pathlib import Path
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
known_sources = [key for key in WAVELENGTHS.keys()]
def set_input_files(args):
"""
Set input directory and files
Parameters
----------
args argparse.Namespace
the arguments from the parser
It is implemented as this:
If input is a file, we first try to read it as a file list and store all listed file names.
If any filename is invalid, then proceed to treat it as a data file.
Otherwise if we have a directory, glob all files within it.
Returns
-------
args argparse.Namespace
"""
if not Path(args.input).exists():
raise ValueError("Please specify valid input file or directory.")
if not Path(args.input).is_dir():
input_dir = Path.cwd() / Path(args.input).parent
file_names = []
with open(args.input, "r") as f:
for line in f:
if not os.path.isfile(line.strip()):
file_names = []
break
else:
file_name = line.strip()
file_names.append(file_name)
if len(file_names) > 0:
input_file_name = file_names
else:
input_file_name = Path(args.input).name
else:
input_dir = Path(args.input).resolve()
input_files = [file for file in glob.glob(str(input_dir) + "/*", recursive=True) if os.path.isfile(file)]
input_file_name = [os.path.basename(input_file_path) for input_file_path in input_files]
setattr(args, "input_directory", input_dir)
setattr(args, "input_file", input_file_name)
return args
def set_output_directory(args):
"""
set the output directory based on the given input arguments
Parameters
----------
args argparse.Namespace
the arguments from the parser
Returns
-------
pathlib.PosixPath that contains the full path of the output directory
it is determined as follows:
If user provides an output directory, use it.
Otherwise, we set it to the current directory if nothing is provided.
We then create the directory if it does not exist.
"""
output_dir = Path(args.output_directory).resolve() if args.output_directory else Path.cwd().resolve()
output_dir.mkdir(parents=True, exist_ok=True)
return output_dir
def set_wavelength(args):
"""
Set the wavelength based on the given input arguments
Parameters
----------
args argparse.Namespace
the arguments from the parser
Returns
-------
float: the wavelength value
we raise an ValueError if the input wavelength is non-positive
or if the input anode_type is not one of the known sources
"""
if args.wavelength is not None and args.wavelength <= 0:
raise ValueError(
"No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength."
)
if not args.wavelength and args.anode_type and args.anode_type not in WAVELENGTHS:
raise ValueError(
f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources, }."
)
if args.wavelength:
return args.wavelength
elif args.anode_type:
return WAVELENGTHS[args.anode_type]
else:
return WAVELENGTHS["Mo"]
def _load_key_value_pair(s):
items = s.split("=")
key = items[0].strip()
if len(items) > 1:
value = "=".join(items[1:])
return (key, value)
def load_user_metadata(args):
"""
Load user metadata into the provided argparse Namespace, raise ValueError if in incorrect format
Parameters
----------
args argparse.Namespace
the arguments from the parser
Returns
-------
the updated argparse Namespace with user metadata inserted as key-value pairs
"""
reserved_keys = vars(args).keys()
if args.user_metadata:
for item in args.user_metadata:
if "=" not in item:
raise ValueError(
"Please provide key-value pairs in the format key=value. "
"For more information, use `labpdfproc --help.`"
)
key, value = _load_key_value_pair(item)
if key in reserved_keys:
raise ValueError(f"{key} is a reserved name. Please rerun using a different key name. ")
if hasattr(args, key):
raise ValueError(f"Please do not specify repeated keys: {key}. ")
setattr(args, key, value)
delattr(args, "user_metadata")
return args