Skip to content

Commit

Permalink
Make filters (de-)serialize to the correct thing
Browse files Browse the repository at this point in the history
  • Loading branch information
minnerbe committed Oct 19, 2023
1 parent edccdaa commit 9dc961c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 35 deletions.
40 changes: 24 additions & 16 deletions src/main/java/org/janelia/saalfeldlab/n5/zarr/DType.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
*/
package org.janelia.saalfeldlab.n5.zarr;

import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;

import org.janelia.saalfeldlab.n5.ByteArrayDataBlock;
import org.janelia.saalfeldlab.n5.DataBlock;
Expand All @@ -45,13 +43,7 @@
import org.janelia.saalfeldlab.n5.LongArrayDataBlock;
import org.janelia.saalfeldlab.n5.ShortArrayDataBlock;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import static org.janelia.saalfeldlab.n5.zarr.Filter.VLEN_UTF8;

/**
* Enumerates available zarr data types as defined at
Expand Down Expand Up @@ -219,8 +211,15 @@ public DType(final String typestr, final Collection<Filter> filters) {
case OBJECT:
nBytes = 1;
nBits = 0;
dataBlockFactory = null;
byteBlockFactory = null;
if (filters.contains(VLEN_UTF8)) {
dataBlockFactory = (blockSize, gridPosition, numElements) ->
new ZarrCompatibleStringDataBlock(blockSize, gridPosition, new String[0]);
byteBlockFactory = (blockSize, gridPosition, numElements) ->
new ByteArrayDataBlock(blockSize, gridPosition, new byte[numElements * nBytes]);
} else {
dataBlockFactory = null;
byteBlockFactory = null;
}
break;
// case BOOLEAN:
// case OTHER: // not sure about this
Expand All @@ -237,7 +236,7 @@ public DType(final String typestr, final Collection<Filter> filters) {
new ByteArrayDataBlock(blockSize, gridPosition, new byte[numElements * nBytes]);
}

dataType = getDataType(primitive, nBytes);
dataType = getDataType(primitive, nBytes, filters);
}

public DType(final DataType dataType, final int nPrimitives) {
Expand Down Expand Up @@ -279,6 +278,11 @@ public DType(final DataType dataType, final int nPrimitives) {
break;
// case INT8:
// case UINT8:
case STRING:
nBytes = 1;
dataBlockFactory = (blockSize, gridPosition, numElements) ->
new ZarrCompatibleStringDataBlock(blockSize, gridPosition, new String[0]);
break;
default:
nBytes = nPrimitives;
dataBlockFactory = (blockSize, gridPosition, numElements) ->
Expand All @@ -300,7 +304,8 @@ public DataType getDataType() {

protected final static DataType getDataType(
final Primitive primitive,
final int nBytes) {
final int nBytes,
final Collection<Filter> filters) {

switch (primitive) {
case INT:
Expand Down Expand Up @@ -344,7 +349,10 @@ protected final static DataType getDataType(
return DataType.UINT8; // fallback
}
case OBJECT:
return DataType.OBJECT;
if (filters.contains(VLEN_UTF8))
return DataType.STRING;
else
return DataType.OBJECT;
default:
return DataType.UINT8; // fallback
}
Expand All @@ -362,10 +370,10 @@ public String toString() {
*
* @return list of filters
*/
public List<Filter> getFilters() {
public Collection<Filter> getFilters() {
if (dataType == DataType.STRING) {
ArrayList<Filter> filterSet = new ArrayList<>();
filterSet.add(new ZarrCompatibleStringDataBlock.VLenStringFilter());
filterSet.add(VLEN_UTF8);
return filterSet;
}
else
Expand Down
56 changes: 43 additions & 13 deletions src/main/java/org/janelia/saalfeldlab/n5/zarr/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,69 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

/**
* Place holder interface for filters
* Filter types
*
* TODO implement some
*
* @author Stephan Saalfeld &lt;[email protected]&gt;
* @author Michael Innerberger
*/
public interface Filter {
public enum Filter {
// Note: the JSON (de-)serializer below is very much tailored to this filter, which serializes to "{"id":"vlen-utf8"}"
// If additional filters are implemented, consider also changing the type adapter below
VLEN_UTF8("vlen-utf8");

public static JsonAdapter jsonAdapter = new Filter.JsonAdapter();
private final String id;

class JsonAdapter implements JsonDeserializer<Filter> {
Filter(final String id) {
this.id = id;
}

public String getId() {
return id;
}

public static Filter fromString(final String id) {
for (final Filter filter : values())
if (filter.getId().equals(id))
return filter;
return null;
}

public static final JsonAdapter jsonAdapter = new JsonAdapter();

public static class JsonAdapter implements JsonDeserializer<Filter>, JsonSerializer<Filter> {

@Override
public Filter deserialize(
final JsonElement json,
final Type typeOfT,
final JsonDeserializationContext context) throws JsonParseException {

final JsonObject jsonObject = json.getAsJsonObject();
final JsonElement jsonId = jsonObject.get("id");
final JsonElement jsonId = json.getAsJsonObject().get("id");
if (jsonId == null)
return null;
final String id = jsonId.getAsString();
switch (id) {
case "vlen-utf8":
return new ZarrCompatibleStringDataBlock.VLenStringFilter();
default:
return null;
}

final String stringId = jsonId.getAsString();
return Filter.fromString(stringId);
}

@Override
public JsonElement serialize(
final Filter filter,
final Type typeOfSrc,
final JsonSerializationContext context) {

final JsonObject serialization = new JsonObject();
serialization.add("id", new JsonPrimitive(filter.getId()));
return serialization;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,4 @@ protected String[] deserialize(byte[] rawBytes) {

return strings;
}

public static class VLenStringFilter implements Filter {
public final String id = "vlen-utf8";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,18 @@ protected ZArrayAttributes createZArrayAttributes(final DatasetAttributes datase
reorder(shape);
final int[] chunks = datasetAttributes.getBlockSize().clone();
reorder(chunks);
final DType dType = new DType(datasetAttributes.getDataType());

final ZArrayAttributes zArrayAttributes = new ZArrayAttributes(
N5ZarrReader.VERSION.getMajor(),
shape,
chunks,
new DType(datasetAttributes.getDataType()),
dType,
ZarrCompressor.fromCompression(datasetAttributes.getCompression()),
"0",
'C',
dimensionSeparator,
null);
dType.getFilters());

return zArrayAttributes;
}
Expand Down

0 comments on commit 9dc961c

Please sign in to comment.