Skip to content

Commit

Permalink
Test The Units/Pixel
Browse files Browse the repository at this point in the history
There is a 1.3% error because of the way VCell handles the "atomic" unit of pixels (in VCell they aren't exactly atomic). It can be calculated to 0%, but this is an initial start.
  • Loading branch information
AvocadoMoon committed Jul 22, 2024
1 parent 6cd4bc5 commit 0618eb6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import com.amazonaws.services.s3.AmazonS3URI;
import com.google.gson.GsonBuilder;
import ij.ImagePlus;
import ij.measure.Calibration;
import ij.plugin.Duplicator;
import net.imglib2.cache.img.CachedCellImg;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.type.numeric.real.DoubleType;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.ssl.SSLContexts;
import org.janelia.saalfeldlab.n5.N5Exception;
import org.janelia.saalfeldlab.n5.N5FSReader;
import org.janelia.saalfeldlab.n5.N5KeyValueReader;
import org.janelia.saalfeldlab.n5.N5Reader;
Expand Down Expand Up @@ -174,6 +176,7 @@ public ImagePlus getImgPlusFromN5File() throws IOException {
ImagePlus imagePlus = ImageJFunctions.wrap((CachedCellImg<DoubleType, ?>) N5Utils.open(n5AmazonS3Reader, dataSetChosen), userSetFileName);
long end = System.currentTimeMillis();
logger.debug("Read N5 File " + userSetFileName + " Into ImageJ taking: " + ((end - start) / 1000) + "s");
setUnits(n5AmazonS3Reader, imagePlus);
return imagePlus;
}

Expand All @@ -189,6 +192,20 @@ public void setURI(URI uri){
}
}

private void setUnits(N5Reader n5Reader, ImagePlus imagePlus){
try{
double pixelWidth = n5Reader.getAttribute(dataSetChosen, "pixelWidth", double.class);
double pixelHeight = n5Reader.getAttribute(dataSetChosen, "pixelHeight", double.class);
String unit = n5Reader.getAttribute(dataSetChosen, "unit", String.class);
imagePlus.getCalibration().setUnit(unit);
imagePlus.getCalibration().pixelHeight = pixelHeight;
imagePlus.getCalibration().pixelWidth = pixelWidth;
}
catch (N5Exception n5Exception){
logger.error("Can't read units.");
}
}

public void setDataSetChosen(String dataSetChosen) {
this.dataSetChosen = dataSetChosen;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
import java.util.HashMap;

public class N5DataSetFile {
public String uri;
public String[] variables;
public HashMap<Integer, String> mask;
public double[][] histMax; //channel(variable), frame for [][]
public double[][] histMin;
public double[][] histAverage;
public N5DataSetFile(String uri, String[] variables, HashMap<Integer, String> mask, double[][] histMax, double[][] histMin, double[][] histAverage){
public final String uri;
public final String[] variables;
public final HashMap<Integer, String> mask;
public final double[][] histMax; //channel(variable), frame for [][]
public final double[][] histMin;
public final double[][] histAverage;
public final double totalArea;
public final double testDomainArea;
public N5DataSetFile(String uri, String[] variables, HashMap<Integer, String> mask, double[][] histMax, double[][] histMin, double[][] histAverage, double totalArea, double testDomainArea){
this.uri = uri;
this.histMax = histMax;
this.histMin = histMin;
this.histAverage = histAverage;
this.variables = variables;
this.mask = mask;
this.totalArea = totalArea;
this.testDomainArea = testDomainArea;
}

public static N5DataSetFile[] alphaTestFiles(){
Expand All @@ -29,7 +33,9 @@ public static N5DataSetFile[] alphaTestFiles(){
4.441933641025697, 4.752525652114409, 5.021489447662615, 5.25417298783172, 5.4553370523518065, 5.62917275251317, 5.7793451286004585, 5.909046810912881, 6.0210510711120975}},
new double[][]{{
6.728509585652239, 6.7285095856523345, 6.728509585652324, 6.72850958565233, 6.728509585652335, 6.728509585652381, 6.728509585652381, 6.728509585652362, 6.728509585652365,
6.728509585652356, 6.7285095856523185, 6.728509585652278, 6.7285095856522865, 6.728509585652241, 6.728509585652113, 6.728509585652124, 6.7285095856521355, 6.728509585652127, 6.728509585652108}});
6.728509585652356, 6.7285095856523185, 6.728509585652278, 6.7285095856522865, 6.728509585652241, 6.728509585652113, 6.728509585652124, 6.7285095856521355, 6.728509585652127, 6.728509585652108}},
484,
314.64);

return new N5DataSetFile[]{frapSimulationResultsMasked};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package org.vcell.N5;

import com.amazonaws.regions.Regions;
import ij.ImagePlus;
import ij.io.Opener;
import ij.plugin.Duplicator;
import ij.plugin.ImageCalculator;
import ij.plugin.Thresholder;
import ij.process.Blitter;
import ij.process.ByteProcessor;
import ij.process.ImageProcessor;
import org.junit.*;

Expand All @@ -16,7 +12,6 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;


/*
Expand Down Expand Up @@ -128,6 +123,50 @@ public void testS3AlphaInstanceLoadedIntoMemory() throws IOException {
}
}

interface PixelCalculations {
int grabEdge(int w, int h, int w1, int h1);
}

@Test
public void testUnits() throws IOException {
N5DataSetFile[] n5DataSetFiles = N5DataSetFile.alphaTestFiles();
for (N5DataSetFile n5DataSetFile: n5DataSetFiles){
SimResultsLoader simResultsLoader = new SimResultsLoader(n5DataSetFile.uri, "");
simResultsLoader.createS3Client();
ImagePlus imagePlus = simResultsLoader.getImgPlusFromN5File();
double areaOfPixel = imagePlus.getCalibration().getX(1) * imagePlus.getCalibration().getY(1);
double totalArea = areaOfPixel * imagePlus.getWidth() * imagePlus.getHeight();
Assert.assertEquals(n5DataSetFile.totalArea, totalArea, 0.0000001);

totalArea = 0;
imagePlus.setPosition(imagePlus.getNChannels(), 1, 1);
ImageProcessor imageProcessor = imagePlus.getProcessor();

PixelCalculations pixelCalculations = ((w, h, w1, h1) -> {
int inBounds = (0 <= w1) && (w1 < imagePlus.getWidth()) && (0 <= h1) && (h1 < imagePlus.getHeight()) ? 2 : 1;
if (inBounds == 2 && imageProcessor.getf(w1, h1) != imageProcessor.getf(w, h)){
return inBounds;
}
return 1;
});
for (int h = 0; h < imagePlus.getHeight(); h++){
for (int w = 0; w < imagePlus.getWidth(); w++){
double sum = pixelCalculations.grabEdge(w, h, w, h -1) *
pixelCalculations.grabEdge(w, h, w, h + 1) *
pixelCalculations.grabEdge(w, h,w + 1, h) *
pixelCalculations.grabEdge(w, h,w -1, h);
if (imageProcessor.getf(w, h) != 1 && sum > 1){
totalArea += (areaOfPixel / sum);
}
if (imageProcessor.getf(w, h) == 1){
totalArea += areaOfPixel;
}
}
}
Assert.assertEquals(n5DataSetFile.testDomainArea, totalArea, n5DataSetFile.testDomainArea * 0.013);
}
}

public void alphaStatsTest(ImagePlus imagePlus, N5DataSetFile n5DataSetFile, stats testType){
double[][] controlData = {{}};
switch (testType){
Expand Down

0 comments on commit 0618eb6

Please sign in to comment.