Skip to content

Commit 3f2f158

Browse files
committed
Add first version of BatchProcessor
1 parent 9fdbc9a commit 3f2f158

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed

pom.xml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<parent>
9+
<groupId>org.scijava</groupId>
10+
<artifactId>pom-scijava</artifactId>
11+
<version>16.1.0</version>
12+
<relativePath />
13+
</parent>
14+
15+
<artifactId>batch-processor</artifactId>
16+
<version>0.1.0-SNAPSHOT</version>
17+
18+
<name>Batch Processor</name>
19+
<description>A Batch Processor for SciJava Modules and Scripts</description>
20+
<url>N/A</url>
21+
<inceptionYear>2017</inceptionYear>
22+
<organization>
23+
<name>None</name>
24+
<url>N/A</url>
25+
</organization>
26+
<licenses>
27+
<license>
28+
<name>Simplified BSD License</name>
29+
<distribution>repo</distribution>
30+
</license>
31+
</licenses>
32+
33+
<developers>
34+
<!-- See https://imagej.net/Team -->
35+
<developer>
36+
<id>imagejan</id>
37+
<name>Jan Eglinger</name>
38+
<url>http://imagej.net/User:Eglinger</url>
39+
<roles>
40+
<role>founder</role>
41+
<role>lead</role>
42+
<role>developer</role>
43+
<role>debugger</role>
44+
<role>reviewer</role>
45+
<role>support</role>
46+
<role>maintainer</role>
47+
</roles>
48+
</developer>
49+
</developers>
50+
<contributors>
51+
<contributor>
52+
<name>Curtis Rueden</name>
53+
<url>http://imagej.net/User:Rueden</url>
54+
<roles><role>founder</role></roles>
55+
<properties><id>ctrueden</id></properties>
56+
</contributor>
57+
</contributors>
58+
59+
<mailingLists>
60+
<mailingList>
61+
<name>ImageJ Forum</name>
62+
<archive>http://forum.imagej.net/</archive>
63+
</mailingList>
64+
</mailingLists>
65+
66+
<scm>
67+
<connection>scm:git:git://github.com/imagejan/scijava-batch-processor</connection>
68+
<developerConnection>scm:git:git@github.com:imagejan/scijava-batch-processor</developerConnection>
69+
<tag>HEAD</tag>
70+
<url>https://github.com/imagejan/scijava-batch-processor</url>
71+
</scm>
72+
<issueManagement>
73+
<system>GitHub Issues</system>
74+
<url>https://github.com/imagejan/scijava-batch-processor/issues</url>
75+
</issueManagement>
76+
<ciManagement>
77+
<system>None</system>
78+
</ciManagement>
79+
80+
<properties>
81+
<package-name>org.scijava</package-name>
82+
<main-class>org.scijava.batch.BatchProcessor</main-class>
83+
<license.licenseName>bsd_2</license.licenseName>
84+
<license.copyrightOwners>N/A</license.copyrightOwners>
85+
</properties>
86+
87+
<repositories>
88+
<repository>
89+
<id>imagej.public</id>
90+
<url>http://maven.imagej.net/content/groups/public</url>
91+
</repository>
92+
</repositories>
93+
94+
<dependencies>
95+
<dependency>
96+
<groupId>org.scijava</groupId>
97+
<artifactId>scijava-common</artifactId>
98+
</dependency>
99+
<dependency>
100+
<groupId>net.imagej</groupId>
101+
<artifactId>imagej</artifactId>
102+
</dependency>
103+
</dependencies>
104+
</project>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.scijava.batch;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.Future;
8+
9+
import net.imagej.ImageJ;
10+
11+
import org.scijava.command.Command;
12+
import org.scijava.command.DynamicCommand;
13+
import org.scijava.log.LogService;
14+
import org.scijava.module.Module;
15+
import org.scijava.module.ModuleException;
16+
import org.scijava.module.ModuleInfo;
17+
import org.scijava.module.ModuleItem;
18+
import org.scijava.module.ModuleService;
19+
import org.scijava.module.MutableModuleItem;
20+
import org.scijava.plugin.Parameter;
21+
import org.scijava.plugin.Plugin;
22+
import org.scijava.script.ScriptInfo;
23+
import org.scijava.script.ScriptModule;
24+
import org.scijava.script.ScriptService;
25+
26+
/**
27+
* Batch process a list of files in a folder by running a given script,
28+
* automatically providing the {@link File} input
29+
*
30+
* @author Jan Eglinger
31+
*/
32+
@Plugin(type = Command.class, label = "Batch Processor", initializer = "initScriptChoice", menuPath = "Process>Batch>SciJava Batch Processor")
33+
public class BatchProcessor extends DynamicCommand {
34+
35+
// -- Parameters --
36+
37+
@Parameter
38+
private LogService log;
39+
40+
@Parameter
41+
private ModuleService modules;
42+
43+
@Parameter
44+
private ScriptService scripts;
45+
46+
@Parameter(label = "Input directory", style = "directory")
47+
private File inputFolder;
48+
49+
@Parameter(label = "Script to run")
50+
private String scriptChoice;
51+
52+
// -- Initializer methods --
53+
54+
/**
55+
* Generate a list of all {@link ScriptInfo} objects that take a
56+
* {@link File} input, and populate the parameter choices with their
57+
* identifiers.
58+
*/
59+
protected void initScriptChoice() {
60+
MutableModuleItem<String> input = getInfo().getMutableInput("scriptChoice", String.class);
61+
List<String> choices = new ArrayList<>();
62+
for (ScriptInfo script : scripts.getScripts()) {
63+
try {
64+
if (modules.getSingleInput(script.createModule(), File.class) != null) {
65+
choices.add(script.getIdentifier());
66+
// choices.add(script.getPath());
67+
// TODO create map with readable names and modules
68+
}
69+
} catch (ModuleException exc) {
70+
log.warn("Could not create module for script: ", exc);
71+
}
72+
}
73+
input.setChoices(choices);
74+
}
75+
76+
// -- Main method --
77+
78+
@Override
79+
public void run() {
80+
ModuleInfo moduleInfo = modules.getModuleById(scriptChoice);
81+
ScriptModule module;
82+
try {
83+
module = (ScriptModule) moduleInfo.createModule();
84+
} catch (ModuleException exc) {
85+
log.error("Error during module creation", exc);
86+
return;
87+
}
88+
module.setContext(getContext());
89+
ModuleItem<File> fileInput = modules.getSingleInput(module, File.class);
90+
for (File file : inputFolder.listFiles()) {
91+
fileInput.setValue(module, file);
92+
module.resolveInput(fileInput.getName());
93+
94+
Future<Module> instance = modules.run(module, true);
95+
try {
96+
log.info(instance.get());
97+
} catch (InterruptedException exc) {
98+
log.error("Error: interrupted module execution", exc);
99+
return;
100+
} catch (ExecutionException exc) {
101+
log.error("Error during module execution", exc);
102+
// continue loop
103+
}
104+
}
105+
}
106+
107+
/** Test the batch processor. */
108+
public static void main(final String... args) throws Exception {
109+
// Launch ImageJ as usual.
110+
final ImageJ ij = net.imagej.Main.launch(args);
111+
112+
ij.command().run(BatchProcessor.class, true);
113+
}
114+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!groovy
2+
3+
#@File inputFile
4+
#@String name
5+
#@LogService log
6+
7+
log.info("Hello $name, now processing $inputFile")

0 commit comments

Comments
 (0)