diff --git a/render-ws-java-client/src/main/java/org/janelia/render/client/embackground/BackgroundModel.java b/render-ws-java-client/src/main/java/org/janelia/render/client/embackground/BackgroundModel.java
index 4add63c4c..fbc41b47f 100644
--- a/render-ws-java-client/src/main/java/org/janelia/render/client/embackground/BackgroundModel.java
+++ b/render-ws-java-client/src/main/java/org/janelia/render/client/embackground/BackgroundModel.java
@@ -8,6 +8,7 @@
import org.ejml.dense.row.factory.LinearSolverFactory_DDRM;
import org.ejml.interfaces.linsol.LinearSolverDense;
+import java.io.Serializable;
import java.util.Collection;
import java.util.List;
@@ -15,7 +16,7 @@
/**
* An abstract base model for background correction in 2D slices of EM data.
*/
-public abstract class BackgroundModel> extends AbstractModel {
+public abstract class BackgroundModel> extends AbstractModel implements Serializable {
private final double[] coefficients;
diff --git a/render-ws-spark-client/src/main/java/org/janelia/render/client/spark/intensityadjust/BackgroundCorrectionClient.java b/render-ws-spark-client/src/main/java/org/janelia/render/client/spark/intensityadjust/BackgroundCorrectionClient.java
index 12a693c36..3bf575eb3 100644
--- a/render-ws-spark-client/src/main/java/org/janelia/render/client/spark/intensityadjust/BackgroundCorrectionClient.java
+++ b/render-ws-spark-client/src/main/java/org/janelia/render/client/spark/intensityadjust/BackgroundCorrectionClient.java
@@ -1,12 +1,18 @@
package org.janelia.render.client.spark.intensityadjust;
import com.beust.jcommander.Parameter;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.janelia.alignment.util.Grid;
import org.janelia.render.client.ClientRunner;
import org.janelia.render.client.embackground.BackgroundModel;
+import org.janelia.render.client.embackground.FourthOrderBackground;
+import org.janelia.render.client.embackground.QuadraticBackground;
import org.janelia.render.client.parameter.CommandLineParameters;
import org.janelia.saalfeldlab.n5.DatasetAttributes;
import org.janelia.saalfeldlab.n5.N5FSReader;
@@ -16,8 +22,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -30,6 +38,23 @@
* is valid for all z layers starting at the given z value until the next z value in the list.
* Models are specified by an identifier ("quadratic" or "fourthOrder") and a list of coefficients (6 or 9,
* respectively). Coefficients can be found interactively using {@link org.janelia.render.client.embackground.BG_Plugin}.
+ *
+ * In particular, the parameter file should have the following format. There is one root object with a single key
+ * "models". The value of this key is an array of objects, each with three keys: "z", "model", and "coefficients"s, e.g.:
+ *
+ * {
+ * "models": [ {
+ * "z": 0,
+ * "model": "quadratic",
+ * "coefficients": [1.0, 0.0, 1.0, 0.0, 0.0, 0.0]
+ * }, {
+ * "z": 10,
+ * "model": "fourthOrder",
+ * "coefficients": [1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
+ * }
+ * ]
+ * }
+ *
*/
public class BackgroundCorrectionClient implements Serializable {
@@ -89,12 +114,13 @@ public void run() throws IOException {
}
}
- public void runWithContext(final JavaSparkContext sparkContext) {
+ public void runWithContext(final JavaSparkContext sparkContext) throws IOException {
LOG.info("runWithContext: entry");
// read parameters
- final Map> models = null;
+ final BackgroundModelProvider modelProvider = BackgroundModelProvider.fromFile(parameters.parameterFile);
+ System.exit(0);
// set up input and output N5 datasets
final DatasetAttributes inputAttributes;
@@ -119,7 +145,7 @@ public void runWithContext(final JavaSparkContext sparkContext) {
// parallelize computation over blocks of the input/output dataset
final List blocks = Grid.create(inputAttributes.getDimensions(), inputAttributes.getBlockSize());
final JavaRDD blockRDD = sparkContext.parallelize(blocks, blocks.size());
- blockRDD.foreach(block -> processSingleBlock(parameters, models));
+// blockRDD.foreach(block -> processSingleBlock(parameters, models));
LOG.info("runWithContext: exit");
}
@@ -128,5 +154,59 @@ private static void processSingleBlock(final Parameters parameters, final Map zValues;
+ private final List> models;
+
+ private BackgroundModelProvider(final List zValues, final List> models) {
+ this.zValues = zValues;
+ this.models = models;
+ }
+
+ public BackgroundModel> getModel(final int z) {
+ for (int i = 0; i < zValues.size() - 1; i++) {
+ if (z >= zValues.get(i) && z < zValues.get(i + 1)) {
+ return models.get(i);
+ }
+ }
+ return null;
+ }
+
+
+ public static BackgroundModelProvider fromFile(final String fileName) throws IOException {
+
+ final List zValues = new ArrayList<>();
+ final List> models = new ArrayList<>();
+
+ final JsonArray root;
+ try (final FileReader reader = new FileReader(fileName)) {
+ root = JsonParser.parseReader(reader).getAsJsonObject().getAsJsonArray("models");
+ }
+
+ for (final JsonElement jsonElement : root) {
+ final JsonObject parameterSet = jsonElement.getAsJsonObject();
+ final int z = parameterSet.get("fromZ").getAsInt();
+ final String modelType = parameterSet.get("model").getAsString();
+ final double[] coefficients = parameterSet.getAsJsonArray("coefficients").asList().stream()
+ .map(JsonElement::getAsDouble)
+ .mapToDouble(Double::doubleValue).toArray();
+
+ LOG.info("Extract model from parameters file: fromZ={}, modelType={}, coefficients={}", z, modelType, coefficients);
+
+ zValues.add(z);
+ if (modelType.equals("quadratic")) {
+ models.add(new QuadraticBackground(coefficients));
+ } else if (modelType.equals("fourthOrder")) {
+ models.add(new FourthOrderBackground(coefficients));
+ } else {
+ throw new IllegalArgumentException("Unknown model type: " + modelType);
+ }
+ }
+
+ return new BackgroundModelProvider(zValues, models);
+ }
+ }
+
private static final Logger LOG = LoggerFactory.getLogger(BackgroundCorrectionClient.class);
}