def main(args): """ Trains Apriori on the specified dataset (uses vote UCI dataset if no dataset specified). :param args: the commandline arguments :type args: list """ # load a dataset if len(args) <= 1: data_file = helper.get_data_dir() + os.sep + "vote.arff" else: data_file = args[1] helper.print_info("Loading dataset: " + data_file) loader = Loader("weka.core.converters.ArffLoader") data = loader.load_file(data_file) data.class_is_last() # build Apriori, using last attribute as class attribute apriori = Associator(classname="weka.associations.Apriori", options=["-c", "-1"]) apriori.build_associations(data) print(str(apriori)) # iterate association rules (low-level) helper.print_info("Rules (low-level)") # make the underlying rules list object iterable in Python rules = javabridge.iterate_collection(apriori.jwrapper.getAssociationRules().getRules().o) for i, r in enumerate(rules): # wrap the Java object to make its methods accessible rule = JWrapper(r) print(str(i+1) + ". " + str(rule)) # output some details on rule print(" - consequence support: " + str(rule.getConsequenceSupport())) print(" - premise support: " + str(rule.getPremiseSupport())) print(" - total support: " + str(rule.getTotalSupport())) print(" - total transactions: " + str(rule.getTotalTransactions())) # iterate association rules (high-level) helper.print_info("Rules (high-level)") print("can produce rules? " + str(apriori.can_produce_rules())) print("rule metric names: " + str(apriori.rule_metric_names)) rules = apriori.association_rules() if rules is not None: print("producer: " + rules.producer) print("# rules: " + str(len(rules))) for i, rule in enumerate(rules): print(str(i+1) + ". " + str(rule)) # output some details on rule print(" - consequence support: " + str(rule.consequence_support)) print(" - consequence: " + str(rule.consequence)) print(" - premise support: " + str(rule.premise_support)) print(" - premise: " + str(rule.premise)) print(" - total support: " + str(rule.total_support)) print(" - total transactions: " + str(rule.total_transactions)) print(" - metric names: " + str(rule.metric_names)) print(" - metric values: " + str(rule.metric_values)) print(" - metric value 'Confidence': " + str(rule.metric_value('Confidence'))) print(" - primary metric name: " + str(rule.primary_metric_name)) print(" - primary metric value: " + str(rule.primary_metric_value))
class ModuleInfo(object): def __init__(self): self.o = instance getInput = J.make_method( "getInput", "(Ljava/lang/String;)Lorg/scijava/module/ModuleItem;", doc = "Gets the input item with the given name.", fn_post_process=wrap_module_item) getOutput = J.make_method( "getOutput", "(Ljava/lang/String;)Lorg/scijava/module/ModuleItem;", doc = "Gets the output item with the given name.", fn_post_process=wrap_module_item) getInputs = J.make_method( "inputs", "()Ljava/lang/Iterable;", """Get the module info's input module items""", fn_post_process=lambda iterator: map(wrap_module_item, J.iterate_collection(iterator))) getOutputs = J.make_method( "outputs", "()Ljava/lang/Iterable;", """Get the module info's output module items""", fn_post_process=lambda iterator: map(wrap_module_item, J.iterate_collection(iterator))) getTitle = J.make_method( "getTitle", "()Ljava/lang/String;") createModule = J.make_method( "createModule", "()Lorg/scijava/module/Module;", fn_post_process=wrap_module) getMenuPath = J.make_method( "getMenuPath", "()Lorg/scijava/MenuPath;") getMenuRoot = J.make_method( "getMenuRoot", "()Ljava/lang/String;") getName = J.make_method("getName", "()Ljava/lang/String;") getClassName = J.make_method("getClassName", "()Ljava/lang/String;")
def get_pixel_data(self, axes=None): imgplus = self.getImgPlus() pixel_data = get_pixel_data(imgplus) script = """ var result = java.util.ArrayList(); for (i=0;i<imgplus.numDimensions();i++) result.add(imgplus.axis(i).type()); result""" inv_axes = J.run_script(script, dict(imgplus=imgplus)) inv_axes = list(J.iterate_collection(inv_axes)) transpose = calculate_transpose(inv_axes, axes) return pixel_data.transpose(transpose)
def get_pixel_data(self, axes = None): imgplus = self.getImgPlus() pixel_data = get_pixel_data(imgplus) script = """ var result = java.util.ArrayList(); for (i=0;i<imgplus.numDimensions();i++) result.add(imgplus.axis(i).type()); result""" inv_axes = J.run_script(script, dict(imgplus=imgplus)) inv_axes = list(J.iterate_collection(inv_axes)) transpose = calculate_transpose(inv_axes, axes) return pixel_data.transpose(transpose)
class DisplayService(object): def __init__(self): self.o = o def createDisplay(self, name, dataset): """Create a display that contains the given dataset :param dataset: :param name: """ return wrap_display( J.call( o, "createDisplay", "(Ljava/lang/String;Ljava/lang/Object;)Lorg/scijava/display/Display;", name, dataset)) def getActiveDisplay(self, klass=None): """Get the first display, optionally of the given type from the list klass - if not None, return the first display of this type, otherwise return the first display :param klass: """ if klass is None: return wrap_display( J.call(self.o, "getActiveDisplay", "()Lorg/scijava/display/Display;")) else: return wrap_display( J.call(self.o, "getActiveDisplay", "(Ljava/lang/Class;)Lorg/scijava/display/Display;", klass)) def getActiveImageDisplay(self): """Get the active imagej.data.display.ImageDisplay""" return wrap_display( J.call(self.o, "getActiveDisplay", "()Lorg/scijava/display/Display;", J.class_for_name("net.imagej.display.ImageDisplay"))) setActiveDisplay = J.make_method("setActiveDisplay", "(Lorg/scijava/display/Display;)V") getDisplays = J.make_method("getDisplays", "()Ljava/util/List;", fn_post_process=lambda x: map( wrap_display, J.iterate_collection(x))) getDisplay = J.make_method( "getDisplay", "(Ljava/lang/String;)Lorg/scijava/display/Display;", fn_post_process=wrap_display) isUniqueName = J.make_method("isUniqueName", "(Ljava/lang/String;)Z")
class ScriptService(object): def __init__(self, o=o): self.o = o getPluginService = J.make_method( "getPluginService", "()Lorg/scijava/plugin/PluginService;") getIndex = J.make_method( "getIndex", "()Lorg/scijava/script/ScriptLanguageIndex;") getLanguages = J.make_method( "getLanguages", "()Ljava/util/List;", doc = "Return the script engine factories supported by this service", fn_post_process = lambda jlangs: [ wrap_script_engine_factory(o) for o in J.iterate_collection(jlangs)]) getByFileExtension = J.make_method( "getLanguageByExtension", "(Ljava/lang/String;)Lorg/scijava/script/ScriptLanguage;", fn_post_process=wrap_script_engine_factory) getByName = J.make_method( "getLanguageByName", "(Ljava/lang/String;)Lorg/scijava/script/ScriptLanguage;", fn_post_process=wrap_script_engine_factory)
def test_02_06_menu_entry(self): svc = ij2.get_module_service(self.context) module_infos = svc.getModules() for module_info in module_infos: if module_info.getClassName() == \ 'net.imagej.plugins.commands.assign.AddSpecifiedNoiseToDataValues': menu_path = module_info.getMenuPath() for item in J.iterate_collection(menu_path): menu_entry = ij2.wrap_menu_entry(item) menu_entry.getName() menu_entry.setName("Foo") menu_entry.getWeight() menu_entry.setWeight(5) menu_entry.getMnemonic() menu_entry.setMnemonic("X") menu_entry.getAccelerator() menu_entry.setAccelerator(None) menu_entry.getIconPath() menu_entry.setIconPath(None) break else: raise AssertionError("Could not find target module")
def __iter__(self): return J.iterate_collection(self.o)
class DisplayService(object): def __init__(self): self.o = o def createDisplay(self, name, dataset): '''Create a display that contains the given dataset''' # # Must be run on the gui thread # jcallable = J.run_script( """new java.util.concurrent.Callable() { call:function() { return displayService.createDisplay(name, dataset); } }; """, dict(displayService=self.o, name=name, dataset=dataset.o)) future = J.make_future_task(jcallable, fn_post_process=wrap_display) return J.execute_future_in_main_thread(future) def getActiveDisplay(self, klass=None): '''Get the first display, optionally of the given type from the list klass - if not None, return the first display of this type, otherwise return the first display ''' if klass is None: return wrap_display( J.call(self.o, "getActiveDisplay", "()Lorg/scijava/display/Display;")) else: return wrap_display( J.call(self.o, "getActiveDisplay", "(Ljava/lang/Class;)Lorg/scijava/display/Display;", klass)) def getActiveImageDisplay(self): '''Get the active imagej.data.display.ImageDisplay''' return wrap_display( J.call(self.o, "getActiveDisplay", "()Lorg/scijava/display/Display;", J.class_for_name("net.imagej.display.ImageDisplay"))) def setActiveDisplay(self, display): '''Make this the active display''' # Note: has to be run in GUI thread on mac r = J.run_script( """new java.lang.Runnable() { run:function() { displayService.setActiveDisplay(display); } } """, dict(displayService=self.o, display=display.o)) J.execute_runnable_in_main_thread(r, True) getDisplays = J.make_method("getDisplays", "()Ljava/util/List;", fn_post_process=lambda x: map( wrap_display, J.iterate_collection(x))) getDisplay = J.make_method( "getDisplay", "(Ljava/lang/String;)Lorg/scijava/display/Display;", fn_post_process=wrap_display) isUniqueName = J.make_method("isUniqueName", "(Ljava/lang/String;)Z")
def main(args): """ Trains Apriori on the specified dataset (uses vote UCI dataset if no dataset specified). :param args: the commandline arguments :type args: list """ # load a dataset if len(args) <= 1: data_file = helper.get_data_dir() + os.sep + "vote.arff" else: data_file = args[1] helper.print_info("Loading dataset: " + data_file) loader = Loader("weka.core.converters.ArffLoader") data = loader.load_file(data_file) data.class_is_last() # build Apriori, using last attribute as class attribute apriori = Associator(classname="weka.associations.Apriori", options=["-c", "-1"]) apriori.build_associations(data) print(str(apriori)) # iterate association rules (low-level) helper.print_info("Rules (low-level)") # make the underlying rules list object iterable in Python rules = javabridge.iterate_collection( apriori.jwrapper.getAssociationRules().getRules().o) for i, r in enumerate(rules): # wrap the Java object to make its methods accessible rule = JWrapper(r) print(str(i + 1) + ". " + str(rule)) # output some details on rule print(" - consequence support: " + str(rule.getConsequenceSupport())) print(" - premise support: " + str(rule.getPremiseSupport())) print(" - total support: " + str(rule.getTotalSupport())) print(" - total transactions: " + str(rule.getTotalTransactions())) # iterate association rules (high-level) helper.print_info("Rules (high-level)") print("can produce rules? " + str(apriori.can_produce_rules())) print("rule metric names: " + str(apriori.rule_metric_names)) rules = apriori.association_rules() if rules is not None: print("producer: " + rules.producer) print("# rules: " + str(len(rules))) for i, rule in enumerate(rules): print(str(i + 1) + ". " + str(rule)) # output some details on rule print(" - consequence support: " + str(rule.consequence_support)) print(" - consequence: " + str(rule.consequence)) print(" - premise support: " + str(rule.premise_support)) print(" - premise: " + str(rule.premise)) print(" - total support: " + str(rule.total_support)) print(" - total transactions: " + str(rule.total_transactions)) print(" - metric names: " + str(rule.metric_names)) print(" - metric values: " + str(rule.metric_values)) print(" - metric value 'Confidence': " + str(rule.metric_value('Confidence'))) print(" - primary metric name: " + str(rule.primary_metric_name)) print(" - primary metric value: " + str(rule.primary_metric_value))