[3b4243d] | 1 | """ |
---|
| 2 | SAS data representations. |
---|
| 3 | |
---|
| 4 | Plotting functions for data sets: |
---|
| 5 | |
---|
| 6 | :func:`plot_data` plots the data file. |
---|
| 7 | |
---|
| 8 | :func:`plot_theory` plots a calculated result from the model. |
---|
| 9 | |
---|
| 10 | Wrappers for the sasview data loader and data manipulations: |
---|
| 11 | |
---|
| 12 | :func:`load_data` loads a sasview data file. |
---|
| 13 | |
---|
| 14 | :func:`set_beam_stop` masks the beam stop from the data. |
---|
| 15 | |
---|
| 16 | :func:`set_half` selects the right or left half of the data, which can |
---|
| 17 | be useful for shear measurements which have not been properly corrected |
---|
| 18 | for path length and reflections. |
---|
| 19 | |
---|
| 20 | :func:`set_top` cuts the top part off the data. |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | Empty data sets for evaluating models without data: |
---|
| 24 | |
---|
| 25 | :func:`empty_data1D` creates an empty dataset, which is useful for plotting |
---|
| 26 | a theory function before the data is measured. |
---|
| 27 | |
---|
| 28 | :func:`empty_data2D` creates an empty 2D dataset. |
---|
| 29 | |
---|
| 30 | Note that the empty datasets use a minimal representation of the SasView |
---|
| 31 | objects so that models can be run without SasView on the path. You could |
---|
| 32 | also use these for your own data loader. |
---|
| 33 | |
---|
| 34 | """ |
---|
| 35 | import traceback |
---|
| 36 | |
---|
[7ae2b7f] | 37 | import numpy as np # type: ignore |
---|
[3b4243d] | 38 | |
---|
[a5b8477] | 39 | try: |
---|
| 40 | from typing import Union, Dict, List, Optional |
---|
| 41 | except ImportError: |
---|
| 42 | pass |
---|
| 43 | else: |
---|
| 44 | Data = Union["Data1D", "Data2D", "SesansData"] |
---|
| 45 | |
---|
[3b4243d] | 46 | def load_data(filename): |
---|
[a5b8477] | 47 | # type: (str) -> Data |
---|
[3b4243d] | 48 | """ |
---|
| 49 | Load data using a sasview loader. |
---|
| 50 | """ |
---|
[7ae2b7f] | 51 | from sas.sascalc.dataloader.loader import Loader # type: ignore |
---|
[3b4243d] | 52 | loader = Loader() |
---|
| 53 | data = loader.load(filename) |
---|
| 54 | if data is None: |
---|
| 55 | raise IOError("Data %r could not be loaded" % filename) |
---|
| 56 | return data |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | def set_beam_stop(data, radius, outer=None): |
---|
[a5b8477] | 60 | # type: (Data, float, Optional[float]) -> None |
---|
[3b4243d] | 61 | """ |
---|
| 62 | Add a beam stop of the given *radius*. If *outer*, make an annulus. |
---|
| 63 | """ |
---|
[4e00c13] | 64 | from sas.sascalc.dataloader.manipulations import Ringcut |
---|
[3b4243d] | 65 | if hasattr(data, 'qx_data'): |
---|
| 66 | data.mask = Ringcut(0, radius)(data) |
---|
| 67 | if outer is not None: |
---|
| 68 | data.mask += Ringcut(outer, np.inf)(data) |
---|
| 69 | else: |
---|
| 70 | data.mask = (data.x < radius) |
---|
| 71 | if outer is not None: |
---|
| 72 | data.mask |= (data.x >= outer) |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | def set_half(data, half): |
---|
[a5b8477] | 76 | # type: (Data, str) -> None |
---|
[3b4243d] | 77 | """ |
---|
| 78 | Select half of the data, either "right" or "left". |
---|
| 79 | """ |
---|
[4e00c13] | 80 | from sas.sascalc.dataloader.manipulations import Boxcut |
---|
[3b4243d] | 81 | if half == 'right': |
---|
| 82 | data.mask += \ |
---|
| 83 | Boxcut(x_min=-np.inf, x_max=0.0, y_min=-np.inf, y_max=np.inf)(data) |
---|
| 84 | if half == 'left': |
---|
| 85 | data.mask += \ |
---|
| 86 | Boxcut(x_min=0.0, x_max=np.inf, y_min=-np.inf, y_max=np.inf)(data) |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | def set_top(data, cutoff): |
---|
[a5b8477] | 90 | # type: (Data, float) -> None |
---|
[3b4243d] | 91 | """ |
---|
| 92 | Chop the top off the data, above *cutoff*. |
---|
| 93 | """ |
---|
[4e00c13] | 94 | from sas.sascalc.dataloader.manipulations import Boxcut |
---|
[3b4243d] | 95 | data.mask += \ |
---|
| 96 | Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=cutoff)(data) |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | class Data1D(object): |
---|
[299edd2] | 100 | """ |
---|
| 101 | 1D data object. |
---|
| 102 | |
---|
| 103 | Note that this definition matches the attributes from sasview, with |
---|
| 104 | some generic 1D data vectors and some SAS specific definitions. Some |
---|
| 105 | refactoring to allow consistent naming conventions between 1D, 2D and |
---|
| 106 | SESANS data would be helpful. |
---|
| 107 | |
---|
| 108 | **Attributes** |
---|
| 109 | |
---|
| 110 | *x*, *dx*: $q$ vector and gaussian resolution |
---|
| 111 | |
---|
| 112 | *y*, *dy*: $I(q)$ vector and measurement uncertainty |
---|
| 113 | |
---|
| 114 | *mask*: values to include in plotting/analysis |
---|
| 115 | |
---|
| 116 | *dxl*: slit widths for slit smeared data, with *dx* ignored |
---|
| 117 | |
---|
| 118 | *qmin*, *qmax*: range of $q$ values in *x* |
---|
| 119 | |
---|
| 120 | *filename*: label for the data line |
---|
| 121 | |
---|
| 122 | *_xaxis*, *_xunit*: label and units for the *x* axis |
---|
| 123 | |
---|
| 124 | *_yaxis*, *_yunit*: label and units for the *y* axis |
---|
| 125 | """ |
---|
[3b4243d] | 126 | def __init__(self, x=None, y=None, dx=None, dy=None): |
---|
[a5b8477] | 127 | # type: (Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray]) -> None |
---|
[3b4243d] | 128 | self.x, self.y, self.dx, self.dy = x, y, dx, dy |
---|
| 129 | self.dxl = None |
---|
[69ec80f] | 130 | self.filename = None |
---|
| 131 | self.qmin = x.min() if x is not None else np.NaN |
---|
| 132 | self.qmax = x.max() if x is not None else np.NaN |
---|
[2c1bb7b0] | 133 | # TODO: why is 1D mask False and 2D mask True? |
---|
| 134 | self.mask = (np.isnan(y) if y is not None |
---|
[eafc9fa] | 135 | else np.zeros_like(x, 'b') if x is not None |
---|
[2c1bb7b0] | 136 | else None) |
---|
[69ec80f] | 137 | self._xaxis, self._xunit = "x", "" |
---|
| 138 | self._yaxis, self._yunit = "y", "" |
---|
[3b4243d] | 139 | |
---|
| 140 | def xaxis(self, label, unit): |
---|
[a5b8477] | 141 | # type: (str, str) -> None |
---|
[3b4243d] | 142 | """ |
---|
| 143 | set the x axis label and unit |
---|
| 144 | """ |
---|
| 145 | self._xaxis = label |
---|
| 146 | self._xunit = unit |
---|
| 147 | |
---|
| 148 | def yaxis(self, label, unit): |
---|
[a5b8477] | 149 | # type: (str, str) -> None |
---|
[3b4243d] | 150 | """ |
---|
| 151 | set the y axis label and unit |
---|
| 152 | """ |
---|
| 153 | self._yaxis = label |
---|
| 154 | self._yunit = unit |
---|
| 155 | |
---|
[a5b8477] | 156 | class SesansData(Data1D): |
---|
[40a87fa] | 157 | """ |
---|
| 158 | SESANS data object. |
---|
| 159 | |
---|
| 160 | This is just :class:`Data1D` with a wavelength parameter. |
---|
| 161 | |
---|
| 162 | *x* is spin echo length and *y* is polarization (P/P0). |
---|
| 163 | """ |
---|
[a5b8477] | 164 | def __init__(self, **kw): |
---|
| 165 | Data1D.__init__(self, **kw) |
---|
| 166 | self.lam = None # type: Optional[np.ndarray] |
---|
[3b4243d] | 167 | |
---|
| 168 | class Data2D(object): |
---|
[299edd2] | 169 | """ |
---|
| 170 | 2D data object. |
---|
| 171 | |
---|
| 172 | Note that this definition matches the attributes from sasview. Some |
---|
| 173 | refactoring to allow consistent naming conventions between 1D, 2D and |
---|
| 174 | SESANS data would be helpful. |
---|
| 175 | |
---|
| 176 | **Attributes** |
---|
| 177 | |
---|
| 178 | *qx_data*, *dqx_data*: $q_x$ matrix and gaussian resolution |
---|
| 179 | |
---|
| 180 | *qy_data*, *dqy_data*: $q_y$ matrix and gaussian resolution |
---|
| 181 | |
---|
| 182 | *data*, *err_data*: $I(q)$ matrix and measurement uncertainty |
---|
| 183 | |
---|
| 184 | *mask*: values to exclude from plotting/analysis |
---|
| 185 | |
---|
| 186 | *qmin*, *qmax*: range of $q$ values in *x* |
---|
| 187 | |
---|
| 188 | *filename*: label for the data line |
---|
| 189 | |
---|
| 190 | *_xaxis*, *_xunit*: label and units for the *x* axis |
---|
| 191 | |
---|
| 192 | *_yaxis*, *_yunit*: label and units for the *y* axis |
---|
| 193 | |
---|
| 194 | *_zaxis*, *_zunit*: label and units for the *y* axis |
---|
| 195 | |
---|
| 196 | *Q_unit*, *I_unit*: units for Q and intensity |
---|
| 197 | |
---|
| 198 | *x_bins*, *y_bins*: grid steps in *x* and *y* directions |
---|
| 199 | """ |
---|
[69ec80f] | 200 | def __init__(self, x=None, y=None, z=None, dx=None, dy=None, dz=None): |
---|
[a5b8477] | 201 | # type: (Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray]) -> None |
---|
[69ec80f] | 202 | self.qx_data, self.dqx_data = x, dx |
---|
| 203 | self.qy_data, self.dqy_data = y, dy |
---|
| 204 | self.data, self.err_data = z, dz |
---|
[c094758] | 205 | self.mask = (np.isnan(z) if z is not None |
---|
| 206 | else np.zeros_like(x, dtype='bool') if x is not None |
---|
[2c1bb7b0] | 207 | else None) |
---|
[69ec80f] | 208 | self.q_data = np.sqrt(x**2 + y**2) |
---|
| 209 | self.qmin = 1e-16 |
---|
| 210 | self.qmax = np.inf |
---|
[3b4243d] | 211 | self.detector = [] |
---|
| 212 | self.source = Source() |
---|
[69ec80f] | 213 | self.Q_unit = "1/A" |
---|
| 214 | self.I_unit = "1/cm" |
---|
[299edd2] | 215 | self.xaxis("Q_x", "1/A") |
---|
| 216 | self.yaxis("Q_y", "1/A") |
---|
| 217 | self.zaxis("Intensity", "1/cm") |
---|
[69ec80f] | 218 | self._xaxis, self._xunit = "x", "" |
---|
| 219 | self._yaxis, self._yunit = "y", "" |
---|
| 220 | self._zaxis, self._zunit = "z", "" |
---|
| 221 | self.x_bins, self.y_bins = None, None |
---|
[40a87fa] | 222 | self.filename = None |
---|
[3b4243d] | 223 | |
---|
| 224 | def xaxis(self, label, unit): |
---|
[a5b8477] | 225 | # type: (str, str) -> None |
---|
[3b4243d] | 226 | """ |
---|
| 227 | set the x axis label and unit |
---|
| 228 | """ |
---|
| 229 | self._xaxis = label |
---|
| 230 | self._xunit = unit |
---|
| 231 | |
---|
| 232 | def yaxis(self, label, unit): |
---|
[a5b8477] | 233 | # type: (str, str) -> None |
---|
[3b4243d] | 234 | """ |
---|
| 235 | set the y axis label and unit |
---|
| 236 | """ |
---|
| 237 | self._yaxis = label |
---|
| 238 | self._yunit = unit |
---|
| 239 | |
---|
| 240 | def zaxis(self, label, unit): |
---|
[a5b8477] | 241 | # type: (str, str) -> None |
---|
[3b4243d] | 242 | """ |
---|
| 243 | set the y axis label and unit |
---|
| 244 | """ |
---|
| 245 | self._zaxis = label |
---|
| 246 | self._zunit = unit |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | class Vector(object): |
---|
[299edd2] | 250 | """ |
---|
| 251 | 3-space vector of *x*, *y*, *z* |
---|
| 252 | """ |
---|
[3b4243d] | 253 | def __init__(self, x=None, y=None, z=None): |
---|
[a5b8477] | 254 | # type: (float, float, Optional[float]) -> None |
---|
[3b4243d] | 255 | self.x, self.y, self.z = x, y, z |
---|
| 256 | |
---|
| 257 | class Detector(object): |
---|
[69ec80f] | 258 | """ |
---|
| 259 | Detector attributes. |
---|
| 260 | """ |
---|
| 261 | def __init__(self, pixel_size=(None, None), distance=None): |
---|
[a5b8477] | 262 | # type: (Tuple[float, float], float) -> None |
---|
[69ec80f] | 263 | self.pixel_size = Vector(*pixel_size) |
---|
| 264 | self.distance = distance |
---|
[3b4243d] | 265 | |
---|
| 266 | class Source(object): |
---|
[69ec80f] | 267 | """ |
---|
| 268 | Beam attributes. |
---|
| 269 | """ |
---|
| 270 | def __init__(self): |
---|
[a5b8477] | 271 | # type: () -> None |
---|
[69ec80f] | 272 | self.wavelength = np.NaN |
---|
| 273 | self.wavelength_unit = "A" |
---|
[3b4243d] | 274 | |
---|
| 275 | |
---|
[d18582e] | 276 | def empty_data1D(q, resolution=0.0): |
---|
[a5b8477] | 277 | # type: (np.ndarray, float) -> Data1D |
---|
[3b4243d] | 278 | """ |
---|
| 279 | Create empty 1D data using the given *q* as the x value. |
---|
| 280 | |
---|
| 281 | *resolution* dq/q defaults to 5%. |
---|
| 282 | """ |
---|
| 283 | |
---|
| 284 | #Iq = 100 * np.ones_like(q) |
---|
| 285 | #dIq = np.sqrt(Iq) |
---|
| 286 | Iq, dIq = None, None |
---|
[d18582e] | 287 | q = np.asarray(q) |
---|
[3b4243d] | 288 | data = Data1D(q, Iq, dx=resolution * q, dy=dIq) |
---|
| 289 | data.filename = "fake data" |
---|
| 290 | return data |
---|
| 291 | |
---|
| 292 | |
---|
[d18582e] | 293 | def empty_data2D(qx, qy=None, resolution=0.0): |
---|
[a5b8477] | 294 | # type: (np.ndarray, Optional[np.ndarray], float) -> Data2D |
---|
[3b4243d] | 295 | """ |
---|
| 296 | Create empty 2D data using the given mesh. |
---|
| 297 | |
---|
| 298 | If *qy* is missing, create a square mesh with *qy=qx*. |
---|
| 299 | |
---|
| 300 | *resolution* dq/q defaults to 5%. |
---|
| 301 | """ |
---|
| 302 | if qy is None: |
---|
| 303 | qy = qx |
---|
[d18582e] | 304 | qx, qy = np.asarray(qx), np.asarray(qy) |
---|
[69ec80f] | 305 | # 5% dQ/Q resolution |
---|
[3b4243d] | 306 | Qx, Qy = np.meshgrid(qx, qy) |
---|
| 307 | Qx, Qy = Qx.flatten(), Qy.flatten() |
---|
[a5b8477] | 308 | Iq = 100 * np.ones_like(Qx) # type: np.ndarray |
---|
[3b4243d] | 309 | dIq = np.sqrt(Iq) |
---|
| 310 | if resolution != 0: |
---|
| 311 | # https://www.ncnr.nist.gov/staff/hammouda/distance_learning/chapter_15.pdf |
---|
| 312 | # Should have an additional constant which depends on distances and |
---|
| 313 | # radii of the aperture, pixel dimensions and wavelength spread |
---|
| 314 | # Instead, assume radial dQ/Q is constant, and perpendicular matches |
---|
| 315 | # radial (which instead it should be inverse). |
---|
| 316 | Q = np.sqrt(Qx**2 + Qy**2) |
---|
[69ec80f] | 317 | dqx = resolution * Q |
---|
| 318 | dqy = resolution * Q |
---|
[ac21c7f] | 319 | else: |
---|
[69ec80f] | 320 | dqx = dqy = None |
---|
[3b4243d] | 321 | |
---|
[69ec80f] | 322 | data = Data2D(x=Qx, y=Qy, z=Iq, dx=dqx, dy=dqy, dz=dIq) |
---|
[ce166d3] | 323 | data.x_bins = qx |
---|
| 324 | data.y_bins = qy |
---|
[69ec80f] | 325 | data.filename = "fake data" |
---|
| 326 | |
---|
| 327 | # pixel_size in mm, distance in m |
---|
| 328 | detector = Detector(pixel_size=(5, 5), distance=4) |
---|
| 329 | data.detector.append(detector) |
---|
[3b4243d] | 330 | data.source.wavelength = 5 # angstroms |
---|
| 331 | data.source.wavelength_unit = "A" |
---|
| 332 | return data |
---|
| 333 | |
---|
| 334 | |
---|
[013adb7] | 335 | def plot_data(data, view='log', limits=None): |
---|
[a5b8477] | 336 | # type: (Data, str, Optional[Tuple[float, float]]) -> None |
---|
[3b4243d] | 337 | """ |
---|
| 338 | Plot data loaded by the sasview loader. |
---|
[299edd2] | 339 | |
---|
| 340 | *data* is a sasview data object, either 1D, 2D or SESANS. |
---|
| 341 | |
---|
| 342 | *view* is log or linear. |
---|
| 343 | |
---|
| 344 | *limits* sets the intensity limits on the plot; if None then the limits |
---|
| 345 | are inferred from the data. |
---|
[3b4243d] | 346 | """ |
---|
| 347 | # Note: kind of weird using the plot result functions to plot just the |
---|
| 348 | # data, but they already handle the masking and graph markup already, so |
---|
| 349 | # do not repeat. |
---|
| 350 | if hasattr(data, 'lam'): |
---|
[69ec80f] | 351 | _plot_result_sesans(data, None, None, use_data=True, limits=limits) |
---|
[3b4243d] | 352 | elif hasattr(data, 'qx_data'): |
---|
[69ec80f] | 353 | _plot_result2D(data, None, None, view, use_data=True, limits=limits) |
---|
[3b4243d] | 354 | else: |
---|
[69ec80f] | 355 | _plot_result1D(data, None, None, view, use_data=True, limits=limits) |
---|
[3b4243d] | 356 | |
---|
| 357 | |
---|
[013adb7] | 358 | def plot_theory(data, theory, resid=None, view='log', |
---|
[ea75043] | 359 | use_data=True, limits=None, Iq_calc=None): |
---|
[a5b8477] | 360 | # type: (Data, Optional[np.ndarray], Optional[np.ndarray], str, bool, Optional[Tuple[float,float]], Optional[np.ndarray]) -> None |
---|
[299edd2] | 361 | """ |
---|
| 362 | Plot theory calculation. |
---|
| 363 | |
---|
| 364 | *data* is needed to define the graph properties such as labels and |
---|
| 365 | units, and to define the data mask. |
---|
| 366 | |
---|
| 367 | *theory* is a matrix of the same shape as the data. |
---|
| 368 | |
---|
| 369 | *view* is log or linear |
---|
| 370 | |
---|
| 371 | *use_data* is True if the data should be plotted as well as the theory. |
---|
| 372 | |
---|
| 373 | *limits* sets the intensity limits on the plot; if None then the limits |
---|
| 374 | are inferred from the data. |
---|
[a5b8477] | 375 | |
---|
| 376 | *Iq_calc* is the raw theory values without resolution smearing |
---|
[299edd2] | 377 | """ |
---|
[3b4243d] | 378 | if hasattr(data, 'lam'): |
---|
[69ec80f] | 379 | _plot_result_sesans(data, theory, resid, use_data=True, limits=limits) |
---|
[3b4243d] | 380 | elif hasattr(data, 'qx_data'): |
---|
[69ec80f] | 381 | _plot_result2D(data, theory, resid, view, use_data, limits=limits) |
---|
[3b4243d] | 382 | else: |
---|
[ea75043] | 383 | _plot_result1D(data, theory, resid, view, use_data, |
---|
| 384 | limits=limits, Iq_calc=Iq_calc) |
---|
[3b4243d] | 385 | |
---|
| 386 | |
---|
[40a87fa] | 387 | def protect(func): |
---|
[a5b8477] | 388 | # type: (Callable) -> Callable |
---|
[299edd2] | 389 | """ |
---|
| 390 | Decorator to wrap calls in an exception trapper which prints the |
---|
| 391 | exception and continues. Keyboard interrupts are ignored. |
---|
| 392 | """ |
---|
[3b4243d] | 393 | def wrapper(*args, **kw): |
---|
[eafc9fa] | 394 | """ |
---|
[5c962df] | 395 | Trap and print errors from function. |
---|
| 396 | """ |
---|
[3b4243d] | 397 | try: |
---|
[40a87fa] | 398 | return func(*args, **kw) |
---|
[ee8f734] | 399 | except Exception: |
---|
[3b4243d] | 400 | traceback.print_exc() |
---|
| 401 | |
---|
| 402 | return wrapper |
---|
| 403 | |
---|
| 404 | |
---|
| 405 | @protect |
---|
[ea75043] | 406 | def _plot_result1D(data, theory, resid, view, use_data, |
---|
| 407 | limits=None, Iq_calc=None): |
---|
[a5b8477] | 408 | # type: (Data1D, Optional[np.ndarray], Optional[np.ndarray], str, bool, Optional[Tuple[float, float]], Optional[np.ndarray]) -> None |
---|
[3b4243d] | 409 | """ |
---|
| 410 | Plot the data and residuals for 1D data. |
---|
| 411 | """ |
---|
[7ae2b7f] | 412 | import matplotlib.pyplot as plt # type: ignore |
---|
| 413 | from numpy.ma import masked_array, masked # type: ignore |
---|
[3b4243d] | 414 | |
---|
[69ec80f] | 415 | use_data = use_data and data.y is not None |
---|
| 416 | use_theory = theory is not None |
---|
| 417 | use_resid = resid is not None |
---|
[ea75043] | 418 | use_calc = use_theory and Iq_calc is not None |
---|
| 419 | num_plots = (use_data or use_theory) + use_calc + use_resid |
---|
[40a87fa] | 420 | non_positive_x = (data.x <= 0.0).any() |
---|
[3b4243d] | 421 | |
---|
| 422 | scale = data.x**4 if view == 'q4' else 1.0 |
---|
| 423 | |
---|
[69ec80f] | 424 | if use_data or use_theory: |
---|
[1d61d07] | 425 | if num_plots > 1: |
---|
| 426 | plt.subplot(1, num_plots, 1) |
---|
| 427 | |
---|
[9404dd3] | 428 | #print(vmin, vmax) |
---|
[644430f] | 429 | all_positive = True |
---|
| 430 | some_present = False |
---|
[69ec80f] | 431 | if use_data: |
---|
[644430f] | 432 | mdata = masked_array(data.y, data.mask.copy()) |
---|
[3b4243d] | 433 | mdata[~np.isfinite(mdata)] = masked |
---|
| 434 | if view is 'log': |
---|
| 435 | mdata[mdata <= 0] = masked |
---|
[092cb3c] | 436 | plt.errorbar(data.x, scale*mdata, yerr=data.dy, fmt='.') |
---|
[d15a908] | 437 | all_positive = all_positive and (mdata > 0).all() |
---|
[644430f] | 438 | some_present = some_present or (mdata.count() > 0) |
---|
| 439 | |
---|
[3b4243d] | 440 | |
---|
[69ec80f] | 441 | if use_theory: |
---|
[e78edc4] | 442 | # Note: masks merge, so any masked theory points will stay masked, |
---|
| 443 | # and the data mask will be added to it. |
---|
[644430f] | 444 | mtheory = masked_array(theory, data.mask.copy()) |
---|
| 445 | mtheory[~np.isfinite(mtheory)] = masked |
---|
[3b4243d] | 446 | if view is 'log': |
---|
[d15a908] | 447 | mtheory[mtheory <= 0] = masked |
---|
[1d61d07] | 448 | plt.plot(data.x, scale*mtheory, '-', hold=True) |
---|
[d15a908] | 449 | all_positive = all_positive and (mtheory > 0).all() |
---|
[644430f] | 450 | some_present = some_present or (mtheory.count() > 0) |
---|
| 451 | |
---|
[013adb7] | 452 | if limits is not None: |
---|
| 453 | plt.ylim(*limits) |
---|
[69ec80f] | 454 | |
---|
[e78edc4] | 455 | plt.xscale('linear' if not some_present or non_positive_x else view) |
---|
[644430f] | 456 | plt.yscale('linear' |
---|
| 457 | if view == 'q4' or not some_present or not all_positive |
---|
| 458 | else view) |
---|
[092cb3c] | 459 | plt.xlabel("$q$/A$^{-1}$") |
---|
[644430f] | 460 | plt.ylabel('$I(q)$') |
---|
[3b4243d] | 461 | |
---|
[ea75043] | 462 | if use_calc: |
---|
| 463 | # Only have use_calc if have use_theory |
---|
| 464 | plt.subplot(1, num_plots, 2) |
---|
| 465 | qx, qy, Iqxy = Iq_calc |
---|
[40a87fa] | 466 | plt.pcolormesh(qx, qy[qy > 0], np.log10(Iqxy[qy > 0, :])) |
---|
[ea75043] | 467 | plt.xlabel("$q_x$/A$^{-1}$") |
---|
| 468 | plt.xlabel("$q_y$/A$^{-1}$") |
---|
[d6f5da6] | 469 | plt.xscale('log') |
---|
| 470 | plt.yscale('log') |
---|
[ea75043] | 471 | #plt.axis('equal') |
---|
| 472 | |
---|
[69ec80f] | 473 | if use_resid: |
---|
[644430f] | 474 | mresid = masked_array(resid, data.mask.copy()) |
---|
| 475 | mresid[~np.isfinite(mresid)] = masked |
---|
| 476 | some_present = (mresid.count() > 0) |
---|
[69ec80f] | 477 | |
---|
| 478 | if num_plots > 1: |
---|
[ea75043] | 479 | plt.subplot(1, num_plots, use_calc + 2) |
---|
[092cb3c] | 480 | plt.plot(data.x, mresid, '-') |
---|
| 481 | plt.xlabel("$q$/A$^{-1}$") |
---|
[3b4243d] | 482 | plt.ylabel('residuals') |
---|
[e78edc4] | 483 | plt.xscale('linear' if not some_present or non_positive_x else view) |
---|
[3b4243d] | 484 | |
---|
| 485 | |
---|
| 486 | @protect |
---|
[69ec80f] | 487 | def _plot_result_sesans(data, theory, resid, use_data, limits=None): |
---|
[a5b8477] | 488 | # type: (SesansData, Optional[np.ndarray], Optional[np.ndarray], bool, Optional[Tuple[float, float]]) -> None |
---|
[299edd2] | 489 | """ |
---|
| 490 | Plot SESANS results. |
---|
| 491 | """ |
---|
[7ae2b7f] | 492 | import matplotlib.pyplot as plt # type: ignore |
---|
[69ec80f] | 493 | use_data = use_data and data.y is not None |
---|
| 494 | use_theory = theory is not None |
---|
| 495 | use_resid = resid is not None |
---|
| 496 | num_plots = (use_data or use_theory) + use_resid |
---|
| 497 | |
---|
| 498 | if use_data or use_theory: |
---|
[a5b8477] | 499 | is_tof = (data.lam != data.lam[0]).any() |
---|
[69ec80f] | 500 | if num_plots > 1: |
---|
| 501 | plt.subplot(1, num_plots, 1) |
---|
| 502 | if use_data: |
---|
[84db7a5] | 503 | if is_tof: |
---|
[a5b8477] | 504 | plt.errorbar(data.x, np.log(data.y)/(data.lam*data.lam), |
---|
| 505 | yerr=data.dy/data.y/(data.lam*data.lam)) |
---|
[84db7a5] | 506 | else: |
---|
| 507 | plt.errorbar(data.x, data.y, yerr=data.dy) |
---|
[3b4243d] | 508 | if theory is not None: |
---|
[84db7a5] | 509 | if is_tof: |
---|
| 510 | plt.plot(data.x, np.log(theory)/(data.lam*data.lam), '-', hold=True) |
---|
| 511 | else: |
---|
| 512 | plt.plot(data.x, theory, '-', hold=True) |
---|
[013adb7] | 513 | if limits is not None: |
---|
| 514 | plt.ylim(*limits) |
---|
[84db7a5] | 515 | |
---|
| 516 | plt.xlabel('spin echo length ({})'.format(data._xunit)) |
---|
| 517 | if is_tof: |
---|
[40a87fa] | 518 | plt.ylabel(r'(Log (P/P$_0$))/$\lambda^2$') |
---|
[84db7a5] | 519 | else: |
---|
| 520 | plt.ylabel('polarization (P/P0)') |
---|
| 521 | |
---|
[3b4243d] | 522 | |
---|
| 523 | if resid is not None: |
---|
[69ec80f] | 524 | if num_plots > 1: |
---|
| 525 | plt.subplot(1, num_plots, (use_data or use_theory) + 1) |
---|
[3b4243d] | 526 | plt.plot(data.x, resid, 'x') |
---|
[84db7a5] | 527 | plt.xlabel('spin echo length ({})'.format(data._xunit)) |
---|
[3b4243d] | 528 | plt.ylabel('residuals (P/P0)') |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | @protect |
---|
[69ec80f] | 532 | def _plot_result2D(data, theory, resid, view, use_data, limits=None): |
---|
[a5b8477] | 533 | # type: (Data2D, Optional[np.ndarray], Optional[np.ndarray], str, bool, Optional[Tuple[float,float]]) -> None |
---|
[3b4243d] | 534 | """ |
---|
| 535 | Plot the data and residuals for 2D data. |
---|
| 536 | """ |
---|
[7ae2b7f] | 537 | import matplotlib.pyplot as plt # type: ignore |
---|
[69ec80f] | 538 | use_data = use_data and data.data is not None |
---|
| 539 | use_theory = theory is not None |
---|
| 540 | use_resid = resid is not None |
---|
| 541 | num_plots = use_data + use_theory + use_resid |
---|
[3b4243d] | 542 | |
---|
| 543 | # Put theory and data on a common colormap scale |
---|
[69ec80f] | 544 | vmin, vmax = np.inf, -np.inf |
---|
[a5b8477] | 545 | target = None # type: Optional[np.ndarray] |
---|
[69ec80f] | 546 | if use_data: |
---|
| 547 | target = data.data[~data.mask] |
---|
| 548 | datamin = target[target > 0].min() if view == 'log' else target.min() |
---|
| 549 | datamax = target.max() |
---|
| 550 | vmin = min(vmin, datamin) |
---|
| 551 | vmax = max(vmax, datamax) |
---|
| 552 | if use_theory: |
---|
| 553 | theorymin = theory[theory > 0].min() if view == 'log' else theory.min() |
---|
| 554 | theorymax = theory.max() |
---|
| 555 | vmin = min(vmin, theorymin) |
---|
| 556 | vmax = max(vmax, theorymax) |
---|
| 557 | |
---|
| 558 | # Override data limits from the caller |
---|
| 559 | if limits is not None: |
---|
[013adb7] | 560 | vmin, vmax = limits |
---|
[3b4243d] | 561 | |
---|
[69ec80f] | 562 | # Plot data |
---|
| 563 | if use_data: |
---|
| 564 | if num_plots > 1: |
---|
| 565 | plt.subplot(1, num_plots, 1) |
---|
[3b4243d] | 566 | _plot_2d_signal(data, target, view=view, vmin=vmin, vmax=vmax) |
---|
| 567 | plt.title('data') |
---|
[644430f] | 568 | h = plt.colorbar() |
---|
| 569 | h.set_label('$I(q)$') |
---|
[3b4243d] | 570 | |
---|
[69ec80f] | 571 | # plot theory |
---|
| 572 | if use_theory: |
---|
| 573 | if num_plots > 1: |
---|
| 574 | plt.subplot(1, num_plots, use_data+1) |
---|
[3b4243d] | 575 | _plot_2d_signal(data, theory, view=view, vmin=vmin, vmax=vmax) |
---|
| 576 | plt.title('theory') |
---|
[644430f] | 577 | h = plt.colorbar() |
---|
[d15a908] | 578 | h.set_label(r'$\log_{10}I(q)$' if view == 'log' |
---|
[013adb7] | 579 | else r'$q^4 I(q)$' if view == 'q4' |
---|
| 580 | else '$I(q)$') |
---|
[3b4243d] | 581 | |
---|
[69ec80f] | 582 | # plot resid |
---|
| 583 | if use_resid: |
---|
| 584 | if num_plots > 1: |
---|
| 585 | plt.subplot(1, num_plots, use_data+use_theory+1) |
---|
[3b4243d] | 586 | _plot_2d_signal(data, resid, view='linear') |
---|
| 587 | plt.title('residuals') |
---|
[644430f] | 588 | h = plt.colorbar() |
---|
[d15a908] | 589 | h.set_label(r'$\Delta I(q)$') |
---|
[3b4243d] | 590 | |
---|
| 591 | |
---|
| 592 | @protect |
---|
| 593 | def _plot_2d_signal(data, signal, vmin=None, vmax=None, view='log'): |
---|
[a5b8477] | 594 | # type: (Data2D, np.ndarray, Optional[float], Optional[float], str) -> Tuple[float, float] |
---|
[3b4243d] | 595 | """ |
---|
| 596 | Plot the target value for the data. This could be the data itself, |
---|
| 597 | the theory calculation, or the residuals. |
---|
| 598 | |
---|
| 599 | *scale* can be 'log' for log scale data, or 'linear'. |
---|
| 600 | """ |
---|
[7ae2b7f] | 601 | import matplotlib.pyplot as plt # type: ignore |
---|
| 602 | from numpy.ma import masked_array # type: ignore |
---|
[3b4243d] | 603 | |
---|
| 604 | image = np.zeros_like(data.qx_data) |
---|
| 605 | image[~data.mask] = signal |
---|
| 606 | valid = np.isfinite(image) |
---|
| 607 | if view == 'log': |
---|
| 608 | valid[valid] = (image[valid] > 0) |
---|
[013adb7] | 609 | if vmin is None: vmin = image[valid & ~data.mask].min() |
---|
| 610 | if vmax is None: vmax = image[valid & ~data.mask].max() |
---|
[3b4243d] | 611 | image[valid] = np.log10(image[valid]) |
---|
| 612 | elif view == 'q4': |
---|
| 613 | image[valid] *= (data.qx_data[valid]**2+data.qy_data[valid]**2)**2 |
---|
[013adb7] | 614 | if vmin is None: vmin = image[valid & ~data.mask].min() |
---|
| 615 | if vmax is None: vmax = image[valid & ~data.mask].max() |
---|
| 616 | else: |
---|
| 617 | if vmin is None: vmin = image[valid & ~data.mask].min() |
---|
| 618 | if vmax is None: vmax = image[valid & ~data.mask].max() |
---|
| 619 | |
---|
[3b4243d] | 620 | image[~valid | data.mask] = 0 |
---|
| 621 | #plottable = Iq |
---|
| 622 | plottable = masked_array(image, ~valid | data.mask) |
---|
[7824276] | 623 | # Divide range by 10 to convert from angstroms to nanometers |
---|
[ea75043] | 624 | xmin, xmax = min(data.qx_data), max(data.qx_data) |
---|
| 625 | ymin, ymax = min(data.qy_data), max(data.qy_data) |
---|
[013adb7] | 626 | if view == 'log': |
---|
| 627 | vmin, vmax = np.log10(vmin), np.log10(vmax) |
---|
[ce166d3] | 628 | plt.imshow(plottable.reshape(len(data.x_bins), len(data.y_bins)), |
---|
[ea75043] | 629 | interpolation='nearest', aspect=1, origin='lower', |
---|
[3b4243d] | 630 | extent=[xmin, xmax, ymin, ymax], vmin=vmin, vmax=vmax) |
---|
[ea75043] | 631 | plt.xlabel("$q_x$/A$^{-1}$") |
---|
| 632 | plt.ylabel("$q_y$/A$^{-1}$") |
---|
[013adb7] | 633 | return vmin, vmax |
---|
[3b4243d] | 634 | |
---|
| 635 | def demo(): |
---|
[a5b8477] | 636 | # type: () -> None |
---|
[299edd2] | 637 | """ |
---|
| 638 | Load and plot a SAS dataset. |
---|
| 639 | """ |
---|
[3b4243d] | 640 | data = load_data('DEC07086.DAT') |
---|
| 641 | set_beam_stop(data, 0.004) |
---|
| 642 | plot_data(data) |
---|
[7ae2b7f] | 643 | import matplotlib.pyplot as plt # type: ignore |
---|
| 644 | plt.show() |
---|
[3b4243d] | 645 | |
---|
| 646 | |
---|
| 647 | if __name__ == "__main__": |
---|
| 648 | demo() |
---|