def setupOutputs(self): filename = self.Filename.value info = vigra.impex.ImageInfo(filename) assert [tag.key for tag in info.getAxisTags()] == ["x", "y", "c"] shape_xyc = info.getShape() shape_yxc = (shape_xyc[1], shape_xyc[0], shape_xyc[2]) self.Image.meta.dtype = info.getDtype() self.Image.meta.prefer_2d = True numImages = vigra.impex.numberImages(filename) if numImages == 1: # For 2D, we use order yxc. self.Image.meta.shape = shape_yxc v_tags = info.getAxisTags() self.Image.meta.axistags = vigra.AxisTags([v_tags[k] for k in "yxc"]) else: # For 3D, we use zyxc # Insert z-axis shape shape_zyxc = (numImages,) + shape_yxc self.Image.meta.shape = shape_zyxc # Insert z tag z_tag = vigra.defaultAxistags("z")[0] tags_xyc = [tag for tag in info.getAxisTags()] tags_zyxc = [z_tag] + list(reversed(tags_xyc[:-1])) + tags_xyc[-1:] self.Image.meta.axistags = vigra.AxisTags(tags_zyxc)
def testUsingPreloadedArryasWhenScriptingBatchProcessing(self): args = app.parse_args([]) args.headless = True args.project = self.PROJECT_FILE shell = app.main(args) assert isinstance(shell.workflow, PixelClassificationWorkflow) # Obtain the training operator opPixelClassification = shell.workflow.pcApplet.topLevelOperator # Sanity checks assert len(opPixelClassification.InputImages) > 0 assert opPixelClassification.Classifier.ready() input_data1 = numpy.random.randint(0, 255, (2, 20, 20, 5, 1)).astype(numpy.uint8) input_data2 = numpy.random.randint(0, 255, (2, 20, 20, 5, 1)).astype(numpy.uint8) role_data_dict = { "Raw Data": [ PreloadedArrayDatasetInfo(preloaded_array=input_data1, axistags=vigra.AxisTags("tzyxc")), PreloadedArrayDatasetInfo(preloaded_array=input_data2, axistags=vigra.AxisTags("tzyxc")), ] } predictions = shell.workflow.batchProcessingApplet.run_export(role_data_dict, export_to_array=True) for result in predictions: assert result.shape == (2, 20, 20, 5, 2)
def create_axistags(self): """ Generate a vigra.AxisTags object corresponding to this VoxelsMetadata. (Requires vigra.) """ tags_f = vigra.AxisTags() tags_f.insert( 0, vigra.AxisInfo('c', typeFlags=vigra.AxisType.Channels)) dtypes = [] channel_labels = [] for channel_fields in self["Properties"]["Values"]: dtypes.append(numpy.dtype(channel_fields["DataType"]).type) channel_labels.append(channel_fields["Label"]) # We monkey-patch the channel labels onto the axistags object as a new member tags_f.channelLabels = channel_labels for axisfields in self['Axes']: key = str(axisfields["Label"]).lower() res = axisfields["Resolution"] tag = vigra.defaultAxistags(key)[0] tag.resolution = res tags_f.insert(len(tags_f), tag) # TODO: Check resolution units, because apparently # they can be different from one axis to the next... assert all( [dtype == dtypes[0] for dtype in dtypes] ), \ "Can't support heterogeneous channel types: {}".format( dtypes ) # Reverse from F-order to C-order tags_c = vigra.AxisTags(list(tags_f)[::-1]) return tags_c
def setup_method(self, method): self.graph = Graph() self.operator_identity_1 = OpArrayPiper(graph=self.graph) self.operator_identity_2 = OpArrayPiper(graph=self.graph) self.operator_identity_1.Input.meta.axistags = vigra.AxisTags("txyzc") self.operator_identity_2.Input.meta.axistags = vigra.AxisTags("txyzc")
def testHDF5(self): logger.info("Generating sample data...") sampleData = numpy.indices((150, 250, 150), dtype=numpy.float32).sum(0) sampleData = sampleData.view(vigra.VigraArray) sampleData.axistags = vigra.defaultAxistags('xyz') graph = Graph() opData = OpArrayPiper(graph=graph) opData.Input.setValue(sampleData) op = OpCompressedCache(parent=None, graph=graph) #logger.debug("Setting block shape...") op.BlockShape.setValue([75, 125, 150]) op.Input.connect(opData.Output) assert op.OutputHdf5.ready() slicing = numpy.s_[0:75, 125:250, 0:150] slicing_str = str( [list(_) for _ in zip(*[[_.start, _.stop] for _ in slicing])]) expectedData = sampleData[slicing].view(numpy.ndarray) slicing_2 = numpy.s_[0:75, 0:125, 0:150] expectedData_2 = expectedData[slicing_2].view(numpy.ndarray) #logger.debug("Requesting data...") tempdir = tempfile.mkdtemp() try: with h5py.File(os.path.join(tempdir, "data.h5"), "w") as h5_file: op.OutputHdf5[slicing].writeInto(h5_file).wait() assert slicing_str in h5_file, "Missing dataset!" assert (h5_file[slicing_str][()] == expectedData ).all(), "Incorrect output!" with h5py.File(os.path.join(tempdir, "data.h5"), "r") as h5_file: graph = Graph() opData = OpArrayPiper(graph=graph) opData.Input.meta.axistags = vigra.AxisTags('xyz') opData.Input.setValue(numpy.empty_like(expectedData_2)) op = OpCompressedCache(parent=None, graph=graph) op.InputHdf5.meta.axistags = vigra.AxisTags('xyz') op.InputHdf5.meta.shape = (75, 125, 150) #logger.debug("Setting block shape...") op.BlockShape.setValue([75, 125, 150]) op.Input.connect(opData.Output) op.InputHdf5[slicing_2] = h5_file[slicing_str] result = op.Output[slicing_2].wait() assert (result == expectedData_2).all(), "Incorrect output!" finally: shutil.rmtree(tempdir)
def setUp(self): self.graph = Graph() self.operator_identity_1 = OpArrayPiper(graph=self.graph) self.operator_identity_2 = OpArrayPiper(graph=self.graph) self.operator_identity_2.Input.allow_mask = False self.operator_identity_2.Output.allow_mask = False self.operator_identity_1.Input.meta.axistags = vigra.AxisTags("txyzc") self.operator_identity_2.Input.meta.axistags = vigra.AxisTags("txyzc")
def setUp(self): self.graph = Graph() self.operator_identity_1 = OpArrayPiperWithAccessCount( graph=self.graph) self.operator_identity_2 = OpArrayPiperWithAccessCount( graph=self.graph) self.operator_identity_1.Input.meta.axistags = vigra.AxisTags("txyzc") self.operator_identity_2.Input.meta.axistags = vigra.AxisTags("txyzc")
def setup_method(self, method): self.graph = Graph() self.operator_identity_1 = OpArrayPiperWithAccessCount( graph=self.graph) self.operator_identity_2 = OpArrayPiperWithAccessCount( graph=self.graph) self.operator_identity_2.Input.allow_mask = False self.operator_identity_2.Output.allow_mask = False self.operator_identity_1.Input.meta.axistags = vigra.AxisTags("txyzc") self.operator_identity_2.Input.meta.axistags = vigra.AxisTags("txyzc")
def setup_method(self, method): self.graph = Graph() self.operator_identity = OpArrayPiperWithAccessCount(graph=self.graph) self.operator_identity.Input.meta.axistags = vigra.AxisTags("txyzc") self.operator_identity.Input.meta.has_mask = True
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): self.graph = Graph() self.operator_identity = OpArrayPiper(graph=self.graph) self.operator_identity.Input.meta.axistags = vigra.AxisTags("txyzc") self.operator_identity.Input.meta.has_mask = True
def test_withAxisTags(self): # Write it again, this time with weird axistags axistags = vigra.AxisTags( vigra.AxisInfo("x", vigra.AxisType.Space), vigra.AxisInfo("y", vigra.AxisType.Space), vigra.AxisInfo("z", vigra.AxisType.Space), vigra.AxisInfo("c", vigra.AxisType.Channels), vigra.AxisInfo("t", vigra.AxisType.Time), ) # Write the dataset to an hdf5 file # (Note: Don't use vigra to do this, which may reorder the axes) self.h5File["volume"].create_dataset("tagged_data", data=self.data) self.n5File["volume"].create_dataset("tagged_data", data=self.data) # Write the axistags attribute self.h5File["volume/tagged_data"].attrs["axistags"] = axistags.toJSON() self.n5File["volume/tagged_data"].attrs["axistags"] = axistags.toJSON() # Read the data with an operator self.h5_op.H5N5File.setValue(self.h5File) self.n5_op.H5N5File.setValue(self.n5File) self.h5_op.InternalPath.setValue("volume/tagged_data") self.n5_op.InternalPath.setValue("volume/tagged_data") assert self.h5_op.OutputImage.meta.shape == self.data.shape assert self.n5_op.OutputImage.meta.shape == self.data.shape numpy.testing.assert_array_equal(self.h5_op.OutputImage.value, self.data) numpy.testing.assert_array_equal(self.n5_op.OutputImage.value, self.data)
def setupOutputs(self): inputSlot = self.inputs["Input"] self.outputs["Output"].meta.assignFrom(inputSlot.meta) self.Output.meta.axistags = vigra.AxisTags( [vigra.AxisInfo("t"), vigra.AxisInfo("x"), vigra.AxisInfo("y"), vigra.AxisInfo("z"), vigra.AxisInfo("c")] )
def test_2d_along_t(self): data = numpy.random.randint(0, 255, (20, 100, 200, 3)).astype(numpy.uint8) expected_axistags = vigra.AxisTags([ vigra.AxisInfo("t", typeFlags=vigra.AxisType.Time), vigra.AxisInfo("y", typeFlags=vigra.AxisType.Space), vigra.AxisInfo("x", typeFlags=vigra.AxisType.Space), vigra.AxisInfo("c", typeFlags=vigra.AxisType.Channels), ]) with tempdir() as d: tiff_path = d + "/test-2d-{slice_index:02d}.tiff" tiff_glob_path = d + "/test-2d-*.tiff" for slice_index, t_slice in enumerate(data): vigra.impex.writeImage( vigra.taggedView(t_slice, "yxc"), tiff_path.format(slice_index=slice_index), dtype="NATIVE", mode="w", ) op = OpTiffSequenceReader(graph=Graph()) op.SequenceAxis.setValue("t") op.GlobString.setValue(tiff_glob_path) assert op.Output.ready() assert op.Output.meta.axistags == expected_axistags assert (op.Output[5:10, 50:100, 100:150].wait() == data[5:10, 50:100, 100:150]).all()
def setupOutputs(self): assert len(self.Input.meta.shape ) == 4, "Data must be exactly 3D+c (no time axis)" assert self.Input.meta.getAxisKeys()[-1] == 'c' assert self.Input.meta.shape[-1] == 1, "Input must be 1-channel" self.Output.meta.assignFrom(self.Input.meta) self.Output.meta.dtype = numpy.float32 self.Output.meta.shape = self.Input.meta.shape[:-1] + (3, 3) # axistags: start with input, drop channel and append i,j input_axistags = copy.copy(self.Input.meta.axistags) tag_list = [tag for tag in input_axistags] tag_list = tag_list[:-1] tag_list.append(vigra.AxisInfo('i', description='eigenvector index')) tag_list.append( vigra.AxisInfo('j', description='eigenvector component')) self.Output.meta.axistags = vigra.AxisTags(tag_list) # Calculate anisotropy factor. x_tag = self.Input.meta.axistags['x'] z_tag = self.Input.meta.axistags['z'] self.z_anisotropy_factor = 1.0 if z_tag.resolution != 0.0 and x_tag.resolution != 0.0: self.z_anisotropy_factor = z_tag.resolution / x_tag.resolution logger.debug("Anisotropy factor: {}/{} = {}".format( z_tag.resolution, x_tag.resolution, self.z_anisotropy_factor))
def setup_method(self, method): self.graph = Graph() self.operator_identity = OpArrayPiper(graph=self.graph) self.operator_identity.Input.allow_mask = False self.operator_identity.Output.allow_mask = False self.operator_identity.Input.meta.axistags = vigra.AxisTags("txyzc")
def setUp(self): self.graph = Graph() self.operator_identity = OpArrayPiperWithAccessCount(graph=self.graph) self.operator_identity.Input.allow_mask = False self.operator_identity.Output.allow_mask = False self.operator_identity.Input.meta.axistags = vigra.AxisTags("txyzc")
def setUp(self): self.graph = Graph() self.operator_border = OpMaskArray(graph=self.graph) self.operator_identity = OpArrayPiper(graph=self.graph) self.operator_border.InputArray.meta.axistags = vigra.AxisTags("txyzc") self.operator_identity.Input.connect(self.operator_border.Output)
def setupOutputs(self): # Copy the input metadata to both outputs self.Output.meta.assignFrom(self.Input.meta) self.Output.meta.shape = self.Output.meta.shape[1:] + (1, ) self.Output.meta.dtype = numpy.uint64 spatial_dims = [_ for _ in self.Output.meta.axistags if _.isSpatial()] self.Output.meta.axistags = vigra.AxisTags(*(spatial_dims + [vigra.AxisInfo.c]))
def _change_axistags_order(self, new_axiskeys): # Update tags old_axiskeys = [tag.key for tag in self.axistags] new_axisinfos = [] for key in new_axiskeys: if key in old_axiskeys: new_axisinfos.append(self.axistags[key]) else: new_axisinfos.append(vigra.defaultAxistags(key)[0]) self.axistags = vigra.AxisTags(new_axisinfos)
def setupOutputs(self): self.Output.meta.shape = self.shape self.Output.meta.dtype = self.dtype self.Output.meta.axistags = vigra.AxisTags([ vigra.AxisInfo("t"), vigra.AxisInfo("x"), vigra.AxisInfo("y"), vigra.AxisInfo("z"), vigra.AxisInfo("c"), ])
def setupOutputs(self): # Copy the input metadata to both outputs self.Output.meta.assignFrom(self.Input.meta) self.Output.meta.shape = self.Output.meta.shape[:-1] + (4, ) self.Output.meta.dtype = numpy.uint8 dims = [_ for _ in self.Output.meta.axistags if not _.isChannel()] self.Output.meta.axistags = vigra.AxisTags(*(dims + [vigra.AxisInfo.c]))
def opLabelArray(): raw_data = numpy.zeros((256, 256, 256, 1), dtype=numpy.uint32) opLabelArrays = OperatorWrapper(OpCompressedUserLabelArray, graph=Graph()) opLabelArrays.Input.resize(1) opLabelArrays.Input[0].meta.axistags = vigra.AxisTags("zyxc") opLabelArrays.Input[0].setValue(raw_data) opLabelArrays.shape.setValue(raw_data.shape) opLabelArrays.eraser.setValue(255) opLabelArrays.deleteLabel.setValue(-1) opLabelArrays.blockShape.setValue((64, 64, 64, 1)) return opLabelArrays
def setupOutputs(self): # Copy the input metadata to both outputs self.Output.meta.assignFrom(self.Input.meta) self.Output.meta.dtype = numpy.float64 spatial_dims = [_ for _ in self.Output.meta.axistags if _.isSpatial()] self.Output.meta.axistags = vigra.AxisTags(vigra.AxisInfo.c, *spatial_dims) self.Output.meta.shape = (self.K.value, ) + self.Input.meta.shape[1:-1]
def setupOutputs(self): # Copy the input metadata to both outputs self.Output.meta.assignFrom(self.Input.meta) self.Output.meta.axistags = vigra.AxisTags(*list( nanshe.util.iters.iter_with_skip_indices(self.Output.meta.axistags, self.Axis.value))) self.Output.meta.shape = self.Output.meta.shape[:self.Axis.value] +\ self.Output.meta.shape[self.Axis.value+1:] self.Output.meta.generation = self._generation
def axisTagObjectFromFlag(flag): if flag in ['x', 'y', 'z']: type = vigra.AxisType.Space elif flag == 'c': type = vigra.AxisType.Channel elif flag == 't': type = vigra.AxisType.Time else: print "Requested flag", str(flag) raise return vigra.AxisTags(vigra.AxisInfo(flag, type))
def setupOutputs(self): input_shape = self.Input.meta.shape input_axiskeys = self.Input.meta.getAxisKeys() assert input_shape[-2:] == (3,3) assert input_axiskeys[-2:] == ['i', 'j'] tag_list = [tag for tag in self.Input.meta.axistags] tag_list = tag_list[:-2] tag_list.append( vigra.defaultAxistags('c')[0] ) self.Output.meta.assignFrom(self.Input.meta) self.Output.meta.shape = input_shape[:-2] + (9,) self.Output.meta.axistags = vigra.AxisTags(tag_list)
def test_withAxisTags(self): # Write it again, this time with weird axistags axistags = vigra.AxisTags(vigra.AxisInfo('x', vigra.AxisType.Space), vigra.AxisInfo('y', vigra.AxisType.Space), vigra.AxisInfo('z', vigra.AxisType.Space), vigra.AxisInfo('c', vigra.AxisType.Channels), vigra.AxisInfo('t', vigra.AxisType.Time)) # Write the dataset to an hdf5 file # (Note: Don't use vigra to do this, which may reorder the axes) self.h5File['volume'].create_dataset('tagged_data', data=self.data) # Write the axistags attribute self.h5File['volume/tagged_data'].attrs['axistags'] = axistags.toJSON() # Read the data with an operator self.op.Hdf5File.setValue(self.h5File) self.op.InternalPath.setValue('volume/tagged_data') assert self.op.OutputImage.meta.shape == self.data.shape assert self.op.OutputImage[0, 1, 2, 1, 0].wait() == 4
def _init_objects(self): raw_data = numpy.zeros((100, 100, 100, 1), dtype=numpy.uint32) raw_data[0:15, 0:15, 0:15, 0:1] = numpy.ma.masked opLabelArrays = OperatorWrapper(OpCompressedUserLabelArray, graph=Graph()) opLabelArrays.Input.resize(1) opLabelArrays.Input[0].meta.has_mask = True opLabelArrays.Input[0].meta.axistags = vigra.AxisTags("zyxc") opLabelArrays.Input[0].setValue(raw_data) opLabelArrays.shape.setValue(raw_data.shape) opLabelArrays.eraser.setValue(255) opLabelArrays.deleteLabel.setValue(-1) opLabelArrays.blockShape.setValue((10, 10, 10, 1)) # This will serialize/deserialize data to the h5 file. slotSerializer = SerialBlockSlot(opLabelArrays.Output, opLabelArrays.Input, opLabelArrays.nonzeroBlocks) return opLabelArrays, slotSerializer
def blockwise_view(a, blockshape, aslist=False, require_aligned_blocks=True): """ Return a 2N-D view of the given N-D array, rearranged so each ND block (tile) of the original array is indexed by its block address using the first N indexes of the output array. Note: This function is nearly identical to ``skimage.util.view_as_blocks()``, except: - "imperfect" block shapes are permitted (via require_aligned_blocks=False) - only contiguous arrays are accepted. (This function will NOT silently copy your array.) As a result, the return value is *always* a view of the input. Args: a: The ND array blockshape: The tile shape aslist: If True, return all blocks as a list of ND blocks instead of a 2D array indexed by ND block coordinate. require_aligned_blocks: If True, check to make sure no data is "left over" in each row/column/etc. of the output view. That is, the blockshape must divide evenly into the full array shape. If False, "leftover" items that cannot be made into complete blocks will be discarded from the output view. Here's a 2D example (this function also works for ND): >>> a = numpy.arange(1,21).reshape(4,5) >>> print a [[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15] [16 17 18 19 20]] >>> view = blockwise_view(a, (2,2), False) >>> print view [[[[ 1 2] [ 6 7]] <BLANKLINE> [[ 3 4] [ 8 9]]] <BLANKLINE> <BLANKLINE> [[[11 12] [16 17]] <BLANKLINE> [[13 14] [18 19]]]] Inspired by the 2D example shown here: http://stackoverflow.com/a/8070716/162094 """ assert a.flags[ 'C_CONTIGUOUS'], "This function relies on the memory layout of the array." blockshape = tuple(blockshape) outershape = tuple(numpy.array(a.shape) // blockshape) view_shape = outershape + blockshape if require_aligned_blocks: assert (numpy.mod(a.shape, blockshape) == 0).all(), \ "blockshape {} must divide evenly into array shape {}"\ .format( blockshape, a.shape ) # inner strides: strides within each block (same as original array) intra_block_strides = a.strides # outer strides: strides from one block to another inter_block_strides = tuple(a.strides * numpy.array(blockshape)) # This is where the magic happens. # Generate a view with our new strides (outer+inner). view = numpy.lib.stride_tricks.as_strided(a, shape=view_shape, strides=(inter_block_strides + intra_block_strides)) # Special handling for VigraArrays if _vigra_available and isinstance(a, vigra.VigraArray) and hasattr( a, 'axistags'): view_axistags = vigra.AxisTags([vigra.AxisInfo() for _ in blockshape] + list(a.axistags)) view = vigra.taggedView(view, view_axistags) if aslist: return list(map(view.__getitem__, numpy.ndindex(outershape))) return view