def create_sparse_instance(cls, values, max_values, classname="weka.core.SparseInstance", weight=1.0): """ Creates a new sparse instance. :param values: the list of tuples (0-based index and internal format float). The indices of the tuples must be in ascending order and "max_values" must be set to the maximum number of attributes in the dataset. :type values: list :param max_values: the maximum number of attributes :type max_values: int :param classname: the classname of the instance (eg weka.core.SparseInstance). :type classname: str :param weight: the weight of the instance :type weight: float """ jni_classname = classname.replace(".", "/") indices = [] vals = [] for (i, v) in values: indices.append(i) vals.append(float(v)) indices = numpy.array(indices, dtype=numpy.int32) vals = numpy.array(vals) return Instance( javabridge.make_instance( jni_classname, "(D[D[II)V", weight, javabridge.get_env().make_double_array(vals), javabridge.get_env().make_int_array(indices), max_values))
def main(): """ Just runs some example code. """ # load a dataset iris_file = helper.get_data_dir() + os.sep + "iris.arff" helper.print_info("Loading dataset: " + iris_file) loader = Loader("weka.core.converters.ArffLoader") iris_data = loader.load_file(iris_file) iris_data.class_is_last() # train classifier classifier = Classifier("weka.classifiers.trees.J48") classifier.build_classifier(iris_data) # save and read object helper.print_title("I/O: single object") outfile = tempfile.gettempdir() + os.sep + "j48.model" serialization.write(outfile, classifier) model = Classifier(jobject=serialization.read(outfile)) print(model) # save classifier and dataset header (multiple objects) helper.print_title("I/O: single object") serialization.write_all(outfile, [classifier, Instances.template_instances(iris_data)]) objects = serialization.read_all(outfile) for i, obj in enumerate(objects): helper.print_info("Object #" + str(i+1) + ":") if javabridge.get_env().is_instance_of(obj, javabridge.get_env().find_class("weka/core/Instances")): obj = Instances(jobject=obj) elif javabridge.get_env().is_instance_of(obj, javabridge.get_env().find_class("weka/classifiers/Classifier")): obj = Classifier(jobject=obj) print(obj)
def prepare_run(self, workspace): '''Create an IPD for every url that passes the filter''' if workspace.pipeline.in_batch_mode(): return True file_list = workspace.pipeline.file_list if self.filter_choice != FILTER_CHOICE_NONE: if self.filter_choice == FILTER_CHOICE_IMAGES: expression = FILTER_DEFAULT else: expression = self.filter.value_text env = J.get_env() ifcls = J.class_for_name("org.cellprofiler.imageset.ImageFile") scls = env.find_class("java/lang/String") iffilter = J.make_instance( "org/cellprofiler/imageset/filter/Filter", "(Ljava/lang/String;Ljava/lang/Class;)V", expression, ifcls) file_array = env.make_object_array(len(file_list), scls) for i, url in enumerate(file_list): if isinstance(url, unicode): ourl = env.new_string(url) else: ourl = env.new_string_utf(url) env.set_object_array_element(file_array, i, ourl) passes_filter = J.call( iffilter, "filterURLs", "([Ljava/lang/String;)[Z", file_array) if isinstance(passes_filter, J.JB_Object): passes_filter = J.get_env().get_boolean_array_elements( passes_filter) file_list = [f for f, passes in zip(file_list, passes_filter) if passes] workspace.pipeline.set_filtered_file_list(file_list, self) return True
def to_wrapped_array(arr_instance, pyType): """ Wraps a current array Arguments: arr_instance -- A java object containing the array pyType -- The type of the javaObj Returns: The wrapped java array """ pyClass_base = re.search('(?<=\.)[a-zA-Z0-9_]+(?=\'\>)',str(pyType)).group() func_dict = {} if pyClass_base == 'metaByte': func_dict = {'getter':Array.getByte, 'setter':Array.setByte} elif pyClass_base == 'metaBoolean': func_dict = {'getter':Array.getBoolean, 'setter':Array.setBoolean} elif pyClass_base == 'metaInteger': func_dict = {'getter':Array.getInt, 'setter':Array.set} elif pyClass_base == 'metaLong': func_dict = {'getter':Array.getLong, 'setter':Array.setLong} elif pyClass_base == 'metaFloat': func_dict = {'getter':Array.getFloat, 'setter':Array.setFloat} elif pyClass_base == 'metaDouble': func_dict = {'getter':Array.getDouble, 'setter':Array.setDouble} if not issubclass(pyType,javaArray): primative_name = re.search('(?<=meta).+',pyClass_base).group().lower() if primative_name == 'integer': primative_name = primative_name[:3] return javaPrimativeArray.make_array_from_obj(primative_name, arr_instance) elif pyType.dim == 1: ## Create the array python object arr_type = None arr_type = type(pyClass_base,(javaArray,),{'baseClass':pyType}) if not hasattr(pyType, 'ref_type'): arr_type.ref_type = pyType.baseClass else: arr_type.ref_type = pyType.ref_type arr_type.dim = pyType.dim + 1 py_arr_obj = arr_type() py_arr_obj.o = arr_instance py_arr_obj._java_name = javabridge.get_env().get_object_class(arr_instance) if issubclass(pyType, javaArray): py_arr_obj.pyArr = map(lambda x: javaPrimativeArray.make_array_from_obj(pyClass_base, x), javabridge.get_env().get_object_array_elements(arr_instance)) return py_arr_obj elif pyType.dim > 1: raise ValueError("Dimensions greater than 2 not supported for primative types") else: raise ValueError("Invalid input")
def stem(self, s): """ Performs stemming on the string. :param s: the string to stem :type s: str :return: the stemmed string :rtype: str """ return javabridge.get_env().get_string(self.__stem(javabridge.get_env().new_string_utf(s)))
def test_01_04_call_varargs(self): sclass = J.JWrapper(J.class_for_name("java.lang.String")); for constructor in J.get_env().get_object_array_elements( sclass.getConstructors().o): wconstructor = J.JWrapper(constructor) parameter_types = J.get_env().get_object_array_elements( wconstructor.getParameterTypes().o) c1 = sclass.getConstructor(*parameter_types) self.assertTrue(c1.equals(constructor))
def setter(self, key, val): if not isinstance(val, self.baseClass): raise TypeError("Value not an instance of %s." % str(self.baseClass)) elif not isinstance(val, String): val = String(val) self.pyArr[key] = val if key < 0: key = len(self) + key javabridge.get_env().set_object_array_element(self.o, key, val.o)
def configure_resultproducer(self): """ Configures and returns the ResultProducer and PropertyPath as tuple. :return: producer and property path :rtype: tuple """ rproducer = javabridge.make_instance("weka/experiment/RandomSplitResultProducer", "()V") javabridge.call(rproducer, "setRandomizeData", "(Z)V", not self.preserve_order) javabridge.call(rproducer, "setTrainPercent", "(D)V", self.percentage) speval, classifier = self.configure_splitevaluator() javabridge.call(rproducer, "setSplitEvaluator", "(Lweka/experiment/SplitEvaluator;)V", speval) prop_path = javabridge.get_env().make_object_array( 2, javabridge.get_env().find_class("weka/experiment/PropertyNode")) cls = javabridge.get_env().find_class("weka/experiment/RandomSplitResultProducer") desc = javabridge.make_instance( "java/beans/PropertyDescriptor", "(Ljava/lang/String;Ljava/lang/Class;)V", "splitEvaluator", cls) node = javabridge.make_instance( "weka/experiment/PropertyNode", "(Ljava/lang/Object;Ljava/beans/PropertyDescriptor;Ljava/lang/Class;)V", speval, desc, cls) javabridge.get_env().set_object_array_element(prop_path, 0, node) cls = javabridge.get_env().get_object_class(speval) desc = javabridge.make_instance( "java/beans/PropertyDescriptor", "(Ljava/lang/String;Ljava/lang/Class;)V", "classifier", cls) node = javabridge.make_instance( "weka/experiment/PropertyNode", "(Ljava/lang/Object;Ljava/beans/PropertyDescriptor;Ljava/lang/Class;)V", javabridge.get_env().get_object_class(speval), desc, cls) javabridge.get_env().set_object_array_element(prop_path, 1, node) return rproducer, prop_path
def configure_resultproducer(self): """ Configures and returns the ResultProducer and PropertyPath as tuple. :return: producer and property path :rtype: tuple """ rproducer = javabridge.make_instance("weka/experiment/CrossValidationResultProducer", "()V") javabridge.call(rproducer, "setNumFolds", "(I)V", self.folds) speval, classifier = self.configure_splitevaluator() javabridge.call(rproducer, "setSplitEvaluator", "(Lweka/experiment/SplitEvaluator;)V", speval) prop_path = javabridge.get_env().make_object_array( 2, javabridge.get_env().find_class("weka/experiment/PropertyNode")) cls = javabridge.get_env().find_class("weka/experiment/CrossValidationResultProducer") desc = javabridge.make_instance( "java/beans/PropertyDescriptor", "(Ljava/lang/String;Ljava/lang/Class;)V", "splitEvaluator", cls) node = javabridge.make_instance( "weka/experiment/PropertyNode", "(Ljava/lang/Object;Ljava/beans/PropertyDescriptor;Ljava/lang/Class;)V", speval, desc, cls) javabridge.get_env().set_object_array_element(prop_path, 0, node) cls = javabridge.get_env().get_object_class(speval) desc = javabridge.make_instance( "java/beans/PropertyDescriptor", "(Ljava/lang/String;Ljava/lang/Class;)V", "classifier", cls) node = javabridge.make_instance( "weka/experiment/PropertyNode", "(Ljava/lang/Object;Ljava/beans/PropertyDescriptor;Ljava/lang/Class;)V", javabridge.get_env().get_object_class(speval), desc, cls) javabridge.get_env().set_object_array_element(prop_path, 1, node) return rproducer, prop_path
def string_list_to_array(l): """ Turns a Python unicode string list into a Java String array. :param l: the string list :type: list :rtype: java string array :return: JB_Object """ result = javabridge.get_env().make_object_array(len(l), javabridge.get_env().find_class("java/lang/String")) for i in xrange(len(l)): javabridge.get_env().set_object_array_element(result, i, javabridge.get_env().new_string_utf(l[i])) return result
def coefficients(self): m = javabridge.call(self.jobject, "coefficients", "()[[D") result = [javabridge.get_env().get_array_length(m)] rows = javabridge.get_env().get_object_array_elements(m) for row in rows: elements = [] for i, element in enumerate(javabridge.get_env().get_double_array_elements(row)): elements.append(float(element)) result.append(elements) return result
def tags(self): """ Returns the associated tags. :return: the list of Tag objects :rtype: list """ result = [] a = javabridge.call(self.jobject, "getTags", "()Lweka/core/Tag;]") length = javabridge.get_env().get_array_length(a) wrapped = javabridge.get_env().get_object_array_elements(a) for i in xrange(length): result.append(Tag(javabridge.get_env().get_string(wrapped[i]))) return result
def string_array_to_list(a): """ Turns the Java string array into Python unicode string list. :param a: the string array to convert :type a: JB_Object :return: the string list :rtype: list """ result = [] length = javabridge.get_env().get_array_length(a) wrapped = javabridge.get_env().get_object_array_elements(a) for i in xrange(length): result.append(javabridge.get_env().get_string(wrapped[i])) return result
def omero_login(): global __omero_config_file global __omero_session_id global __omero_server global __omero_username global __omero_port global __omero_password if __omero_config_file is not None and os.path.isfile(__omero_config_file): env = jutil.get_env() config = env.make_object_array(1, env.find_class("java/lang/String")) env.set_object_array_element( config, 0, env.new_string("--Ice.Config=%s" % __omero_config_file)) script = """ var client = Packages.omero.client(config); client.createSession(); client.getSessionId(); """ __omero_session_id = jutil.run_script(script, dict(config=config)) elif all([x is not None for x in (__omero_server, __omero_port, __omero_username, __omero_password)]): set_omero_credentials(__omero_server, __omero_port, __omero_username, __omero_password) else: __omero_login_fn() return __omero_session_id
def __init__(self): env = jutil.get_env() class_name = 'loci/formats/ImageReader' klass = env.find_class(class_name) base_klass = env.find_class('loci/formats/IFormatReader') self.o = jutil.make_instance("loci/formats/ClassList", "(Ljava/lang/String;" "Ljava/lang/Class;" # base "Ljava/lang/Class;)V", # location in jar "readers.txt", base_klass, klass) problem_classes = [ # BDReader will read all .tif files in an experiment if it's # called to load a .tif. # 'loci.formats.in.BDReader', # # MRCReader will read .stk files which should be read # by MetamorphReader # 'loci.formats.in.MRCReader' ] for problem_class in problem_classes: # Move to back klass = jutil.class_for_name(problem_class) self.remove_class(klass) self.add_class(klass)
def nominal_counts(self): """ Counts of each nominal value. :return: Counts of each nominal value :rtype: ndarray """ return javabridge.get_env().get_int_array_elements(javabridge.get_field(self.jobject, "nominalCounts", "[I"))
def __setitem__(self, key, val): # Check if value member of baseClass if isinstance(val, javaArray): if val.dim != (self.dim - 1): raise TypeError("Value is not of correct dimension") if val.dim == 1 and val.baseClass != self.ref_type: raise TypeError("Value is not of correct reference type") elif val.dim > 1 and val.ref_type != self.ref_type: raise TypeError("Value is not of correct reference type") elif not isinstance(val,self.baseClass): raise TypeError("Value not an instance of %s." % str(self.baseClass)) self.pyArr[key] = val if key < 0: key = len(self) + key javabridge.get_env().set_object_array_element(self.o, key, val.o)
def __init__(self, base_class_name, d=None): '''Initialize the proxy with the interface name and methods :param base_class_name: the class name of the interface to implement in dotted form (e.g. java.lang.Runnable) :param d: an optional dictionary of method name to implementation ''' self.ref_id, self.ref = J.create_jref(self) self.__d = d or {} jclass = J.class_for_name(base_class_name) loader = J.call(jclass, "getClassLoader", "()Ljava/lang/ClassLoader;") env = J.get_env() classes = env.make_object_array(1, env.find_class("java/lang/Class")) env.set_object_array_element(classes, 0, jclass) handler = J.make_instance( "org/cellprofiler/javabridge/CPythonInvocationHandler", "(Ljava/lang/String;)V", self.ref_id) self.o = J.static_call( "java/lang/reflect/Proxy", "newProxyInstance", "(Ljava/lang/ClassLoader;" "[Ljava/lang/Class;" "Ljava/lang/reflect/InvocationHandler;)" "Ljava/lang/Object;", loader, classes, handler)
def __init__(self, o): '''Initialize the JWrapper with a Java object :param o: a Java object (class = JB_Object) ''' STATIC = J.get_static_field("java/lang/reflect/Modifier", "STATIC", "I") self.o = o self.class_wrapper = J.get_class_wrapper(o) env = J.get_env() jmethods = env.get_object_array_elements(self.class_wrapper.getMethods()) methods = {} for jmethod in jmethods: if (J.call(jmethod, "getModifiers", "()I") & STATIC) == STATIC: continue method = J.get_method_wrapper(jmethod) name = method.getName() if name not in methods: methods[name] = [] fn = lambda naame=name: lambda *args: self.__call(naame, *args) fn = fn() fn.__doc__ = J.to_string(jmethod) setattr(self, name, fn) else: fn = getattr(self, name) fn.__doc__ = fn.__doc__ +"\n"+J.to_string(jmethod) methods[name].append(method) self.methods = methods
def test_03_07_cw_get_fields(self): c = javabridge.get_class_wrapper('java.lang.String') fields = c.getFields() fields = javabridge.get_env().get_object_array_elements(fields) self.assertEqual(len(fields), 1) self.assertEqual(javabridge.call(fields[0], 'getName', '()Ljava/lang/String;'), "CASE_INSENSITIVE_ORDER")
def selection(self): """ Returns the selection list. :return: the list of 0-based integer indices :rtype: list """ return javabridge.get_env().get_int_array_elements(javabridge.call(self.jobject, "getSelection", "()[I"))
def get_pixel_data(img): '''Get the pixel data from an image''' interval = wrap_interval(img) dims = interval.dimensions() # # Make a Java double array # a = np.zeros(np.prod(dims), np.float64) ja = J.get_env().make_double_array(np.ascontiguousarray(a)) strides = np.cumprod([1] + dims[:0:-1]).astype(int)[::-1] J.static_call("net/imglib2/util/ImgUtil", "copy", "(Lnet/imglib2/img/Img;[DI[I)V", img, ja, 0, strides) a = J.get_env().get_double_array_elements(ja) a.shape = dims return a
def patch_start_vm(*args, **kwargs): jvm_args = list(args[0]) + [ "-Dloci.bioformats.loaded=true", "-Djava.util.prefs.PreferencesFactory="+ "org.cellprofiler.headlesspreferences"+ ".HeadlessPreferencesFactory"] # # Find the ij1patcher # if hasattr(sys, 'frozen') and sys.platform == 'win32': root = os.path.dirname(sys.argv[0]) else: root = os.path.dirname(__file__) jardir = os.path.join(root, "imagej", "jars") patchers = sorted([ x for x in os.listdir(jardir) if x.startswith("ij1-patcher") and x.endswith(".jar")]) if len(patchers) > 0: jvm_args.append( "-javaagent:%s=init" % os.path.join(jardir, patchers[-1])) result = start_vm(jvm_args, *args[1:], **kwargs) if javabridge.get_env() is not None: try: event_service_cls = javabridge.JClassWrapper( "org.bushe.swing.event.ThreadSafeEventService") event_service_cls.CLEANUP_PERIOD_MS_DEFAULT = None except: pass return result
def values(self): """ Returns the internal values of this instance. :return: the values as numpy array :rtype: ndarray """ return javabridge.get_env().get_double_array_elements(javabridge.call(self.jobject, "toDoubleArray", "()[D"))
def __len__(self): """ Returns the length of the array. :return: the array length :rtype: int """ return javabridge.get_env().get_array_length(self.jobject)
def install_package(pkge, version="Latest"): """ The list of packages to install. :param pkge: the name of the repository package, a URL (http/https) or a zip file :type pkge: str :param version: in case of the repository packages, the version :type version: str :return: whether successfully installed :rtype: bool """ establish_cache() if pkge.startswith("http://") or pkge.startswith("https://"): url = javabridge.make_instance( "java/net/URL", "(Ljava/lang/String;)V", javabridge.get_env().new_string_utf(pkge)) return not javabridge.static_call( "weka/core/WekaPackageManager", "installPackageFromURL", "(Ljava/net/URL;[Ljava/io/PrintStream;)Ljava/lang/String;", url, []) is None elif pkge.lower().endswith(".zip"): return not javabridge.static_call( "weka/core/WekaPackageManager", "installPackageFromArchive", "(Ljava/lang/String;[Ljava/io/PrintStream;)Ljava/lang/String;", pkge, []) is None else: return javabridge.static_call( "weka/core/WekaPackageManager", "installPackageFromRepository", "(Ljava/lang/String;Ljava/lang/String;[Ljava/io/PrintStream;)Z", pkge, version, [])
def __call_static(self, method_name, *args): '''Call the appropriate overloaded method with the given name :param method_name: the name of the method to call :param *args: the arguments to the method, which are used to disambiguate between similarly named methods ''' env = J.get_env() last_e = None for method in self.methods[method_name]: params = env.get_object_array_elements(method.getParameterTypes()) is_var_args = J.call(method.o, "isVarArgs", "()Z") if len(args) < len(params) - (1 if is_var_args else 0): continue if len(args) > len(params) and not is_var_args: continue if is_var_args: pm1 = len(params)-1 args1 = args[:pm1] + [args[pm1:]] else: args1 = args try: cargs = [cast(o, klass) for o, klass in zip(args1, params)] except: last_e = sys.exc_info()[1] continue rtype = J.call(method.o, "getReturnType", "()Ljava/lang/Class;") args_sig = "".join(map(sig, params)) rsig = sig(rtype) msig = "(%s)%s" % (args_sig, rsig) result = J.static_call(self.cname, method_name, msig, *cargs) if isinstance(result, J.JB_Object): result = JWrapper(result) return result raise TypeError("No matching method found for %s" % method_name)
def __call__(self, *args): '''Constructors''' env = J.get_env() jconstructors = self.klass.getConstructors() for jconstructor in env.get_object_array_elements(jconstructors): constructor = J.get_constructor_wrapper(jconstructor) params = env.get_object_array_elements( constructor.getParameterTypes()) is_var_args = J.call(constructor.o, "isVarArgs", "()Z") if len(args) < len(params) - (1 if is_var_args else 0): continue if len(args) > len(params) and not is_var_args: continue if is_var_args: pm1 = len(params)-1 args1 = args[:pm1] + [args[pm1:]] else: args1 = args try: cargs = [cast(o, klass) for o, klass in zip(args1, params)] except: last_e = sys.exc_info()[1] continue args_sig = "".join(map(sig, params)) msig = "(%s)V" % (args_sig) result = J.make_instance(self.cname, msig, *cargs) result = JWrapper(result) return result raise TypeError("No matching constructor found")
def __init__(self, class_name): '''Initialize to wrap a class name :param class_name: name of class in dotted form, e.g. java.lang.Integer ''' STATIC = J.get_static_field("java/lang/reflect/Modifier", "STATIC", "I") self.cname = class_name.replace(".", "/") self.klass = J.get_class_wrapper(J.class_for_name(class_name), True) self.static_methods = {} env = J.get_env() jmethods = env.get_object_array_elements(self.klass.getMethods()) methods = {} for jmethod in jmethods: if (J.call(jmethod, "getModifiers", "()I") & STATIC) != STATIC: continue method = J.get_method_wrapper(jmethod) name = method.getName() if name not in methods: methods[name] = [] fn = lambda naame=name: lambda *args: self.__call_static(naame, *args) fn = fn() fn.__doc__ = J.to_string(jmethod) setattr(self, name, fn) else: fn = getattr(self, name) fn.__doc__ = fn.__doc__ +"\n"+J.to_string(jmethod) methods[name].append(method) self.methods = methods
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 vm_get_env(): return javabridge.get_env()
def get_measurements_for_good_pipeline(nimages=1, group_numbers=None): """Get an appropriately initialized measurements structure for the good pipeline""" import cellprofiler_core path = os.path.abspath( os.path.join( os.path.dirname(cellprofiler_core.__file__), "..", "tests/data/ExampleSBSImages", ) ) # path = os.path.join(tests.modules.example_images_directory(), "ExampleSBSImages") m = cellprofiler_core.measurement.Measurements() if group_numbers is None: group_numbers = [1] * nimages group_indexes = [1] last_group_number = group_numbers[0] group_index = 1 for group_number in group_numbers: if group_number == last_group_number: group_index += 1 else: group_index = 1 group_indexes.append(group_index) for i in range(1, nimages + 1): filename = "Channel2-%02d-%s-%02d.tif" % ( i, "ABCDEFGH"[int((i - 1) / 12)], ((i - 1) % 12) + 1, ) url = cellprofiler_core.utilities.pathname.pathname2url( os.path.join(path, filename) ) m[ cellprofiler_core.constants.measurement.IMAGE, cellprofiler_core.constants.measurement.C_FILE_NAME + "_DNA", i, ] = filename m[ cellprofiler_core.constants.measurement.IMAGE, cellprofiler_core.constants.measurement.C_PATH_NAME + "_DNA", i, ] = path m[ cellprofiler_core.constants.measurement.IMAGE, cellprofiler_core.constants.measurement.C_URL + "_DNA", i, ] = url m[ cellprofiler_core.constants.measurement.IMAGE, cellprofiler_core.constants.measurement.GROUP_NUMBER, i, ] = group_numbers[i - 1] m[ cellprofiler_core.constants.measurement.IMAGE, cellprofiler_core.constants.measurement.GROUP_INDEX, i, ] = group_indexes[i - 1] jblob = javabridge.run_script( """ importPackage(Packages.org.cellprofiler.imageset); importPackage(Packages.org.cellprofiler.imageset.filter); var imageFile=new ImageFile(new java.net.URI(url)); var imageFileDetails = new ImageFileDetails(imageFile); var imageSeries=new ImageSeries(imageFile, 0); var imageSeriesDetails = new ImageSeriesDetails(imageSeries, imageFileDetails); var imagePlane=new ImagePlane(imageSeries, 0, ImagePlane.ALWAYS_MONOCHROME); var ipd = new ImagePlaneDetails(imagePlane, imageSeriesDetails); var stack = ImagePlaneDetailsStack.makeMonochromeStack(ipd); var stacks = java.util.Collections.singletonList(stack); var keys = java.util.Collections.singletonList(imageNumber); var imageSet = new ImageSet(stacks, keys); imageSet.compress(java.util.Collections.singletonList("DNA"), null); """, dict(url=url, imageNumber=str(i)), ) blob = javabridge.get_env().get_byte_array_elements(jblob) m[ cellprofiler_core.constants.measurement.IMAGE, cellprofiler_core.modules.namesandtypes.M_IMAGE_SET, i, blob.dtype, ] = blob pipeline = cellprofiler_core.pipeline.Pipeline() pipeline.loadtxt(six.moves.StringIO(GOOD_PIPELINE)) pipeline.write_pipeline_measurement(m) return m
def test_01_02_call_noargs(self): jobj = J.get_env().new_string(u"Hello, world.") obj = J.JWrapper(jobj) self.assertEquals(obj.toLowerCase(), "hello, world.")
def test_03_06_cw_get_constructors(self): c = javabridge.get_class_wrapper('java.lang.String') constructors = c.getConstructors() constructors = javabridge.get_env().get_object_array_elements( constructors) self.assertEqual(len(constructors), 15)
def read(self, c = None, z = 0, t = 0, series = None, index = None, rescale = True, wants_max_intensity = False, channel_names = None, XYWH=None): '''Read a single plane from the image reader file. :param c: read from this channel. `None` = read color image if multichannel or interleaved RGB. :param z: z-stack index :param t: time index :param series: series for ``.flex`` and similar multi-stack formats :param index: if `None`, fall back to ``zct``, otherwise load the indexed frame :param rescale: `True` to rescale the intensity scale to 0 and 1; `False` to return the raw values native to the file. :param wants_max_intensity: if `False`, only return the image; if `True`, return a tuple of image and max intensity :param channel_names: provide the channel names for the OME metadata :param XYWH: a (x, y, w, h) tuple ''' FormatTools = make_format_tools_class() ChannelSeparator = make_reader_wrapper_class( "loci/formats/ChannelSeparator") env = jutil.get_env() if series is not None: self.rdr.setSeries(series) if XYWH is not None: assert isinstance(XYWH, tuple) and len(XYWH) == 4, "Invalid XYWH tuple" openBytes_func = lambda x: self.rdr.openBytesXYWH(x, XYWH[0], XYWH[1], XYWH[2], XYWH[3]) width, height = XYWH[2], XYWH[3] else: openBytes_func = self.rdr.openBytes width, height = self.rdr.getSizeX(), self.rdr.getSizeY() pixel_type = self.rdr.getPixelType() little_endian = self.rdr.isLittleEndian() if pixel_type == FormatTools.INT8: dtype = np.int8 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 = self.rdr.getMetadataValue('MaxSampleValue') if max_sample_value is not None: try: scale = jutil.call(max_sample_value, 'intValue', '()I') except: logger.warning("WARNING: failed to get MaxSampleValue for image. Intensities may be improperly scaled.") if index is not None: image = np.frombuffer(openBytes_func(index), dtype) if len(image) / height / width in (3,4): n_channels = int(len(image) / height / width) if self.rdr.isInterleaved(): image.shape = (height, width, n_channels) else: image.shape = (n_channels, height, width) image = image.transpose(1, 2, 0) else: image.shape = (height, width) elif self.rdr.isRGB() and self.rdr.isInterleaved(): index = self.rdr.getIndex(z,0,t) image = np.frombuffer(openBytes_func(index), dtype) image.shape = (height, width, self.rdr.getSizeC()) if image.shape[2] > 3: image = image[:, :, :3] elif c is not None and self.rdr.getRGBChannelCount() == 1: index = self.rdr.getIndex(z,c,t) image = np.frombuffer(openBytes_func(index), dtype) image.shape = (height, width) elif self.rdr.getRGBChannelCount() > 1: n_planes = self.rdr.getRGBChannelCount() rdr = ChannelSeparator(self.rdr) planes = [ np.frombuffer( (rdr.openBytes(rdr.getIndex(z,i,t)) if XYWH is None else rdr.openBytesXYWH(rdr.getIndex(z,i,t), XYWH[0], XYWH[1], XYWH[2], XYWH[3])), dtype ) for i in range(n_planes)] if len(planes) > 3: planes = planes[:3] elif len(planes) < 3: # > 1 and < 3 means must be 2 # see issue #775 planes.append(np.zeros(planes[0].shape, planes[0].dtype)) image = np.dstack(planes) image.shape=(height, width, 3) del rdr elif self.rdr.getSizeC() > 1: images = [ np.frombuffer(openBytes_func(self.rdr.getIndex(z,i,t)), dtype) for i in range(self.rdr.getSizeC())] image = np.dstack(images) image.shape = (height, width, self.rdr.getSizeC()) if not channel_names is None: metadata = metadatatools.MetadataRetrieve(self.metadata) for i in range(self.rdr.getSizeC()): index = self.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) elif self.rdr.isIndexed(): # # The image data is indexes into a color lookup-table # But sometimes the table is the identity table and just generates # a monochrome RGB image # index = self.rdr.getIndex(z,0,t) image = np.frombuffer(openBytes_func(index),dtype) if pixel_type in (FormatTools.INT16, FormatTools.UINT16): lut = self.rdr.get16BitLookupTable() if lut is not None: lut = np.array( [env.get_short_array_elements(d) for d in env.get_object_array_elements(lut)])\ .transpose() else: lut = self.rdr.get8BitLookupTable() if lut is not None: lut = np.array( [env.get_byte_array_elements(d) for d in env.get_object_array_elements(lut)])\ .transpose() image.shape = (height, width) if (lut is not None) \ and not np.all(lut == np.arange(lut.shape[0])[:, np.newaxis]): image = lut[image, :] else: index = self.rdr.getIndex(z,0,t) image = np.frombuffer(openBytes_func(index),dtype) image.shape = (height,width) if rescale: image = image.astype(np.float32) / float(scale) if wants_max_intensity: return image, scale return image
def test_01_01_init(self): jobj = J.get_env().new_string(u"Hello, world.") obj = J.JWrapper(jobj) self.assertEquals(jobj, obj.o)
def test_01_03_call_args(self): jobj = J.get_env().new_string(u"Hello, world.") obj = J.JWrapper(jobj) result = obj.replace("Hello,", "Goodbye cruel") self.assertEquals(result, "Goodbye cruel world.")
def test_03_03_cw_get_classes(self): c = javabridge.get_class_wrapper('java.lang.Number') classes = c.getClasses() self.assertEqual( len(javabridge.get_env().get_object_array_elements(classes)), 0)
def from_jobject(cls, obj): env = javabridge.get_env() # type: javabridge.JB_Env inst = cls(jobject=obj, options=None) return inst
# are captured and witten back can be N-D) # import javabridge as J import numpy as np importClass("net.imagej.display.ImageDisplay") importClass("net.imglib2.util.ImgUtil") display = d.getActiveDisplay(ImageDisplay.klass) data = display.getActiveView().getData() imgplus = data.getImgPlus() ndims = imgplus.numDimensions() start = [imgplus.min(i) for i in range(ndims)] end = [imgplus.max(i) + 1 for i in range(ndims)] dims = np.array(end) - np.array(start) a = np.zeros(np.prod(dims), np.float64) ja = J.get_env().make_double_array(np.ascontiguousarray(a)) strides = np.ones(len(dims), int) for i in range(0, len(dims) - 1): strides[-i - 2] = strides[-i - 1] * dims[-i - 1] ImgUtil.copy(imgplus, ja, 0, strides) a = J.get_env().get_double_array_elements(ja) a.shape = dims # # OK now apply a little amateurish warping. # i, j = np.mgrid[0:dims[0], 0:dims[1]].astype(float) id = np.sin(2 * np.pi * i * frequency / dims[0]) * magnitude jd = np.sin(2 * np.pi * j * frequency / dims[1]) * magnitude ii = np.maximum(0, np.minimum(dims[0] - 1, i + id)).astype(int) jj = np.maximum(0, np.minimum(dims[1] - 1, j + jd)).astype(int)