|
| 1 | +.. _Diffraction Objects Example: |
| 2 | + |
| 3 | +:tocdepth: -1 |
| 4 | + |
| 5 | +Diffraction Objects Example |
| 6 | +########################### |
| 7 | + |
| 8 | +This example will demonstrate how to use the functions in the ``diffpy.utils.diffraction_objects`` module |
| 9 | +to create a ``DiffractionObject`` instance and analyze your diffraction data using relevant functions. |
| 10 | + |
| 11 | +1) To create a ``DiffractionObject``, you need to specify the type of the independent variable |
| 12 | + (referred to as ``xtype``, one of ``q``, ``tth``, or ``d``), |
| 13 | + an ``xarray`` of the corresponding values, and a ``yarray`` of the intensity values. |
| 14 | + It is strongly encouraged to specify the ``wavelength`` in order to access |
| 15 | + most of the other functionalities in the class. |
| 16 | + Additionally, you can specify the type of your scattering experiment using the ``scat_quantity`` parameter, |
| 17 | + the name of your diffraction object using the ``name`` parameter, |
| 18 | + and a ``metadata`` dictionary containing relevant information about the data. Here's an example: |
| 19 | + |
| 20 | +.. code-block:: python |
| 21 | + import numpy as np |
| 22 | + from diffpy.utils.diffraction_objects import DiffractionObject |
| 23 | + x = np.array([0.12, 0.24, 0.31, 0.4]) # independent variable (e.g., q) |
| 24 | + y = np.array([10, 20, 40, 60]) # intensity values |
| 25 | + metadata = { |
| 26 | + "sample": "rock salt from the beach", |
| 27 | + "composition": "NaCl", |
| 28 | + "temperature": "300 K,", |
| 29 | + "experimenters": "Phill, Sally" |
| 30 | + } |
| 31 | + do = DiffractionObject( |
| 32 | + xarray=x, |
| 33 | + yarray=y, |
| 34 | + xtype="q", |
| 35 | + wavelength=1.54, |
| 36 | + scat_quantity="x-ray", |
| 37 | + name="beach_rock_salt_1", |
| 38 | + metadata=metadata |
| 39 | + ) |
| 40 | + print(do.metadata) |
| 41 | +
|
| 42 | + By creating a ``DiffractionObject`` instance, you store not only the diffraction data |
| 43 | + but also all the associated information for analysis. |
| 44 | +
|
| 45 | +2) ``DiffractionObject`` automatically populates the ``xarray`` onto ``q``, ``tth``, and ``d``-spacing. |
| 46 | + If you want to access your diffraction data in a specific spacing, you can do this: |
| 47 | +
|
| 48 | +.. code-block:: python |
| 49 | + q = do.on_xtype("q") |
| 50 | + tth = do.on_xtype("tth") |
| 51 | + d = do.on_xtype("d") |
| 52 | +
|
| 53 | + This will return the ``xarray`` and ``yarray`` as a list of two 1D arrays, based on the specified ``xtype``. |
| 54 | +
|
| 55 | +3) Once the ``DiffractionObject`` is created, you can use ``get_array_index`` to get the index of the closest value |
| 56 | + in the ``xarray`` to a specified value. |
| 57 | + This is useful for alignment or comparison tasks. |
| 58 | + For example, assume you have created a ``DiffractionObject`` called ``do``, |
| 59 | + and you want to find the closest index of ``tth=80``, you can type the following: :: |
| 60 | + |
| 61 | + index = do.get_array_index(80, xtype="tth") |
| 62 | + |
| 63 | + If you do not specify an ``xtype``, it will default to the ``xtype`` used when creating the ``DiffractionObject``. |
| 64 | + For example, if you have created a ``DiffractionObject`` called ``do`` with ``xtype="q"``, |
| 65 | + you can find its closest index for ``q=0.25`` by typing either of the following: :: |
| 66 | + |
| 67 | + index = do.get_array_index(0.25) # no input xtype, defaults to q |
| 68 | + index = do.get_array_index(0.25, xtype="q") |
| 69 | + |
| 70 | +4) You can compare diffraction objects too. |
| 71 | + For example, you can use the ``scale_to`` function to rescale one diffraction object to align its intensity values |
| 72 | + with a second diffraction object at a (closest) specified value on a specified ``xarray``. |
| 73 | + This makes it easier for visualizing and comparing two intensity curves on the same plot. |
| 74 | + For example, to scale ``do1`` to match ``do2`` at ``tth=60``: |
| 75 | + |
| 76 | +.. code-block:: python |
| 77 | + # Create Diffraction Objects do1 and do2 |
| 78 | + do1 = DiffractionObject( |
| 79 | + xarray=np.array([10, 15, 25, 30, 60, 140]), |
| 80 | + yarray=np.array([10, 20, 25, 30, 60, 100]), |
| 81 | + xtype="tth", |
| 82 | + wavelength=2*np.pi |
| 83 | + ) |
| 84 | + do2 = DiffractionObject( |
| 85 | + xarray=np.array([10, 20, 25, 30, 60, 140]), |
| 86 | + yarray=np.array([2, 3, 4, 5, 6, 7]), |
| 87 | + xtype="tth", |
| 88 | + wavelength=2*np.pi |
| 89 | + ) |
| 90 | + do1_scaled = do1.scale_to(do2, tth=60) |
| 91 | +
|
| 92 | + Here, the scaling factor is computed at ``tth=60``, aligning the intensity values. |
| 93 | + ``do1_scaled`` will have the intensity array ``np.array([1, 2, 2.5, 3, 6, 10])``. |
| 94 | + You can also scale based on other axes (e.g., ``q=0.2``): :: |
| 95 | +
|
| 96 | + do1_scaled = do1.scale_to(do2, q=0.2) |
| 97 | +
|
| 98 | + The function finds the closest indices for ``q=0.2`` and scales the ``yarray`` accordingly. |
| 99 | +
|
| 100 | + Additionally, you can apply an ``offset`` to the scaled ``yarray``. For example: :: |
| 101 | +
|
| 102 | + do1_scaled = do1.scale_to(do2, tth=60, offset=2) # add 2 to the scaled yarray |
| 103 | + do1_scaled = do1.scale_to(do2, tth=60, offset=-2) # subtract 2 from the scaled yarray |
| 104 | +
|
| 105 | + This allows you to shift the scaled data for easier comparison. |
| 106 | +
|
| 107 | +5) You can create a copy of a diffraction object using the ``copy`` function, |
| 108 | + when you want to preserve the original data while working with a modified version. :: |
| 109 | +
|
| 110 | + # Create a copy of Diffraction Object do |
| 111 | + do_copy = do.copy() |
| 112 | +
|
| 113 | +6) The ``dump`` function saves the diffraction data and relevant information to a specified file. |
| 114 | + You can choose one of the data axis (``q``, ``tth``, or ``d``) to export, with ``q`` as the default. |
| 115 | +
|
| 116 | +.. code-block:: python |
| 117 | + # Assume you have created a Diffraction Object do |
| 118 | + file = "diffraction_data.xy" |
| 119 | + do.dump(file, xtype="q") |
| 120 | +
|
| 121 | + In the saved file "diffraction_data.xy", |
| 122 | + the relevant information (name, scattering quantity, metadata, etc.) is included in the header. |
| 123 | + Your analysis time and software version are automatically recorded as well. |
| 124 | + The diffraction data is saved as two columns: the ``q`` values and corresponding intensity values. |
| 125 | + This ensures your diffraction data, along with all other information, |
| 126 | + is properly documented and saved for future reference. |
0 commit comments