|
| 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. |
0 commit comments