Changes in sasmodels/data.py [d6f5da6:a5b8477] in sasmodels
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/data.py
rd6f5da6 ra5b8477 35 35 import traceback 36 36 37 import numpy as np 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"] 38 45 39 46 def load_data(filename): 47 # type: (str) -> Data 40 48 """ 41 49 Load data using a sasview loader. 42 50 """ 43 from sas.sascalc.dataloader.loader import Loader 51 from sas.sascalc.dataloader.loader import Loader # type: ignore 44 52 loader = Loader() 45 53 data = loader.load(filename) … … 50 58 51 59 def set_beam_stop(data, radius, outer=None): 60 # type: (Data, float, Optional[float]) -> None 52 61 """ 53 62 Add a beam stop of the given *radius*. If *outer*, make an annulus. 54 63 """ 55 from sas.dataloader.manipulations import Ringcut 64 from sas.dataloader.manipulations import Ringcut # type: ignore 56 65 if hasattr(data, 'qx_data'): 57 66 data.mask = Ringcut(0, radius)(data) … … 65 74 66 75 def set_half(data, half): 76 # type: (Data, str) -> None 67 77 """ 68 78 Select half of the data, either "right" or "left". 69 79 """ 70 from sas.dataloader.manipulations import Boxcut 80 from sas.dataloader.manipulations import Boxcut # type: ignore 71 81 if half == 'right': 72 82 data.mask += \ … … 78 88 79 89 def set_top(data, cutoff): 90 # type: (Data, float) -> None 80 91 """ 81 92 Chop the top off the data, above *cutoff*. 82 93 """ 83 from sas.dataloader.manipulations import Boxcut 94 from sas.dataloader.manipulations import Boxcut # type: ignore 84 95 data.mask += \ 85 96 Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=cutoff)(data) … … 114 125 """ 115 126 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]) -> None 116 128 self.x, self.y, self.dx, self.dy = x, y, dx, dy 117 129 self.dxl = None … … 127 139 128 140 def xaxis(self, label, unit): 141 # type: (str, str) -> None 129 142 """ 130 143 set the x axis label and unit … … 134 147 135 148 def yaxis(self, label, unit): 149 # type: (str, str) -> None 136 150 """ 137 151 set the y axis label and unit … … 140 154 self._yunit = unit 141 155 142 156 class SesansData(Data1D): 157 def __init__(self, **kw): 158 Data1D.__init__(self, **kw) 159 self.lam = None # type: Optional[np.ndarray] 143 160 144 161 class Data2D(object): … … 175 192 """ 176 193 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]) -> None 177 195 self.qx_data, self.dqx_data = x, dx 178 196 self.qy_data, self.dqy_data = y, dy … … 197 215 198 216 def xaxis(self, label, unit): 217 # type: (str, str) -> None 199 218 """ 200 219 set the x axis label and unit … … 204 223 205 224 def yaxis(self, label, unit): 225 # type: (str, str) -> None 206 226 """ 207 227 set the y axis label and unit … … 211 231 212 232 def zaxis(self, label, unit): 233 # type: (str, str) -> None 213 234 """ 214 235 set the y axis label and unit … … 223 244 """ 224 245 def __init__(self, x=None, y=None, z=None): 246 # type: (float, float, Optional[float]) -> None 225 247 self.x, self.y, self.z = x, y, z 226 248 … … 230 252 """ 231 253 def __init__(self, pixel_size=(None, None), distance=None): 254 # type: (Tuple[float, float], float) -> None 232 255 self.pixel_size = Vector(*pixel_size) 233 256 self.distance = distance … … 238 261 """ 239 262 def __init__(self): 263 # type: () -> None 240 264 self.wavelength = np.NaN 241 265 self.wavelength_unit = "A" … … 243 267 244 268 def empty_data1D(q, resolution=0.0): 269 # type: (np.ndarray, float) -> Data1D 245 270 """ 246 271 Create empty 1D data using the given *q* as the x value. … … 259 284 260 285 def empty_data2D(qx, qy=None, resolution=0.0): 286 # type: (np.ndarray, Optional[np.ndarray], float) -> Data2D 261 287 """ 262 288 Create empty 2D data using the given mesh. … … 272 298 Qx, Qy = np.meshgrid(qx, qy) 273 299 Qx, Qy = Qx.flatten(), Qy.flatten() 274 Iq = 100 * np.ones_like(Qx) 300 Iq = 100 * np.ones_like(Qx) # type: np.ndarray 275 301 dIq = np.sqrt(Iq) 276 302 if resolution != 0: … … 300 326 301 327 def plot_data(data, view='log', limits=None): 328 # type: (Data, str, Optional[Tuple[float, float]]) -> None 302 329 """ 303 330 Plot data loaded by the sasview loader. … … 323 350 def plot_theory(data, theory, resid=None, view='log', 324 351 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]) -> None 325 353 """ 326 354 Plot theory calculation. … … 337 365 *limits* sets the intensity limits on the plot; if None then the limits 338 366 are inferred from the data. 367 368 *Iq_calc* is the raw theory values without resolution smearing 339 369 """ 340 370 if hasattr(data, 'lam'): … … 348 378 349 379 def protect(fn): 380 # type: (Callable) -> Callable 350 381 """ 351 382 Decorator to wrap calls in an exception trapper which prints the … … 358 389 try: 359 390 return fn(*args, **kw) 360 except KeyboardInterrupt: 361 raise 362 except: 391 except Exception: 363 392 traceback.print_exc() 364 393 … … 369 398 def _plot_result1D(data, theory, resid, view, use_data, 370 399 limits=None, Iq_calc=None): 400 # type: (Data1D, Optional[np.ndarray], Optional[np.ndarray], str, bool, Optional[Tuple[float, float]], Optional[np.ndarray]) -> None 371 401 """ 372 402 Plot the data and residuals for 1D data. 373 403 """ 374 import matplotlib.pyplot as plt 375 from numpy.ma import masked_array, masked 404 import matplotlib.pyplot as plt # type: ignore 405 from numpy.ma import masked_array, masked # type: ignore 376 406 377 407 use_data = use_data and data.y is not None … … 446 476 @protect 447 477 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]]) -> None 448 479 """ 449 480 Plot SESANS results. 450 481 """ 451 import matplotlib.pyplot as plt 482 import matplotlib.pyplot as plt # type: ignore 452 483 use_data = use_data and data.y is not None 453 484 use_theory = theory is not None … … 456 487 457 488 if use_data or use_theory: 458 is_tof = np.any(data.lam!=data.lam[0])489 is_tof = (data.lam != data.lam[0]).any() 459 490 if num_plots > 1: 460 491 plt.subplot(1, num_plots, 1) 461 492 if use_data: 462 493 if is_tof: 463 plt.errorbar(data.x, np.log(data.y)/(data.lam*data.lam), yerr=data.dy/data.y/(data.lam*data.lam)) 494 plt.errorbar(data.x, np.log(data.y)/(data.lam*data.lam), 495 yerr=data.dy/data.y/(data.lam*data.lam)) 464 496 else: 465 497 plt.errorbar(data.x, data.y, yerr=data.dy) … … 489 521 @protect 490 522 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]]) -> None 491 524 """ 492 525 Plot the data and residuals for 2D data. 493 526 """ 494 import matplotlib.pyplot as plt 527 import matplotlib.pyplot as plt # type: ignore 495 528 use_data = use_data and data.data is not None 496 529 use_theory = theory is not None … … 500 533 # Put theory and data on a common colormap scale 501 534 vmin, vmax = np.inf, -np.inf 535 target = None # type: Optional[np.ndarray] 502 536 if use_data: 503 537 target = data.data[~data.mask] … … 548 582 @protect 549 583 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] 550 585 """ 551 586 Plot the target value for the data. This could be the data itself, … … 554 589 *scale* can be 'log' for log scale data, or 'linear'. 555 590 """ 556 import matplotlib.pyplot as plt 557 from numpy.ma import masked_array 591 import matplotlib.pyplot as plt # type: ignore 592 from numpy.ma import masked_array # type: ignore 558 593 559 594 image = np.zeros_like(data.qx_data) … … 589 624 590 625 def demo(): 626 # type: () -> None 591 627 """ 592 628 Load and plot a SAS dataset. … … 595 631 set_beam_stop(data, 0.004) 596 632 plot_data(data) 597 import matplotlib.pyplot as plt; plt.show() 633 import matplotlib.pyplot as plt # type: ignore 634 plt.show() 598 635 599 636
Note: See TracChangeset
for help on using the changeset viewer.