Skip to content

Commit

Permalink
Adapt API changes introduced by imglib/imglib2-roi#71
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed Apr 5, 2024
1 parent 8d43cc5 commit b4c78f8
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void addLabel(Labeling labeling, Label label,
.stream().map(Label::name).collect(Collectors.toList()));
if (newLabelName == null) return;
Label newLabel = labeling.addLabel(newLabelName);
Cursor<Void> cursor = region.cursor();
Cursor<Void> cursor = region.inside().cursor();
RandomAccess<LabelingType<Label>> ra = labeling.randomAccess();
while (cursor.hasNext()) {
cursor.fwd();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sc/fiji/labkit/ui/labeling/Labeling.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static Labeling fromMap(Map<String, IterableRegion<BitType>> regions) {
new SparseRandomAccessIntType(interval));
RandomAccess<LabelingType<Label>> ra = imgLabeling.randomAccess();
regions.forEach((label, region) -> {
Cursor<Void> cursor = region.cursor();
Cursor<Void> cursor = region.inside().cursor();
while (cursor.hasNext()) {
cursor.fwd();
ra.setPosition(cursor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private JsonElement regionToJson(Gson gson,
IterableRegion<BitType> region)
{
JsonArray result = new JsonArray();
Cursor<Void> cursor = region.cursor();
Cursor<Void> cursor = region.inside().cursor();
long[] coords = new long[cursor.numDimensions()];
while (cursor.hasNext()) {
cursor.fwd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void localizeLabel(final Label label) {

private static Interval getBoundingBox(IterableRegion<BitType> region) {
int numDimensions = region.numDimensions();
Cursor<?> cursor = region.cursor();
Cursor<?> cursor = region.inside().cursor();
if (!cursor.hasNext()) return null;
long[] min = new long[numDimensions];
long[] max = new long[numDimensions];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static void addAction(Extensible extensible, LabelingModel model) {

static List<Long> connectedComponetsSizes(IterableRegion<BitType> region) {
List<Long> sizes = new ArrayList<>();
Cursor<Void> cursor = region.cursor();
Cursor<Void> cursor = region.inside().cursor();
SparseRandomAccessIntType visitedImage = new SparseRandomAccessIntType(
region);
RandomAccess<IntType> visited = visitedImage.randomAccess();
Expand All @@ -124,12 +124,12 @@ static List<Long> connectedComponetsSizes(IterableRegion<BitType> region) {
visited.setPosition(cursor);
if (visited.get().get() == 0) {
currentIndex++;
long countBefore = visitedImage.sparsityPattern().size();
long countBefore = visitedImage.sparsityPattern().inside().size();
Filter<Pair<BitType, IntType>, Pair<BitType, IntType>> filter = (
current, seed) -> current.getA().get() && current.getB().get() == 0;
FloodFill.fill(region, visitedImage, cursor, new IntType(currentIndex),
new DiamondShape(1), filter);
long countAfter = visitedImage.sparsityPattern().size();
long countAfter = visitedImage.sparsityPattern().inside().size();
sizes.add(countAfter - countBefore);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private void trainFrame(Training training, List<String> classes, Labeling labeli
ImgPlus<?> image, FeatureCalculator featuresCalculator)
{
SparseRandomAccessIntType classIndices = getClassIndices(labeling, classes);
if (classIndices.sparsityPattern().size() == 0)
if (classIndices.sparsityPattern().inside().size() == 0)
return;
DiskCachedCellImg<FloatType, ?> cachedFeatureBlock = cachedFeatureBlock(featuresCalculator,
image);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.imglib2.AbstractWrappedInterval;
import net.imglib2.Cursor;
import net.imglib2.Interval;
import net.imglib2.IterableInterval;
import net.imglib2.Localizable;
import net.imglib2.Point;
import net.imglib2.RandomAccess;
Expand All @@ -56,6 +57,8 @@ public class SparseIterableRegion extends AbstractWrappedInterval<Interval>

final private IntervalIndexer2 indexer;

private final InsideIterable inside;

public SparseIterableRegion(Interval interval) {
this(interval, new TLongHashSet());
}
Expand All @@ -64,6 +67,7 @@ public SparseIterableRegion(Interval interval, TLongSet positions) {
super(interval);
this.codes = positions;
this.indexer = new IntervalIndexer2(interval);
this.inside = new InsideIterable();
}

public void add(Localizable position) {
Expand All @@ -79,43 +83,58 @@ private boolean contains(Localizable position) {
}

@Override
public Cursor<Void> cursor() {
return new SparseRoiCursor();
public RandomAccess<BitType> randomAccess() {
return new SparseRoiRandomAccess();
}

@Override
public Cursor<Void> localizingCursor() {
return cursor();
public RandomAccess<BitType> randomAccess(Interval interval) {
return randomAccess();
}

@Override
public long size() {
return codes.size();
public IterableInterval< Void > inside()
{
return inside;
}

@Override
public Void firstElement() {
return null;
}
private class InsideIterable extends AbstractWrappedInterval< Interval > implements IterableInterval< Void >
{
InsideIterable()
{
super( SparseIterableRegion.this );
}

@Override
public Object iterationOrder() {
return null;
}
@Override
public Cursor<Void> cursor() {
return new SparseRoiCursor();
}

@Override
public Iterator<Void> iterator() {
return cursor();
}
@Override
public Cursor<Void> localizingCursor() {
return cursor();
}

@Override
public RandomAccess<BitType> randomAccess() {
return new SparseRoiRandomAccess();
@Override
public long size() {
return codes.size();
}

@Override
public Void firstElement() {
return null;
}

@Override
public Object iterationOrder() {
return this;
}
}

@Override
public RandomAccess<BitType> randomAccess(Interval interval) {
return randomAccess();
public BitType getType()
{
return new BitType();
}

private class SparseRoiCursor extends AbstractCursor<Void> implements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public RandomAccess<IntType> randomAccess(Interval interval) {
}

public Cursor<IntType> sparseCursor() {
return new MappingCursor<>(sparsityPattern().cursor(), randomAccess());
return new MappingCursor<>(sparsityPattern().inside().cursor(), randomAccess());
}

public IterableRegion<? extends BooleanType<?>> sparsityPattern() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/demo/custom_segmenter/MeanCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MeanCalculator {
public void addSample(RandomAccessibleInterval<?> image,
IterableRegion<BitType> region)
{
Cursor<Void> cursor = region.cursor();
Cursor<Void> cursor = region.inside().cursor();
RandomAccess<RealType<?>> randomAccess = Cast.unchecked(image.randomAccess());
while (cursor.hasNext()) {
cursor.fwd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testSize() {
RandomAccess<BitType> ra = roi.randomAccess();
ra.setPosition(positionA);
ra.get().set(true);
assertEquals(1, roi.size());
assertEquals(1, roi.inside().size());
}

@Test
Expand All @@ -78,7 +78,7 @@ public void testCursor() {
RandomAccess<BitType> ra = roi.randomAccess();
ra.setPosition(positionA);
ra.get().set(true);
Cursor<Void> cursor = roi.cursor();
Cursor<Void> cursor = roi.inside().cursor();
assertTrue(cursor.hasNext());
cursor.fwd();
assertEquals(positionA[1], cursor.getLongPosition(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public void testNoEntryValue() {
noEntryValue);
Views.iterable(image).forEach(x -> x.setInteger(noEntryValue));
// test
assertFalse(image.sparsityPattern().cursor().hasNext());
assertFalse(image.sparsityPattern().inside().cursor().hasNext());
}
}

0 comments on commit b4c78f8

Please sign in to comment.