예제 #1
0
 def allowOpenToCheckType(self, allow):
     '''Allow the "isThisType" function to open files
     
     For the cluster, you want to tell potential file formats
     not to open the image file to test if it's their format.
     '''
     if not hasattr(self, "allowOpenToCheckType_method"):
         self.allowOpenToCheckType_method = None
         class_wrapper = jutil.get_class_wrapper(self.o)
         methods = class_wrapper.getMethods()
         for method in jutil.get_env().get_object_array_elements(methods):
             m = jutil.get_method_wrapper(method)
             if m.getName() in ('allowOpenToCheckType', 'setAllowOpenFiles'):
                 self.allowOpenToCheckType_method = m
     if self.allowOpenToCheckType_method is not None:
         object_class = env.find_class('java/lang/Object')
         jexception = jutil.get_env().exception_occurred()
         if jexception is not None:
             raise jutil.JavaException(jexception)
         
         boolean_value = jutil.make_instance('java/lang/Boolean', 
                                             '(Z)V', allow)
         args = jutil.get_env().make_object_array(1, object_class)
         jexception = jutil.get_env().exception_occurred()
         if jexception is not None:
             raise jutil.JavaException(jexception)
         jutil.get_env().set_object_array_element(args, 0, boolean_value)
         jexception = jutil.get_env().exception_occurred()
         if jexception is not None:
             raise jutil.JavaException(jexception)
         self.allowOpenToCheckType_method.invoke(self.o, args)
예제 #2
0
 def test_03_05_cw_get_annotations(self):
     c = J.get_class_wrapper('java.security.Identity')
     annotations = c.getAnnotations()
     annotations = J.get_env().get_object_array_elements(annotations)
     self.assertEqual(len(annotations), 1)
     self.assertEqual(J.to_string(annotations[0]),
                      '@java.lang.Deprecated()')
예제 #3
0
 def test_03_08_cw_get_field(self):
     c = J.get_class_wrapper('java.lang.String')
     field = c.getField('CASE_INSENSITIVE_ORDER')
     modifiers = J.call(field, 'getModifiers', '()I')
     static = J.get_static_field('java/lang/reflect/Modifier', 'STATIC',
                                 'I')
     self.assertEqual((modifiers & static), static)
예제 #4
0
 def test_03_07_cw_get_fields(self):
     c = J.get_class_wrapper('java.lang.String')
     fields = c.getFields()
     fields = J.get_env().get_object_array_elements(fields)
     self.assertEqual(len(fields), 1)
     self.assertEqual(J.call(fields[0], 'getName', '()Ljava/lang/String;'),
                      "CASE_INSENSITIVE_ORDER")
예제 #5
0
 def test_03_07_cw_get_fields(self):
     c = J.get_class_wrapper('java.lang.String')
     fields = c.getFields()
     fields = J.get_env().get_object_array_elements(fields)
     self.assertEqual(len(fields), 1)
     self.assertEqual(J.call(fields[0], 'getName', '()Ljava/lang/String;'),
                      "CASE_INSENSITIVE_ORDER")
예제 #6
0
 def test_03_11_cw_get_constructor(self):
     c = J.get_class_wrapper('java.lang.String')
     sclass = J.class_for_name('java.lang.String')
     constructor = c.getConstructor([sclass])
     self.assertEqual(
         J.call(constructor, 'getName', '()Ljava/lang/String;'),
         'java.lang.String')
예제 #7
0
 def test_03_10_cw_get_methods(self):
     c = J.get_class_wrapper('java.lang.String')
     mmm = J.get_env().get_object_array_elements(c.getMethods())
     self.assertTrue(
         any([
             J.call(m, 'getName', '()Ljava/lang/String;') == 'concat'
             for m in mmm
         ]))
예제 #8
0
def get_plugin(classname):
    """Return an instance of the named plugin"""
    if classname.startswith("ij."):
        cls = J.class_for_name(classname)
    else:
        cls = J.class_for_name(classname, get_user_loader())
    cls = J.get_class_wrapper(cls, True)
    constructor = J.get_constructor_wrapper(cls.getConstructor(None))
    return constructor.newInstance(None)
예제 #9
0
def get_plugin(classname):
    '''Return an instance of the named plugin'''
    if classname.startswith("ij."):
        cls = J.class_for_name(classname)
    else:
        cls = J.class_for_name(classname, get_user_loader())
    cls = J.get_class_wrapper(cls, True)
    constructor = J.get_constructor_wrapper(cls.getConstructor(None))
    return constructor.newInstance(None)
예제 #10
0
 def test_03_09_cw_get_method(self):
     sclass = J.class_for_name('java.lang.String')
     iclass = J.get_static_field('java/lang/Integer', 'TYPE', 
                                 'Ljava/lang/Class;')
     c = J.get_class_wrapper('java.lang.String')
     m = c.getMethod('charAt', [ iclass ])
     self.assertEqual(J.to_string(J.call(m, 'getReturnType', '()Ljava/lang/Class;')), 'char')
     m = c.getMethod('concat', [ sclass])
     self.assertEqual(J.to_string(J.call(m, 'getReturnType', '()Ljava/lang/Class;')), 
                      'class java.lang.String')
예제 #11
0
 def test_03_09_cw_get_method(self):
     sclass = J.class_for_name('java.lang.String')
     iclass = J.get_static_field('java/lang/Integer', 'TYPE',
                                 'Ljava/lang/Class;')
     c = J.get_class_wrapper('java.lang.String')
     m = c.getMethod('charAt', [iclass])
     self.assertEqual(
         J.to_string(J.call(m, 'getReturnType', '()Ljava/lang/Class;')),
         'char')
     m = c.getMethod('concat', [sclass])
     self.assertEqual(
         J.to_string(J.call(m, 'getReturnType', '()Ljava/lang/Class;')),
         'class java.lang.String')
예제 #12
0
def get_field_type(field):
    '''Determine the data type of the field
    
    field - a field returned from get_input/output_fields_and_parameters
    
    Returns one of FT_INTEGER, FT_FLOAT, FT_IMAGE, FT_BOOL
    '''
    t = field.getType()
    # t is a class itself, to wrap, we replace the class of class (= Class)
    # with t
    tc = J.get_class_wrapper(t)
    tc.o = t
    name = tc.getCanonicalName()
    return field_mapping.get(name, None)
예제 #13
0
def get_field_type(field):
    '''Determine the data type of the field
    
    field - a field returned from get_input/output_fields_and_parameters
    
    Returns one of FT_INTEGER, FT_FLOAT, FT_IMAGE, FT_BOOL
    '''
    t = field.getType()
    # t is a class itself, to wrap, we replace the class of class (= Class)
    # with t
    tc = J.get_class_wrapper(t)
    tc.o = t
    name = tc.getCanonicalName()
    return field_mapping.get(name, None)
예제 #14
0
 def test_03_06_cw_get_constructors(self):
     c = J.get_class_wrapper('java.lang.String')
     constructors = c.getConstructors()
     constructors = J.get_env().get_object_array_elements(constructors)
     self.assertEqual(len(constructors), 15)
예제 #15
0
 def test_03_04_cw_get_annotation(self):
     c = J.get_class_wrapper('java.security.Identity')
     annotation = c.getAnnotation(J.class_for_name('java.lang.Deprecated'))
     self.assertTrue(annotation is not None)
예제 #16
0
 def test_03_03_cw_get_classes(self):
     c = J.get_class_wrapper('java.lang.Number')
     classes = c.getClasses()
     self.assertEqual(len(J.get_env().get_object_array_elements(classes)),
                      0)
예제 #17
0
 def test_03_02_cw_from_string(self):
     '''Get a class wrapper from a string'''
     c = J.get_class_wrapper("java.lang.Number")
예제 #18
0
 def test_03_03_cw_get_classes(self):
     c = J.get_class_wrapper('java.lang.Number')
     classes = c.getClasses()
     self.assertEqual(len(J.get_env().get_object_array_elements(classes)), 0)
예제 #19
0
 def test_04_03_field_type(self):
     c = J.get_class_wrapper('java.lang.Byte')
     f = J.get_field_wrapper(c.getField('MAX_VALUE'))
     t = f.getType()
     self.assertEqual(J.to_string(t), 'byte')
예제 #20
0
 def test_03_04_cw_get_annotation(self):
     c = J.get_class_wrapper('java.security.Identity')
     annotation = c.getAnnotation(J.class_for_name('java.lang.Deprecated'))
     self.assertTrue(annotation is not None)
예제 #21
0
 def test_04_01_field_get(self):
     c = J.get_class_wrapper('java.lang.Byte')
     f = J.get_field_wrapper(c.getField('MAX_VALUE'))
     v = f.get(None)
     self.assertEqual(J.to_string(v), '127')
예제 #22
0
 def test_03_10_cw_get_methods(self):
     c = J.get_class_wrapper('java.lang.String')
     mmm = J.get_env().get_object_array_elements(c.getMethods())
     self.assertTrue(any([J.call(m, 'getName', '()Ljava/lang/String;') == 'concat'
                          for m in mmm]))
예제 #23
0
 def test_03_05_cw_get_annotations(self):
     c = J.get_class_wrapper('java.security.Identity')
     annotations = c.getAnnotations()
     annotations = J.get_env().get_object_array_elements(annotations)
     self.assertEqual(len(annotations), 1)
     self.assertEqual(J.to_string(annotations[0]),'@java.lang.Deprecated()')
예제 #24
0
 def test_03_08_cw_get_field(self):
     c = J.get_class_wrapper('java.lang.String')
     field = c.getField('CASE_INSENSITIVE_ORDER')
     modifiers = J.call(field, 'getModifiers', '()I')
     static = J.get_static_field('java/lang/reflect/Modifier','STATIC','I')
     self.assertEqual((modifiers & static), static)
예제 #25
0
 def test_03_01_cw_from_class(self):
     '''Get a class wrapper from a class'''
     c = J.get_class_wrapper(J.make_instance('java/lang/Integer', '(I)V',
                                             14))
예제 #26
0
 def test_03_06_cw_get_constructors(self):
     c = J.get_class_wrapper('java.lang.String')
     constructors = c.getConstructors()
     constructors = J.get_env().get_object_array_elements(constructors)
     self.assertEqual(len(constructors), 15)
예제 #27
0
 def fn(path=path, c=c, z=z, t=t, series=series, index=index,
        rescale=rescale, wants_max_intensity=wants_max_intensity,
        channel_names=channel_names):
     FormatTools = make_format_tools_class()
     ImageReader = make_image_reader_class()
     ChannelSeparator = make_reader_wrapper_class(
         "loci/formats/ChannelSeparator")
     
     #
     # Bioformats is more picky about slashes than Python
     #
     if sys.platform.startswith("win"):
         path = path.replace("/",os.path.sep)
     #
     # Bypass the ImageReader and scroll through the class list. The
     # goal here is to ask the FormatHandler if it thinks it could
     # possibly parse the file, then only give the FormatReader access
     # to the open file stream so it can't damage the file server.
     #
     
     env = jutil.get_env()
     class_list = get_class_list()
     stream = jutil.make_instance('loci/common/RandomAccessInputStream',
                                  '(Ljava/lang/String;)V', path)
     filename = os.path.split(path)[1]
     IFormatReader = make_iformat_reader_class()
     rdr = None
     for klass in env.get_object_array_elements(class_list.get_classes()):
         wclass = jutil.get_class_wrapper(klass, True)
         maybe_rdr = IFormatReader()
         maybe_rdr.o = wclass.newInstance()
         maybe_rdr.setGroupFiles(False)
         if maybe_rdr.suffixNecessary:
             if not maybe_rdr.isThisTypeSZ(filename, False):
                 continue
             if maybe_rdr.suffixSufficient:
                 rdr = maybe_rdr
                 break
         if (maybe_rdr.isThisTypeStream(stream)):
             rdr = maybe_rdr
             break
     if rdr is None:
         raise ValueError("Could not find a Bio-Formats reader for %s", path)
     mdoptions = metadatatools.get_metadata_options(metadatatools.ALL)
     rdr.setMetadataOptions(mdoptions)
     metadata = metadatatools.createOMEXMLMetadata()
     rdr.setMetadataStore(metadata)
     rdr.setId(path)
     width = rdr.getSizeX()
     height = rdr.getSizeY()
     pixel_type = rdr.getPixelType()
     little_endian = rdr.isLittleEndian()
     if pixel_type == FormatTools.INT8:
         dtype = np.char
         scale = 255
     elif pixel_type == FormatTools.UINT8:
         dtype = np.uint8
         scale = 255
     elif pixel_type == FormatTools.UINT16:
         dtype = '<u2' if little_endian else '>u2'
         scale = 65535
     elif pixel_type == FormatTools.INT16:
         dtype = '<i2' if little_endian else '>i2'
         scale = 65535
     elif pixel_type == FormatTools.UINT32:
         dtype = '<u4' if little_endian else '>u4'
         scale = 2**32
     elif pixel_type == FormatTools.INT32:
         dtype = '<i4' if little_endian else '>i4'
         scale = 2**32-1
     elif pixel_type == FormatTools.FLOAT:
         dtype = '<f4' if little_endian else '>f4'
         scale = 1
     elif pixel_type == FormatTools.DOUBLE:
         dtype = '<f8' if little_endian else '>f8'
         scale = 1
     max_sample_value = rdr.getMetadataValue('MaxSampleValue')
     if max_sample_value is not None:
         try:
             scale = jutil.call(max_sample_value, 'intValue', '()I')
         except:
             bioformats.logger.warning("WARNING: failed to get MaxSampleValue for image. Intensities may be improperly scaled.")
     if series is not None:
         rdr.setSeries(series)
     if index is not None:
         image = np.frombuffer(rdr.openBytes(index), dtype)
         if len(image) / height / width in (3,4):
             image.shape = (height, width, int(len(image) / height / width))
         else:
             image.shape = (height, width)
     elif rdr.isRGB() and rdr.isInterleaved():
         index = rdr.getIndex(z,0,t)
         image = np.frombuffer(rdr.openBytes(index), dtype)
         image.shape = (height, width, 3)
     elif c is not None and rdr.getRGBChannelCount() == 1:
         index = rdr.getIndex(z,c,t)
         image = np.frombuffer(rdr.openBytes(index), dtype)
         image.shape = (height, width)
     elif rdr.getRGBChannelCount() > 1:
         rdr.close()
         rdr = ImageReader()
         rdr.allowOpenToCheckType(False)
         rdr = ChannelSeparator(rdr)
         rdr.setGroupFiles(False)
         rdr.setId(path)
         red_image, green_image, blue_image = [
             np.frombuffer(rdr.openBytes(rdr.getIndex(z,i,t)),dtype)
             for i in range(3)]
         image = np.dstack((red_image, green_image, blue_image))
         image.shape=(height,width,3)
     elif rdr.getSizeC() > 1:
         images = [np.frombuffer(rdr.openBytes(rdr.getIndex(z,i,t)), dtype)
                   for i in range(rdr.getSizeC())]
         image = np.dstack(images)
         image.shape = (height, width, rdr.getSizeC())
         if not channel_names is None:
             metadata = metadatatools.MetadataRetrieve(metadata)
             for i in range(rdr.getSizeC()):
                 index = rdr.getIndex(z, 0, t)
                 channel_name = metadata.getChannelName(index, i)
                 if channel_name is None:
                     channel_name = metadata.getChannelID(index, i)
                 channel_names.append(channel_name)
     else:
         index = rdr.getIndex(z,0,t)
         image = np.frombuffer(rdr.openBytes(index),dtype)
         image.shape = (height,width)
         
     rdr.close()
     jutil.call(stream, 'close', '()V')
     del rdr
     #
     # Run the Java garbage collector here.
     #
     jutil.static_call("java/lang/System", "gc","()V")
     if rescale:
         image = image.astype(np.float32) / float(scale)
     if wants_max_intensity:
         return image, scale
     return image
예제 #28
0
 def test_04_01_field_get(self):
     c = J.get_class_wrapper('java.lang.Byte')
     f = J.get_field_wrapper(c.getField('MAX_VALUE'))
     v = f.get(None)
     self.assertEqual(J.to_string(v), '127')
예제 #29
0
 def test_04_02_field_name(self):
     c = J.get_class_wrapper('java.lang.Byte')
     f = J.get_field_wrapper(c.getField('MAX_VALUE'))
     self.assertEqual(f.getName(), 'MAX_VALUE')
예제 #30
0
 def test_04_02_field_name(self):
     c = J.get_class_wrapper('java.lang.Byte')
     f = J.get_field_wrapper(c.getField('MAX_VALUE'))
     self.assertEqual(f.getName(), 'MAX_VALUE')
예제 #31
0
 def test_04_03_field_type(self):
     c = J.get_class_wrapper('java.lang.Byte')
     f = J.get_field_wrapper(c.getField('MAX_VALUE'))
     t = f.getType()
     self.assertEqual(J.to_string(t), 'byte')
예제 #32
0
 def test_03_01_cw_from_class(self):
     '''Get a class wrapper from a class'''
     c = J.get_class_wrapper(
         J.make_instance('java/lang/Integer', '(I)V', 14))
예제 #33
0
 def test_03_11_cw_get_constructor(self):
     c = J.get_class_wrapper('java.lang.String')
     sclass = J.class_for_name('java.lang.String')
     constructor = c.getConstructor([sclass])
     self.assertEqual(J.call(constructor, 'getName', '()Ljava/lang/String;'),
                      'java.lang.String')
예제 #34
0
 def test_03_02_cw_from_string(self):
     '''Get a class wrapper from a string'''
     c = J.get_class_wrapper("java.lang.Number")