[c97724e] | 1 | """ |
---|
[d459d4e] | 2 | Conversion of scattering cross section from SANS (I(q), or rather, ds/dO) in absolute |
---|
| 3 | units (cm-1)into SESANS correlation function G using a Hankel transformation, then converting |
---|
| 4 | the SESANS correlation function into polarisation from the SESANS experiment |
---|
[c97724e] | 5 | |
---|
[d459d4e] | 6 | Everything is in units of metres except specified otherwise (NOT TRUE!!!) |
---|
| 7 | Everything is in conventional units (nm for spin echo length) |
---|
[c97724e] | 8 | |
---|
| 9 | Wim Bouwman (w.g.bouwman@tudelft.nl), June 2013 |
---|
| 10 | """ |
---|
| 11 | |
---|
| 12 | from __future__ import division |
---|
| 13 | |
---|
[7ae2b7f] | 14 | import numpy as np # type: ignore |
---|
| 15 | from numpy import pi, exp # type: ignore |
---|
[54f1d96] | 16 | from scipy.special import j0 |
---|
[94d13f1] | 17 | |
---|
[54f1d96] | 18 | class SesansTransform(object): |
---|
[94d13f1] | 19 | """ |
---|
| 20 | Spin-Echo SANS transform calculator. Similar to a resolution function, |
---|
| 21 | the SesansTransform object takes I(q) for the set of *q_calc* values and |
---|
| 22 | produces a transformed dataset |
---|
| 23 | |
---|
| 24 | *SElength* (A) is the set of spin-echo lengths in the measured data. |
---|
| 25 | |
---|
| 26 | *zaccept* (1/A) is the maximum acceptance of scattering vector in the spin |
---|
| 27 | echo encoding dimension (for ToF: Q of min(R) and max(lam)). |
---|
| 28 | |
---|
| 29 | *Rmax* (A) is the maximum size sensitivity; larger radius requires more |
---|
| 30 | computation time. |
---|
| 31 | """ |
---|
| 32 | #: SElength from the data in the original data units; not used by transform |
---|
| 33 | #: but the GUI uses it, so make sure that it is present. |
---|
[54f1d96] | 34 | q = None # type: np.ndarray |
---|
| 35 | |
---|
[94d13f1] | 36 | #: q values to calculate when computing transform |
---|
| 37 | q_calc = None # type: np.ndarray |
---|
| 38 | |
---|
[54f1d96] | 39 | # transform arrays |
---|
[94d13f1] | 40 | _H = None # type: np.ndarray |
---|
| 41 | _H0 = None # type: np.ndarray |
---|
[54f1d96] | 42 | |
---|
[94d13f1] | 43 | def __init__(self, z, SElength, zaccept, Rmax): |
---|
| 44 | # type: (np.ndarray, float, float) -> None |
---|
| 45 | #import logging; logging.info("creating SESANS transform") |
---|
| 46 | self.q = z |
---|
[a86e249] | 47 | # isoriented flag determines whether data is from an oriented sample or not, should be a selection variable upon entering SESANS data. |
---|
| 48 | if self.isoriented==False: |
---|
| 49 | self._set_hankel(SElength, zaccept, Rmax) |
---|
| 50 | if self.isoriented==True: |
---|
| 51 | self._set_cosmat(SElength, zaccept, Rmax) |
---|
[54f1d96] | 52 | |
---|
| 53 | def apply(self, Iq): |
---|
[a86e249] | 54 | try: |
---|
[e4e5e29] | 55 | G0 = np.dot(self._H0, Iq) |
---|
| 56 | G = np.dot(self._H.T, Iq) |
---|
| 57 | P = G - G0 |
---|
[a86e249] | 58 | except: |
---|
| 59 | try: |
---|
| 60 | dq = self.q_calc[0] |
---|
| 61 | G0 = sum(np.dot(self._cos0, Iq) * dq) |
---|
| 62 | G = sum(np.dot(self._cosmat.T, Iq) * dq) |
---|
| 63 | P = G - G0 |
---|
| 64 | except: |
---|
| 65 | raise ValueError('Sesanstransform.apply cannot generate either a Hankel transform or a cosine transform of you SESANS data') |
---|
[54f1d96] | 66 | return P |
---|
| 67 | |
---|
[94d13f1] | 68 | def _set_hankel(self, SElength, zaccept, Rmax): |
---|
| 69 | # type: (np.ndarray, float, float) -> None |
---|
| 70 | # Force float32 arrays, otherwise run into memory problems on some machines |
---|
| 71 | SElength = np.asarray(SElength, dtype='float32') |
---|
[54f1d96] | 72 | |
---|
[94d13f1] | 73 | #Rmax = #value in text box somewhere in FitPage? |
---|
| 74 | q_max = 2*pi / (SElength[1] - SElength[0]) |
---|
| 75 | q_min = 0.1 * 2*pi / (np.size(SElength) * SElength[-1]) |
---|
| 76 | q = np.arange(q_min, q_max, q_min, dtype='float32') |
---|
| 77 | dq = q_min |
---|
[54f1d96] | 78 | |
---|
[94d13f1] | 79 | H0 = np.float32(dq/(2*pi)) * q |
---|
[54f1d96] | 80 | |
---|
[94d13f1] | 81 | repq = np.tile(q, (SElength.size, 1)).T |
---|
| 82 | repSE = np.tile(SElength, (q.size, 1)) |
---|
| 83 | H = np.float32(dq/(2*pi)) * j0(repSE*repq) * repq |
---|
| 84 | |
---|
| 85 | self.q_calc = q |
---|
| 86 | self._H, self._H0 = H, H0 |
---|
[e4e5e29] | 87 | |
---|
| 88 | def _set_cosmat(self, SElength, zaccept, Rmax): |
---|
| 89 | # type: (np.ndarray, float, float) -> None |
---|
| 90 | # Force float32 arrays, otherwise run into memory problems on some machines |
---|
| 91 | SElength = np.asarray(SElength, dtype='float32') |
---|
[a86e249] | 92 | |
---|
[e4e5e29] | 93 | # Rmax = #value in text box somewhere in FitPage? |
---|
| 94 | q_max = 2 * pi / (SElength[1] - SElength[0]) |
---|
| 95 | q_min = 0.1 * 2 * pi / (np.size(SElength) * SElength[-1]) |
---|
| 96 | |
---|
| 97 | q = np.arange(q_min, q_max, q_min, dtype='float32') |
---|
| 98 | dq = q_min |
---|
| 99 | |
---|
| 100 | cos0 = np.float32(dq / (2 * pi)) |
---|
| 101 | |
---|
| 102 | repq = np.tile(q, (SElength.size, 1)).T |
---|
| 103 | repSE = np.tile(SElength, (q.size, 1)) |
---|
| 104 | cosmat = np.float32(dq / (2 * pi)) * np.cos(repSE * repq) |
---|
| 105 | |
---|
| 106 | self.q_calc = q |
---|
| 107 | self._cosmat, self._cos0 = cosmat, cos0 |
---|