[d9633b1] | 1 | """ |
---|
| 2 | Define the resolution functions for the data. |
---|
| 3 | |
---|
| 4 | This defines classes for 1D and 2D resolution calculations. |
---|
| 5 | """ |
---|
[7954f5c] | 6 | from __future__ import division |
---|
[d138d43] | 7 | |
---|
[7ae2b7f] | 8 | from scipy.special import erf # type: ignore |
---|
| 9 | from numpy import sqrt, log, log10, exp, pi # type: ignore |
---|
| 10 | import numpy as np # type: ignore |
---|
[190fc2b] | 11 | |
---|
[d138d43] | 12 | __all__ = ["Resolution", "Perfect1D", "Pinhole1D", "Slit1D", |
---|
| 13 | "apply_resolution_matrix", "pinhole_resolution", "slit_resolution", |
---|
| 14 | "pinhole_extend_q", "slit_extend_q", "bin_edges", |
---|
| 15 | "interpolate", "linear_extrapolation", "geometric_extrapolation", |
---|
[823e620] | 16 | ] |
---|
[d138d43] | 17 | |
---|
[d9633b1] | 18 | MINIMUM_RESOLUTION = 1e-8 |
---|
| 19 | |
---|
[eb588ef] | 20 | |
---|
| 21 | # When extrapolating to -q, what is the minimum positive q relative to q_min |
---|
| 22 | # that we wish to calculate? |
---|
| 23 | MIN_Q_SCALE_FOR_NEGATIVE_Q_EXTRAPOLATION = 0.01 |
---|
| 24 | |
---|
[346bc88] | 25 | class Resolution(object): |
---|
[d9633b1] | 26 | """ |
---|
| 27 | Abstract base class defining a 1D resolution function. |
---|
| 28 | |
---|
| 29 | *q* is the set of q values at which the data is measured. |
---|
| 30 | |
---|
| 31 | *q_calc* is the set of q values at which the theory needs to be evaluated. |
---|
| 32 | This may extend and interpolate the q values. |
---|
| 33 | |
---|
| 34 | *apply* is the method to call with I(q_calc) to compute the resolution |
---|
| 35 | smeared theory I(q). |
---|
| 36 | """ |
---|
[7ae2b7f] | 37 | q = None # type: np.ndarray |
---|
| 38 | q_calc = None # type: np.ndarray |
---|
[33c8d73] | 39 | def apply(self, theory): |
---|
[d9633b1] | 40 | """ |
---|
[33c8d73] | 41 | Smear *theory* by the resolution function, returning *Iq*. |
---|
[d9633b1] | 42 | """ |
---|
| 43 | raise NotImplementedError("Subclass does not define the apply function") |
---|
| 44 | |
---|
[7954f5c] | 45 | |
---|
[346bc88] | 46 | class Perfect1D(Resolution): |
---|
[d9633b1] | 47 | """ |
---|
| 48 | Resolution function to use when there is no actual resolution smearing |
---|
| 49 | to be applied. It has the same interface as the other resolution |
---|
| 50 | functions, but returns the identity function. |
---|
| 51 | """ |
---|
| 52 | def __init__(self, q): |
---|
| 53 | self.q_calc = self.q = q |
---|
| 54 | |
---|
[33c8d73] | 55 | def apply(self, theory): |
---|
| 56 | return theory |
---|
[d9633b1] | 57 | |
---|
[7954f5c] | 58 | |
---|
[346bc88] | 59 | class Pinhole1D(Resolution): |
---|
[d9633b1] | 60 | r""" |
---|
| 61 | Pinhole aperture with q-dependent gaussian resolution. |
---|
[63b32bb] | 62 | |
---|
[d9633b1] | 63 | *q* points at which the data is measured. |
---|
[49d1d42f] | 64 | |
---|
[33c8d73] | 65 | *q_width* gaussian 1-sigma resolution at each data point. |
---|
[d9633b1] | 66 | |
---|
| 67 | *q_calc* is the list of points to calculate, or None if this should |
---|
[7954f5c] | 68 | be estimated from the *q* and *q_width*. |
---|
[d9633b1] | 69 | """ |
---|
[f1b8c90] | 70 | def __init__(self, q, q_width, q_calc=None, nsigma=3): |
---|
[d9633b1] | 71 | #*min_step* is the minimum point spacing to use when computing the |
---|
| 72 | #underlying model. It should be on the order of |
---|
| 73 | #$\tfrac{1}{10}\tfrac{2\pi}{d_\text{max}}$ to make sure that fringes |
---|
| 74 | #are computed with sufficient density to avoid aliasing effects. |
---|
| 75 | |
---|
| 76 | # Protect against calls with q_width=0. The extend_q function will |
---|
| 77 | # not extend the q if q_width is 0, but q_width must be non-zero when |
---|
| 78 | # constructing the weight matrix to avoid division by zero errors. |
---|
| 79 | # In practice this should never be needed, since resolution should |
---|
| 80 | # default to Perfect1D if the pinhole geometry is not defined. |
---|
[49d1d42f] | 81 | self.q, self.q_width = q, q_width |
---|
[823e620] | 82 | self.q_calc = (pinhole_extend_q(q, q_width, nsigma=nsigma) |
---|
| 83 | if q_calc is None else np.sort(q_calc)) |
---|
[ea75043] | 84 | self.weight_matrix = pinhole_resolution(self.q_calc, self.q, |
---|
| 85 | np.maximum(q_width, MINIMUM_RESOLUTION)) |
---|
[d9633b1] | 86 | |
---|
[33c8d73] | 87 | def apply(self, theory): |
---|
| 88 | return apply_resolution_matrix(self.weight_matrix, theory) |
---|
[d9633b1] | 89 | |
---|
[7954f5c] | 90 | |
---|
[346bc88] | 91 | class Slit1D(Resolution): |
---|
[d9633b1] | 92 | """ |
---|
[ea75043] | 93 | Slit aperture with resolution function. |
---|
[d9633b1] | 94 | |
---|
| 95 | *q* points at which the data is measured. |
---|
[49d1d42f] | 96 | |
---|
[ea75043] | 97 | *dqx* slit width in qx |
---|
[d9633b1] | 98 | |
---|
[ea75043] | 99 | *dqy* slit height in qy |
---|
[7954f5c] | 100 | |
---|
| 101 | *q_calc* is the list of points to calculate, or None if this should |
---|
| 102 | be estimated from the *q* and *q_width*. |
---|
| 103 | |
---|
| 104 | The *weight_matrix* is computed by :func:`slit1d_resolution` |
---|
[d9633b1] | 105 | """ |
---|
[ea75043] | 106 | def __init__(self, q, qx_width, qy_width=0., q_calc=None): |
---|
| 107 | # Remember what width/dqy was used even though we won't need them |
---|
[7954f5c] | 108 | # after the weight matrix is constructed |
---|
[ea75043] | 109 | self.qx_width, self.qy_width = qx_width, qy_width |
---|
[7954f5c] | 110 | |
---|
[dbde9f8] | 111 | # Allow independent resolution on each point even though it is not |
---|
| 112 | # needed in practice. |
---|
[ea75043] | 113 | if np.isscalar(qx_width): |
---|
| 114 | qx_width = np.ones(len(q))*qx_width |
---|
[dbde9f8] | 115 | else: |
---|
[ea75043] | 116 | qx_width = np.asarray(qx_width) |
---|
| 117 | if np.isscalar(qy_width): |
---|
| 118 | qy_width = np.ones(len(q))*qy_width |
---|
[dbde9f8] | 119 | else: |
---|
[ea75043] | 120 | qy_width = np.asarray(qy_width) |
---|
[dbde9f8] | 121 | |
---|
[7954f5c] | 122 | self.q = q.flatten() |
---|
[ea75043] | 123 | self.q_calc = slit_extend_q(q, qx_width, qy_width) \ |
---|
[7954f5c] | 124 | if q_calc is None else np.sort(q_calc) |
---|
[d9633b1] | 125 | self.weight_matrix = \ |
---|
[ea75043] | 126 | slit_resolution(self.q_calc, self.q, qx_width, qy_width) |
---|
[49d1d42f] | 127 | |
---|
[33c8d73] | 128 | def apply(self, theory): |
---|
| 129 | return apply_resolution_matrix(self.weight_matrix, theory) |
---|
[d9633b1] | 130 | |
---|
| 131 | |
---|
[33c8d73] | 132 | def apply_resolution_matrix(weight_matrix, theory): |
---|
[d9633b1] | 133 | """ |
---|
| 134 | Apply the resolution weight matrix to the computed theory function. |
---|
| 135 | """ |
---|
[9404dd3] | 136 | #print("apply shapes", theory.shape, weight_matrix.shape) |
---|
[fdc538a] | 137 | Iq = np.dot(theory[None, :], weight_matrix) |
---|
[9404dd3] | 138 | #print("result shape",Iq.shape) |
---|
[d9633b1] | 139 | return Iq.flatten() |
---|
| 140 | |
---|
[7954f5c] | 141 | |
---|
[63b32bb] | 142 | def pinhole_resolution(q_calc, q, q_width): |
---|
[3fdb4b6] | 143 | """ |
---|
| 144 | Compute the convolution matrix *W* for pinhole resolution 1-D data. |
---|
| 145 | |
---|
| 146 | Each row *W[i]* determines the normalized weight that the corresponding |
---|
| 147 | points *q_calc* contribute to the resolution smeared point *q[i]*. Given |
---|
| 148 | *W*, the resolution smearing can be computed using *dot(W,q)*. |
---|
| 149 | |
---|
[7954f5c] | 150 | *q_calc* must be increasing. *q_width* must be greater than zero. |
---|
[3fdb4b6] | 151 | """ |
---|
[7954f5c] | 152 | # The current algorithm is a midpoint rectangle rule. In the test case, |
---|
| 153 | # neither trapezoid nor Simpson's rule improved the accuracy. |
---|
[3fdb4b6] | 154 | edges = bin_edges(q_calc) |
---|
[fdc538a] | 155 | edges[edges < 0.0] = 0.0 # clip edges below zero |
---|
[40a87fa] | 156 | cdf = erf((edges[:, None] - q[None, :]) / (sqrt(2.0)*q_width)[None, :]) |
---|
| 157 | weights = cdf[1:] - cdf[:-1] |
---|
[fdc538a] | 158 | weights /= np.sum(weights, axis=0)[None, :] |
---|
[3fdb4b6] | 159 | return weights |
---|
| 160 | |
---|
[49d1d42f] | 161 | |
---|
[eb588ef] | 162 | def slit_resolution(q_calc, q, width, height, n_height=30): |
---|
[7954f5c] | 163 | r""" |
---|
| 164 | Build a weight matrix to compute *I_s(q)* from *I(q_calc)*, given |
---|
[eb588ef] | 165 | $q_\perp$ = *width* and $q_\parallel$ = *height*. *n_height* is |
---|
| 166 | is the number of steps to use in the integration over $q_\parallel$ |
---|
| 167 | when both $q_\perp$ and $q_\parallel$ are non-zero. |
---|
[7954f5c] | 168 | |
---|
[eb588ef] | 169 | Each $q$ can have an independent width and height value even though |
---|
| 170 | current instruments use the same slit setting for all measured points. |
---|
[7954f5c] | 171 | |
---|
| 172 | If slit height is large relative to width, use: |
---|
| 173 | |
---|
| 174 | .. math:: |
---|
| 175 | |
---|
[eb588ef] | 176 | I_s(q_i) = \frac{1}{\Delta q_\perp} |
---|
[5258859] | 177 | \int_0^{\Delta q_\perp} |
---|
| 178 | I\left(\sqrt{q_i^2 + q_\perp^2}\right) \,dq_\perp |
---|
[7954f5c] | 179 | |
---|
| 180 | If slit width is large relative to height, use: |
---|
| 181 | |
---|
| 182 | .. math:: |
---|
| 183 | |
---|
[eb588ef] | 184 | I_s(q_i) = \frac{1}{2 \Delta q_\parallel} |
---|
| 185 | \int_{-\Delta q_\parallel}^{\Delta q_\parallel} |
---|
[5258859] | 186 | I\left(|q_i + q_\parallel|\right) \,dq_\parallel |
---|
[eb588ef] | 187 | |
---|
| 188 | For a mixture of slit width and height use: |
---|
| 189 | |
---|
| 190 | .. math:: |
---|
| 191 | |
---|
| 192 | I_s(q_i) = \frac{1}{2 \Delta q_\parallel \Delta q_\perp} |
---|
[5258859] | 193 | \int_{-\Delta q_\parallel}^{\Delta q_\parallel} |
---|
| 194 | \int_0^{\Delta q_\perp} |
---|
| 195 | I\left(\sqrt{(q_i + q_\parallel)^2 + q_\perp^2}\right) |
---|
| 196 | \,dq_\perp dq_\parallel |
---|
[eb588ef] | 197 | |
---|
[2f63032] | 198 | **Definition** |
---|
[eb588ef] | 199 | |
---|
| 200 | We are using the mid-point integration rule to assign weights to each |
---|
| 201 | element of a weight matrix $W$ so that |
---|
| 202 | |
---|
| 203 | .. math:: |
---|
| 204 | |
---|
[5258859] | 205 | I_s(q) = W\,I(q_\text{calc}) |
---|
[eb588ef] | 206 | |
---|
| 207 | If *q_calc* is at the mid-point, we can infer the bin edges from the |
---|
| 208 | pairwise averages of *q_calc*, adding the missing edges before |
---|
| 209 | *q_calc[0]* and after *q_calc[-1]*. |
---|
| 210 | |
---|
| 211 | For $q_\parallel = 0$, the smeared value can be computed numerically |
---|
| 212 | using the $u$ substitution |
---|
| 213 | |
---|
| 214 | .. math:: |
---|
| 215 | |
---|
| 216 | u_j = \sqrt{q_j^2 - q^2} |
---|
| 217 | |
---|
| 218 | This gives |
---|
| 219 | |
---|
| 220 | .. math:: |
---|
| 221 | |
---|
| 222 | I_s(q) \approx \sum_j I(u_j) \Delta u_j |
---|
| 223 | |
---|
| 224 | where $I(u_j)$ is the value at the mid-point, and $\Delta u_j$ is the |
---|
| 225 | difference between consecutive edges which have been first converted |
---|
| 226 | to $u$. Only $u_j \in [0, \Delta q_\perp]$ are used, which corresponds |
---|
[5258859] | 227 | to $q_j \in \left[q, \sqrt{q^2 + \Delta q_\perp}\right]$, so |
---|
[eb588ef] | 228 | |
---|
| 229 | .. math:: |
---|
| 230 | |
---|
| 231 | W_{ij} = \frac{1}{\Delta q_\perp} \Delta u_j |
---|
[5258859] | 232 | = \frac{1}{\Delta q_\perp} \left( |
---|
| 233 | \sqrt{q_{j+1}^2 - q_i^2} - \sqrt{q_j^2 - q_i^2} \right) |
---|
| 234 | \ \text{if}\ q_j \in \left[q_i, \sqrt{q_i^2 + q_\perp^2}\right] |
---|
[eb588ef] | 235 | |
---|
| 236 | where $I_s(q_i)$ is the theory function being computed and $q_j$ are the |
---|
| 237 | mid-points between the calculated values in *q_calc*. We tweak the |
---|
| 238 | edges of the initial and final intervals so that they lie on integration |
---|
| 239 | limits. |
---|
| 240 | |
---|
| 241 | (To be precise, the transformed midpoint $u(q_j)$ is not necessarily the |
---|
| 242 | midpoint of the edges $u((q_{j-1}+q_j)/2)$ and $u((q_j + q_{j+1})/2)$, |
---|
| 243 | but it is at least in the interval, so the approximation is going to be |
---|
| 244 | a little better than the left or right Riemann sum, and should be |
---|
| 245 | good enough for our purposes.) |
---|
| 246 | |
---|
| 247 | For $q_\perp = 0$, the $u$ substitution is simpler: |
---|
| 248 | |
---|
| 249 | .. math:: |
---|
| 250 | |
---|
[5258859] | 251 | u_j = \left|q_j - q\right| |
---|
[eb588ef] | 252 | |
---|
| 253 | so |
---|
| 254 | |
---|
| 255 | .. math:: |
---|
| 256 | |
---|
[5258859] | 257 | W_{ij} = \frac{1}{2 \Delta q_\parallel} \Delta u_j |
---|
[eb588ef] | 258 | = \frac{1}{2 \Delta q_\parallel} (q_{j+1} - q_j) |
---|
[5258859] | 259 | \ \text{if}\ q_j \in |
---|
| 260 | \left[q-\Delta q_\parallel, q+\Delta q_\parallel\right] |
---|
[eb588ef] | 261 | |
---|
| 262 | However, we need to support cases were $u_j < 0$, which means using |
---|
[5258859] | 263 | $2 (q_{j+1} - q_j)$ when $q_j \in \left[0, q_\parallel-q_i\right]$. |
---|
| 264 | This is not an issue for $q_i > q_\parallel$. |
---|
[eb588ef] | 265 | |
---|
[5258859] | 266 | For both $q_\perp > 0$ and $q_\parallel > 0$ we perform a 2 dimensional |
---|
[eb588ef] | 267 | integration with |
---|
| 268 | |
---|
| 269 | .. math:: |
---|
| 270 | |
---|
[5258859] | 271 | u_{jk} = \sqrt{q_j^2 - (q + (k\Delta q_\parallel/L))^2} |
---|
| 272 | \ \text{for}\ k = -L \ldots L |
---|
[eb588ef] | 273 | |
---|
| 274 | for $L$ = *n_height*. This gives |
---|
| 275 | |
---|
| 276 | .. math:: |
---|
| 277 | |
---|
| 278 | W_{ij} = \frac{1}{2 \Delta q_\perp q_\parallel} |
---|
[5258859] | 279 | \sum_{k=-L}^L \Delta u_{jk} |
---|
| 280 | \left(\frac{\Delta q_\parallel}{2 L + 1}\right) |
---|
[eb588ef] | 281 | |
---|
| 282 | |
---|
[7954f5c] | 283 | """ |
---|
[dbde9f8] | 284 | #np.set_printoptions(precision=6, linewidth=10000) |
---|
[7954f5c] | 285 | |
---|
[dbde9f8] | 286 | # The current algorithm is a midpoint rectangle rule. |
---|
[7954f5c] | 287 | q_edges = bin_edges(q_calc) # Note: requires q > 0 |
---|
[fdc538a] | 288 | q_edges[q_edges < 0.0] = 0.0 # clip edges below zero |
---|
[dbde9f8] | 289 | weights = np.zeros((len(q), len(q_calc)), 'd') |
---|
| 290 | |
---|
[9404dd3] | 291 | #print(q_calc) |
---|
[dbde9f8] | 292 | for i, (qi, w, h) in enumerate(zip(q, width, height)): |
---|
| 293 | if w == 0. and h == 0.: |
---|
| 294 | # Perfect resolution, so return the theory value directly. |
---|
| 295 | # Note: assumes that q is a subset of q_calc. If qi need not be |
---|
| 296 | # in q_calc, then we can do a weighted interpolation by looking |
---|
| 297 | # up qi in q_calc, then weighting the result by the relative |
---|
| 298 | # distance to the neighbouring points. |
---|
| 299 | weights[i, :] = (q_calc == qi) |
---|
[eb588ef] | 300 | elif h == 0: |
---|
| 301 | weights[i, :] = _q_perp_weights(q_edges, qi, w) |
---|
[dbde9f8] | 302 | elif w == 0: |
---|
[eb588ef] | 303 | in_x = 1.0 * ((q_calc >= qi-h) & (q_calc <= qi+h)) |
---|
| 304 | abs_x = 1.0*(q_calc < abs(qi - h)) if qi < h else 0. |
---|
[9404dd3] | 305 | #print(qi - h, qi + h) |
---|
| 306 | #print(in_x + abs_x) |
---|
[fdc538a] | 307 | weights[i, :] = (in_x + abs_x) * np.diff(q_edges) / (2*h) |
---|
[eb588ef] | 308 | else: |
---|
[40a87fa] | 309 | for k in range(-n_height, h_height+1): |
---|
| 310 | weights[i, :] += _q_perp_weights(q_edges, qi+k*h/n_height, w) |
---|
| 311 | weights[i, :] /= 2*n_height + 1 |
---|
[dbde9f8] | 312 | |
---|
| 313 | return weights.T |
---|
[7954f5c] | 314 | |
---|
| 315 | |
---|
[eb588ef] | 316 | def _q_perp_weights(q_edges, qi, w): |
---|
| 317 | # Convert bin edges from q to u |
---|
| 318 | u_limit = np.sqrt(qi**2 + w**2) |
---|
| 319 | u_edges = q_edges**2 - qi**2 |
---|
| 320 | u_edges[q_edges < abs(qi)] = 0. |
---|
| 321 | u_edges[q_edges > u_limit] = u_limit**2 - qi**2 |
---|
| 322 | weights = np.diff(np.sqrt(u_edges))/w |
---|
[9404dd3] | 323 | #print("i, qi",i,qi,qi+width) |
---|
| 324 | #print(q_calc) |
---|
| 325 | #print(weights) |
---|
[eb588ef] | 326 | return weights |
---|
| 327 | |
---|
| 328 | |
---|
[7954f5c] | 329 | def pinhole_extend_q(q, q_width, nsigma=3): |
---|
[d9633b1] | 330 | """ |
---|
| 331 | Given *q* and *q_width*, find a set of sampling points *q_calc* so |
---|
[5258859] | 332 | that each point $I(q)$ has sufficient support from the underlying |
---|
[d9633b1] | 333 | function. |
---|
| 334 | """ |
---|
[7954f5c] | 335 | q_min, q_max = np.min(q - nsigma*q_width), np.max(q + nsigma*q_width) |
---|
[f1b8c90] | 336 | return linear_extrapolation(q, q_min, q_max) |
---|
[49d1d42f] | 337 | |
---|
| 338 | |
---|
[7954f5c] | 339 | def slit_extend_q(q, width, height): |
---|
| 340 | """ |
---|
| 341 | Given *q*, *width* and *height*, find a set of sampling points *q_calc* so |
---|
| 342 | that each point I(q) has sufficient support from the underlying |
---|
| 343 | function. |
---|
| 344 | """ |
---|
[eb588ef] | 345 | q_min, q_max = np.min(q-height), np.max(np.sqrt((q+height)**2 + width**2)) |
---|
| 346 | |
---|
[7954f5c] | 347 | return geometric_extrapolation(q, q_min, q_max) |
---|
[49d1d42f] | 348 | |
---|
| 349 | |
---|
[3fdb4b6] | 350 | def bin_edges(x): |
---|
[d9633b1] | 351 | """ |
---|
| 352 | Determine bin edges from bin centers, assuming that edges are centered |
---|
| 353 | between the bins. |
---|
| 354 | |
---|
| 355 | Note: this uses the arithmetic mean, which may not be appropriate for |
---|
| 356 | log-scaled data. |
---|
| 357 | """ |
---|
[fdc538a] | 358 | if len(x) < 2 or (np.diff(x) < 0).any(): |
---|
[3fdb4b6] | 359 | raise ValueError("Expected bins to be an increasing set") |
---|
| 360 | edges = np.hstack([ |
---|
| 361 | x[0] - 0.5*(x[1] - x[0]), # first point minus half first interval |
---|
| 362 | 0.5*(x[1:] + x[:-1]), # mid points of all central intervals |
---|
| 363 | x[-1] + 0.5*(x[-1] - x[-2]), # last point plus half last interval |
---|
| 364 | ]) |
---|
| 365 | return edges |
---|
[49d1d42f] | 366 | |
---|
[7954f5c] | 367 | |
---|
| 368 | def interpolate(q, max_step): |
---|
| 369 | """ |
---|
| 370 | Returns *q_calc* with points spaced at most max_step apart. |
---|
| 371 | """ |
---|
| 372 | step = np.diff(q) |
---|
[fdc538a] | 373 | index = step > max_step |
---|
[7954f5c] | 374 | if np.any(index): |
---|
| 375 | inserts = [] |
---|
[fdc538a] | 376 | for q_i, step_i in zip(q[:-1][index], step[index]): |
---|
[7954f5c] | 377 | n = np.ceil(step_i/max_step) |
---|
[fdc538a] | 378 | inserts.extend(q_i + np.arange(1, n)*(step_i/n)) |
---|
[7954f5c] | 379 | # Extend a couple of fringes beyond the end of the data |
---|
[fdc538a] | 380 | inserts.extend(q[-1] + np.arange(1, 8)*max_step) |
---|
| 381 | q_calc = np.sort(np.hstack((q, inserts))) |
---|
[7954f5c] | 382 | else: |
---|
| 383 | q_calc = q |
---|
| 384 | return q_calc |
---|
| 385 | |
---|
| 386 | |
---|
| 387 | def linear_extrapolation(q, q_min, q_max): |
---|
| 388 | """ |
---|
| 389 | Extrapolate *q* out to [*q_min*, *q_max*] using the step size in *q* as |
---|
[f1b8c90] | 390 | a guide. Extrapolation below uses about the same size as the first |
---|
| 391 | interval. Extrapolation above uses about the same size as the final |
---|
[7954f5c] | 392 | interval. |
---|
| 393 | |
---|
| 394 | if *q_min* is zero or less then *q[0]/10* is used instead. |
---|
| 395 | """ |
---|
| 396 | q = np.sort(q) |
---|
[ea75043] | 397 | if q_min + 2*MINIMUM_RESOLUTION < q[0]: |
---|
[eb588ef] | 398 | if q_min <= 0: q_min = q_min*MIN_Q_SCALE_FOR_NEGATIVE_Q_EXTRAPOLATION |
---|
[fdc538a] | 399 | n_low = np.ceil((q[0]-q_min) / (q[1]-q[0])) if q[1] > q[0] else 15 |
---|
[f1b8c90] | 400 | q_low = np.linspace(q_min, q[0], n_low+1)[:-1] |
---|
[7954f5c] | 401 | else: |
---|
| 402 | q_low = [] |
---|
[ea75043] | 403 | if q_max - 2*MINIMUM_RESOLUTION > q[-1]: |
---|
[fdc538a] | 404 | n_high = np.ceil((q_max-q[-1]) / (q[-1]-q[-2])) if q[-1] > q[-2] else 15 |
---|
[f1b8c90] | 405 | q_high = np.linspace(q[-1], q_max, n_high+1)[1:] |
---|
[7954f5c] | 406 | else: |
---|
| 407 | q_high = [] |
---|
| 408 | return np.concatenate([q_low, q, q_high]) |
---|
| 409 | |
---|
| 410 | |
---|
[f1b8c90] | 411 | def geometric_extrapolation(q, q_min, q_max, points_per_decade=None): |
---|
[7954f5c] | 412 | r""" |
---|
| 413 | Extrapolate *q* to [*q_min*, *q_max*] using geometric steps, with the |
---|
| 414 | average geometric step size in *q* as the step size. |
---|
| 415 | |
---|
| 416 | if *q_min* is zero or less then *q[0]/10* is used instead. |
---|
| 417 | |
---|
[f1b8c90] | 418 | *points_per_decade* sets the ratio between consecutive steps such |
---|
| 419 | that there will be $n$ points used for every factor of 10 increase |
---|
| 420 | in *q*. |
---|
| 421 | |
---|
| 422 | If *points_per_decade* is not given, it will be estimated as follows. |
---|
| 423 | Starting at $q_1$ and stepping geometrically by $\Delta q$ to $q_n$ |
---|
| 424 | in $n$ points gives a geometric average of: |
---|
[7954f5c] | 425 | |
---|
| 426 | .. math:: |
---|
| 427 | |
---|
| 428 | \log \Delta q = (\log q_n - log q_1) / (n - 1) |
---|
| 429 | |
---|
| 430 | From this we can compute the number of steps required to extend $q$ |
---|
| 431 | from $q_n$ to $q_\text{max}$ by $\Delta q$ as: |
---|
| 432 | |
---|
| 433 | .. math:: |
---|
| 434 | |
---|
| 435 | n_\text{extend} = (\log q_\text{max} - \log q_n) / \log \Delta q |
---|
| 436 | |
---|
| 437 | Substituting: |
---|
| 438 | |
---|
[d138d43] | 439 | .. math:: |
---|
| 440 | |
---|
[a146eaa] | 441 | n_\text{extend} = (n-1) (\log q_\text{max} - \log q_n) |
---|
[7954f5c] | 442 | / (\log q_n - log q_1) |
---|
| 443 | """ |
---|
| 444 | q = np.sort(q) |
---|
[f1b8c90] | 445 | if points_per_decade is None: |
---|
| 446 | log_delta_q = (len(q) - 1) / (log(q[-1]) - log(q[0])) |
---|
| 447 | else: |
---|
| 448 | log_delta_q = log(10.) / points_per_decade |
---|
[7954f5c] | 449 | if q_min < q[0]: |
---|
[eb588ef] | 450 | if q_min < 0: q_min = q[0]*MIN_Q_SCALE_FOR_NEGATIVE_Q_EXTRAPOLATION |
---|
[f1b8c90] | 451 | n_low = log_delta_q * (log(q[0])-log(q_min)) |
---|
[fdc538a] | 452 | q_low = np.logspace(log10(q_min), log10(q[0]), np.ceil(n_low)+1)[:-1] |
---|
[7954f5c] | 453 | else: |
---|
| 454 | q_low = [] |
---|
| 455 | if q_max > q[-1]: |
---|
[f1b8c90] | 456 | n_high = log_delta_q * (log(q_max)-log(q[-1])) |
---|
[7954f5c] | 457 | q_high = np.logspace(log10(q[-1]), log10(q_max), np.ceil(n_high)+1)[1:] |
---|
| 458 | else: |
---|
| 459 | q_high = [] |
---|
| 460 | return np.concatenate([q_low, q, q_high]) |
---|
| 461 | |
---|
| 462 | |
---|
[d9633b1] | 463 | ############################################################################ |
---|
| 464 | # unit tests |
---|
| 465 | ############################################################################ |
---|
| 466 | import unittest |
---|
| 467 | |
---|
[7954f5c] | 468 | |
---|
| 469 | def eval_form(q, form, pars): |
---|
[5925e90] | 470 | """ |
---|
| 471 | Return the SAS model evaluated at *q*. |
---|
| 472 | |
---|
| 473 | *form* is the SAS model returned from :fun:`core.load_model`. |
---|
| 474 | |
---|
| 475 | *pars* are the parameter values to use when evaluating. |
---|
| 476 | """ |
---|
[6d6508e] | 477 | from sasmodels import direct_model |
---|
[48fbd50] | 478 | kernel = form.make_kernel([q]) |
---|
[6d6508e] | 479 | theory = direct_model.call_kernel(kernel, pars) |
---|
[7954f5c] | 480 | kernel.release() |
---|
| 481 | return theory |
---|
| 482 | |
---|
| 483 | |
---|
| 484 | def gaussian(q, q0, dq): |
---|
[5925e90] | 485 | """ |
---|
| 486 | Return the Gaussian resolution function. |
---|
| 487 | |
---|
| 488 | *q0* is the center, *dq* is the width and *q* are the points to evaluate. |
---|
| 489 | """ |
---|
[7954f5c] | 490 | return exp(-0.5*((q-q0)/dq)**2)/(sqrt(2*pi)*dq) |
---|
| 491 | |
---|
| 492 | |
---|
[dbde9f8] | 493 | def romberg_slit_1d(q, width, height, form, pars): |
---|
[7954f5c] | 494 | """ |
---|
| 495 | Romberg integration for slit resolution. |
---|
| 496 | |
---|
| 497 | This is an adaptive integration technique. It is called with settings |
---|
| 498 | that make it slow to evaluate but give it good accuracy. |
---|
| 499 | """ |
---|
[7ae2b7f] | 500 | from scipy.integrate import romberg # type: ignore |
---|
[6871c9e] | 501 | |
---|
[6d6508e] | 502 | par_set = set([p.name for p in form.info.parameters.call_parameters]) |
---|
[303d8d6] | 503 | if any(k not in par_set for k in pars.keys()): |
---|
| 504 | extra = set(pars.keys()) - par_set |
---|
[d2bb604] | 505 | raise ValueError("bad parameters: [%s] not in [%s]" |
---|
| 506 | % (", ".join(sorted(extra)), |
---|
| 507 | ", ".join(sorted(pars.keys())))) |
---|
[6871c9e] | 508 | |
---|
[dbde9f8] | 509 | if np.isscalar(width): |
---|
| 510 | width = [width]*len(q) |
---|
| 511 | if np.isscalar(height): |
---|
| 512 | height = [height]*len(q) |
---|
| 513 | _int_w = lambda w, qi: eval_form(sqrt(qi**2 + w**2), form, pars) |
---|
| 514 | _int_h = lambda h, qi: eval_form(abs(qi+h), form, pars) |
---|
[eb588ef] | 515 | # If both width and height are defined, then it is too slow to use dblquad. |
---|
| 516 | # Instead use trapz on a fixed grid, interpolated into the I(Q) for |
---|
| 517 | # the extended Q range. |
---|
| 518 | #_int_wh = lambda w, h, qi: eval_form(sqrt((qi+h)**2 + w**2), form, pars) |
---|
| 519 | q_calc = slit_extend_q(q, np.asarray(width), np.asarray(height)) |
---|
| 520 | Iq = eval_form(q_calc, form, pars) |
---|
[dbde9f8] | 521 | result = np.empty(len(q)) |
---|
| 522 | for i, (qi, w, h) in enumerate(zip(q, width, height)): |
---|
| 523 | if h == 0.: |
---|
[40a87fa] | 524 | total = romberg(_int_w, 0, w, args=(qi,), |
---|
| 525 | divmax=100, vec_func=True, tol=0, rtol=1e-8) |
---|
| 526 | result[i] = total/w |
---|
[dbde9f8] | 527 | elif w == 0.: |
---|
[40a87fa] | 528 | total = romberg(_int_h, -h, h, args=(qi,), |
---|
| 529 | divmax=100, vec_func=True, tol=0, rtol=1e-8) |
---|
| 530 | result[i] = total/(2*h) |
---|
[dbde9f8] | 531 | else: |
---|
[fdc538a] | 532 | w_grid = np.linspace(0, w, 21)[None, :] |
---|
| 533 | h_grid = np.linspace(-h, h, 23)[:, None] |
---|
[40a87fa] | 534 | u_sub = sqrt((qi+h_grid)**2 + w_grid**2) |
---|
| 535 | f_at_u = np.interp(u_sub, q_calc, Iq) |
---|
[9404dd3] | 536 | #print(np.trapz(Iu, w_grid, axis=1)) |
---|
[40a87fa] | 537 | total = np.trapz(np.trapz(f_at_u, w_grid, axis=1), h_grid[:, 0]) |
---|
| 538 | result[i] = total / (2*h*w) |
---|
[fdc538a] | 539 | # from scipy.integrate import dblquad |
---|
| 540 | # r, err = dblquad(_int_wh, -h, h, lambda h: 0., lambda h: w, |
---|
| 541 | # args=(qi,)) |
---|
| 542 | # result[i] = r/(w*2*h) |
---|
[dbde9f8] | 543 | |
---|
[7954f5c] | 544 | # r should be [float, ...], but it is [array([float]), array([float]),...] |
---|
[dbde9f8] | 545 | return result |
---|
[7954f5c] | 546 | |
---|
| 547 | |
---|
[f1b8c90] | 548 | def romberg_pinhole_1d(q, q_width, form, pars, nsigma=5): |
---|
[7954f5c] | 549 | """ |
---|
| 550 | Romberg integration for pinhole resolution. |
---|
| 551 | |
---|
| 552 | This is an adaptive integration technique. It is called with settings |
---|
| 553 | that make it slow to evaluate but give it good accuracy. |
---|
| 554 | """ |
---|
[7ae2b7f] | 555 | from scipy.integrate import romberg # type: ignore |
---|
[7954f5c] | 556 | |
---|
[6d6508e] | 557 | par_set = set([p.name for p in form.info.parameters.call_parameters]) |
---|
[303d8d6] | 558 | if any(k not in par_set for k in pars.keys()): |
---|
| 559 | extra = set(pars.keys()) - par_set |
---|
[d2bb604] | 560 | raise ValueError("bad parameters: [%s] not in [%s]" |
---|
| 561 | % (", ".join(sorted(extra)), |
---|
| 562 | ", ".join(sorted(pars.keys())))) |
---|
[6871c9e] | 563 | |
---|
[2472141] | 564 | func = lambda q, q0, dq: eval_form(q, form, pars)*gaussian(q, q0, dq) |
---|
| 565 | total = [romberg(func, max(qi-nsigma*dqi, 1e-10*q[0]), qi+nsigma*dqi, |
---|
[40a87fa] | 566 | args=(qi, dqi), divmax=100, vec_func=True, |
---|
| 567 | tol=0, rtol=1e-8) |
---|
| 568 | for qi, dqi in zip(q, q_width)] |
---|
[2472141] | 569 | return np.asarray(total).flatten() |
---|
[7954f5c] | 570 | |
---|
| 571 | |
---|
[d9633b1] | 572 | class ResolutionTest(unittest.TestCase): |
---|
[5925e90] | 573 | """ |
---|
| 574 | Test the resolution calculations. |
---|
| 575 | """ |
---|
[d9633b1] | 576 | |
---|
| 577 | def setUp(self): |
---|
[33c8d73] | 578 | self.x = 0.001*np.arange(1, 11) |
---|
[d9633b1] | 579 | self.y = self.Iq(self.x) |
---|
| 580 | |
---|
| 581 | def Iq(self, q): |
---|
| 582 | "Linear function for resolution unit test" |
---|
| 583 | return 12.0 - 1000.0*q |
---|
| 584 | |
---|
| 585 | def test_perfect(self): |
---|
| 586 | """ |
---|
| 587 | Perfect resolution and no smearing. |
---|
| 588 | """ |
---|
| 589 | resolution = Perfect1D(self.x) |
---|
[33c8d73] | 590 | theory = self.Iq(resolution.q_calc) |
---|
| 591 | output = resolution.apply(theory) |
---|
[d9633b1] | 592 | np.testing.assert_equal(output, self.y) |
---|
| 593 | |
---|
| 594 | def test_slit_zero(self): |
---|
| 595 | """ |
---|
| 596 | Slit smearing with perfect resolution. |
---|
| 597 | """ |
---|
[ea75043] | 598 | resolution = Slit1D(self.x, qx_width=0, qy_width=0, q_calc=self.x) |
---|
[33c8d73] | 599 | theory = self.Iq(resolution.q_calc) |
---|
| 600 | output = resolution.apply(theory) |
---|
[d9633b1] | 601 | np.testing.assert_equal(output, self.y) |
---|
| 602 | |
---|
[7954f5c] | 603 | @unittest.skip("not yet supported") |
---|
| 604 | def test_slit_high(self): |
---|
[d9633b1] | 605 | """ |
---|
| 606 | Slit smearing with height 0.005 |
---|
| 607 | """ |
---|
[ea75043] | 608 | resolution = Slit1D(self.x, qx_width=0, qy_width=0.005, q_calc=self.x) |
---|
[7954f5c] | 609 | theory = self.Iq(resolution.q_calc) |
---|
| 610 | output = resolution.apply(theory) |
---|
[fdc538a] | 611 | answer = [ |
---|
| 612 | 9.0618, 8.6402, 8.1187, 7.1392, 6.1528, |
---|
| 613 | 5.5555, 4.5584, 3.5606, 2.5623, 2.0000, |
---|
| 614 | ] |
---|
[7954f5c] | 615 | np.testing.assert_allclose(output, answer, atol=1e-4) |
---|
| 616 | |
---|
| 617 | @unittest.skip("not yet supported") |
---|
| 618 | def test_slit_both_high(self): |
---|
| 619 | """ |
---|
| 620 | Slit smearing with width < 100*height. |
---|
| 621 | """ |
---|
| 622 | q = np.logspace(-4, -1, 10) |
---|
[ea75043] | 623 | resolution = Slit1D(q, qx_width=0.2, qy_width=np.inf) |
---|
[7954f5c] | 624 | theory = 1000*self.Iq(resolution.q_calc**4) |
---|
| 625 | output = resolution.apply(theory) |
---|
[fdc538a] | 626 | answer = [ |
---|
| 627 | 8.85785, 8.43012, 7.92687, 6.94566, 6.03660, |
---|
| 628 | 5.40363, 4.40655, 3.40880, 2.41058, 2.00000, |
---|
| 629 | ] |
---|
[7954f5c] | 630 | np.testing.assert_allclose(output, answer, atol=1e-4) |
---|
| 631 | |
---|
| 632 | @unittest.skip("not yet supported") |
---|
| 633 | def test_slit_wide(self): |
---|
| 634 | """ |
---|
| 635 | Slit smearing with width 0.0002 |
---|
| 636 | """ |
---|
[ea75043] | 637 | resolution = Slit1D(self.x, qx_width=0.0002, qy_width=0, q_calc=self.x) |
---|
[33c8d73] | 638 | theory = self.Iq(resolution.q_calc) |
---|
| 639 | output = resolution.apply(theory) |
---|
[fdc538a] | 640 | answer = [ |
---|
| 641 | 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, |
---|
| 642 | ] |
---|
[7954f5c] | 643 | np.testing.assert_allclose(output, answer, atol=1e-4) |
---|
| 644 | |
---|
| 645 | @unittest.skip("not yet supported") |
---|
| 646 | def test_slit_both_wide(self): |
---|
| 647 | """ |
---|
| 648 | Slit smearing with width > 100*height. |
---|
| 649 | """ |
---|
[ea75043] | 650 | resolution = Slit1D(self.x, qx_width=0.0002, qy_width=0.000001, |
---|
[7954f5c] | 651 | q_calc=self.x) |
---|
| 652 | theory = self.Iq(resolution.q_calc) |
---|
| 653 | output = resolution.apply(theory) |
---|
[fdc538a] | 654 | answer = [ |
---|
| 655 | 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, |
---|
| 656 | ] |
---|
[d9633b1] | 657 | np.testing.assert_allclose(output, answer, atol=1e-4) |
---|
| 658 | |
---|
| 659 | def test_pinhole_zero(self): |
---|
| 660 | """ |
---|
| 661 | Pinhole smearing with perfect resolution |
---|
| 662 | """ |
---|
| 663 | resolution = Pinhole1D(self.x, 0.0*self.x) |
---|
[33c8d73] | 664 | theory = self.Iq(resolution.q_calc) |
---|
| 665 | output = resolution.apply(theory) |
---|
[d9633b1] | 666 | np.testing.assert_equal(output, self.y) |
---|
| 667 | |
---|
| 668 | def test_pinhole(self): |
---|
| 669 | """ |
---|
| 670 | Pinhole smearing with dQ = 0.001 [Note: not dQ/Q = 0.001] |
---|
| 671 | """ |
---|
| 672 | resolution = Pinhole1D(self.x, 0.001*np.ones_like(self.x), |
---|
| 673 | q_calc=self.x) |
---|
[33c8d73] | 674 | theory = 12.0-1000.0*resolution.q_calc |
---|
| 675 | output = resolution.apply(theory) |
---|
[fdc538a] | 676 | answer = [ |
---|
| 677 | 10.44785079, 9.84991299, 8.98101708, |
---|
| 678 | 7.99906585, 6.99998311, 6.00001689, |
---|
| 679 | 5.00093415, 4.01898292, 3.15008701, 2.55214921, |
---|
| 680 | ] |
---|
[d9633b1] | 681 | np.testing.assert_allclose(output, answer, atol=1e-8) |
---|
| 682 | |
---|
[7954f5c] | 683 | |
---|
| 684 | class IgorComparisonTest(unittest.TestCase): |
---|
[5925e90] | 685 | """ |
---|
| 686 | Test resolution calculations against those returned by Igor. |
---|
| 687 | """ |
---|
[7954f5c] | 688 | |
---|
| 689 | def setUp(self): |
---|
| 690 | self.pars = TEST_PARS_PINHOLE_SPHERE |
---|
| 691 | from sasmodels import core |
---|
[8b935d1] | 692 | self.model = core.load_model("sphere", dtype='double') |
---|
[7954f5c] | 693 | |
---|
[5925e90] | 694 | def _eval_sphere(self, pars, resolution): |
---|
[6d6508e] | 695 | from sasmodels import direct_model |
---|
[48fbd50] | 696 | kernel = self.model.make_kernel([resolution.q_calc]) |
---|
[6d6508e] | 697 | theory = direct_model.call_kernel(kernel, pars) |
---|
[7954f5c] | 698 | result = resolution.apply(theory) |
---|
| 699 | kernel.release() |
---|
| 700 | return result |
---|
| 701 | |
---|
[5925e90] | 702 | def _compare(self, q, output, answer, tolerance): |
---|
[dbde9f8] | 703 | #err = (output - answer)/answer |
---|
| 704 | #idx = abs(err) >= tolerance |
---|
| 705 | #problem = zip(q[idx], output[idx], answer[idx], err[idx]) |
---|
[9404dd3] | 706 | #print("\n".join(str(v) for v in problem)) |
---|
[7954f5c] | 707 | np.testing.assert_allclose(output, answer, rtol=tolerance) |
---|
| 708 | |
---|
| 709 | def test_perfect(self): |
---|
| 710 | """ |
---|
| 711 | Compare sphere model with NIST Igor SANS, no resolution smearing. |
---|
| 712 | """ |
---|
| 713 | pars = TEST_PARS_SLIT_SPHERE |
---|
| 714 | data_string = TEST_DATA_SLIT_SPHERE |
---|
| 715 | |
---|
| 716 | data = np.loadtxt(data_string.split('\n')).T |
---|
[40a87fa] | 717 | q, _, answer, _ = data |
---|
[7954f5c] | 718 | resolution = Perfect1D(q) |
---|
[5925e90] | 719 | output = self._eval_sphere(pars, resolution) |
---|
| 720 | self._compare(q, output, answer, 1e-6) |
---|
[7954f5c] | 721 | |
---|
| 722 | def test_pinhole(self): |
---|
| 723 | """ |
---|
| 724 | Compare pinhole resolution smearing with NIST Igor SANS |
---|
| 725 | """ |
---|
| 726 | pars = TEST_PARS_PINHOLE_SPHERE |
---|
| 727 | data_string = TEST_DATA_PINHOLE_SPHERE |
---|
| 728 | |
---|
| 729 | data = np.loadtxt(data_string.split('\n')).T |
---|
| 730 | q, q_width, answer = data |
---|
| 731 | resolution = Pinhole1D(q, q_width) |
---|
[5925e90] | 732 | output = self._eval_sphere(pars, resolution) |
---|
[7954f5c] | 733 | # TODO: relative error should be lower |
---|
[5925e90] | 734 | self._compare(q, output, answer, 3e-4) |
---|
[7954f5c] | 735 | |
---|
| 736 | def test_pinhole_romberg(self): |
---|
| 737 | """ |
---|
| 738 | Compare pinhole resolution smearing with romberg integration result. |
---|
| 739 | """ |
---|
| 740 | pars = TEST_PARS_PINHOLE_SPHERE |
---|
| 741 | data_string = TEST_DATA_PINHOLE_SPHERE |
---|
| 742 | pars['radius'] *= 5 |
---|
| 743 | |
---|
| 744 | data = np.loadtxt(data_string.split('\n')).T |
---|
| 745 | q, q_width, answer = data |
---|
| 746 | answer = romberg_pinhole_1d(q, q_width, self.model, pars) |
---|
| 747 | ## Getting 0.1% requires 5 sigma and 200 points per fringe |
---|
| 748 | #q_calc = interpolate(pinhole_extend_q(q, q_width, nsigma=5), |
---|
[40a87fa] | 749 | # 2*np.pi/pars['radius']/200) |
---|
[7954f5c] | 750 | #tol = 0.001 |
---|
| 751 | ## The default 3 sigma and no extra points gets 1% |
---|
[7ae2b7f] | 752 | q_calc = None # type: np.ndarray |
---|
| 753 | tol = 0.01 |
---|
[7954f5c] | 754 | resolution = Pinhole1D(q, q_width, q_calc=q_calc) |
---|
[5925e90] | 755 | output = self._eval_sphere(pars, resolution) |
---|
[7954f5c] | 756 | if 0: # debug plot |
---|
[7ae2b7f] | 757 | import matplotlib.pyplot as plt # type: ignore |
---|
[7954f5c] | 758 | resolution = Perfect1D(q) |
---|
[5925e90] | 759 | source = self._eval_sphere(pars, resolution) |
---|
[7954f5c] | 760 | plt.loglog(q, source, '.') |
---|
| 761 | plt.loglog(q, answer, '-', hold=True) |
---|
| 762 | plt.loglog(q, output, '-', hold=True) |
---|
| 763 | plt.show() |
---|
[5925e90] | 764 | self._compare(q, output, answer, tol) |
---|
[7954f5c] | 765 | |
---|
| 766 | def test_slit(self): |
---|
| 767 | """ |
---|
| 768 | Compare slit resolution smearing with NIST Igor SANS |
---|
| 769 | """ |
---|
| 770 | pars = TEST_PARS_SLIT_SPHERE |
---|
| 771 | data_string = TEST_DATA_SLIT_SPHERE |
---|
| 772 | |
---|
| 773 | data = np.loadtxt(data_string.split('\n')).T |
---|
| 774 | q, delta_qv, _, answer = data |
---|
[ea75043] | 775 | resolution = Slit1D(q, qx_width=delta_qv, qy_width=0) |
---|
[5925e90] | 776 | output = self._eval_sphere(pars, resolution) |
---|
[7954f5c] | 777 | # TODO: eliminate Igor test since it is too inaccurate to be useful. |
---|
| 778 | # This means we can eliminate the test data as well, and instead |
---|
| 779 | # use a generated q vector. |
---|
[5925e90] | 780 | self._compare(q, output, answer, 0.5) |
---|
[7954f5c] | 781 | |
---|
| 782 | def test_slit_romberg(self): |
---|
| 783 | """ |
---|
| 784 | Compare slit resolution smearing with romberg integration result. |
---|
| 785 | """ |
---|
| 786 | pars = TEST_PARS_SLIT_SPHERE |
---|
| 787 | data_string = TEST_DATA_SLIT_SPHERE |
---|
| 788 | |
---|
| 789 | data = np.loadtxt(data_string.split('\n')).T |
---|
| 790 | q, delta_qv, _, answer = data |
---|
[dbde9f8] | 791 | answer = romberg_slit_1d(q, delta_qv, 0., self.model, pars) |
---|
[40a87fa] | 792 | q_calc = slit_extend_q(interpolate(q, 2*np.pi/pars['radius']/20), |
---|
[dbde9f8] | 793 | delta_qv[0], 0.) |
---|
[ea75043] | 794 | resolution = Slit1D(q, qx_width=delta_qv, qy_width=0, q_calc=q_calc) |
---|
[5925e90] | 795 | output = self._eval_sphere(pars, resolution) |
---|
[7954f5c] | 796 | # TODO: relative error should be lower |
---|
[5925e90] | 797 | self._compare(q, output, answer, 0.025) |
---|
[7954f5c] | 798 | |
---|
[6871c9e] | 799 | def test_ellipsoid(self): |
---|
| 800 | """ |
---|
| 801 | Compare romberg integration for ellipsoid model. |
---|
| 802 | """ |
---|
| 803 | from .core import load_model |
---|
| 804 | pars = { |
---|
| 805 | 'scale':0.05, |
---|
[486fcf6] | 806 | 'r_polar':500, 'r_equatorial':15000, |
---|
| 807 | 'sld':6, 'sld_solvent': 1, |
---|
[6871c9e] | 808 | } |
---|
| 809 | form = load_model('ellipsoid', dtype='double') |
---|
[fdc538a] | 810 | q = np.logspace(log10(4e-5), log10(2.5e-2), 68) |
---|
[dbde9f8] | 811 | width, height = 0.117, 0. |
---|
[ea75043] | 812 | resolution = Slit1D(q, qx_width=width, qy_width=height) |
---|
[dbde9f8] | 813 | answer = romberg_slit_1d(q, width, height, form, pars) |
---|
[6871c9e] | 814 | output = resolution.apply(eval_form(resolution.q_calc, form, pars)) |
---|
| 815 | # TODO: 10% is too much error; use better algorithm |
---|
[9404dd3] | 816 | #print(np.max(abs(answer-output)/answer)) |
---|
[5925e90] | 817 | self._compare(q, output, answer, 0.1) |
---|
[6871c9e] | 818 | |
---|
[7954f5c] | 819 | #TODO: can sas q spacing be too sparse for the resolution calculation? |
---|
| 820 | @unittest.skip("suppress sparse data test; not supported by current code") |
---|
| 821 | def test_pinhole_sparse(self): |
---|
| 822 | """ |
---|
| 823 | Compare pinhole resolution smearing with NIST Igor SANS on sparse data |
---|
| 824 | """ |
---|
| 825 | pars = TEST_PARS_PINHOLE_SPHERE |
---|
| 826 | data_string = TEST_DATA_PINHOLE_SPHERE |
---|
| 827 | |
---|
| 828 | data = np.loadtxt(data_string.split('\n')).T |
---|
| 829 | q, q_width, answer = data[:, ::20] # Take every nth point |
---|
| 830 | resolution = Pinhole1D(q, q_width) |
---|
[5925e90] | 831 | output = self._eval_sphere(pars, resolution) |
---|
| 832 | self._compare(q, output, answer, 1e-6) |
---|
[7954f5c] | 833 | |
---|
| 834 | |
---|
| 835 | # pinhole sphere parameters |
---|
| 836 | TEST_PARS_PINHOLE_SPHERE = { |
---|
| 837 | 'scale': 1.0, 'background': 0.01, |
---|
[486fcf6] | 838 | 'radius': 60.0, 'sld': 1, 'sld_solvent': 6.3, |
---|
[7954f5c] | 839 | } |
---|
| 840 | # Q, dQ, I(Q) calculated by NIST Igor SANS package |
---|
| 841 | TEST_DATA_PINHOLE_SPHERE = """\ |
---|
[d9633b1] | 842 | 0.001278 0.0002847 2538.41176383 |
---|
| 843 | 0.001562 0.0002905 2536.91820405 |
---|
| 844 | 0.001846 0.0002956 2535.13182479 |
---|
| 845 | 0.002130 0.0003017 2533.06217813 |
---|
| 846 | 0.002414 0.0003087 2530.70378586 |
---|
| 847 | 0.002698 0.0003165 2528.05024192 |
---|
| 848 | 0.002982 0.0003249 2525.10408349 |
---|
| 849 | 0.003266 0.0003340 2521.86667499 |
---|
| 850 | 0.003550 0.0003437 2518.33907750 |
---|
| 851 | 0.003834 0.0003539 2514.52246995 |
---|
| 852 | 0.004118 0.0003646 2510.41798319 |
---|
| 853 | 0.004402 0.0003757 2506.02690988 |
---|
| 854 | 0.004686 0.0003872 2501.35067884 |
---|
| 855 | 0.004970 0.0003990 2496.38678318 |
---|
| 856 | 0.005253 0.0004112 2491.16237596 |
---|
| 857 | 0.005537 0.0004237 2485.63911673 |
---|
| 858 | 0.005821 0.0004365 2479.83657083 |
---|
| 859 | 0.006105 0.0004495 2473.75676948 |
---|
| 860 | 0.006389 0.0004628 2467.40145990 |
---|
| 861 | 0.006673 0.0004762 2460.77293372 |
---|
| 862 | 0.006957 0.0004899 2453.86724627 |
---|
| 863 | 0.007241 0.0005037 2446.69623838 |
---|
| 864 | 0.007525 0.0005177 2439.25775219 |
---|
| 865 | 0.007809 0.0005318 2431.55421398 |
---|
| 866 | 0.008093 0.0005461 2423.58785521 |
---|
| 867 | 0.008377 0.0005605 2415.36158137 |
---|
| 868 | 0.008661 0.0005750 2406.87009473 |
---|
| 869 | 0.008945 0.0005896 2398.12841186 |
---|
| 870 | 0.009229 0.0006044 2389.13360806 |
---|
| 871 | 0.009513 0.0006192 2379.88958042 |
---|
| 872 | 0.009797 0.0006341 2370.39776774 |
---|
| 873 | 0.010080 0.0006491 2360.69528793 |
---|
| 874 | 0.010360 0.0006641 2350.85169027 |
---|
| 875 | 0.010650 0.0006793 2340.42023633 |
---|
| 876 | 0.010930 0.0006945 2330.11206013 |
---|
| 877 | 0.011220 0.0007097 2319.20109972 |
---|
| 878 | 0.011500 0.0007251 2308.43503981 |
---|
| 879 | 0.011780 0.0007404 2297.44820179 |
---|
| 880 | 0.012070 0.0007558 2285.83853677 |
---|
| 881 | 0.012350 0.0007713 2274.41290746 |
---|
| 882 | 0.012640 0.0007868 2262.36219581 |
---|
| 883 | 0.012920 0.0008024 2250.51169731 |
---|
| 884 | 0.013200 0.0008180 2238.45596231 |
---|
| 885 | 0.013490 0.0008336 2225.76495666 |
---|
| 886 | 0.013770 0.0008493 2213.29618391 |
---|
| 887 | 0.014060 0.0008650 2200.19110751 |
---|
| 888 | 0.014340 0.0008807 2187.34050325 |
---|
| 889 | 0.014620 0.0008965 2174.30529864 |
---|
| 890 | 0.014910 0.0009123 2160.61632548 |
---|
| 891 | 0.015190 0.0009281 2147.21038112 |
---|
| 892 | 0.015470 0.0009440 2133.62023580 |
---|
| 893 | 0.015760 0.0009598 2119.37907426 |
---|
| 894 | 0.016040 0.0009757 2105.45234903 |
---|
| 895 | 0.016330 0.0009916 2090.86319102 |
---|
| 896 | 0.016610 0.0010080 2076.60576032 |
---|
| 897 | 0.016890 0.0010240 2062.19214565 |
---|
| 898 | 0.017180 0.0010390 2047.10550219 |
---|
| 899 | 0.017460 0.0010550 2032.38715621 |
---|
| 900 | 0.017740 0.0010710 2017.52560123 |
---|
| 901 | 0.018030 0.0010880 2001.99124318 |
---|
| 902 | 0.018310 0.0011040 1986.84662060 |
---|
| 903 | 0.018600 0.0011200 1971.03389745 |
---|
| 904 | 0.018880 0.0011360 1955.61395119 |
---|
| 905 | 0.019160 0.0011520 1940.08291563 |
---|
| 906 | 0.019450 0.0011680 1923.87672225 |
---|
| 907 | 0.019730 0.0011840 1908.10656374 |
---|
| 908 | 0.020020 0.0012000 1891.66297192 |
---|
| 909 | 0.020300 0.0012160 1875.66789021 |
---|
| 910 | 0.020580 0.0012320 1859.56357196 |
---|
| 911 | 0.020870 0.0012490 1842.79468290 |
---|
| 912 | 0.021150 0.0012650 1826.50064489 |
---|
| 913 | 0.021430 0.0012810 1810.11533702 |
---|
| 914 | 0.021720 0.0012970 1793.06840882 |
---|
| 915 | 0.022000 0.0013130 1776.51153580 |
---|
| 916 | 0.022280 0.0013290 1759.87201249 |
---|
| 917 | 0.022570 0.0013460 1742.57354412 |
---|
| 918 | 0.022850 0.0013620 1725.79397319 |
---|
| 919 | 0.023140 0.0013780 1708.35831550 |
---|
| 920 | 0.023420 0.0013940 1691.45256069 |
---|
| 921 | 0.023700 0.0014110 1674.48561783 |
---|
| 922 | 0.023990 0.0014270 1656.86525366 |
---|
| 923 | 0.024270 0.0014430 1639.79847285 |
---|
| 924 | 0.024550 0.0014590 1622.68887088 |
---|
| 925 | 0.024840 0.0014760 1604.96421100 |
---|
| 926 | 0.025120 0.0014920 1587.85768129 |
---|
| 927 | 0.025410 0.0015080 1569.99297335 |
---|
| 928 | 0.025690 0.0015240 1552.84580279 |
---|
| 929 | 0.025970 0.0015410 1535.54074115 |
---|
| 930 | 0.026260 0.0015570 1517.75249337 |
---|
| 931 | 0.026540 0.0015730 1500.40115023 |
---|
| 932 | 0.026820 0.0015900 1483.03632237 |
---|
| 933 | 0.027110 0.0016060 1465.05942429 |
---|
| 934 | 0.027390 0.0016220 1447.67682181 |
---|
| 935 | 0.027670 0.0016390 1430.46495191 |
---|
| 936 | 0.027960 0.0016550 1412.49232282 |
---|
| 937 | 0.028240 0.0016710 1395.13182318 |
---|
| 938 | 0.028520 0.0016880 1377.93439837 |
---|
| 939 | 0.028810 0.0017040 1359.99528971 |
---|
| 940 | 0.029090 0.0017200 1342.67274512 |
---|
| 941 | 0.029370 0.0017370 1325.55375609 |
---|
| 942 | """ |
---|
| 943 | |
---|
[7954f5c] | 944 | # Slit sphere parameters |
---|
| 945 | TEST_PARS_SLIT_SPHERE = { |
---|
| 946 | 'scale': 0.01, 'background': 0.01, |
---|
[486fcf6] | 947 | 'radius': 60000, 'sld': 1, 'sld_solvent': 4, |
---|
[7954f5c] | 948 | } |
---|
| 949 | # Q dQ I(Q) I_smeared(Q) |
---|
| 950 | TEST_DATA_SLIT_SPHERE = """\ |
---|
| 951 | 2.26097e-05 0.117 5.5781372896e+09 1.4626077708e+06 |
---|
| 952 | 2.53847e-05 0.117 5.0363141458e+09 1.3117318023e+06 |
---|
| 953 | 2.81597e-05 0.117 4.4850108103e+09 1.1594863713e+06 |
---|
| 954 | 3.09347e-05 0.117 3.9364658459e+09 1.0094881630e+06 |
---|
| 955 | 3.37097e-05 0.117 3.4019975074e+09 8.6518941303e+05 |
---|
| 956 | 3.92597e-05 0.117 2.4139519814e+09 6.0232158311e+05 |
---|
| 957 | 4.48097e-05 0.117 1.5816877820e+09 3.8739994090e+05 |
---|
| 958 | 5.03597e-05 0.117 9.3715407224e+08 2.2745304775e+05 |
---|
| 959 | 5.59097e-05 0.117 4.8387917428e+08 1.2101295768e+05 |
---|
| 960 | 6.14597e-05 0.117 2.0193586928e+08 6.0055107771e+04 |
---|
| 961 | 6.70097e-05 0.117 5.5886110911e+07 3.2749521065e+04 |
---|
| 962 | 7.25597e-05 0.117 3.7782348010e+06 2.6350963616e+04 |
---|
| 963 | 7.81097e-05 0.117 5.3407817904e+06 2.9624963314e+04 |
---|
| 964 | 8.36597e-05 0.117 2.7975485523e+07 3.4403962254e+04 |
---|
| 965 | 8.92097e-05 0.117 4.9845448282e+07 3.6130017903e+04 |
---|
| 966 | 9.47597e-05 0.117 6.0092588905e+07 3.3495107849e+04 |
---|
| 967 | 1.00310e-04 0.117 5.6823430831e+07 2.7475726279e+04 |
---|
| 968 | 1.05860e-04 0.117 4.3857024036e+07 2.0144282226e+04 |
---|
| 969 | 1.11410e-04 0.117 2.7277144760e+07 1.3647403260e+04 |
---|
| 970 | 1.22510e-04 0.117 3.3119334113e+06 6.6519711526e+03 |
---|
| 971 | 1.33610e-04 0.117 1.4412859402e+06 6.9726212813e+03 |
---|
| 972 | 1.44710e-04 0.117 8.5620162463e+06 8.1441335775e+03 |
---|
| 973 | 1.55810e-04 0.117 9.6957429033e+06 6.4559996521e+03 |
---|
| 974 | 1.66910e-04 0.117 4.3818341914e+06 3.6252154156e+03 |
---|
| 975 | 1.78010e-04 0.117 2.7448997387e+05 2.4006505342e+03 |
---|
| 976 | 1.89110e-04 0.117 8.0472009936e+05 2.8187789089e+03 |
---|
| 977 | 2.00210e-04 0.117 2.8149552834e+06 3.0915662855e+03 |
---|
| 978 | 2.11310e-04 0.117 2.7510907861e+06 2.3722530293e+03 |
---|
| 979 | 2.22410e-04 0.117 1.0053133293e+06 1.4473468311e+03 |
---|
| 980 | 2.33510e-04 0.117 5.8428305052e+03 1.2048540556e+03 |
---|
| 981 | 2.44610e-04 0.117 5.1699305004e+05 1.4625670042e+03 |
---|
| 982 | 2.55710e-04 0.117 1.2120227268e+06 1.5010705973e+03 |
---|
| 983 | 2.66810e-04 0.117 9.7896842846e+05 1.1336343426e+03 |
---|
| 984 | 2.77910e-04 0.117 2.5507264791e+05 8.1848818080e+02 |
---|
| 985 | 3.05660e-04 0.117 5.2403101181e+05 7.4913374357e+02 |
---|
| 986 | 3.33410e-04 0.117 5.8699343809e+04 4.4669964560e+02 |
---|
| 987 | 3.61160e-04 0.117 3.0844327150e+05 4.6774007542e+02 |
---|
| 988 | 3.88910e-04 0.117 8.3360142970e+03 2.7169550220e+02 |
---|
| 989 | 4.16660e-04 0.117 1.8630080583e+05 3.0710983679e+02 |
---|
| 990 | 4.44410e-04 0.117 3.1616804732e-01 1.7959006831e+02 |
---|
| 991 | 4.72160e-04 0.117 1.1299016314e+05 2.0763952339e+02 |
---|
| 992 | 4.99910e-04 0.117 2.9952522747e+03 1.2536542765e+02 |
---|
| 993 | 5.27660e-04 0.117 6.7625695649e+04 1.4013969777e+02 |
---|
| 994 | 5.55410e-04 0.117 7.6927460089e+03 8.2145593180e+01 |
---|
| 995 | 6.10910e-04 0.117 1.1229057779e+04 8.4519745643e+01 |
---|
| 996 | 6.66410e-04 0.117 1.3035567943e+04 8.1554625609e+01 |
---|
| 997 | 7.21910e-04 0.117 1.3309931343e+04 7.4437319172e+01 |
---|
| 998 | 7.77410e-04 0.117 1.2462626212e+04 6.4697088261e+01 |
---|
| 999 | 8.32910e-04 0.117 1.0912927143e+04 5.3773301044e+01 |
---|
| 1000 | 8.88410e-04 0.117 9.0172597469e+03 4.2843375753e+01 |
---|
| 1001 | 9.43910e-04 0.117 7.0496495917e+03 3.2771032724e+01 |
---|
| 1002 | 9.99410e-04 0.117 5.2030483682e+03 2.4113557144e+01 |
---|
| 1003 | 1.05491e-03 0.117 3.5988976711e+03 1.7160773658e+01 |
---|
| 1004 | 1.11041e-03 0.117 2.2996060652e+03 1.2016626459e+01 |
---|
| 1005 | 1.22141e-03 0.117 6.4766590598e+02 6.0373017740e+00 |
---|
| 1006 | 1.33241e-03 0.117 4.1963483264e+01 4.5215452974e+00 |
---|
| 1007 | 1.44341e-03 0.117 6.3370708246e+01 5.1054681903e+00 |
---|
| 1008 | 1.55441e-03 0.117 3.0736750577e+02 5.9176165298e+00 |
---|
| 1009 | 1.66541e-03 0.117 5.0327682399e+02 5.9815000189e+00 |
---|
| 1010 | 1.77641e-03 0.117 5.4084331454e+02 5.1634639625e+00 |
---|
| 1011 | 1.88741e-03 0.117 4.3488671756e+02 3.8535158148e+00 |
---|
| 1012 | 1.99841e-03 0.117 2.6322287860e+02 2.5824997753e+00 |
---|
| 1013 | 2.10941e-03 0.117 1.0793633150e+02 1.7315517194e+00 |
---|
| 1014 | 2.22041e-03 0.117 1.8474448850e+01 1.4077213604e+00 |
---|
| 1015 | 2.33141e-03 0.117 1.5864062279e+00 1.4771560682e+00 |
---|
| 1016 | 2.44241e-03 0.117 3.2267213848e+01 1.6916253448e+00 |
---|
| 1017 | 2.55341e-03 0.117 7.4289116207e+01 1.8274751193e+00 |
---|
| 1018 | 2.66441e-03 0.117 9.9000521929e+01 1.7706812289e+00 |
---|
| 1019 | """ |
---|
[49d1d42f] | 1020 | |
---|
[db8756e] | 1021 | def main(): |
---|
| 1022 | """ |
---|
| 1023 | Run tests given is sys.argv. |
---|
| 1024 | |
---|
| 1025 | Returns 0 if success or 1 if any tests fail. |
---|
| 1026 | """ |
---|
| 1027 | import sys |
---|
[7ae2b7f] | 1028 | import xmlrunner # type: ignore |
---|
[db8756e] | 1029 | |
---|
| 1030 | suite = unittest.TestSuite() |
---|
| 1031 | suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])) |
---|
| 1032 | |
---|
| 1033 | runner = xmlrunner.XMLTestRunner(output='logs') |
---|
| 1034 | result = runner.run(suite) |
---|
| 1035 | return 1 if result.failures or result.errors else 0 |
---|
| 1036 | |
---|
| 1037 | |
---|
[49d1d42f] | 1038 | ############################################################################ |
---|
| 1039 | # usage demo |
---|
| 1040 | ############################################################################ |
---|
| 1041 | |
---|
| 1042 | def _eval_demo_1d(resolution, title): |
---|
[dbde9f8] | 1043 | import sys |
---|
[49d1d42f] | 1044 | from sasmodels import core |
---|
[6d6508e] | 1045 | from sasmodels import direct_model |
---|
[dbde9f8] | 1046 | name = sys.argv[1] if len(sys.argv) > 1 else 'cylinder' |
---|
| 1047 | |
---|
| 1048 | if name == 'cylinder': |
---|
[486fcf6] | 1049 | pars = {'length':210, 'radius':500, 'background': 0} |
---|
[dbde9f8] | 1050 | elif name == 'teubner_strey': |
---|
| 1051 | pars = {'a2':0.003, 'c1':-1e4, 'c2':1e10, 'background':0.312643} |
---|
| 1052 | elif name == 'sphere' or name == 'spherepy': |
---|
| 1053 | pars = TEST_PARS_SLIT_SPHERE |
---|
| 1054 | elif name == 'ellipsoid': |
---|
| 1055 | pars = { |
---|
[486fcf6] | 1056 | 'scale':0.05, 'background': 0, |
---|
| 1057 | 'r_polar':500, 'r_equatorial':15000, |
---|
| 1058 | 'sld':6, 'sld_solvent': 1, |
---|
[dbde9f8] | 1059 | } |
---|
| 1060 | else: |
---|
| 1061 | pars = {} |
---|
[17bbadd] | 1062 | model_info = core.load_model_info(name) |
---|
| 1063 | model = core.build_model(model_info) |
---|
[49d1d42f] | 1064 | |
---|
[48fbd50] | 1065 | kernel = model.make_kernel([resolution.q_calc]) |
---|
[6d6508e] | 1066 | theory = direct_model.call_kernel(kernel, pars) |
---|
[33c8d73] | 1067 | Iq = resolution.apply(theory) |
---|
[49d1d42f] | 1068 | |
---|
[dbde9f8] | 1069 | if isinstance(resolution, Slit1D): |
---|
[ea75043] | 1070 | width, height = resolution.dqx, resolution.dqy |
---|
[dbde9f8] | 1071 | Iq_romb = romberg_slit_1d(resolution.q, width, height, model, pars) |
---|
| 1072 | else: |
---|
| 1073 | dq = resolution.q_width |
---|
| 1074 | Iq_romb = romberg_pinhole_1d(resolution.q, dq, model, pars) |
---|
| 1075 | |
---|
[7ae2b7f] | 1076 | import matplotlib.pyplot as plt # type: ignore |
---|
[33c8d73] | 1077 | plt.loglog(resolution.q_calc, theory, label='unsmeared') |
---|
[49d1d42f] | 1078 | plt.loglog(resolution.q, Iq, label='smeared', hold=True) |
---|
[dbde9f8] | 1079 | plt.loglog(resolution.q, Iq_romb, label='romberg smeared', hold=True) |
---|
[49d1d42f] | 1080 | plt.legend() |
---|
| 1081 | plt.title(title) |
---|
| 1082 | plt.xlabel("Q (1/Ang)") |
---|
| 1083 | plt.ylabel("I(Q) (1/cm)") |
---|
| 1084 | |
---|
| 1085 | def demo_pinhole_1d(): |
---|
[5925e90] | 1086 | """ |
---|
| 1087 | Show example of pinhole smearing. |
---|
| 1088 | """ |
---|
[dbde9f8] | 1089 | q = np.logspace(-4, np.log10(0.2), 400) |
---|
[33c8d73] | 1090 | q_width = 0.1*q |
---|
| 1091 | resolution = Pinhole1D(q, q_width) |
---|
[49d1d42f] | 1092 | _eval_demo_1d(resolution, title="10% dQ/Q Pinhole Resolution") |
---|
| 1093 | |
---|
| 1094 | def demo_slit_1d(): |
---|
[5925e90] | 1095 | """ |
---|
| 1096 | Show example of slit smearing. |
---|
| 1097 | """ |
---|
[eb588ef] | 1098 | q = np.logspace(-4, np.log10(0.2), 100) |
---|
[dbde9f8] | 1099 | w = h = 0. |
---|
[eb588ef] | 1100 | #w = 0.000000277790 |
---|
[dbde9f8] | 1101 | w = 0.0277790 |
---|
| 1102 | #h = 0.00277790 |
---|
[eb588ef] | 1103 | #h = 0.0277790 |
---|
[dbde9f8] | 1104 | resolution = Slit1D(q, w, h) |
---|
[fdc538a] | 1105 | _eval_demo_1d(resolution, title="(%g,%g) Slit Resolution"%(w, h)) |
---|
[49d1d42f] | 1106 | |
---|
| 1107 | def demo(): |
---|
[5925e90] | 1108 | """ |
---|
| 1109 | Run the resolution demos. |
---|
| 1110 | """ |
---|
[7ae2b7f] | 1111 | import matplotlib.pyplot as plt # type: ignore |
---|
[49d1d42f] | 1112 | plt.subplot(121) |
---|
| 1113 | demo_pinhole_1d() |
---|
[dbde9f8] | 1114 | #plt.yscale('linear') |
---|
[49d1d42f] | 1115 | plt.subplot(122) |
---|
| 1116 | demo_slit_1d() |
---|
[dbde9f8] | 1117 | #plt.yscale('linear') |
---|
[49d1d42f] | 1118 | plt.show() |
---|
| 1119 | |
---|
| 1120 | |
---|
| 1121 | if __name__ == "__main__": |
---|
[7f7f99f] | 1122 | #demo() |
---|
[fdc538a] | 1123 | main() |
---|