Skip to content

Commit

Permalink
Add location of wrapper to call of wrapper scripts as first argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
johann-petrak committed Jul 10, 2016
1 parent a0b6ecd commit 069108b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
2 changes: 1 addition & 1 deletion creole.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- creole.xml for Learning_Framework -->
<CREOLE-DIRECTORY
ID="gate.LearningFramework"
VERSION="3.4-beta"
VERSION="3.4-gamma"
DESCRIPTION="Learning Framework"
HELPURL="https://github.com/GateNLP/gateplugin-LearningFramework/wiki"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class EngineSklearnExternal extends Engine {

ProcessBase process;

// These variables get set from the wrapper-specific config file, java properties or
// environment variables.
private String shellcmd = null;
private String shellparms = null;
private String wrapperhome = null;

/**
* Try to find the script running the sklearn-Wrapper command.
* If apply is true, the executable for application is searched,
Expand Down Expand Up @@ -72,17 +78,20 @@ private File findWrapperCommand(File dataDirectory, boolean apply) {
throw new GateRuntimeException("Could not load yaml file "+sklearnInfoFile,ex);
}
tmp = null;
Map map = null;
if(obj instanceof Map) {
Map map = (Map)obj;
map = (Map)obj;
tmp = (String)map.get("sklearnwrapper.home");
} else {
throw new GateRuntimeException("Info file has strange format: "+sklearnInfoFile.getAbsolutePath());
}
if(tmp == null) {
System.err.println("sklearn.yaml file present but does not contain sklearnwrapper.home setting");
} else {
if(tmp != null) {
homeDir = tmp;
}
// Also get any other settings that may be present:
// shell command
shellcmd = (String)map.get("shellcmd");
shellparms = (String)map.get("shellparms");
}
if(homeDir == null) {
throw new GateRuntimeException("SklearnWrapper home not set, please see https://github.com/GateNLP/gateplugin-LearningFramework/wiki/UsingSklearn");
Expand All @@ -94,6 +103,7 @@ private File findWrapperCommand(File dataDirectory, boolean apply) {
if(!wrapperHome.isDirectory()) {
throw new GateRuntimeException("SklearnWrapper home is not a directory: "+wrapperHome.getAbsolutePath());
}
wrapperhome = wrapperHome.getAbsolutePath();
// Now, depending on the operating system, and on train/apply,
// find the correct script to execute
File commandFile;
Expand Down Expand Up @@ -129,9 +139,18 @@ private File findWrapperCommand(File dataDirectory, boolean apply) {
protected void loadModel(File directory, String parms) {
// Instead of loading a model, this establishes a connection with the
// external sklearn process.

File commandFile = findWrapperCommand(directory, true);
String modelFileName = new File(directory,"sklmodel").getAbsolutePath();
String finalCommand = commandFile.getAbsolutePath()+" "+modelFileName;
String finalCommand = commandFile.getAbsolutePath()+" "+wrapperhome+" "+modelFileName;
// if we have a shell command prepend that, and if we have shell parms too, include them
if(shellcmd != null) {
String tmp = shellcmd;
if(shellparms != null) {
shellcmd += " " + shellparms;
}
finalCommand = shellcmd + " " + finalCommand;
}
System.err.println("Running: "+finalCommand);
// Create a fake Model jsut to make LF_Apply... happy which checks if this is null
model = "ExternalSklearnWrapperModel";
Expand Down Expand Up @@ -173,7 +192,15 @@ public void trainModel(File dataDirectory, String instanceType, String parms) {
Exporter.EXPORTER_MATRIXMARKET2_CLASS, dataDirectory, instanceType, parms);
String dataFileName = dataDirectory.getAbsolutePath()+File.separator;
String modelFileName = new File(dataDirectory, "sklmodel").getAbsolutePath();
String finalCommand = commandFile.getAbsolutePath()+" "+dataFileName+" "+modelFileName+" "+sklearnClass+" "+sklearnParms;
String finalCommand = commandFile.getAbsolutePath()+" "+wrapperhome+" "+dataFileName+" "+modelFileName+" "+sklearnClass+" "+sklearnParms;
// if we have a shell command prepend that, and if we have shell parms too, include them
if(shellcmd != null) {
String tmp = shellcmd;
if(shellparms != null) {
shellcmd += " " + shellparms;
}
finalCommand = shellcmd + " " + finalCommand;
}
System.err.println("Running: "+finalCommand);
// Create a fake Model jsut to make LF_Apply... happy which checks if this is null
model = "ExternalSklearnWrapperModel";
Expand Down Expand Up @@ -311,4 +338,5 @@ protected void loadMalletCorpusRepresentation(File directory) {
corpusRepresentationMallet = CorpusRepresentationMalletTarget.load(directory);
}


}
38 changes: 32 additions & 6 deletions src/gate/plugin/learningframework/engines/EngineWekaExternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class EngineWekaExternal extends Engine {

ProcessBase process;

// These variables get set from the wrapper-specific config file, java properties or
// environment variables.
private String shellcmd = null;
private String shellparms = null;
private String wrapperhome = null;

/**
* Try to find the script running the Weka-Wrapper command.
* If apply is true, the executable for application is searched,
Expand Down Expand Up @@ -72,17 +78,20 @@ private File findWrapperCommand(File dataDirectory, boolean apply) {
throw new GateRuntimeException("Could not load yaml file "+wekaInfoFile,ex);
}
tmp = null;
Map map = null;
if(obj instanceof Map) {
Map map = (Map)obj;
map = (Map)obj;
tmp = (String)map.get("wekawrapper.home");
} else {
throw new GateRuntimeException("Info file has strange format: "+wekaInfoFile.getAbsolutePath());
}
if(tmp == null) {
System.err.println("weka.yaml file present but does not contain wekawrapper.home setting");
} else {
if(tmp != null) {
homeDir = tmp;
}
// Also get any other settings that may be present:
// shell command
shellcmd = (String)map.get("shellcmd");
shellparms = (String)map.get("shellparms");
}
if(homeDir == null) {
throw new GateRuntimeException("WekaWrapper home not set, please see https://github.com/GateNLP/gateplugin-LearningFramework/wiki/UsingWeka");
Expand All @@ -94,6 +103,7 @@ private File findWrapperCommand(File dataDirectory, boolean apply) {
if(!wrapperHome.isDirectory()) {
throw new GateRuntimeException("WekaWrapper home is not a directory: "+wrapperHome.getAbsolutePath());
}
wrapperhome = wrapperHome.getAbsolutePath();
// Now, depending on the operating system, and on train/apply,
// find the correct script to execute
File commandFile;
Expand Down Expand Up @@ -139,7 +149,15 @@ protected void loadModel(File directory, String parms) {
if(!new File(header).exists()) {
throw new GateRuntimeException("File not found: "+header);
}
String finalCommand = commandFile.getAbsolutePath()+" "+modelFileName+" "+header;
String finalCommand = commandFile.getAbsolutePath()+" "+wrapperhome+" "+modelFileName+" "+header;
// if we have a shell command prepend that, and if we have shell parms too, include them
if(shellcmd != null) {
String tmp = shellcmd;
if(shellparms != null) {
shellcmd += " " + shellparms;
}
finalCommand = shellcmd + " " + finalCommand;
}
System.err.println("Running: "+finalCommand);
// Create a fake Model jsut to make LF_Apply... happy which checks if this is null
model = "ExternalWekaWrapperModel";
Expand Down Expand Up @@ -181,7 +199,15 @@ public void trainModel(File dataDirectory, String instanceType, String parms) {
Exporter.EXPORTER_ARFF_CLASS, dataDirectory, instanceType, parms);
String dataFileName = new File(dataDirectory,Globals.dataBasename+".arff").getAbsolutePath();
String modelFileName = new File(dataDirectory, FILENAME_MODEL).getAbsolutePath();
String finalCommand = commandFile.getAbsolutePath()+" "+dataFileName+" "+modelFileName+" "+wekaClass+" "+wekaParms;
String finalCommand = commandFile.getAbsolutePath()+" "+wrapperhome+" "+dataFileName+" "+modelFileName+" "+wekaClass+" "+wekaParms;
// if we have a shell command prepend that, and if we have shell parms too, include them
if(shellcmd != null) {
String tmp = shellcmd;
if(shellparms != null) {
shellcmd += " " + shellparms;
}
finalCommand = shellcmd + " " + finalCommand;
}
System.err.println("Running: "+finalCommand);
// Create a fake Model jsut to make LF_Apply... happy which checks if this is null
model = "ExternalWekaWrapperModel";
Expand Down

0 comments on commit 069108b

Please sign in to comment.