Skip to content

Commit 89de7c2

Browse files
authored
Merge pull request #172 from yucongalicechen/tools-docs
docs: for tools
2 parents 9618ab0 + 07f11c3 commit 89de7c2

File tree

7 files changed

+233
-4
lines changed

7 files changed

+233
-4
lines changed

README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ Example
110110
Navigate to the directory that contains 1D diffraction patterns that you would like to process.
111111
Activate the conda environment (`conda activate diffpy.labpdfproc_env`) that contains the package and run the following command ::
112112

113-
labpdfproc <muD> <path/to/inputfile.txt>
113+
labpdfproc <path/to/inputfile.txt> --mud <muD>
114114

115115
Here replace <muD> with the value of muD for your sample
116116
and <path/to/inputfile.txt> with the path and filename of your input file.
117117
For example, if the uncorrected data case is called zro2_mo.xy and is in the current directory
118118
and it has a muD of 2.5 then the command would be ::
119119

120-
labpdfproc 2.5 zro2_mo.xy
120+
labpdfproc zro2_mo.xy --mud 2.5
121121

122122
Please type ::
123123

doc/source/examples/examples.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Landing page for diffpy.labpdfproc examples.
88

99
.. toctree::
1010
functions_example
11+
tools_example

doc/source/examples/tools_example.rst

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
.. _Tools Example:
2+
3+
:tocdepth: -1
4+
5+
Tools Example
6+
#############
7+
8+
This example will demonstrate how to use ``diffpy.labpdfproc.tools`` module
9+
to preprocess diffraction data using an ``argparse.Namespace`` called ``args``.
10+
These functions help set up inputs, metadata, and parameters before applying absorption correction.
11+
12+
1. We begin by estimating the mu*D value used for absorption correction using ``set_mud(args)``.
13+
You can do this in one of the following four ways:
14+
15+
.. code-block:: python
16+
17+
from argparse import Namespace
18+
# Option 1: Manual value
19+
args = Namespace(mud=2)
20+
# Option 2: From a z-scan file
21+
args = Namespace(z_scan_file="zscan.xy")
22+
# Option 3: Using mass density
23+
args = Namespace(sample_composition="ZrO2", energy=1.745, density=1.2)
24+
# Option 4: Using packing fraction
25+
args = Namespace(sample_composition="ZrO2", energy=1.745, packing_fraction=0.3)
26+
# Set and view the computed mu*D value
27+
args = set_mud(args)
28+
print(args.mud)
29+
30+
Note that only one method should be used at a time. The following are invalid examples:
31+
32+
.. code-block:: python
33+
34+
# Invalid example 1: manual mu*D and z-scan file both provided
35+
args = Namespace(mud=2, z_scan_file="zscan.xy")
36+
# Invalid example 2: missing required energy
37+
args = Namespace(sample_composition="ZrO2", density=1.2)
38+
# Invalid example 3: both mass density and packing fraction specified
39+
args = Namespace(sample_composition="ZrO2", energy=1.745, density=1.2, packing_fraction=0.3)
40+
41+
If the packing fraction option is not supported at the moment, you can approximate
42+
``mass density = packing fraction * material density``, where the material density can be looked up manually.
43+
44+
45+
2. Next, we load the input files for correction using ``set_input_lists(args)``:
46+
47+
.. code-block:: python
48+
49+
args = Namespace(input="file1.xy file2.xy")
50+
args = set_input_lists(args)
51+
print(args.input_paths)
52+
53+
This function resolves the input filenames into full file paths.
54+
For details on supported input formats, check ``labpdfproc --help``.
55+
56+
57+
3. We now set the output directory where the cve and corrected files will be saved:
58+
59+
.. code-block:: python
60+
61+
args = Namespace(output_directory="output") # creates "output/" directory if it doesn't exist
62+
args = set_output_directory(args)
63+
print(args.output_directory)
64+
65+
If a filename is provided instead of a directory, an error will be raised.
66+
If no output directory is specified, it defaults to the current working directory.
67+
68+
69+
4. We then set wavelength and xtype, both of which are necessary for absorption correction:
70+
71+
.. code-block:: python
72+
73+
# Option 1: Specify wavelength directly
74+
args = Namespace(wavelength=0.7)
75+
# Option 2: Use a valid anode type
76+
args = Namespace(anode_type="Mo")
77+
args = set_wavelength(args)
78+
79+
Note that you should specify either a wavelength or an anode type, not both, to avoid conflicts.
80+
If you provide an anode type, the corresponding wavelength will be retrieved from global parameters.
81+
You may use ``labpdfproc --help`` to view a list of valid anode types.
82+
If neither is given, it's only acceptable if the input diffraction data is already on a two-theta grid.
83+
To simplify workflows and avoid re-entering it every time,
84+
we recommend saving the wavelength or anode type to a diffpy config file. For example:
85+
86+
.. code-block:: python
87+
88+
from pathlib import Path
89+
import json
90+
home_dir = Path.home()
91+
wavelength_data = {"wavelength": 0.3}
92+
with open(home_dir / "diffpyconfig.json", "w") as f:
93+
json.dump(wavelength_data, f)
94+
95+
To set the x-axis type (xtype) for your diffraction data:
96+
97+
.. code-block:: python
98+
99+
args = Namespace(xtype="tth")
100+
args = set_xtype(args)
101+
102+
This sets the xtype to ``tth``. Other valid options including ``q`` and ``d`` spacing.
103+
104+
105+
5. Finally, we load user metadata, user information, and package information into ``args``.
106+
To load metadata, pass key-value pairs as a list:
107+
108+
.. code-block:: python
109+
110+
args = Namespace(
111+
user_metadata=[
112+
"facility=NSLS II",
113+
"beamline=28ID-2",
114+
])
115+
args = load_user_metadata(args)
116+
117+
This ensures all key-value pairs are parsed and added as attributes.
118+
119+
To load your user information (username, email, and orcid), you can manually add it through ``args``:
120+
121+
.. code-block:: python
122+
123+
args = Namespace(username="Joe", email="[email protected]", orcid="0000-0000-0000-0000")
124+
args = load_user_info(args)
125+
print(args.username, args.email, args.orcid)
126+
127+
Alternatively, this can be saved in a config file
128+
(see https://www.diffpy.org/diffpy.utils/examples/tools_example.html).
129+
If nothing is found, you will be prompted to create one.
130+
Note that it is not recommended to store personal information on a public or shared computer.
131+
132+
Furthermore, the function ``load_package_info(args)`` is used to attach package name and version info for reproducibility.
133+
This is typically run automatically but can be called explicitly:
134+
135+
.. code-block:: python
136+
137+
args = load_package_info(args)
138+
print(args.package_info) # Output example: {"diffpy.labpdfproc": "0.0.1", "diffpy.utils": "3.0.0"}
139+
140+
141+
6. We also provide a convenient function to run all steps above at once:
142+
143+
.. code-block:: python
144+
145+
args = preprocessing_args(args)
146+
147+
148+
7. The final step is converting your ``args`` to a metadata dictionary
149+
so that it can be attached to the diffraction object's header during output writing.
150+
Using the function ``load_metadata(args, filepath)``
151+
requires both the ``argument.Namespace`` and the current input file path.
152+
For more details about working with diffraction objects and how they are written to output files, see
153+
https://www.diffpy.org/diffpy.utils/examples/diffraction_objects_example.html.
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.. _Tools Utility:
2+
3+
Tools Utility
4+
=============
5+
6+
The ``diffpy.labpdfproc.tools`` module provides functions to
7+
manage user inputs, output directories, and diffraction information.
8+
These functions work mostly with an ``argparse.Namespace`` object and help prepare data for diffraction processing.
9+
10+
- ``set_mud()``: This function determines the mu*D value used to compute absorption correction.
11+
Users can either provide mu*D directly, upload a z-scan file,
12+
or specify relevant chemical information to get a theoretical estimation.
13+
14+
- ``set_input_lists()``: This function parses all specified input files.
15+
Users can provide a list of files or directories via command-line arguments or GUI selection.
16+
You can also specify a ``file_list.txt`` that contains file paths (one per line),
17+
including paths to entire directories.
18+
Wildcard patterns are also supported for matching multiple files.
19+
20+
- ``set_output_directory()``: This function ensures that an output directory is defined and exists.
21+
If it is not explicitly provided, it defaults to the current working directory.
22+
This directory is used to store all cve and corrected output files.
23+
24+
- ``load_wavelength_from_config_file()`` and ``set_wavelength()``:
25+
These two functions work together to determine the wavelength for processing.
26+
``load_wavelength_from_config_file()`` reads the default wavelength or anode type
27+
from user input or a diffpy configuration file, and ``set_wavelength()`` finalizes the wavelength value.
28+
It is recommended to store the wavelength information in a diffpy configuration file
29+
to avoid re-entering it every time.
30+
31+
- ``set_xtype()``: This function sets the x-axis variable for intensity interpolation (e.g. two-theta, q, d-spacing)
32+
and should be the same of the input diffraction data. The default is two-theta.
33+
34+
- ``load_user_metadata()``: This function loads experimental metadata
35+
or any user-defined key-value pairs to be recorded in the output files.
36+
37+
- ``load_user_info()``: This function loads user identification (name, email, orcid).
38+
Users can provide the information directly through arguments or store it in a diffpy configuration file.
39+
If no information is found, the function will prompt the user to create a configuration file,
40+
which is then saved for use in output metadata and future uses.
41+
42+
- ``load_package_info()``: This functions stores the package name and version for record-keeping.
43+
44+
- ``preprocessing_args()``: This is a convenience function that runs all standard setup steps listed above.
45+
It ensures the input/output paths, wavelength, mu*D value, metadata, and user and package info are
46+
fully initialized before applying the correction.
47+
48+
- ``load_metadata()``: This function transfers all collected information from the ``argparse.Namespace`` object
49+
into a diffraction object, allowing it to be saved in the output file header for tracking purpose.
50+
51+
For a more in-depth tutorial for how to use these tools, click :ref:`here <Tools Example>`.

doc/source/utilities/utilities.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Check the :ref:`examples<Examples>` provided for how to use these.
1111
:depth: 2
1212

1313
.. include:: functions_utility.rst
14+
.. include:: tools_utility.rst

news/tools-doc.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* Utility and example documentation for ``tools`` module.
4+
5+
**Changed:**
6+
7+
* Readme: muD now requires the ``--mud`` flag instead of a required argument.
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/labpdfproc/tools.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,13 @@ def preprocessing_args(args):
457457
The updated argparse Namespace with arguments preprocessed.
458458
"""
459459
args = set_mud(args)
460-
args = load_package_info(args)
461-
args = load_user_info(args)
462460
args = set_input_lists(args)
463461
args = set_output_directory(args)
464462
args = set_wavelength(args)
465463
args = set_xtype(args)
466464
args = load_user_metadata(args)
465+
args = load_user_info(args)
466+
args = load_package_info(args)
467467
return args
468468

469469

0 commit comments

Comments
 (0)