Skip to content

Commit 21987fd

Browse files
committed
Unit tests for CellposeSAM
1 parent 83bb92e commit 21987fd

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package fiji.plugin.appose.cellpose;
2+
3+
import org.junit.jupiter.api.Test;
4+
import fiji.plugin.appose.cellpose.cp4.Cellpose4Parameters;
5+
import ij.IJ;
6+
import ij.ImagePlus;
7+
import ij.plugin.ChannelSplitter;
8+
import ij.plugin.Duplicator;
9+
import ij.plugin.RGBStackMerge;
10+
import ij.process.ImageStatistics;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
15+
16+
public class TestCellPoseSAMAppose
17+
{
18+
19+
@Test
20+
public void defaultRunCPSAM() throws Exception
21+
{
22+
try
23+
{
24+
final ImagePlus imp = IJ.openImage( "http://imagej.net/images/blobs.gif" );
25+
// Get all default parameters
26+
final Cellpose4Parameters paramsCP4 = Cellpose4Parameters.builder()
27+
.build();
28+
// Run it
29+
final ImagePlus[] outputCP4 = Cellpose.cellpose4( imp, paramsCP4 );
30+
// Get the label image results
31+
final ImagePlus labelsCP4 = outputCP4[ 0 ];
32+
// Check the label image dimensions
33+
assertEquals( labelsCP4.getWidth(), imp.getWidth(), "CP4, 2D image: labels image dimension (width) is uncorrect" );
34+
assertEquals( labelsCP4.getHeight(), imp.getHeight(), "CP4, 2D image: labels image dimension (height) is uncorrect" );
35+
36+
// Check the label image statistics if it looks like a successfull run
37+
ImageStatistics stats = labelsCP4.getStatistics();
38+
assertFalse( stats.max <= 0, "CP4: No labels were found in blob image, with default parameters" );
39+
assertTrue( stats.max > 50, "CP4: Not enough labels were found in blob image, with default parameters" );
40+
}
41+
catch (Exception e)
42+
{
43+
throw e;
44+
}
45+
}
46+
47+
// Test different input image dimensions
48+
@Test
49+
public void defaultRunCPSAM_Image2DMultiChannels() throws Exception
50+
{
51+
52+
try
53+
{
54+
// 2D image with 3 channels
55+
final ImagePlus impRGB = IJ.openImage( "https://imagej.net/images/FluorescentCells.jpg" );
56+
// convert RGB to composite ImagePlus
57+
ImagePlus[] channels = ChannelSplitter.split( impRGB );
58+
final ImagePlus imp = RGBStackMerge.mergeChannels(channels, false);
59+
// Get all default parameters
60+
final Cellpose4Parameters paramsCP4 = Cellpose4Parameters.builder()
61+
.build();
62+
// Run it
63+
final ImagePlus[] outputCP4 = Cellpose.cellpose4( imp, paramsCP4 );
64+
// Get the label image results
65+
final ImagePlus labelsCP4 = outputCP4[ 0 ];
66+
// Check the label image dimensions
67+
assertEquals( imp.getWidth(), labelsCP4.getWidth(), "CP4, 2D image: labels image dimension (width) is uncorrect" );
68+
assertEquals( imp.getHeight(), labelsCP4.getHeight(), "CP4, 2D image: labels image dimension (height) is uncorrect" );
69+
70+
// Check the label image statistics if it looks like a successfull run
71+
ImageStatistics stats = labelsCP4.getStatistics();
72+
assertFalse( stats.max <= 0, "CP4: No labels were found in blob image, with default parameters" );
73+
assertTrue( stats.max > 3, "CP4: Not enough labels were found in blob image, with default parameters" );
74+
}
75+
catch (Exception e)
76+
{
77+
throw e;
78+
}
79+
}
80+
81+
@Test
82+
public void defaultRunCPSAM_Image3DMultiChannels() throws Exception
83+
{
84+
// New image
85+
try
86+
{
87+
// 4D image with channels, Z, time
88+
final ImagePlus stack = IJ.openImage( "https://imagej.net/images/mitosis.tif" );
89+
// Keep only one time point
90+
ImagePlus imp = new Duplicator().run(
91+
stack,
92+
1, stack.getNChannels(),
93+
1, stack.getNSlices(),
94+
3, 3
95+
);
96+
// Get all default parameters
97+
final Cellpose4Parameters paramsCP4 = Cellpose4Parameters.builder()
98+
.chan0(0)
99+
.chan1(null)
100+
.do3D( false )
101+
.stitchThreshold( 0.1 )
102+
.build();
103+
// Run it
104+
final ImagePlus[] outputCP4 = Cellpose.cellpose4( imp, paramsCP4 );
105+
// Get the label image results
106+
final ImagePlus labelsCP4 = outputCP4[ 0 ];
107+
// Check the label image dimensions
108+
assertEquals( imp.getWidth(), labelsCP4.getWidth(), "CP4, 3D+chan image: labels image dimension (width) is uncorrect" );
109+
assertEquals( imp.getHeight(), labelsCP4.getHeight(), "CP4, 3D+chanD image: labels image dimension (height) is uncorrect" );
110+
111+
// Check the label image statistics if it looks like a successfull run
112+
ImageStatistics stats = labelsCP4.getStatistics();
113+
assertFalse( stats.max <= 0, "CP4: No labels were found in 4D image, with default parameters" );
114+
assertTrue( stats.max == 1, "CP4: Too much labels were found in 4D image, with default parameters" );
115+
}
116+
catch (Exception e)
117+
{
118+
throw e;
119+
}
120+
}
121+
122+
}

0 commit comments

Comments
 (0)