def _singleMissingLayer(layer=30, nx=64, ny=64, nz=100, method="linear"): expected_output = _volume(nx=nx, ny=ny, nz=nz, method=method) volume = vigra.VigraArray(expected_output) missing = vigra.VigraArray(np.zeros(volume.shape), axistags=volume.axistags, dtype=np.uint8) volume[:, :, layer] = 0 missing[:] = 0 missing[:, :, layer] = 1 return (volume, missing, expected_output)
def test5D(self): vol = vigra.VigraArray(np.ones((50, 50, 10, 3, 7)), axistags=vigra.defaultAxistags("cxzty")) miss = vigra.VigraArray(vol) miss[:, :, 3, 2, 4] = 1 self.op.InputVolume.setValue(vol) self.op.Missing.setValue(miss) self.op.Output[:].wait()
def testNothingToInterpolate(self): vol = vigra.VigraArray( np.ones((50,50,10,3,7)), axistags=vigra.defaultAxistags('cxzty') ) miss = vigra.VigraArray(vol)*0 self.op.InputVolume.setValue(vol) self.op.Missing.setValue(miss) assert_array_equal( self.op.Output[:].wait(), vol.view(np.ndarray), err_msg="interpolation where nothing had to be interpolated")
def setupOutputs(self): tags = vigra.defaultAxistags(outer_order) a = vigra.VigraArray(outer_shape, value=1, axistags=tags) b = vigra.VigraArray(outer_shape, value=2, axistags=tags) self.lazyOp1.in_a.setValue(a) self.lazyOp1.in_b.setValue(b) self.lazyOp1.setupOutputs() self.lazyOp2.setupOutputs() self.lazyOp2.in_a.connect(self.lazyOp1.out_a) self.lazyOp2.in_b.connect(self.lazyOp1.out_b)
def execute(self, slot, subindex, roi, result): roi = roi.copy() #request,set or compute the necessary parameters axistags = self.Input.meta.axistags inputShape = self.Input.meta.shape channelIndex = axistags.index('c') channelsPerC = self.channelsPerChannel() channelRes = self.getChannelResolution() timeIndex = axistags.index('t') if timeIndex >= roi.dim: timeIndex = None roi.setInputShape(inputShape) origRoi = roi.copy() sigma = self.setupFilter() halo = self.calculateHalo(sigma) #set up the roi to get the necessary source roi.expandByShape(halo, channelIndex, timeIndex).adjustChannel(channelsPerC, channelIndex, channelRes) source = self.Input(roi.start, roi.stop).wait() source = vigra.VigraArray(source, axistags=axistags) #set up the grid for the iterator, and the iterator srcGrid = [ source.shape[i] if i != channelIndex else channelRes for i in range(len(source.shape)) ] trgtGrid = [ inputShape[i] if i != channelIndex else self.channelsPerChannel() for i in range(len(source.shape)) ] if timeIndex is not None: srcGrid[timeIndex] = 1 trgtGrid[timeIndex] = 1 nIt = newIterator(origRoi, srcGrid, trgtGrid, timeIndex=timeIndex, channelIndex=channelIndex) #set up roi to work with vigra filters if timeIndex > channelIndex and timeIndex is not None: origRoi.popDim(timeIndex) origRoi.popDim(channelIndex) elif timeIndex < channelIndex and timeIndex is not None: origRoi.popDim(channelIndex) origRoi.popDim(timeIndex) else: origRoi.popDim(channelIndex) origRoi.adjustRoi(halo) #iterate over the requested volumes #warnings.warn("TODO: This loop could be parallelized for better performance.") for src, trgt, mask in nIt: result[trgt] = self.vigraFilter(source=source[src], window_size=self.windowSize, roi=origRoi)[mask] return result
def testPartialAllocate(): nx = 15 ny = 20 nz = 17 nc = 7 stack = vigra.VigraArray((nx, ny, nz, nc), axistags=vigra.defaultAxistags('xyzc')) stack[...] = numpy.random.rand(nx, ny, nz, nc) g = Graph() #assume that the slicer works slicerX = OpMultiArraySlicer_REDUCE_DIM(graph=g) slicerX.inputs["Input"].setValue(stack) slicerX.inputs["AxisFlag"].setValue('x') #insert the x dimension stackerX = OpMultiArrayStacker(graph=g) stackerX.inputs["AxisFlag"].setValue('x') stackerX.inputs["AxisIndex"].setValue(0) stackerX.inputs["Images"].connect(slicerX.outputs["Slices"]) key = (slice(2, 3, None), slice(15, 18, None), slice(12, 15, None), slice(0, 7, None)) newdata = stackerX.outputs["Output"][key].wait() substack = stack[key] print(newdata.shape, substack.shape) assert_array_equal(newdata, substack.view(numpy.ndarray))
def setup(self): graph = Graph() try: import blist except ImportError: raise unittest.SkipTest from lazyflow.operators.opBlockedSparseLabelArray import OpBlockedSparseLabelArray op = OpBlockedSparseLabelArray(graph=graph) arrayshape = (1,100,100,10,1) op.inputs["shape"].setValue( arrayshape ) blockshape = (1,10,10,10,1) # Why doesn't this work if blockshape is an ndarray? op.inputs["blockShape"].setValue( blockshape ) op.eraser.setValue(100) dummyData = vigra.VigraArray(arrayshape, axistags=vigra.defaultAxistags('txyzc')) op.Input.setValue( dummyData ) slicing = sl[0:1, 1:15, 2:36, 3:7, 0:1] inDataShape = slicing2shape(slicing) inputData = ( 3*numpy.random.random(inDataShape) ).astype(numpy.uint8) op.Input[slicing] = inputData data = numpy.zeros(arrayshape, dtype=numpy.uint8) data[slicing] = inputData self.op = op self.slicing = slicing self.inData = inputData self.data = data
def node_locations_to_array(arr_shape, node_locations): """ Given a vigra image in xy and a dict containing xy coordinates, return a vigra image of the same shape, where nodes are represented by their integer ID, and every other pixel is -1. Parameters ---------- arr_shape : tuple node_locations : dict dict whose values are a dicts containing a 'coords' dict (relative within this image) and a 'treenode_id' value Returns ------- vigra.VigraArray """ arr_xy = vigra.VigraArray(arr_shape, dtype=np.int64, value=-1, axistags=vigra.AxisTags('xy')) for node_location in node_locations.values(): coords = node_location['coords'] arr_xy[coords['x'], coords['y']] = int(node_location['treenode_id']) return arr_xy
def setup(self): graph = Graph() op = OpCompressedUserLabelArray(graph=graph) arrayshape = (1, 100, 100, 10, 1) op.inputs["shape"].setValue(arrayshape) blockshape = (1, 10, 10, 10, 1 ) # Why doesn't this work if blockshape is an ndarray? op.inputs["blockShape"].setValue(blockshape) op.eraser.setValue(100) dummyData = vigra.VigraArray(arrayshape, axistags=vigra.defaultAxistags("txyzc"), dtype=numpy.uint8) op.Input.setValue(dummyData) slicing = numpy.s_[0:1, 1:15, 2:36, 3:7, 0:1] inDataShape = slicing2shape(slicing) inputData = (3 * numpy.random.random(inDataShape)).astype(numpy.uint8) op.Input[slicing] = inputData data = numpy.zeros(arrayshape, dtype=numpy.uint8) data[slicing] = inputData # Sanity check... assert (op.Output[:].wait()[slicing] == data[slicing]).all() self.op = op self.slicing = slicing self.inData = inputData self.data = data
def prepareVolnOp(self, possible_axes="tzyxc", num=5, AxisOrder=None, config_via_init=False): tagStr = "".join(random.sample(possible_axes, random.randint(2, num))) axisTags = vigra.defaultAxistags(tagStr) self.shape = [random.randint(20, 30) for tag in axisTags] self.array = numpy.random.rand(*self.shape) * 255 self.array = (float(250) / 255 * self.array + 5).astype(int) self.inArray = vigra.VigraArray(self.array, axistags=axisTags) opProvider = OpArrayProvider(graph=self.graph) opProvider.Input.setValue(self.inArray) if config_via_init: self.operator = OpReorderAxes(graph=self.graph, Input=opProvider.Output, AxisOrder=AxisOrder) else: self.operator.Input.connect(opProvider.Output) if AxisOrder is not None: self.operator.AxisOrder.setValue(AxisOrder)
def setup_method(self, method): self.testVol = vigra.VigraArray((200, 200, 200)) self.testVol[:] = numpy.random.rand(200, 200, 200) self.graph = Graph() self.op = OpArrayPiper(graph=self.graph) self.op.inputs["Input"].setValue(self.testVol) self.roiOp = OpRoiTest(graph=self.graph) self.roiOp.inputs["input"].setValue(self.testVol)
def visualTest(self, operator): if GENERATE_VISUAL_DEBUG_IMAGES: start, stop = [200, 200, 0], [400, 400, 1] testArray = vigra.VigraArray((400, 400, 3)) roiResult = vigra.VigraArray( tuple([sto - sta for sta, sto in zip(start, stop)])) testArray[100:300, 100:300, 0] = 1 testArray[200:300, 200:300, 1] = 1 testArray[100:200, 100:200, 2] = 1 operator.inputs["Input"].setValue(testArray) wholeResult = operator.outputs["Output"]().wait() wholeResult = wholeResult[:, :, 0:3] roiResult[:, :, 0:1] = operator.outputs["Output"](start, stop).wait() vigra.impex.writeImage(testArray, operator.name + 'before.png') vigra.impex.writeImage(wholeResult, operator.name + 'afterWhole.png') vigra.impex.writeImage(roiResult, operator.name + 'afterRoi.png')
def setUp(self): self.graph = lazyflow.graph.Graph() self.testDataFileName = 'bigH5TestData.h5' self.datasetInternalPath = 'volume/data' # Generate some test data self.dataShape = (1, 10, 128, 128, 1) self.testData = vigra.VigraArray( self.dataShape, axistags=vigra.defaultAxistags('txyzc') ) # default vigra order this time... self.testData[...] = numpy.indices(self.dataShape).sum(0)
def testIntegerRange(self): ''' test if the output is in the right integer range in particular, too large values should be set to max and too small values to min ''' v = np.zeros((1, 1, 5), dtype=np.uint8) v[0, 0, :] = [220, 255, 0, 255, 220] v = vigra.VigraArray(v, axistags=vigra.defaultAxistags('xyz'), dtype=np.uint8) m = vigra.VigraArray(v, axistags=vigra.defaultAxistags('xyz'), dtype=np.uint8) m[:] = 0 m[0, 0, 2] = 1 for interpolationMethod in ['cubic']: self.op.InputVolume.setValue(v) self.op.Missing.setValue(m) self.op.InterpolationMethod.setValue(interpolationMethod) self.op.InputVolume.setValue(v) out = self.op.Output[:].wait().view(np.ndarray) # natural comparison self.assertEqual(out[0, 0, 2], 255) v = np.zeros((1, 1, 5), dtype=np.uint8) v[0, 0, :] = [220, 255, 0, 255, 220] v = 255 - vigra.VigraArray( v, axistags=vigra.defaultAxistags('xyz'), dtype=np.uint8) m = vigra.VigraArray(v, axistags=vigra.defaultAxistags('xyz'), dtype=np.uint8) m[:] = 0 m[0, 0, 2] = 1 for interpolationMethod in ['cubic']: self.op.InputVolume.setValue(v) self.op.Missing.setValue(m) self.op.InterpolationMethod.setValue(interpolationMethod) self.op.InputVolume.setValue(v) out = self.op.Output[:].wait().view(np.ndarray) # natural comparison self.assertEqual(out[0, 0, 2], 0)
def test_OpMaxChannelIndicatorOperator_unexpected_roi_raises(graph): data = vigra.VigraArray(numpy.zeros((1, 3, 5, 7, 2)), axistags=vigra.defaultAxistags("tzyxc")) with pytest.raises(InvalidRoiException): # automatically connects op1.Output to op2.Input ... with Pipeline(graph=graph) as pipe_line: pipe_line.add(OpArrayPiper, Input=data) pipe_line.add(OpMaxChannelIndicatorOperator) pipe_line[-1].Output[()].wait()
def setup_method(self, method): self.graph = lazyflow.graph.Graph() self.testDataH5FileName = "bigH5TestData.h5" self.testDataN5FileName = "bigN5TestData.n5" self.datasetInternalPath = "volume/data" # Generate some test data self.dataShape = (1, 10, 128, 128, 1) self.testData = vigra.VigraArray( self.dataShape, axistags=vigra.defaultAxistags("txyzc"), order="C") self.testData[...] = numpy.indices(self.dataShape).sum(0)
def setUp(self): self.graph = lazyflow.graph.Graph() self._tmpdir = tempfile.mkdtemp() # Generate some test data self.dataShape = (1, 10, 64, 128, 2) self._axisorder = 'tzyxc' self.testData = vigra.VigraArray( self.dataShape, axistags=vigra.defaultAxistags(self._axisorder), order='C' ).astype(numpy.uint8) self.testData[...] = numpy.indices(self.dataShape).sum(0)
def test_OpMaxChannelIndicatorOperator_all_zeros(graph): """Special case for all zeros in probability maps""" data = vigra.VigraArray(numpy.zeros((1, 3, 5, 7, 2)), axistags=vigra.defaultAxistags("tzyxc")) # automatically connects op1.Output to op2.Input ... with Pipeline(graph=graph) as pipe_line: pipe_line.add(OpArrayPiper, Input=data) pipe_line.add(OpMaxChannelIndicatorOperator) for c in range(data.channels): assert not numpy.any(pipe_line[-1].Output[..., c].wait())
def _volume(nx=64,ny=64,nz=100,method='linear'): b = vigra.VigraArray( np.ones((nx,ny,nz)), axistags=vigra.defaultAxistags('xyz') ) if method == 'linear': for i in range(b.shape[2]): b[:,:,i]*=(i+1)+50 elif method == 'cubic': s = nz//3 for z in range(b.shape[2]): b[:,:,z]= (z-s)**2*z*150.0/(nz*(nz-s)**2) + 30 elif method == 'constant': b[:] = 124 else: raise NotImplementedError("unknown method '{}'.".format(method)) return b
def generalOperatorTest(self, operator): for dim in self.testDimensions: testArray = vigra.VigraArray( (20, ) * len(dim), axistags=vigra.VigraArray.defaultAxistags(dim)) testArray[(slice(2, 5, None), ) * len(dim)] = 10 operator.inputs["Input"].setValue(testArray) for i, j in [(i, j) for i, j in itertools.permutations(range(0, 10), 2) if i < j]: start = [i] * len(dim) stop = [j] * len(dim) operator.outputs["Output"](start, stop).wait()
def test_reorderd_ops_in_parent_op(self): opParent = OpParent(graph=Graph()) opParent.setupOutputs() # check that method types are treated correctly (actual tests in LazyOp) opParent.lazyOp1.meth(*test_args) opParent.lazyOp1.class_meth(*test_args) opParent.lazyOp1.static_meth(*test_args) opParent.lazyOp1.property_meth opParent.lazyOp1.except_meth(*test_args) # when accessing the child's operators they should conform with the outside order. Here, the shape of the # resulting numpy.ndarray is tested to make sure we are not just checking meta data. assert opParent.lazyOp1.out_a[:].wait().shape == outer_shape assert opParent.lazyOp1.out_b[:].wait().shape == outer_shape tags = vigra.defaultAxistags(outer_order) expect = vigra.VigraArray(outer_shape, value=3, axistags=tags) assert (expect == opParent.lazyOp1.out_b[:].wait()).all() assert expect.axistags == opParent.lazyOp1.out_b.meta.axistags assert expect.shape == opParent.lazyOp1.out_b.meta.shape assert opParent.lazyOp2.in_a[:].wait().shape == outer_shape assert opParent.lazyOp2.in_b[:].wait().shape == outer_shape assert opParent.lazyOp2.out_a[:].wait().shape == outer_shape assert opParent.lazyOp2.out_b[:].wait().shape == outer_shape tags = vigra.defaultAxistags(outer_order) expect = vigra.VigraArray(outer_shape, value=4, axistags=tags) assert (expect == opParent.lazyOp2.out_b[:].wait()).all() assert expect.axistags == opParent.lazyOp2.out_b.meta.axistags assert expect.shape == opParent.lazyOp2.out_b.meta.shape # checking inner shapes by pretending to make inner calls opParent.lazyOp2._inner_call = True assert opParent.lazyOp2.in_a[:].wait().shape == inner_shape assert opParent.lazyOp2.in_b[:].wait().shape == inner_shape assert opParent.lazyOp2.out_a[:].wait().shape == inner_shape assert opParent.lazyOp2.out_b[:].wait().shape == inner_shape
def setUp(self): self.graph = lazyflow.graph.Graph() self._tmpdir = tempfile.mkdtemp() self._name_pattern = 'test_stack_slice_{slice_index}.png' self._stack_filepattern = os.path.join( self._tmpdir, self._name_pattern ) # Generate some test data self.dataShape = (1, 10, 64, 128, 2) self._axisorder = 'tzyxc' self.testData = vigra.VigraArray( self.dataShape, axistags=vigra.defaultAxistags(self._axisorder), order='C' ).astype(numpy.uint8) self.testData[...] = numpy.indices(self.dataShape).sum(0)
def setupClass(cls): cls.tmpdir = tempfile.mkdtemp() cls.imgFileNames2D = [] cls.imgFileNames2Dc = [] # Comparison of compressed data not possible - those types will be # skipped in raw comparison: cls.compressedExtensions = ['.jpg', '.jpeg'] cls.projectFileName = os.path.join(cls.tmpdir, 'testProject.ilp') # Create a couple test images of different types # in order to simplify and unify testing among the different file types # the extra dimension is added, as vigra would add one anyway. cls.imgData2D = numpy.random.randint(0, 255, (10, 11, 1)).astype(numpy.uint8) # v- image data variables in order to reflect the correct axis-order # otherwise the axes get scrambled when writing/reloading vimgData2D = vigra.VigraArray(cls.imgData2D, axistags=vigra.defaultAxistags('yxc'), dtype=numpy.uint8) testNpyFileName = os.path.join(cls.tmpdir, "testimage2D.npy") numpy.save(testNpyFileName, cls.imgData2D) cls.imgFileNames2D.append(testNpyFileName) testNpzFileName = os.path.join(cls.tmpdir, "testimage2D.npz") numpy.savez(testNpzFileName, data=cls.imgData2D) testNpzFileName = "{}/data".format(testNpzFileName) cls.imgFileNames2D.append(testNpzFileName) testH5FileName = os.path.join(cls.tmpdir, "testimage2D.h5") vigra.impex.writeHDF5(data=cls.imgData2D, filenameOrGroup=testH5FileName, pathInFile='test/data') testH5FileName = "{}/test/data".format(testH5FileName) cls.imgFileNames2D.append(testH5FileName) for extension in vigra.impex.listExtensions().split(' '): tmpFileName = os.path.join(cls.tmpdir, "testImage2D.{}".format(extension)) # not all extensions support this kind of pixeltype try: vigra.impex.writeImage( vimgData2D, tmpFileName, ) cls.imgFileNames2D.append(tmpFileName) except RuntimeError, e: msg = str(e).replace('\n', '') print( "Couldn't write temp 2D image file using vigra with `{}` " "extension : {}".format(extension, msg))
def reorder_axes(input_arr: numpy.ndarray, *, from_axes_tags: str, to_axes_tags: str): if isinstance(from_axes_tags, AxisTags): from_axes_tags = "".join(from_axes_tags.keys()) if isinstance(to_axes_tags, AxisTags): to_axes_tags = "".join(to_axes_tags.keys()) op = OpReorderAxes(graph=Graph()) tagged_arr = vigra.VigraArray(input_arr, axistags=vigra.defaultAxistags(from_axes_tags)) op.Input.setValue(tagged_arr) op.AxisOrder.setValue(to_axes_tags) return op.Output([]).wait()
def get_result_operator(input_data): """ Returns the result of the wrapping operator pipeline, which gets accessed via the GUI. """ input_data = vigra.VigraArray(input_data, axistags=vigra.defaultAxistags(AXIS_TAGS)) graph = Graph() with Pipeline(graph=graph) as get_wsdt: get_wsdt.add(OpArrayPiper, Input=input_data) get_wsdt.add(OpCachedWsdt, FreezeCache=False) wsdt_result = get_wsdt[-1].outputs["Superpixels"][:].wait() return wsdt_result
def prepareVolnOp(self): tags = random.sample(self.axis, random.randint(2, len(self.axis))) tagStr = '' for s in tags: tagStr += s axisTags = vigra.defaultAxistags(tagStr) self.shape = [] for tag in axisTags: self.shape.append(random.randint(20, 30)) self.array = (numpy.random.rand(*tuple(self.shape)) * 255) self.array = (float(250) / 255 * self.array + 5).astype(int) self.inArray = vigra.VigraArray(self.array, axistags=axisTags) self.operator.inputs["input"].setValue(self.inArray)
def test_OpMaxChannelIndicatorOperator(graph): data = vigra.VigraArray(numpy.zeros((1, 3, 5, 7, 2)), axistags=vigra.defaultAxistags("tzyxc")) data[:, :, :, ::2, 0] = 1 data[:, :, :, 1::2, 1] = 1 expected = data == 1 # automatically connects op1.Output to op2.Input ... with Pipeline(graph=graph) as pipe_line: pipe_line.add(OpArrayPiper, Input=data) pipe_line.add(OpMaxChannelIndicatorOperator) for c in range(data.channels): numpy.testing.assert_array_equal( pipe_line[-1].Output[..., c].wait(), expected[..., c, None])
def prepareVolnOp(self, possible_axes='tzyxc', num=5): tags = random.sample(possible_axes, random.randint(2, num)) tagStr = '' for s in tags: tagStr += s axisTags = vigra.defaultAxistags(tagStr) self.shape = [] for tag in axisTags: self.shape.append(random.randint(20, 30)) self.array = (numpy.random.rand(*tuple(self.shape)) * 255) self.array = (float(250) / 255 * self.array + 5).astype(int) self.inArray = vigra.VigraArray(self.array, axistags=axisTags) opProvider = OpArrayProvider(graph=self.operator.graph) opProvider.Input.setValue(self.inArray) self.operator.Input.connect(opProvider.Output)
def extend_data_tzyxc(self, data_nr=None): """Extends the dimension of the dataset and its labels to tzyxc. If data_nr is None, all datasets are extended. :param data_nr: number of dataset """ if data_nr is None: for i in range(self.data_count): self.extend_data_tzyxc(i) else: # Reshape the data with the correct axistags. data = self.get_data(data_nr) axisorder = self.get_axisorder(data_nr) if not hasattr(data, "axistags"): data = vigra.VigraArray( data, axistags=vigra.defaultAxistags(axisorder), dtype=data.dtype) new_data = reshape_tzyxc(data) # Save the reshaped dataset. output_folder, output_filename = os.path.split( self.get_cache_data_path(data_nr)) output_path = os.path.join( output_folder, str(data_nr).zfill(4) + "_" + output_filename) if self.is_internal(data_nr): output_key = self.get_dataset_id(data_nr) else: output_key = self.get_data_key(data_nr) vigra.writeHDF5(new_data, output_path, output_key, compression=self._compression) # Update the project file. self.set_data_path_key(data_nr, output_path, output_key) self._set_internal(data_nr, False) self.set_axisorder(data_nr, "tzyxc") self._set_axistags_from_data(data_nr) # If the dataset has labels, reshape them. if self._label_block_count(data_nr) > 0: self._reshape_labels(data_nr, axisorder, "tzyxc")
def test_OpMaxChannelIndicatorOperator_tied_pixels(graph): data = vigra.VigraArray(numpy.zeros((1, 3, 5, 7, 2)), axistags=vigra.defaultAxistags("tzyxc")) data[..., 0] = 1 data[:, :, 0:2, 0:2, :] = 0.5 # for tied pixels we expect the first occurrence to "win" expected = numpy.zeros_like(data) expected[..., 0] = 1 # automatically connects op1.Output to op2.Input ... with Pipeline(graph=graph) as pipe_line: pipe_line.add(OpArrayPiper, Input=data) pipe_line.add(OpMaxChannelIndicatorOperator) for c in range(data.channels): numpy.testing.assert_array_equal( pipe_line[-1].Output[..., c].wait(), expected[..., c, None])