Skip to content

Commit 01a16f9

Browse files
Fix bugs in the dataset source package
1 parent f9c8614 commit 01a16f9

4 files changed

Lines changed: 185 additions & 1 deletion

File tree

LibPeakaboo/src/main/java/org/peakaboo/dataset/source/model/components/datasize/SimpleDataSize.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Coord<Integer> getDataDimensions() {
3232
@Override
3333
public Coord<Integer> getDataCoordinatesAtIndex(int index) throws IndexOutOfBoundsException {
3434
int cx = index % dataWidth;
35-
int cy = (index - cx) / dataHeight;
35+
int cy = (index - cx) / dataWidth;
3636
return new Coord<>(cx, cy);
3737
}
3838

LibPeakaboo/src/main/java/org/peakaboo/dataset/source/model/components/scandata/analysis/DataSourceAnalysis.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public int scanCount() {
7676

7777
public static DataSourceAnalysis merge(List<Analysis> analyses) {
7878
DataSourceAnalysis result = new DataSourceAnalysis();
79+
80+
// Remove any empty analyses and exit fast if none are left after
81+
analyses = analyses.stream().filter(a -> a.scanCount() > 0).toList();
82+
if (analyses.size() == 0) { return result; }
83+
7984
result.init(analyses.get(0).channelsPerScan());
8085
for (var analysis : analyses) {
8186
SpectrumCalculations.addLists_inplace(result.summedSpectrum, analysis.summedPlot());
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.peakaboo.dataset.source.model.components.datasize;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.peakaboo.framework.accent.Coord;
6+
7+
public class SimpleDataSizeTest {
8+
9+
private SimpleDataSize makeSize(int width, int height) {
10+
SimpleDataSize size = new SimpleDataSize();
11+
size.setDataWidth(width);
12+
size.setDataHeight(height);
13+
return size;
14+
}
15+
16+
@Test
17+
public void testNonSquareWide() {
18+
// 5 columns x 3 rows
19+
SimpleDataSize size = makeSize(5, 3);
20+
21+
// index 0 => (0,0)
22+
Coord<Integer> c0 = size.getDataCoordinatesAtIndex(0);
23+
Assert.assertEquals(0, (int) c0.x);
24+
Assert.assertEquals(0, (int) c0.y);
25+
26+
// index 4 => (4,0) — end of first row
27+
Coord<Integer> c4 = size.getDataCoordinatesAtIndex(4);
28+
Assert.assertEquals(4, (int) c4.x);
29+
Assert.assertEquals(0, (int) c4.y);
30+
31+
// index 5 => (0,1) — start of second row
32+
Coord<Integer> c5 = size.getDataCoordinatesAtIndex(5);
33+
Assert.assertEquals(0, (int) c5.x);
34+
Assert.assertEquals(1, (int) c5.y);
35+
36+
// index 11 => (1,2)
37+
Coord<Integer> c11 = size.getDataCoordinatesAtIndex(11);
38+
Assert.assertEquals(1, (int) c11.x);
39+
Assert.assertEquals(2, (int) c11.y);
40+
41+
// index 14 => (4,2) — last element
42+
Coord<Integer> c14 = size.getDataCoordinatesAtIndex(14);
43+
Assert.assertEquals(4, (int) c14.x);
44+
Assert.assertEquals(2, (int) c14.y);
45+
}
46+
47+
@Test
48+
public void testNonSquareTall() {
49+
// 3 columns x 5 rows
50+
SimpleDataSize size = makeSize(3, 5);
51+
52+
// index 4 => (1,1)
53+
Coord<Integer> c4 = size.getDataCoordinatesAtIndex(4);
54+
Assert.assertEquals(1, (int) c4.x);
55+
Assert.assertEquals(1, (int) c4.y);
56+
57+
// index 14 => (2,4) — last element
58+
Coord<Integer> c14 = size.getDataCoordinatesAtIndex(14);
59+
Assert.assertEquals(2, (int) c14.x);
60+
Assert.assertEquals(4, (int) c14.y);
61+
}
62+
63+
@Test
64+
public void testSquare() {
65+
SimpleDataSize size = makeSize(4, 4);
66+
67+
Coord<Integer> c5 = size.getDataCoordinatesAtIndex(5);
68+
Assert.assertEquals(1, (int) c5.x);
69+
Assert.assertEquals(1, (int) c5.y);
70+
71+
Coord<Integer> c15 = size.getDataCoordinatesAtIndex(15);
72+
Assert.assertEquals(3, (int) c15.x);
73+
Assert.assertEquals(3, (int) c15.y);
74+
}
75+
76+
@Test
77+
public void testRoundTrip() {
78+
// For every index in a non-square grid, verify index -> coords -> index
79+
SimpleDataSize size = makeSize(5, 3);
80+
int total = 5 * 3;
81+
for (int i = 0; i < total; i++) {
82+
Coord<Integer> c = size.getDataCoordinatesAtIndex(i);
83+
int reconstructed = c.y * 5 + c.x;
84+
Assert.assertEquals("Round-trip failed for index " + i, i, reconstructed);
85+
}
86+
}
87+
88+
@Test
89+
public void testDimensions() {
90+
SimpleDataSize size = makeSize(7, 3);
91+
Coord<Integer> dims = size.getDataDimensions();
92+
Assert.assertEquals(7, (int) dims.x);
93+
Assert.assertEquals(3, (int) dims.y);
94+
}
95+
96+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.peakaboo.dataset.source.model.components.scandata.analysis;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.peakaboo.framework.cyclops.spectrum.ArraySpectrum;
9+
import org.peakaboo.framework.cyclops.spectrum.Spectrum;
10+
11+
public class DataSourceAnalysisTest {
12+
13+
@Test
14+
public void testMergeEmptyList() {
15+
DataSourceAnalysis result = DataSourceAnalysis.merge(new ArrayList<>());
16+
Assert.assertEquals(-1, result.channelsPerScan());
17+
Assert.assertEquals(0, result.scanCount());
18+
}
19+
20+
@Test
21+
public void testMergeAllZeroScanCount() {
22+
// Analyses that never processed any data
23+
DataSourceAnalysis a = new DataSourceAnalysis();
24+
DataSourceAnalysis b = new DataSourceAnalysis();
25+
DataSourceAnalysis result = DataSourceAnalysis.merge(List.of(a, b));
26+
Assert.assertEquals(-1, result.channelsPerScan());
27+
Assert.assertEquals(0, result.scanCount());
28+
}
29+
30+
@Test
31+
public void testMergeTwoAnalyses() {
32+
DataSourceAnalysis a = new DataSourceAnalysis();
33+
a.process(new ArraySpectrum(new float[]{1.0f, 2.0f, 3.0f}, false));
34+
a.process(new ArraySpectrum(new float[]{4.0f, 5.0f, 6.0f}, false));
35+
36+
DataSourceAnalysis b = new DataSourceAnalysis();
37+
b.process(new ArraySpectrum(new float[]{10.0f, 20.0f, 30.0f}, false));
38+
39+
DataSourceAnalysis result = DataSourceAnalysis.merge(List.of(a, b));
40+
41+
Assert.assertEquals(3, result.channelsPerScan());
42+
Assert.assertEquals(3, result.scanCount());
43+
44+
// Summed: (1+4+10, 2+5+20, 3+6+30) = (15, 27, 39)
45+
Spectrum summed = new ArraySpectrum(result.summedPlot());
46+
Assert.assertEquals(15.0f, summed.get(0), 1e-5f);
47+
Assert.assertEquals(27.0f, summed.get(1), 1e-5f);
48+
Assert.assertEquals(39.0f, summed.get(2), 1e-5f);
49+
50+
// Maximum: max(4,10), max(5,20), max(6,30) = (10, 20, 30)
51+
Spectrum max = new ArraySpectrum(result.maximumPlot());
52+
Assert.assertEquals(10.0f, max.get(0), 1e-5f);
53+
Assert.assertEquals(20.0f, max.get(1), 1e-5f);
54+
Assert.assertEquals(30.0f, max.get(2), 1e-5f);
55+
56+
// Max intensity: max(6, 30) = 30
57+
Assert.assertEquals(30.0f, result.maximumIntensity(), 1e-5f);
58+
}
59+
60+
@Test
61+
public void testSingleAnalysisProcess() {
62+
DataSourceAnalysis a = new DataSourceAnalysis();
63+
a.process(new ArraySpectrum(new float[]{2.0f, 4.0f}, false));
64+
a.process(new ArraySpectrum(new float[]{6.0f, 8.0f}, false));
65+
66+
Assert.assertEquals(2, a.channelsPerScan());
67+
Assert.assertEquals(2, a.scanCount());
68+
69+
// Average: (8/2, 12/2) = (4, 6)
70+
Spectrum avg = new ArraySpectrum(a.averagePlot());
71+
Assert.assertEquals(4.0f, avg.get(0), 1e-5f);
72+
Assert.assertEquals(6.0f, avg.get(1), 1e-5f);
73+
}
74+
75+
@Test
76+
public void testNullSpectrumIgnored() {
77+
DataSourceAnalysis a = new DataSourceAnalysis();
78+
a.process(null);
79+
Assert.assertEquals(-1, a.channelsPerScan());
80+
Assert.assertEquals(0, a.scanCount());
81+
}
82+
83+
}

0 commit comments

Comments
 (0)