def run(self, module_info, pre = None, post = None, **kwargs): '''Run a module module_info - the module_info of the module to run pre - list of PreprocessorPlugins to run before running module post - list of PostprocessorPlugins to run after running module *kwargs - names and values for input parameters ''' input_map = J.make_map(kwargs) if pre is not None: pre = J.static_call("java/util/Arrays", "asList", "([Ljava/lang/Object;)Ljava/util/List;", pre) if post is not None: post = J.static_call("java/util/Arrays", "asList", "([Ljava/lang/Object;)Ljava/util/List;", post) future = J.call( self.o, "run", "(Lorg/scijava/module/ModuleInfo;" "Ljava/util/List;" "Ljava/util/List;" "Ljava/util/Map;)" "Ljava/util/concurrent/Future;", module_info, pre, post, input_map) return J.call( self.o, "waitFor", "(Ljava/util/concurrent/Future;)Lorg/scijava/module/Module;", future)
def create_dataset(context, pixel_data, name = None, axes = None): '''Create a dataset from a numpy array pixel_data - numpy array where index 0 is the I or Y axis, index 1 is the J or X axis and index 2, if it exists, is the channel axis. name - optional name for the dataset ''' dataset_service = get_dataset_service(context) if axes is None: if pixel_data.ndim == 2: axes = [Axes().X, Axes().Y] pixel_data = pixel_data.transpose((1,0)) else: axes = [Axes().X, Axes().Y, Axes().CHANNEL] pixel_data = pixel_data.transpose((1,0,2)) # # Create a dataset of the correct shape, with the correct axes. # We make a 64-bit floating point image. # dataset = dataset_service.create1( np.array(pixel_data.shape), name, axes, 64, True, True) imgplus = dataset.getImgPlus() # # Now use a copying utility to fill the imgplus with array data # strides = np.cumprod([1]+ list(pixel_data.shape[:0:-1]))[::-1] J.static_call("net/imglib2/util/ImgUtil", "copy", "([DI[ILnet/imglib2/img/Img;)V", pixel_data.flatten(), 0, strides, imgplus) return dataset
def get_commands(): """Return a list of the available command strings""" hashtable = J.static_call('ij/Menus', 'getCommands', '()Ljava/util/Hashtable;') if hashtable is None: # # This is a little bogus, but works - trick IJ into initializing # execute_command("pleaseignorethis") hashtable = J.static_call('ij/Menus', 'getCommands', '()Ljava/util/Hashtable;') if hashtable is None: return [] keys = J.call(hashtable, "keys", "()Ljava/util/Enumeration;") keys = J.jenumeration_to_string_list(keys) values = J.call(hashtable, "values", "()Ljava/util/Collection;") values = [J.to_string(x) for x in J.iterate_java( J.call(values, 'iterator', "()Ljava/util/Iterator;"))] class CommandList(list): def __init__(self): super(CommandList, self).__init__(keys) self.values = values return CommandList()
def finalize(self, result): try: javabridge.deactivate_awt() import imagej.imagej2 if imagej.imagej2.the_imagej_context is not None: script = """ new java.lang.Runnable () { run: function() { ctx.getContext().dispose(); } }""" runnable = javabridge.run_script( script, dict(ctx=imagej.imagej2.the_imagej_context)) javabridge.execute_runnable_in_main_thread(runnable, True) imagej.imagej2.the_imagej_context = None javabridge.static_call("java/lang/System", "gc", "()V") except: pass try: from ilastik.core.jobMachine import GLOBAL_WM GLOBAL_WM.stopWorkers() except: logging.root.warn("Failed to stop Ilastik") try: from cellprofiler.utilities.zmqrequest import join_to_the_boundary join_to_the_boundary() except: logging.root.warn("Failed to stop zmq boundary")
def refresh_cache(): """ Refreshes the cache. """ establish_cache() javabridge.static_call( "weka/core/WekaPackageManager", "refreshCache", "([Ljava/io/PrintStream;)Ljava/lang/Exception;", [])
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 filter(self, data): """ Filters the dataset(s). When providing a list, this can be used to create compatible train/test sets, since the filter only gets initialized with the first dataset and all subsequent datasets get transformed using the same setup. NB: inputformat(Instances) must have been called beforehand. :param data: the Instances to filter :type data: Instances or list of Instances :return: the filtered Instances object(s) :rtype: Instances or list of Instances """ if isinstance(data, list): result = [] for d in data: result.append(Instances(javabridge.static_call( "Lweka/filters/Filter;", "useFilter", "(Lweka/core/Instances;Lweka/filters/Filter;)Lweka/core/Instances;", d.jobject, self.jobject))) return result else: return Instances(javabridge.static_call( "Lweka/filters/Filter;", "useFilter", "(Lweka/core/Instances;Lweka/filters/Filter;)Lweka/core/Instances;", data.jobject, self.jobject))
def execute_command(command, options = None): '''Execute the named command within ImageJ''' if options is None: J.static_call("ij/IJ", "run", "(Ljava/lang/String;)V", command) else: J.static_call("ij/IJ", "run", "(Ljava/lang/String;Ljava/lang/String;)V", command, options)
def update_never_remind(): '''Tell ImageJ never to remind us of updates Not as harsh as it sounds - this is done with headless preferences which go to /dev/null. ''' never = J.get_static_field("java/lang/Long", "MAX_VALUE", "J") J.static_call("net/imagej/updater/UpToDate", "setLatestNag", "(J)V", never)
def begin(self): javabridge.static_call("org/apache/log4j/BasicConfigurator", "configure", "()V") log4j_logger = javabridge.static_call("org/apache/log4j/Logger", "getRootLogger", "()Lorg/apache/log4j/Logger;") warn_level = javabridge.get_static_field("org/apache/log4j/Level","WARN", "Lorg/apache/log4j/Level;") javabridge.call(log4j_logger, "setLevel", "(Lorg/apache/log4j/Level;)V", warn_level)
def save(cls, filename, experiment): """ Saves the experiment to disk. :param filename: the filename to save the experiment to :type filename: str :param experiment: the Experiment to save :type experiment: Experiment """ javabridge.static_call( "weka/experiment/Experiment", "write", "(Ljava/lang/String;Lweka/experiment/Experiment;)V", filename, experiment.jobject)
def make_data(cls, generator, args): """ Generates data using the generator and commandline arguments. :param generator: the generator instance to use :type generator: DataGenerator :param args: the command-line arguments :type args: list """ javabridge.static_call( "Lweka/datagenerators/DataGenerator;", "makeData", "(Lweka/datagenerators/DataGenerator;[Ljava/lang/String;)V", generator.jobject, args)
def uninstall_package(name): """ Uninstalls a package. :param name: the name of the package :type name: str :return: whether successfully uninstalled :rtype: bool """ establish_cache() javabridge.static_call( "weka/core/WekaPackageManager", "uninstallPackage", "(Ljava/lang/String;Z[Ljava/io/PrintStream;)V", name, True, [])
def write(filename, jobject): """ Serializes the object to disk. JavaObject instances get automatically unwrapped :param filename: the file to serialize the object to :type filename: str :param jobject: the object to serialize :type jobject: JB_Object or JavaObject """ if isinstance(jobject, JavaObject): jobject = jobject.jobject javabridge.static_call( "Lweka/core/SerializationHelper;", "write", "(Ljava/lang/String;Ljava/lang/Object;)V", filename, jobject)
def type_str(self, short=False): """ Returns the type of the attribute as string. :return: the type :rtype: str """ if short: return javabridge.static_call( "weka/core/Attribute", "typeToStringShort", "(Lweka/core/Attribute;)Ljava/lang/String;", self.jobject) else: return javabridge.static_call( "weka/core/Attribute", "typeToString", "(Lweka/core/Attribute;)Ljava/lang/String;", self.jobject)
def close(self): if hasattr(self, "rdr"): self.rdr.close() del self.rdr.o del self.rdr if hasattr(self, "stream") and self.stream is not None: jutil.call(self.stream, 'close', '()V') del self.stream if self.using_temp_file: os.remove(self.path) self.using_temp_file = False # # Run the Java garbage collector here. # jutil.static_call("java/lang/System", "gc","()V")
def get_context(): '''Get the ImageJ context This is a singleton ImageJ context. We need a singleton for now because of http://trac.imagej.net/ticket/1413 This is an imagej.ImageJ, which at one point was the context. Call self.getContext() to get the org.scijava.Context which may be what you want. ''' global the_imagej_context if the_imagej_context is None: the_imagej_context = create_context(None) # # We have to turn off the updater and tell ImageJ to never call # System.exit. We have to tell ImageJ that we read the readme file. # # To Do: programatically turn off the updater and take control of # the quitting and exit process. # max_value = J.run_script( "java.lang.Long.toString(java.lang.Long.MAX_VALUE);") prefs = [ ("net.imagej.updater.UpToDate", "latestNag", max_value), ("net.imagej.options.OptionsMisc", "exitWhenQuitting", "false")] plugin_service = the_imagej_context.getService( "org.scijava.plugin.PluginService") ui_interface = J.class_for_name("org.scijava.ui.UserInterface") script = """ var result = java.lang.System.getProperty('ij.ui'); if (! result) { var infos = pluginService.getPluginsOfType(ui_interface); if (infos.size() > 0) { result = infos.get(0).getClassName(); } } result;""" ui_class = J.run_script(script, dict(pluginService=plugin_service, ui_interface=ui_interface)) first_run = "firstRun-"+the_imagej_context.getVersion() if ui_class: prefs.append((ui_class, first_run, "false")) for class_name, key, value in prefs: c = J.class_for_name(class_name) J.static_call( "org/scijava/util/Prefs", "put", "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)V", c, key, value) return the_imagej_context
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 close_all_windows(): '''Close all ImageJ windows Hide the ImageJ windows so that they don't go through the Save dialog, then call the Window Manager's closeAllWindows to get the rest. ''' jimage_list = javabridge.static_call('ij/WindowManager', 'getIDList', '()[I') if jimage_list is None: return image_list = javabridge.get_env().get_int_array_elements(jimage_list) for image_id in image_list: ip = javabridge.static_call('ij/WindowManager', 'getImage', '(I)Lij/ImagePlus;', image_id) ip = imagej.imageplus.get_imageplus_wrapper(ip) ip.hide() javabridge.static_call('ij/WindowManager', 'closeAllWindows', '()Z')
def __setitem__(self, key, value): """ Sets the specified element in the array. :param key: the index of the element to set :type key: int :param value: the object to set (JavaObject or JB_Object) """ if isinstance(value, JavaObject): obj = value.jobject else: obj = value if not isinstance(key, (int, long)): raise Exception("Key must be an integer!") javabridge.static_call( "Ljava/lang/reflect/Array;", "set", "(Ljava/lang/Object;ILjava/lang/Object;)V", self.jobject, key, obj)
def __init__(self, seed = None, numNodes = 10, numEdges = 10): if seed is not None: RandomUtil = javabridge.static_call("edu/cmu/tetrad/util/RandomUtil","getInstance","()Ledu/cmu/tetrad/util/RandomUtil;") javabridge.call(RandomUtil, "setSeed", "(J)V", seed) dag = None initEdges = -1 while initEdges < numEdges: graph = javabridge.static_call("edu/cmu/tetrad/graph/GraphUtils","randomGraph","(IIIIIIZ)Ledu/cmu/tetrad/graph/Graph;",numNodes,0,numEdges,30,15,15,False) dag = javabridge.JClassWrapper("edu.cmu.tetrad.graph.Dag")(graph) initEdges = dag.getNumEdges() self.nodes = pycausal.extractTetradGraphNodes(dag) self.edges = pycausal.extractTetradGraphEdges(dag) self.graph = pycausal.generatePyDotGraph(self.nodes,self.edges) self.dag = dag
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 start_vm( args=None, class_path=None, max_heap_size=None, run_headless=False ): """ Start the JVM via Javabridge. """ if class_path is None: class_path = get_java_class_path() # Start the JVM. javabridge.start_vm( args=args, class_path=class_path, max_heap_size=max_heap_size, run_headless=run_headless ) # Suppress Java logging to stdout. java_stack = javabridge.make_instance( 'java/io/ByteArrayOutputStream', "()V" ) java_stack_ps = javabridge.make_instance( 'java/io/PrintStream', "(Ljava/io/OutputStream;)V", java_stack ) javabridge.static_call( 'Ljava/lang/System;', "setErr", '(Ljava/io/PrintStream;)V', java_stack_ps ) java_out = javabridge.make_instance( 'java/io/ByteArrayOutputStream', "()V" ) java_out_ps = javabridge.make_instance( 'java/io/PrintStream', "(Ljava/io/OutputStream;)V", java_out ) javabridge.static_call( 'Ljava/lang/System;', "setOut", '(Ljava/io/PrintStream;)V', java_out_ps ) javabridge.attach()
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 set_property(self, path, jobject): """ Attempts to set the value (jobject, a Java object) of the provided (bean) property path. :param path: the property path, e.g., "filter" for a setFilter(...)/getFilter() method pair :type path: str :param jobject: the Java object to set; if instance of JavaObject class, the jobject member is automatically used :type jobject: JB_Object """ # unwrap? if isinstance(jobject, JavaObject): jobject = jobject.jobject javabridge.static_call( "Lweka/core/PropertyPath;", "setValue", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", self.jobject, path, jobject)
def write_all(filename, jobjects): """ Serializes the list of objects to disk. JavaObject instances get automatically unwrapped :param filename: the file to serialize the object to :type filename: str :param jobjects: the list of objects to serialize :type jobjects: list """ array = javabridge.get_env().make_object_array(len(jobjects), javabridge.get_env().find_class("java/lang/Object")) for i in xrange(len(jobjects)): obj = jobjects[i] if isinstance(obj, JavaObject): obj = obj.jobject javabridge.get_env().set_object_array_element(array, i, obj) javabridge.static_call( "Lweka/core/SerializationHelper;", "writeAll", "(Ljava/lang/String;[Ljava/lang/Object;)V", filename, array)
def init_logger(): rootLoggerName = javabridge.get_static_field("org/slf4j/Logger", "ROOT_LOGGER_NAME", "Ljava/lang/String;") rootLogger = javabridge.static_call("org/slf4j/LoggerFactory", "getLogger", "(Ljava/lang/String;)Lorg/slf4j/Logger;", rootLoggerName) logLevel = javabridge.get_static_field("ch/qos/logback/classic/Level", "WARN", "Lch/qos/logback/classic/Level;") javabridge.call(rootLogger, "setLevel", "(Lch/qos/logback/classic/Level;)V", logLevel)
def test_01_08_jenumeration_to_string_list(self): properties = javabridge.static_call("java/lang/System", "getProperties", "()Ljava/util/Properties;") d = javabridge.get_dictionary_wrapper(properties) keys = javabridge.jenumeration_to_string_list(d.keys()) enum = javabridge.get_enumeration_wrapper(d.keys()) for i in range(d.size()): key = javabridge.to_string(enum.nextElement()) self.assertEqual(key, keys[i])
def test_01_09_jdictionary_to_string_dictionary(self): properties = javabridge.static_call("java/lang/System", "getProperties", "()Ljava/util/Properties;") d = javabridge.get_dictionary_wrapper(properties) pyd = javabridge.jdictionary_to_string_dictionary(properties) keys = javabridge.jenumeration_to_string_list(d.keys()) for key in keys: value = javabridge.to_string(d.get(key)) self.assertEqual(pyd[key], value)
def get_prc(data): """ Calculates the area under the precision recall curve (PRC). :param data: the threshold curve data :type data: Instances :return: the area :rtype: float """ return javabridge.static_call( "weka/classifiers/evaluation/ThresholdCurve", "getPRCArea", "(Lweka/core/Instances;)D", data.jobject)
def saver_for_file(filename): """ Returns a Saver that can load the specified file, based on the file extension. None if failed to determine. :param filename: the filename to get the saver for :type filename: str :return: the associated saver instance or None if none found :rtype: Saver """ saver = javabridge.static_call( "weka/core/converters/ConverterUtils", "getSaverForFile", "(Ljava/lang/String;)Lweka/core/converters/AbstractFileSaver;", filename) if saver is None: return None else: return Saver(jobject=saver)
def test_01_07_get_dictionary_wrapper(self): properties = javabridge.static_call("java/lang/System", "getProperties", "()Ljava/util/Properties;") d = javabridge.get_dictionary_wrapper(properties) self.assertTrue(d.size() > 10) self.assertFalse(d.isEmpty()) keys = javabridge.get_enumeration_wrapper(d.keys()) values = javabridge.get_enumeration_wrapper(d.elements()) n_elems = d.size() for i in range(n_elems): self.assertTrue(keys.hasMoreElements()) key = javabridge.to_string(keys.nextElement()) self.assertTrue(values.hasMoreElements()) value = javabridge.to_string(values.nextElement()) self.assertEqual(javabridge.to_string(d.get(key)), value) self.assertFalse(keys.hasMoreElements()) self.assertFalse(values.hasMoreElements())
def read_all(filename): """ Reads the serialized objects from disk. Caller must wrap objects in appropriate Python wrapper classes. :param filename: the file with the serialized objects :type filename: str :return: the list of JB_OBjects :rtype: list """ array = javabridge.static_call("Lweka/core/SerializationHelper;", "readAll", "(Ljava/lang/String;)[Ljava/lang/Object;", filename) if array is None: return None else: return javabridge.get_env().get_object_array_elements(array)
def loadDiscreteData(self, df): tetradData = None if(len(df.index)*df.columns.size <= 1500): dataBox = javabridge.JClassWrapper('edu.cmu.tetrad.data.VerticalIntDataBox')(len(df.index),df.columns.size) node_list = javabridge.JClassWrapper('java.util.ArrayList')() col_no = 0 for col in df.columns: cat_array = sorted(set(df[col])) cat_list = javabridge.JClassWrapper('java.util.ArrayList')() for cat in cat_array: cat = str(cat) cat_list.add(cat) nodname = javabridge.JClassWrapper('java.lang.String')(col) nodi = javabridge.JClassWrapper('edu.cmu.tetrad.data.DiscreteVariable')(nodname,cat_list) node_list.add(nodi) for row in df.index: value = javabridge.JClassWrapper('java.lang.Integer')(cat_array.index(df.iloc[row,col_no])) dataBox.set(row,col_no,value) col_no = col_no + 1 tetradData = javabridge.JClassWrapper('edu.cmu.tetrad.data.BoxDataSet')(dataBox, node_list) else: # Generate random name temp_data_file = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10)) + '.csv' temp_data_path = os.path.join(tempfile.gettempdir(), temp_data_file) df.to_csv(temp_data_path, sep = "\t", index = False) # Read Data from File f = javabridge.JClassWrapper('java.io.File')(temp_data_path) delimiter = javabridge.get_static_field('edu/pitt/dbmi/data/Delimiter','TAB','Ledu/pitt/dbmi/data/Delimiter;') dataReader = javabridge.JClassWrapper('edu.pitt.dbmi.data.reader.tabular.VerticalDiscreteTabularDataReader')(f,delimiter) tetradData = dataReader.readInData() tetradData = javabridge.static_call('edu/pitt/dbmi/causal/cmd/util/TetradDataUtils','toDataModel','(Ledu/pitt/dbmi/data/Dataset;)Ledu/cmu/tetrad/data/DataModel;', tetradData) os.remove(temp_data_path) return tetradData
def _init_logger(): """This is so that Javabridge doesn't spill out a lot of DEBUG messages during runtime. From CellProfiler/python-bioformats. """ rootLoggerName = jb.get_static_field("org/slf4j/Logger", "ROOT_LOGGER_NAME", "Ljava/lang/String;") rootLogger = jb.static_call("org/slf4j/LoggerFactory", "getLogger", "(Ljava/lang/String;)Lorg/slf4j/Logger;", rootLoggerName) logLevel = jb.get_static_field("ch/qos/logback/classic/Level", "WARN", "Lch/qos/logback/classic/Level;") jb.call(rootLogger, "setLevel", "(Lch/qos/logback/classic/Level;)V", logLevel)
def crossvalidate_model(cls, clusterer, data, num_folds, rnd): """ Cross-validates the clusterer and returns the loglikelihood. :param clusterer: the clusterer instance to evaluate :type clusterer: Clusterer :param data: the data to evaluate on :type data: Instances :param num_folds: the number of folds :type num_folds: int :param rnd: the random number generator to use :type rnd: Random :return: the cross-validated loglikelihood :rtype: float """ return javabridge.static_call( "Lweka/clusterers/ClusterEvaluation;", "crossValidateModel", "(Lweka/clusterers/DensityBasedClusterer;Lweka/core/Instances;ILjava/util/Random;)D", clusterer.jobject, data.jobject, num_folds, rnd.jobject)
def loadContinuousData(self, df, outputDataset = False): tetradData = None if(len(df.index)*df.columns.size <= 1500): dataBox = javabridge.JClassWrapper('edu.cmu.tetrad.data.DoubleDataBox')(len(df.index),df.columns.size) node_list = javabridge.JClassWrapper('java.util.ArrayList')() col_no = 0 for col in df.columns: nodi = javabridge.JClassWrapper('edu.cmu.tetrad.data.ContinuousVariable')(col) node_list.add(nodi) for row in df.index: value = javabridge.JClassWrapper('java.lang.Double')(df.iloc[row,col_no]) dataBox.set(row,col_no,value) col_no = col_no + 1 tetradData = javabridge.JClassWrapper('edu.cmu.tetrad.data.BoxDataSet')(dataBox, node_list) else: #Generate random name temp_data_file = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10)) + '.csv' temp_data_path = os.path.join(tempfile.gettempdir(), temp_data_file) df.to_csv(temp_data_path, sep = '\t', index = False) # Read Data from File f = javabridge.JClassWrapper('java.io.File')(temp_data_path) delimiter = javabridge.get_static_field('edu/pitt/dbmi/data/Delimiter','TAB','Ledu/pitt/dbmi/data/Delimiter;') dataReader = javabridge.JClassWrapper('edu.pitt.dbmi.data.reader.tabular.ContinuousTabularDataFileReader')(f,delimiter) tetradData = dataReader.readInData() tetradData = javabridge.static_call('edu/pitt/dbmi/causal/cmd/util/TetradDataUtils','toDataModel','(Ledu/pitt/dbmi/data/Dataset;)Ledu/cmu/tetrad/data/DataModel;', tetradData) os.remove(temp_data_path) if(not outputDataset): tetradData = javabridge.JClassWrapper('edu.cmu.tetrad.data.CovarianceMatrixOnTheFly')(tetradData) return tetradData
def _start_lif_reader(self): jv.start_vm(class_path=bf.JARS) log_level = 'ERROR' # reduce log level rootLoggerName = jv.get_static_field("org/slf4j/Logger", "ROOT_LOGGER_NAME", "Ljava/lang/String;") rootLogger = jv.static_call("org/slf4j/LoggerFactory", "getLogger", "(Ljava/lang/String;)Lorg/slf4j/Logger;", rootLoggerName) logLevel = jv.get_static_field("ch/qos/logback/classic/Level", log_level, "Lch/qos/logback/classic/Level;") jv.call(rootLogger, "setLevel", "(Lch/qos/logback/classic/Level;)V", logLevel) self.ir = bf.ImageReader(self.lif_file_path, perform_init=True) mdroot = et.ElementTree.fromstring(bf.get_omexml_metadata(self.lif_file_path)) mds = list(map(lambda e: e.attrib, mdroot.iter('{http://www.openmicroscopy.org/Schemas/OME/2016-06}Pixels'))) # lif can contain multiple images, select one that is likely to be the timeseries self.metadata = None self.lif_stack_idx = 0 for idx, md in enumerate(mds): if int(md['SizeT']) > 1: self.lif_stack_idx = idx self.metadata = md if not self.metadata: raise ValueError('lif does not contain an image with sizeT > 1')
def wrapped(*args, **kwargs): try: javabridge.start_vm(class_path=bioformats.JARS) myloglevel = "ERROR" # user string argument for logLevel. rootLoggerName = javabridge.get_static_field( "org/slf4j/Logger", "ROOT_LOGGER_NAME", "Ljava/lang/String;") rootLogger = javabridge.static_call( "org/slf4j/LoggerFactory", "getLogger", "(Ljava/lang/String;)Lorg/slf4j/Logger;", rootLoggerName, ) logLevel = javabridge.get_static_field( "ch/qos/logback/classic/Level", myloglevel, "Lch/qos/logback/classic/Level;", ) javabridge.call(rootLogger, "setLevel", "(Lch/qos/logback/classic/Level;)V", logLevel) return func(*args, **kwargs) finally: javabridge.kill_vm()
def has_omero_packages(): '''Return True if we can find the packages needed for OMERO In order to run OMERO, you'll need the OMERO client and ICE on your class path (not supplied with python-bioformats and specific to your server's version) ''' global __has_omero_jars if __has_omero_jars is None: class_loader = jutil.static_call("java/lang/ClassLoader", "getSystemClassLoader", "()Ljava/lang/ClassLoader;") for klass in ("Glacier2.PermissionDeniedException", "loci.ome.io.OmeroReader", "omero.client"): try: jutil.call(class_loader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;", klass) except: __has_omero_jars = False break else: __has_omero_jars = True return __has_omero_jars
def set_current_image(image_plus): '''Put the given image on the top of the batch mode image stack image_plus - a wrapped imagePlus ''' # # Make sure we are in batch mode prior to adding the image. # If not, the image just goes into the garbage. # J.static_call("ij/macro/Interpreter", "setBatchMode", "(Z)V", True) # # Remove the image, if it exists, from its current position # on the stack # J.static_call("ij/macro/Interpreter", "removeBatchModeImage", "(Lij/ImagePlus;)V", image_plus.o) J.static_call("ij/macro/Interpreter", "addBatchModeImage", "(Lij/ImagePlus;)V", image_plus.o)
def init_clojure_runtime(): """Initialize the clojure runtime. This needs to happen at least once before attempting to require a namespace or lookup a clojure var.""" javabridge.static_call("clojure/lang/RT", "init", "()V")
def symbol(sym_name): """Create a clojure symbol from a string""" return javabridge.static_call("clojure/lang/Symbol", "intern", "(Ljava/lang/String;)Lclojure/lang/Symbol;", sym_name)
def test_01_03_01_static_call(self): result = javabridge.static_call("Ljava/lang/String;", "valueOf", "(I)Ljava/lang/String;",123) self.assertEqual(result, "123")
def get_image_by_id(imagej_id): '''Get an ImagePlus object by its ID''' return get_imageplus_wrapper( J.static_call('ij/WindowManager', 'getImage', '(I)Lij/ImagePlus;', imagej_id))
def get_id_list(): '''Get the list of IDs of open images''' jid_list = J.get_env().get_int_array_elements( J.static_call('ij/WindowManager', 'getIDList', '()[I')) return jid_list
def make_unique_name(proposed_name): '''Create a unique title name for an imageplus object''' return J.static_call('ij/WindowManager', 'makeUniqueName', '(Ljava/lang/String;)Ljava/lang/String;', proposed_name)
def run_imagej(*args): J.static_call("net/imagej/Main", "main", "([Ljava/lang/String;)V", *[unicode(arg) for arg in args])
def loadMixedData(self, df, numCategoriesToDiscretize = 4): tetradData = None if(len(df.index)*df.columns.size <= 1500): node_list = javabridge.JClassWrapper('java.util.ArrayList')() cont_list = [] disc_list = [] col_no = 0 for col in df.columns: cat_array = sorted(set(df[col])) if(len(cat_array) > numCategoriesToDiscretize): # Continuous variable nodi = javabridge.JClassWrapper('edu.cmu.tetrad.data.ContinuousVariable')(col) node_list.add(nodi) cont_list.append(col_no) else: # Discrete variable cat_list = javabridge.JClassWrapper('java.util.ArrayList')() for cat in cat_array: cat = str(cat) cat_list.add(cat) nodname = javabridge.JClassWrapper('java.lang.String')(col) nodi = javabridge.JClassWrapper('edu.cmu.tetrad.data.DiscreteVariable')(nodname,cat_list) node_list.add(nodi) disc_list.append(col_no) col_no = col_no + 1 mixedDataBox = javabridge.JClassWrapper('edu.cmu.tetrad.data.MixedDataBox')(node_list, len(df.index)) for row in df.index: for col in cont_list: value = javabridge.JClassWrapper('java.lang.Double')(df.iloc[row,col]) mixedDataBox.set(row,col,value) for col in disc_list: cat_array = sorted(set(df[df.columns[col]])) value = javabridge.JClassWrapper('java.lang.Integer')(cat_array.index(df.iloc[row,col])) mixedDataBox.set(row,col,value) tetradData = javabridge.JClassWrapper('edu.cmu.tetrad.data.BoxDataSet')(mixedDataBox, node_list) else: # Generate random name temp_data_file = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10)) + '.csv' temp_data_path = os.path.join(tempfile.gettempdir(), temp_data_file) df.to_csv(temp_data_path, sep = "\t", index = False) # Read Data from File f = javabridge.JClassWrapper('java.io.File')(temp_data_path) delimiter = javabridge.get_static_field('edu/pitt/dbmi/data/Delimiter','TAB','Ledu/pitt/dbmi/data/Delimiter;') dataReader = javabridge.JClassWrapper('edu.pitt.dbmi.data.reader.tabular.MixedTabularDataFileReader')(numCategoriesToDiscretize, f,delimiter) tetradData = dataReader.readInData() tetradData = javabridge.static_call('edu/pitt/dbmi/causal/cmd/util/TetradDataUtils','toDataModel','(Ledu/pitt/dbmi/data/Dataset;)Ledu/cmu/tetrad/data/DataModel;', tetradData) os.remove(temp_data_path) return tetradData
def establish_cache(): """ Establishes the package cache if necessary. """ javabridge.static_call( "weka/core/WekaPackageManager", "establishCacheIfNeeded", "([Ljava/io/PrintStream;)Ljava/lang/Exception;", [])
def tearDownClass(cls): del cls.context J.static_call('java/lang/System', 'gc', '()V')
def getPixelTypeString(cls, pixel_type): return jutil.static_call('loci/formats/FormatTools', 'getPixelTypeString', '(I)Ljava/lang/String;', pixel_type)
def is_image_extension(suffix): '''Return True if the extension is one of those recongized by bioformats''' extensions = J.get_collection_wrapper( J.static_call("org/cellprofiler/imageset/filter/IsImagePredicate", "getImageSuffixes", "()Ljava/util/Set;")) return extensions.contains(suffix.lower())
def __init__(self, df, penaltydiscount=4, depth=3, faithfulness=True, verbose=False, java_max_heap_size=None): tetrad_libdir = os.path.join(os.path.dirname(__file__), 'lib') # print 'tetrad_libdir: %s' % tetrad_libdir for l in glob.glob(tetrad_libdir + os.sep + "*.jar"): javabridge.JARS.append(str(l)) javabridge.start_vm(run_headless=True, max_heap_size=java_max_heap_size) javabridge.attach() tetradData = None if (len(df.index) * df.columns.size <= 1500): node_list = javabridge.JWrapper( javabridge.make_instance("java/util/ArrayList", "()V")) for col in df.columns: nodname = javabridge.make_instance("java/lang/String", "(Ljava/lang/String;)V", col) nodi = javabridge.make_instance( "edu/cmu/tetrad/graph/GraphNode", "(Ljava/lang/String;)V", nodname) node_list.add(nodi) tetradMatrix = javabridge.JWrapper( javabridge.make_instance("edu/cmu/tetrad/util/TetradMatrix", "(II)V", len(df.index), df.columns.size)) for row in df.index: for col in range(0, df.columns.size): tetradMatrix.set(row, col, df.ix[row][col]) tetradData = javabridge.static_call( "edu/cmu/tetrad/data/ColtDataSet", "makeContinuousData", "(Ljava/util/List;Ledu/cmu/tetrad/util/TetradMatrix;)Ledu/cmu/tetrad/data/DataSet;", node_list, tetradMatrix) else: #Generate random name temp_data_file = ''.join( random.choice(string.lowercase) for i in range(10)) + '.csv' temp_data_path = os.path.join(tempfile.gettempdir(), temp_data_file) df.to_csv(temp_data_path, sep="\t") f = javabridge.make_instance("java/io/File", "(Ljava/lang/String;)V", temp_data_path) excludeVar = javabridge.JWrapper( javabridge.make_instance("java/util/HashSet", "()V")) excludeVar.add("MULT") tetradData = javabridge.static_call( "edu/cmu/tetrad/data/BigDataSetUtility", "readInContinuousData", "(Ljava/io/File;CLjava/util/Set;)Ledu/cmu/tetrad/data/DataSet;", f, "\t", excludeVar) os.remove(temp_data_path) fgs = javabridge.make_instance("edu/cmu/tetrad/search/Fgs", "(Ledu/cmu/tetrad/data/DataSet;)V", tetradData) fgs = javabridge.JWrapper(fgs) fgs.setPenaltyDiscount( penaltydiscount ) # set to 2 if variable# <= 50 otherwise set it to 4 fgs.setDepth(depth) #-1 fgs.setNumPatternsToStore(0) fgs.setFaithfulnessAssumed(faithfulness) fgs.setVerbose(verbose) tetradGraph = fgs.search() graph = pydot.Dot(graph_type='digraph') n = tetradGraph.getNodeNames().toString() n = n[1:len(n) - 1] n = n.split(",") nodes = [] for i in range(0, len(n)): node = n[i] n[i] = node.strip() nodes.append(pydot.Node(n[i])) graph.add_node(nodes[i]) self.nodes = n e = tetradGraph.getEdges().toString() e = e[1:len(e) - 1] e = e.split(",") for i in range(0, len(e)): e[i] = e[i].strip() token = e[i].split(" ") if (len(token) == 3): src = token[0] arc = token[1] dst = token[2] if (pycausal.isNodeExisting(n, src) and pycausal.isNodeExisting(n, dst)): edge = pydot.Edge(nodes[n.index(src)], nodes[n.index(dst)]) if (arc == "---"): edge.set_arrowhead("none") graph.add_edge(edge) self.edges = e javabridge.detach() javabridge.kill_vm() self.graph = graph
def get_user_loader(): '''The class loader used to load user plugins''' return J.static_call("ij/IJ", "getClassLoader", "()Ljava/lang/ClassLoader;")
def allow_quit(): '''Allow the CellProfilerAppEventService to dispose of ImageJ ''' J.static_call("org/cellprofiler/ijutils/CellProfilerApp", "allowQuit", "()V")
def tetradGraphToDot(self, tetradGraph): graph_dot = javabridge.static_call( 'edu/cmu/tetrad/graph/GraphUtils', 'graphToDot', '(Ledu/cmu/tetrad/graph/Graph;)Ljava/lang/String;', tetradGraph) return graph_dot
def get_image_by_name(title): '''Get the ImagePlus object whose title (in the window) matches "title"''' return get_imageplus_wrapper( J.static_call('ij/WindowManager', 'getImage', '(Ljava/lang/String;)Lij/ImagePlus;', title))
def start(class_path=None, bundled=True, packages=False, system_cp=False, max_heap_size=None, system_info=False): """ Initializes the javabridge connection (starts up the JVM). :param class_path: the additional classpath elements to add :type class_path: list :param bundled: whether to add jars from the "lib" directory :type bundled: bool :param packages: whether to add jars from Weka packages as well (bool) or an alternative Weka home directory (str) :type packages: bool or str :param system_cp: whether to add the system classpath as well :type system_cp: bool :param max_heap_size: the maximum heap size (-Xmx parameter, eg 512m or 4g) :type max_heap_size: str :param system_info: whether to print the system info (generated by weka.core.SystemInfo) :type system_info: bool """ global started global with_package_support if started is not None: logger.info("JVM already running, call jvm.stop() first") return # add user-defined jars first if class_path is not None: for cp in class_path: logger.debug("Adding user-supplied classpath=" + cp) javabridge.JARS.append(cp) if bundled: logger.debug("Adding bundled jars") add_bundled_jars() if system_cp: logger.debug("Adding system classpath") add_system_classpath() logger.debug("Classpath=" + str(javabridge.JARS)) logger.debug("MaxHeapSize=" + ("default" if (max_heap_size is None) else max_heap_size)) args = [] weka_home = None if packages is not None: if isinstance(packages, bool): if packages: with_package_support = True logger.debug("Package support enabled") else: logger.debug("Package support disabled") args.append("-Dweka.packageManager.loadPackages=false") if isinstance(packages, str): if os.path.exists(packages) and os.path.isdir(packages): logger.debug("Using alternative Weka home directory: " + packages) weka_home = packages with_package_support = True else: logger.warning("Invalid Weka home: " + packages) javabridge.start_vm(args=args, run_headless=True, max_heap_size=max_heap_size) javabridge.attach() started = True if weka_home is not None: from weka.core.classes import Environment env = Environment.system_wide() logger.debug("Using alternative Weka home directory: " + packages) env.add_variable("WEKA_HOME", weka_home) # initialize package manager javabridge.static_call( "Lweka/core/WekaPackageManager;", "loadPackages", "(Z)V", False) # output system info if system_info: logger.debug("System info:") jobj = javabridge.make_instance("weka/core/SystemInfo", "()V") hashtable = javabridge.call(jobj, "getSystemInfo", "()Ljava/util/Hashtable;") info = javabridge.jdictionary_to_string_dictionary(hashtable) for k in info.keys(): logger.debug(k + "=" + info[k])