Skip to content

Commit

Permalink
Consolidate HackImageUrlPathClient and BackgroundCorrectedImageUrlClient
Browse files Browse the repository at this point in the history
  • Loading branch information
minnerbe committed Dec 13, 2024
1 parent dc2af2f commit 79fae18
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 157 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.File;
import java.util.Map;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -22,8 +23,11 @@

/**
* Java client to copy a stack's tiles, changing the image URL paths for all tiles in the resulting stack.
* In this case, the image URLs are changed to point to images that have been contrast-adjusted using the Hayworth
* pipeline.
* Currently implemented URL transformations:
* - HayworthContrastPathTransformation: changes the image URL to point to images that have been contrast-adjusted using
* the Hayworth pipeline.
* - BasicBackgroundCorrectionPathTransformation: changes the image URL to point to images that have been
* background-corrected using the BaSiC background correction method.
*
* @author Eric Trautman
*/
Expand All @@ -48,6 +52,8 @@ public static class Parameters extends CommandLineParameters {
private String targetStack;
}

private static final Logger LOG = LoggerFactory.getLogger(HackImageUrlPathClient.class);

public static void main(final String[] args) {

final ClientRunner clientRunner = new ClientRunner(args) {
Expand All @@ -60,20 +66,22 @@ public void runClient(final String[] args)

LOG.info("runClient: entry, parameters={}", parameters);

final HackImageUrlPathClient client = new HackImageUrlPathClient(parameters);
final UnaryOperator<String> pathTransformation = new BasicBackgroundCorrectionPathTransformation();
final HackImageUrlPathClient client = new HackImageUrlPathClient(parameters, pathTransformation);
client.fixStackData();
}
};
clientRunner.run();
}

private final Parameters parameters;

private final RenderDataClient renderDataClient;
private final UnaryOperator<String> pathTransformation;

private HackImageUrlPathClient(final Parameters parameters) {
private HackImageUrlPathClient(final Parameters parameters, final UnaryOperator<String> pathTransformation) {
this.parameters = parameters;
this.renderDataClient = parameters.renderWeb.getDataClient();
this.pathTransformation = pathTransformation;
}

private void fixStackData() throws Exception {
Expand All @@ -96,47 +104,59 @@ private void fixStackData() throws Exception {
}

private void fixTileSpec(final TileSpec tileSpec) {

final Integer zeroLevelKey = 0;

for (final ChannelSpec channelSpec : tileSpec.getAllChannels()) {

final Map.Entry<Integer, ImageAndMask> entry = channelSpec.getFirstMipmapEntry();
if ((entry == null) || !zeroLevelKey.equals(entry.getKey())) {
continue;
}

if ((entry != null) && zeroLevelKey.equals(entry.getKey())) {

final ImageAndMask sourceImageAndMask = entry.getValue();
final ImageAndMask sourceImageAndMask = entry.getValue();

// file:/nrs/hess/data/hess_wafer_53/raw/imaging/msem/scan_001/wafer_53_scan_001_20220427_23-16-30/402_/000005/402_000005_001_2022-04-28T1457426331720.png
final String imageUrl = sourceImageAndMask.getImageUrl();
final String imageUrl = sourceImageAndMask.getImageUrl();
final String transformedUrl = pathTransformation.apply(imageUrl);

final Matcher m = PATH_PATTERN.matcher(imageUrl);
if (m.matches()) {
if (transformedUrl == null) {
throw new IllegalArgumentException("could not transform image URL: " + imageUrl);
}

// file:/nrs/hess/data/hess_wafer_53/msem_with_hayworth_contrast/scan_001/402_/000005/402_000005_001_2022-04-28T1457426331720.png
final File hackFile = new File("/nrs/hess/data/hess_wafer_53/msem_with_hayworth_contrast/" +
m.group(1) + m.group(2));
if (! hackFile.exists()) {
throw new IllegalArgumentException("file does not exist: " + hackFile);
}
final File hackFile = new File(transformedUrl);
if (! hackFile.exists()) {
throw new IllegalArgumentException("target file does not exist: " + hackFile);
}

final ImageAndMask hackedImageAndMask =
sourceImageAndMask.copyWithDerivedUrls("file:" + hackFile.getAbsolutePath(),
sourceImageAndMask.getMaskUrl());
final ImageAndMask hackedImageAndMask = sourceImageAndMask.copyWithDerivedUrls("file:" + hackFile.getAbsolutePath(),
sourceImageAndMask.getMaskUrl());
channelSpec.putMipmap(zeroLevelKey, hackedImageAndMask);
}
}

channelSpec.putMipmap(zeroLevelKey, hackedImageAndMask);

} else {
throw new IllegalArgumentException("invalid image URL: " + imageUrl);
}
private static class HayworthContrastPathTransformation implements UnaryOperator<String> {
private final Pattern PATH_PATTERN = Pattern.compile("^file:/nrs.*/(scan_\\d\\d\\d/)wafer.*/(\\d\\d\\d_/.*png)$");

// original: file:/nrs/hess/data/hess_wafer_53/raw/imaging/msem/scan_001/wafer_53_scan_001_20220427_23-16-30/402_/000005/402_000005_001_2022-04-28T1457426331720.png
// target: file:/nrs/hess/data/hess_wafer_53/msem_with_hayworth_contrast/scan_001/402_/000005/402_000005_001_2022-04-28T1457426331720.png
@Override
public String apply(final String path) {
final Matcher matcher = PATH_PATTERN.matcher(path);
if (matcher.matches()) {
return "/nrs/hess/data/hess_wafer_53/msem_with_hayworth_contrast/" + matcher.group(1) + matcher.group(2);
} else {
return null;
}

}

}

private final Pattern PATH_PATTERN = Pattern.compile("^file:/nrs.*/(scan_\\d\\d\\d/)wafer.*/(\\d\\d\\d_/.*png)$");
private static class BasicBackgroundCorrectionPathTransformation implements UnaryOperator<String> {

// original: file:/nrs/hess/ibeammsem/system_02/wafers/wafer_60/acquisition/scans/scan_004/slabs/slab_0399/mfovs/mfov_0000/sfov_001.png
// target: file:/nrs/hess/ibeammsem/system_02/wafers/wafer_60/acquisition/background_corrected/scans/scan_004/slabs/slab_0399/mfovs/mfov_0000/sfov_001.png
@Override
public String apply(final String path) {
return path.substring(5, 62) + "/background_corrected" + path.substring(62);
}
}

private static final Logger LOG = LoggerFactory.getLogger(HackImageUrlPathClient.class);
}

0 comments on commit 79fae18

Please sign in to comment.