Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
* The modified tile specs are written to a target stack named by appending {@code --targetStackSuffix} to the source
* stack name (the stack is derived from the source if it does not yet exist), and that target stack is completed once
* all layers have been saved.
* <p>
* If {@code --stitchFixUri} is given, the layers that SOFIMA restitched for this slab (see {@link SofimaStitchFix})
* have a transform of each tile replaced before the displacement field is appended. The fix is applied to tiles
* read from the unchanged source stack, so the field's offset still comes from the original stack bounds, which is
* what the field was computed on.
*
* @author Michael Innerberger
*/
Expand Down Expand Up @@ -131,13 +136,26 @@ public void addDisplacementField() throws Exception {
throw new IllegalArgumentException("Failed to process SOFIMA field at " + fieldUri, e);
}

final List<Double> zValues = renderClient.getStackZValues(params.stack);

// Load and check any stitch fixes before the target stack is touched, so a mismatch fails up front.
// Fixed layers are numbered from the stack's minZ, like the field's z-slices.
final SofimaStitchFix stitchFix;
if (params.sofima.hasStitchFixUri()) {
stitchFix = SofimaStitchFix.load(params.sofima.stitchFixUri,
SofimaParameters.getSlabForStack(params.stack),
offset[2]);
stitchFix.validateZValues(zValues);
} else {
stitchFix = null;
}

// Set up the target stack. The suffix is validated as non-empty, so the target stack
// is always distinct from the source stack and is always derived from it.
final String targetStack = params.sofima.getTargetStackId(sourceStackMetaData.getStackId()).getStack();
renderClient.setupDerivedStack(sourceStackMetaData, targetStack);

// Get and process all z values
final List<Double> zValues = renderClient.getStackZValues(params.stack);
// Process all z values
LOG.info("addDisplacementField: processing {} layers with {} threads", zValues.size(), params.numThreads);

// One pool, reused for the tiles of each layer in turn (see addFieldToLayer for why tiles and not layers)
Expand All @@ -146,7 +164,7 @@ public void addDisplacementField() throws Exception {
// Derive the slice from z itself rather than from the running layer index, so that a gap in the
// stack's z values does not shift every later layer onto the wrong slice.
final long fieldZIndex = Math.round(z - offset[2]);
addFieldToLayer(z, buildDataString(fieldUri, fieldZIndex, scale, offset), targetStack, pool);
addFieldToLayer(z, buildDataString(fieldUri, fieldZIndex, scale, offset), stitchFix, targetStack, pool);
}
}

Expand All @@ -158,19 +176,26 @@ public void addDisplacementField() throws Exception {
}

/**
* Adds the transform to every tile spec of one layer and saves them. Deriving a bounding box evaluates the
* Applies the layer's stitch fix (if any), adds the transform to every tile spec of one layer and saves them. Deriving a bounding box evaluates the
* transform over the tile's footprint, so this is where the (blocking) field chunk reads happen. Tiles are the
* natural axis to parallelize: they cover disjoint parts of the field, so concurrent tiles load disjoint chunks
* and the round-trip latency of those reads overlaps.
*/
private void addFieldToLayer(final Double z,
final String dataString,
final SofimaStitchFix stitchFix,
final String targetStack,
final ForkJoinPool pool)
throws IOException {

final ResolvedTileSpecCollection tileSpecs = renderClient.getResolvedTiles(params.stack, z);

// Replace the restitched transforms first (cheap, and checked for all tiles before any is changed), so
// that the bounding boxes below are derived once from the final transform lists.
if ((stitchFix != null) && stitchFix.hasFixForZ(z)) {
stitchFix.apply(z, tileSpecs);
}

// Run the parallel stream inside the given pool. Balances the load internally
pool.invoke(ForkJoinTask.adapt(() -> tileSpecs.getTileSpecs().parallelStream().forEach(tileSpec -> {
final LeafTransformSpec transformSpec = new LeafTransformSpec(DisplacementFieldTransform.class.getName(), dataString);
Expand Down
Loading
Loading