|
| 1 | +import test from 'tape-catch'; |
| 2 | +import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData'; |
| 3 | +import vtkRTAnalyticSource from 'vtk.js/Sources/Filters/Sources/RTAnalyticSource'; |
| 4 | + |
| 5 | +test('Test vtkImageData instance', (t) => { |
| 6 | + t.ok(vtkImageData, 'Make sure the class definition exists'); |
| 7 | + const instance = vtkImageData.newInstance(); |
| 8 | + t.ok(instance); |
| 9 | + t.end(); |
| 10 | +}); |
| 11 | + |
| 12 | +test('Test vtkImageData histogram', (t) => { |
| 13 | + const spacing = 0.7; |
| 14 | + const size = 50; |
| 15 | + const compareFloat = (a, b) => Math.abs(a - b) < Number.EPSILON; |
| 16 | + |
| 17 | + const source = vtkRTAnalyticSource.newInstance(); |
| 18 | + source.setWholeExtent([0, size, 0, size, 0, size]); |
| 19 | + source.update(); |
| 20 | + |
| 21 | + const image = source.getOutputData(); |
| 22 | + image.setSpacing([spacing, spacing, spacing]); |
| 23 | + |
| 24 | + const bounds = image.getBounds(); |
| 25 | + const hist = image.computeHistogram(bounds); |
| 26 | + |
| 27 | + const baseline1 = { |
| 28 | + minimum: 9, |
| 29 | + maximum: 185, |
| 30 | + average: 64.65, |
| 31 | + variance: 782.87, |
| 32 | + sigma: 27.98, |
| 33 | + count: 132651, |
| 34 | + }; |
| 35 | + |
| 36 | + t.ok( |
| 37 | + hist.minimum === baseline1.minimum, |
| 38 | + 'computeHistogram return value test: minimum' |
| 39 | + ); |
| 40 | + t.ok( |
| 41 | + hist.maximum === baseline1.maximum, |
| 42 | + 'computeHistogram return value test: maximum' |
| 43 | + ); |
| 44 | + t.ok( |
| 45 | + compareFloat(hist.average.toFixed(2), baseline1.average), |
| 46 | + 'computeHistogram return value test: average' |
| 47 | + ); |
| 48 | + t.ok( |
| 49 | + compareFloat(hist.variance.toFixed(2), baseline1.variance), |
| 50 | + 'computeHistogram return value test: variance' |
| 51 | + ); |
| 52 | + t.ok( |
| 53 | + compareFloat(hist.sigma.toFixed(2), baseline1.sigma), |
| 54 | + 'computeHistogram return value test: sigma' |
| 55 | + ); |
| 56 | + t.ok( |
| 57 | + hist.count === baseline1.count, |
| 58 | + 'computeHistogram return value test: count' |
| 59 | + ); |
| 60 | + |
| 61 | + // masking function that ignores the bottom 10 and top 10 rows of voxels. |
| 62 | + const voxelFunc = (idx) => idx[0] > 9 && idx[0] < 40; |
| 63 | + |
| 64 | + const baseline2 = { |
| 65 | + minimum: 9, |
| 66 | + maximum: 173, |
| 67 | + average: 65.29, |
| 68 | + variance: 676.51, |
| 69 | + sigma: 26.01, |
| 70 | + count: 78030, |
| 71 | + }; |
| 72 | + |
| 73 | + const histWithMask = image.computeHistogram(bounds, voxelFunc); |
| 74 | + |
| 75 | + t.ok( |
| 76 | + histWithMask.minimum === baseline2.minimum && |
| 77 | + histWithMask.maximum === baseline2.maximum && |
| 78 | + compareFloat(histWithMask.average.toFixed(2), baseline2.average) && |
| 79 | + compareFloat(histWithMask.variance.toFixed(2), baseline2.variance) && |
| 80 | + compareFloat(histWithMask.sigma.toFixed(2), baseline2.sigma) && |
| 81 | + histWithMask.count === baseline2.count, |
| 82 | + 'computeHistogram test with masking function' |
| 83 | + ); |
| 84 | + |
| 85 | + const voxelFuncNone = (idx) => false; |
| 86 | + const baseline3 = { |
| 87 | + minimum: Infinity, |
| 88 | + maximum: -Infinity, |
| 89 | + average: 0, |
| 90 | + variance: 0, |
| 91 | + sigma: 0, |
| 92 | + count: 0, |
| 93 | + }; |
| 94 | + const histNone = image.computeHistogram(bounds, voxelFuncNone); |
| 95 | + |
| 96 | + t.ok( |
| 97 | + histNone.minimum === baseline3.minimum && |
| 98 | + histNone.maximum === baseline3.maximum && |
| 99 | + compareFloat(histNone.average.toFixed(2), baseline3.average) && |
| 100 | + compareFloat(histNone.variance.toFixed(2), baseline3.variance) && |
| 101 | + compareFloat(histNone.sigma.toFixed(2), baseline3.sigma) && |
| 102 | + histNone.count === baseline3.count, |
| 103 | + 'computeHistogram test with zero number of voxels that qualify' |
| 104 | + ); |
| 105 | + |
| 106 | + t.end(); |
| 107 | +}); |
0 commit comments