Changes in sasmodels/data.py [a5b8477:e78edc4] in sasmodels


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/data.py

    ra5b8477 re78edc4  
    3535import traceback 
    3636 
    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"] 
     37import numpy as np 
    4538 
    4639def load_data(filename): 
    47     # type: (str) -> Data 
    4840    """ 
    4941    Load data using a sasview loader. 
    5042    """ 
    51     from sas.sascalc.dataloader.loader import Loader  # type: ignore 
     43    from sas.sascalc.dataloader.loader import Loader 
    5244    loader = Loader() 
    5345    data = loader.load(filename) 
     
    5850 
    5951def set_beam_stop(data, radius, outer=None): 
    60     # type: (Data, float, Optional[float]) -> None 
    6152    """ 
    6253    Add a beam stop of the given *radius*.  If *outer*, make an annulus. 
    6354    """ 
    64     from sas.dataloader.manipulations import Ringcut  # type: ignore 
     55    from sas.dataloader.manipulations import Ringcut 
    6556    if hasattr(data, 'qx_data'): 
    6657        data.mask = Ringcut(0, radius)(data) 
     
    7465 
    7566def set_half(data, half): 
    76     # type: (Data, str) -> None 
    7767    """ 
    7868    Select half of the data, either "right" or "left". 
    7969    """ 
    80     from sas.dataloader.manipulations import Boxcut  # type: ignore 
     70    from sas.dataloader.manipulations import Boxcut 
    8171    if half == 'right': 
    8272        data.mask += \ 
     
    8878 
    8979def set_top(data, cutoff): 
    90     # type: (Data, float) -> None 
    9180    """ 
    9281    Chop the top off the data, above *cutoff*. 
    9382    """ 
    94     from sas.dataloader.manipulations import Boxcut  # type: ignore 
     83    from sas.dataloader.manipulations import Boxcut 
    9584    data.mask += \ 
    9685        Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=cutoff)(data) 
     
    125114    """ 
    126115    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 
    128116        self.x, self.y, self.dx, self.dy = x, y, dx, dy 
    129117        self.dxl = None 
     
    139127 
    140128    def xaxis(self, label, unit): 
    141         # type: (str, str) -> None 
    142129        """ 
    143130        set the x axis label and unit 
     
    147134 
    148135    def yaxis(self, label, unit): 
    149         # type: (str, str) -> None 
    150136        """ 
    151137        set the y axis label and unit 
     
    154140        self._yunit = unit 
    155141 
    156 class SesansData(Data1D): 
    157     def __init__(self, **kw): 
    158         Data1D.__init__(self, **kw) 
    159         self.lam = None # type: Optional[np.ndarray] 
     142 
    160143 
    161144class Data2D(object): 
     
    192175    """ 
    193176    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 
    195177        self.qx_data, self.dqx_data = x, dx 
    196178        self.qy_data, self.dqy_data = y, dy 
     
    215197 
    216198    def xaxis(self, label, unit): 
    217         # type: (str, str) -> None 
    218199        """ 
    219200        set the x axis label and unit 
     
    223204 
    224205    def yaxis(self, label, unit): 
    225         # type: (str, str) -> None 
    226206        """ 
    227207        set the y axis label and unit 
     
    231211 
    232212    def zaxis(self, label, unit): 
    233         # type: (str, str) -> None 
    234213        """ 
    235214        set the y axis label and unit 
     
    244223    """ 
    245224    def __init__(self, x=None, y=None, z=None): 
    246         # type: (float, float, Optional[float]) -> None 
    247225        self.x, self.y, self.z = x, y, z 
    248226 
     
    252230    """ 
    253231    def __init__(self, pixel_size=(None, None), distance=None): 
    254         # type: (Tuple[float, float], float) -> None 
    255232        self.pixel_size = Vector(*pixel_size) 
    256233        self.distance = distance 
     
    261238    """ 
    262239    def __init__(self): 
    263         # type: () -> None 
    264240        self.wavelength = np.NaN 
    265241        self.wavelength_unit = "A" 
     
    267243 
    268244def empty_data1D(q, resolution=0.0): 
    269     # type: (np.ndarray, float) -> Data1D 
    270245    """ 
    271246    Create empty 1D data using the given *q* as the x value. 
     
    284259 
    285260def empty_data2D(qx, qy=None, resolution=0.0): 
    286     # type: (np.ndarray, Optional[np.ndarray], float) -> Data2D 
    287261    """ 
    288262    Create empty 2D data using the given mesh. 
     
    298272    Qx, Qy = np.meshgrid(qx, qy) 
    299273    Qx, Qy = Qx.flatten(), Qy.flatten() 
    300     Iq = 100 * np.ones_like(Qx)  # type: np.ndarray 
     274    Iq = 100 * np.ones_like(Qx) 
    301275    dIq = np.sqrt(Iq) 
    302276    if resolution != 0: 
     
    326300 
    327301def plot_data(data, view='log', limits=None): 
    328     # type: (Data, str, Optional[Tuple[float, float]]) -> None 
    329302    """ 
    330303    Plot data loaded by the sasview loader. 
     
    350323def plot_theory(data, theory, resid=None, view='log', 
    351324                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 
    353325    """ 
    354326    Plot theory calculation. 
     
    365337    *limits* sets the intensity limits on the plot; if None then the limits 
    366338    are inferred from the data. 
    367  
    368     *Iq_calc* is the raw theory values without resolution smearing 
    369339    """ 
    370340    if hasattr(data, 'lam'): 
     
    378348 
    379349def protect(fn): 
    380     # type: (Callable) -> Callable 
    381350    """ 
    382351    Decorator to wrap calls in an exception trapper which prints the 
     
    389358        try: 
    390359            return fn(*args, **kw) 
    391         except Exception: 
     360        except KeyboardInterrupt: 
     361            raise 
     362        except: 
    392363            traceback.print_exc() 
    393364 
     
    398369def _plot_result1D(data, theory, resid, view, use_data, 
    399370                   limits=None, Iq_calc=None): 
    400     # type: (Data1D, Optional[np.ndarray], Optional[np.ndarray], str, bool, Optional[Tuple[float, float]], Optional[np.ndarray]) -> None 
    401371    """ 
    402372    Plot the data and residuals for 1D data. 
    403373    """ 
    404     import matplotlib.pyplot as plt  # type: ignore 
    405     from numpy.ma import masked_array, masked  # type: ignore 
     374    import matplotlib.pyplot as plt 
     375    from numpy.ma import masked_array, masked 
    406376 
    407377    use_data = use_data and data.y is not None 
     
    410380    use_calc = use_theory and Iq_calc is not None 
    411381    num_plots = (use_data or use_theory) + use_calc + use_resid 
    412  
     382    non_positive_x = (data.x<=0.0).any() 
    413383 
    414384    scale = data.x**4 if view == 'q4' else 1.0 
     
    432402 
    433403        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. 
    434406            mtheory = masked_array(theory, data.mask.copy()) 
    435407            mtheory[~np.isfinite(mtheory)] = masked 
     
    443415            plt.ylim(*limits) 
    444416 
    445         plt.xscale('linear' if not some_present else view) 
     417        plt.xscale('linear' if not some_present or non_positive_x  else view) 
    446418        plt.yscale('linear' 
    447419                   if view == 'q4' or not some_present or not all_positive 
     
    471443        plt.xlabel("$q$/A$^{-1}$") 
    472444        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) 
    474446 
    475447 
    476448@protect 
    477449def _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 
    479450    """ 
    480451    Plot SESANS results. 
    481452    """ 
    482     import matplotlib.pyplot as plt  # type: ignore 
     453    import matplotlib.pyplot as plt 
    483454    use_data = use_data and data.y is not None 
    484455    use_theory = theory is not None 
     
    487458 
    488459    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]) 
    490461        if num_plots > 1: 
    491462            plt.subplot(1, num_plots, 1) 
    492463        if use_data: 
    493464            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)) 
    496466            else: 
    497467                plt.errorbar(data.x, data.y, yerr=data.dy) 
     
    521491@protect 
    522492def _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 
    524493    """ 
    525494    Plot the data and residuals for 2D data. 
    526495    """ 
    527     import matplotlib.pyplot as plt  # type: ignore 
     496    import matplotlib.pyplot as plt 
    528497    use_data = use_data and data.data is not None 
    529498    use_theory = theory is not None 
     
    533502    # Put theory and data on a common colormap scale 
    534503    vmin, vmax = np.inf, -np.inf 
    535     target = None # type: Optional[np.ndarray] 
    536504    if use_data: 
    537505        target = data.data[~data.mask] 
     
    582550@protect 
    583551def _plot_2d_signal(data, signal, vmin=None, vmax=None, view='log'): 
    584     # type: (Data2D, np.ndarray, Optional[float], Optional[float], str) -> Tuple[float, float] 
    585552    """ 
    586553    Plot the target value for the data.  This could be the data itself, 
     
    589556    *scale* can be 'log' for log scale data, or 'linear'. 
    590557    """ 
    591     import matplotlib.pyplot as plt  # type: ignore 
    592     from numpy.ma import masked_array  # type: ignore 
     558    import matplotlib.pyplot as plt 
     559    from numpy.ma import masked_array 
    593560 
    594561    image = np.zeros_like(data.qx_data) 
     
    624591 
    625592def demo(): 
    626     # type: () -> None 
    627593    """ 
    628594    Load and plot a SAS dataset. 
     
    631597    set_beam_stop(data, 0.004) 
    632598    plot_data(data) 
    633     import matplotlib.pyplot as plt  # type: ignore 
    634     plt.show() 
     599    import matplotlib.pyplot as plt; plt.show() 
    635600 
    636601 
Note: See TracChangeset for help on using the changeset viewer.