Beispiel #1
0
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))
Beispiel #2
0
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)
Beispiel #3
0
 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))
Beispiel #4
0
def test_autoBin():

    tests = [
        (np.random.random(100), (0, 1)),
        (np.random.random(100), (0, 0)),
        (np.random.random(1000), (0, 1)),
        (np.random.randint(1, 100, 100), (1, 100)),
        (np.random.randint(1, 1000, 1000), (1, 1000)),
    ]

    for data, drange in tests:
        assert hseries.autoBin(data, drange) > 0
    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)
Beispiel #6
0
 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