Changes in sasmodels/data.py [a5b8477:e78edc4] in sasmodels
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/data.py
ra5b8477 re78edc4 35 35 import traceback 36 36 37 import numpy as np # type: ignore 38 39 try: 40 from typing import Union, Dict, List, Optional 41 except ImportError: 42 pass 43 else: 44 Data = Union["Data1D", "Data2D", "SesansData"] 37 import numpy as np 45 38 46 39 def load_data(filename): 47 # type: (str) -> Data48 40 """ 49 41 Load data using a sasview loader. 50 42 """ 51 from sas.sascalc.dataloader.loader import Loader # type: ignore43 from sas.sascalc.dataloader.loader import Loader 52 44 loader = Loader() 53 45 data = loader.load(filename) … … 58 50 59 51 def set_beam_stop(data, radius, outer=None): 60 # type: (Data, float, Optional[float]) -> None61 52 """ 62 53 Add a beam stop of the given *radius*. If *outer*, make an annulus. 63 54 """ 64 from sas.dataloader.manipulations import Ringcut # type: ignore55 from sas.dataloader.manipulations import Ringcut 65 56 if hasattr(data, 'qx_data'): 66 57 data.mask = Ringcut(0, radius)(data) … … 74 65 75 66 def set_half(data, half): 76 # type: (Data, str) -> None77 67 """ 78 68 Select half of the data, either "right" or "left". 79 69 """ 80 from sas.dataloader.manipulations import Boxcut # type: ignore70 from sas.dataloader.manipulations import Boxcut 81 71 if half == 'right': 82 72 data.mask += \ … … 88 78 89 79 def set_top(data, cutoff): 90 # type: (Data, float) -> None91 80 """ 92 81 Chop the top off the data, above *cutoff*. 93 82 """ 94 from sas.dataloader.manipulations import Boxcut # type: ignore83 from sas.dataloader.manipulations import Boxcut 95 84 data.mask += \ 96 85 Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=cutoff)(data) … … 125 114 """ 126 115 def __init__(self, x=None, y=None, dx=None, dy=None): 127 # type: (Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray]) -> None128 116 self.x, self.y, self.dx, self.dy = x, y, dx, dy 129 117 self.dxl = None … … 139 127 140 128 def xaxis(self, label, unit): 141 # type: (str, str) -> None142 129 """ 143 130 set the x axis label and unit … … 147 134 148 135 def yaxis(self, label, unit): 149 # type: (str, str) -> None150 136 """ 151 137 set the y axis label and unit … … 154 140 self._yunit = unit 155 141 156 class SesansData(Data1D): 157 def __init__(self, **kw): 158 Data1D.__init__(self, **kw) 159 self.lam = None # type: Optional[np.ndarray] 142 160 143 161 144 class Data2D(object): … … 192 175 """ 193 176 def __init__(self, x=None, y=None, z=None, dx=None, dy=None, dz=None): 194 # type: (Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray]) -> None195 177 self.qx_data, self.dqx_data = x, dx 196 178 self.qy_data, self.dqy_data = y, dy … … 215 197 216 198 def xaxis(self, label, unit): 217 # type: (str, str) -> None218 199 """ 219 200 set the x axis label and unit … … 223 204 224 205 def yaxis(self, label, unit): 225 # type: (str, str) -> None226 206 """ 227 207 set the y axis label and unit … … 231 211 232 212 def zaxis(self, label, unit): 233 # type: (str, str) -> None234 213 """ 235 214 set the y axis label and unit … … 244 223 """ 245 224 def __init__(self, x=None, y=None, z=None): 246 # type: (float, float, Optional[float]) -> None247 225 self.x, self.y, self.z = x, y, z 248 226 … … 252 230 """ 253 231 def __init__(self, pixel_size=(None, None), distance=None): 254 # type: (Tuple[float, float], float) -> None255 232 self.pixel_size = Vector(*pixel_size) 256 233 self.distance = distance … … 261 238 """ 262 239 def __init__(self): 263 # type: () -> None264 240 self.wavelength = np.NaN 265 241 self.wavelength_unit = "A" … … 267 243 268 244 def empty_data1D(q, resolution=0.0): 269 # type: (np.ndarray, float) -> Data1D270 245 """ 271 246 Create empty 1D data using the given *q* as the x value. … … 284 259 285 260 def empty_data2D(qx, qy=None, resolution=0.0): 286 # type: (np.ndarray, Optional[np.ndarray], float) -> Data2D287 261 """ 288 262 Create empty 2D data using the given mesh. … … 298 272 Qx, Qy = np.meshgrid(qx, qy) 299 273 Qx, Qy = Qx.flatten(), Qy.flatten() 300 Iq = 100 * np.ones_like(Qx) # type: np.ndarray274 Iq = 100 * np.ones_like(Qx) 301 275 dIq = np.sqrt(Iq) 302 276 if resolution != 0: … … 326 300 327 301 def plot_data(data, view='log', limits=None): 328 # type: (Data, str, Optional[Tuple[float, float]]) -> None329 302 """ 330 303 Plot data loaded by the sasview loader. … … 350 323 def plot_theory(data, theory, resid=None, view='log', 351 324 use_data=True, limits=None, Iq_calc=None): 352 # type: (Data, Optional[np.ndarray], Optional[np.ndarray], str, bool, Optional[Tuple[float,float]], Optional[np.ndarray]) -> None353 325 """ 354 326 Plot theory calculation. … … 365 337 *limits* sets the intensity limits on the plot; if None then the limits 366 338 are inferred from the data. 367 368 *Iq_calc* is the raw theory values without resolution smearing369 339 """ 370 340 if hasattr(data, 'lam'): … … 378 348 379 349 def protect(fn): 380 # type: (Callable) -> Callable381 350 """ 382 351 Decorator to wrap calls in an exception trapper which prints the … … 389 358 try: 390 359 return fn(*args, **kw) 391 except Exception: 360 except KeyboardInterrupt: 361 raise 362 except: 392 363 traceback.print_exc() 393 364 … … 398 369 def _plot_result1D(data, theory, resid, view, use_data, 399 370 limits=None, Iq_calc=None): 400 # type: (Data1D, Optional[np.ndarray], Optional[np.ndarray], str, bool, Optional[Tuple[float, float]], Optional[np.ndarray]) -> None401 371 """ 402 372 Plot the data and residuals for 1D data. 403 373 """ 404 import matplotlib.pyplot as plt # type: ignore405 from numpy.ma import masked_array, masked # type: ignore374 import matplotlib.pyplot as plt 375 from numpy.ma import masked_array, masked 406 376 407 377 use_data = use_data and data.y is not None … … 410 380 use_calc = use_theory and Iq_calc is not None 411 381 num_plots = (use_data or use_theory) + use_calc + use_resid 412 382 non_positive_x = (data.x<=0.0).any() 413 383 414 384 scale = data.x**4 if view == 'q4' else 1.0 … … 432 402 433 403 if use_theory: 404 # Note: masks merge, so any masked theory points will stay masked, 405 # and the data mask will be added to it. 434 406 mtheory = masked_array(theory, data.mask.copy()) 435 407 mtheory[~np.isfinite(mtheory)] = masked … … 443 415 plt.ylim(*limits) 444 416 445 plt.xscale('linear' if not some_present else view)417 plt.xscale('linear' if not some_present or non_positive_x else view) 446 418 plt.yscale('linear' 447 419 if view == 'q4' or not some_present or not all_positive … … 471 443 plt.xlabel("$q$/A$^{-1}$") 472 444 plt.ylabel('residuals') 473 plt.xscale('linear' if not some_present else view)445 plt.xscale('linear' if not some_present or non_positive_x else view) 474 446 475 447 476 448 @protect 477 449 def _plot_result_sesans(data, theory, resid, use_data, limits=None): 478 # type: (SesansData, Optional[np.ndarray], Optional[np.ndarray], bool, Optional[Tuple[float, float]]) -> None479 450 """ 480 451 Plot SESANS results. 481 452 """ 482 import matplotlib.pyplot as plt # type: ignore453 import matplotlib.pyplot as plt 483 454 use_data = use_data and data.y is not None 484 455 use_theory = theory is not None … … 487 458 488 459 if use_data or use_theory: 489 is_tof = (data.lam != data.lam[0]).any()460 is_tof = np.any(data.lam!=data.lam[0]) 490 461 if num_plots > 1: 491 462 plt.subplot(1, num_plots, 1) 492 463 if use_data: 493 464 if is_tof: 494 plt.errorbar(data.x, np.log(data.y)/(data.lam*data.lam), 495 yerr=data.dy/data.y/(data.lam*data.lam)) 465 plt.errorbar(data.x, np.log(data.y)/(data.lam*data.lam), yerr=data.dy/data.y/(data.lam*data.lam)) 496 466 else: 497 467 plt.errorbar(data.x, data.y, yerr=data.dy) … … 521 491 @protect 522 492 def _plot_result2D(data, theory, resid, view, use_data, limits=None): 523 # type: (Data2D, Optional[np.ndarray], Optional[np.ndarray], str, bool, Optional[Tuple[float,float]]) -> None524 493 """ 525 494 Plot the data and residuals for 2D data. 526 495 """ 527 import matplotlib.pyplot as plt # type: ignore496 import matplotlib.pyplot as plt 528 497 use_data = use_data and data.data is not None 529 498 use_theory = theory is not None … … 533 502 # Put theory and data on a common colormap scale 534 503 vmin, vmax = np.inf, -np.inf 535 target = None # type: Optional[np.ndarray]536 504 if use_data: 537 505 target = data.data[~data.mask] … … 582 550 @protect 583 551 def _plot_2d_signal(data, signal, vmin=None, vmax=None, view='log'): 584 # type: (Data2D, np.ndarray, Optional[float], Optional[float], str) -> Tuple[float, float]585 552 """ 586 553 Plot the target value for the data. This could be the data itself, … … 589 556 *scale* can be 'log' for log scale data, or 'linear'. 590 557 """ 591 import matplotlib.pyplot as plt # type: ignore592 from numpy.ma import masked_array # type: ignore558 import matplotlib.pyplot as plt 559 from numpy.ma import masked_array 593 560 594 561 image = np.zeros_like(data.qx_data) … … 624 591 625 592 def demo(): 626 # type: () -> None627 593 """ 628 594 Load and plot a SAS dataset. … … 631 597 set_beam_stop(data, 0.004) 632 598 plot_data(data) 633 import matplotlib.pyplot as plt # type: ignore 634 plt.show() 599 import matplotlib.pyplot as plt; plt.show() 635 600 636 601
Note: See TracChangeset
for help on using the changeset viewer.