forked from diffpy/diffpy.labpdfproc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
609 lines (526 loc) · 17.8 KB
/
tools.py
File metadata and controls
609 lines (526 loc) · 17.8 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import copy
from pathlib import Path
from diffpy.utils.diffraction_objects import (
ANGLEQUANTITIES,
QQUANTITIES,
XQUANTITIES,
)
from diffpy.utils.tools import (
_load_config,
check_and_build_global_config,
compute_mu_using_xraydb,
compute_mud,
get_package_info,
get_user_info,
)
# Reference values are taken from
# https://x-server.gmca.aps.anl.gov/cgi/www_dbli.exe?x0hdb=waves
# Ka1Ka2 values are calculated as: (Ka1 * 2 + Ka2) / 3
# For CuKa1Ka2: (1.54056 * 2 + 1.544398) / 3 = 1.54184
WAVELENGTHS = {
"Mo": 0.71073,
"MoKa1": 0.70930,
"MoKa1Ka2": 0.71073,
"Ag": 0.56087,
"AgKa1": 0.55941,
"AgKa1Ka2": 0.56087,
"Cu": 1.54184,
"CuKa1": 1.54056,
"CuKa1Ka2": 1.54184,
}
known_sources = [key for key in WAVELENGTHS.keys()]
# Exclude wavelength to avoid duplication,
# as it's written explicitly by diffpy.utils dump function.
# Exclude "theoretical_from_density" and "theoretical_from_packing"
# as they are only used for theoretical mu*D estimation
# and will be written into separate arguments for clarity.
METADATA_KEYS_TO_EXCLUDE = [
"output_correction",
"input",
"input_paths",
"force",
"energy",
]
def set_output_directory(args):
"""Set the output directory based on the given input arguments.
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.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Returns
-------
args : argparse.Namespace
The updated arguments,
with output_directory as the full path to the output file directory.
"""
output_dir = (
Path(args.output_directory).resolve()
if args.output_directory
else Path.cwd().resolve()
)
output_dir.mkdir(parents=True, exist_ok=True)
args.output_directory = output_dir
return args
def _expand_user_input(args):
"""Expand the list of inputs by adding files from file lists and
wildcards.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Returns
-------
args : argparse.Namespace
The updated arguments with the modified input list.
"""
file_list_inputs = [
input_name for input_name in args.input if "file_list" in input_name
]
for file_list_input in file_list_inputs:
with open(file_list_input, "r") as f:
file_inputs = [input_name.strip() for input_name in f.readlines()]
args.input.extend(file_inputs)
args.input.remove(file_list_input)
wildcard_inputs = [
input_name for input_name in args.input if "*" in input_name
]
for wildcard_input in wildcard_inputs:
input_files = [
str(file)
for file in Path(".").glob(wildcard_input)
if "file_list" not in file.name
and "diffpyconfig.json" not in file.name
]
args.input.extend(input_files)
args.input.remove(wildcard_input)
return args
def set_input_lists(args):
"""Set input directory and files. It takes cli inputs, checks if
they are files or directories and creates a list of files to be
processed which is stored in the args Namespace.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Raises
------
FileNotFoundError
Raised when an input is invalid.
Returns
-------
args : argparse.Namespace
The updated arguments with the modified input list.
"""
input_paths = []
args = _expand_user_input(args)
for input_name in args.input:
input_path = Path(input_name).resolve()
if input_path.exists():
if input_path.is_file():
input_paths.append(input_path)
elif input_path.is_dir():
input_files = input_path.glob("*")
input_files = [
file.resolve()
for file in input_files
if file.is_file()
and "file_list" not in file.name
and "diffpyconfig.json" not in file.name
]
input_paths.extend(input_files)
else:
raise FileNotFoundError(
f"Cannot find {input_name}. "
f"Please specify valid input file(s) or directories."
)
else:
raise FileNotFoundError(
f"Cannot find {input_name}. "
f"Please specify valid input file(s) or directories."
)
setattr(args, "input_paths", list(set(input_paths)))
return args
def normalize_wavelength(args):
"""Normalize args.wavelength to a float.
If args.wavelength is:
- None: return args unchanged
- float-like: convert to float
- string: look up corresponding value in WAVELENGTHS (case-insensitive)
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Returns
-------
args : argparse.Namespace
The updated arguments with args.wavelength.
Raises
------
ValueError
If a string wavelength is not a known source.
"""
if args.wavelength is None:
return args
try:
args.wavelength = float(args.wavelength)
return args
except (TypeError, ValueError):
pass
key = str(args.wavelength).strip()
matched = next(
(k for k in WAVELENGTHS if k.lower() == key.lower()),
None,
)
if matched is None:
raise ValueError(
f"Anode type '{args.wavelength}' not recognized. "
f"Please rerun specifying an anode type from {*known_sources, }."
)
args.wavelength = WAVELENGTHS[matched]
return args
def load_wavelength_from_config_file(args):
"""Load wavelength from config files.
It prioritizes values in the following order:
1. cli inputs, 2. local config file, 3. global config file.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Returns
-------
args : argparse.Namespace
The updated arguments with the updated wavelength and anode type.
"""
if args.wavelength is not None:
return normalize_wavelength(args)
global_config_file = _load_config(Path().home() / "diffpyconfig.json")
local_config_file = _load_config(Path().cwd() / "diffpyconfig.json")
config_file = None
if (
isinstance(local_config_file, dict)
and "wavelength" in local_config_file
):
config_file = local_config_file
elif (
isinstance(global_config_file, dict)
and "wavelength" in global_config_file
):
config_file = global_config_file
if config_file is not None:
args.wavelength = config_file.get("wavelength")
return normalize_wavelength(args)
else:
raise ValueError(
"\nThe wavelength was not specified and no "
"configuration file 'diffpyconfig.json' containing "
"the wavelength or X-ray source was found in either the "
"local or home directories. Either specify the wavelength "
"or source using the -w/--wavelength option or "
"create a configuration file.\n\n"
"You can add the wavelength or anode type to a "
"configuration file on this computer. Once created, it "
"will be automatically used for subsequent diffpy data "
"by default, and you will only need to do this once.\n\n"
"For detailed instructions on creating the configuration "
"file, please refer to:\n"
"https://www.diffpy.org/diffpy.labpdfproc/examples/"
"toolsexample.html"
)
def set_wavelength(args):
"""Set the wavelength based on args.wavelength.
args.wavelength may be:
- None
- a number (explicit wavelength in Å)
- a string (X-ray source name)
If a string is provided, it must match a key in WAVELENGTHS.
Parameters
----------
args : argparse.Namespace
Raises
------
ValueError
If wavelength is required but missing,
if a string wavelength is not a known source,
or if a numeric wavelength is non-positive.
Returns
-------
args : argparse.Namespace
Updated arguments with args.wavelength as a float.
"""
args = normalize_wavelength(args)
if args.wavelength is None:
if args.xtype not in ANGLEQUANTITIES:
raise ValueError(
f"Please provide a wavelength or anode type "
f"because the independent variable axis is not on two-theta. "
f"Allowed anode types are {*known_sources, }."
)
return args
if args.wavelength <= 0:
raise ValueError(
f"Wavelength = {args.wavelength} is not valid. "
"Please rerun specifying a known anode type "
"or a positive wavelength."
)
return args
def set_xtype(args):
"""Set the xtype based on the given input arguments, raise an error
if xtype is not one of {*XQUANTITIES, }.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Returns
-------
args : argparse.Namespace
The updated arguments with the xtype as one of q, tth, or d.
"""
if args.xtype.lower() not in XQUANTITIES:
raise ValueError(
f"Unknown xtype: {args.xtype}. "
f"Allowed xtypes are {*XQUANTITIES, }."
)
args.xtype = (
"q"
if args.xtype.lower() in QQUANTITIES
else "tth" if args.xtype.lower() in ANGLEQUANTITIES else "d"
)
return args
def _set_mud_from_zscan(args):
"""Experimental estimation of mu*D from a z-scan file."""
filepath = Path(args.z_scan_file).resolve()
if not filepath.is_file():
raise FileNotFoundError(
f"Cannot find {args.z_scan_file}. "
f"Please specify a valid file path."
)
args.z_scan_file = str(filepath)
args.mud = compute_mud(filepath)
return args
def _parse_theoretical_input(input_str):
"""Helper function to parse and validate the input string."""
parts = [part.strip() for part in input_str.split(",")]
if len(parts) != 3:
raise ValueError(
f"Invalid mu*D input '{input_str}'. "
"Expected format is 'sample composition, energy, "
"sample mass density or packing fraction' "
"(e.g., 'ZrO2,17.45,0.5').",
)
sample_composition = parts[0]
energy = float(parts[1])
mass_density_or_packing_fraction = float(parts[2])
return sample_composition, energy, mass_density_or_packing_fraction
def _set_theoretical_mud_from_density(args):
"""Theoretical estimation of mu*D from sample composition, energy,
and sample mass density."""
args = normalize_wavelength(args)
if args.wavelength is None:
args = load_wavelength_from_config_file(args)
energy = 12.398 / args.wavelength
args.energy = energy
args.mud = (
compute_mu_using_xraydb(
args.sample_composition,
args.energy,
sample_mass_density=args.sample_mass_density,
)
* args.diameter
)
return args
def _set_theoretical_mud_from_packing(args):
"""Theoretical estimation of mu*D from sample composition, energy,
and packing fraction."""
sample_composition, energy, packing_fraction = _parse_theoretical_input(
args.theoretical_from_packing
)
args.sample_composition = sample_composition
args.energy = energy
args.packing_fraction = packing_fraction
args.mud = (
compute_mu_using_xraydb(
args.sample_composition,
args.energy,
packing_fraction=args.packing_fraction,
)
* args.diameter
)
return args
def set_mud(args):
"""Compute and set mu*D based on the selected method.
Options include:
1. Manually entering a value.
2. Estimating from a z-scan file.
3. Estimating theoretically based on sample mass density.
4. Estimating theoretically based on packing fraction.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Returns
-------
args : argparse.Namespace
The updated arguments with mu*D.
"""
if args.command == "mud":
return args
if args.command == "zscan":
return _set_mud_from_zscan(args)
if args.command == "sample":
return _set_theoretical_mud_from_density(args)
return args
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 args, raise ValueError if it is in
incorrect format.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Returns
-------
args : argparse.Namespace
The updated argparse Namespace
with user metadata inserted as key-value pairs.
"""
reserved_keys = set(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. "
f"Please rerun using a different key name."
)
if hasattr(args, key):
raise ValueError(
f"Please do not specify repeated keys: " f"{key}."
)
setattr(args, key, value)
delattr(args, "user_metadata")
return args
def load_user_info(args):
"""Load user info into args. If none is provided, call
check_and_build_global_config function from diffpy.utils to prompt
the user for inputs. Otherwise, call get_user_info with the provided
arguments.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Returns
-------
args : argparse.Namespace
The updated argparse Namespace
with username, email, and orcid inserted.
"""
if args.username is None or args.email is None:
check_and_build_global_config()
config = get_user_info(
owner_name=args.username,
owner_email=args.email,
owner_orcid=args.orcid,
)
args.username = config.get("owner_name")
args.email = config.get("owner_email")
args.orcid = config.get("owner_orcid")
return args
def load_package_info(args):
"""Load diffpy.labpdfproc package name and version into args using
get_package_info function from diffpy.utils.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Returns
-------
args : argparse.Namespace
The updated argparse Namespace
with diffpy.labpdfproc name and version inserted.
"""
metadata = get_package_info("diffpy.labpdfproc")
setattr(args, "package_info", metadata["package_info"])
return args
def _check_saved_file_exists(args):
"""Check if the output files already exist based on the input paths
and output directory."""
existing_files = []
for path in args.input_paths:
outfile = args.output_directory / (path.stem + "-mud-corrected.chi")
if outfile.exists() and not args.force:
existing_files.append(outfile)
if args.output_correction:
corrfile = args.output_directory / (path.stem + "_cve.chi")
if corrfile.exists() and not args.force:
existing_files.append(corrfile)
if existing_files:
existing_files_str = "\n".join(str(f) for f in existing_files)
raise FileExistsError(
"The following output files already exist:"
f"\n{existing_files_str}\n"
"Use --force to overwrite them."
)
def preprocessing_args(args):
"""Perform preprocessing on the provided args. The process includes
loading package and user information, setting input, output,
wavelength, anode type, xtype, mu*D, and loading user metadata.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
Returns
-------
args : argparse.Namespace
The updated argparse Namespace with arguments preprocessed.
Raises
------
FileExistsError
If the output files already exist and --force is not used.
"""
args = load_wavelength_from_config_file(args)
args = set_mud(args)
args = set_input_lists(args)
args = set_output_directory(args)
args = set_wavelength(args)
args = set_xtype(args)
args = load_user_metadata(args)
args = load_user_info(args)
args = load_package_info(args)
_check_saved_file_exists(args)
return args
# Update load_metadata to use 'input_directory' consistently:
def load_metadata(args, filepath):
"""Load the relevant metadata from args to write into the header of
the output files.
Parameters
----------
args : argparse.Namespace
The arguments from the parser.
filepath : Path
The filepath of the current input file.
Returns
-------
metadata : dict
The dictionary with relevant arguments from the parser.
"""
metadata = copy.deepcopy(vars(args))
for key in METADATA_KEYS_TO_EXCLUDE:
metadata.pop(key, None)
metadata["mud"] = round(float(metadata["mud"]), 4)
metadata["input_directory"] = str(filepath)
metadata["output_directory"] = str(metadata["output_directory"])
return metadata