22
33Using funcxy with Commonly-Used Diffraction Software
44####################################################
5+
56The general xy morph ``funcxy `` can be used to tune parameters
67of many popular diffraction software functions.
78
@@ -11,6 +12,7 @@ and `PyFai <https://pyfai.readthedocs.io/en/stable/>`_.
1112
1213Getting a Better PDF with PDFgetx3
1314==================================
15+
1416In PDFgetx3, the ``PDFGetter `` takes in a 1D diffraction
1517pattern I(Q) and returns a PDF G(r).
1618
@@ -33,6 +35,7 @@ glass background. We want to match a target calculated PDF G(r)
3335stored in a file named ``target.cgr ``.
3436Let's also say we have a measured I(Q) of the
3537glass background ``background.chi ``.
38+
3639.. code-block :: python
3740
3841 from diffpy.pdfgetx.pdfgetter import PDFGetter
@@ -65,13 +68,12 @@ glass background ``background.chi``.
6568 " qmaxinst" : 25.0 , " rpoly" : 0.9
6669 }
6770
68- morph_info, morph_table = morph_arrays(
71+ morph_info, morphed_gr = morph_arrays(
6972 sample_iq, target_gr,
7073 funcxy = (wrap, params_to_morph)
7174 )
7275
73-
74- You can now plot ``morph_table `` against your ``target_gr `` to see
76+ You can now plot ``morphed_gr `` against your ``target_gr `` to see
7577how well your morphing refinement of the PDF-getting parameters
7678as done!
7779To see what the refined values of the parameters are,
@@ -88,6 +90,7 @@ call to ``morph_arrays``.
8890
8991Performing Detector Calibration with PyFai
9092==========================================
93+
9194When performing azimuthal integration, it is important to
9295ensure your beam center and detector distances are calibrated.
9396However, it is possible that they have shifted
@@ -96,10 +99,24 @@ across measurements. Here, we will use morphing to the rescue!
9699Let's say we just measured a diffraction pattern stored
97100as a NumPy object in ``diffraction_image.npy ``, but some
98101of the detector geometries are off.
102+ Our azimuthally integrated ``sample.chi `` looks a bit off.
99103Before this measurement, you measured an amazing
100104I(Q) pattern ``target.chi `` with a perfectly calibrated
101105sample-to-detector distance and beam center.
102- We will use morphing to try to recalibrate.
106+ We will use morphing to try to match the integration of
107+ the 2D pattern to the target 1D function.
108+
109+ For the integration, we will need some information, such as
110+ the wavelength of the beam,
111+ the size of each pixel in the 2D image
112+ (``pixel1 `` is the horizontal length in meters and
113+ ``pixel2 `` is the vertical length in meters),
114+ and a guess of the beam center.
115+ This information can be found on the
116+ `PyFai documentation <https://pyfai.readthedocs.io/en/stable/usage/cookbook/integration_with_python.html >`_.
117+ For our example, let's say we have a ``1024``x``1024 `` pixel image
118+ where each pixel is a ``100 `` micron by ``100 `` micron region, and
119+ our wavelength was ``1.11 `` angstroms.
103120
104121.. code-block :: python
105122
@@ -110,18 +127,55 @@ We will use morphing to try to recalibrate.
110127 from diffpy.utils.parsers.loaddata import loadData
111128
112129 pattern_2d = np.load(" diffraction_image.npy" )
113- wavelength = #
114- pixel1 = #
115- pixel2 = #
130+ wavelength = 0.1110e-9 # in m
131+ pixel1 = 1e-4 # in m
132+ pixel2 = 1e-4 # in m
133+ cent_x = 511 # in number of pixels
134+ cent_y = 511 # in number of pixels
116135
117136 ai = pyfai.AzimuthalIntegrator()
118137 ai.wavelength = wavelength
119138 detector = pfd.Detector()
120139 detector.max_shape = pattern_2d.shape
121140
141+
122142 def wrap (x , y , sample_to_detector_dist , cent_offset_x , cent_offset_y ):
123143 detector.pixel1 = pixel1
124144 detector.pixel2 = pixel2
145+ ai.detector = detector
125146
147+ ai.setFit2D(
148+ directDist = sample_to_detector_dist,
149+ centerX = cent_x+ cent_offset_x,
150+ centerY = cent_y+ cent_offset_y
151+ )
126152
127- ai.detector = detector
153+ return ai.integrate1D_ng(
154+ pattern_2d,
155+ npt = 1000 , unit = " q_A^-1" ,
156+ method = " mean"
157+ )
158+
159+
160+ params_to_morph = {
161+ " sample_to_detector_dist" : 60 , # in mm
162+ " cent_offset_x" : 0 , # in number of pixels
163+ " cent_offset_y" : 0 # in number of pixels
164+ }
165+
166+ sample_chi = loadData(" sample.chi" )
167+ target_chi = loadData(" target.chi" )
168+
169+ morph_info, morphed_chi = morph_arrays(
170+ sample_chi, target_chi,
171+ funcxy = (wrap, params_to_morph)
172+ )
173+
174+ You can now plot ``morphed_chi `` against your ``target_chi ``
175+ to see if the refinement has helped in the calibration!
176+ To see the calibrated values, you can print out ``morph_info ``.
177+
178+ If you would like to morph over other PyFai parameters
179+ (e.g. ``rot1 ``, ``tilt ``, ``wavelength ``),
180+ you can adjust the wrapper function ``wrap `` to take in
181+ these parameters.
0 commit comments