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)
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()')
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)
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")
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')
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 ]))
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)
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)
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')
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')
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)
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)
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)
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)
def test_03_02_cw_from_string(self): '''Get a class wrapper from a string''' c = J.get_class_wrapper("java.lang.Number")
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')
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')
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]))
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()')
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)
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))
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
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')
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))
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')