-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from saalfeldlab/fix-fragment-segment-assignm…
…ent-deserialization Serialize initial lut supplier in paintera project
- Loading branch information
Showing
8 changed files
with
231 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...b/paintera/serialization/assignments/N5FragmentSegmentAssignmentInitialLutSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.janelia.saalfeldlab.paintera.serialization.assignments; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.JsonSerializationContext; | ||
import org.janelia.saalfeldlab.paintera.data.n5.N5Meta; | ||
import org.janelia.saalfeldlab.paintera.data.n5.ReflectionException; | ||
import org.janelia.saalfeldlab.paintera.serialization.PainteraSerialization; | ||
import org.janelia.saalfeldlab.paintera.serialization.SerializationHelpers; | ||
import org.janelia.saalfeldlab.util.n5.N5FragmentSegmentAssignmentInitialLut; | ||
import org.janelia.saalfeldlab.util.n5.N5FragmentSegmentAssignmentPersister; | ||
import org.scijava.plugin.Plugin; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
|
||
|
||
@Plugin(type = PainteraSerialization.PainteraAdapter.class) | ||
public class N5FragmentSegmentAssignmentInitialLutSerializer implements | ||
PainteraSerialization.PainteraAdapter<N5FragmentSegmentAssignmentInitialLut> | ||
{ | ||
|
||
|
||
private static final String N5_META_KEY = "N5"; | ||
|
||
@Override | ||
public N5FragmentSegmentAssignmentInitialLut deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { | ||
try { | ||
final N5Meta meta = SerializationHelpers.deserializeFromClassInfo(jsonElement.getAsJsonObject().get(N5_META_KEY).getAsJsonObject(), context); | ||
return new N5FragmentSegmentAssignmentInitialLut(meta); | ||
} catch (ClassNotFoundException e) { | ||
throw new JsonParseException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public JsonElement serialize(N5FragmentSegmentAssignmentInitialLut src, Type type, JsonSerializationContext context) { | ||
final JsonObject map = new JsonObject(); | ||
map.add(N5_META_KEY, SerializationHelpers.serializeWithClassInfo(src.getMeta(), context)); | ||
return map; | ||
} | ||
|
||
@Override | ||
public Class<N5FragmentSegmentAssignmentInitialLut> getTargetClass() { | ||
return N5FragmentSegmentAssignmentInitialLut.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/org/janelia/saalfeldlab/util/n5/N5FragmentSegmentAssignmentInitialLut.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.janelia.saalfeldlab.util.n5; | ||
|
||
import com.google.gson.annotations.Expose; | ||
import gnu.trove.map.TLongLongMap; | ||
import gnu.trove.map.hash.TLongLongHashMap; | ||
import net.imglib2.Cursor; | ||
import net.imglib2.RandomAccessibleInterval; | ||
import net.imglib2.converter.Converters; | ||
import net.imglib2.type.NativeType; | ||
import net.imglib2.type.numeric.IntegerType; | ||
import net.imglib2.type.numeric.integer.UnsignedLongType; | ||
import net.imglib2.view.Views; | ||
import org.janelia.saalfeldlab.n5.DataType; | ||
import org.janelia.saalfeldlab.n5.N5Reader; | ||
import org.janelia.saalfeldlab.n5.imglib2.N5Utils; | ||
import org.janelia.saalfeldlab.paintera.data.n5.N5Meta; | ||
import org.janelia.saalfeldlab.paintera.data.n5.ReflectionException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.lang.invoke.MethodHandles; | ||
import java.util.function.Supplier; | ||
|
||
public class N5FragmentSegmentAssignmentInitialLut implements Supplier<TLongLongMap> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
@Expose | ||
private final N5Meta meta; | ||
|
||
public N5FragmentSegmentAssignmentInitialLut(final N5Reader container, final String dataset) throws ReflectionException { | ||
this(N5Meta.fromReader(container, dataset)); | ||
} | ||
|
||
public N5FragmentSegmentAssignmentInitialLut(final N5Meta meta) { | ||
this.meta = meta; | ||
} | ||
|
||
public N5Meta getMeta() { | ||
return this.meta; | ||
} | ||
|
||
@Override | ||
public TLongLongMap get() { | ||
try { | ||
RandomAccessibleInterval<UnsignedLongType> data = openDatasetSafe(meta.reader(), meta.dataset()); | ||
final long[] keys = new long[(int) data.dimension(0)]; | ||
final long[] values = new long[keys.length]; | ||
LOG.debug("Found {} assignments", keys.length); | ||
final Cursor<UnsignedLongType> keyCursor = Views.flatIterable(Views.hyperSlice(data, 1, 0L)).cursor(); | ||
final Cursor<UnsignedLongType> valueCursor = Views.flatIterable(Views.hyperSlice(data, 1, 1L)).cursor(); | ||
for (int i = 0; i < keys.length; ++i) { | ||
keys[i] = keyCursor.next().getIntegerLong(); | ||
values[i] = valueCursor.next().getIntegerLong(); | ||
} | ||
return new TLongLongHashMap(keys, values); | ||
} catch (IOException e) { | ||
LOG.debug("Exception while trying to return initial lut from N5", e); | ||
LOG.error("Unable to read initial lut from {} -- returning empty map", meta, e); | ||
return new TLongLongHashMap(); | ||
} | ||
} | ||
|
||
private static RandomAccessibleInterval<UnsignedLongType> openDatasetSafe( | ||
final N5Reader reader, | ||
final String dataset | ||
) throws IOException { | ||
return DataType.UINT64.equals(reader.getDatasetAttributes(dataset).getDataType()) | ||
? N5Utils.open(reader, dataset) | ||
: openAnyIntegerTypeAsUnsignedLongType(reader, dataset); | ||
} | ||
|
||
private static <T extends IntegerType<T> & NativeType<T>> RandomAccessibleInterval<UnsignedLongType> openAnyIntegerTypeAsUnsignedLongType( | ||
final N5Reader reader, | ||
final String dataset | ||
) throws IOException { | ||
return Converters.convert(N5Utils.<T>open(reader, dataset), (s, t) -> t.setInteger(s.getIntegerLong()), new UnsignedLongType()); | ||
} | ||
} |
Oops, something went wrong.