Skip to content

Commit 973518c

Browse files
author
Paul Kienzle
committed
Send data mask from theory calculator into plotter
1 parent d5c0465 commit 973518c

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

example/fit.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
set_beam_stop(radial_data, 0.00669, outer=0.025)
1414
set_top(radial_data, -.0185)
1515

16+
# Set data limits on fit
17+
#radial_data.qmin, radial_data.qmax = 0.01, 0.02
18+
1619
tan_data = load_data('DEC07266.DAT')
1720
set_beam_stop(tan_data, 0.00669, outer=0.025)
1821
set_top(tan_data, -.0185)

example/simul_fit.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import numpy as np
22
from bumps.names import FitProblem, FreeVariables
33

4+
import sasdata
45
from sasmodels.bumps_model import Experiment, Model
56
from sasmodels.core import load_model
67
from sasmodels.data import load_data
78

89
# latex data, same sample usans and sans
910
# particles radius ~2300, uniform dispersity
10-
datasets = load_data('latex_smeared.xml', index='all')
11+
datasets = load_data(str(sasdata.data_path / '1d_data' / 'latex_smeared.xml'), index='all')
1112
#[print(data) for data in datasets]
1213

1314
# A single sphere model to share between the datasets. We will use
@@ -57,7 +58,9 @@
5758
# model1.background.range(0, 2)
5859
# model2.background.range(0, 2)
5960

61+
# Set data fitting limits
62+
#datasets[1].qmin, datasets[1].qmax = 1e-4, 4e-3
63+
6064
# Setup the experiments, sharing the same model across all datasets.
6165
M = [Experiment(data=data, model=model, name=data.run[0]) for data in datasets]
62-
6366
problem = FitProblem(M, freevars=free)

sasmodels/bumps_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def plot(self, view=None):
266266
data, theory, resid = self._data, self.theory(), self.residuals()
267267
# TODO: hack to display oriented usans 2-D pattern
268268
Iq_calc = self.Iq_calc if isinstance(self.Iq_calc, tuple) else None
269-
plot_theory(data, theory, resid, view, Iq_calc=Iq_calc)
269+
plot_theory(data, theory, resid, view, Iq_calc=Iq_calc, index=self.index)
270270

271271
def simulate_data(self, noise=None):
272272
# type: (float) -> None

sasmodels/data.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def empty_data2D(qx, qy=None, resolution=0.0):
413413
return data
414414

415415

416-
def plot_data(data, view=None, limits=None):
416+
def plot_data(data, view=None, limits=None, index=None):
417417
# type: (Data, str, OptLimits) -> None
418418
"""
419419
Plot data loaded by the sasview loader.
@@ -431,13 +431,13 @@ def plot_data(data, view=None, limits=None):
431431
if hasattr(data, 'isSesans') and data.isSesans:
432432
_plot_result_sesans(data, None, None, view, use_data=True, limits=limits)
433433
elif hasattr(data, 'qx_data') and not getattr(data, 'radial', False):
434-
_plot_result2D(data, None, None, view, use_data=True, limits=limits)
434+
_plot_result2D(data, None, None, view, use_data=True, limits=limits, index=index)
435435
else:
436-
_plot_result1D(data, None, None, view, use_data=True, limits=limits)
436+
_plot_result1D(data, None, None, view, use_data=True, limits=limits, index=index)
437437

438438

439439
def plot_theory(data, theory, resid=None, view=None, use_data=True,
440-
limits=None, Iq_calc=None):
440+
limits=None, Iq_calc=None, index=None):
441441
# type: (Data, OptArray, OptArray, OptString, bool, OptLimits, OptArray) -> None
442442
"""
443443
Plot theory calculation.
@@ -461,10 +461,10 @@ def plot_theory(data, theory, resid=None, view=None, use_data=True,
461461
if hasattr(data, 'isSesans') and data.isSesans:
462462
_plot_result_sesans(data, theory, resid, view, use_data=True, limits=limits)
463463
elif hasattr(data, 'qx_data') and not getattr(data, 'radial', False):
464-
_plot_result2D(data, theory, resid, view, use_data, limits=limits)
464+
_plot_result2D(data, theory, resid, view, use_data, limits=limits, index=index)
465465
else:
466466
_plot_result1D(data, theory, resid, view, use_data,
467-
limits=limits, Iq_calc=Iq_calc)
467+
limits=limits, Iq_calc=Iq_calc, index=index)
468468

469469

470470
def protect(func):
@@ -490,7 +490,7 @@ def wrapper(*args, **kw):
490490

491491
@protect
492492
def _plot_result1D(data, theory, resid, view, use_data,
493-
limits=None, Iq_calc=None):
493+
limits=None, Iq_calc=None, index=None):
494494
# type: (Data1D, OptArray, OptArray, str, bool, OptLimits, OptArray) -> None
495495
"""
496496
Plot the data and residuals for 1D data.
@@ -515,6 +515,9 @@ def _plot_result1D(data, theory, resid, view, use_data,
515515

516516
scale = 1e8 * data.x**4 if view == 'q4' else 1.0
517517

518+
if index is None:
519+
index = slice(None)
520+
518521
if use_data or use_theory:
519522
if num_plots > 1:
520523
plt.subplot(1, num_plots, 1)
@@ -538,8 +541,8 @@ def _plot_result1D(data, theory, resid, view, use_data,
538541
# Note: masks merge, so any masked theory points will stay masked,
539542
# and the data mask will be added to it.
540543
#mtheory = masked_array(theory, data.mask.copy())
541-
theory_x = data.x[data.mask == 0]
542-
theory_scale = scale if np.isscalar(scale) else scale[data.mask == 0]
544+
theory_x = data.x[index]
545+
theory_scale = scale if np.isscalar(scale) else scale[index]
543546
mtheory = masked_array(theory)
544547
mtheory[~np.isfinite(mtheory)] = masked
545548
if view == 'log':
@@ -573,7 +576,7 @@ def _plot_result1D(data, theory, resid, view, use_data,
573576
#plt.axis('equal')
574577

575578
if use_resid:
576-
theory_x = data.x[data.mask == 0]
579+
theory_x = data.x[index]
577580
mresid = masked_array(resid)
578581
mresid[~np.isfinite(mresid)] = masked
579582
some_present = (mresid.count() > 0)
@@ -641,7 +644,7 @@ def _plot_result_sesans(data, theory, resid, view, use_data, limits=None):
641644

642645

643646
@protect
644-
def _plot_result2D(data, theory, resid, view, use_data, limits=None):
647+
def _plot_result2D(data, theory, resid, view, use_data, limits=None, index=None):
645648
# type: (Data2D, OptArray, OptArray, str, bool, OptLimits) -> None
646649
"""
647650
Plot the data and residuals for 2D data.
@@ -653,12 +656,15 @@ def _plot_result2D(data, theory, resid, view, use_data, limits=None):
653656
use_theory = theory is not None
654657
use_resid = resid is not None
655658
num_plots = use_data + use_theory + use_resid
659+
mask = ~data.mask
660+
if index is None:
661+
index = mask
656662

657663
# Put theory and data on a common colormap scale
658664
vmin, vmax = np.inf, -np.inf
659665
target = None # type: OptArray
660666
if use_data:
661-
target = data.data[~data.mask]
667+
target = data.data[mask] # full data
662668
datamin = target[target > 0].min() if view == 'log' else target.min()
663669
datamax = target.max()
664670
vmin = min(vmin, datamin)
@@ -677,7 +683,7 @@ def _plot_result2D(data, theory, resid, view, use_data, limits=None):
677683
if use_data:
678684
if num_plots > 1:
679685
plt.subplot(1, num_plots, 1)
680-
_plot_2d_signal(data, target, view=view, vmin=vmin, vmax=vmax)
686+
_plot_2d_signal(data, target, view=view, vmin=vmin, vmax=vmax, index=mask)
681687
plt.title('data')
682688
h = plt.colorbar()
683689
h.set_label('$I(q)$')
@@ -686,7 +692,7 @@ def _plot_result2D(data, theory, resid, view, use_data, limits=None):
686692
if use_theory:
687693
if num_plots > 1:
688694
plt.subplot(1, num_plots, use_data+1)
689-
_plot_2d_signal(data, theory, view=view, vmin=vmin, vmax=vmax)
695+
_plot_2d_signal(data, theory, view=view, vmin=vmin, vmax=vmax, index=index)
690696
plt.title('theory')
691697
h = plt.colorbar()
692698
h.set_label(r'$\log_{10}I(q)$' if view == 'log'
@@ -697,14 +703,14 @@ def _plot_result2D(data, theory, resid, view, use_data, limits=None):
697703
if use_resid:
698704
if num_plots > 1:
699705
plt.subplot(1, num_plots, use_data+use_theory+1)
700-
_plot_2d_signal(data, resid, view='linear')
706+
_plot_2d_signal(data, resid, view='linear', index=index)
701707
plt.title('residuals')
702708
h = plt.colorbar()
703709
h.set_label(r'$\Delta I(q)$')
704710

705711

706712
@protect
707-
def _plot_2d_signal(data, signal, vmin=None, vmax=None, view=None):
713+
def _plot_2d_signal(data, signal, vmin=None, vmax=None, view=None, index=None):
708714
# type: (Data2D, np.ndarray, Optional[float], Optional[float], str) -> tuple[float, float]
709715
"""
710716
Plot the target value for the data. This could be the data itself,
@@ -719,8 +725,8 @@ def _plot_2d_signal(data, signal, vmin=None, vmax=None, view=None):
719725
view = 'log'
720726

721727
image = np.zeros_like(data.qx_data)
722-
image[~data.mask] = signal
723-
valid = np.isfinite(image) & ~data.mask
728+
image[index] = signal
729+
valid = np.isfinite(image) & index
724730
if view == 'log':
725731
valid &= image > 0
726732
if vmin is None:

0 commit comments

Comments
 (0)