Skip to content

Commit

Permalink
Update to parent pom-indago-2.2.11 because for new labkit version 0.1.14
Browse files Browse the repository at this point in the history
  • Loading branch information
maarzt committed Oct 10, 2018
1 parent 19488e8 commit d607eb3
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 63 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>sc.fiji</groupId>
<artifactId>pom-indago</artifactId>
<version>2.2.10</version>
<version>2.2.11</version>
</parent>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/indago/tr2d/plugins/seg/LabkitPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public boolean isUsable() {
public List<RandomAccessibleInterval<IntType>> getOutputs() {
if(!isUsable()) return Collections.emptyList();
List< RandomAccessibleInterval< IntType > > segmentations =
segmentation.getSegmentations();
segmentationModel.getSegmentations();
saveSettings();
return segmentations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public MySegmentationItem(SegmentationModel model, Segmenter segmenter) {
}

private static Labeling defaultLabeling(SegmentationModel model) {
return new Labeling(Arrays.asList("background", "foreground"), model
.image());
return Labeling.createEmpty(Arrays.asList("background", "foreground"), model.image());
}

private void resetProbability() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,23 @@ public class PredictionLayer implements BdvLayer {
private final RandomAccessibleContainer<VolatileARGBType> segmentationContainer;
private final SharedQueue queue = new SharedQueue(Runtime.getRuntime()
.availableProcessors());
private final Holder< Boolean > visibility;
private Notifier<Runnable> listeners = new Notifier<>();
private Notifier<Runnable> makeVisible = new Notifier<>();
private RandomAccessibleInterval<? extends NumericType<?>> view;
private AffineTransform3D transformation;
private Set<MySegmentationItem> alreadyRegistered = Collections.newSetFromMap(
new WeakHashMap<>());

public PredictionLayer(Holder<? extends MySegmentationItem> model) {
public PredictionLayer(Holder< ? extends MySegmentationItem > model,
Holder< Boolean > visibility) {
this.model = model;
SegmentationResultsModel selected = model.get().results();
this.segmentationContainer = new RandomAccessibleContainer<>(
getEmptyPrediction(selected));
this.transformation = selected.transformation();
this.view = Views.interval(segmentationContainer, selected.interval());
this.visibility = visibility;
classifierChanged();
model.notifier().add(ignore -> classifierChanged());
registerListener(model.get());
Expand Down Expand Up @@ -147,7 +150,7 @@ public String title() {
}

@Override
public Notifier< Runnable > makeVisible() {
return makeVisible;
public Holder< Boolean > visibility() {
return visibility;
}
}
Original file line number Diff line number Diff line change
@@ -1,68 +1,57 @@

package com.indago.tr2d.plugins.seg;

import net.imglib2.RandomAccessibleInterval;
import net.imglib2.labkit.DefaultExtensible;
import net.imglib2.labkit.Extensible;
import net.imglib2.labkit.BasicLabelingComponent;
import net.imglib2.labkit.actions.SelectClassifier;
import net.imglib2.labkit.DefaultExtensible;
import net.imglib2.labkit.actions.ClassifierSettingsAction;
import net.imglib2.labkit.actions.LabelEditAction;
import net.imglib2.labkit.models.ColoredLabelsModel;
import net.imglib2.labkit.panel.GuiUtils;
import net.imglib2.labkit.panel.ImageInfoPanel;
import net.imglib2.labkit.panel.LabelPanel;
import net.imglib2.labkit.panel.SegmenterPanel;
import net.imglib2.labkit.segmentation.TrainClassifier;
import net.imglib2.type.numeric.NumericType;
import net.imglib2.type.numeric.integer.IntType;
import net.miginfocom.swing.MigLayout;
import org.scijava.Context;

import javax.swing.*;
import java.util.List;

public class SegmentationComponent implements AutoCloseable {

private final JSplitPane panel;

private final JFrame dialogBoxOwner = null;

private final BasicLabelingComponent labelingComponent;

private final Context context;

private final SegmentationModel segmentationModel;

private final DefaultExtensible extensible;

public SegmentationComponent(SegmentationModel segmentationModel)
{
this.segmentationModel = segmentationModel;
this.context = segmentationModel.getContext();
JFrame dialogBoxOwner = null;
this.extensible = new DefaultExtensible(segmentationModel.getContext(),
dialogBoxOwner);
labelingComponent = new BasicLabelingComponent(dialogBoxOwner,
segmentationModel.imageLabelingModel());
labelingComponent.addBdvLayer(new PredictionLayer(segmentationModel
.selectedSegmenter()));
.selectedSegmenter(), segmentationModel.segmentationVisibility()));
initActions();
JPanel leftPanel = initLeftPanel();
this.panel = initPanel(leftPanel, labelingComponent.getComponent());
}

private void initActions() {
Extensible extensible = new DefaultExtensible(context, dialogBoxOwner,
labelingComponent);
new TrainClassifier(extensible, segmentationModel);
new SelectClassifier(extensible, segmentationModel.selectedSegmenter());
new LabelEditAction(extensible, true, new ColoredLabelsModel(segmentationModel.imageLabelingModel()));
new ClassifierSettingsAction(extensible, segmentationModel.selectedSegmenter());
labelingComponent.addShortcuts(extensible.getShortCuts());
}

private JPanel initLeftPanel() {
JPanel panel = new JPanel();
panel.setLayout(new MigLayout("", "[grow]", "[][grow][grow][]"));
ActionMap actions = getActions();
panel.add(GuiUtils.createCheckboxGroupedPanel(actions.get("Image"), GuiUtils
.createDimensionsInfo(segmentationModel.image())), "grow, wrap");
panel.add(GuiUtils.createCheckboxGroupedPanel(actions.get("Labeling"),
new LabelPanel(dialogBoxOwner, new ColoredLabelsModel(segmentationModel
.imageLabelingModel()), true).getComponent()), "grow, wrap");
panel.add(GuiUtils.createCheckboxGroupedPanel(actions.get("Segmentation"),
new SegmenterPanel(segmentationModel).getComponent()),
"grow, wrap");
panel.add(ImageInfoPanel.newFramedImageInfoPanel(segmentationModel.imageLabelingModel()), "grow, wrap");
panel.add(LabelPanel.newFramedLabelPanel(segmentationModel.imageLabelingModel(), extensible, true), "grow, wrap");
panel.add(SegmenterPanel.newFramedSegmeterPanel(segmentationModel, extensible), "grow, wrap");
panel.add(new ThresholdButton(segmentationModel).getComponent(), "grow");
return panel;
}
Expand All @@ -80,15 +69,6 @@ public JComponent getComponent() {
return panel;
}

private ActionMap getActions() {
return labelingComponent.getActions();
}

public List<RandomAccessibleInterval<IntType >> getSegmentations()
{
return segmentationModel.getSegmentations();
}

@Override
public void close() {
labelingComponent.close();
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/indago/tr2d/plugins/seg/SegmentationModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.indago.io.ProjectFolder;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.cell.CellGrid;
import net.imglib2.labkit.color.ColorMap;
import net.imglib2.labkit.inputimage.DefaultInputImage;
import net.imglib2.labkit.inputimage.InputImage;
import net.imglib2.labkit.labeling.Labeling;
Expand Down Expand Up @@ -41,6 +40,7 @@ public class SegmentationModel implements
private List<MySegmentationItem> segmenters = new ArrayList<>();
private final RandomAccessibleInterval<? extends NumericType<?>> compatibleImage;
private final CellGrid grid;
private final Holder<Boolean> segmentationVisibility = new DefaultHolder<>(true);

private Context context;

Expand Down Expand Up @@ -97,11 +97,6 @@ public Holder<MySegmentationItem> selectedSegmenter() {
return selectedSegmenter;
}

@Override
public ColorMap colorMap() {
return imageLabelingModel.colorMapProvider().colorMap();
}

@Override
public AffineTransform3D labelTransformation() {
return imageLabelingModel.labelTransformation();
Expand Down Expand Up @@ -210,4 +205,7 @@ public void save(ProjectFolder folder) throws IOException {
}
}

public Holder<Boolean> segmentationVisibility() {
return segmentationVisibility;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static void main(String... args) {
ArrayImgs.unsignedBytes(100, 100, 100),
new Context());
JFrame frame = new JFrame();
frame.add(new SegmenterPanel(segmentationModel)
frame.add(new SegmenterPanel(segmentationModel, ignore -> new JPopupMenu())
.getComponent());
frame.add(new ThresholdButton(segmentationModel).getComponent(),
BorderLayout.PAGE_END);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class SegmentationComponentDemo {

private final SegmentationComponent segmenter;
private final SegmentationModel model;

public static void main(String... args) {
new SegmentationComponentDemo();
Expand All @@ -28,8 +29,7 @@ private SegmentationComponentDemo() {
Img<? extends NumericType<?>> image = ImageJFunctions.wrap(new ImagePlus(
"/home/arzt/Documents/Notes/Tr2d/ProjectFiles/raw.tif"));
Context context = new Context();
SegmentationModel model =
new SegmentationModel(image, context);
model = new SegmentationModel(image, context);
segmenter = new SegmentationComponent(model);
frame.add(segmenter.getComponent());
frame.add(getBottomPanel(), BorderLayout.PAGE_END);
Expand All @@ -46,7 +46,7 @@ private JPanel getBottomPanel() {
}

private void showSegmentation() {
for (RandomAccessibleInterval<IntType> segmentation : segmenter
for (RandomAccessibleInterval<IntType> segmentation : model
.getSegmentations())
{
Views.iterable(segmentation).forEach(x -> x.mul(50));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.imglib2.img.array.ArrayImg;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.img.basictypeaccess.array.ByteArray;
import net.imglib2.labkit.labeling.Label;
import net.imglib2.labkit.labeling.Labeling;
import net.imglib2.type.numeric.integer.ByteType;
import net.imglib2.type.numeric.integer.IntType;
import org.junit.Test;
Expand All @@ -18,6 +20,8 @@
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
Expand All @@ -35,13 +39,18 @@ public void testSerializeSingleLabeling() throws IOException {
// setup
SegmentationModel model = new SegmentationModel(image, context);
ProjectFolder projectFolder = tmpProjectFolder();
getPixel(model.labeling(), 0,0,0).add("foreground");
addPixel(model.labeling(), "foreground", 0, 0, 0);
// process
model.save(projectFolder);
SegmentationModel model2 = SegmentationModel
.open(image, context, projectFolder);
// test
assertEquals(Collections.singleton("foreground"), getPixel(model2.labeling(), 0, 0, 0));
assertEquals(Collections.singleton("foreground"), getPixelNames(model2.labeling(), 0, 0, 0));
}

private Set<String> getPixelNames(Labeling labeling, long... position) {
return getPixel(labeling, position).stream().map(Label::name).collect(
Collectors.toSet());
}

@Test
Expand All @@ -64,14 +73,14 @@ public void testSerializeSingleLabeling2() throws IOException {
assertSame(model.labeling(), model.segmenters().get(0).labeling());
ProjectFolder projectFolder = tmpProjectFolder();
model.addSegmenter();
getPixel(model.segmenters().get(0).labeling(), 0,0,0).add("foreground");
addPixel(model.segmenters().get(0).labeling(), "foreground", 0, 0, 0);
// process
model.save(projectFolder);
SegmentationModel model2 = SegmentationModel
.open(image, context, projectFolder);
// test
assertSame(model2.labeling(), model2.segmenters().get(0).labeling());
assertEquals(Collections.singleton("foreground"), getPixel(model2.segmenters().get(0).labeling(), 0, 0, 0));
assertEquals(Collections.singleton("foreground"), getPixelNames(model2.segmenters().get(0).labeling(), 0, 0, 0));
}

@Test
Expand All @@ -81,15 +90,20 @@ public void testSerializeMultipleLabelings() throws IOException {
ProjectFolder projectFolder = tmpProjectFolder();
model.addSegmenter();
List< MySegmentationItem > segmenters = model.segmenters();
getPixel(segmenters.get(0).labeling(), 0, 0, 0).add("foreground");
getPixel(segmenters.get(1).labeling(), 0, 0, 0).add("background");
addPixel(segmenters.get(0).labeling(), "foreground", 0, 0, 0);
addPixel(segmenters.get(1).labeling(), "background", 0, 0, 0);
// process
model.save(projectFolder);
SegmentationModel model2 = SegmentationModel
.open(image, context, projectFolder);
// test
assertEquals(Collections.singleton("foreground"), getPixel(model2.segmenters().get(0).labeling(), 0, 0, 0));
assertEquals(Collections.singleton("background"), getPixel(model2.segmenters().get(1).labeling(), 0, 0, 0));
assertEquals(Collections.singleton("foreground"), getPixelNames(model2.segmenters().get(0).labeling(), 0, 0, 0));
assertEquals(Collections.singleton("background"), getPixelNames(model2.segmenters().get(1).labeling(), 0, 0, 0));
}

private void addPixel(Labeling labeling, String foreground, long... position)
{
getPixel(labeling, position).add(labeling.getLabel(foreground));
}

@Test
Expand Down Expand Up @@ -139,8 +153,9 @@ public void testSerialize() throws IOException {
}

private void train(MySegmentationItem item) {
getPixel(item.labeling(),0,0,0).add("foreground");
getPixel(item.labeling(),1,1,1).add("background");
final Labeling labeling = item.labeling();
addPixel(labeling, "foreground", 0, 0, 0);
addPixel(labeling, "background", 1, 1, 1);
item.train();
}

Expand Down

0 comments on commit d607eb3

Please sign in to comment.