Skip to content

Commit 281b2f9

Browse files
docs: refine docstrings in tools, add orcid argument
1 parent 0532b0e commit 281b2f9

File tree

2 files changed

+101
-78
lines changed

2 files changed

+101
-78
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ def define_arguments():
120120
),
121121
"default": None,
122122
},
123+
{
124+
"name": ["--orcid"],
125+
"help": (
126+
"ORCID will be loaded from config files. Specify here "
127+
"only if you want to override that behavior at runtime. "
128+
),
129+
"default": None,
130+
},
123131
{
124132
"name": ["-z", "--z-scan-file"],
125133
"help": "Path to the z-scan file to be loaded to determine the mu*D value",

src/diffpy/labpdfproc/tools.py

Lines changed: 93 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,41 @@
1313

1414

1515
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.
2317
24-
it is determined as follows:
18+
It is determined as follows:
2519
If user provides an output directory, use it.
2620
Otherwise, we set it to the current directory if nothing is provided.
2721
We then create the directory if it does not exist.
2822
23+
Parameters
24+
----------
25+
args : argparse.Namespace
26+
The arguments from the parser.
27+
2928
Returns
3029
-------
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.
3232
"""
3333
output_dir = Path(args.output_directory).resolve() if args.output_directory else Path.cwd().resolve()
3434
output_dir.mkdir(parents=True, exist_ok=True)
35-
return output_dir
35+
args.output_directory = output_dir
36+
return args
3637

3738

3839
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.
4141
4242
Parameters
4343
----------
44-
args argparse.Namespace
45-
the arguments from the parser
44+
args : argparse.Namespace
45+
The arguments from the parser.
4646
4747
Returns
4848
-------
49-
the arguments with the modified input list
50-
49+
args : argparse.Namespace
50+
The updated arguments with the modified input list.
5151
"""
5252
file_list_inputs = [input_name for input_name in args.input if "file_list" in input_name]
5353
for file_list_input in file_list_inputs:
@@ -57,28 +57,34 @@ def _expand_user_input(args):
5757
args.input.remove(file_list_input)
5858
wildcard_inputs = [input_name for input_name in args.input if "*" in input_name]
5959
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+
]
6165
args.input.extend(input_files)
6266
args.input.remove(wildcard_input)
6367
return args
6468

6569

6670
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
7172
a list of files to be processed which is stored in the args Namespace.
7273
7374
Parameters
7475
----------
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.
7783
7884
Returns
7985
-------
80-
args argparse.Namespace
81-
86+
args : argparse.Namespace
87+
The updated arguments with the modified input list.
8288
"""
8389

8490
input_paths = []
@@ -91,34 +97,41 @@ def set_input_lists(args):
9197
elif input_path.is_dir():
9298
input_files = input_path.glob("*")
9399
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
95103
]
96104
input_paths.extend(input_files)
97105
else:
98106
raise FileNotFoundError(
99107
f"Cannot find {input_name}. Please specify valid input file(s) or directories."
100108
)
101109
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+
)
103113
setattr(args, "input_paths", list(set(input_paths)))
104114
return args
105115

106116

107117
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.
110120
111121
Parameters
112122
----------
113-
args argparse.Namespace
114-
the arguments from the parser
123+
args : argparse.Namespace
124+
The arguments from the parser.
115125
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.
118130
119131
Returns
120132
-------
121-
args argparse.Namespace
133+
args : argparse.Namespace
134+
The updated arguments with the wavelength.
122135
"""
123136
if args.wavelength is not None and args.wavelength <= 0:
124137
raise ValueError(
@@ -139,17 +152,17 @@ def set_wavelength(args):
139152

140153

141154
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, }.
144156
145157
Parameters
146158
----------
147-
args argparse.Namespace
148-
the arguments from the parser
159+
args : argparse.Namespace
160+
The arguments from the parser.
149161
150162
Returns
151163
-------
152-
args argparse.Namespace
164+
args : argparse.Namespace
165+
The updated arguments with the xtype as one of q, tth, or d.
153166
"""
154167
if args.xtype.lower() not in XQUANTITIES:
155168
raise ValueError(f"Unknown xtype: {args.xtype}. Allowed xtypes are {*XQUANTITIES, }.")
@@ -160,17 +173,17 @@ def set_xtype(args):
160173

161174

162175
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.
165177
166178
Parameters
167179
----------
168-
args argparse.Namespace
169-
the arguments from the parser
180+
args : argparse.Namespace
181+
The arguments from the parser.
170182
171183
Returns
172184
-------
173-
args argparse.Namespace
185+
args : argparse.Namespace
186+
The updated arguments with mu*D.
174187
"""
175188
if args.z_scan_file:
176189
filepath = Path(args.z_scan_file).resolve()
@@ -186,22 +199,21 @@ def _load_key_value_pair(s):
186199
key = items[0].strip()
187200
if len(items) > 1:
188201
value = "=".join(items[1:])
189-
return (key, value)
202+
return key, value
190203

191204

192205
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.
195207
196208
Parameters
197209
----------
198-
args argparse.Namespace
199-
the arguments from the parser
210+
args : argparse.Namespace
211+
The arguments from the parser.
200212
201213
Returns
202214
-------
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.
205217
"""
206218

207219
reserved_keys = vars(args).keys()
@@ -215,73 +227,74 @@ def load_user_metadata(args):
215227
)
216228
key, value = _load_key_value_pair(item)
217229
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.")
219231
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}.")
221233
setattr(args, key, value)
222234
delattr(args, "user_metadata")
223235
return args
224236

225237

226238
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
229240
diffpy.utils to prompt the user for inputs. Otherwise, call get_user_info with the provided arguments.
230241
231242
Parameters
232243
----------
233-
args argparse.Namespace
234-
the arguments from the parser, default is None
244+
args : argparse.Namespace
245+
The arguments from the parser.
235246
236247
Returns
237248
-------
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.
240251
"""
241252
if args.username is None or args.email is None:
242253
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)
244255
args.username = config.get("owner_name")
245256
args.email = config.get("owner_email")
257+
args.orcid = config.get("owner_orcid")
246258
return args
247259

248260

249261
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.
252263
253264
Parameters
254265
----------
255-
args argparse.Namespace
256-
the arguments from the parser, default is None
266+
args : argparse.Namespace
267+
The arguments from the parser.
257268
258269
Returns
259270
-------
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.
262273
"""
263274
metadata = get_package_info("diffpy.labpdfproc")
264275
setattr(args, "package_info", metadata["package_info"])
265276
return args
266277

267278

268279
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.
271283
272284
Parameters
273285
----------
274-
args argparse.Namespace
275-
the arguments from the parser, default is None
286+
args : argparse.Namespace
287+
The arguments from the parser.
276288
277289
Returns
278290
-------
279-
the updated argparse Namespace with arguments preprocessed
291+
args : argparse.Namespace
292+
The updated argparse Namespace with arguments preprocessed.
280293
"""
281294
args = load_package_info(args)
282295
args = load_user_info(args)
283296
args = set_input_lists(args)
284-
args.output_directory = set_output_directory(args)
297+
args = set_output_directory(args)
285298
args = set_wavelength(args)
286299
args = set_xtype(args)
287300
args = set_mud(args)
@@ -290,19 +303,21 @@ def preprocessing_args(args):
290303

291304

292305
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.
295307
296308
Parameters
297309
----------
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.
300315
301316
Returns
302317
-------
303-
A dictionary with relevant arguments from the parser
318+
metadata : dict
319+
The dictionary with relevant arguments from the parser.
304320
"""
305-
306321
metadata = copy.deepcopy(vars(args))
307322
for key in METADATA_KEYS_TO_EXCLUDE:
308323
metadata.pop(key, None)

0 commit comments

Comments
 (0)