Created
May 19, 2021 14:26
-
-
Save bogovicj/6b94f679089a9d7bd9d034e9d0fe21e0 to your computer and use it in GitHub Desktop.
Tmp n5 parser for caleb
This file contains hidden or 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
package org.janelia.saalfeldlab.n5.metadata; | |
import org.janelia.saalfeldlab.n5.DatasetAttributes; | |
import org.janelia.saalfeldlab.n5.N5Reader; | |
import org.janelia.saalfeldlab.n5.N5TreeNode; | |
import java.io.IOException; | |
import java.util.Map; | |
import java.util.Optional; | |
import static org.janelia.saalfeldlab.n5.metadata.MultiscaleMetadata.IS_LABEL_MULTISET_KEY; | |
public class N5GenericSingleScaleMetadataParser implements N5MetadataParser<N5GenericSingleScaleMetadata> { | |
// extends AbstractN5DatasetMetadataParser<N5GenericSingleScaleMetadata> { | |
public static final String MIN = "min"; | |
public static final String MAX = "max"; | |
public static final String RESOLUTION = "resolution"; | |
public static final String OFFSET = "offset"; | |
public static final String IS_LABEL_MULTISET = "isLabelMultiset"; | |
public static final String DOWNSAMPLING_FACTORS = "downsamplingFactors"; | |
// { | |
// keysToTypes.put(MIN, double.class); | |
// keysToTypes.put(MAX, double.class); | |
// keysToTypes.put(RESOLUTION, double[].class); | |
// keysToTypes.put(OFFSET, double[].class); | |
// keysToTypes.put(DOWNSAMPLING_FACTORS, double[].class); | |
// keysToTypes.put(IS_LABEL_MULTISET, boolean.class); | |
// } | |
// @Override | |
// public boolean check(final Map<String, Object> metaMap) { | |
// | |
// final Map<String, Class<?>> requiredKeys = | |
// AbstractN5DatasetMetadataParser.datasetAtttributeKeys(); | |
// for (final String k : requiredKeys.keySet()) { | |
// if (!metaMap.containsKey(k)) | |
// return false; | |
// else if (metaMap.get(k) == null) | |
// return false; | |
// } | |
// | |
// return true; | |
// } | |
@Override | |
public Optional<N5GenericSingleScaleMetadata> parseMetadata(final N5Reader n5, final N5TreeNode node) { | |
try { | |
final DatasetAttributes attributes = n5.getDatasetAttributes(node.getPath()); | |
if (attributes == null) | |
return Optional.empty(); | |
final double min = Optional.ofNullable(n5.getAttribute(node.getPath(), MIN, double.class)) | |
.orElse(0.0); | |
final double max = Optional.ofNullable(n5.getAttribute(node.getPath(), MAX, double.class)) | |
.orElseGet(() -> PainteraBaseMetadata.maxForDataType(attributes.getDataType())); | |
final double[] resolution = Optional.ofNullable(n5.getAttribute(node.getPath(), RESOLUTION, double[].class)) | |
.orElse(new double[]{1.0, 1.0, 1.0}); | |
if (resolution.length != 3) { | |
return Optional.empty(); | |
} | |
final double[] offset = Optional.ofNullable(n5.getAttribute(node.getPath(), OFFSET, double[].class)) | |
.orElse(new double[]{1.0, 1.0, 1.0}); | |
final double[] downsamplingFactors = Optional.ofNullable(n5.getAttribute(node.getPath(), DOWNSAMPLING_FACTORS, double[].class)) | |
.orElse(new double[]{1.0, 1.0, 1.0}); | |
final Boolean isLabelMultiset = Optional.ofNullable(n5.getAttribute(node.getPath(), IS_LABEL_MULTISET, Boolean.class)) | |
.orElse(false); | |
N5GenericSingleScaleMetadata metadata = new N5GenericSingleScaleMetadata(node.getPath(), attributes, min, max, resolution, offset, | |
downsamplingFactors, isLabelMultiset); | |
if (metadata.offset.length != 3) | |
return Optional.empty(); | |
return Optional.of(metadata); | |
} catch (IOException e) { | |
return Optional.empty(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment