diff options
author | Onderwaater <onderwaa@esrf.fr> | 2015-09-21 16:05:01 +0200 |
---|---|---|
committer | Onderwaater <onderwaa@esrf.fr> | 2015-09-21 16:05:01 +0200 |
commit | 411f072242bf7d767eaf19eea0873a2889a6fb3b (patch) | |
tree | 2612db95652093a99128de24a48d6989af0f50e5 | |
parent | 91d677d63bccc32bbabee5c226995108d8e6bb6d (diff) |
support masking the intensity array + loglog plotting for the gui
-rw-r--r-- | BINoculars/.dispatcher.py.swo | bin | 20480 -> 0 bytes | |||
-rw-r--r-- | BINoculars/backends/id03.py | 13 | ||||
-rw-r--r-- | BINoculars/plot.py | 7 | ||||
-rw-r--r-- | BINoculars/space.py | 25 | ||||
-rw-r--r-- | gui.py | 38 | ||||
-rw-r--r-- | test/id03.py | 11 |
6 files changed, 74 insertions, 20 deletions
diff --git a/BINoculars/.dispatcher.py.swo b/BINoculars/.dispatcher.py.swo Binary files differdeleted file mode 100644 index 4d6f52e..0000000 --- a/BINoculars/.dispatcher.py.swo +++ /dev/null diff --git a/BINoculars/backends/id03.py b/BINoculars/backends/id03.py index 434b81c..ad96e86 100644 --- a/BINoculars/backends/id03.py +++ b/BINoculars/backends/id03.py @@ -273,6 +273,7 @@ class ID03Input(backend.InputBase): self.config.xmask = slice(None) if self.config.ymask is None: self.config.ymask = slice(None) + self.config.maskmatrix = load_matrix(config.pop('maskmatrix', None)) #Optional, if supplied pixels where the mask is 0 will be removed if self.config.pr: self.config.pr = util.parse_tuple(self.config.pr, length=2, type=int) self.config.sdd = float(config.pop('sdd'))# sample to detector distance (mm) @@ -458,6 +459,11 @@ class EH1(ID03Input): delta_range= app[0] * (numpy.arange(data.shape[0]) - centralpixel[0]) + delta # masking + if self.config.maskmatrix is not None: + if self.config.maskmatrix.shape != data.shape: + raise errors.BackendError('The mask matrix does not have the same shape as the images') + data = numpy.ma.array(data, mask = ~self.config.maskmatrix) + gamma_range = gamma_range[self.config.ymask] delta_range = delta_range[self.config.xmask] intensity = self.apply_mask(data, self.config.xmask, self.config.ymask) @@ -563,6 +569,12 @@ class EH2(ID03Input): delta_range = app[1] * (numpy.arange(data.shape[1]) - centralpixel[1]) + delta # masking + + if self.config.maskmatrix is not None: + if self.config.maskmatrix.shape != data.shape: + raise errors.BackendError('The mask matrix does not have the same shape as the images') + data = numpy.ma.array(data, mask = ~self.config.maskmatrix) + gamma_range = gamma_range[self.config.xmask] delta_range = delta_range[self.config.ymask] intensity = self.apply_mask(data, self.config.xmask, self.config.ymask) @@ -574,7 +586,6 @@ class EH2(ID03Input): Phor = 1 - (numpy.sin(mu * numpy.pi / 180.) * numpy.sin(delta_grid * numpy.pi / 180.) * numpy.cos(gamma_grid* numpy.pi / 180.) + numpy.cos(mu* numpy.pi / 180.) * numpy.sin(gamma_grid* numpy.pi / 180.))**2 intensity /= Phor - return intensity, (wavelength, UB, gamma_range, delta_range, theta, mu, chi, phi) def get_point_params(self, scan, first, last): diff --git a/BINoculars/plot.py b/BINoculars/plot.py index 298ec52..713972c 100644 --- a/BINoculars/plot.py +++ b/BINoculars/plot.py @@ -101,7 +101,7 @@ def get_clipped_norm(data, clipping=0.0, log=True): return matplotlib.colors.Normalize(vmin, vmax)
-def plot(space, fig, ax, log=True, clipping=0.0, fit=None, norm=None, colorbar=True, labels=True, **plotopts):
+def plot(space, fig, ax, log=True, loglog = False, clipping=0.0, fit=None, norm=None, colorbar=True, labels=True, **plotopts):
if space.dimension == 1:
data = space.get_masked()
xrange = numpy.ma.array(space.axes[0][:], mask=data.mask)
@@ -109,12 +109,17 @@ def plot(space, fig, ax, log=True, clipping=0.0, fit=None, norm=None, colorbar=T if log:
p1 = ax.semilogy(xrange, data, 'wo', **plotopts)
p2 = ax.semilogy(xrange, fit, 'r', linewidth=2, **plotopts)
+ elif loglog:
+ p1 = ax.loglog(xrange, data, 'wo', **plotopts)
+ p2 = ax.loglog(xrange, fit, 'r', linewidth=2, **plotopts)
else:
p1 = ax.plot(xrange, data, 'wo', **plotopts)
p2 = ax.plot(xrange, fit, 'r', linewidth=2, **plotopts)
else:
if log:
p1 = ax.semilogy(xrange, data, **plotopts)
+ elif loglog:
+ p1 = ax.loglog(xrange, data, **plotopts)
else:
p1 = ax.plot(xrange, data, **plotopts)
p2 = []
diff --git a/BINoculars/space.py b/BINoculars/space.py index 44fafb5..b74c4a1 100644 --- a/BINoculars/space.py +++ b/BINoculars/space.py @@ -585,7 +585,13 @@ class Space(object): if len(coordinates) != len(self.axes): raise ValueError('dimension mismatch between coordinates and axes') - valid = numpy.isfinite(intensity) + if isinstance(intensity, numpy.ma.core.MaskedArray): + mask = intensity.mask + intensity = intensity.data + else: + mask = numpy.ones_like(intensity) + + valid = numpy.bitwise_and(numpy.isfinite(intensity), ~mask) intensity = intensity[valid] if not intensity.size: return @@ -610,6 +616,7 @@ class Space(object): labels n-tuple of axis labels coordinates n-tuple of data coordinate arrays intensity data intensity array""" + axes = tuple(Axis(coord.min(), coord.max(), res, label) for res, label, coord in zip(resolutions, labels, coordinates)) newspace = cls(axes) newspace.process_image(coordinates, intensity) @@ -763,10 +770,18 @@ def axis_offset(space, label, offset): return space.transform_coordinates((ax.res for ax in space.axes), (ax.label for ax in space.axes), transformation) def bkgsubtract(space, bkg): - bkg.photons = bkg.photons * space.contributions / bkg.contributions - bkg.photons[bkg.contributions == 0] = 0 - bkg.contributions = space.contributions - return space - bkg + if space.dimension == bkg.dimension: + bkg.photons = bkg.photons * space.contributions / bkg.contributions + bkg.photons[bkg.contributions == 0] = 0 + bkg.contributions = space.contributions + return space - bkg + else: + photons = numpy.broadcast_arrays(space.photons, bkg.photons)[1] + contributions = numpy.broadcast_arrays(space.contributions, bkg.contributions)[1] + bkg = Space(space.axes) + bkg.photons = photons + bkg.contributions = contributions + return bkgsubtract(space, bkg) def make_compatible(spaces): if not numpy.alen(numpy.unique(len(space.axes) for space in spaces)) == 1: @@ -387,9 +387,22 @@ class ProjectWidget(QtGui.QWidget): self.canvas = FigureCanvasQTAgg(self.figure) self.toolbar = HiddenToolbar(self.show_coords,self.update_sliders, self.canvas) - self.log = QtGui.QCheckBox('log', self) + self.lin = QtGui.QRadioButton('lin', self) + self.lin.setChecked(False) + QtCore.QObject.connect(self.lin, QtCore.SIGNAL("toggled(bool)"), self.plot) + + self.log = QtGui.QRadioButton('log', self) self.log.setChecked(True) - QtCore.QObject.connect(self.log, QtCore.SIGNAL("stateChanged(int)"), self.plot) + QtCore.QObject.connect(self.log, QtCore.SIGNAL("toggled(bool)"), self.plot) + + self.loglog = QtGui.QRadioButton('loglog', self) + self.loglog.setChecked(False) + QtCore.QObject.connect(self.loglog, QtCore.SIGNAL("toggled(bool)"), self.plot) + + self.loggroup = QtGui.QButtonGroup(self) + self.loggroup.addButton(self.lin) + self.loggroup.addButton(self.log) + self.loggroup.addButton(self.loglog) self.swap_axes = QtGui.QCheckBox('swap axes', self) self.swap_axes.setChecked(False) @@ -433,8 +446,6 @@ class ProjectWidget(QtGui.QWidget): self.control_widget = QtGui.QWidget(self) hbox = QtGui.QHBoxLayout() left = QtGui.QVBoxLayout() - radiobox = QtGui.QHBoxLayout() - left.addWidget(self.button_save) radiobox = QtGui.QHBoxLayout() @@ -444,13 +455,15 @@ class ProjectWidget(QtGui.QWidget): self.group.addButton(rb) radiobox.addWidget(rb) + radiobox.addWidget(self.lin) + radiobox.addWidget(self.log) + radiobox.addWidget(self.loglog) + datarangebox = QtGui.QHBoxLayout() - datarangebox.addWidget(self.log) datarangebox.addWidget(self.samerange) datarangebox.addWidget(self.legend) datarangebox.addWidget(self.swap_axes) - left.addLayout(radiobox) left.addLayout(datarangebox) left.addWidget(self.datarange) @@ -520,14 +533,16 @@ class ProjectWidget(QtGui.QWidget): self.datarange.setDisabled(True) self.samerange.setDisabled(True) self.swap_axes.setDisabled(True) + self.loglog.setEnabled(True) elif len(self.limitwidget.sliders) - len(self.projection) == 2: + self.loglog.setDisabled(True) self.datarange.setEnabled(True) self.samerange.setEnabled(True) self.swap_axes.setEnabled(True) self.plot() def get_norm(self, mi, ma): - log = self.log.checkState() + log = self.log.isChecked() rangemin = self.datarange.low() * 1.0 / self.datarange.maximum() rangemax = self.datarange.high() * 1.0 / self.datarange.maximum() @@ -546,7 +561,7 @@ class ProjectWidget(QtGui.QWidget): return matplotlib.colors.Normalize(vmin, vmax) def get_normlist(self): - log = self.log.checkState() + log = self.log.isChecked() same = self.samerange.checkState() if same: @@ -562,7 +577,8 @@ class ProjectWidget(QtGui.QWidget): self.parent.statusbar.clearMessage() self.figure_images = [] - log = self.log.checkState() + log = self.log.isChecked() + loglog = self.loglog.isChecked() plotcount = len(self.table.selection) plotcolumns = int(numpy.ceil(numpy.sqrt(plotcount))) @@ -587,7 +603,7 @@ class ProjectWidget(QtGui.QWidget): self.datamax = [] for space in spaces: data = space.get_masked().compressed() - if log: + if log or loglog: data = data[data > 0] self.datamin.append(data.min()) self.datamax.append(data.max()) @@ -616,7 +632,7 @@ class ProjectWidget(QtGui.QWidget): space = space.reorder(list(ax.label for ax in space.axes)[::-1]) self.ax.space = space - im = BINoculars.plot.plot(space,self.figure, self.ax, log = log,label = basename, norm = norm[i]) + im = BINoculars.plot.plot(space, self.figure, self.ax, log = log, loglog = loglog, label = basename, norm = norm[i]) self.figure_images.append(im) diff --git a/test/id03.py b/test/id03.py index 6b365e8..e7fed8c 100644 --- a/test/id03.py +++ b/test/id03.py @@ -1,6 +1,8 @@ from BINoculars.backends import id03 import BINoculars.util +import BINoculars.space import os +import numpy import unittest @@ -13,6 +15,8 @@ class TestCase(unittest.TestCase): cfg_unparsed['pixelsize'] = '0.055, 0.055' cfg_unparsed['imagefolder'] = specfile.replace('sixc_tutorial.spec', 'images') cfg_unparsed['centralpixel'] = '50 ,50' + numpy.save('mask.npy', numpy.identity(516)) + cfg_unparsed['maskmatrix'] = 'mask.npy' self.id03input = id03.EH2(cfg_unparsed) self.projection = id03.pixels({'resolution' : '1'}) @@ -20,10 +24,13 @@ class TestCase(unittest.TestCase): jobs = list(self.id03input.generate_jobs(['820'])) destination_opts = self.id03input.get_destination_options(['820']) imagedata = self.id03input.process_job(jobs[0]) - self.projection.project(*imagedata.next()[1]) + intensity, coords = imagedata.next() + projected = self.projection.project(*coords) + space = BINoculars.space.Space.from_image((1,1), ('x','y'), projected, intensity) + print space def tearDown(self): - pass + os.remove('mask.npy') if __name__ == '__main__': unittest.main() |