|
| 1 | +package net.preibisch.fusiontask.task; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.List; |
| 5 | +import java.util.concurrent.Callable; |
| 6 | + |
| 7 | +import org.janelia.saalfeldlab.n5.N5FSWriter; |
| 8 | +import org.janelia.saalfeldlab.n5.N5Writer; |
| 9 | +import org.janelia.saalfeldlab.n5.RawCompression; |
| 10 | +import org.janelia.saalfeldlab.n5.imglib2.N5Utils; |
| 11 | + |
| 12 | +import com.google.gson.JsonIOException; |
| 13 | +import com.google.gson.JsonSyntaxException; |
| 14 | + |
| 15 | +import mpicbg.spim.data.SpimDataException; |
| 16 | +import mpicbg.spim.data.sequence.ViewId; |
| 17 | +import net.imglib2.RandomAccessibleInterval; |
| 18 | +import net.imglib2.type.numeric.real.FloatType; |
| 19 | +import net.preibisch.distribution.algorithm.blockmanager.block.BasicBlockInfo; |
| 20 | +import net.preibisch.distribution.algorithm.clustering.kafka.KafkaManager; |
| 21 | +import net.preibisch.distribution.algorithm.clustering.kafka.KafkaProperties; |
| 22 | +import net.preibisch.distribution.algorithm.clustering.scripting.TaskType; |
| 23 | +import net.preibisch.distribution.algorithm.controllers.items.BlocksMetaData; |
| 24 | +import net.preibisch.distribution.algorithm.task.FusionParams; |
| 25 | +import net.preibisch.distribution.io.img.XMLFile; |
| 26 | +import net.preibisch.distribution.io.img.n5.N5File; |
| 27 | +import net.preibisch.distribution.tools.Tools; |
| 28 | +import net.preibisch.mvrecon.fiji.spimdata.boundingbox.BoundingBox; |
| 29 | +import picocli.CommandLine; |
| 30 | +import picocli.CommandLine.Option; |
| 31 | + |
| 32 | +public class FusionV2 implements Callable<Void> { |
| 33 | + @Option(names = { "-t", "--task" }, required = false, description = "The path of the Data") |
| 34 | + private String task; |
| 35 | + |
| 36 | + @Option(names = { "-o", "--output" }, required = false, description = "The path of the Data") |
| 37 | + private String output; |
| 38 | + |
| 39 | + @Option(names = { "-i", "--input" }, required = false, description = "The path of the Data") |
| 40 | + private String input; |
| 41 | + |
| 42 | + @Option(names = { "-m", "--meta" }, required = false, description = "The path of the MetaData file") |
| 43 | + private String metadataPath; |
| 44 | + |
| 45 | + @Option(names = { "-p", "--param" }, required = false, description = "The path of the MetaData file") |
| 46 | + private String paramPath; |
| 47 | + |
| 48 | + @Option(names = { "-v","-view" }, required = false, description = "The id of block") |
| 49 | + private int view; |
| 50 | + @Option(names = { "-id" }, required = false, description = "The id of block") |
| 51 | + private Integer id; |
| 52 | + |
| 53 | + public FusionV2() { |
| 54 | + // TODO Auto-generated constructor stub |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Void call() throws Exception { |
| 59 | + try { |
| 60 | + |
| 61 | + try { |
| 62 | + id = id - 1; |
| 63 | + } catch (Exception e) { |
| 64 | + KafkaManager.error(-1, e.toString()); |
| 65 | + System.out.println("Error id"); |
| 66 | + throw new Exception("Specify id!"); |
| 67 | + } |
| 68 | + TaskType type = TaskType.of(task); |
| 69 | + switch (type) { |
| 70 | + case PREPARE: |
| 71 | + generateN5(input, metadataPath,paramPath, output, id,view); |
| 72 | + return null; |
| 73 | + case PROCESS: |
| 74 | + blockTask(input, metadataPath, paramPath, output, id,view); |
| 75 | + return null; |
| 76 | + |
| 77 | + default: |
| 78 | + KafkaManager.error(id, "Specify task"); |
| 79 | + System.out.println("Error"); |
| 80 | + throw new Exception("Specify task!"); |
| 81 | + } |
| 82 | + } catch (Exception e) { |
| 83 | + KafkaManager.error(id, e.toString()); |
| 84 | + System.out.println("Error"); |
| 85 | + throw new Exception("Specify task!"); |
| 86 | + } |
| 87 | + // MyLogger.log.info("Block " + id + " saved !"); |
| 88 | + } |
| 89 | + |
| 90 | + public static void blockTask(String inputPath, String metadataPath, String paramPath, String outputPath, int id,int view) { |
| 91 | + try { |
| 92 | + KafkaManager.log(id, "Start process"); |
| 93 | + // XMLFile inputFile = XMLFile.XMLFile(inputPath); |
| 94 | + BlocksMetaData md = BlocksMetaData.fromJson(metadataPath); |
| 95 | + FusionParams params = FusionParams.fromJson(paramPath); |
| 96 | + String jobId = md.getJobId(); |
| 97 | + KafkaProperties.setJobId(jobId); |
| 98 | + KafkaManager.log(id, "Got metadata !"); |
| 99 | + BasicBlockInfo binfo = md.getBlocksInfo().get(id); |
| 100 | + KafkaManager.log(id, "Got block info !"); |
| 101 | +// BoundingBox bb = new BoundingBox(Util.long2int(binfo.getMin()), Util.long2int(binfo.getMax())); |
| 102 | + KafkaManager.log(id, "Bounding box created: " + params.getBb().toString()); |
| 103 | + List<ViewId> viewIds = params.getViewIds().get(view); |
| 104 | + KafkaManager.log(id, "Got view ids "); |
| 105 | + |
| 106 | + XMLFile inputFile = XMLFile.XMLFile(inputPath, params.getBb(), params.getDownsampling(), viewIds); |
| 107 | + |
| 108 | + KafkaManager.log(id, "Input loaded. "); |
| 109 | + // XMLFile inputFile = XMLFile.XMLFile(inputPath); |
| 110 | + RandomAccessibleInterval<FloatType> block = inputFile.fuse(params.getBb(),view); |
| 111 | + |
| 112 | + KafkaManager.log(id, "Got block. "); |
| 113 | + N5File outputFile = N5File.open(outputPath); |
| 114 | + outputFile.saveBlock(block, binfo.getGridOffset()); |
| 115 | + KafkaManager.log(id, "Task finished " + id); |
| 116 | + KafkaManager.done(id, "Task finished " + id); |
| 117 | + } catch (SpimDataException | IOException e) { |
| 118 | + KafkaManager.error(id, e.toString()); |
| 119 | + e.printStackTrace(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static void generateN5(String inputPath, String metadataPath,String paramPath, String outputPath, int id,int view) { |
| 124 | + try { |
| 125 | + KafkaManager.log(id, "Start generate n5"); |
| 126 | + BlocksMetaData md = BlocksMetaData.fromJson(metadataPath); |
| 127 | + FusionParams params = FusionParams.fromJson(paramPath); |
| 128 | + BoundingBox bb = new BoundingBox(params.getBb()); |
| 129 | + long[] dims = bb.getDimensions((int)params.getDownsampling()); |
| 130 | + int blockUnit = md.getBlockUnit(); |
| 131 | + N5File outputFile = new N5File(outputPath, dims, blockUnit); |
| 132 | + outputFile.create(); |
| 133 | + KafkaManager.log(id, "N5 Generated"); |
| 134 | + KafkaManager.done(id, "N5 Generated"); |
| 135 | + } catch (JsonSyntaxException | JsonIOException | IOException e) { |
| 136 | + |
| 137 | + KafkaManager.error(id, e.toString()); |
| 138 | + e.printStackTrace(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static void generateN5fromXML(String inputPath, String metadataPath, String outputPath, int id) { |
| 143 | + try { |
| 144 | + System.out.println("Start generating output"); |
| 145 | + XMLFile inputFile = XMLFile.XMLFile(inputPath); |
| 146 | + RandomAccessibleInterval<FloatType> virtual = inputFile.fuse(); |
| 147 | + String dataset = "/volumes/raw"; |
| 148 | + N5Writer writer = new N5FSWriter(outputPath); |
| 149 | + BlocksMetaData md = BlocksMetaData.fromJson(metadataPath); |
| 150 | + // long[] dims = md.getDimensions(); |
| 151 | + int blockUnit = md.getBlockUnit(); |
| 152 | + int[] blocks = Tools.array(blockUnit, virtual.numDimensions()); |
| 153 | + |
| 154 | + N5Utils.save(virtual, writer, dataset, blocks, new RawCompression()); |
| 155 | + System.out.println("Ouptut generated"); |
| 156 | + } catch (SpimDataException | IOException e1) { |
| 157 | + // TODO Auto-generated catch block |
| 158 | + e1.printStackTrace(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + public static void main(String[] args) { |
| 163 | + CommandLine.call(new FusionV2(), args); |
| 164 | + } |
| 165 | +} |
0 commit comments