forked from imagej/imagej-scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorialsScriptTest.java
More file actions
279 lines (220 loc) · 10.1 KB
/
TutorialsScriptTest.java
File metadata and controls
279 lines (220 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*-
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2015 - 2022 Board of Regents of the University of
* Wisconsin-Madison.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package net.imagej.scripting;
import io.scif.services.DatasetIOService;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import javax.script.ScriptException;
import net.imagej.Dataset;
import net.imagej.ImgPlus;
import net.imglib2.type.numeric.RealType;
import org.junit.Assert;
import org.junit.Test;
import org.scijava.plugin.Parameter;
import org.scijava.script.ScriptModule;
/**
* @author Hadrien Mary
*/
public class TutorialsScriptTest extends AbstractScriptTest {
@Parameter
private DatasetIOService datasetIOService;
@Test
public <T extends RealType<T>> void testCropConfocalSeries()
throws InterruptedException, ExecutionException, IOException,
URISyntaxException, FileNotFoundException, ScriptException
{
final Map<String, Object> parameters = new HashMap<>();
final String testPath =
"8bit-signed&pixelType=int8&axes=X,Y,Channel,Z,&lengths=400,400,2,25.fake";
final Dataset data = datasetIOService.open(testPath);
parameters.put("data", data);
parameters.put("sigma1", 4.0);
parameters.put("sigma2", 1.0);
final File scriptFile = new File(getClass().getResource(
"/script_templates/Tutorials/Crop_Confocal_Series.py").toURI());
final ScriptModule m = scriptService.run(scriptFile, true, parameters)
.get();
@SuppressWarnings("unchecked")
final ImgPlus<T> output = (ImgPlus<T>) m.getOutput("c0");
final double[] expected = { 0.0, -128.0, -128.0, -128.0, -128.0, -128.0,
-128.0, -128.0, -128.0, -128.0, -128.0, -128.0, -128.0, -128.0, -128.0,
-128.0, -128.0, -128.0, -128.0, -128.0, -128.0, -88.0 };
assertSamplesEqual(output, 19, expected);
}
// @Test
public void testFindTemplate() throws InterruptedException,
ExecutionException, IOException, URISyntaxException, FileNotFoundException,
ScriptException
{
final Map<String, Object> parameters = new HashMap<>();
final String testPath1 =
"8bit-signed&pixelType=int8&axes=X,Y,&lengths=400,400.fake";
final String testPath2 =
"8bit-signed&pixelType=int8&axes=X,Y,&lengths=40,40.fake";
final Dataset data1 = datasetIOService.open(testPath1);
final Dataset data2 = datasetIOService.open(testPath2);
parameters.put("image", data1);
parameters.put("template", data2);
final File scriptFile = new File(getClass().getResource(
"/script_templates/Tutorials/Find_Template.py").toURI());
scriptService.run(scriptFile, true, parameters).get();
}
@Test
@SuppressWarnings("unchecked")
public <T extends RealType<T>> void testCreateAndConvolvePoints()
throws InterruptedException, ExecutionException, IOException,
URISyntaxException, FileNotFoundException, ScriptException
{
final Map<String, Object> parameters = new HashMap<>();
parameters.put("xSize", 128);
parameters.put("ySize", 128);
parameters.put("zSize", 24);
final File scriptFile = new File(getClass().getResource(
"/script_templates/Tutorials/Create_and_Convolve_Points.py").toURI());
final ScriptModule m = scriptService.run(scriptFile, true, parameters)
.get();
final ImgPlus<T> output1 = (ImgPlus<T>) m.getOutput("phantom");
final double[] expected1 = new double[] { 255.0, 0.0 };
assertSamplesEqual(output1, 102432, expected1);
final ImgPlus<T> output2 = (ImgPlus<T>) m.getOutput("convolved");
final double[] expected2 = { -4.118816399056868E-9, -2.6888646864620114E-9,
-7.762487319595834E-10, 1.5811233433637994E-9, 4.294427036199977E-9,
7.265440249426547E-9, 1.0367559255541892E-8, 1.346087241671512E-8,
1.6421539683619812E-8, 1.9126369821265143E-8, 2.14812700960465E-8,
2.3422717987386932E-8, 2.490532402532608E-8, 2.5911424117452952E-8,
2.6449438195186303E-8, 2.6547994025349908E-8, 2.6253854201740978E-8,
2.561551504243198E-8, 2.470525828357495E-8, 2.357626804894153E-8,
2.228407325333137E-8 };
assertSamplesEqual(output2, 0, expected2);
}
@Test
@SuppressWarnings("unchecked")
public <T extends RealType<T>> void testOpsThresholdMeasure()
throws InterruptedException, ExecutionException, IOException,
URISyntaxException, FileNotFoundException, ScriptException
{
final String testPath =
"8bit-signed&pixelType=int8&axes=X,Y,&lengths=100,100.fake";
final Dataset data = datasetIOService.open(testPath);
final Map<String, Object> parameters = new HashMap<>();
parameters.put("inputData", data);
parameters.put("sigma", 3);
final File scriptFile = new File(getClass().getResource(
"/script_templates/Tutorials/Ops_Threshold_Measure.py").toURI());
final ScriptModule m = scriptService.run(scriptFile, true, parameters)
.get();
final ImgPlus<T> output1 = (ImgPlus<T>) m.getOutput("logFiltered");
final double[] expected1 = { 1.5894572769070692E-8, -3.1789145538141383E-8,
0.0, 3.1789145538141383E-8, 5.563100202721216E-8, 3.973643103449831E-8,
1.5894572769070692E-8, -3.1789145538141383E-8, 0.0, 3.1789145538141383E-8,
-5.563100202721216E-8, -1.2715658215256553E-7, -1.1126200405442432E-7,
-4.7683716530855236E-8, 0.05621512606739998, 0.1999766081571579,
0.44583258032798767, 0.6688981652259827, 0.5973716974258423,
0.06918635219335556, -0.6852334141731262 };
assertSamplesEqual(output1, 0, expected1);
final ImgPlus<T> output2 = (ImgPlus<T>) m.getOutput("thresholded");
final double[] expected2 = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0 };
assertSamplesEqual(output2, 0, expected2);
}
@Test
@SuppressWarnings("unchecked")
public <T extends RealType<T>> void testProjections()
throws InterruptedException, ExecutionException, IOException,
URISyntaxException, FileNotFoundException, ScriptException
{
final String testPath =
"8bit-signed&pixelType=int8&axes=X,Y,Z,&lengths=100,100,100.fake";
final Dataset data = datasetIOService.open(testPath);
final Map<String, Object> parameters = new HashMap<>();
parameters.put("data", data);
final File scriptFile = new File(getClass().getResource(
"/script_templates/Tutorials/Projections.py").toURI());
final ScriptModule m = scriptService.run(scriptFile, true, parameters)
.get();
final ImgPlus<T> output1 = (ImgPlus<T>) m.getOutput("maxProjection");
final double[] expected1 = { 99.0, 99.0, 99.0, -29.0, -29.0, -29.0, -29.0,
-29.0, -29.0, -29.0, -29.0, -29.0, -29.0, -98.0 };
assertSamplesEqual(output1, 17, expected1);
final ImgPlus<T> output2 = (ImgPlus<T>) m.getOutput("sumProjection");
final double[] expected2 = { 4950.0, 4950.0, 4950.0, -7850.0, -7850.0,
-7850.0, -7850.0, -7850.0, -7850.0, -7850.0, -7850.0, -7850.0, -7850.0,
-9800.0 };
assertSamplesEqual(output2, 17, expected2);
}
@Test
@SuppressWarnings("unchecked")
public <T extends RealType<T>> void testSimpleConvolution()
throws InterruptedException, ExecutionException, IOException,
URISyntaxException, FileNotFoundException, ScriptException
{
final String testPath =
"8bit-signed&pixelType=int8&axes=X,Y,&lengths=100,100.fake";
final Dataset data = datasetIOService.open(testPath);
final Map<String, Object> parameters = new HashMap<>();
parameters.put("inputData", data);
final File scriptFile = new File(getClass().getResource(
"/script_templates/Tutorials/Simple_Convolution.groovy").toURI());
final ScriptModule m = scriptService.run(scriptFile, true, parameters)
.get();
final ImgPlus<T> output1 = (ImgPlus<T>) m.getOutput("filtered");
final double[] expected1 = { -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0,
-2.0, -2.0, -2.0, -3.0, -5.0, -7.0, -10.0, -15.0, -21.0, -29.0, -38.0,
-48.0, -59.0 };
assertSamplesEqual(output1, 0, expected1);
final ImgPlus<T> output2 = (ImgPlus<T>) m.getOutput("result");
final double[] expected2 = { 4.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
2.0, 0.0, 1.0, 5.0, 6.0, 7.0, 14.0, 18.0, 27.0, 36.0, 46.0, 61.0 };
assertSamplesEqual(output2, 0, expected2);
}
@Test
@SuppressWarnings("unchecked")
public <T extends RealType<T>> void testSlicewiseThreshold()
throws InterruptedException, ExecutionException, IOException,
URISyntaxException, FileNotFoundException, ScriptException
{
final String testPath =
"8bit-signed&pixelType=int8&axes=X,Y,Z,&lengths=100,100,100.fake";
final Dataset data = datasetIOService.open(testPath);
final Map<String, Object> parameters = new HashMap<>();
parameters.put("data", data);
final File scriptFile = new File(getClass().getResource(
"/script_templates/Tutorials/Slicewise_Threshold.py").toURI());
final ScriptModule m = scriptService.run(scriptFile, true, parameters)
.get();
final ImgPlus<T> output1 = (ImgPlus<T>) m.getOutput("thresholdedPlus");
Assert.assertNotNull(output1);
}
}