Changeset 3c56da87 in sasmodels for sasmodels/bumps_model.py


Ignore:
Timestamp:
Mar 4, 2015 10:55:38 PM (9 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
3a45c2c
Parents:
b89f519
Message:

lint cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/bumps_model.py

    rb89f519 r3c56da87  
    88# CRUFT python 2.6 
    99if not hasattr(datetime.timedelta, 'total_seconds'): 
    10     def delay(dt): return dt.days * 86400 + dt.seconds + 1e-6 * dt.microseconds 
     10    def delay(dt): 
     11        """Return number date-time delta as number seconds""" 
     12        return dt.days * 86400 + dt.seconds + 1e-6 * dt.microseconds 
    1113else: 
    12     def delay(dt): return dt.total_seconds() 
     14    def delay(dt): 
     15        """Return number date-time delta as number seconds""" 
     16        return dt.total_seconds() 
    1317 
    1418import numpy as np 
     
    141145    from sas.dataloader.manipulations import Boxcut 
    142146    if half == 'right': 
    143         data.mask += Boxcut(x_min=-np.inf, x_max=0.0, y_min=-np.inf, y_max=np.inf)(data) 
     147        data.mask += \ 
     148            Boxcut(x_min=-np.inf, x_max=0.0, y_min=-np.inf, y_max=np.inf)(data) 
    144149    if half == 'left': 
    145         data.mask += Boxcut(x_min=0.0, x_max=np.inf, y_min=-np.inf, y_max=np.inf)(data) 
    146  
    147  
    148 def set_top(data, max): 
    149     """ 
    150     Chop the top off the data, above *max*. 
     150        data.mask += \ 
     151            Boxcut(x_min=0.0, x_max=np.inf, y_min=-np.inf, y_max=np.inf)(data) 
     152 
     153 
     154def set_top(data, cutoff): 
     155    """ 
     156    Chop the top off the data, above *cutoff*. 
    151157    """ 
    152158    from sas.dataloader.manipulations import Boxcut 
    153     data.mask += Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=max)(data) 
    154  
    155  
    156 def plot_data(data, iq, vmin=None, vmax=None, view='log'): 
     159    data.mask += \ 
     160        Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=cutoff)(data) 
     161 
     162 
     163def plot_data(data, Iq, vmin=None, vmax=None, view='log'): 
    157164    """ 
    158165    Plot the target value for the data.  This could be the data itself, 
     
    161168    *scale* can be 'log' for log scale data, or 'linear'. 
    162169    """ 
    163     from numpy.ma import masked_array, masked 
     170    from numpy.ma import masked_array 
    164171    import matplotlib.pyplot as plt 
    165172    if hasattr(data, 'qx_data'): 
    166         iq = iq + 0 
    167         valid = np.isfinite(iq) 
     173        Iq = Iq + 0 
     174        valid = np.isfinite(Iq) 
    168175        if view == 'log': 
    169             valid[valid] = (iq[valid] > 0) 
    170             iq[valid] = np.log10(iq[valid]) 
     176            valid[valid] = (Iq[valid] > 0) 
     177            Iq[valid] = np.log10(Iq[valid]) 
    171178        elif view == 'q4': 
    172             iq[valid] = iq*(data.qx_data[valid]**2+data.qy_data[valid]**2)**2 
    173         iq[~valid | data.mask] = 0 
    174         #plottable = iq 
    175         plottable = masked_array(iq, ~valid | data.mask) 
     179            Iq[valid] = Iq*(data.qx_data[valid]**2+data.qy_data[valid]**2)**2 
     180        Iq[~valid | data.mask] = 0 
     181        #plottable = Iq 
     182        plottable = masked_array(Iq, ~valid | data.mask) 
    176183        xmin, xmax = min(data.qx_data), max(data.qx_data) 
    177184        ymin, ymax = min(data.qy_data), max(data.qy_data) 
    178185        try: 
    179             if vmin is None: vmin = iq[valid & ~data.mask].min() 
    180             if vmax is None: vmax = iq[valid & ~data.mask].max() 
     186            if vmin is None: vmin = Iq[valid & ~data.mask].min() 
     187            if vmax is None: vmax = Iq[valid & ~data.mask].max() 
    181188        except: 
    182189            vmin, vmax = 0, 1 
     
    186193    else: # 1D data 
    187194        if view == 'linear' or view == 'q4': 
    188             #idx = np.isfinite(iq) 
     195            #idx = np.isfinite(Iq) 
    189196            scale = data.x**4 if view == 'q4' else 1.0 
    190             plt.plot(data.x, scale*iq) #, '.') 
     197            plt.plot(data.x, scale*Iq) #, '.') 
    191198        else: 
    192199            # Find the values that are finite and positive 
    193             idx = np.isfinite(iq) 
    194             idx[idx] = iq[idx]>0 
    195             iq[~idx] = np.nan 
    196             plt.loglog(data.x, iq) 
     200            idx = np.isfinite(Iq) 
     201            idx[idx] = (Iq[idx] > 0) 
     202            Iq[~idx] = np.nan 
     203            plt.loglog(data.x, Iq) 
    197204 
    198205 
     
    220227    plt.plot(data.x, mresid, 'x') 
    221228 
     229# pylint: disable=unused-argument 
    222230def _plot_sesans(data, theory, view): 
    223231    import matplotlib.pyplot as plt 
     
    286294            q = sesans.make_q(data.sample.zacceptance, data.Rmax) 
    287295            self.index = slice(None, None) 
    288             self.iq = data.y 
    289             self.diq = data.dy 
     296            self.Iq = data.y 
     297            self.dIq = data.dy 
    290298            self._theory = np.zeros_like(q) 
    291299            q_vectors = [q] 
    292300        elif self.data_type == 'Iqxy': 
    293301            self.index = (data.mask == 0) & (~np.isnan(data.data)) 
    294             self.iq = data.data[self.index] 
    295             self.diq = data.err_data[self.index] 
     302            self.Iq = data.data[self.index] 
     303            self.dIq = data.err_data[self.index] 
    296304            self._theory = np.zeros_like(data.data) 
    297305            if not partype['orientation'] and not partype['magnetic']: 
     
    301309        elif self.data_type == 'Iq': 
    302310            self.index = (data.x >= data.qmin) & (data.x <= data.qmax) & ~np.isnan(data.y) 
    303             self.iq = data.y[self.index] 
    304             self.diq = data.dy[self.index] 
     311            self.Iq = data.y[self.index] 
     312            self.dIq = data.dy[self.index] 
    305313            self._theory = np.zeros_like(data.y) 
    306314            q_vectors = [data.x] 
     
    316324        pars = [] 
    317325        for p in model.info['parameters']: 
    318             name, default, limits, ptype = p[0], p[2], p[3], p[4] 
     326            name, default, limits = p[0], p[2], p[3] 
    319327            value = kw.pop(name, default) 
    320328            setattr(self, name, Parameter.default(value, name=name, limits=limits)) 
     
    335343        self._parameter_names = pars 
    336344        if kw: 
    337             raise TypeError("unexpected parameters: %s" % (", ".join(sorted(kw.keys())))) 
     345            raise TypeError("unexpected parameters: %s" 
     346                            % (", ".join(sorted(kw.keys())))) 
    338347        self.update() 
    339348 
     
    345354            Return the number of points 
    346355        """ 
    347         return len(self.iq) 
     356        return len(self.Iq) 
    348357 
    349358    def parameters(self): 
     
    365374            #self._theory[:] = self._fn.eval(pars, pd_pars) 
    366375            if self.data_type == 'sesans': 
    367                 P = sesans.hankel(self.data.x, self.data.lam * 1e-9, 
    368                                   self.data.sample.thickness / 10, self._fn_inputs[0], 
    369                                   self._theory) 
    370                 self._cache['theory'] = P 
     376                result = sesans.hankel(self.data.x, self.data.lam * 1e-9, 
     377                                       self.data.sample.thickness / 10, 
     378                                       self._fn_inputs[0], self._theory) 
     379                self._cache['theory'] = result 
    371380            else: 
    372381                self._cache['theory'] = self._theory 
     
    375384    def residuals(self): 
    376385        #if np.any(self.err ==0): print "zeros in err" 
    377         return (self.theory()[self.index] - self.iq) / self.diq 
     386        return (self.theory()[self.index] - self.Iq) / self.dIq 
    378387 
    379388    def nllf(self): 
    380         R = self.residuals() 
     389        delta = self.residuals() 
    381390        #if np.any(np.isnan(R)): print "NaN in residuals" 
    382         return 0.5 * np.sum(R ** 2) 
    383  
    384     def __call__(self): 
    385         return 2 * self.nllf() / self.dof 
     391        return 0.5 * np.sum(delta ** 2) 
     392 
     393    #def __call__(self): 
     394    #    return 2 * self.nllf() / self.dof 
    386395 
    387396    def plot(self, view='log'): 
     
    402411        print "noise", noise 
    403412        if noise is None: 
    404             noise = self.diq[self.index] 
     413            noise = self.dIq[self.index] 
    405414        else: 
    406415            noise = 0.01 * noise 
    407             self.diq[self.index] = noise 
     416            self.dIq[self.index] = noise 
    408417        y = self.theory() 
    409418        y += y * np.random.randn(*y.shape) * noise 
     
    428437        relative = self.model.info['partype']['pd-rel'] 
    429438        limits = self.model.info['limits'] 
    430         disperser, value, npts, width, nsigma = \ 
    431             [getattr(self, par + ext) for ext in ('_pd_type', '', '_pd_n', '_pd', '_pd_nsigma')] 
    432         v, w = weights.get_weights( 
     439        disperser, value, npts, width, nsigma = [ 
     440            getattr(self, par + ext) 
     441            for ext in ('_pd_type', '', '_pd_n', '_pd', '_pd_nsigma')] 
     442        value, weight = weights.get_weights( 
    433443            disperser, int(npts.value), width.value, nsigma.value, 
    434444            value.value, limits[par], par in relative) 
    435         return v, w / w.max() 
     445        return value, weight / np.sum(weight) 
    436446 
    437447    def __getstate__(self): 
     
    442452 
    443453    def __setstate__(self, state): 
     454        # pylint: disable=attribute-defined-outside-init 
    444455        self.__dict__ = state 
    445456 
Note: See TracChangeset for help on using the changeset viewer.