Skip to content

Commit

Permalink
Merge pull request #3 from tomburke-rse/switch-bson-with-json
Browse files Browse the repository at this point in the history
Rewrite to json from bson
  • Loading branch information
ctrueden authored Apr 20, 2022
2 parents b0dffc0 + b658367 commit aeb36b7
Show file tree
Hide file tree
Showing 38 changed files with 374 additions and 1,522 deletions.
2 changes: 1 addition & 1 deletion labeling.iml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<orderEntry type="library" name="Maven: com.googlecode.efficient-java-matrix-library:ejml:0.24" level="project" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
<orderEntry type="library" name="Maven: net.sf.trove4j:trove4j:3.0.3" level="project" />
<orderEntry type="library" name="Maven: org.mongodb:bson:4.2.3" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.6" level="project" />
<orderEntry type="library" name="Maven: io.scif:scifio:0.41.1" level="project" />
<orderEntry type="library" name="Maven: io.scif:scifio-jai-imageio:1.1.1" level="project" />
<orderEntry type="library" name="Maven: net.imagej:imagej-common:0.34.0" level="project" />
Expand Down
15 changes: 10 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>30.0.0</version>
<relativePath />
<relativePath/>
</parent>

<groupId>net.imglib2</groupId>
Expand Down Expand Up @@ -110,14 +111,18 @@

<!-- Other dependencies -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>4.2.3</version>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>io.scif</groupId>
<artifactId>scifio</artifactId>
</dependency>
<dependency>
<groupId>org.scijava</groupId>
<artifactId>scijava-common</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
326 changes: 129 additions & 197 deletions src/main/java/net/imglib2/labeling/DefaultLabelingIOService.java

Large diffs are not rendered by default.

106 changes: 30 additions & 76 deletions src/main/java/net/imglib2/labeling/LabelingIOService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -34,11 +34,9 @@
package net.imglib2.labeling;

import net.imagej.ImageJService;
import net.imglib2.labeling.codecs.LabelingMappingCodec;
import net.imglib2.labeling.data.Container;
import net.imglib2.roi.labeling.ImgLabeling;
import net.imglib2.type.numeric.IntegerType;
import org.bson.codecs.Codec;

import java.io.IOException;
import java.util.function.LongFunction;
Expand All @@ -48,111 +46,67 @@
* A service to easily access a load/save functionality for BSON-based labeling data files.
* Basic support for primitive types and BSON standard types is already included. For non-primitive types,
* a codec must be set and the class must be given as an argument to the methods.
* Examples for Codecs can be found at {@link LabelingMappingCodec}.
*
* @author Tom Burke
*/
public interface LabelingIOService extends ImageJService {

<T, I extends IntegerType<I>> ImgLabeling<T,I> load(String file) throws IOException;

<T, I extends IntegerType<I>> ImgLabeling<T,I> load(String file, Class<T> clazz, Codec<T>... codecs) throws IOException;

<T, I extends IntegerType<I>> void save(ImgLabeling<T,I> imgLabeling, String file) throws IOException;

<T, I extends IntegerType<I>> void save(ImgLabeling<T,I> imgLabeling, String file, Class<T> clazz, Codec<T>... codecs) throws IOException;
<T, I extends IntegerType<I>> ImgLabeling<T, I> load(String file, Class<T> labelType, Class<I> backingType) throws IOException;

<T, I extends IntegerType<I>> void save(ImgLabeling<T, I> imgLabeling, String file) throws IOException;

/**
* Load a labeling container from the given file path as string.
* The file path must point to the bson file containing the labeling data.
*
* @param file The path to the file
* @param codecs One or more codecs necessary to convert the data to clazz.
* @param metadataClazz the metadata class
* @param <T> the label value
* @param <I> IntegerType for the pixel value
* @param <S> Class of the meta data
* @throws IOException on file read fail
* @param file The path to the file
* @param metadataType the metadata class
* @param <T> the label value
* @param <I> IntegerType for the pixel value
* @param <S> Class of the meta data
* @return a container object holding the ImgLabeling (as well as an optional source mapping)
*/
<S, T, I extends IntegerType<I>> Container<S, T, I> loadWithMetadata(String file, Class<S> metadataClazz, Codec<?>... codecs) throws IOException;


/**
* Load a labeling container from the given file path as string.
* The file path must point to the bson file containing the labeling data.
*
* @param file The path to the file
* @param clazz The class to convert the contains of the file to
* @param metadataClazz the metadata class
* @param codecs One or more codecs necessary to convert the data to clazz.
* @param <T> the label value
* @param <I> IntegerType for the pixel value
* @param <S> Class of the meta data
* @throws IOException on file read fail
* @return a container object holding the ImgLabeling (as well as an optional source mapping)
*/
<S, T, I extends IntegerType<I>> Container<S, T, I> loadWithMetadata(String file, Class<T> clazz, Class<S> metadataClazz, Codec<?>... codecs) throws IOException;
<S, T, I extends IntegerType<I>> Container<S, T, I> loadWithMetadata(String file, Class<S> metadataType, Class<T> labelType, Class<I> backingType) throws IOException;

/**
* Load a labeling container from the given file path as string.
* The file path must point to the bson file containing the labeling data.
*
* @param file The path to the file
* @param idToLabel a function transforming the label of type T into something else
* @param file The path to the file
* @param idToLabel a function transforming the label of type T into something else
* @param metadataClazz the metadata class
* @param codecs One or more codecs necessary to convert the data to clazz.
* @param <T> the label value
* @param <I> IntegerType for the pixel value
* @param <S> Class of the meta data
* @throws IOException on file read fail
* @param <T> the label value
* @param <I> IntegerType for the pixel value
* @param <S> Class of the meta data
* @return a container object holding the ImgLabeling (as well as an optional source mapping)
* @throws IOException on file read fail
*/
<S, T, I extends IntegerType<I>> Container<S, T, I> loadWithMetadata(String file, LongFunction<T> idToLabel, Class<S> metadataClazz, Codec<?>... codecs) throws IOException;

/**
* Save an ImgLabelingContainer in the file-path, transforming it into a bson file and an image.
* The path must contain the filename (ending does not matter).
*
* @param codecs one or more codecs to convert clazz into something bsonifyable
* @param container the container with the ImgLabeling and a metadata object
* @param file the path pointing to the file, including the filename
* @param metadataClazz the metadata class
* @param <T> the label value
* @param <I> IntegerType for the pixel value
* @param <S> Class of the meta data
*/
<S, T, I extends IntegerType<I>> void saveWithMetaData(Container<S, T, I> container, String file, Class<S> metadataClazz, Codec<?>... codecs);
<S, T, I extends IntegerType<I>> Container<S, T, I> loadWithMetadata(String file, LongFunction<T> idToLabel, Class<S> metadataClazz) throws IOException;

/**
* Save an ImgLabelingContainer in the file-path, transforming it into a bson file and an image.
* The path must contain the filename (ending does not matter).
*
* @param clazz the class of the type T that is contained in the labeling
* @param codecs one or more codecs to convert clazz into something bsonifyable
* @param container the container with the ImgLabeling and a metadata object
* @param file the path pointing to the file, including the filename
* @param metadataClazz the metadata class
* @param <T> the label value
* @param <I> IntegerType for the pixel value
* @param <S> Class of the meta data
* @param imgLabeling the imglabeling object that needs to be serialized
* @param file the path pointing to the file, including the filename
* @param <T> the label value
* @param <I> IntegerType for the pixel value
* @param <S> Class of the meta data
*/
<S, T, I extends IntegerType<I>> void saveWithMetaData(Container<S, T, I> container, String file, Class<T> clazz, Class<S> metadataClazz, Codec<?>... codecs);
<S, T, I extends IntegerType<I>> void saveWithMetaData(ImgLabeling<T, I> imgLabeling, String file, S metadata) throws IOException;

/**
* Save an ImgLabelingContainer in the file-path, transforming it into a bson file and an image.
* The path must contain the filename (ending does not matter).
*
* @param labelToId a function to convert the type T to a long value.
* @param container the container with the ImgLabeling and a metadata object
* @param codecs one or more codecs to convert clazz into something bsonifyable
* @param file the path pointing to the file, including the filename
* @param metadataClazz the metadata class
* @param <T> the label value
* @param <I> IntegerType for the pixel value
* @param <S> Class of the meta data
* @param imgLabeling the imglabeling object that needs to be serialized
* @param labelToId a function to convert the type T to a long value.
* @param file the path pointing to the file, including the filename
* @param <T> the label value
* @param <I> IntegerType for the pixel value
* @param <S> Class of the meta data
*/
<S, T, I extends IntegerType<I>> void saveWithMetaData(Container<S, T, I> container, String file, ToLongFunction<T> labelToId, Class<S> metadataClazz, Codec<?>... codecs);
<S, T, I extends IntegerType<I>> void saveWithMetaData(ImgLabeling<T, I> imgLabeling, String file, ToLongFunction<T> labelToId, S metadata) throws IOException;

}
Loading

0 comments on commit aeb36b7

Please sign in to comment.