File: coherence.py

package info (click to toggle)
python-xrt 1.6.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,572 kB
  • sloc: python: 59,424; xml: 4,786; lisp: 4,082; sh: 22; javascript: 18; makefile: 17
file content (383 lines) | stat: -rw-r--r-- 14,246 bytes parent folder | download
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
# -*- coding: utf-8 -*-
r"""
The module :mod:`~xrt.backends.raycing.coherence` has functions for 1D and 2D
analysis of coherence and functions for 1D plotting of degree of coherence and
and 2D plotting of eigen modes.

The input for the analysis functions is a 3D stack of field images. It can be
obtained directly from the undulator class, or from a plot object after several
repeats of wave propagation of a filament beam through a beamline. Examples can
be found in ``...\tests\raycing\test_coherent_fraction_stack.py`` and in
:ref:`SoftiMAX`.

.. autofunction:: calc_1D_coherent_fraction

.. autofunction:: plot_1D_degree_of_coherence

.. autofunction:: calc_degree_of_transverse_coherence_4D

.. autofunction:: calc_degree_of_transverse_coherence_PCA

.. autofunction:: calc_eigen_modes_4D

.. autofunction:: calc_eigen_modes_PCA

.. autofunction:: plot_eigen_modes

"""
__author__ = "Konstantin Klementiev, Roman Chernikov"
__date__ = "21 Jan 2018"
import numpy as np
import scipy.linalg as spl
from scipy.signal import argrelextrema
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches


def calc_1D_coherent_fraction(U, axisName, axis, p=0):
    """
    Calculates 1D degree of coherence (DoC). From its width in respect to the
    width of intensity distribution also infers the coherent fraction. Both
    widths are rms. The one of intensity is calculated over the whole axis, the
    one of DoC is calculated between the first local minima around the center
    provided that these minima are lower than 0.5.

    *U*: complex valued ndarray, shape(repeats, nx, ny)
        3D stack of field images. For a 1D cut along *axis*, the middle of the
        other dimension is sliced out.

    *axis*: str, one of 'x' or ('y' or 'z')
        Specifies the 1D axis of interest.

    *p*: float, distance to screen
        If non-zero, the calculated mutual intensity will be divided by *p*\ ².
        This is useful to get proper physical units of the returned intensity
        if the function is applied directly to the field stacks given by
        Undulator.multi_electron_stack() that is calculated in angular units.

    Returns a tuple of mutual intensity, 1D intensity, 1D DoC, rms width of
    intensity, rms width of DoC (between the local minima, see above), the
    position of the minima (only the positive side) and the coherent fraction.
    This tuple can be fed to the plotting function
    :func:`plot_1D_degree_of_coherence`.

    """
    repeats, binsx, binsz = U.shape
    if axisName == 'x':
        Uc = U[:, :, binsz//2]
    elif axisName in ['y', 'z']:
        Uc = U[:, binsx//2, :]
    else:
        raise ValueError("unknown axis")
    J = np.dot(Uc.T.conjugate(), Uc) / repeats
    if p > 0:  # p is distance
        J /= p**2
    II = np.abs(np.diag(J))  # intensity as the main diagonal
#    II = (np.abs(Uc)**2).sum(axis=0) / repeats  # intensity by definition
    J /= II**0.5 * II[:, np.newaxis]**0.5
    Jd = np.abs(np.diag(np.fliplr(J)))  # DoC as the cross-diagonal

    varI = (II * axis**2).sum() / II.sum()
    axisEx = 2 * axis

    lm = argrelextrema(Jd, np.less)[0]  # local min
    # for variance up to the 1st local minimum which is < 0.5:
    lm = lm[(axisEx[lm] > 0) & (Jd[lm] < 0.5)]
    if len(lm) > 0:
        cond = np.abs(axisEx) <= axisEx[lm[0]]
        limJd = axisEx[lm[0]]
    else:
        cond = slice(None)  # for unconstrained variance calculation
        limJd = None
    varJd = (Jd * axisEx**2)[cond].sum() / Jd[cond].sum()
    cohFr = (4*varI/varJd + 1)**(-0.5)
    return J, II, Jd, varI, varJd, limJd, cohFr


def plot_1D_degree_of_coherence(data1D, axisName, axis, unit="mm", fig2=None,
                                isIntensityNormalized=False, locLegend='auto'):
    """
    Provides two plots: a 2D plot of mutual intensity and a 1D plot of
    intensity and DoC. The latter plot can be shared between the two 1D axes if
    this function is invoked two times.

    *data1D*: tuple returned by :func:`calc_1D_coherent_fraction`.

    *axisName*: str, used in labels.

    *axis*: 1D array of abscissa.

    *unit*: str, used in labels.

    *fig2*: matplotlib figure object, if needed for shared plotting of two 1D
    axes.

    *isIntensityNormalized*: bool, controls the intensity axis label.

    *locLegend*: str, legend location in matplotlib style.

    Returns the two figure objects for the user to add suptitles and to export
    to image files.

    Plot examples for one and two 1D curves:

    .. imagezoom:: _images/_DOC-1D-1.*
    .. imagezoom:: _images/_DOC-1D-2.*

    """
    Jx, Ix, Jdx, varIx, varJdx, limJd, cohFrx = data1D

    fig1 = plt.figure()

    ax = fig1.add_subplot(111, aspect=1)
    am = axis.max()
    unitStr = "({0})".format(unit) if unit else ""
    ax.set_xlabel("{0}$_1$ {1}".format(axisName, unitStr))
    ax.set_ylabel("{0}$_2$ {1}".format(axisName, unitStr))
    ax.imshow(np.abs(Jx), origin='upper', extent=[-am, am, am, -am],
              interpolation='none')

    if fig2 is None:
        fig2 = plt.figure()
        ax2 = fig2.add_subplot(111)
        if isIntensityNormalized:
            ax2.set_ylabel("normalized intensity (a.u.)", color="C0")
        else:
            ax2.set_ylabel("intensity (ph/s/{0}²/0.1%BW)".format(unit),
                           color="C0")
        ax2.tick_params(axis='y', colors="C0")
        ax2.myLabel = "{0} or {0}$_1-${0}$_2$".format(axisName)
        ax2.myUnit = " ({0})".format(unit)
        ax3 = ax2.twinx()
        ax3.set_ylabel("degree of coherence", color="C1")
        ax3.tick_params(axis='y', colors="C1")
        ax3.spines['left'].set_color("C0")
        ax3.spines['right'].set_color("C1")
        lstyle = '-'
        loc = "center left" if locLegend == 'auto' else locLegend
        rHeight = 1.02
    else:
        ax2, ax3 = fig2.axes
        ax2.myLabel += " or {0} or {0}$_1-${0}$_2$".format(axisName)
        lstyle = '--'
        loc = "center right" if locLegend == 'auto' else locLegend
        rHeight = 1.0
    ax2.set_xlabel(ax2.myLabel + ax2.myUnit)

    l1, = ax2.plot(axis, Ix, "C0"+lstyle)
    ax2.set_ylim([0, 1.02 if isIntensityNormalized else None])

    l3, = ax3.plot(axis*2, Jdx, "C1"+lstyle)
    if limJd is not None:
        rect = mpatches.Rectangle(
            (-limJd, 0), width=2*limJd, height=rHeight, color='C1', alpha=0.1)
        ax3.add_patch(rect)

    ax3.set_ylim([0, 1.02])

    pp = mpatches.Patch(color='none')  # a fake element to add text to legends
    legendX = ax3.legend(
        (l1, l3, pp),
        ("I({0}),\nσ$_{0}$ = {1:.2f} {2}".format(axisName, varIx**0.5, unit),
         "DoC({0}$_1-${0}$_2$),\nξ$_{0}$ = {1:.2f} {2}".format(
             axisName, varJdx**0.5, unit),
         "\nζ$_{0}$ = {1:.1f} %".format(axisName, cohFrx*100)),
        loc=loc)
    ax3.add_artist(legendX)

    return fig1, fig2


def calc_degree_of_transverse_coherence_4D(J):
    """
    Calculates DoTC from the mutual intensity *J* as Tr(*J²*)/Tr²(*J*). This
    function should only be used for demonstration purpose. There is a faster
    alternative: :func:`calc_degree_of_transverse_coherence_PCA`.
    """
    res = np.diag(np.dot(J, J)).sum() / np.diag(J).sum()**2
    return res.real


def calc_degree_of_transverse_coherence_PCA(U):
    """
    Calculates DoTC from the field stack *U*. The field images of *U* are
    flattened to form the matrix D shaped as (repeats, nx×ny).
    DoTC = Tr(*D*\ :sup:`+`\ *DD*\ :sup:`+`\ *D*)/Tr²(*D*\ :sup:`+`\ *D*),
    which is equal to the original DoTC for the mutual intensity *J*:
    DoTC = Tr(*J²*)/Tr²(*J*).
    """
    repeats, binsx, binsz = U.shape
    k = binsx * binsz
    D = np.array(U).reshape((repeats, k), order='F').T
    DTD = np.dot(D.T.conjugate(), D)
    res = np.diag(np.dot(DTD, DTD)).sum() / np.diag(DTD).sum()**2
    return res.real


def calc_eigen_modes_4D(J, eigenN=4):
    """
    Solves the eigenvalue problem for the mutual intensity *J*. This function
    should only be used for demonstration purpose. There is a much faster
    alternative: :func:`calc_eigen_modes_PCA`.
    """
    k = J.shape[0]
    J /= np.diag(J).sum()
    kwargs = dict(eigvals=(k-eigenN, k-1)) if eigenN else {}
    w, v = spl.eigh(J, **kwargs)
    if eigenN is None:
        rE = np.dot(np.dot(v, np.diag(w)), np.conj(v.T))
        print("diff J--decomposed(J) = {0}".format(np.abs(J-rE).sum()))
    return w, v


def calc_eigen_modes_PCA(U, eigenN=4, maxRepeats=None, normalize=False):
    """
    Solves the PCA problem for the field stack *U* shaped as (repeats, nx, ny).
    The field images are flattened to form the matrix D shaped as
    (repeats, nx×ny). The eigenvalue problem is solved for the matrix
    *D*\ :sup:`+`\*D*.

    Returns a tuple of two arrays: eigenvalues in a 1D array and eigenvectors
    as columns of a 2D array. This is a much faster and exact replacement of
    the full eigen mode decomposition by :func:`calc_eigen_modes_4D`.

    *eigenN* sets the number of returned eigen modes. If None, the number of
    modes is inferred from the shape of the field stack *U* and is equal to the
    number of macroelectrons (repeats). If given as a 2-tuple, *eigenN* refers
    to the number of eigenvalues and eigenvectors separately.

    if *maxRepeats* are given, the stack is sliced up to that number. This
    option is introduced in order not to make the eigenvalue problem too big.

    If *normalize* is True, the eigenvectors are normalized, otherwise
    they are the PCs of the field stack in the original field units.
    """
    locU = U if maxRepeats is None else U[:maxRepeats, :, :]
    repeats, binsx, binsz = locU.shape
    if eigenN is None:
        eigenNw, eigenNv = repeats, repeats
    elif isinstance(eigenN, (tuple, list)):
        eigenNw, eigenNv = eigenN[:2]
        if eigenNw is None:
            eigenNw = repeats
        if eigenNv is None:
            eigenNv = repeats
    else:
        eigenNw, eigenNv = eigenN, eigenN
    minEigenNw = min(eigenNw, eigenNv)
    maxEigenNw = max(eigenNw, eigenNv)
    if repeats < minEigenNw:
        raise ValueError('"repeats" must be >= {0}'.format(minEigenNw))
    k = binsx * binsz
    D = np.array(locU).reshape((repeats, k), order='F').T
    DTD = np.dot(D.T.conjugate(), D)
    DTD /= np.diag(DTD).sum()
    kwargs = dict(eigvals=(repeats-maxEigenNw, repeats-1))
    wPCA, vPCA = spl.eigh(DTD, **kwargs)
    outPCA = np.zeros((k, eigenNv), dtype=np.complex128)
    for i in range(eigenNv):
        mPCA = np.outer(vPCA[:, -1-i], vPCA[:, -1-i].T.conjugate())
        vv = np.dot(D, mPCA)[:, 0]
        if normalize:
            outPCA[:, -1-i] = vv / np.dot(vv, vv.conj())**0.5
        else:
            outPCA[:, -1-i] = vv
    if eigenNv == repeats and normalize:
        rE = np.dot(np.dot(vPCA, np.diag(wPCA)), np.conj(vPCA.T))
        print("diff DTD--decomposed(DTD) = {0}".format(np.abs(DTD-rE).sum()))
    return wPCA, outPCA
calc_eigen_modes = calc_eigen_modes_PCA


def plot_eigen_modes(x, y, w, v, xlabel='', ylabel=''):
    """
    Provides 2D plots of the first 4 eigen modes in the given *x* and *y*
    coordinates. The eigen modes are specified by eigenvalues and eigenvectors
    *w* and *v* returned by :func:`calc_eigen_modes_PCA` or
    :func:`calc_eigen_modes_4D`.

    Plot example:

    .. imagezoom:: _images/_Modes-2D.*

    """
    limx, limz = [x.min(), x.max()], [y.min(), y.max()]
    isSliceX, isSliceZ = False, False
    if limx[0] == limx[1]:
        limx = [l*0.1 for l in limz]
        isSliceX = True
    if limz[0] == limz[1]:
        limz = [l*0.1 for l in limx]
        isSliceZ = True
    dlimx, dlimz = limx[1] - limx[0], limz[1] - limz[0]
    fxz = float(dlimz)/dlimx

    xMargin = 80
    yMargin = xMargin - 30
    space = 4
    dX = 256
    dY = dX * fxz
    dpi = 100

    cmap = mpl.colormaps['cubehelix']
#    cmap = None  # default to rc image.cmap value

    extent = limx + limz

    if w is None:
        return
#    norm = (abs(v[:, -4:])**2).sum(axis=0)
#    v[:, -4:] /= norm**0.5

    xFigSize = float(2*xMargin + space + 2*dX)
#    yFigSize = float(2*yMargin + space + 2*dY + 20/fxz)
    yFigSize = float(2*yMargin + space + 2*dY)
    figMs = plt.figure(figsize=(xFigSize/dpi, yFigSize/dpi), dpi=dpi)

    rect2d = [xMargin/xFigSize, (yMargin+dY+space)/yFigSize,
              dX/xFigSize, dY/yFigSize]
    ax0 = figMs.add_axes(rect2d, aspect=1)
    rect2d = [(xMargin+dX+space)/xFigSize, (yMargin+dY+space)/yFigSize,
              dX/xFigSize, dY/yFigSize]
    ax1 = figMs.add_axes(rect2d, aspect=1)
    rect2d = [xMargin/xFigSize, yMargin/yFigSize,
              dX/xFigSize, dY/yFigSize]
    ax2 = figMs.add_axes(rect2d, aspect=1)
    rect2d = [(xMargin+dX+space)/xFigSize, yMargin/yFigSize,
              dX/xFigSize, dY/yFigSize]
    ax3 = figMs.add_axes(rect2d, aspect=1)
    for ax in [ax0, ax1, ax2, ax3]:
        ax.set_xlim(extent[0], extent[1])
        ax.set_ylim(extent[2], extent[3])
        ax.tick_params(axis='x', colors='grey')
        ax.tick_params(axis='y', colors='grey')
        if isSliceX:
            ax.set_xticks([0])
        if isSliceZ:
            ax.set_yticks([0])

    for ax in [ax0, ax1]:
        ax.xaxis.tick_top()
    for ax in [ax1, ax3]:
        ax.yaxis.tick_right()
    modeName = 'mode'
    labels = ['0th (coherent) {0}: w={1:.3f}', '1st residual {0}: w={1:.3f}',
              '2nd residual {0}: w={1:.3f}', '3rd residual {0}: w={1:.3f}']
    for iax, (ax, lab) in enumerate(zip([ax0, ax1, ax2, ax3], labels)):
        assert v[:, -iax-1].shape[0] == len(y)*len(x)
        im = (v[:, -iax-1]).reshape(len(y), len(x))
        imA = (im.real**2 + im.imag**2).astype(float)
        ax.imshow(imA, extent=extent, cmap=cmap, interpolation='none')
        plt.text(
            0.5, 0.95, lab.format(modeName, w[-iax-1]),
            transform=ax.transAxes, ha='center', va='top', color='w', size=10)

    if xlabel:
        figMs.text((xMargin+dX+space/2.)/xFigSize, yMargin*0.3/yFigSize,
                   xlabel, ha='center', va='center')
    if ylabel:
        figMs.text(xMargin*0.3/xFigSize, (yMargin+dY+space/2.)/yFigSize,
                   ylabel, ha='center', va='center', rotation='vertical')

    return figMs