Skip to content

Commit

Permalink
Store unfinished work on parameter file
Browse files Browse the repository at this point in the history
  • Loading branch information
minnerbe committed Nov 7, 2024
1 parent 8e5290f commit 8243597
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
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;


/**
* An abstract base model for background correction in 2D slices of EM data.
*/
public abstract class BackgroundModel<T extends BackgroundModel<T>> extends AbstractModel<T> {
public abstract class BackgroundModel<T extends BackgroundModel<T>> extends AbstractModel<T> implements Serializable {

private final double[] coefficients;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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}.
* </p>
* 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.:
* <pre>
* {
* "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]
* }
* ]
* }
* </pre>
*/
public class BackgroundCorrectionClient implements Serializable {

Expand Down Expand Up @@ -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<Integer, BackgroundModel<?>> models = null;
final BackgroundModelProvider modelProvider = BackgroundModelProvider.fromFile(parameters.parameterFile);
System.exit(0);

// set up input and output N5 datasets
final DatasetAttributes inputAttributes;
Expand All @@ -119,7 +145,7 @@ public void runWithContext(final JavaSparkContext sparkContext) {
// parallelize computation over blocks of the input/output dataset
final List<Grid.Block> blocks = Grid.create(inputAttributes.getDimensions(), inputAttributes.getBlockSize());
final JavaRDD<Grid.Block> blockRDD = sparkContext.parallelize(blocks, blocks.size());
blockRDD.foreach(block -> processSingleBlock(parameters, models));
// blockRDD.foreach(block -> processSingleBlock(parameters, models));

LOG.info("runWithContext: exit");
}
Expand All @@ -128,5 +154,59 @@ private static void processSingleBlock(final Parameters parameters, final Map<In

}


private static class BackgroundModelProvider implements Serializable {
private final List<Integer> zValues;
private final List<BackgroundModel<?>> models;

private BackgroundModelProvider(final List<Integer> zValues, final List<BackgroundModel<?>> 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<Integer> zValues = new ArrayList<>();
final List<BackgroundModel<?>> 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);
}

0 comments on commit 8243597

Please sign in to comment.