|
11 | 11 | import java.util.concurrent.Future;
|
12 | 12 | import java.util.stream.IntStream;
|
13 | 13 |
|
14 |
| -import com.drew.lang.annotations.Nullable; |
15 |
| - |
| 14 | +import org.jetbrains.annotations.Nullable; |
16 | 15 | import org.scijava.Context;
|
17 | 16 | import org.scijava.Priority;
|
18 | 17 | import org.scijava.command.CommandModule;
|
|
30 | 29 | import io.github.mianalysis.mia.module.Module;
|
31 | 30 | import io.github.mianalysis.mia.module.Modules;
|
32 | 31 | import io.github.mianalysis.mia.module.images.transform.ExtractSubstack;
|
33 |
| -import io.github.mianalysis.mia.object.Image; |
34 | 32 | import io.github.mianalysis.mia.object.Obj;
|
35 | 33 | import io.github.mianalysis.mia.object.Objs;
|
36 |
| -import io.github.mianalysis.mia.object.Status; |
37 | 34 | import io.github.mianalysis.mia.object.Workspace;
|
| 35 | +import io.github.mianalysis.mia.object.image.Image; |
38 | 36 | import io.github.mianalysis.mia.object.parameters.BooleanP;
|
39 | 37 | import io.github.mianalysis.mia.object.parameters.ChoiceP;
|
40 | 38 | import io.github.mianalysis.mia.object.parameters.FilePathP;
|
|
49 | 47 | import io.github.mianalysis.mia.object.refs.collections.ObjMeasurementRefs;
|
50 | 48 | import io.github.mianalysis.mia.object.refs.collections.ParentChildRefs;
|
51 | 49 | import io.github.mianalysis.mia.object.refs.collections.PartnerRefs;
|
| 50 | +import io.github.mianalysis.mia.object.system.Status; |
52 | 51 | import io.github.mianalysis.mia.object.units.TemporalUnit;
|
53 | 52 | import io.github.sjcross.sjcommon.exceptions.IntegerOverflowException;
|
54 | 53 | import io.github.sjcross.sjcommon.object.volume.SpatCal;
|
@@ -143,38 +142,38 @@ public String getDescription() {
|
143 | 142 | return "Implements the StarDist plugin to detect objects. For more information on StarDist please see <a href=\"https://imagej.net/plugins/stardist\">https://imagej.net/plugins/stardist</a>.";
|
144 | 143 | }
|
145 | 144 |
|
146 |
| - File getModelFile(@Nullable HashMap<String, Object> paramsCNN) { |
147 |
| - switch ((String) parameters.getValue(MODEL_MODE)) { |
148 |
| - case ModelModes.FROM_FILE: |
149 |
| - return new File((String) parameters.getValue(MODEL_PATH)); |
150 |
| - |
151 |
| - case ModelModes.PRE_DEFINED: |
152 |
| - StarDist2DModel model = null; |
153 |
| - switch ((String) parameters.getValue(MODEL_NAME)) { |
154 |
| - case ModelNames.DSB_2018: |
155 |
| - model = MODELS.get(MODEL_DSB2018_PAPER); |
156 |
| - break; |
157 |
| - case ModelNames.VERSATILE_FLUORESCENT_NUCLEI: |
158 |
| - model = MODELS.get(MODEL_DSB2018_HEAVY_AUGMENTATION); |
159 |
| - break; |
160 |
| - case ModelNames.VERSATILE_HE_NUCLEI: |
161 |
| - model = MODELS.get(MODEL_HE_HEAVY_AUGMENTATION); |
162 |
| - break; |
163 |
| - } |
| 145 | + File getModelFile(@Nullable HashMap<String, Object> paramsCNN, Workspace workspace) { |
| 146 | + switch ((String) parameters.getValue(MODEL_MODE,workspace)) { |
| 147 | + case ModelModes.FROM_FILE: |
| 148 | + return new File((String) parameters.getValue(MODEL_PATH,workspace)); |
| 149 | + |
| 150 | + case ModelModes.PRE_DEFINED: |
| 151 | + StarDist2DModel model = null; |
| 152 | + switch ((String) parameters.getValue(MODEL_NAME,workspace)) { |
| 153 | + case ModelNames.DSB_2018: |
| 154 | + model = MODELS.get(MODEL_DSB2018_PAPER); |
| 155 | + break; |
| 156 | + case ModelNames.VERSATILE_FLUORESCENT_NUCLEI: |
| 157 | + model = MODELS.get(MODEL_DSB2018_HEAVY_AUGMENTATION); |
| 158 | + break; |
| 159 | + case ModelNames.VERSATILE_HE_NUCLEI: |
| 160 | + model = MODELS.get(MODEL_HE_HEAVY_AUGMENTATION); |
| 161 | + break; |
| 162 | + } |
164 | 163 |
|
165 |
| - if (model.canGetFile()) { |
166 |
| - try { |
167 |
| - paramsCNN.put("blockMultiple", model.sizeDivBy); |
168 |
| - paramsCNN.put("overlap", model.tileOverlap); |
169 |
| - return model.getFile(); |
170 |
| - } catch (IOException e) { |
| 164 | + if (model.canGetFile()) { |
| 165 | + try { |
| 166 | + paramsCNN.put("blockMultiple", model.sizeDivBy); |
| 167 | + paramsCNN.put("overlap", model.tileOverlap); |
| 168 | + return model.getFile(); |
| 169 | + } catch (IOException e) { |
| 170 | + MIA.log.writeWarning("Can't get StarDist model"); |
| 171 | + return null; |
| 172 | + } |
| 173 | + } else { |
171 | 174 | MIA.log.writeWarning("Can't get StarDist model");
|
172 | 175 | return null;
|
173 | 176 | }
|
174 |
| - } else { |
175 |
| - MIA.log.writeWarning("Can't get StarDist model"); |
176 |
| - return null; |
177 |
| - } |
178 | 177 | }
|
179 | 178 |
|
180 | 179 | return null;
|
@@ -209,80 +208,89 @@ Pair<Dataset, Dataset> splitPrediction(final Dataset prediction, final DatasetSe
|
209 | 208 | @Override
|
210 | 209 | protected Status process(Workspace workspace) {
|
211 | 210 | // Getting input image
|
212 |
| - String inputImageName = parameters.getValue(INPUT_IMAGE); |
| 211 | + String inputImageName = parameters.getValue(INPUT_IMAGE,workspace); |
| 212 | + String outputObjectsName = parameters.getValue(OUTPUT_OBJECTS,workspace); |
| 213 | + |
213 | 214 | Image image = workspace.getImages().get(inputImageName);
|
214 | 215 | ImagePlus ipl = image.getImagePlus();
|
215 | 216 | SpatCal cal = SpatCal.getFromImage(ipl);
|
216 | 217 | int nFrames = ipl.getNFrames();
|
217 | 218 | double frameInterval = ipl.getCalibration().frameInterval;
|
218 | 219 |
|
219 |
| - // Creating output object collection |
220 |
| - String outputObjectsName = parameters.getValue(OUTPUT_OBJECTS); |
| 220 | + // Creating output object collection |
221 | 221 | Objs outputObjects = new Objs(outputObjectsName, cal, nFrames, frameInterval, TemporalUnit.getOMEUnit());
|
222 | 222 |
|
223 | 223 | ImageJ ij = new ImageJ();
|
224 | 224 | Context context = MIA.ijService.context();
|
225 | 225 | DatasetService datasetService = (DatasetService) MIA.ijService.context().service("net.imagej.DatasetService");
|
226 |
| - |
| 226 | + |
227 | 227 | // Initialising parameter sets
|
228 | 228 | HashMap<String, Object> paramsCNN = new HashMap<>();
|
229 |
| - paramsCNN.put("normalizeInput", parameters.getValue(NORMALISE_INPUT)); |
230 |
| - paramsCNN.put("percentileBottom", parameters.getValue(PERCENTILE_LOW)); |
231 |
| - paramsCNN.put("percentileTop", parameters.getValue(PERCENTILE_HIGH)); |
| 229 | + paramsCNN.put("normalizeInput", parameters.getValue(NORMALISE_INPUT,workspace)); |
| 230 | + paramsCNN.put("percentileBottom", parameters.getValue(PERCENTILE_LOW,workspace)); |
| 231 | + paramsCNN.put("percentileTop", parameters.getValue(PERCENTILE_HIGH,workspace)); |
232 | 232 | paramsCNN.put("clip", false);
|
233 |
| - paramsCNN.put("nTiles", parameters.getValue(NUMBER_OF_TILES)); |
| 233 | + paramsCNN.put("nTiles", parameters.getValue(NUMBER_OF_TILES,workspace)); |
234 | 234 | paramsCNN.put("blockMultiple", 64);
|
235 | 235 | paramsCNN.put("overlap", 64);
|
236 | 236 | paramsCNN.put("batchSize", 1);
|
237 | 237 | paramsCNN.put("showProgressDialog", false);
|
238 | 238 |
|
239 |
| - File modelFile = getModelFile(paramsCNN); |
| 239 | + File modelFile = getModelFile(paramsCNN,workspace); |
240 | 240 | if (modelFile == null)
|
241 | 241 | return Status.FAIL;
|
242 | 242 | paramsCNN.put("modelFile", modelFile);
|
243 | 243 |
|
244 | 244 | HashMap<String, Object> paramsNMS = new HashMap<>();
|
245 |
| - paramsNMS.put("probThresh", parameters.getValue(PROB_THRESHOLD)); |
246 |
| - paramsNMS.put("nmsThresh", parameters.getValue(OVERLAP_THRESHOLD)); |
247 |
| - paramsNMS.put("excludeBoundary", parameters.getValue(BOUNDARY_EXCLUSION)); |
| 245 | + paramsNMS.put("probThresh", parameters.getValue(PROB_THRESHOLD,workspace)); |
| 246 | + paramsNMS.put("nmsThresh", parameters.getValue(OVERLAP_THRESHOLD,workspace)); |
| 247 | + paramsNMS.put("excludeBoundary", parameters.getValue(BOUNDARY_EXCLUSION,workspace)); |
248 | 248 | paramsNMS.put("roiPosition", Opt.ROI_POSITION_STACK);
|
249 | 249 | paramsNMS.put("verbose", false);
|
250 | 250 | paramsNMS.put("outputType", Opt.OUTPUT_POLYGONS);
|
251 | 251 |
|
252 |
| - for (int z = 0; z < ipl.getNSlices(); z++) { |
253 |
| - Module.writeProgressStatus((z + 1), ipl.getNSlices(), "slices", "StarDist"); |
254 |
| - |
255 |
| - Image subs = ExtractSubstack.extractSubstack(image, "Subs", "1", String.valueOf(z + 1), "1"); |
256 |
| - ImgPlus img = subs.getImgPlus(); |
257 |
| - DefaultDataset dataset = new DefaultDataset(context, img); |
258 |
| - paramsCNN.put("input", dataset); |
259 |
| - |
260 |
| - try { |
261 |
| - Future<CommandModule> futureCNN = ij.command().run(de.csbdresden.csbdeep.commands.GenericNetwork.class, |
262 |
| - false, paramsCNN); |
263 |
| - Dataset prediction = (Dataset) futureCNN.get().getOutput("output"); |
264 |
| - |
265 |
| - Pair<Dataset, Dataset> probAndDist = splitPrediction(prediction, datasetService); |
266 |
| - Dataset probDS = probAndDist.getA(); |
267 |
| - Dataset distDS = probAndDist.getB(); |
268 |
| - paramsNMS.put("prob", probDS); |
269 |
| - paramsNMS.put("dist", distDS); |
270 |
| - |
271 |
| - Future<CommandModule> futureNMS = ij.command().run(de.csbdresden.stardist.StarDist2DNMS.class, false, |
272 |
| - paramsNMS); |
273 |
| - Candidates polygons = (Candidates) futureNMS.get().getOutput("polygons"); |
274 |
| - List<Integer> indices = polygons.getWinner(); |
275 |
| - |
276 |
| - for (Integer idx : indices) { |
277 |
| - PolygonRoi polygon = polygons.getPolygonRoi(idx); |
278 |
| - Obj obj = outputObjects.createAndAddNewObject(VolumeType.QUADTREE); |
279 |
| - try { |
280 |
| - obj.addPointsFromRoi(polygon, z); |
281 |
| - } catch (IntegerOverflowException e) { |
| 252 | + int count = 0; |
| 253 | + int total = ipl.getNFrames()*ipl.getNSlices(); |
| 254 | + for (int t = 0; t < ipl.getNFrames(); t++) { |
| 255 | + for (int z = 0; z < ipl.getNSlices(); z++) { |
| 256 | + Image subs = ExtractSubstack.extractSubstack(image, "Subs", "1-end", String.valueOf(z + 1), String.valueOf(t + 1)); |
| 257 | + ImgPlus img = subs.getImgPlus(); |
| 258 | + DefaultDataset dataset = new DefaultDataset(context, img); |
| 259 | + paramsCNN.put("input", dataset); |
| 260 | + |
| 261 | + try { |
| 262 | + Future<CommandModule> futureCNN = ij.command().run( |
| 263 | + de.csbdresden.csbdeep.commands.GenericNetwork.class, |
| 264 | + false, paramsCNN); |
| 265 | + Dataset prediction = (Dataset) futureCNN.get().getOutput("output"); |
| 266 | + |
| 267 | + Pair<Dataset, Dataset> probAndDist = splitPrediction(prediction, datasetService); |
| 268 | + Dataset probDS = probAndDist.getA(); |
| 269 | + Dataset distDS = probAndDist.getB(); |
| 270 | + paramsNMS.put("prob", probDS); |
| 271 | + paramsNMS.put("dist", distDS); |
| 272 | + |
| 273 | + Future<CommandModule> futureNMS = ij.command().run(de.csbdresden.stardist.StarDist2DNMS.class, |
| 274 | + false, |
| 275 | + paramsNMS); |
| 276 | + Candidates polygons = (Candidates) futureNMS.get().getOutput("polygons"); |
| 277 | + List<Integer> indices = polygons.getWinner(); |
| 278 | + |
| 279 | + for (Integer idx : indices) { |
| 280 | + PolygonRoi polygon = polygons.getPolygonRoi(idx); |
| 281 | + Obj obj = outputObjects.createAndAddNewObject(VolumeType.QUADTREE); |
| 282 | + try { |
| 283 | + obj.addPointsFromRoi(polygon, z); |
| 284 | + } catch (IntegerOverflowException e) { |
| 285 | + } |
| 286 | + obj.setT(t); |
282 | 287 | }
|
| 288 | + } catch (InterruptedException | ExecutionException e) { |
| 289 | + return Status.FAIL; |
283 | 290 | }
|
284 |
| - } catch (InterruptedException | ExecutionException e) { |
285 |
| - return Status.FAIL; |
| 291 | + |
| 292 | + Module.writeProgressStatus(++count, total, "slices", "StarDist"); |
| 293 | + |
286 | 294 | }
|
287 | 295 | }
|
288 | 296 |
|
@@ -329,13 +337,13 @@ public Parameters updateAndGetParameters() {
|
329 | 337 |
|
330 | 338 | returnedParameters.add(parameters.getParameter(MODEL_SEPARATOR));
|
331 | 339 | returnedParameters.add(parameters.getParameter(MODEL_MODE));
|
332 |
| - switch ((String) parameters.getValue(MODEL_MODE)) { |
333 |
| - case ModelModes.FROM_FILE: |
334 |
| - returnedParameters.add(parameters.getParameter(MODEL_PATH)); |
335 |
| - break; |
336 |
| - case ModelModes.PRE_DEFINED: |
337 |
| - returnedParameters.add(parameters.getParameter(MODEL_NAME)); |
338 |
| - break; |
| 340 | + switch ((String) parameters.getValue(MODEL_MODE,null)) { |
| 341 | + case ModelModes.FROM_FILE: |
| 342 | + returnedParameters.add(parameters.getParameter(MODEL_PATH)); |
| 343 | + break; |
| 344 | + case ModelModes.PRE_DEFINED: |
| 345 | + returnedParameters.add(parameters.getParameter(MODEL_NAME)); |
| 346 | + break; |
339 | 347 | }
|
340 | 348 |
|
341 | 349 | returnedParameters.add(parameters.getParameter(PREDICTION_SEPARATOR));
|
|
0 commit comments