Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sjcross committed Aug 16, 2022
2 parents b44852a + 0d804e3 commit 9670b15
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 97 deletions.
25 changes: 15 additions & 10 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<classpathentry kind="src" path=".apt_generated">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<classpathentry kind="src" output="target/test-classes" path=".apt_generated_tests">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path=".apt_generated">
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path=".apt_generated_tests">
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
"java.configuration.updateBuildConfiguration": "automatic",
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx2G -Xms100m"
}
20 changes: 17 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<artifactId>MIA_DeepLearning</artifactId>
<groupId>io.github.mianalysis</groupId>
<version>1.0.2</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -45,14 +45,28 @@
<scope>${scp}</scope>
</dependency>

<dependency>
<groupId>ome</groupId>
<artifactId>formats-bsd</artifactId>
<version>LATEST</version>
<scope>${scp}</scope>
</dependency>

<dependency>
<groupId>net.imagej</groupId>
<artifactId>ij</artifactId>
<version>LATEST</version>
<scope>${scp}</scope>
</dependency>

<dependency>
<groupId>com.github.mianalysis</groupId>
<artifactId>mia</artifactId>
<version>v1.0.0</version>
<version>v1.1.0</version>
<scope>${scp}</scope>
<!-- <groupId>io.github.mianalysis</groupId>
<artifactId>mia</artifactId>
<version>0.21.3</version>
<version>1.1.0-SNAPSHOT</version>
<scope>${scp}</scope> -->
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import java.util.concurrent.Future;
import java.util.stream.IntStream;

import com.drew.lang.annotations.Nullable;

import org.jetbrains.annotations.Nullable;
import org.scijava.Context;
import org.scijava.Priority;
import org.scijava.command.CommandModule;
Expand All @@ -30,11 +29,10 @@
import io.github.mianalysis.mia.module.Module;
import io.github.mianalysis.mia.module.Modules;
import io.github.mianalysis.mia.module.images.transform.ExtractSubstack;
import io.github.mianalysis.mia.object.Image;
import io.github.mianalysis.mia.object.Obj;
import io.github.mianalysis.mia.object.Objs;
import io.github.mianalysis.mia.object.Status;
import io.github.mianalysis.mia.object.Workspace;
import io.github.mianalysis.mia.object.image.Image;
import io.github.mianalysis.mia.object.parameters.BooleanP;
import io.github.mianalysis.mia.object.parameters.ChoiceP;
import io.github.mianalysis.mia.object.parameters.FilePathP;
Expand All @@ -49,6 +47,7 @@
import io.github.mianalysis.mia.object.refs.collections.ObjMeasurementRefs;
import io.github.mianalysis.mia.object.refs.collections.ParentChildRefs;
import io.github.mianalysis.mia.object.refs.collections.PartnerRefs;
import io.github.mianalysis.mia.object.system.Status;
import io.github.mianalysis.mia.object.units.TemporalUnit;
import io.github.sjcross.sjcommon.exceptions.IntegerOverflowException;
import io.github.sjcross.sjcommon.object.volume.SpatCal;
Expand Down Expand Up @@ -143,38 +142,38 @@ public String getDescription() {
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>.";
}

File getModelFile(@Nullable HashMap<String, Object> paramsCNN) {
switch ((String) parameters.getValue(MODEL_MODE)) {
case ModelModes.FROM_FILE:
return new File((String) parameters.getValue(MODEL_PATH));

case ModelModes.PRE_DEFINED:
StarDist2DModel model = null;
switch ((String) parameters.getValue(MODEL_NAME)) {
case ModelNames.DSB_2018:
model = MODELS.get(MODEL_DSB2018_PAPER);
break;
case ModelNames.VERSATILE_FLUORESCENT_NUCLEI:
model = MODELS.get(MODEL_DSB2018_HEAVY_AUGMENTATION);
break;
case ModelNames.VERSATILE_HE_NUCLEI:
model = MODELS.get(MODEL_HE_HEAVY_AUGMENTATION);
break;
}
File getModelFile(@Nullable HashMap<String, Object> paramsCNN, Workspace workspace) {
switch ((String) parameters.getValue(MODEL_MODE,workspace)) {
case ModelModes.FROM_FILE:
return new File((String) parameters.getValue(MODEL_PATH,workspace));

case ModelModes.PRE_DEFINED:
StarDist2DModel model = null;
switch ((String) parameters.getValue(MODEL_NAME,workspace)) {
case ModelNames.DSB_2018:
model = MODELS.get(MODEL_DSB2018_PAPER);
break;
case ModelNames.VERSATILE_FLUORESCENT_NUCLEI:
model = MODELS.get(MODEL_DSB2018_HEAVY_AUGMENTATION);
break;
case ModelNames.VERSATILE_HE_NUCLEI:
model = MODELS.get(MODEL_HE_HEAVY_AUGMENTATION);
break;
}

if (model.canGetFile()) {
try {
paramsCNN.put("blockMultiple", model.sizeDivBy);
paramsCNN.put("overlap", model.tileOverlap);
return model.getFile();
} catch (IOException e) {
if (model.canGetFile()) {
try {
paramsCNN.put("blockMultiple", model.sizeDivBy);
paramsCNN.put("overlap", model.tileOverlap);
return model.getFile();
} catch (IOException e) {
MIA.log.writeWarning("Can't get StarDist model");
return null;
}
} else {
MIA.log.writeWarning("Can't get StarDist model");
return null;
}
} else {
MIA.log.writeWarning("Can't get StarDist model");
return null;
}
}

return null;
Expand Down Expand Up @@ -209,80 +208,89 @@ Pair<Dataset, Dataset> splitPrediction(final Dataset prediction, final DatasetSe
@Override
protected Status process(Workspace workspace) {
// Getting input image
String inputImageName = parameters.getValue(INPUT_IMAGE);
String inputImageName = parameters.getValue(INPUT_IMAGE,workspace);
String outputObjectsName = parameters.getValue(OUTPUT_OBJECTS,workspace);

Image image = workspace.getImages().get(inputImageName);
ImagePlus ipl = image.getImagePlus();
SpatCal cal = SpatCal.getFromImage(ipl);
int nFrames = ipl.getNFrames();
double frameInterval = ipl.getCalibration().frameInterval;

// Creating output object collection
String outputObjectsName = parameters.getValue(OUTPUT_OBJECTS);
// Creating output object collection
Objs outputObjects = new Objs(outputObjectsName, cal, nFrames, frameInterval, TemporalUnit.getOMEUnit());

ImageJ ij = new ImageJ();
Context context = MIA.ijService.context();
DatasetService datasetService = (DatasetService) MIA.ijService.context().service("net.imagej.DatasetService");

// Initialising parameter sets
HashMap<String, Object> paramsCNN = new HashMap<>();
paramsCNN.put("normalizeInput", parameters.getValue(NORMALISE_INPUT));
paramsCNN.put("percentileBottom", parameters.getValue(PERCENTILE_LOW));
paramsCNN.put("percentileTop", parameters.getValue(PERCENTILE_HIGH));
paramsCNN.put("normalizeInput", parameters.getValue(NORMALISE_INPUT,workspace));
paramsCNN.put("percentileBottom", parameters.getValue(PERCENTILE_LOW,workspace));
paramsCNN.put("percentileTop", parameters.getValue(PERCENTILE_HIGH,workspace));
paramsCNN.put("clip", false);
paramsCNN.put("nTiles", parameters.getValue(NUMBER_OF_TILES));
paramsCNN.put("nTiles", parameters.getValue(NUMBER_OF_TILES,workspace));
paramsCNN.put("blockMultiple", 64);
paramsCNN.put("overlap", 64);
paramsCNN.put("batchSize", 1);
paramsCNN.put("showProgressDialog", false);

File modelFile = getModelFile(paramsCNN);
File modelFile = getModelFile(paramsCNN,workspace);
if (modelFile == null)
return Status.FAIL;
paramsCNN.put("modelFile", modelFile);

HashMap<String, Object> paramsNMS = new HashMap<>();
paramsNMS.put("probThresh", parameters.getValue(PROB_THRESHOLD));
paramsNMS.put("nmsThresh", parameters.getValue(OVERLAP_THRESHOLD));
paramsNMS.put("excludeBoundary", parameters.getValue(BOUNDARY_EXCLUSION));
paramsNMS.put("probThresh", parameters.getValue(PROB_THRESHOLD,workspace));
paramsNMS.put("nmsThresh", parameters.getValue(OVERLAP_THRESHOLD,workspace));
paramsNMS.put("excludeBoundary", parameters.getValue(BOUNDARY_EXCLUSION,workspace));
paramsNMS.put("roiPosition", Opt.ROI_POSITION_STACK);
paramsNMS.put("verbose", false);
paramsNMS.put("outputType", Opt.OUTPUT_POLYGONS);

for (int z = 0; z < ipl.getNSlices(); z++) {
Module.writeProgressStatus((z + 1), ipl.getNSlices(), "slices", "StarDist");

Image subs = ExtractSubstack.extractSubstack(image, "Subs", "1", String.valueOf(z + 1), "1");
ImgPlus img = subs.getImgPlus();
DefaultDataset dataset = new DefaultDataset(context, img);
paramsCNN.put("input", dataset);

try {
Future<CommandModule> futureCNN = ij.command().run(de.csbdresden.csbdeep.commands.GenericNetwork.class,
false, paramsCNN);
Dataset prediction = (Dataset) futureCNN.get().getOutput("output");

Pair<Dataset, Dataset> probAndDist = splitPrediction(prediction, datasetService);
Dataset probDS = probAndDist.getA();
Dataset distDS = probAndDist.getB();
paramsNMS.put("prob", probDS);
paramsNMS.put("dist", distDS);

Future<CommandModule> futureNMS = ij.command().run(de.csbdresden.stardist.StarDist2DNMS.class, false,
paramsNMS);
Candidates polygons = (Candidates) futureNMS.get().getOutput("polygons");
List<Integer> indices = polygons.getWinner();

for (Integer idx : indices) {
PolygonRoi polygon = polygons.getPolygonRoi(idx);
Obj obj = outputObjects.createAndAddNewObject(VolumeType.QUADTREE);
try {
obj.addPointsFromRoi(polygon, z);
} catch (IntegerOverflowException e) {
int count = 0;
int total = ipl.getNFrames()*ipl.getNSlices();
for (int t = 0; t < ipl.getNFrames(); t++) {
for (int z = 0; z < ipl.getNSlices(); z++) {
Image subs = ExtractSubstack.extractSubstack(image, "Subs", "1-end", String.valueOf(z + 1), String.valueOf(t + 1));
ImgPlus img = subs.getImgPlus();
DefaultDataset dataset = new DefaultDataset(context, img);
paramsCNN.put("input", dataset);

try {
Future<CommandModule> futureCNN = ij.command().run(
de.csbdresden.csbdeep.commands.GenericNetwork.class,
false, paramsCNN);
Dataset prediction = (Dataset) futureCNN.get().getOutput("output");

Pair<Dataset, Dataset> probAndDist = splitPrediction(prediction, datasetService);
Dataset probDS = probAndDist.getA();
Dataset distDS = probAndDist.getB();
paramsNMS.put("prob", probDS);
paramsNMS.put("dist", distDS);

Future<CommandModule> futureNMS = ij.command().run(de.csbdresden.stardist.StarDist2DNMS.class,
false,
paramsNMS);
Candidates polygons = (Candidates) futureNMS.get().getOutput("polygons");
List<Integer> indices = polygons.getWinner();

for (Integer idx : indices) {
PolygonRoi polygon = polygons.getPolygonRoi(idx);
Obj obj = outputObjects.createAndAddNewObject(VolumeType.QUADTREE);
try {
obj.addPointsFromRoi(polygon, z);
} catch (IntegerOverflowException e) {
}
obj.setT(t);
}
} catch (InterruptedException | ExecutionException e) {
return Status.FAIL;
}
} catch (InterruptedException | ExecutionException e) {
return Status.FAIL;

Module.writeProgressStatus(++count, total, "slices", "StarDist");

}
}

Expand Down Expand Up @@ -329,13 +337,13 @@ public Parameters updateAndGetParameters() {

returnedParameters.add(parameters.getParameter(MODEL_SEPARATOR));
returnedParameters.add(parameters.getParameter(MODEL_MODE));
switch ((String) parameters.getValue(MODEL_MODE)) {
case ModelModes.FROM_FILE:
returnedParameters.add(parameters.getParameter(MODEL_PATH));
break;
case ModelModes.PRE_DEFINED:
returnedParameters.add(parameters.getParameter(MODEL_NAME));
break;
switch ((String) parameters.getValue(MODEL_MODE,null)) {
case ModelModes.FROM_FILE:
returnedParameters.add(parameters.getParameter(MODEL_PATH));
break;
case ModelModes.PRE_DEFINED:
returnedParameters.add(parameters.getParameter(MODEL_NAME));
break;
}

returnedParameters.add(parameters.getParameter(PREDICTION_SEPARATOR));
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/json/org.scijava.plugin.Plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"class":"io.github.mianalysis.mia_deeplearning.moduledependencies.StarDistDetectionDependency","values":{"priority":-100.0,"type":"io.github.mianalysis.mia.moduledependencies.Dependency","visible":true}}{"class":"io.github.mianalysis.mia_deeplearning.module.StarDistDetection","values":{"priority":-100.0,"type":"io.github.mianalysis.mia.module.Module","visible":true}}

0 comments on commit 9670b15

Please sign in to comment.