Options in GUI for custom Weka classifier - java

I've implemented my own custom classifier for Weka, which inherits from Classifier and implements Serializable and also OptionHandler.
At the moment i've been able to run the classifier from the Weka UI perfectly, but without parameters. I know that i need to implement the following methods in order to see the options in the UI:
public void setOptions(String [] options) throws Exception
public String [] getOptions()
public Enumeration listOptions()
I've implemented getOptions with some dummy code and it's being perfectly called by the UI (I used a System.out.println to log this). And I also implemented a dummy code for listOptions but it's never being called.
When I try to select the options for my algorithm by the UI i only see the "Debug" option. Do you have any ideas how to solve this problem and see my options by the UI?
Basically the thing that i want to do is to run my algorithm from Weka but using custom options/parameters.
I've read this document but i think it's not very useful:
http://weka.wikispaces.com/Writing+your+own+Classifier+(post+3.5.2)
Do you have any idea or example to solve this?

I had the same problem.
I realized by chance :) that if I use the get and set methods instead of just accessing the attributes it works!
More clearly, for an attribute named "threshold" represented by "T" as an option:
This code doesn't work:
public String[] getOptions() {
String[] options = new String[3];
int current = 0;
options[current++] = "-T";
options[current++] = "" + threshold;
while (current < options.length) {
options[current++] = "";
}
return options;
}
And this code works:
public String[] getOptions() {
String[] options = new String[3];
int current = 0;
options[current++] = "-T";
options[current++] = "" + getThreshold();
while (current < options.length) {
options[current++] = "";
}
return options;
}
The same for setOptions(String[] options) method.
You only need to use the accessors.

Related

retrieving only certain properties when building a list of objects

I recently switch from C# to Java and not able to solve this problem. I am automation UI using selenium. I like to build a model for a list of elements on a page, retrieve property and then work with these properties. In the example bellow, I am searching on amazon.com and getting a list of results. I have SearchResultsModel class which represents every item returned, public List<SearchResultModel> GetAllResults(bool title = false;bool isPrime = false;bool price = false) method which retrieves data from UI and place it my model, it has default parameters which allows me to manipulate what data I want to retrieve instead of retrieving everything. Then I invoking by List<SearchResultModel> actual = myPage.GetAllResults(title:true,isPrime:true); in this instance I get a list of SearchResultsModel that each contain only 2 properties - title and isPrime.
In ideal world I should retrieve all the data from the page but it takes lots of time to do so and defeats the whole purpose of automation of being faster then manual testing.
I could use method overload but then I end up with tens or even hundreds methods. In this example I have only 3 property so I will end up having 9 methods, in case of an object with 10 properties, I am afraid to even do a math. I could use varagrs but then building an argument will become a mess.
I am not sure how to solve this problem in Java. Please advise
public class SearchResultsModel
{
//model that represents a single search result item
public string Title{get;set;}
public boolean IsPrime{get;set;}
public float Price {get;set;]
}
//method to retrieve all the search results from UI
public List<SearchResultModel> GetAllResults(bool title = false;bool isPrime = false;bool price = false)
{
List<SearchResultModel> toReturn = new List<SearchResultModel>();
IList<IWebElement> results = driver.FindElements(By.css("my locattors"))
foreach(IWebElement element in results)
{
SearchResultModel result = new SearchResultModel();
result.Title = title? element.FindElement(By.css("some locator")).GetText(): null;
result.IsPrime = isPrime? element.FindElement(By.css("some locator")).Selected(): false;
result.Price = price? element.FindElement(By.css("some locator")).GetText(): null;
toReturn.Add(result);
}
return toReturn;
}
//this is how I can invoke objects only with a specific properties
List<SearchResultModel> actual = myPage.GetAllResults(title:true,isPrime:true);
foreach(SearchResultModel model in actual)
{
Assert.That(model.isPrime == true);
}
I would build a class that represents what options you want to search for. It's basically a parallel to your SearchResultsModel. You pass an instance of that class into the GetAllResults method and then check each property's value to see if it should be pulled or not.
An example of the options class
public class SearchResultsOptions
{
public boolean Title{get;set;}
public boolean IsPrime{get;set;}
public boolean Price {get;set;]
...
}
An adapted version of your GetAllResults method
public List<SearchResultModel> GetAllResults(SearchResultsOptions searchResultsOptions)
{
List<SearchResultModel> toReturn = new List<SearchResultModel>();
IList<IWebElement> results = driver.FindElements(By.css("my locattors"))
foreach(IWebElement element in results)
{
SearchResultModel result = new SearchResultModel();
result.Title = searchResultsOptions.Title? element.FindElement(By.css("some locator")).GetText(): null;
result.IsPrime = searchResultsOptions.IsPrime? element.FindElement(By.css("some locator")).Selected(): false;
result.Price = searchResultsOptions.Price? element.FindElement(By.css("some locator")).GetText(): null;
// add more fields here
toReturn.Add(result);
}
return toReturn;
}
You would have to instantiate the options class and then pass it to your method
SearchResultsOptions searchResultsOptions = new SearchResultsOptions(true, false, true);
List<SearchResultModel> searchResultsModel = GetAllResults(searchResultsOptions);

Flutter - Convert Java routine to flutter

I have an android java pattern that I would like to convert to something similar in flutter. It's pretty straight forward but I am having difficulty finding a clear example. What this involves is having a utility class that does repetitive string manipulation routines that I can use anywhere in the project.
Here is an example. I use the CapEachWord routine in a lot of source members. See the arrow below.
import com.auto.accident.report.util.utils;
#Override
public void onResults(Bundle results) {
ArrayList<String> result = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
switch (REQ_CODE) {
case REQ_CODE_NOTE_SUBJECT: {
DA_RESULT = utils.capEachWord(DA_RESULT); <-------------------
tieAN_SUBJECT.setText(DA_RESULT);
startNoteInput();
break;
}
}
CapEachWord resides in a source member called utils and looks like this.
public class utils {
public static String capEachWord(String DA_RESULT) {
int splitLength;
int index = 0;
String[] words = DA_RESULT.split(" ");
splitLength = words.length;
StringBuilder DA_RESULTBuilder = new StringBuilder();
while (index < splitLength) {
int DA_SIZE = words[index].length();
words[index] = words[index].substring(0,
1).toUpperCase() +
words[index].substring(1,
DA_SIZE);
DA_RESULTBuilder.append(words[index]);
if (index != splitLength) {
DA_RESULTBuilder.append(" ");
}
index++;
}
DA_RESULT = DA_RESULTBuilder.toString();
return DA_RESULT;
}
}
What I need to know is how to properly include the utils, structure the utils member and
and ask for the conversion result. The actual conversion code I can work out myself.
You can include your utils file with import './utils.dart'; (by relative path) or import 'package:your_package_name:path/to/utils.dart'; (for path from lib folder).
In your case I would make just class Utils or something, with static methods that I could use.
Usage would be simple after proper importing to some source file:
//...
myString = Utils.capEachWord(otherString);

What input should I put in method call in blue j?

This is for the assignmet for the course Principle of Software Design on Coursera. When I try to run the void sortbylargestdepth method, I am prompted to put some input in the function call method. What input should I put there to run the method? Is this what suppose to happen or did I make a mistake in writing code? Below I have included the code as well as the screenshot of the problem: Hope to get a reply soon.
Here is the code for the method:
public void sortByLargestDepth(ArrayList<QuakeEntry> in) {
for (int i=0; i< 50; i++) {
int minIdx = getLargestDepth(in,i);
QuakeEntry qi = in.get(i);
QuakeEntry qmin = in.get(minIdx);
in.set(i,qmin);
in.set(minIdx,qi);
}
The method should have the signature changed to:
public void sortByLargestDepth(ArrayList<QuakeEntry> quakeEntries)
As the method is expecting an ArrayList<QuakeEntry (list of QuakeEntry's)
So you need to create a list and pass it in to the method call if one is not already provided.
Because we don't have the code for QuakeEntry, I will assume there is a default no-arg constructor. It would look something like:
ArrayList<QuakeEntry> entries = new ArrayList<>();
QuakeEntry entry1 = new QuakeEntry();
//Init entry 1 here
QuakeEntry entry2 = new QuakeEntry();
//Init entry 2 here (or better yet have a constructor that allows you to init
sortByLargestDepth(entries);

Return value from method java

I have a program in java that I wrote to return a table of values. Later on as the functions of this program grew I found that I would like to access a variable within the method that isn't returned but I am not sure the best way to go about it. I know that you cannot return more than one value but how would I go about accessing this variable without a major overhaul?
here is a simplified version of my code:
public class Reader {
public String[][] fluidigmReader(String cllmp) throws IOException {
//read in a file
while ((inpt = br.readLine()) != null) {
if (!inpt.equals("Calls")) {
continue;
}
break;
}
br.readLine();
inpt = br.readLine();
//set up parse parse parameters and parse
prse = inpt.split(dlmcma, -1);
while ((inpt = br.readLine()) != null) {
buffed.add(inpt);
}
int lncnt = 0;
String tbl[][] = new String[buffed.size()][rssnps.size()];
for (int s = 0; s < buffed.size(); s++) {
prse = buffed.get(s).split(dlmcma);
//turns out I want this smpls ArrayList elsewhere
smpls.add(prse[1]);
//making the table to search through
for (int m = 0; m < prse.length; m++) {
tbl[lncnt][m] = prse[m];
}
lncnt++;
}
//but I return just the tbl here
return tbl;
}
Can anyone recommend a way to use smpls in another class without returning it? Is this perhaps when you use a get/set sort of setup?
Sorry if this seems like an obvious question, I am still new to the world of modular programming
Right now you have this tbl variable. Wrap it in a class and add the list to the class.
class TableWrapper {
// default accessing for illustrative purposes -
// setters and getters are a good idea
String[][] table;
List<String> samples;
TableWrapper(String[][] table, List<String> samples) {
this.table = table;
this.samples = samples;
}
}
Then refactor your method to return the wrapper object.
public TableWrapper fluidigmReader(String cllmp) throws IOException {
// your code here
String tbl[][] = new String[buffed.size()][rssnps.size()];
TableWrapper tw = new TableWrapper(tbl,smpls);
// more of your code
return tw;
}
Then later in your code where you were going
String[][] tbl = fluidigmReader(cllmp);
You instead go
TableWrapper tw = fluidigmReader(cllmp);
String[][] tbl = tw.table;
List<String> smpls = tw.samples;
If you had used a dedicated class for the return value (such as the TableWrapper mentioned in another answer), then you could add additional fields there.
That is the good thing about classes - they can be extended. But you cannot extend String[][] in Java.
You can set a field, instead of a local variable, which you can retrieve later with a getter. You want to avoid it unless it is needed, but in this case it is.
You can use class(Inside Reader class) variable for this. But make sure that it's read/write is synchronized

Asking about threading, arrays and cache memory

I hope in a good manner :-)
I wrote this piece of code.
What I wished to do, is to build something like "cache".
I assumed that I had to watch for different threads, as might many calls get to that class, so I tried the ThreadLocal functionality.
Base pattern is
have "MANY SETS of VECTOR"
The vector holds something like:
VECTOR.FieldName = "X"
VECTOR.FieldValue= "Y"
So many Vector objects in a set. Different set for different calls from different machines, users, objects.
private static CacheVector instance = null;
private static SortedSet<SplittingVector> s = null;
private static TreeSet<SplittingVector> t = null;
private static ThreadLocal<SortedSet<SplittingVector>> setOfVectors = new ThreadLocal<SortedSet<SplittingVector>>();
private static class MyComparator implements Comparator<SplittingVector> {
public int compare(SplittingVector a, SplittingVector b) {
return 1;
}
// No need to override equals.
}
private CacheVector() {
}
public static SortedSet<SplittingVector> getInstance(SplittingVector vector) {
if (instance == null) {
instance = new CacheVector();
//TreeSet<SplittingVector>
t = new TreeSet<SplittingVector>(new MyComparator());
t.add(vector);
s = Collections.synchronizedSortedSet(t);//Sort the set of vectors
CacheVector.assign(s);
} else {
//TreeSet<SplittingVector> t = new TreeSet<SplittingVector>();
t.add(vector);
s = Collections.synchronizedSortedSet(t);//Sort the set of vectors
CacheVector.assign(s);
}
return CacheVector.setOfVectors.get();
}
public SortedSet<SplittingVector> retrieve() throws Exception {
SortedSet<SplittingVector> set = setOfVectors.get();
if (set == null) {
throw new Exception("SET IS EMPTY");
}
return set;
}
private static void assign(SortedSet<SplittingVector> nSet) {
CacheVector.setOfVectors.set(nSet);
}
So... I have it in the attach and I use it like this:
CachedVector cache = CachedVector.getInstance(bufferedline);
The nice part: Bufferedline is a splitted line based on some delimiter from data files. Files can be of any size.
So how do you see this code? Should I be worry ?
I apologise for the size of this message!
Writing correct multi-threaded code is not that easy (i.e. your singleton fails to be), so try to rely on existing solutions if posssible. If you're searching for a thread-safe Cache implementation in Java, check out this LinkedHashMap. You can use it to implement a LRU cache. And collections.synchronizedMap(). can make this thread-safe.

Categories