def to_dataset(self, data): """Converts the data into an ImageJ dataset""" if self._is_xarraylike(data): return self._xarray_to_dataset(data) if self._is_arraylike(data): return self._numpy_to_dataset(data) if scyjava.isjava(data): return self._java_to_dataset(data) raise TypeError(f'Type not supported: {type(data)}')
def from_java(self, data): """ Converts the data into a python equivalent """ if not isjava(data): return data if self._ij.convert().supports(data, Dataset): # HACK: Converter exists for ImagePlus -> Dataset, but not ImagePlus -> RAI. data = self._ij.convert().convert(data, Dataset) if (self._ij.convert().supports(data, RandomAccessibleInterval)): rai = self._ij.convert().convert(data, RandomAccessibleInterval) return self.rai_to_numpy(rai) return to_python(data)
def dims(self, image): """ Return the dimensions of the equivalent numpy array for the image. Reverse dimension order from Java. """ if isinstance(image, numpy.ndarray): return image.shape if not isjava(image): raise TypeError('Unsupported type: ' + str(type(image))) if jclass('net.imglib2.Dimensions').isInstance(image): return [image.dimension(d) for d in range(image.numDimensions() -1, -1, -1)] if jclass('ij.ImagePlus').isInstance(image): dims = image.getDimensions() dims.reverse() dims = [dim for dim in dims if dim > 1] return dims raise TypeError('Unsupported Java type: ' + str(jclass(image).getName()))
def from_java(self, data): """ Converts the data into a python equivalent """ # todo: convert a datset to xarray if not isjava(data): return data try: if self._ij.convert().supports(data, Dataset): # HACK: Converter exists for ImagePlus -> Dataset, but not ImagePlus -> RAI. data = self._ij.convert().convert(data, Dataset) return self._dataset_to_xarray(data) if self._ij.convert().supports(data, RandomAccessibleInterval): rai = self._ij.convert().convert(data, RandomAccessibleInterval) return self.rai_to_numpy(rai) except Exception as exc: _dump_exception(exc) raise exc return to_python(data)
def dtype(self, image_or_type): """ Return the dtype of the equivalent numpy array for the given image or type. """ if type(image_or_type) == numpy.dtype: return image_or_type if self._is_arraylike(image_or_type): return image_or_type.dtype if not isjava(image_or_type): raise TypeError('Unsupported type: ' + str(type(image_or_type))) # -- ImgLib2 types -- if jclass('net.imglib2.type.Type').isInstance(image_or_type): ij2_types = { 'net.imglib2.type.logic.BitType': 'bool', 'net.imglib2.type.numeric.integer.ByteType': 'int8', 'net.imglib2.type.numeric.integer.ShortType': 'int16', 'net.imglib2.type.numeric.integer.IntType': 'int32', 'net.imglib2.type.numeric.integer.LongType': 'int64', 'net.imglib2.type.numeric.integer.UnsignedByteType': 'uint8', 'net.imglib2.type.numeric.integer.UnsignedShortType': 'uint16', 'net.imglib2.type.numeric.integer.UnsignedIntType': 'uint32', 'net.imglib2.type.numeric.integer.UnsignedLongType': 'uint64', 'net.imglib2.type.numeric.real.FloatType': 'float32', 'net.imglib2.type.numeric.real.DoubleType': 'float64', } for c in ij2_types: if jclass(c).isInstance(image_or_type): return numpy.dtype(ij2_types[c]) raise TypeError( 'Unsupported ImgLib2 type: {}'.format(image_or_type)) # -- ImgLib2 images -- if jclass('net.imglib2.IterableInterval').isInstance( image_or_type): ij2_type = image_or_type.firstElement() return self.dtype(ij2_type) if jclass('net.imglib2.RandomAccessibleInterval').isInstance( image_or_type): Util = autoclass('net.imglib2.util.Util') ij2_type = Util.getTypeFromInterval(image_or_type) return self.dtype(ij2_type) # -- ImageJ1 images -- if jclass('ij.ImagePlus').isInstance(image_or_type): ij1_type = image_or_type.getType() ImagePlus = autoclass('ij.ImagePlus') ij1_types = { ImagePlus.GRAY8: 'uint8', ImagePlus.GRAY16: 'uint16', ImagePlus.GRAY32: 'float32', # NB: ImageJ1's 32-bit type is float32, not uint32. } for t in ij1_types: if ij1_type == t: return numpy.dtype(ij1_types[t]) raise TypeError( 'Unsupported ImageJ1 type: {}'.format(ij1_type)) raise TypeError('Unsupported Java type: ' + str(jclass(image_or_type).getName()))