def init_reader(self): mdoptions = metadatatools.get_metadata_options(metadatatools.ALL) self.rdr.setMetadataOptions(mdoptions) self.rdr.setGroupFiles(False) self.metadata = metadatatools.createOMEXMLMetadata() self.rdr.setMetadataStore(self.metadata) try: self.rdr.setId(self.path) except jutil.JavaException as e: logger.warn(e.message) for line in traceback.format_exc().split("\n"): logger.warn(line) je = e.throwable if has_omero_packages() and jutil.is_instance_of( je, "Glacier2/PermissionDeniedException"): # Handle at a higher level raise if jutil.is_instance_of( je, "loci/formats/FormatException"): je = jutil.call(je, "getCause", "()Ljava/lang/Throwable;") if jutil.is_instance_of( je, "java/io/FileNotFoundException"): raise IOError( errno.ENOENT, "The file, \"%s\", does not exist." % path, path) e2 = IOError( errno.EINVAL, "Could not load the file as an image (see log for details)", self.path.encode('utf-8')) raise e2
def context_to_locals(context): '''convert the local context as a Java map to a dictionary of locals''' d = {"JWrapper": JWrapper, "importClass": importClass} m = J.get_map_wrapper(context) for k in m: key = J.to_string(k) o = m[k] if isinstance(o, J.JB_Object): if J.is_instance_of(o, "java/lang/String"): d[key] = J.to_string(o) continue for class_name, method, signature in (("java/lang/Boolean", "booleanValue", "()Z"), ("java/lang/Byte", "byteValue", "()B"), ("java/lang/Integer", "intValue", "()I"), ("java/lang/Long", "longValue", "()L"), ("java/lang/Float", "floatValue", "()F"), ("java/lang/Double", "doubleValue", "()D")): if J.is_instance_of(o, class_name): d[key] = J.call(o, method, signature) break else: d[key] = JWrapper(o) else: d[key] = o return d
def init_reader(self): mdoptions = metadatatools.get_metadata_options(metadatatools.ALL) self.rdr.setMetadataOptions(mdoptions) self.rdr.setGroupFiles(False) self.metadata = metadatatools.createOMEXMLMetadata() self.rdr.setMetadataStore(self.metadata) try: self.rdr.setId(self.path) except jutil.JavaException as e: logger.warn(e.message) for line in traceback.format_exc().split("\n"): logger.warn(line) je = e.throwable if has_omero_packages() and jutil.is_instance_of( je, "Glacier2/PermissionDeniedException"): # Handle at a higher level raise if jutil.is_instance_of(je, "loci/formats/FormatException"): je = jutil.call(je, "getCause", "()Ljava/lang/Throwable;") if jutil.is_instance_of(je, "java/io/FileNotFoundException"): raise IOError(errno.ENOENT, "The file, \"%s\", does not exist." % path, path) e2 = IOError( errno.EINVAL, "Could not load the file as an image (see log for details)", self.path.encode('utf-8')) raise e2
def load_file(self, dfile, incremental=False): """ Loads the specified file and returns the Instances object. In case of incremental loading, only the structure. :param dfile: the file to load :type dfile: str :param incremental: whether to load the dataset incrementally :type incremental: bool :return: the full dataset or the header (if incremental) :rtype: Instances """ self.enforce_type(self.jobject, "weka.core.converters.FileSourcedConverter") self.incremental = incremental if not javabridge.is_instance_of(dfile, "Ljava/io/File;"): dfile = javabridge.make_instance( "Ljava/io/File;", "(Ljava/lang/String;)V", javabridge.get_env().new_string_utf(str(dfile))) javabridge.call(self.jobject, "reset", "()V") javabridge.call(self.jobject, "setFile", "(Ljava/io/File;)V", dfile) if incremental: self.structure = Instances( javabridge.call(self.jobject, "getStructure", "()Lweka/core/Instances;")) return self.structure else: return Instances( javabridge.call(self.jobject, "getDataSet", "()Lweka/core/Instances;"))
def load_file(self, dfile, incremental=False): """ Loads the specified file and returns the Instances object. In case of incremental loading, only the structure. :param dfile: the file to load :type dfile: str :param incremental: whether to load the dataset incrementally :type incremental: bool :return: the full dataset or the header (if incremental) :rtype: Instances :raises Exception: if the file does not exist """ self.enforce_type(self.jobject, "weka.core.converters.FileSourcedConverter") self.incremental = incremental if not javabridge.is_instance_of(dfile, "Ljava/io/File;"): dfile = javabridge.make_instance( "Ljava/io/File;", "(Ljava/lang/String;)V", javabridge.get_env().new_string_utf(str(dfile))) javabridge.call(self.jobject, "reset", "()V") # check whether file exists, otherwise previously set file gets loaded again sfile = javabridge.to_string(dfile) if not os.path.exists(sfile): raise Exception("Dataset file does not exist: " + str(sfile)) javabridge.call(self.jobject, "setFile", "(Ljava/io/File;)V", dfile) if incremental: self.structure = Instances(javabridge.call(self.jobject, "getStructure", "()Lweka/core/Instances;")) return self.structure else: return Instances(javabridge.call(self.jobject, "getDataSet", "()Lweka/core/Instances;"))
def load_file(self, dfile, incremental=False): """ Loads the specified file and returns the Instances object. In case of incremental loading, only the structure. :param dfile: the file to load :type dfile: str :param incremental: whether to load the dataset incrementally :type incremental: bool :return: the full dataset or the header (if incremental) :rtype: Instances :raises Exception: if the file does not exist """ self.enforce_type(self.jobject, "weka.core.converters.FileSourcedConverter") self.incremental = incremental if not javabridge.is_instance_of(dfile, "Ljava/io/File;"): dfile = javabridge.make_instance( "Ljava/io/File;", "(Ljava/lang/String;)V", javabridge.get_env().new_string_utf(str(dfile))) javabridge.call(self.jobject, "reset", "()V") # check whether file exists, otherwise previously set file gets loaded again sfile = javabridge.to_string(dfile) if not os.path.exists(sfile): raise Exception("Dataset file does not exist: " + str(sfile)) javabridge.call(self.jobject, "setFile", "(Ljava/io/File;)V", dfile) if incremental: self.structure = Instances( javabridge.call(self.jobject, "getStructure", "()Lweka/core/Instances;")) return self.structure else: return Instances( javabridge.call(self.jobject, "getDataSet", "()Lweka/core/Instances;"))
def test_haplotypeAllelePresenceForAllTaxa(self): if debug: print "Testing haplotypeAllelePresenceForAllTaxa" try: bitset_major = self.data.haplotypeAllelePresenceForAllTaxa(0,True,WHICH_ALLELE.Major) self.assertIsInstance(bitset_major,BitSet) except JavaException as e: self.assertTrue(is_instance_of(e.throwable, java_imports['UnsupportedOperationException']))
def test_depthForAlleles(self): if debug: print "Testing depthForAlleles" try: arr = self.data.depthForAlleles(0,0) self.assertIsInstance(arr[0],metaInteger) except JavaException as e: self.assertTrue(is_instance_of(e.throwable, java_imports['NullPointerException']))
def test_siteScores(self): if debug: print "Testing siteScores" try: score = self.data.siteScores() self.assertIsInstance(score,metaInteger) except JavaException as e: self.assertTrue(is_instance_of(e.throwable, java_imports['IllegalStateException']))
def test_haplotypeAllelePresenceForSitesBlock(self): if debug: print "Testing haplotypeAllelePresenceForSitesBlock" try: arr = self.data.haplotypeAllelePresenceForSitesBlock(0,True,WHICH_ALLELE.Major, 0,1) self.assertIsInstance(arr,meta_long_array) except JavaException as e: self.assertTrue(is_instance_of(e.throwable, java_imports['UnsupportedOperationException']))
def test_genomeVersion(self): if debug: print "Testing genomeVersion" try: version = self.data.genomeVersion() if version is not None: self.assertIsInstance(version, metaString) except JavaException as e: self.assertTrue(is_instance_of(e.throwable, java_imports['UnsupportedOperationException']))
def test_siteScoreType(self): if debug: print "Testing siteScoreType" try: scoretype = self.data.siteScoreType() self.assertTrue(any(map(lambda x: scoretype.equals(x), self.data.SITE_SCORE_TYPE.__dict__.values()))) except JavaException as e: self.assertTrue(is_instance_of(e.throwable, java_imports['NullPointerException']))
def test_01_11_02_isnt_instance_of(self): klassByte = self.env.find_class("java/lang/Byte") method_id = self.env.get_method_id(klassByte, '<init>', '(Ljava/lang/String;)V') jbyte = self.env.new_object(klassByte, method_id, self.env.new_string_utf("55")) klassString = self.env.find_class("java/lang/String") self.assertFalse(self.env.is_instance_of(jbyte, klassString)) # <AK> added self.assertFalse(javabridge.is_instance_of(None, "java/lang/String"))
def load_file(self, dfile, incremental=False, class_index=None): """ Loads the specified file and returns the Instances object. In case of incremental loading, only the structure. :param dfile: the file to load :type dfile: str :param incremental: whether to load the dataset incrementally :type incremental: bool :param class_index: the class index string to use ('first', 'second', 'third', 'last-2', 'last-1', 'last' or 1-based index) :type class_index: str :return: the full dataset or the header (if incremental) :rtype: Instances :raises Exception: if the file does not exist """ self.enforce_type(self.jobject, "weka.core.converters.FileSourcedConverter") self.incremental = incremental if not javabridge.is_instance_of(dfile, "Ljava/io/File;"): dfile = javabridge.make_instance( "Ljava/io/File;", "(Ljava/lang/String;)V", javabridge.get_env().new_string_utf(str(dfile))) javabridge.call(self.jobject, "reset", "()V") # check whether file exists, otherwise previously set file gets loaded again sfile = javabridge.to_string(dfile) if not os.path.exists(sfile): raise Exception("Dataset file does not exist: " + str(sfile)) javabridge.call(self.jobject, "setFile", "(Ljava/io/File;)V", dfile) if incremental: self.structure = Instances( javabridge.call(self.jobject, "getStructure", "()Lweka/core/Instances;")) result = self.structure else: result = Instances( javabridge.call(self.jobject, "getDataSet", "()Lweka/core/Instances;")) if class_index is not None: if class_index == 'first': result.class_index = 0 elif class_index == 'second': result.class_index = 1 elif class_index == 'third': result.class_index = 2 elif class_index == 'last-2': result.class_index = result.num_attributes - 3 elif class_index == 'last-1': result.class_index = result.num_attributes - 2 elif class_index == 'last': result.class_index = result.num_attributes - 1 else: result.class_index = int(class_index) return result
def check_type(cls, jobject, intf_or_class, jni_intf_or_class=None): """ Returns whether the object implements the specified interface or is a subclass. E.g.: self._check_type('weka.core.OptionHandler', 'Lweka/core/OptionHandler;') or self._check_type('weka.core.converters.AbstractFileLoader') :param jobject: the Java object to check :type jobject: JB_Object :param intf_or_class: the classname in Java notation (eg "weka.core.DenseInstance;") :type jni_intf_or_class: str :return: whether object implements interface or is subclass :rtype: bool """ if jni_intf_or_class is None: jni_intf_or_class = "L" + intf_or_class.replace(".", "/") + ";" return javabridge.is_instance_of(jobject, jni_intf_or_class)
def save_file(self, data, dfile): """ Saves the Instances object in the specified file. :param data: the data to save :type data: Instances :param dfile: the file to save the data to :type dfile: str """ self.enforce_type(self.jobject, "weka.core.converters.FileSourcedConverter") if not javabridge.is_instance_of(dfile, "Ljava/io/File;"): dfile = javabridge.make_instance( "Ljava/io/File;", "(Ljava/lang/String;)V", javabridge.get_env().new_string_utf(str(dfile))) javabridge.call(self.jobject, "setFile", "(Ljava/io/File;)V", dfile) javabridge.call(self.jobject, "setInstances", "(Lweka/core/Instances;)V", data.jobject) javabridge.call(self.jobject, "writeBatch", "()V")
def check_type(cls, jobject, intf_or_class, jni_intf_or_class=None): """ Returns whether the object implements the specified interface or is a subclass. E.g.: self._check_type('weka.core.OptionHandler', 'Lweka/core/OptionHandler;') or self._check_type('weka.core.converters.AbstractFileLoader') :param jobject: the Java object to check :type jobject: JB_Object :param intf_or_class: the classname in Java notation (eg "weka.core.Instance") :type intf_or_class: str :param jni_intf_or_class: the classname in JNI notation (eg "Lweka/core/Instance;") :type jni_intf_or_class: str :return: whether object implements interface or is subclass :rtype: bool """ if jni_intf_or_class is None: jni_intf_or_class = "L" + intf_or_class.replace(".", "/") + ";" return javabridge.is_instance_of(jobject, jni_intf_or_class)
def load_file(self, dfile, incremental=False): """ Loads the specified file and returns the Instances object. In case of incremental loading, only the structure. :param dfile: the file to load :type dfile: str :param incremental: whether to load the dataset incrementally :type incremental: bool :return: the full dataset or the header (if incremental) :rtype: Instances """ self.enforce_type(self.jobject, "weka.core.converters.FileSourcedConverter") self.incremental = incremental if not javabridge.is_instance_of(dfile, "Ljava/io/File;"): dfile = javabridge.make_instance( "Ljava/io/File;", "(Ljava/lang/String;)V", javabridge.get_env().new_string_utf(str(dfile))) javabridge.call(self.jobject, "reset", "()V") javabridge.call(self.jobject, "setFile", "(Ljava/io/File;)V", dfile) if incremental: self.structure = Instances(javabridge.call(self.jobject, "getStructure", "()Lweka/core/Instances;")) return self.structure else: return Instances(javabridge.call(self.jobject, "getDataSet", "()Lweka/core/Instances;"))
def __init__(self, path=None, url=None, perform_init=True): self.stream = None file_scheme = "file:" self.using_temp_file = False if url is not None: url = str(url) if url.lower().startswith(file_scheme): url = url2pathname(url[len(file_scheme):]) path = url self.path = path if path is None: if url.lower().startswith("omero:"): while True: # # We keep trying to contact the OMERO server via the # login dialog until the user gives up or we connect. # try: self.rdr = get_omero_reader() self.path = url if perform_init: self.init_reader() return except jutil.JavaException as e: je = e.throwable if jutil.is_instance_of( je, "loci/formats/FormatException"): je = jutil.call(je, "getCause", "()Ljava/lang/Throwable;") if jutil.is_instance_of( je, "Glacier2/PermissionDeniedException"): omero_logout() omero_login() else: logger.warn(e.message) for line in traceback.format_exc().split("\n"): logger.warn(line) if jutil.is_instance_of( je, "java/io/FileNotFoundException"): raise IOError( errno.ENOENT, "The file, \"%s\", does not exist." % path, path) e2 = IOError( errno.EINVAL, "Could not load the file as an image (see log for details)", path.encode('utf-8')) raise e2 else: # # Other URLS, copy them to a tempfile location # ext = url[url.rfind("."):] src = urlopen(url) dest_fd, self.path = tempfile.mkstemp(suffix=ext) try: dest = os.fdopen(dest_fd, 'wb') shutil.copyfileobj(src, dest) except: src.close() dest.close() os.remove(self.path) self.using_temp_file = True src.close() dest.close() urlpath = urlparse(url)[2] filename = unquote(urlpath.split("/")[-1]) else: if sys.platform.startswith("win"): self.path = self.path.replace("/", os.path.sep) filename = os.path.split(path)[1] if not os.path.isfile(self.path): raise IOError( errno.ENOENT, "The file, \"%s\", does not exist." % path, path) self.stream = jutil.make_instance('loci/common/RandomAccessInputStream', '(Ljava/lang/String;)V', self.path) self.rdr = None class_list = get_class_list() find_rdr_script = """ var classes = class_list.getClasses(); var rdr = null; var lc_filename = java.lang.String(filename.toLowerCase()); for (pass=0; pass < 3; pass++) { for (class_idx in classes) { var maybe_rdr = classes[class_idx].newInstance(); if (pass == 0) { if (maybe_rdr.isThisType(filename, false)) { rdr = maybe_rdr; break; } continue; } else if (pass == 1) { var suffixes = maybe_rdr.getSuffixes(); var suffix_found = false; for (suffix_idx in suffixes) { var suffix = java.lang.String(suffixes[suffix_idx]); suffix = suffix.toLowerCase(); if (lc_filename.endsWith(suffix)) { suffix_found = true; break; } } if (! suffix_found) continue; } if (maybe_rdr.isThisType(stream)) { rdr = maybe_rdr; break; } } if (rdr) break; } rdr; """ IFormatReader = make_iformat_reader_class() jrdr = jutil.run_script(find_rdr_script, dict(class_list = class_list, filename = filename, stream = self.stream)) if jrdr is None: raise ValueError("Could not find a Bio-Formats reader for %s", self.path) self.rdr = IFormatReader() self.rdr.o = jrdr if perform_init: self.init_reader()
def test_02_02_01_get_modules(self): svc = ij2.get_module_service(self.context) module_infos = svc.getModules() self.assertTrue(J.is_instance_of(module_infos[0].o, "org/scijava/module/ModuleInfo"))
def __iter__(self): if not J.is_instance_of(self.o,'java/util/Collection'): raise TypeError("%s is not a Collection and does not support __iter__" % self) return self.Iterator(self)
def __setitem__(self, i, v): if not J.is_instance_of(self.o,'java/util/Collection'): raise TypeError("%s is not a Collection and does not support __setitem__" % self) return self.set(i, v)
def test_02_02_01_get_modules(self): svc = ij2.get_module_service(self.context) module_infos = svc.getModules() self.assertTrue( J.is_instance_of(module_infos[0].o, "org/scijava/module/ModuleInfo"))
def __init__(self, path=None, url=None, perform_init=True): self.stream = None file_scheme = "file:" self.using_temp_file = False if url is not None: url = str(url) if url.lower().startswith(file_scheme): url = url2pathname(url[len(file_scheme):]) path = url self.path = path if path is None: if url.lower().startswith("omero:"): while True: # # We keep trying to contact the OMERO server via the # login dialog until the user gives up or we connect. # try: self.rdr = get_omero_reader() self.path = url if perform_init: self.init_reader() return except jutil.JavaException as e: je = e.throwable if jutil.is_instance_of( je, "loci/formats/FormatException"): je = jutil.call(je, "getCause", "()Ljava/lang/Throwable;") if jutil.is_instance_of( je, "Glacier2/PermissionDeniedException"): omero_logout() omero_login() else: logger.warn(e.message) for line in traceback.format_exc().split("\n"): logger.warn(line) if jutil.is_instance_of( je, "java/io/FileNotFoundException"): raise IOError( errno.ENOENT, "The file, \"%s\", does not exist." % path, path) e2 = IOError( errno.EINVAL, "Could not load the file as an image (see log for details)", path.encode('utf-8')) raise e2 else: # # Other URLS, copy them to a tempfile location # ext = url[url.rfind("."):] src = urlopen(url) dest_fd, self.path = tempfile.mkstemp(suffix=ext) try: dest = os.fdopen(dest_fd, 'wb') shutil.copyfileobj(src, dest) except: src.close() dest.close() os.remove(self.path) self.using_temp_file = True src.close() dest.close() urlpath = urlparse(url)[2] filename = unquote(urlpath.split("/")[-1]) else: if sys.platform.startswith("win"): self.path = self.path.replace("/", os.path.sep) filename = os.path.split(path)[1] if not os.path.isfile(self.path): raise IOError(errno.ENOENT, "The file, \"%s\", does not exist." % path, path) self.stream = jutil.make_instance( 'loci/common/RandomAccessInputStream', '(Ljava/lang/String;)V', self.path) self.rdr = None class_list = get_class_list() find_rdr_script = """ var classes = class_list.getClasses(); var rdr = null; var lc_filename = java.lang.String(filename.toLowerCase()); for (pass=0; pass < 3; pass++) { for (class_idx in classes) { var maybe_rdr = classes[class_idx].newInstance(); if (pass == 0) { if (maybe_rdr.isThisType(filename, false)) { rdr = maybe_rdr; break; } continue; } else if (pass == 1) { var suffixes = maybe_rdr.getSuffixes(); var suffix_found = false; for (suffix_idx in suffixes) { var suffix = java.lang.String(suffixes[suffix_idx]); suffix = suffix.toLowerCase(); if (lc_filename.endsWith(suffix)) { suffix_found = true; break; } } if (! suffix_found) continue; } if (maybe_rdr.isThisType(stream)) { rdr = maybe_rdr; break; } } if (rdr) break; } rdr; """ IFormatReader = make_iformat_reader_class() jrdr = jutil.run_script( find_rdr_script, dict(class_list=class_list, filename=filename, stream=self.stream)) if jrdr is None: raise ValueError("Could not find a Bio-Formats reader for %s", self.path) self.rdr = IFormatReader() self.rdr.o = jrdr if perform_init: self.init_reader()
def __setitem__(self, i, v): if not J.is_instance_of(self.o, 'java/util/Collection'): raise TypeError( "%s is not a Collection and does not support __setitem__" % self) return self.set(i, v)
def __iter__(self): if not J.is_instance_of(self.o, 'java/util/Collection'): raise TypeError( "%s is not a Collection and does not support __iter__" % self) return self.Iterator(self)