Пример #1
0
 def aaaNumpyFile(self):
     g =graph.Graph()
     npfile = "/home/akreshuk/data/synapse_small_4d.npy"
     reader = OpInputDataReader(graph=g)
     reader.FilePath.setValue(npfile)
     #out = reader.Output[:].wait()
     #print out.shape
     
     opFeatures = OpPixelFeaturesPresmoothed(graph=g)
     opFeatures.Scales.setValue(self.scales)
     opFeatures.FeatureIds.setValue(self.featureIds)
     opFeatures.Input.connect(reader.Output)
     opFeatures.Matrix.setValue(self.selectedFeatures[5])
     out = opFeatures.Output[:].wait()
     print out.shape
     
     opFeaturesInterp = OpPixelFeaturesInterpPresmoothed(graph=g)
     opFeaturesInterp.Scales.setValue(self.scales)
     opFeaturesInterp.FeatureIds.setValue(self.featureIds)
     opFeaturesInterp.Input.connect(reader.Output)
     opFeaturesInterp.Matrix.setValue(self.selectedFeatures[5])
     opFeaturesInterp.InterpolationScaleZ.setValue(2)
     out = opFeaturesInterp.Output[:].wait()
     
     print out.shape
Пример #2
0
    def test_compute_in_2d(self):
        op = OpPixelFeaturesPresmoothed(graph=Graph())

        op.Scales.setValue([0.7, 1, 1.6])

        op.FeatureIds.setValue([
            "GaussianSmoothing",
            "LaplacianOfGaussian",
            "StructureTensorEigenvalues",
            "HessianOfGaussianEigenvalues",
            "GaussianGradientMagnitude",
            "DifferenceOfGaussians",
        ])

        op.SelectionMatrix.setValue(
            numpy.array([
                [True, False, True],
                [False, True, False],
                [False, False, True],
                [True, False, False],
                [False, True, False],
                [False, False, True],
            ]))

        # compute result over whole volume in 2d
        op.ComputeIn2d.setValue([True] * 3)
        op.Input.setValue(self.data)
        computed_whole = op.Output[:].wait()

        # compute result for every z slice independently
        z_slices = []
        for z in range(self.data.shape[2]):
            op.Input.setValue(self.data[:, :, z:z + 1])
            z_slices.append(op.Output[:].wait())

        computed_per_slice = numpy.concatenate(z_slices, axis=2)
        assert computed_per_slice.shape == computed_whole.shape

        if DEBUG:
            check_channel = 6
            plt.figure(figsize=(5, 20))
            for z in range(self.data.shape[2]):
                plt.subplot(self.data.shape[2], 2, 2 * z + 1)
                plt.imshow(computed_whole[0, check_channel, z])
                plt.title("whole")
                plt.subplot(self.data.shape[2], 2, 2 * z + 2)
                plt.imshow(computed_per_slice[0, check_channel, z])
                plt.title("per slice")

            plt.show()

        assert computed_whole.shape == computed_per_slice.shape
        assert numpy.allclose(
            computed_whole, computed_per_slice), abs(computed_whole -
                                                     computed_per_slice).max()
Пример #3
0
 def runFeatures(self, data, dataInterp):
     g = graph.Graph()
     opFeatures = OpPixelFeaturesPresmoothed(graph=g)
     opFeaturesInterp = OpPixelFeaturesInterpPresmoothed(graph=g)
     
     opFeatures.Input.setValue(dataInterp)
     opFeaturesInterp.Input.setValue(data)
     
     opFeatures.Scales.setValue(self.scales)
     opFeaturesInterp.Scales.setValue(self.scales)
     
     opFeatures.FeatureIds.setValue(self.featureIds)
     opFeaturesInterp.FeatureIds.setValue(self.featureIds)
     
     opFeaturesInterp.InterpolationScaleZ.setValue(self.scaleZ)
     
     #for i, imatrix in enumerate(self.selectedFeatures[0:1]):
     for i, imatrix in enumerate(self.selectedFeatures):
         opFeatures.Matrix.setValue(imatrix)
         opFeaturesInterp.Matrix.setValue(imatrix)
         outputInterpData = opFeatures.Output[:].wait()
         outputInterpFeatures = opFeaturesInterp.Output[:].wait()
         
         for iz in range(self.nz):
         #for iz in range(2, 3):
             #print iz, iz*self.scaleZ
             try:
                 outputInterpDataSlice = opFeatures.Output[:, :, iz*self.scaleZ:iz*self.scaleZ+1, :].wait()
                 outputInterpFeaturesSlice = opFeaturesInterp.Output[:, :, iz, :].wait()
                 assert_array_almost_equal(outputInterpDataSlice, outputInterpFeaturesSlice, 1)
                 assert_array_almost_equal(outputInterpData[:, :, iz*self.scaleZ, 0], outputInterpFeatures[:, :, iz, 0], 1)
                 #assert_array_almost_equal(outputInterpDataSlice[:, :, 0, :], outputInterpData[:, :, iz*self.scaleZ, :], 3)
                 assert_array_almost_equal(outputInterpFeatures[:, :, iz, :], outputInterpFeaturesSlice[:, :, 0, :], 1)
             except AssertionError:
                 print "failed for feature:", imatrix, i
                 print "failed for slice:", iz
                 print "inter data:", outputInterpData[:, :, iz*self.scaleZ, 0]
                 print "inter features:", outputInterpFeatures[:, :, iz, 0]
                 print "inter data slice:", outputInterpDataSlice[:, :, 0, 0]
                 print "inter features:", outputInterpFeaturesSlice[:, :, 0, 0]
                 raise AssertionError
Пример #4
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Create the operator that actually generates the features
        self.opPixelFeatures = OpPixelFeaturesPresmoothed(parent=self)

        # Connect our internal operators to our external inputs
        self.opPixelFeatures.Scales.connect(self.Scales)
        self.opPixelFeatures.FeatureIds.connect(self.FeatureIds)
        self.opPixelFeatures.SelectionMatrix.connect(self.SelectionMatrix)
        self.opPixelFeatures.ComputeIn2d.connect(self.ComputeIn2d)
        self.opReorderIn = OpReorderAxes(parent=self)
        self.opReorderIn.AxisOrder.setValue('tczyx')
        self.opReorderIn.Input.connect(self.InputImage)
        self.opPixelFeatures.Input.connect(self.opReorderIn.Output)
        self.opReorderOut = OpReorderAxes(parent=self)
        self.opReorderOut.Input.connect(self.opPixelFeatures.Output)
        self.opReorderLayers = OperatorWrapper(
            OpReorderAxes, parent=self, broadcastingSlotNames=["AxisOrder"])
        self.opReorderLayers.Input.connect(self.opPixelFeatures.Features)

        self.WINDOW_SIZE = self.opPixelFeatures.WINDOW_SIZE
    def __init__(self, *args, **kwargs):
        super(OpFeatureSelection, self).__init__(*args, **kwargs)

        # Two internal operators: features and cache
        self.opPixelFeatures = OpPixelFeaturesPresmoothed(parent=self)
        self.opPixelFeatureCache = OpSlicedBlockedArrayCache(parent=self)
        self.opPixelFeatureCache.name = "opPixelFeatureCache"

        # Connect the cache to the feature output
        self.opPixelFeatureCache.Input.connect(self.opPixelFeatures.Output)
        self.opPixelFeatureCache.fixAtCurrent.setValue(False)

        # Connect our internal operators to our external inputs 
        self.opPixelFeatures.Scales.connect( self.Scales )
        self.opPixelFeatures.FeatureIds.connect( self.FeatureIds )
        self.opPixelFeatures.Matrix.connect( self.SelectionMatrix )
        self.opPixelFeatures.Input.connect( self.InputImage )
        
        # Connect our external outputs to our internal operators
        self.OutputImage.connect( self.opPixelFeatures.Output )
        self.CachedOutputImage.connect( self.opPixelFeatureCache.Output )
        self.FeatureLayers.connect( self.opPixelFeatures.Features )
Пример #6
0
 def aaaSlices(self):
     g = graph.Graph()
     opFeatures = OpPixelFeaturesPresmoothed(graph=g)
     opFeatures.Scales.setValue(self.scales)
     opFeatures.FeatureIds.setValue(self.featureIds)
     opFeatures.Input.setValue(self.dataNoChannels)
     for i, imatrix in enumerate(self.selectedFeatures):
         opFeatures.Matrix.setValue(imatrix)
         #compute in one piece
         dataOne = opFeatures.Output[:].wait()
         
         #compute slice-wise
         for z in range(self.nz):
             dataSlice = opFeatures.Output[:, :, z:z+1].wait()
             try:
                 assert_array_almost_equal(dataOne[:, :, z:z+1], dataSlice, 2)
             except AssertionError:
                 print "wrong for matrix:", imatrix
                 print "wrong for slice:", z
                 print dataOne[:, :, z:z+1]
                 print dataSlice
                 raise AssertionError
Пример #7
0
    def __init__(self, *args, **kwargs):
        super(OpFeatureSelectionNoCache, self).__init__(*args, **kwargs)

        # Create the operator that actually generates the features
        self.opPixelFeatures = OpPixelFeaturesPresmoothed(parent=self)

        # Connect our internal operators to our external inputs
        self.opPixelFeatures.Scales.connect(self.Scales)
        self.opPixelFeatures.FeatureIds.connect(self.FeatureIds)
        self.opReorderIn = OpReorderAxes(parent=self)
        self.opReorderIn.Input.connect(self.InputImage)
        self.opPixelFeatures.Input.connect(self.opReorderIn.Output)
        self.opReorderOut = OpReorderAxes(parent=self)
        self.opReorderOut.Input.connect(self.opPixelFeatures.Output)
        self.opReorderLayers = OperatorWrapper(
            OpReorderAxes, parent=self, broadcastingSlotNames=["AxisOrder"])
        self.opReorderLayers.Input.connect(self.opPixelFeatures.Features)

        # We don't connect SelectionMatrix here because we want to
        #  check it for errors (See setupOutputs)
        # self.opPixelFeatures.SelectionMatrix.connect( self.SelectionMatrix )

        self.WINDOW_SIZE = self.opPixelFeatures.WINDOW_SIZE
Пример #8
0
    def test(self):
        graph = Graph()

        testVolumePath = 'tinyfib_volume.h5'

        # Unzip the data if necessary
        if not os.path.exists(testVolumePath):
            zippedTestVolumePath = testVolumePath + ".gz"
            assert os.path.exists(zippedTestVolumePath)
            os.system("gzip -d " + zippedTestVolumePath)
            assert os.path.exists(testVolumePath)

        f = h5py.File(testVolumePath, 'r')
        data = f['data'][...]
        data = data.view(vigra.VigraArray)
        data.axistags = vigra.defaultAxistags('txyzc')

        labels = f['labels'][...]
        assert data.shape[:-1] == labels.shape[:-1]
        assert labels.shape[-1] == 1
        assert len(data.shape) == 5
        f.close()
        scales = [0.3, 0.7, 1, 1.6, 3.5, 5.0, 10.0]
        featureIds = OpPixelFeaturesPresmoothed.DefaultFeatureIds

        # The following conditions cause this test to *usually* fail, but *sometimes* pass:
        # When using Structure Tensor EVs at sigma >= 3.5 (NaNs in feature matrix)
        # When using Gaussian Gradient Mag at sigma >= 3.5 (inf in feature matrix)
        # When using *any feature* at sigma == 10.0 (NaNs in feature matrix)

        #                    sigma:   0.3    0.7    1.0    1.6    3.5    5.0   10.0
        selections = numpy.array([
            [False, False, False, False, False, False, False],
            [False, False, False, False, False, False, False],
            [False, False, False, False, True, False, False],  # ST EVs
            [False, False, False, False, False, False, False],
            [False, False, False, False, False, False, False],  # GGM
            [False, False, False, False, False, False, False]
        ])

        opFeatures = OpPixelFeaturesPresmoothed(graph=graph)
        opFeatures.Input.setValue(data)
        opFeatures.Scales.setValue(scales)
        opFeatures.FeatureIds.setValue(featureIds)
        opFeatures.Matrix.setValue(selections)

        opTrain = OpTrainRandomForestBlocked(graph=graph)
        opTrain.Images.resize(1)
        opTrain.Images[0].connect(opFeatures.Output)
        opTrain.Labels.resize(1)
        opTrain.nonzeroLabelBlocks.resize(1)

        # This test only fails when this flag is True.
        use_sparse_label_storage = True

        if use_sparse_label_storage:
            opLabelArray = OpBlockedSparseLabelArray(graph=graph)
            opLabelArray.inputs["shape"].setValue(labels.shape)
            opLabelArray.inputs["blockShape"].setValue((1, 32, 32, 32, 1))
            opLabelArray.inputs["eraser"].setValue(100)

            opTrain.nonzeroLabelBlocks[0].connect(opLabelArray.nonzeroBlocks)

            # Slice the label data into the sparse array storage
            opLabelArray.Input[...] = labels[...]
            opTrain.Labels[0].connect(opLabelArray.Output)
        else:
            # Skip the sparse storage operator and provide labels as one big block
            opTrain.Labels[0].setValue(labels)
            # One big block
            opTrain.nonzeroLabelBlocks.resize(1)
            opTrain.nonzeroLabelBlocks[0].setValue([[slice(None, None, None)] *
                                                    5])

        # Sanity check: Make sure we configured the training operator correctly.
        readySlots = [slot.ready() for slot in opTrain.inputs.values()]
        assert all(readySlots)

        # Generate the classifier
        classifier = opTrain.Classifier.value