def test_HistogramSeries(): class Overlay(object): def name(self): return str(id(self)) hs = hseries.HistogramSeries(Overlay(), None, None, None) data1 = np.random.randint(1, 1000, 1000) data2 = np.random.randint(-1000, 1000, 1000) hs.setHistogramData(data1, 'data1') # HistogramSeries.dataRange upper bound is exclusive d1min, d1max = data1.min(), data1.max() drange = (d1min, d1max + (d1max - d1min) / 10000.0) nbins = hseries.autoBin(data1, drange) hx, hy, nvals = hseries.histogram(data1, nbins, drange, drange) gotx, goty = hs.getData() assert hs.nbins == nbins assert hs.numHistogramValues == nvals assert np.all(np.isclose(hx, gotx)) assert np.all(np.isclose(hy, goty)) hs.autoBin = False hs.nbins = 124 hx, hy, nvals = hseries.histogram(data1, 124, drange, drange) gotx, goty = hs.getData() assert np.all(np.isclose(hx, gotx)) assert np.all(np.isclose(hy, goty)) drange = (10, 80.1) hs.dataRange.x = 10, 80.1 hx, hy, nvals = hseries.histogram(data1, 124, drange, drange) gotx, goty = hs.getData() assert np.all(np.isclose(hx, gotx)) assert np.all(np.isclose(hy, goty)) hs.setHistogramData(data2, 'data2') hs.autoBin = True hs.ignoreZeros = False d2min, d2max = data2.min(), data2.max() drange = d2min, d2max + (d2max - d2min) / 10000.0 nbins = hseries.autoBin(data2, drange) hx, hy, nvals = hseries.histogram(data2, nbins, drange, drange) gotx, goty = hs.getData() assert np.all(np.isclose(hx, gotx)) assert np.all(np.isclose(hy, goty))
def _test_ComplexHistogramSeries(panel, overlayList, displayCtx): displayCtx = panel.displayCtx data = np.random.randint(1, 255, (10, 10, 10)) + \ 1j * np.random.randint(1, 255, (10, 10, 10)) data.flags.writeable = False data = np.array(data, dtype=np.complex64) img = Image(data, xform=np.eye(4)) overlayList.append(img) realYield() hs = panel.getDataSeries(img) hs.plotReal = True hs.plotImaginary = True hs.plotMagnitude = True hs.plotPhase = True his, hms, hps = hs.extraSeries() real = data.real imag = data.imag mag = (real**2 + imag**2)**0.5 phase = np.arctan2(imag, real) for hdata, hhs in zip([real, imag, mag, phase], [hs, his, hms, hps]): dmin, dmax = hdata.min(), hdata.max() drange = dmin, dmax + (dmax - dmin) / 10000 nbins = hseries.autoBin(hdata, drange) hx, hy, _ = hseries.histogram(hdata, nbins, drange, drange) gotx, goty = hhs.getData() assert np.all(gotx == hx) assert np.all(goty == hy)
def check(gotx, goty, img, vol): data = img[..., vol] dmin = data.min() dmax = data.max() drange = dmin, dmax + (dmax - dmin) / 10000 nbins = hseries.autoBin(data, drange) expx, expy, nvals = hseries.histogram(data, nbins, drange, drange) assert np.all(np.isclose(gotx, expx)) assert np.all(np.isclose(goty, expy))
def __addROIHistogram(self): """Prompts the user to select an ROI mask, calculates the histogram of that mask on the currently selected overlay, and adds the result to the ``HistogramPanel``. """ overlay = self.displayCtx.getSelectedOverlay() opts = self.displayCtx.getOpts(overlay) roiOptions = self.__roiOptions frame = wx.GetApp().GetTopWindow() msg = strings.messages[self, 'selectMask'].format(overlay.name) title = strings.titles[self, 'selectMask'].format(overlay.name) dlg = addmaskdataseries.MaskDialog(frame, [o.name for o in roiOptions], title=title, message=msg, checkbox=False) if dlg.ShowModal() != wx.ID_OK: return maskimg = roiOptions[dlg.GetChoice()] mask = maskimg[:] > 0 if overlay.ndim > 3: data = overlay[opts.index()][mask] else: data = overlay[mask] count = self.__plotPanel.histType == 'count' drange = (np.nanmin(data), np.nanmax(data)) nbins = histogramseries.autoBin(data, drange) xdata, ydata, _ = histogramseries.histogram(data, nbins, drange, drange, includeOutliers=False, count=count) ds = dataseries.DataSeries(overlay, self.overlayList, self.displayCtx, self.__plotPanel) ds.colour = self.__plotPanel.getOverlayPlotColour(overlay) ds.lineStyle = self.__plotPanel.getOverlayPlotStyle(overlay) ds.lineWidth = 2 ds.alpha = 1 ds.label = '{} [mask: {}]'.format(overlay.name, maskimg.name) # We have to run the data through # prepareDataSeries to preprocess # (e.g. smooth) it ds.setData(xdata, ydata) ds.setData(*self.__plotPanel.prepareDataSeries(ds)) self.__plotPanel.dataSeries.append(ds)
def test_histogram(): def check(bins, counts): for i, blo in enumerate(bins[:-1]): count = counts[i] bhi = bins[i + 1] if i + 1 == len(hx) - 1: exp = len([v for v in data if v >= blo and v <= bhi]) else: exp = len([v for v in data if v >= blo and v < bhi]) assert count == exp data = np.random.randint(1, 100, 1000) dmin = data.min() dmax = data.max() hx, hy, nvals = hseries.histogram(data, 10, (dmin, dmax), (dmin, dmax)) assert np.all(hx == np.linspace(dmin, dmax, 11)) assert len(hy) == 10 assert nvals == 1000 check(hx, hy) hx, hy, nvals = hseries.histogram(data, 10, (dmin, dmax), (dmin, dmax), count=False) check(hx, hy * nvals) hx, hy, nvals = hseries.histogram(data, 8, (10, 90), (dmin, dmax)) assert np.all(hx == np.linspace(10, 90, 9)) assert len(hy) == 8 check(hx, hy) hx, hy, nvals = hseries.histogram(data, 8, (10, 90), (dmin, dmax), includeOutliers=True) expbins = list(np.linspace(10, 90, 9)) expbins[0] = dmin expbins[-1] = dmax assert np.all(hx == expbins) assert len(hy) == 8 check(hx, hy)
def calc(d): dmin, dmax = d.min(), d.max() drange = dmin, dmax + (dmax - dmin) / 10000 nbins = hseries.autoBin(d, drange) dx, dy, _ = hseries.histogram(d, nbins, drange, drange) return dx, dy