def test_regionradii(self):
        # Create a volume of flat superpixels, where every slice
        # is the same (except for the actual sp ids)
        num_sp_per_slice = 200
        slice_superpixels = generate_random_voronoi((100,200), num_sp_per_slice)
        
        superpixels = np.zeros( shape=((10,) + slice_superpixels.shape), dtype=np.uint32 )
        for z in range(10):
            superpixels[z] = slice_superpixels + z*num_sp_per_slice
        superpixels = vigra.taggedView(superpixels, 'zyx')

        rag_flat = Rag( superpixels, flat_superpixels=True )
        
        values = np.random.random(size=(superpixels.shape)).astype(np.float32)
        values = vigra.taggedView(values, 'zyx')
        
        flat_features_df = rag_flat.compute_features(values, ['standard_flatedge_regionradii'], edge_group='z')

        # Now compute the radii using a normal 'dense' rag
        rag_dense = Rag( superpixels )
        dense_features_df = rag_dense.compute_features(values, ['edgeregion_edge_regionradii'])

        # Both methods should be reasonably close.
        combined_features_df = pd.merge(flat_features_df, dense_features_df, how='left', on=['sp1', 'sp2'])
        assert np.isclose(combined_features_df['standard_flatedge_regionradii_0'], combined_features_df['edgeregion_edge_regionradii_0'], atol=0.001).all()
        assert np.isclose(combined_features_df['standard_flatedge_regionradii_1'], combined_features_df['edgeregion_edge_regionradii_1'], atol=0.001).all()
    def test_quantiles(self):
        # Create a volume of flat superpixels, where every slice 
        # is the same (except for the actual sp ids)
        num_sp_per_slice = 200
        slice_superpixels = generate_random_voronoi((100,200), num_sp_per_slice)
        
        superpixels = np.zeros( shape=((10,) + slice_superpixels.shape), dtype=np.uint32 )
        for z in range(10):
            superpixels[z] = slice_superpixels + z*num_sp_per_slice
        superpixels = vigra.taggedView(superpixels, 'zyx')

        rag_flat = Rag( superpixels, flat_superpixels=True )
        
        values = np.random.random(size=(superpixels.shape)).astype(np.float32)
        values = vigra.taggedView(values, 'zyx')
        
        flat_features_df = rag_flat.compute_features(values, ['standard_flatedge_quantiles'], edge_group='z')
        flat_features_df2 = rag_flat.compute_features(values, ['standard_edge_quantiles'], edge_group='yx')
        
        # Rename columns
        flat_features_df2.columns = flat_features_df.columns.values
        flat_features_df = pd.concat( (flat_features_df, flat_features_df2), axis=0 )

        # Now compute the quantiles using a normal 'dense' rag
        rag_dense = Rag( superpixels )
        dense_features_df = rag_dense.compute_features(values, ['standard_edge_quantiles'])

        all_features_df = pd.merge(dense_features_df, flat_features_df, how='left', on=['sp1', 'sp2'])
        assert (all_features_df['standard_edge_quantiles_0'] == all_features_df['standard_flatedge_quantiles_0']).all()
        assert (all_features_df['standard_edge_quantiles_100'] == all_features_df['standard_flatedge_quantiles_100']).all()

        # Due to the way histogram ranges are computed, we can't expect quantiles_10 to match closely
        # ... but quantiles_50 seems to be good.
        assert np.isclose(all_features_df['standard_edge_quantiles_50'], all_features_df['standard_edge_quantiles_50']).all()
예제 #3
0
def deepDetexturize(
    srcImg,
    img,
    nIteration=10,
    **kwargs
):
    hist=img.copy()
    mixIn=None
    for i in range(nIteration):

        newImg = detexturize(img=hist,**kwargs)
        newImgIter = newImg.copy()
        newImgIter = vigra.taggedView(newImgIter,'xyc')
        if i == 0:
            mixIn=newImg.copy()
        if i !=0 :
            newImg = numpy.concatenate([newImg,mixIn],axis=2)
            newImg     = vigra.taggedView(newImg,'xyc')
        hist   = histogram(newImg,r=2,sigma=[3.0,3.0])

        f = pylab.figure()
        for n, iterImg in enumerate([srcImg,newImgIter]):
            #f.add_subplot(2, 1, n)  # this line outputs images on top of each other
            f.add_subplot(1, 2, n)  # this line outputs images side-by-side
            if iterImg.ndim==2:
                pylab.imshow(numpy.swapaxes(norm01(iterImg),0,1),cmap='gray')
            else :
                pylab.imshow(numpy.swapaxes(norm01(iterImg),0,1))
        pylab.show()
    def testBasic(self):
        features = numpy.indices( (100,100) ).astype(numpy.float) + 0.5
        features = numpy.rollaxis(features, 0, 3)
        features = vigra.taggedView(features, 'xyc')
        labels = numpy.zeros( (100,100,1), dtype=numpy.uint8 )
        labels = vigra.taggedView(labels, 'xyc')
        
        labels[10,10] = 1
        labels[10,11] = 1
        labels[20,20] = 2
        labels[20,21] = 2

        graph = Graph()
        opTrain = OpTrainClassifierBlocked( graph=graph )
        opTrain.ClassifierFactory.setValue( VigraRfLazyflowClassifierFactory(10) )
        opTrain.Images.resize(1)
        opTrain.Labels.resize(1)
        opTrain.nonzeroLabelBlocks.resize(1)
        opTrain.Images[0].setValue( features )
        opTrain.Labels[0].setValue( labels )
        opTrain.nonzeroLabelBlocks[0].setValue(0) # Dummy for now (not used by operator yet)
        opTrain.MaxLabel.setValue(2)
                
        opTrain.Labels[0].setDirty( numpy.s_[10:11, 10:12] )
        opTrain.Labels[0].setDirty( numpy.s_[20:21, 20:22] )
        opTrain.Labels[0].setDirty( numpy.s_[30:31, 30:32] )
        
        trained_classifier = opTrain.Classifier.value
        
        # This isn't much of a test at the moment...
        assert isinstance( trained_classifier, VigraRfLazyflowClassifier )
    def testBasic2(self):
        a = numpy.ones((100, 101, 102))
        a = a[..., None]
        a = vigra.taggedView(a, "tyxc")

        graph = Graph()

        opPrep = OpArrayPiper(graph=graph)
        opPrep.Input.setValue(a)

        op = OpNansheExtractF0Cached(graph=graph)
        op.Input.connect(opPrep.Output)

        op.HalfWindowSize.setValue(20)
        op.WhichQuantile.setValue(0.5)
        op.TemporalSmoothingGaussianFilterStdev.setValue(5.0)
        op.SpatialSmoothingGaussianFilterStdev.setValue(5.0)
        op.Bias.setValue(100)
        op.BiasEnabled.setValue(True)

        b = op.dF_F[...].wait()
        b = vigra.taggedView(b, "tyxc")

        assert(a.shape == b.shape)

        assert((b == 0).all())
예제 #6
0
    def setUp(self):

        self.nx = 50
        self.ny = 51
        self.nz = 52
        self.nc = 3

        clusters = self.generateData((self.nx, self.ny, self.nz))
        self.data = clusters[0] + clusters[1] + clusters[2] + clusters[3] + clusters[4]
        self.data = self.data.reshape(self.data.shape+(1,))
        self.data = vigra.taggedView(self.data, axistags='xyzc')

        self.minSize = 5  # first cluster doesn't pass this
        self.maxSize = 30  # fourth and fifth cluster don't pass this
        self.highThreshold = 0.65  # third cluster doesn't pass
        self.lowThreshold = 0.1

        # Replicate the 4d data for multiple time slices
        self.data5d = numpy.concatenate(3*(self.data[numpy.newaxis, ...],),
                                        axis=0)
        self.data5d = vigra.taggedView(self.data5d, axistags="txyzc")

        self.sigma = {'x': 0.3, 'y': 0.3, 'z': 0.3}
        #pre-smooth 4d data
        self.data = vigra.filters.gaussianSmoothing(
            self.data.astype(numpy.float32), 0.3)
예제 #7
0
    def testMargin(self):
        graph = Graph()
        vol = np.zeros((100, 110, 10), dtype=np.float32)
        # draw a big plus sign
        vol[50:70, :, :] = 1.0
        vol[:, 60:80, :] = 1.0
        vol = vigra.taggedView(vol, axistags="xyz").withAxes(*"txyzc")
        labels = np.zeros((100, 110, 10), dtype=np.uint32)
        labels[45:75, 55:85, 3:4] = 1
        labels = vigra.taggedView(labels, axistags="xyz").withAxes(*"txyzc")

        op = OpObjectsSegment(graph=graph)
        piper = OpArrayPiper(graph=graph)
        piper.Input.setValue(vol)
        op.Prediction.connect(piper.Output)
        op.LabelImage.setValue(labels)

        # without margin
        op.Margin.setValue(np.asarray((0, 0, 0)))
        out = op.Output[...].wait()
        out = vigra.taggedView(out, axistags=op.Output.meta.axistags)
        out = out.withAxes(*"xyz")
        vol = vol.withAxes(*"xyz")
        assert_array_equal(out[50:70, 60:80, 3] > 0, vol[50:70, 60:80, 3] > 0.5)
        assert np.all(out[:45, ...] == 0)

        # with margin
        op.Margin.setValue(np.asarray((5, 5, 0)))
        out = op.Output[...].wait()
        out = vigra.taggedView(out, axistags=op.Output.meta.axistags)
        out = out.withAxes(*"xyz")
        assert_array_equal(out[45:75, 55:85, 3] > 0, vol[45:75, 55:85, 3] > 0.5)
        assert np.all(out[:40, ...] == 0)
    def testBasic3(self):
        a = numpy.ones((1, 100, 101))
        a = a[..., None]
        a = vigra.taggedView(a, "tyxc")

        r = numpy.array([[0, 0, 0], [1, 3, 4]]).T.copy()

        ar = a.copy()
        for each_r in r:
            nanshe.util.xnumpy.index_axis_at_pos(nanshe.util.xnumpy.index_axis_at_pos(ar, 0, each_r[0]), -1, each_r[-1])[:] = 0


        graph = Graph()

        opPrep = OpArrayPiper(graph=graph)
        opPrep.Input.setValue(ar)

        op = OpNansheRemoveZeroedLinesCached(graph=graph)
        op.Input.connect(opPrep.Output)

        op.ErosionShape.setValue([21, 1])
        op.DilationShape.setValue([1, 3])


        b = op.Output[...].wait()
        b = vigra.taggedView(b, "tyxc")

        assert((a == b).all())
예제 #9
0
    def testOptimalInit(self):
        x = np.asarray([0, 0, 1, 1, .5])
        y = np.asarray([0, 1, 0, 1, .5])
        vol = np.zeros((5, 2), dtype=np.float32)
        vol[:, 0] = x
        vol[:, 1] = y
        vol = vigra.taggedView(vol, axistags='tc')

        z = x*(1-y) + y*(1-x)
        z = vigra.taggedView(z[:, np.newaxis], axistags='tc')

        nvis = 2
        ncent = 5
        layers = [Sigmoid(layer_name='bumps', irange=0, dim=2*nvis*ncent),
                  Sigmoid(layer_name='cents', irange=0, dim=ncent),
                  Linear(layer_name='out', irange=0, dim=1)]
        mlp = MLP(layers=layers, nvis=nvis)

        init = OptimalInitializer.build({}, graph=Graph())
        init.Data.setValue(vol)
        init.Target.setValue(z)
        init.init_model(mlp)

        op = OpForwardLayers(layers[:], graph=Graph())
        op.Input.setValue(vol)

        z_pred = op.Output[..., 0].wait().squeeze()
        assert max(z_pred[0], z_pred[3]) < z_pred[4]
        assert min(z_pred[1], z_pred[2]) > z_pred[4]
    def testCircular(self):
        g = Graph()

        op = OpLazyCC(graph=g)
        op.ChunkShape.setValue((3, 3, 1))

        vol = np.asarray(
            [
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 1, 1, 1, 0, 1, 1, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 1, 1, 1, 1, 1, 1, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
            ],
            dtype=np.uint8,
        )
        vol1 = vigra.taggedView(vol, axistags="yx")
        vol2 = vigra.taggedView(vol, axistags="xy")
        vol3 = vigra.taggedView(np.flipud(vol), axistags="yx")
        vol4 = vigra.taggedView(np.flipud(vol), axistags="xy")

        for v in (vol1, vol2, vol3, vol4):
            op.Input.setValue(v)
            for x in [0, 3, 6]:
                for y in [0, 3, 6]:
                    if x == 3 and y == 3:
                        continue
                    op.Input.setDirty(slice(None))
                    out = op.Output[x : x + 3, y : y + 3].wait()
                    print(out.squeeze())
                    assert out.max() == 1
예제 #11
0
        def process_timestep( t ):
            # Request input and resize it.            
            # FIXME: This is not quite correct.  We should request a halo that is wide enough 
            #        for the BSpline used by resize(). See vigra docs for BSlineBase.radius()        
            step_input_roi = copy.copy(input_roi)
            step_input_roi[0][0] = t
            step_input_roi[1][0] = t+1

            step_input_data = self.Input( *step_input_roi ).wait()
            step_input_data = vigra.taggedView( step_input_data, 'tzyxc' )
            
            step_shape_4d = numpy.array(step_input_data[0].shape)
            step_shape_4d_nochannel = step_shape_4d[:-1]
            squeezed_slicing = numpy.where(step_shape_4d_nochannel == 1, 0, slice(None))
            squeezed_slicing = tuple(squeezed_slicing) + (slice(None),)
            
            step_input_squeezed = step_input_data[0][squeezed_slicing]
            result_step = result[t][squeezed_slicing]
            # vigra assumes wrong axis order if we don't specify one explicitly here...
            result_step = vigra.taggedView( result_step, step_input_squeezed.axistags )
            
            if self.Input.meta.dtype == numpy.float32:
                vigra.sampling.resize( step_input_squeezed, out=result_step )
            else:
                step_input_squeezed = step_input_squeezed.astype( numpy.float32 )
                result_float = vigra.sampling.resize(step_input_squeezed, shape=result_step.shape[:-1])
                result_step[:] = result_float.round()
예제 #12
0
    def relabel(key_label_mapping):
        import numpy

        (subvolume, labels) = key_label_mapping

        # grab broadcast offset
        offset = numpy.uint64( subvolume_offsets.value[subvolume.sv_index] )

        # check for body mask labels and protect from renumber
        fix_bodies = []
        
        labels = labels + offset 
        
        # make sure 0 is 0
        labels[labels == offset] = 0

        # create default map 
        labels_view = vigra.taggedView(labels.astype(numpy.uint64), 'zyx')
        mapping_col = numpy.sort( vigra.analysis.unique(labels_view) )
        label_mappings = dict(zip(mapping_col, mapping_col))
       
        # create maps from merge list
        for mapping in master_merge_list.value:
            if mapping[0] in label_mappings:
                label_mappings[mapping[0]] = mapping[1]

        # apply maps
        new_labels = numpy.empty_like( labels, dtype=numpy.uint64 )
        new_labels_view = vigra.taggedView(new_labels, 'zyx')
        vigra.analysis.applyMapping(labels_view, label_mappings, allow_incomplete_mapping=True, out=new_labels_view)
        return (subvolume, new_labels)
    def testBasic2(self):
        a = numpy.zeros((2,2,2,), dtype=int)
        a[1,1,1] = 1
        a[0,0,0] = 1
        a = a[..., None]
        a = vigra.taggedView(a, "tyxc")

        expected_b = a.astype(float)
        expected_b = vigra.taggedView(expected_b, "tyxc")


        graph = Graph()
        op = OpConvertTypeCached(graph=graph)

        opPrep = OpArrayPiper(graph=graph)
        opPrep.Input.setValue(a)

        op.Input.connect(opPrep.Output)
        op.Dtype.setValue(float)

        b = op.Output[...].wait()
        b = vigra.taggedView(b, "tyxc")


        assert((b == expected_b).all())
예제 #14
0
    def execute(self, slot, subindex, roi, result):
        window = self.WindowSize.value

        if slot is self.Output:
            padding_size = max(window - 1 - roi.start[0], 0)
            padding = np.zeros((padding_size,), dtype=np.float32)
            new_start = (roi.start[0] - window + 1 + padding_size,)
            new_stop = (roi.stop[0],)
            new_roi = SubRegion(self.Input, start=new_start, stop=new_stop)
            input_data = self.Input.get(new_roi).wait()
            input_data = vigra.taggedView(input_data,
                                          axistags=self.Input.meta.axistags)
            input_data = input_data.withAxes('t').view(np.ndarray)
            input_data = np.concatenate((padding, input_data))

            res_view = vigra.taggedView(result,
                                        axistags=self.Output.meta.axistags)
            res_view = res_view.withAxes('t')

            self.apply_window_function(input_data, window, res_view)
        elif slot is self.Valid:
            result[:] = 1
            first_valid_index = window - 1
            num_invalid = first_valid_index - roi.start[0]
            if num_invalid > 0:
                result[:num_invalid] = 0
예제 #15
0
    def test4dAnd5d(self):
        g = Graph()

        oper = OpThresholdTwoLevels5d(graph=g)
        oper.InputImage.setValue(self.data.withAxes(*'txyzc'))
        oper.MinSize.setValue(self.minSize)
        oper.MaxSize.setValue(self.maxSize)
        oper.HighThreshold.setValue(self.highThreshold)
        oper.LowThreshold.setValue(self.lowThreshold)

        output = oper.Output[:].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)

        self.checkResult(output)

        oper5d = OpThresholdTwoLevels(graph=g)
        oper5d.InputImage.setValue(self.data5d)
        oper5d.MinSize.setValue(self.minSize)
        oper5d.MaxSize.setValue(self.maxSize)
        oper5d.HighThreshold.setValue(self.highThreshold)
        oper5d.LowThreshold.setValue(self.lowThreshold)
        oper5d.SmootherSigma.setValue({'x': 0, 'y': 0, 'z': 0})
        oper5d.Channel.setValue(0)
        oper5d.CurOperator.setValue(1)

        out5d = oper5d.Output[0:1, ...].wait()
        out5d = vigra.taggedView(out5d, axistags=oper5d.Output.meta.axistags)

        self.checkResult(out5d)
        numpy.testing.assert_array_equal(out5d, output)
예제 #16
0
    def execute(self, slot, subindex, roi, result):
        assert len(roi.start) == len(roi.stop) == len(self.Output.meta.shape)
        assert slot == self.Output

        t_ind = self.RawVolume.axistags.index('t')
        assert t_ind < len(self.RawVolume.meta.shape)

        # loop over requested time slices
        for res_t_ind, t in enumerate(xrange(roi.start[t_ind],
                                             roi.stop[t_ind])):
            
            # Process entire spatial volume
            s = [slice(None) for i in range(len(self.RawVolume.meta.shape))]
            s[t_ind] = slice(t, t+1)
            s = tuple(s)
            rawVolume = self.RawVolume[s].wait()
            rawVolume = vigra.taggedView(
                rawVolume, axistags=self.RawVolume.meta.axistags)
            labelVolume = self.LabelVolume[s].wait()
            labelVolume = vigra.taggedView(
                labelVolume, axistags=self.LabelVolume.meta.axistags)
    
            # Convert to 4D (preserve axis order)
            axes4d = self.RawVolume.meta.getTaggedShape().keys()
            axes4d = filter(lambda k: k in 'xyzc', axes4d)
            rawVolume = rawVolume.withAxes(*axes4d)
            labelVolume = labelVolume.withAxes(*axes4d)
            acc = self._extract(rawVolume4d, labelVolume4d)
            result[res_t_ind] = acc
        
        return result
예제 #17
0
    def testAgainstOwn(self):
        g = Graph()
        oper = OpThresholdTwoLevels(graph=g)
        oper.InputImage.setValue(self.data5d)
        oper.MinSize.setValue(self.minSize)
        oper.MaxSize.setValue(self.maxSize)
        oper.HighThreshold.setValue(self.highThreshold)
        oper.LowThreshold.setValue(self.lowThreshold)
        oper.SmootherSigma.setValue({'x': 0, 'y': 0, 'z': 0})
        oper.CurOperator.setValue(1)

        output = oper.Output[0, ..., 0].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*'xyz')

        output2 = self.thresholdTwoLevels(self.data5d[0, ..., 0])
        output2 = vigra.taggedView(output2, axistags='xyz')

        ref = output*output2
        idx = np.where(ref != output)
        print(oper.Output.meta.getTaggedShape())
        print(output.shape)
        print(idx)
        print(output[idx])
        print(output2[idx])
        numpy.testing.assert_array_almost_equal(ref, output)
예제 #18
0
    def _label(self, roi, result):
        result = vigra.taggedView(result, axistags=self.Output.meta.axistags)
        # get the background values
        bg = self.Background[...].wait()
        bg = vigra.taggedView(bg, axistags=self.Background.meta.axistags)
        bg = bg.withAxes(*'ct')
        assert np.all(self.Background.meta.shape[3:] ==
                      self.Input.meta.shape[3:]),\
            "Shape of background values incompatible to shape of Input"

        # do labeling in parallel over channels and time slices
        pool = RequestPool()

        start = np.asarray(roi.start, dtype=np.int)
        stop = np.asarray(roi.stop, dtype=np.int)
        for ti, t in enumerate(range(roi.start[4], roi.stop[4])):
            start[4], stop[4] = t, t+1
            for ci, c in enumerate(range(roi.start[3], roi.stop[3])):
                start[3], stop[3] = c, c+1
                newRoi = SubRegion(self.Output,
                                   start=tuple(start), stop=tuple(stop))
                resView = result[..., ci, ti].withAxes(*'xyz')
                req = Request(partial(self._label3d, newRoi,
                                      bg[c, t], resView))
                pool.add(req)

        logger.debug(
            "{}: Computing connected components for ROI {} ...".format(
                self.name, roi))
        pool.wait()
        pool.clean()
        logger.debug("{}: Connected components computed.".format(
            self.name))
예제 #19
0
    def execute(self, slot, subindex, roi, result):

        if slot == self.Detector:
            result = self.dumps()
            return result

        # sanity check
        assert self.DetectionMethod.value in ['svm', 'classic'], \
            "Unknown detection method '{}'".format(self.DetectionMethod.value)

        # prefill result
        resultZYXCT = vigra.taggedView(
            result, self.InputVolume.meta.axistags).withAxes(*'zyxct')

        # acquire data
        data = self.InputVolume.get(roi).wait()
        dataZYXCT = vigra.taggedView(
            data, self.InputVolume.meta.axistags).withAxes(*'zyxct')

        # walk over time and channel axes
        for t in range(dataZYXCT.shape[4]):
            for c in range(dataZYXCT.shape[3]):
                resultZYXCT[..., c, t] = \
                    self._detectMissing(dataZYXCT[..., c, t])

        return result
    def testBasic(self):
        features = numpy.indices((100, 100)).astype(numpy.float32) + 0.5
        features = numpy.rollaxis(features, 0, 3)
        features = vigra.taggedView(features, "xyc")
        labels = numpy.zeros((100, 100, 1), dtype=numpy.uint8)
        labels = vigra.taggedView(labels, "xyc")

        labels[10, 10] = 1
        labels[10, 11] = 1
        labels[20, 20] = 2
        labels[20, 21] = 2

        graph = Graph()
        opFeatureMatrixCache = OpFeatureMatrixCache(graph=graph)
        opFeatureMatrixCache.FeatureImage.setValue(features)
        opFeatureMatrixCache.LabelImage.setValue(labels)

        opFeatureMatrixCache.LabelImage.setDirty(numpy.s_[10:11, 10:12])
        opFeatureMatrixCache.LabelImage.setDirty(numpy.s_[20:21, 20:22])
        opFeatureMatrixCache.LabelImage.setDirty(numpy.s_[30:31, 30:32])

        opTrain = OpTrainClassifierFromFeatureVectors(graph=graph)
        opTrain.ClassifierFactory.setValue(ParallelVigraRfLazyflowClassifierFactory(100))
        opTrain.MaxLabel.setValue(2)
        opTrain.LabelAndFeatureMatrix.connect(opFeatureMatrixCache.LabelAndFeatureMatrix)

        assert opTrain.Classifier.ready()

        trained_classifier = opTrain.Classifier.value

        # This isn't much of a test at the moment...
        assert isinstance(
            trained_classifier, ParallelVigraRfLazyflowClassifier
        ), "classifier is of the wrong type: {}".format(type(trained_classifier))
    def testBasic2(self):
        a = numpy.zeros((2,2,2,))
        a[1,1,1] = 1
        a[0,0,0] = 1
        a = a[..., None]
        a = vigra.taggedView(a, "tyxc")

        expected_b = a.mean(axis=0)
        expected_b = vigra.taggedView(expected_b, "yxc")


        graph = Graph()
        op = OpMeanProjectionCached(graph=graph)

        opPrep = OpArrayPiper(graph=graph)
        opPrep.Input.setValue(a)

        op.Input.connect(opPrep.Output)
        op.Axis.setValue(0)

        b = op.Output[...].wait()
        b = vigra.taggedView(b, "yxc")


        assert((b == expected_b).all())
예제 #22
0
def test2():
    vol = np.zeros((2, 3, 4, 5, 6), dtype=np.uint8)
    vol1 = vigra.taggedView(vol, axistags='txyzc')
    vol2 = vigra.taggedView(vol, axistags='czyxt')

    g = Graph()

    pipe1 = OpArrayPiper(graph=g)
    pipe1.Input.setValue(vol1)
    pipe2 = OpArrayPiper(graph=g)
    pipe2.Input.setValue(vol2)

    op = OpThresholdTwoLevels(graph=g)
    printer = Printer(graph=g)
    printer.Input.connect(op.Output)

    op.InputImage.connect(pipe1.Output)
    #print(op.Output.meta.getTaggedShape())
    #print(op.CachedOutput.meta.getTaggedShape())

    print("")

    #op.InputImage.connect(pipe2.Output)
    op.InputImage.meta.axistags = vol2.axistags
    op.InputImage._sig_changed()
    def testBasic(self):
        features = numpy.indices( (100,100) ).astype(numpy.float32) + 0.5
        features = numpy.rollaxis(features, 0, 3)
        features = vigra.taggedView(features, 'xyc')
        labels = numpy.zeros( (100,100,1), dtype=numpy.uint8 )
        labels = vigra.taggedView(labels, 'xyc')
        
        labels[10,10] = 1
        labels[10,11] = 1
        labels[20,20] = 2
        labels[20,21] = 2
        
        graph = Graph()
        opFeatureMatrixCache = OpFeatureMatrixCache(graph=graph)
        opFeatureMatrixCache.FeatureImage.setValue(features)
        opFeatureMatrixCache.LabelImage.setValue(labels)
        opFeatureMatrixCache.NonZeroLabelBlocks.setValue(0)
        
        labels_and_features = opFeatureMatrixCache.LabelAndFeatureMatrix.value
        assert labels_and_features.shape == (0,3), "Empty feature matrix has wrong shape: {}".format( labels_and_features.shape )
        
        opFeatureMatrixCache.LabelImage.setDirty( numpy.s_[10:11, 10:12] )
        opFeatureMatrixCache.LabelImage.setDirty( numpy.s_[20:21, 20:22] )
        opFeatureMatrixCache.LabelImage.setDirty( numpy.s_[30:31, 30:32] )

        labels_and_features = opFeatureMatrixCache.LabelAndFeatureMatrix.value
        assert labels_and_features.shape == (4,3)
        assert (labels_and_features[:,0] == 1).sum() == 2
        assert (labels_and_features[:,0] == 2).sum() == 2

        # Can't check for equality because feature blocks can be in a random order.
        # Just check that all features are present, regardless of order.
        for feature_vec in [[10.5, 10.5], [10.5, 11.5], [20.5, 20.5], [20.5, 21.5]]:
            assert feature_vec in labels_and_features[:,1:]
예제 #24
0
    def testFunnyAxes(self):
        vol = self.data.withAxes(*'ztxcy')
        g = Graph()
        oper = OpThresholdOneLevel(graph=g)
        oper.MinSize.setValue(self.minSize)
        oper.MaxSize.setValue(self.maxSize)
        oper.Threshold.setValue(0.5)
        oper.InputImage.setValue(vol)

        output = oper.Output[:].wait()
        assert numpy.all(output.shape == vol.shape)

        clusters = self.generateData((self.nx, self.ny, self.nz, self.nc))
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*'xyzc')

        cluster1 = numpy.logical_and(output, clusters[0])
        assert numpy.any(cluster1 != 0)

        oper.MinSize.setValue(5)
        output = oper.Output[:].wait()
        cluster1 = numpy.logical_and(output, clusters[0])
        assert numpy.all(cluster1 == 0)

        cluster4 = numpy.logical_and(output.squeeze(), clusters[3])
        assert numpy.all(cluster4 == 0)

        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert numpy.all(cluster5 == 0)
        oper.Threshold.setValue(0.2)
        output = oper.Output[:].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*'xyzc')
        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert numpy.any(cluster5 != 0)
예제 #25
0
    def testSingletonZ(self):
        vol = np.zeros((82, 70, 1, 5, 5), dtype=np.uint8)
        vol = vigra.taggedView(vol, axistags='xyzct')

        blocks = np.zeros(vol.shape, dtype=np.uint8)
        blocks[30:50, 40:60, :, 2:4, 3:5] = 1
        blocks[30:50, 40:60, :, 2:4, 0:2] = 2
        blocks[60:70, 30:40, :, :, :] = 3

        vol[blocks == 1] = 255
        vol[blocks == 2] = 255
        vol[blocks == 3] = 255

        op = OpLabelVolume(graph=Graph())
        op.Method.setValue(self.method)
        op.Input.setValue(vol)

        out = op.Output[...].wait()
        tags = op.Output.meta.getTaggedShape()
        print(tags)
        out = vigra.taggedView(out, axistags="".join([s for s in tags]))

        for c in range(out.shape[3]):
            for t in range(out.shape[4]):
                print("t={}, c={}".format(t, c))
                assertEquivalentLabeling(blocks[..., c, t], out[..., c, t])
예제 #26
0
 def setUp(self):
     g = Graph()
     rawimg = np.random.randint(0, 255, (2, 10, 10, 10, 1))
     binimg = rawimg>100
     cc0 = vigra.analysis.labelVolumeWithBackground(binimg[0,:, :, :, 0].astype(np.uint8))
     cc1 = vigra.analysis.labelVolumeWithBackground(binimg[1,:, :, :, 0].astype(np.uint8))
     nobj = np.max(cc0)+1+np.max(cc1)+1
     segmimg = np.zeros(rawimg.shape, cc0.dtype)
     segmimg[0,:, : , :, 0] = cc0[:]
     segmimg[1,:, :, :,0] = cc1[:]
     rawimg = vigra.taggedView(rawimg, 'txyzc')
     binimg = vigra.taggedView(rawimg, 'txyzc')
     segmimg = vigra.taggedView(segmimg, 'txyzc')
     
     self.features = {"Bad Plugin": {"bad_feature_1": {}, "bad_feature_2":{}}}
     self.featureArrays = {0: {"Bad Plugin":{"bad_feature_1": np.array(range(nobj)), \
                                            "bad_feature_2": np.array(range(nobj))}},
                           1: {"Bad Plugin":{"bad_feature_1": np.array(range(nobj)), \
                                            "bad_feature_2": np.array(range(nobj))}}}
     
     self.op = OpObjectClassification(graph = g)
     self.op.RawImages.setValues([rawimg])
     self.op.BinaryImages.setValues([binimg])
     self.op.SegmentationImages.setValues([segmimg])
     self.op.ObjectFeatures.setValues([self.featureArrays])
     self.op.ComputedFeatureNames.setValue(self.features)
     self.op.SelectedFeatures.setValue(self.features)
    def testBasic2(self):
        a = numpy.eye(3, dtype = numpy.float32)
        a = a[None, ..., None]

        a = vigra.taggedView(a, "tyxc")


        expected_b = numpy.array([[ 0.59375, -0.375  , -0.34375],
                                  [-0.375  ,  0.625  , -0.375  ],
                                  [-0.34375, -0.375  ,  0.59375]], dtype=numpy.float32)
        expected_b = expected_b[None, ..., None]
        expected_b = vigra.taggedView(expected_b, "tyxc")

        graph = Graph()

        opPrep = OpArrayPiper(graph=graph)
        opPrep.Input.setValue(a)

        op = OpNansheWaveletTransform(graph=graph)
        op.Input.connect(opPrep.Output)

        op.Scale.setValue((0, 1, 1))

        b = op.Output[...].wait()
        b = vigra.taggedView(b, "tyxc")

        assert((b == expected_b).all())
예제 #28
0
def testMinimalRequest():
    """
    Make sure that unneeded slices are not requested by the stacker.
    """
    graph = Graph()
    ops = []
    for op in range(10):
        data = numpy.random.random( (100,100) )
        data = vigra.taggedView( data, 'yx' )
        meta = MetaDict( { 'shape' : data.shape, 
                 'dtype' : data.dtype, 
                 'axistags' : data.axistags } )
        opData = OpTrackedOutputProvider( data, meta, graph=graph )
        ops.append( opData )
    
    opStacker = OpMultiArrayStacker(graph=graph)    
    opStacker.AxisFlag.setValue('z')
    opStacker.AxisIndex.setValue(0)
    opStacker.Images.resize( len(ops) )
    for islot, opData in zip( opStacker.Images, ops ):
        islot.connect( opData.Output )
    
    assert opStacker.Output.meta.getTaggedShape()['z'] == len(ops)
    
    stacked_data = opStacker.Output[3:5,:,:].wait()
    assert stacked_data.shape == (2, 100, 100)                      
    stacked_data = vigra.taggedView( stacked_data, 'zyx' )
    expected_data = numpy.concatenate( (ops[3]._data[numpy.newaxis, :], ops[4]._data[numpy.newaxis, :]) )
    expected_data = vigra.taggedView( expected_data, 'zyx' )
    assert (stacked_data == expected_data).all(), "Stacker returned the wrong data"
    for index, op in enumerate(ops):
        assert len(op.requested_rois) == 0 or index in range(3,5), "Stacker requested more data than it needed."
예제 #29
0
def test1():
    vol = np.zeros((1, 2, 3, 4, 5), dtype=np.uint8)
    vol1 = vigra.taggedView(vol, axistags='txyzc')
    vol2 = vigra.taggedView(vol, axistags='czyxt')

    g = Graph()

    pipe1 = OpArrayPiper(graph=g)
    pipe1.Input.setValue(vol1)
    pipe2 = OpArrayPiper(graph=g)
    pipe2.Input.setValue(vol2)

    slicer = OpMultiArraySlicer(graph=g)
    slicer.AxisFlag.setValue('c')
    op = OperatorWrapper(TestOp, graph=g)
    op.Input.connect(slicer.Slices)
    stacker = OpMultiArrayStacker(graph=g)
    stacker.AxisFlag.setValue('c')
    stacker.Images.connect(op.Output)

    stacker.AxisIndex.setValue(0)
    slicer.Input.connect(pipe1.Output)
    print(stacker.Output.meta.getTaggedShape())

    print()

    stacker.AxisIndex.setValue(0)
    slicer.Input.connect(pipe2.Output)
    print(stacker.Output.meta.getTaggedShape())
예제 #30
0
    def _execute_HYSTERESIS(self, roi, result):
        self._execute_SIMPLE(roi, result)
        final_labels = vigra.taggedView( result, self.Output.meta.axistags )

        core_labels = self.CoreLabels(roi.start, roi.stop).wait()
        core_labels = vigra.taggedView( core_labels, self.CoreLabels.meta.axistags )
        
        select_labels(core_labels, final_labels) # Edits final_labels in-place
예제 #31
0
    def testBasic2(self):
        a = numpy.ones((100, 101, 102))
        a = a[..., None]
        a = vigra.taggedView(a, "tyxc")

        graph = Graph()

        opPrep = OpArrayPiper(graph=graph)
        opPrep.Input.setValue(a)

        op = OpNansheEstimateF0Cached(graph=graph)
        op.Input.connect(opPrep.Output)

        op.HalfWindowSize.setValue(20)
        op.WhichQuantile.setValue(0.5)
        op.TemporalSmoothingGaussianFilterStdev.setValue(5.0)
        op.SpatialSmoothingGaussianFilterStdev.setValue(5.0)

        b = op.Output[...].wait()
        b = vigra.taggedView(b, "tyxc")

        assert(a.shape == b.shape)

        assert((b == 1).all())
예제 #32
0
    def testReconnect(self):
        """
        Can we connect an image, then replace it with a differently-ordered image?
        """
        predRaw = np.zeros((20, 22, 21, 3), dtype=np.uint32)
        pred1 = vigra.taggedView(predRaw, axistags='zyxc')
        pred2 = vigra.taggedView(predRaw, axistags='tyxc')

        oper5d = OpThresholdTwoLevels(graph=Graph())
        oper5d.InputImage.setValue(pred1)
        oper5d.MinSize.setValue(self.minSize)
        oper5d.MaxSize.setValue(self.maxSize)
        oper5d.HighThreshold.setValue(self.highThreshold)
        oper5d.LowThreshold.setValue(self.lowThreshold)
        oper5d.SmootherSigma.setValue(self.sigma)
        oper5d.Channel.setValue(0)
        oper5d.CoreChannel.setValue(0)
        oper5d.CurOperator.setValue(1)

        out5d = oper5d.CachedOutput[:].wait()

        oper5d.InputImage.disconnect() # FIXME: Why is this line necessary? Ideally, it shouldn't be...
        oper5d.InputImage.setValue(pred2)
        out5d = oper5d.CachedOutput[:].wait()
예제 #33
0
    def testSingletonZ(self):
        vol = np.zeros((82, 70, 1, 5, 5), dtype=np.uint8)
        vol = vigra.taggedView(vol, axistags="xyzct")

        blocks = np.zeros(vol.shape, dtype=np.uint8)
        blocks[30:50, 40:60, :, 2:4, 3:5] = 1
        blocks[30:50, 40:60, :, 2:4, 0:2] = 2
        blocks[60:70, 30:40, :, :, :] = 3

        vol[blocks == 1] = 255
        vol[blocks == 2] = 255
        vol[blocks == 3] = 255

        op = OpLabelVolume(graph=Graph())
        op.Method.setValue(self.method)
        op.Input.setValue(vol)

        out = op.Output[...].wait()
        tags = op.Output.meta.getTaggedShape()
        out = vigra.taggedView(out, axistags="".join([s for s in tags]))

        for c in range(out.shape[3]):
            for t in range(out.shape[4]):
                assertEquivalentLabeling(blocks[..., c, t], out[..., c, t])
예제 #34
0
    def _execute_bbox(self, roi, result):
        cc = self.LabelImage.get(roi).wait()
        cc = vigra.taggedView(cc, axistags=self.LabelImage.meta.axistags)
        cc = cc.withAxes(*'xyz')

        logger.debug("computing bboxes...")
        feats = vigra.analysis.extractRegionFeatures(
            cc.astype(np.float32),
            cc.astype(np.uint32),
            features=["Count", "Coord<Minimum>", "Coord<Maximum>"])
        feats_dict = {}
        feats_dict["Coord<Minimum>"] = feats["Coord<Minimum>"]
        feats_dict["Coord<Maximum>"] = feats["Coord<Maximum>"]
        feats_dict["Count"] = feats["Count"]
        return feats_dict
예제 #35
0
    def execute(self, slot, subindex, roi, result):

        if slot == self.Detector:
            result = self.dumps()
            return result

        # sanity check
        assert self.DetectionMethod.value in ["svm", "classic"], "Unknown detection method '{}'".format(
            self.DetectionMethod.value
        )

        # prefill result
        resultZYXCT = vigra.taggedView(result, self.InputVolume.meta.axistags).withAxes(*"zyxct")

        # acquire data
        data = self.InputVolume.get(roi).wait()
        dataZYXCT = vigra.taggedView(data, self.InputVolume.meta.axistags).withAxes(*"zyxct")

        # walk over time and channel axes
        for t in range(dataZYXCT.shape[4]):
            for c in range(dataZYXCT.shape[3]):
                resultZYXCT[..., c, t] = self._detectMissing(dataZYXCT[..., c, t])

        return result
예제 #36
0
    def testOpSelectLabels(self):
        op = OpSelectLabels(graph=Graph())

        small = numpy.asarray(
            [[0, 0, 0],
             [0, 1, 0],
             [0, 0, 0]]).astype(np.uint32)
        big = numpy.asarray(
            [[0, 1, 0],
             [1, 1, 1],
             [0, 1, 0]]).astype(np.uint32)

        small = vigra.taggedView(small[:,:,None], 'yxc')
        big = vigra.taggedView(big[:,:,None], 'yxc')

        op.BigLabels.setValue(big)
        op.SmallLabels.setValue(small)
        out = op.Output[...].wait()
        numpy.testing.assert_array_equal(out, big)

        op.BigLabels.setValue(big)
        op.SmallLabels.setValue(small*0)
        out = op.Output[...].wait()
        numpy.testing.assert_array_equal(out, big*0)
예제 #37
0
    def testReconnectWithoutRequest(self):
        vol = numpy.zeros((200, 100, 50), dtype=numpy.float32)
        vol1 = vigra.taggedView(vol, axistags='xyz')
        vol2 = vigra.taggedView(vol, axistags='zyx').withAxes(*'xyz')
        graph = Graph()

        opData1 = OpArrayPiper(graph=graph)
        opData1.Input.setValue(vol1)

        op = OpCompressedCache(graph=graph)
        op.Input.connect(opData1.Output)
        op.BlockShape.setValue((200, 100, 10))
        out = op.Output[...].wait().view(numpy.ndarray)

        assert (out == vol).all(), "Incorrect output!"

        op.BlockShape.setValue((50, 100, 10))

        # Older versions of OpCompressedCache threw an exception here because
        #  we tried to access the cache after changing the blockshape.
        # But in the current version, we claim that's okay.
        out = op.Output[...].wait()

        assert (out == vol).all(), "Incorrect output!"
예제 #38
0
    def _prepare_data_zyx(self):
        file_base = self._tmp_dir + "/rand_3d"

        X, Y = (100, 100)
        Z = 10
        rand_data_3d = numpy.random.random((Z, Y, X))
        rand_data_3d *= 256
        rand_data_3d = rand_data_3d.astype(numpy.uint8)
        rand_data_3d = vigra.taggedView(rand_data_3d, 'zyx')

        for z in range(Z):
            file_name = file_base + "_z{}.tiff".format(z)
            vigra.impex.writeImage(rand_data_3d[z, :, :], file_name)

        return (rand_data_3d, file_base + "*.tiff")
예제 #39
0
    def test_3d(self, tmp_path):
        data = numpy.random.randint(0, 255,
                                    (50, 100, 200, 3)).astype(numpy.uint8)
        tiff_path = str(tmp_path / "test-3d.tiff")
        for z_slice in data:
            vigra.impex.writeImage(vigra.taggedView(z_slice, "yxc"),
                                   tiff_path,
                                   dtype="NATIVE",
                                   mode="a")

        op = OpTiffReader(graph=Graph())
        op.Filepath.setValue(tiff_path)
        assert op.Output.ready()
        assert (op.Output[20:30, 50:100, 50:150].wait() == data[20:30, 50:100,
                                                                50:150]).all()
예제 #40
0
    def testPatchDetection(self):
        vol = vigra.taggedView(np.ones((5, 5), dtype=np.uint8) * 128,
                               axistags=vigra.defaultAxistags('xy'))
        vol[2:5, 2:5] = 0
        expected = np.zeros((5, 5))
        expected[3:5, 3:5] = 1

        self.op.PatchSize.setValue(2)
        self.op.HaloSize.setValue(1)
        self.op.DetectionMethod.setValue('classic')
        self.op.InputVolume.setValue(vol)

        out = self.op.Output[:].wait()

        assert_array_equal(expected[3:5, 3:5], out[3:5, 3:5])
예제 #41
0
    def setUp(self):

        self.nz = 50
        self.ny = 51
        self.nx = 52
        self.nc = 3

        clusters = self.generateData((self.nz, self.ny, self.nx))
        self.data = clusters[0] + clusters[1] + clusters[2] + clusters[3] + clusters[4]
        self.data = self.data.reshape(self.data.shape + (1,))
        self.data = vigra.taggedView(self.data, axistags="zyxc")

        self.minSize = 5  # first cluster doesn't pass this
        self.maxSize = 30  # fourth and fifth cluster don't pass this
        self.highThreshold = 0.65  # third cluster doesn't pass
        self.lowThreshold = 0.1

        # Replicate the 4d data for multiple time slices
        self.data5d = numpy.concatenate(3 * (self.data[numpy.newaxis, ...],), axis=0)
        self.data5d = vigra.taggedView(self.data5d, axistags="tzyxc")

        self.sigma = {"z": 0.3, "y": 0.3, "x": 0.3}
        # pre-smooth 4d data
        self.data = vigra.filters.gaussianSmoothing(self.data.astype(numpy.float32), 0.3)
    def testBasic(self):
        features = numpy.indices((100, 100)).astype(numpy.float32) + 0.5
        features = numpy.rollaxis(features, 0, 3)
        features = vigra.taggedView(features, 'xyc')
        labels = numpy.zeros((100, 100, 1), dtype=numpy.uint8)
        labels = vigra.taggedView(labels, 'xyc')

        labels[10, 10] = 1
        labels[10, 11] = 1
        labels[20, 20] = 2
        labels[20, 21] = 2

        graph = Graph()
        opFeatureMatrixCache = OpFeatureMatrixCache(graph=graph)
        opFeatureMatrixCache.FeatureImage.setValue(features)
        opFeatureMatrixCache.LabelImage.setValue(labels)
        opFeatureMatrixCache.NonZeroLabelBlocks.setValue(0)

        opFeatureMatrixCache.LabelImage.setDirty(numpy.s_[10:11, 10:12])
        opFeatureMatrixCache.LabelImage.setDirty(numpy.s_[20:21, 20:22])
        opFeatureMatrixCache.LabelImage.setDirty(numpy.s_[30:31, 30:32])

        opTrain = OpTrainClassifierFromFeatureVectors(graph=graph)
        opTrain.ClassifierFactory.setValue(
            ParallelVigraRfLazyflowClassifierFactory(100))
        opTrain.MaxLabel.setValue(2)
        opTrain.LabelAndFeatureMatrix.connect(
            opFeatureMatrixCache.LabelAndFeatureMatrix)

        assert opTrain.Classifier.ready()

        trained_classifer = opTrain.Classifier.value

        # This isn't much of a test at the moment...
        assert isinstance( trained_classifer, ParallelVigraRfLazyflowClassifier ), \
            "classifier is of the wrong type: {}".format(type(trained_classifer))
예제 #43
0
    def test(self):
        """
        Test use-case from https://github.com/ilastik/lazyflow/issues/111
        """
        data = numpy.zeros((20, 20))
        data = vigra.taggedView(data, 'xy')
        op = OpArrayCache(graph=Graph())
        op.Input.setValue(data)

        assert (op.Output[0:20, 0:20].wait() == 0).all()

        # Should not crash...
        op.Input[0:20, 0:20] = numpy.ones((20, 20))

        assert (op.Output[0:20, 0:20].wait() == 1).all()
예제 #44
0
    def _label(self, roi, result):
        result = vigra.taggedView(result, axistags=self.Output.meta.axistags)
        # get the background values
        bg = self.Background[...].wait()
        bg = vigra.taggedView(bg, axistags=self.Background.meta.axistags)
        bg = bg.withAxes(*'ct')
        assert np.all(self.Background.meta.shape[0] ==
                      self.Input.meta.shape[0]),\
            "Shape of background values incompatible to shape of Input"
        assert np.all(self.Background.meta.shape[4] ==
                      self.Input.meta.shape[4]),\
            "Shape of background values incompatible to shape of Input"

        # do labeling in parallel over channels and time slices
        pool = RequestPool()

        start = np.asarray(roi.start, dtype=np.int)
        stop = np.asarray(roi.stop, dtype=np.int)
        for ti, t in enumerate(range(roi.start[0], roi.stop[0])):
            start[0], stop[0] = t, t + 1
            for ci, c in enumerate(range(roi.start[4], roi.stop[4])):
                start[4], stop[4] = c, c + 1
                newRoi = SubRegion(self.Output,
                                   start=tuple(start),
                                   stop=tuple(stop))
                resView = result[ti, ..., ci].withAxes(*'xyz')
                req = Request(partial(self._label3d, newRoi, bg[c, t],
                                      resView))
                pool.add(req)

        logger.debug(
            "{}: Computing connected components for ROI {} ...".format(
                self.name, roi))
        pool.wait()
        pool.clean()
        logger.debug("{}: Connected components computed.".format(self.name))
예제 #45
0
    def execute(self, slot, subindex, roi, result):
        assert slot == self.Output, "Unknown slot requested: {}".format(slot)
        for i in (0, 4):
            assert roi.stop[i] - roi.start[i] == 1,\
                "Invalid roi for graph-cut: {}".format(str(roi))

        # request the prediction image
        pred = self.Prediction.get(roi).wait()
        pred = vigra.taggedView(pred, axistags=self.Prediction.meta.axistags)
        pred = pred.withAxes(*'zyx')

        # prepare result
        resView = vigra.taggedView(result, axistags=self.Output.meta.axistags)
        resView = resView.withAxes(*'zyx')

        logger.info("Executing graph cut ... (this might take a while)")
        threshold_binary = segmentGC(pred, self.Beta.value)
        threshold_binary = vigra.taggedView(threshold_binary, 'zyx')
        logger.info("Graph-cut done")

        # label the segmentation so that this operator is consistent with
        # the other thresholding operators
        vigra.analysis.labelVolumeWithBackground(
            threshold_binary.astype(np.uint8), out=resView)
예제 #46
0
        def testMargin(self):
            graph = Graph()
            vol = np.zeros((100, 110, 10), dtype=np.float32)
            # draw a big plus sign
            vol[50:70, :, :] = 1.0
            vol[:, 60:80, :] = 1.0
            vol = vigra.taggedView(vol, axistags='zyx').withAxes(*'tzyxc')
            labels = np.zeros((100, 110, 10), dtype=np.uint32)
            labels[45:75, 55:85, 3:4] = 1
            labels = vigra.taggedView(labels,
                                      axistags='zyx').withAxes(*'tzyxc')

            op = OpObjectsSegment(graph=graph)
            piper = OpArrayPiper(graph=graph)
            piper.Input.setValue(vol)
            op.Prediction.connect(piper.Output)
            op.LabelImage.setValue(labels)

            # without margin
            op.MarginZYX.setValue(np.asarray((0, 0, 0)))
            out = op.Output[...].wait()
            out = vigra.taggedView(out, axistags=op.Output.meta.axistags)
            out = out.withAxes(*'zyx')
            vol = vol.withAxes(*'zyx')
            assert_array_equal(out[50:70, 60:80, 3] > 0,
                               vol[50:70, 60:80, 3] > .5)
            assert np.all(out[:45, ...] == 0)

            # with margin
            op.MarginZYX.setValue(np.asarray((5, 5, 0)))
            out = op.Output[...].wait()
            out = vigra.taggedView(out, axistags=op.Output.meta.axistags)
            out = out.withAxes(*'zyx')
            assert_array_equal(out[45:75, 55:85, 3] > 0,
                               vol[45:75, 55:85, 3] > .5)
            assert np.all(out[:40, ...] == 0)
예제 #47
0
    def testCompressed(self):
        graph = Graph()
        opDataProvider = OpArrayPiperWithAccessCount(graph=graph)
        opCache = OpUnblockedArrayCache(graph=graph)
        opCache.CompressionEnabled.setValue(True)

        data = np.random.random((100, 100, 100)).astype(np.float32)
        opDataProvider.Input.setValue(vigra.taggedView(data, 'zyx'))
        opCache.Input.connect(opDataProvider.Output)

        roi = ((30, 30, 30), (50, 50, 50))
        cache_data = opCache.Output(*roi).wait()
        assert (cache_data == data[roiToSlice(*roi)]).all()
        assert opDataProvider.accessCount == 1

        # Request the same data a second time.
        # Access count should not change.
        cache_data = opCache.Output(*roi).wait()
        assert (cache_data == data[roiToSlice(*roi)]).all()
        assert opDataProvider.accessCount == 1

        # Now invalidate a part of the data
        # The cache will discard it, so the access count should increase.
        opDataProvider.Input.setDirty((30, 30, 30), (31, 31, 31))
        cache_data = opCache.Output(*roi).wait()
        assert (cache_data == data[roiToSlice(*roi)]).all()
        assert opDataProvider.accessCount == 2

        # Repeat this next part just for safety
        for _ in range(10):
            # Make sure the cache is empty
            opDataProvider.Input.setDirty((30, 30, 30), (31, 31, 31))
            opDataProvider.accessCount = 0

            # Create many requests for the same data.
            # Upstream data should only be accessed ONCE.
            pool = RequestPool()
            for _ in range(10):
                pool.add(opCache.Output(*roi))
            pool.wait()
            assert opDataProvider.accessCount == 1

        # Also, make sure requests for INNER rois of stored blocks are also serviced from memory
        opDataProvider.accessCount = 0
        inner_roi = ((35, 35, 35), (45, 45, 45))
        cache_data = opCache.Output(*inner_roi).wait()
        assert (cache_data == data[roiToSlice(*inner_roi)]).all()
        assert opDataProvider.accessCount == 0
예제 #48
0
def calc_std_features(data_filename, output_filename, n_images, sigmas):
    #return type: none but produces file with name output_filename containing feature_array [x,y,t,c, type_feature, sigmas]
    #data_filename: name of file with dataset 'data' in shape [t,x,y,c] to calculate features on
    #output_filename: name of file with dataset 'data' in shape [x,y,n_images, cDim, 7, len(sigmas)]
    #n_images: compute convolutions on first n_images of data_filename
    #sigmas: sigmas for following features: sigmasGaussian, sigmasLoG, sigmasGGM, sigmasSTE, sigmasHoGE

    #open data file
    rawdataF = h5py.File(data_filename, 'r')
    xDim = rawdataF['data'].shape[1]
    yDim = rawdataF['data'].shape[2]
    cDim = rawdataF['data'].shape[3]

    outF = h5py.File(output_filename, 'w')
    #number of features types: 7 because STE and HoGE consists of two eigenvalues
    n_feat_types = 7
    outF.create_dataset(
        "data", (xDim, yDim, n_images, cDim, n_feat_types, len(sigmas)),
        dtype=np.float32,
        compression='gzip')
    for t in np.arange(n_images):
        data = np.zeros((xDim, yDim, cDim), dtype=np.float32)
        data = rawdataF['data'][t, :, :, :]
        print data.shape
        #rawdata = rawdata.swapaxes(0,2).swapaxes(0,1)
        rawdata = vg.taggedView(data.astype(np.float32), "xyc")
        features = np.zeros((xDim, yDim, cDim, n_feat_types, len(sigmas)),
                            dtype=np.float32)
        for c in np.arange(cDim):
            for i, sigma in enumerate(sigmas):
                features[:, :, c, 0, i] = vg.filters.gaussianSmoothing(
                    rawdata[:, :, c], sigma)
                features[:, :, c, 1, i] = vg.filters.laplacianOfGaussian(
                    rawdata[:, :, c], sigma)
                features[:, :, c, 2, i] = vg.filters.gaussianGradientMagnitude(
                    rawdata[:, :, c], sigma)
                data1 = vg.filters.structureTensorEigenvalues(
                    rawdata[:, :, c], sigma, sigma)
                features[:, :, c, 3, i] = data1[:, :, 0]
                features[:, :, c, 4, i] = data1[:, :, 1]
                data2 = vg.filters.hessianOfGaussianEigenvalues(
                    rawdata[:, :, c], sigma)
                features[:, :, c, 5, i] = data2[:, :, 0]
                features[:, :, c, 6, i] = data2[:, :, 1]
        outF['data'][:, :, t, :, :, :] = features[:, :, :, :, :]
    outF.close()
    rawdataF.close()
    return features
예제 #49
0
    def testCacheApi(self):
        graph = Graph()
        opDataProvider = OpArrayPiperWithAccessCount(graph=graph)
        opCache = OpUnblockedArrayCache(graph=graph)

        data = np.random.random((100, 100, 100)).astype(np.float32)
        opDataProvider.Input.setValue(vigra.taggedView(data, 'zyx'))
        opCache.Input.connect(opDataProvider.Output)

        opCache.Output[10:20, 20:40, 50:100].wait()
        opCache.Output[11:21, 22:43, 53:90].wait()

        l = opCache.getBlockAccessTimes()
        assert len(l) == 2
        for k, t in l:
            assert t > 0.0
예제 #50
0
    def test_attempt_drop_nonsingleton_axis(self):
        """
        Attempt to configure the operator with invalid settings by trying to drop a non-singleton axis.
        The execute method should assert in that case.
        """
        data = numpy.zeros( (100,100,100), dtype=numpy.uint8 )
        data = vigra.taggedView( data, vigra.defaultAxistags('xyz') )
        
        op = OpReorderAxes( graph=Graph() )
        op.Input.setValue( data )
        
        # Attempt to drop some axes that can't be dropped.
        op.AxisOrder.setValue( 'txc' )

        # Make sure this results in an error.        
        self.assertRaises( AssertionError, op.Output[:].wait )
예제 #51
0
    def testStrangeAxesWithout(self):
        pred = np.zeros((20, 22, 21, 3), dtype=np.uint32)
        pred = vigra.taggedView(pred, axistags='tyxc')

        oper5d = OpThresholdTwoLevels(graph=Graph())
        oper5d.InputImage.setValue(pred)
        oper5d.MinSize.setValue(self.minSize)
        oper5d.MaxSize.setValue(self.maxSize)
        oper5d.HighThreshold.setValue(self.highThreshold)
        oper5d.LowThreshold.setValue(self.lowThreshold)
        oper5d.SmootherSigma.setValue(self.sigma)
        oper5d.Channel.setValue(0)
        oper5d.CurOperator.setValue(2)
        oper5d.UsePreThreshold.setValue(False)

        out5d = oper5d.CachedOutput[:].wait()
예제 #52
0
def allPairsOfShortestPath(graph, weights):
    pathFinder = vigra.graphs.ShortestPathPathDijkstra(graph)
    distances = numpy.zeros([graph.nodeNum] * 2)
    for ni, node in enumerate(graph.nodeIter()):
        pathFinder.run(weights**1, node)
        d = pathFinder.distances()**1
        distances[ni, :] = d[:]

        ggf = graph.projectNodeFeaturesToGridGraph(d)
        ggf = vigra.taggedView(ggf, 'xy')

        if ni < 1:
            vigra.imshow(ggf)
            vigra.show()
    print distances.min(), distances.max()
    return distances
    def test_2d(self):
        graph = Graph()
        data2d = numpy.random.random((2,100,100,1,3))
        data2d = vigra.taggedView(data2d, axistags='txyzc')
        # Define operators
        opFeatures = OpFeatureSelection(graph=graph)
        opFeatures.Scales.connect(self.opFeatures.Scales[0])
        opFeatures.FeatureIds.connect(self.opFeatures.FeatureIds[0])
        opFeatures.SelectionMatrix.connect(self.opFeatures.SelectionMatrix[0])

        # Set input data
        opFeatures.InputImage.setValue(data2d)

        # Compute results for the top slice only
        topSlice = [0, slice(None), slice(None), 0, slice(None)]
        result = opFeatures.OutputImage[topSlice].wait()
예제 #54
0
 def __init__(self,
              *,
              preloaded_array: numpy.ndarray,
              axistags: AxisTags = None,
              nickname: str = "",
              **info_kwargs):
     self.preloaded_array = vigra.taggedView(
         preloaded_array, axistags
         or get_default_axisordering(preloaded_array.shape))
     super().__init__(
         nickname=nickname
         or "preloaded-{}-array".format(self.preloaded_array.dtype.name),
         default_tags=self.preloaded_array.axistags,
         laneShape=preloaded_array.shape,
         laneDtype=preloaded_array.dtype,
         **info_kwargs,
     )
예제 #55
0
    def test_attempt_drop_nonsingleton_axis(self):
        """
        Attempt to configure the operator with invalid settings by trying to drop a non-singleton axis.
        The execute method should assert in that case.
        """
        data = numpy.zeros((100, 100, 100), dtype=numpy.uint8)
        data = vigra.taggedView(data, vigra.defaultAxistags("xyz"))

        # Attempt to drop some axes that can't be dropped.
        op = OpReorderAxes(graph=Graph(), Input=data, AxisOrder="txc")

        # Make sure this results in an error.
        req = op.Output[:]
        req.notify_failed(
            lambda *args: None
        )  # We expect an exception here, so disable the default fail handler to hide the traceback
        self.assertRaises(AssertionError, req.wait)
예제 #56
0
        def processSingleObject(i):
            logger.debug("processing object {}".format(i))
            # maxs are inclusive, so we need to add 1
            xmin = max(mins[i][0] - margin[0], 0)
            ymin = max(mins[i][1] - margin[1], 0)
            zmin = max(mins[i][2] - margin[2], 0)
            xmax = min(maxs[i][0] + margin[0] + 1, cc.shape[0])
            ymax = min(maxs[i][1] + margin[1] + 1, cc.shape[1])
            zmax = min(maxs[i][2] + margin[2] + 1, cc.shape[2])
            ccbox = cc[xmin:xmax, ymin:ymax, zmin:zmax]
            resbox = resultXYZ[xmin:xmax, ymin:ymax, zmin:zmax]

            nVoxels = ccbox.size
            if nVoxels > MAXBOXSIZE:
                #problem too large to run graph cut, assign to seed
                logger.warn("Object {} too large for graph cut.".format(i))
                resbox[ccbox == i] = 1
                return

            probbox = pred[xmin:xmax, ymin:ymax, zmin:zmax]
            gcsegm = segmentGC(probbox, beta)
            gcsegm = vigra.taggedView(gcsegm, axistags='xyz')
            ccsegm = vigra.analysis.labelVolumeWithBackground(
                gcsegm.astype(np.uint8))

            # Extended bboxes of different objects might overlap.
            # To avoid conflicting segmentations, we find all connected
            # components in the results and only take the one, which
            # overlaps with the object "core" or "seed", defined by the
            # pre-thresholding
            seed = ccbox == i
            filtered = seed * ccsegm
            passed = vigra.analysis.unique(filtered)
            passed.sort()
            assert len(passed.shape) == 1
            if passed.size > 2:
                logger.warn("ambiguous label assignment for region {}".format(
                    (xmin, xmax, ymin, ymax, zmin, zmax)))
                resbox[ccbox == i] = 1
            elif passed.size <= 1:
                logger.warn("box {} segmented out with beta {}".format(
                    i, beta))
            else:
                # assign to the overlap region
                label = passed[1]  # 0 is background
                resbox[ccsegm == label] = 1
예제 #57
0
def distance_transform(mask, background=False, smoothing=0.0, negate=False):
    """
    Compute the distance transform of voxels inside (or outside) the given mask,
    with smoothing and negation post-processing options as a convenience.
    """
    mask = mask.astype(bool, copy=False)
    mask = vigra.taggedView(mask, 'zyx').astype(np.uint32)
    dt = vigra.filters.distanceTransform(mask, background=background)
    del mask

    if smoothing > 0.0:
        vigra.filters.gaussianSmoothing(dt, smoothing, dt, window_size=2.0)

    if negate:
        dt[:] *= -1

    return dt
    def testSimpleUsage(self):
        n = self.vol.shape[2]
        op = OpMultiArrayStacker(graph=self.g)
        op.AxisFlag.setValue('z')

        provider = OperatorWrapper(OpArrayPiper, graph=self.g)
        vol = self.vol

        op.Images.connect(provider.Output)
        provider.Input.resize(n)

        for i in range(n):
            provider.Input[i].setValue(vol[..., i])

        out = op.Output[...].wait()
        out = vigra.taggedView(out, axistags='xyz')
        numpy.testing.assert_array_equal(out, vol)
예제 #59
0
    def test_tzyxc(self):
        expected_volume_tzyxc, globstring = self._prepare_data(
            "rand_4dc", (5, 10, 50, 100, 3), "tzyxc", "t")

        graph = Graph()
        op = OpStackLoader(graph=graph)
        op.globstring.setValue(globstring)

        assert len(op.stack.meta.axistags) == 5
        assert op.stack.meta.getAxisKeys() == list("tzyxc")
        assert op.stack.meta.dtype == expected_volume_tzyxc.dtype

        vol_from_stack_tzyxc = op.stack[:].wait()
        vol_from_stack_tzyxc = vigra.taggedView(vol_from_stack_tzyxc, "tzyxc")

        assert (vol_from_stack_tzyxc == expected_volume_tzyxc
                ).all(), "4D+c Volume from stack did not match expected data."
예제 #60
0
    def execute(self, slot, subindex, roi, result):
        rag = self.Rag.value
        channel_feature_names = self.FeatureNames.value

        edge_feature_dfs = []
        for c in range(self.VoxelData.meta.shape[-1]):
            channel_name = self.VoxelData.meta.channel_names[c]
            if channel_name not in channel_feature_names:
                continue

            feature_names = [
                decodeToStringIfBytes(f)
                for f in channel_feature_names[channel_name]
            ]
            if not feature_names:
                # No features selected for this channel
                continue

            voxel_data = self.VoxelData[..., c:c + 1].wait()
            voxel_data = vigra.taggedView(voxel_data,
                                          self.VoxelData.meta.axistags)
            voxel_data = voxel_data[..., 0]  # drop channel
            edge_features_df = rag.compute_features(voxel_data, feature_names)

            #if np.isnan(edge_features_df.values).any():
            #    raise RuntimeError("Whoa, why are there NaN values in the feature matrix?")

            edge_features_df = edge_features_df.iloc[:,
                                                     2:]  # Discard columns [sp1, sp2]

            # Prefix all column names with the channel name, to guarantee uniqueness
            # (Generally a nice feature, but also required for serialization.)
            edge_features_df.columns = [
                channel_name + ' ' + feature_name
                for feature_name in edge_features_df.columns.values
            ]
            edge_feature_dfs.append(edge_features_df)

        # Could use join() or merge() here, but we know the rows are already in the right order, and concat() should be faster.
        all_edge_features_df = pd.DataFrame(rag.edge_ids,
                                            columns=['sp1', 'sp2'])
        all_edge_features_df = pd.concat([all_edge_features_df] +
                                         edge_feature_dfs,
                                         axis=1,
                                         copy=False)
        result[0] = all_edge_features_df