|
| 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 | +} |
0 commit comments