-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathN5vAxisPermutationTests.java
More file actions
176 lines (137 loc) · 5.54 KB
/
Copy pathN5vAxisPermutationTests.java
File metadata and controls
176 lines (137 loc) · 5.54 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
package org.janelia.saalfeldlab.n5.bdv;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.ArrayUtils;
import org.janelia.saalfeldlab.n5.N5URI;
import org.janelia.saalfeldlab.n5.N5Writer;
import org.janelia.saalfeldlab.n5.ui.DataSelection;
import org.janelia.saalfeldlab.n5.universe.N5DatasetDiscoverer;
import org.janelia.saalfeldlab.n5.universe.N5Factory;
import org.janelia.saalfeldlab.n5.universe.N5TreeNode;
import org.janelia.saalfeldlab.n5.universe.StorageFormat;
import org.janelia.saalfeldlab.n5.universe.metadata.N5Metadata;
import org.janelia.saalfeldlab.n5.universe.metadata.NgffTests;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import bdv.cache.SharedQueue;
import bdv.tools.brightness.ConverterSetup;
import bdv.util.BdvOptions;
import bdv.viewer.Source;
import bdv.viewer.SourceAndConverter;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.NumericType;
public class N5vAxisPermutationTests {
public static final double EPS = 1e-6;
public static final String[] defaultAxes = new String[]{"x", "y", "c", "z", "t"};
private URI containerUri;
@Before
public void before() {
System.setProperty("java.awt.headless", "true");
try {
containerUri = new File(tempN5PathName()).getCanonicalFile().toURI();
} catch (final IOException e) {}
}
@After
public void after() {
final N5Writer n5 = new N5Factory().openWriter(containerUri.toString());
n5.remove();
}
private static String tempN5PathName() {
try {
final File tmpFile = Files.createTempDirectory("n5-viewer-ngff-test-").toFile();
tmpFile.deleteOnExit();
return tmpFile.getCanonicalPath();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
protected String tempN5Location() throws URISyntaxException {
final String basePath = new File(tempN5PathName()).toURI().normalize().getPath();
return new URI("file", null, basePath, null).toString();
}
@Test
public void testPermutations() throws IOException {
final N5Writer zarr = new N5Factory().openWriter(StorageFormat.ZARR2, containerUri.toString());
// don't check every axis permutation, but some relevant ones, and some strange ones
final String[] names = new String[]{
"xyz", "zyx", "yzx",
"xyc", "xcy", "cyx",
"xyt", "xty", "tyx",
"xyzt", "xtyz", "zxty", "tyzx",
"xyczt", "xyzct", "xytcz", "tzcyx", "ctzxy"
};
// check both c- and f-order storage
for (final String axes : names) {
writeAndTest(zarr, axes + "_c");
writeAndTest(zarr, axes + "_f");
}
}
protected void writeAndTest(final N5Writer zarr, final String dset) throws IOException {
writeAndTest(zarr, dset, NgffTests.isCOrderFromName(dset), NgffTests.permutationFromName(dset));
}
protected <T extends NumericType<T> & NativeType<T>> void writeAndTest(final N5Writer zarr, final String dset, final boolean cOrder, final int[] p)
throws IOException {
// write
NgffTests.writePermutedAxes(zarr, baseName(p, cOrder), cOrder, p);
final String dstNrm = N5URI.normalizeGroupPath(dset);
// read
final N5TreeNode node = N5DatasetDiscoverer.discover(zarr);
final Stream<N5Metadata> metaStream = N5TreeNode.flattenN5Tree(node)
.filter(x -> N5URI.normalizeGroupPath(x.getPath()).equals(dstNrm))
.map(x -> {
return (N5Metadata)x.getMetadata();
});
final DataSelection selection = new DataSelection(zarr, Collections.singletonList(metaStream.findFirst().get()));
final SharedQueue sharedQueue = new SharedQueue(1);
final List<ConverterSetup> converterSetups = new ArrayList<>();
final List<SourceAndConverter<T>> sourcesAndConverters = new ArrayList<>();
final BdvOptions options = BdvOptions.options().frameTitle("N5 Viewer");
final int numTimepoints = N5Viewer.buildN5Sources(
zarr,
selection,
sharedQueue,
converterSetups,
sourcesAndConverters,
options);
assertTrue("sources found", sourcesAndConverters.size() > 0);
final Source<T> src = sourcesAndConverters.get(0).getSpimSource();
final RandomAccessibleInterval<T> rai = src.getSource(0, 0);
final long[] dims = rai.dimensionsAsLongArray();
final AffineTransform3D tform = new AffineTransform3D();
src.getSourceTransform(0, 0, tform);
// test
assertEquals(dset + "size x", NgffTests.NX, dims[0]);
assertEquals(dset + "size y", NgffTests.NY, dims[1]);
assertEquals(dset + "res x", NgffTests.RX, tform.get(0, 0), EPS);
assertEquals(dset + "res y", NgffTests.RY, tform.get(1, 1), EPS);
final char[] axes = dset.split("_")[0].toCharArray();
if (ArrayUtils.contains(axes, NgffTests.Z)) {
assertEquals(dset + "n slices", NgffTests.NZ, dims[2]);
assertEquals(dset + "res z", NgffTests.RZ, tform.get(2, 2), EPS);
}
if (ArrayUtils.contains(axes, NgffTests.C)) {
assertEquals(dset + "n channels", NgffTests.NC, sourcesAndConverters.size());
}
if (ArrayUtils.contains(axes, NgffTests.T)) {
assertEquals(dset + "n timepoints", NgffTests.NT, numTimepoints);
}
}
private static String baseName(final int[] p, final boolean cOrder) {
final String suffix = cOrder ? "_c" : "_f";
return Arrays.stream(p).mapToObj(i -> defaultAxes[i]).collect(Collectors.joining()) + suffix;
}
}