13
13
14
14
15
15
def set_output_directory (args ):
16
- """
17
- set the output directory based on the given input arguments
18
-
19
- Parameters
20
- ----------
21
- args argparse.Namespace
22
- the arguments from the parser
16
+ """Set the output directory based on the given input arguments.
23
17
24
- it is determined as follows:
18
+ It is determined as follows:
25
19
If user provides an output directory, use it.
26
20
Otherwise, we set it to the current directory if nothing is provided.
27
21
We then create the directory if it does not exist.
28
22
23
+ Parameters
24
+ ----------
25
+ args : argparse.Namespace
26
+ The arguments from the parser.
27
+
29
28
Returns
30
29
-------
31
- a Path object that contains the full path of the output directory
30
+ args : argparse.Namespace
31
+ The updated arguments, with output_directory as the full path to the output file directory.
32
32
"""
33
33
output_dir = Path (args .output_directory ).resolve () if args .output_directory else Path .cwd ().resolve ()
34
34
output_dir .mkdir (parents = True , exist_ok = True )
35
- return output_dir
35
+ args .output_directory = output_dir
36
+ return args
36
37
37
38
38
39
def _expand_user_input (args ):
39
- """
40
- Expands the list of inputs by adding files from file lists and wildcards.
40
+ """Expand the list of inputs by adding files from file lists and wildcards.
41
41
42
42
Parameters
43
43
----------
44
- args argparse.Namespace
45
- the arguments from the parser
44
+ args : argparse.Namespace
45
+ The arguments from the parser.
46
46
47
47
Returns
48
48
-------
49
- the arguments with the modified input list
50
-
49
+ args : argparse.Namespace
50
+ The updated arguments with the modified input list.
51
51
"""
52
52
file_list_inputs = [input_name for input_name in args .input if "file_list" in input_name ]
53
53
for file_list_input in file_list_inputs :
@@ -57,28 +57,34 @@ def _expand_user_input(args):
57
57
args .input .remove (file_list_input )
58
58
wildcard_inputs = [input_name for input_name in args .input if "*" in input_name ]
59
59
for wildcard_input in wildcard_inputs :
60
- input_files = [str (file ) for file in Path ("." ).glob (wildcard_input ) if "file_list" not in file .name ]
60
+ input_files = [
61
+ str (file )
62
+ for file in Path ("." ).glob (wildcard_input )
63
+ if "file_list" not in file .name and "diffpyconfig.json" not in file .name
64
+ ]
61
65
args .input .extend (input_files )
62
66
args .input .remove (wildcard_input )
63
67
return args
64
68
65
69
66
70
def set_input_lists (args ):
67
- """
68
- Set input directory and files.
69
-
70
- It takes cli inputs, checks if they are files or directories and creates
71
+ """Set input directory and files. It takes cli inputs, checks if they are files or directories and creates
71
72
a list of files to be processed which is stored in the args Namespace.
72
73
73
74
Parameters
74
75
----------
75
- args argparse.Namespace
76
- the arguments from the parser
76
+ args : argparse.Namespace
77
+ The arguments from the parser.
78
+
79
+ Raises
80
+ ------
81
+ FileNotFoundError
82
+ Raised when an input is invalid.
77
83
78
84
Returns
79
85
-------
80
- args argparse.Namespace
81
-
86
+ args : argparse.Namespace
87
+ The updated arguments with the modified input list.
82
88
"""
83
89
84
90
input_paths = []
@@ -91,34 +97,41 @@ def set_input_lists(args):
91
97
elif input_path .is_dir ():
92
98
input_files = input_path .glob ("*" )
93
99
input_files = [
94
- file .resolve () for file in input_files if file .is_file () and "file_list" not in file .name
100
+ file .resolve ()
101
+ for file in input_files
102
+ if file .is_file () and "file_list" not in file .name and "diffpyconfig.json" not in file .name
95
103
]
96
104
input_paths .extend (input_files )
97
105
else :
98
106
raise FileNotFoundError (
99
107
f"Cannot find { input_name } . Please specify valid input file(s) or directories."
100
108
)
101
109
else :
102
- raise FileNotFoundError (f"Cannot find { input_name } ." )
110
+ raise FileNotFoundError (
111
+ f"Cannot find { input_name } . Please specify valid input file(s) or directories."
112
+ )
103
113
setattr (args , "input_paths" , list (set (input_paths )))
104
114
return args
105
115
106
116
107
117
def set_wavelength (args ):
108
- """
109
- Set the wavelength based on the given input arguments
118
+ """Set the wavelength based on the given anode_type. If a wavelength is provided,
119
+ it will be used, and the anode_type argument will be removed.
110
120
111
121
Parameters
112
122
----------
113
- args argparse.Namespace
114
- the arguments from the parser
123
+ args : argparse.Namespace
124
+ The arguments from the parser.
115
125
116
- we raise a ValueError if the input wavelength is non-positive
117
- or if the input anode_type is not one of the known sources
126
+ Raises
127
+ ------
128
+ ValueError
129
+ Raised when input wavelength is non-positive or if input anode_type is not one of the known sources.
118
130
119
131
Returns
120
132
-------
121
- args argparse.Namespace
133
+ args : argparse.Namespace
134
+ The updated arguments with the wavelength.
122
135
"""
123
136
if args .wavelength is not None and args .wavelength <= 0 :
124
137
raise ValueError (
@@ -139,17 +152,17 @@ def set_wavelength(args):
139
152
140
153
141
154
def set_xtype (args ):
142
- f"""
143
- Set the xtype based on the given input arguments, raise an error if xtype is not one of { * XQUANTITIES , }
155
+ f"""Set the xtype based on the given input arguments, raise an error if xtype is not one of { * XQUANTITIES , } .
144
156
145
157
Parameters
146
158
----------
147
- args argparse.Namespace
148
- the arguments from the parser
159
+ args : argparse.Namespace
160
+ The arguments from the parser.
149
161
150
162
Returns
151
163
-------
152
- args argparse.Namespace
164
+ args : argparse.Namespace
165
+ The updated arguments with the xtype as one of q, tth, or d.
153
166
"""
154
167
if args .xtype .lower () not in XQUANTITIES :
155
168
raise ValueError (f"Unknown xtype: { args .xtype } . Allowed xtypes are { * XQUANTITIES , } ." )
@@ -160,17 +173,17 @@ def set_xtype(args):
160
173
161
174
162
175
def set_mud (args ):
163
- """
164
- Set the mud based on the given input arguments
176
+ """Compute mu*D based on the given z-scan file, if provided.
165
177
166
178
Parameters
167
179
----------
168
- args argparse.Namespace
169
- the arguments from the parser
180
+ args : argparse.Namespace
181
+ The arguments from the parser.
170
182
171
183
Returns
172
184
-------
173
- args argparse.Namespace
185
+ args : argparse.Namespace
186
+ The updated arguments with mu*D.
174
187
"""
175
188
if args .z_scan_file :
176
189
filepath = Path (args .z_scan_file ).resolve ()
@@ -186,22 +199,21 @@ def _load_key_value_pair(s):
186
199
key = items [0 ].strip ()
187
200
if len (items ) > 1 :
188
201
value = "=" .join (items [1 :])
189
- return ( key , value )
202
+ return key , value
190
203
191
204
192
205
def load_user_metadata (args ):
193
- """
194
- Load user metadata into the provided argparse Namespace, raise ValueError if in incorrect format
206
+ """Load user metadata into args, raise ValueError if it is in incorrect format.
195
207
196
208
Parameters
197
209
----------
198
- args argparse.Namespace
199
- the arguments from the parser
210
+ args : argparse.Namespace
211
+ The arguments from the parser.
200
212
201
213
Returns
202
214
-------
203
- the updated argparse Namespace with user metadata inserted as key-value pairs
204
-
215
+ args : argparse. Namespace
216
+ The updated argparse Namespace with user metadata inserted as key-value pairs.
205
217
"""
206
218
207
219
reserved_keys = vars (args ).keys ()
@@ -215,73 +227,74 @@ def load_user_metadata(args):
215
227
)
216
228
key , value = _load_key_value_pair (item )
217
229
if key in reserved_keys :
218
- raise ValueError (f"{ key } is a reserved name. Please rerun using a different key name. " )
230
+ raise ValueError (f"{ key } is a reserved name. Please rerun using a different key name." )
219
231
if hasattr (args , key ):
220
- raise ValueError (f"Please do not specify repeated keys: { key } . " )
232
+ raise ValueError (f"Please do not specify repeated keys: { key } ." )
221
233
setattr (args , key , value )
222
234
delattr (args , "user_metadata" )
223
235
return args
224
236
225
237
226
238
def load_user_info (args ):
227
- """
228
- Load user info into args. If args are not provided, call check_and_build_global_config function from
239
+ """Load user info into args. If none is provided, call check_and_build_global_config function from
229
240
diffpy.utils to prompt the user for inputs. Otherwise, call get_user_info with the provided arguments.
230
241
231
242
Parameters
232
243
----------
233
- args argparse.Namespace
234
- the arguments from the parser, default is None
244
+ args : argparse.Namespace
245
+ The arguments from the parser.
235
246
236
247
Returns
237
248
-------
238
- the updated argparse Namespace with username and email inserted
239
-
249
+ args : argparse. Namespace
250
+ The updated argparse Namespace with username, email, and orcid inserted.
240
251
"""
241
252
if args .username is None or args .email is None :
242
253
check_and_build_global_config ()
243
- config = get_user_info (owner_name = args .username , owner_email = args .email )
254
+ config = get_user_info (owner_name = args .username , owner_email = args .email , owner_orcid = args . orcid )
244
255
args .username = config .get ("owner_name" )
245
256
args .email = config .get ("owner_email" )
257
+ args .orcid = config .get ("owner_orcid" )
246
258
return args
247
259
248
260
249
261
def load_package_info (args ):
250
- """
251
- Load diffpy.labpdfproc package name and version into args using get_package_info function from diffpy.utils
262
+ """Load diffpy.labpdfproc package name and version into args using get_package_info function from diffpy.utils.
252
263
253
264
Parameters
254
265
----------
255
- args argparse.Namespace
256
- the arguments from the parser, default is None
266
+ args : argparse.Namespace
267
+ The arguments from the parser.
257
268
258
269
Returns
259
270
-------
260
- the updated argparse Namespace with diffpy.labpdfproc name and version inserted
261
-
271
+ args : argparse.Namespace
272
+ The updated argparse Namespace with diffpy.labpdfproc name and version inserted.
262
273
"""
263
274
metadata = get_package_info ("diffpy.labpdfproc" )
264
275
setattr (args , "package_info" , metadata ["package_info" ])
265
276
return args
266
277
267
278
268
279
def preprocessing_args (args ):
269
- """
270
- Perform preprocessing on the provided argparse Namespace
280
+ """Perform preprocessing on the provided args.
281
+ The process includes loading package and user information,
282
+ setting input, output, wavelength, xtype, mu*D, and loading user metadata.
271
283
272
284
Parameters
273
285
----------
274
- args argparse.Namespace
275
- the arguments from the parser, default is None
286
+ args : argparse.Namespace
287
+ The arguments from the parser.
276
288
277
289
Returns
278
290
-------
279
- the updated argparse Namespace with arguments preprocessed
291
+ args : argparse.Namespace
292
+ The updated argparse Namespace with arguments preprocessed.
280
293
"""
281
294
args = load_package_info (args )
282
295
args = load_user_info (args )
283
296
args = set_input_lists (args )
284
- args . output_directory = set_output_directory (args )
297
+ args = set_output_directory (args )
285
298
args = set_wavelength (args )
286
299
args = set_xtype (args )
287
300
args = set_mud (args )
@@ -290,19 +303,21 @@ def preprocessing_args(args):
290
303
291
304
292
305
def load_metadata (args , filepath ):
293
- """
294
- Load relevant metadata from args
306
+ """Load the relevant metadata from args to write into the header of the output files.
295
307
296
308
Parameters
297
309
----------
298
- args argparse.Namespace
299
- the arguments from the parser
310
+ args : argparse.Namespace
311
+ The arguments from the parser.
312
+
313
+ filepath : Path
314
+ The filepath of the current input file.
300
315
301
316
Returns
302
317
-------
303
- A dictionary with relevant arguments from the parser
318
+ metadata : dict
319
+ The dictionary with relevant arguments from the parser.
304
320
"""
305
-
306
321
metadata = copy .deepcopy (vars (args ))
307
322
for key in METADATA_KEYS_TO_EXCLUDE :
308
323
metadata .pop (key , None )
0 commit comments