Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added createModel(). Nina asked for it.
  • Loading branch information
egonw committed Aug 11, 2012
1 parent 0d2ba3d commit 53f0dcf
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
Expand All @@ -34,6 +35,7 @@ public abstract class ModelAlgorithm extends Algorithm {

private static final Logger logger = Logger.getLogger(ModelAlgorithm.class);

@SuppressWarnings("serial")
public static String calculate(String service, String model,
String dataSetURI, IProgressMonitor monitor)
throws HttpException, IOException, InterruptedException, GeneralSecurityException {
Expand Down Expand Up @@ -107,6 +109,94 @@ public static String calculate(String service, String model,
return dataset;
}

@SuppressWarnings("serial")
public static String createModel(String algoURI, String datasetURI, List<String> featureURIs,
String predictionFeatureURI, IProgressMonitor monitor)
throws HttpException, IOException, InterruptedException, GeneralSecurityException {
if (monitor == null) monitor = new NullProgressMonitor();
int worked = 0;

HttpClient client = new HttpClient();
PostMethod method = new PostMethod(algoURI);
HttpMethodHelper.addMethodHeaders(method,
new HashMap<String,String>() {{ put("Accept", "text/uri-list"); }}
);
// add the features etc to the URI
datasetURI = datasetURI + "?" + asFeatureURIString(featureURIs) + "&max=100";
logger.debug("create model, datasetURI: " + datasetURI);
method.setParameter("dataset_uri", datasetURI);
method.setParameter("prediction_feature", predictionFeatureURI);
client.executeMethod(method);
int status = method.getStatusCode();
String modelURI = "";
// FIXME: I should really start using the RDF response...
String responseString = method.getResponseBodyAsString();
logger.debug("Status: " + status);
int tailing = 1;
if (status == 200 || status == 202) {
if (responseString.contains("/task/")) {
// OK, we got a task... let's wait until it is done
String task = responseString;
logger.debug("response: " + task);
Thread.sleep(andABit(500)); // let's be friendly, and wait 1 sec
TaskState state = Task.getState(task);
while (!state.isFinished() && !monitor.isCanceled()) {
int onlineWorked = (int)state.getPercentageCompleted();
if (onlineWorked > worked) {
// work done is difference between done before and online done
monitor.worked(onlineWorked - worked);
worked = onlineWorked;
}
// let's be friendly, and wait 2 secs and a bit and increase
// that time after each wait
int waitingTime = andABit(2000*tailing);
logger.debug("Waiting " + waitingTime + "ms.");
waitUnlessInterrupted(waitingTime, monitor);
state = Task.getState(task);
if (state.isRedirected()) {
task = state.getResults();
logger.debug("Got a Task redirect. New task:" + task);
}
// but wait at most 20 secs and a bit
if (tailing < 10) tailing++;
}
if (monitor.isCanceled()) Task.delete(task);
// OK, it should be finished now
modelURI = state.getResults();
} else {
// OK, that was quick!
modelURI = responseString;
logger.debug("No Task, Data set: " + modelURI);
monitor.worked(100);
}
} else if (status == 401) {
throw new GeneralSecurityException("Not authenticated");
} else if (status == 403) {
throw new GeneralSecurityException("Not authorized");
} else if (status == 404) {
logger.debug("Model not found (404): " + responseString);
throw new UnsupportedOperationException("Service not found");
} else {
logger.debug("Model error (" + status + "): " + responseString);
throw new IllegalStateException("Service error: " + status);
}
method.releaseConnection();
modelURI = modelURI.replaceAll("\n", "");
return modelURI;
}

private static String asFeatureURIString(List<String> featureURIs) {
if (featureURIs == null || featureURIs.size() == 0) return "";

StringBuffer buffer = new StringBuffer();
for (int i=0; i<featureURIs.size(); i++) {
String feature = featureURIs.get(i);
buffer.append("feature_uris[]=").append(feature);
if ((i+1)<featureURIs.size()) buffer.append("&"); // was this the last? if not...
}
return buffer.toString();
}

private static void waitUnlessInterrupted(
int waitingTime, IProgressMonitor monitor)
throws InterruptedException {
Expand Down
Expand Up @@ -351,4 +351,15 @@ public String downloadDataSetAsMDLSDfile(String service, String dataSet,
params="String service, String inchi"
)
public List<String> search(String service, String inchi) throws BioclipseException;

@Recorded
@PublishedMethod(
methodSummary="Creates a new regression model using the given algorithm, for the " +
"given data set, with the features as independent variables, and the " +
"prediction features as the dependent feature.",
params="String algoURI, String datasetURI, List<String> featureURIs, String predictionFeatureURI"
)
public String createModel(
String algoURI, String datasetURI, List<String> featureURIs, String predictionFeatureURI)
throws BioclipseException;
}
Expand Up @@ -880,6 +880,24 @@ public List<String> search(String service, String inchi) throws BioclipseExcepti
}
}

public String createModel(
String algoURI, String datasetURI, List<String> featureURIs,
String predictionFeatureURI, IProgressMonitor monitor) throws BioclipseException {
if (monitor == null) monitor = new NullProgressMonitor();
monitor.beginTask("Creating a new model...", 1);

String modelURI;
try {
modelURI = ModelAlgorithm.createModel(
algoURI, datasetURI, featureURIs, predictionFeatureURI, monitor);
return modelURI;
} catch (Exception exception) {
throw new BioclipseException(
"Error while creating a new prediction model: " + exception.getMessage(), exception
);
}
}

private static String normalizeURI(String datasetURI) {
datasetURI = datasetURI.replaceAll("\\n", "");
datasetURI = datasetURI.replaceAll("\\r", "");
Expand Down

0 comments on commit 53f0dcf

Please sign in to comment.