Skip to content

Commit

Permalink
Merge pull request #126 from tischi/write_axis_tags
Browse files Browse the repository at this point in the history
Added axes serialization to json for export
  • Loading branch information
k-dominik authored Jun 6, 2024
2 parents 661310c + 2722a5f commit 93a62c2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/ilastik/ilastik4ij/hdf5/Hdf5.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import static org.ilastik.ilastik4ij.util.ImgUtils.DEFAULT_AXES;
import static org.ilastik.ilastik4ij.util.ImgUtils.argbToMultiChannel;
import static org.ilastik.ilastik4ij.util.ImgUtils.axesOf;
import static org.ilastik.ilastik4ij.util.ImgUtils.axesToJSON;
import static org.ilastik.ilastik4ij.util.ImgUtils.inputBlockDims;
import static org.ilastik.ilastik4ij.util.ImgUtils.outputDims;
import static org.ilastik.ilastik4ij.util.ImgUtils.reversed;
Expand Down Expand Up @@ -286,6 +287,9 @@ public static <T extends NativeType<T>> void writeDataset(
reversed(chunkDims),
compressionLevel)) {

// Add axes tags
writer.string().setAttr(dataset.getDataSetPath(), "axistags", axesToJSON(axes));

callback.accept(0L);

while (gridCursor.hasNext()) {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/ilastik/ilastik4ij/util/ImgUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,41 @@ public static List<AxisType> parseAxes(String json) {
return axes;
}

public static String axesToJSON(List<AxisType> axes)
{
JSONArray jsonAxesArray = new JSONArray();
List<AxisType> axes_for_serialization = new ArrayList<>(axes);
Collections.reverse(axes_for_serialization);
for (AxisType axis : axes_for_serialization)
{
JSONObject jsonAxis = new JSONObject();
String label = axis.getLabel().toLowerCase();
int vigraType;
String vigraKey;
if (axis.isSpatial()){
vigraType = 2;
vigraKey = label;
} else if (label.equals("time")) {
vigraType = 8;
vigraKey = "t";
} else if (label.equals("channel")) {
vigraType = 1;
vigraKey = "c";
} else {
throw new IllegalArgumentException(
String.format("Unknown axis type found with label %s.", label));
}
jsonAxis.put("key", vigraKey);
jsonAxis.put("typeFlags", vigraType);
jsonAxesArray.put(jsonAxis);
}

JSONObject root = new JSONObject();
root.put("axes", jsonAxesArray);

return root.toString();
}

/**
* Treat alpha, red, green, and blue values in {@link ARGBType} image
* as a separate, last channel dimension.
Expand Down

0 comments on commit 93a62c2

Please sign in to comment.