forked from fiji/SNT
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPersistenceAnalyzerTest.java
122 lines (109 loc) · 3.91 KB
/
PersistenceAnalyzerTest.java
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
/*-
* #%L
* Fiji distribution of ImageJ for the life sciences.
* %%
* Copyright (C) 2010 - 2025 Fiji developers.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package sc.fiji.snt;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeNotNull;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import sc.fiji.snt.analysis.PersistenceAnalyzer;
import sc.fiji.snt.analysis.TreeStatistics;
import sc.fiji.snt.util.SWCPoint;
/**
* Tests for {@link PersistenceAnalyzer}
*/
public class PersistenceAnalyzerTest {
private final double precision = 0.0001;
private Tree tree;
private PersistenceAnalyzer pAnalyzer;
private final List<String> allDescriptors = PersistenceAnalyzer.getDescriptors();
private TreeStatistics tAnalyzer;
@Before
public void setUp() throws Exception {
tree = new SNTService().demoTrees().get(0);
pAnalyzer = new PersistenceAnalyzer(tree);
tAnalyzer = new TreeStatistics(tree);
assumeNotNull(tree);
}
@Test
public void testDiagram() {
// Tests basic validity of the Persistence Diagram for each descriptor function.
// TODO devise proper correctness tests for each descriptor.
final int numTips = tAnalyzer.getTips().size();
for (final String descriptor : allDescriptors) {
final List<List<Double>> diagram = pAnalyzer.getDiagram(descriptor);
assertEquals("Number of points in diagram", numTips, diagram.size());
for (final List<Double> point : diagram) {
assertTrue("Two non-negative values per point in diagram",
point.size() == 2 && point.get(0) >= 0 && point.get(1) >= 0);
}
}
}
@Test
public void testBarcode() {
// Use geodesic descriptor since the sum of all intervals equals total cable length.
List<Double> barcode = pAnalyzer.getBarcode("geodesic");
final double cableLength = tAnalyzer.getCableLength();
double sumIntervals = 0d;
for (final double interval : barcode) {
sumIntervals += interval;
}
assertEquals("Barcode: summed intervals", cableLength, sumIntervals, precision);
/*
* For the other descriptors it should be sufficient to demonstrate non-negative
* interval lengths
*/
for (final String descriptor : allDescriptors) {
barcode = pAnalyzer.getBarcode(descriptor);
for (final double interval : barcode) {
assertTrue("Non-negative intervals", interval >= 0);
}
}
}
@Test
public void testDiagramNodes() {
final int numTips = tAnalyzer.getTips().size();
for (final String descriptor : allDescriptors) {
final List<List<SWCPoint>> diagramNodes = pAnalyzer.getDiagramNodes(descriptor);
assertEquals("Number of points in diagram", numTips, diagramNodes.size());
for (final List<SWCPoint> point : diagramNodes) {
assertTrue("Two SWCPoint objects per point in diagram",
point.size() == 2 && point.get(0) instanceof SWCPoint && point.get(1) instanceof SWCPoint);
}
}
}
@Test
public void testLandscape() {
for (final String descriptor : allDescriptors) {
final double[] landscape = pAnalyzer.getLandscape(descriptor, 5, 100);
assertEquals("Landscape size", 500, landscape.length);
double minVal = 0;
for (int i = 0; i < landscape.length; i++) {
if (landscape[i] < minVal) {
minVal = landscape[i];
}
}
assertTrue("Landscape: no negative values", minVal >= 0);
}
}
}