示例#1
0
 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()))
示例#2
0
    def testStructureWithSomeUnsupportedItems(self):
        # Create Java data structure with some challenging items.
        Object = jimport('java.lang.Object')
        jmap = to_java({
            'list': ['a', Object(), 1],
            'set': {'x', Object(), 2},
            'object': Object(),
            'foo': 'bar'
        })
        self.assertEqual('java.util.LinkedHashMap', jclass(jmap).getName())

        # Convert it back to Python.
        pdict = to_python(jmap)
        l = pdict['list']
        self.assertEqual(pdict['list'][0], 'a')
        assert type(pdict['list'][1]) == Object
        assert pdict['list'][2] == 1
        assert 'x' in pdict['set']
        assert 2 in pdict['set']
        assert len(pdict['set']) == 3
        assert type(pdict['object']) == Object
        self.assertEqual(pdict['foo'], 'bar')
示例#3
0
    def testClass(self):
        """
        Tests class detection from Java objects.
        """
        int_class = jclass(to_java(5))
        self.assertEqual('java.lang.Integer', int_class.getName())

        long_class = jclass(to_java(4000000001))
        self.assertEqual('java.lang.Long', long_class.getName())

        bigint_class = jclass(to_java(9879999999999999789))
        self.assertEqual('java.math.BigInteger', bigint_class.getName())

        string_class = jclass(to_java('foobar'))
        self.assertEqual('java.lang.String', string_class.getName())

        list_class = jclass(to_java([1, 2, 3]))
        self.assertEqual('java.util.ArrayList', list_class.getName())

        map_class = jclass(to_java({'a':'b'}))
        self.assertEqual('java.util.LinkedHashMap', map_class.getName())

        self.assertEqual('java.util.Map', jclass('java.util.Map').getName())
示例#4
0
文件: imagej.py 项目: oeway/pyimagej
        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()))