Changeset 013adb7 in sasmodels for sasmodels/data.py


Ignore:
Timestamp:
Dec 23, 2015 3:00:04 PM (8 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:
8b25ee1
Parents:
e21cc31
Message:

fix vlimits on 2D plots during parameter exploration

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/data.py

    rce166d3 r013adb7  
    220220 
    221221 
    222 def plot_data(data, view='log'): 
     222def plot_data(data, view='log', limits=None): 
    223223    """ 
    224224    Plot data loaded by the sasview loader. 
     
    228228    # do not repeat. 
    229229    if hasattr(data, 'lam'): 
    230         _plot_result_sesans(data, None, None, plot_data=True) 
     230        _plot_result_sesans(data, None, None, plot_data=True, limits=limits) 
    231231    elif hasattr(data, 'qx_data'): 
    232         _plot_result2D(data, None, None, view, plot_data=True) 
    233     else: 
    234         _plot_result1D(data, None, None, view, plot_data=True) 
    235  
    236  
    237 def plot_theory(data, theory, resid=None, view='log', plot_data=True): 
     232        _plot_result2D(data, None, None, view, plot_data=True, limits=limits) 
     233    else: 
     234        _plot_result1D(data, None, None, view, plot_data=True, limits=limits) 
     235 
     236 
     237def plot_theory(data, theory, resid=None, view='log', 
     238                plot_data=True, limits=None): 
    238239    if hasattr(data, 'lam'): 
    239         _plot_result_sesans(data, theory, resid, plot_data=True) 
     240        _plot_result_sesans(data, theory, resid, plot_data=True, limits=limits) 
    240241    elif hasattr(data, 'qx_data'): 
    241         _plot_result2D(data, theory, resid, view, plot_data) 
    242     else: 
    243         _plot_result1D(data, theory, resid, view, plot_data) 
     242        _plot_result2D(data, theory, resid, view, plot_data, limits=limits) 
     243    else: 
     244        _plot_result1D(data, theory, resid, view, plot_data, limits=limits) 
    244245 
    245246 
     
    256257 
    257258@protect 
    258 def _plot_result1D(data, theory, resid, view, plot_data): 
     259def _plot_result1D(data, theory, resid, view, plot_data, limits=None): 
    259260    """ 
    260261    Plot the data and residuals for 1D data. 
     
    296297            some_present = some_present or (mtheory.count() > 0) 
    297298 
     299        if limits is not None: 
     300            plt.ylim(*limits) 
    298301        plt.xscale('linear' if not some_present else view) 
    299302        plt.yscale('linear' 
     
    317320 
    318321@protect 
    319 def _plot_result_sesans(data, theory, resid, plot_data): 
     322def _plot_result_sesans(data, theory, resid, plot_data, limits=None): 
    320323    import matplotlib.pyplot as plt 
    321324    if data.y is None: 
     
    331334        if theory is not None: 
    332335            plt.plot(data.x, theory, '-', hold=True) 
     336        if limits is not None: 
     337            plt.ylim(*limits) 
    333338        plt.xlabel('spin echo length (nm)') 
    334339        plt.ylabel('polarization (P/P0)') 
     
    344349 
    345350@protect 
    346 def _plot_result2D(data, theory, resid, view, plot_data): 
     351def _plot_result2D(data, theory, resid, view, plot_data, limits=None): 
    347352    """ 
    348353    Plot the data and residuals for 2D data. 
     
    355360 
    356361    # Put theory and data on a common colormap scale 
    357     vmin, vmax = np.inf, -np.inf 
    358     if plot_data: 
    359         target = data.data[~data.mask] 
    360         datamin = target[target>0].min() if view == 'log' else target.min() 
    361         datamax = target.max() 
    362         vmin = min(vmin, datamin) 
    363         vmax = max(vmax, datamax) 
    364     if plot_theory: 
    365         theorymin = theory[theory>0].min() if view == 'log' else theory.min() 
    366         theorymax = theory.max() 
    367         vmin = min(vmin, theorymin) 
    368         vmax = max(vmax, theorymax) 
     362    if limits is None: 
     363        vmin, vmax = np.inf, -np.inf 
     364        if plot_data: 
     365            target = data.data[~data.mask] 
     366            datamin = target[target>0].min() if view == 'log' else target.min() 
     367            datamax = target.max() 
     368            vmin = min(vmin, datamin) 
     369            vmax = max(vmax, datamax) 
     370        if plot_theory: 
     371            theorymin = theory[theory>0].min() if view=='log' else theory.min() 
     372            theorymax = theory.max() 
     373            vmin = min(vmin, theorymin) 
     374            vmax = max(vmax, theorymax) 
     375    else: 
     376        vmin, vmax = limits 
    369377 
    370378    if plot_data: 
     
    388396        plt.title('theory') 
    389397        h = plt.colorbar() 
    390         h.set_label('$I(q)$') 
     398        h.set_label(r'$\log_{10}I(q)$' if view=='log' 
     399                    else r'$q^4 I(q)$' if view == 'q4' 
     400                    else '$I(q)$') 
    391401 
    392402    #if plot_data or plot_theory: 
     
    420430    if view == 'log': 
    421431        valid[valid] = (image[valid] > 0) 
     432        if vmin is None: vmin = image[valid & ~data.mask].min() 
     433        if vmax is None: vmax = image[valid & ~data.mask].max() 
    422434        image[valid] = np.log10(image[valid]) 
    423435    elif view == 'q4': 
    424436        image[valid] *= (data.qx_data[valid]**2+data.qy_data[valid]**2)**2 
     437        if vmin is None: vmin = image[valid & ~data.mask].min() 
     438        if vmax is None: vmax = image[valid & ~data.mask].max() 
     439    else: 
     440        if vmin is None: vmin = image[valid & ~data.mask].min() 
     441        if vmax is None: vmax = image[valid & ~data.mask].max() 
     442 
    425443    image[~valid | data.mask] = 0 
    426444    #plottable = Iq 
     
    428446    xmin, xmax = min(data.qx_data)/10, max(data.qx_data)/10 
    429447    ymin, ymax = min(data.qy_data)/10, max(data.qy_data)/10 
    430     # TODO: fix vmin, vmax so it is shared for theory/resid 
    431     vmin = vmax = None 
    432     try: 
    433         if vmin is None: vmin = image[valid & ~data.mask].min() 
    434         if vmax is None: vmax = image[valid & ~data.mask].max() 
    435     except: 
    436         vmin, vmax = 0, 1 
     448    if view == 'log': 
     449        vmin, vmax = np.log10(vmin), np.log10(vmax) 
    437450    plt.imshow(plottable.reshape(len(data.x_bins), len(data.y_bins)), 
    438451               interpolation='nearest', aspect=1, origin='upper', 
     
    440453    plt.xlabel("$q_x$/nm$^{-1}$") 
    441454    plt.ylabel("$q_y$/nm$^{-1}$") 
    442  
     455    return vmin, vmax 
    443456 
    444457def demo(): 
Note: See TracChangeset for help on using the changeset viewer.