Skip to content

Commit 39cb136

Browse files
committed
Replace usage of approxEqual with isClose
approxEqual is deprecated, isClose is the replacement. We also update the CI to use GDC-12 which has a recent frontend, as gdc-11 has v2.076 which doesn't include `isClose`, but gdc-12 has `-Werror=deprecated` on by default.
1 parent f7a7cf0 commit 39cb136

File tree

10 files changed

+868
-858
lines changed

10 files changed

+868
-858
lines changed

.github/workflows/d.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ jobs:
1414
os: [ubuntu-latest, macos-latest]
1515
dc: [ldc-latest, dmd-latest]
1616
include:
17-
- os: ubuntu-latest
18-
dc: gdc
17+
- os: ubuntu-22.04
18+
dc: gdc-12
1919

2020
steps:
2121
- uses: actions/checkout@v2
@@ -27,18 +27,20 @@ jobs:
2727
compiler: ${{ matrix.dc }}
2828

2929
- name: Install GDC
30-
if: matrix.dc == 'gdc'
30+
if: matrix.dc == 'gdc-12'
3131
run: |
32-
sudo apt install gdc
32+
sudo apt-get install gdc-12
3333
wget https://github.com/dlang/dub/releases/download/v1.23.0/dub-v1.23.0-linux-x86_64.tar.gz
3434
tar xvf dub-v1.23.0-linux-x86_64.tar.gz
3535
3636
- name: 'Build & Test'
37+
if: matrix.dc != 'gdc-12'
3738
run: |
38-
export PATH=$PATH:`pwd` # for GDC build
39-
# Build the project, with its main file included, without unittests
4039
dub build
41-
# Build and run tests, as defined by `unittest` configuration
42-
# In this mode, `mainSourceFile` is excluded and `version (unittest)` are included
43-
# See https://dub.pm/package-format-json.html#configurations
4440
dub test
41+
42+
- name: 'Build & Test'
43+
if: matrix.dc == 'gdc-12'
44+
run: |
45+
./dub build --compiler=${{ matrix.dc }}
46+
./dub test --compiler=${{ matrix.dc }}

source/dstats/base.d

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,8 @@ unittest {
811811
// Values worked out by hand on paper. If you don't believe me, work
812812
// them out yourself.
813813
assert(auroc([4,5,6], [1,2,3]) == 1);
814-
assert(approxEqual(auroc([8,6,7,5,3,0,9], [3,6,2,4,3,6]), 0.6904762));
815-
assert(approxEqual(auroc([2,7,1,8,2,8,1,8], [3,1,4,1,5,9,2,6]), 0.546875));
814+
assert(isClose(auroc([8,6,7,5,3,0,9], [3,6,2,4,3,6]), 0.6904762, 1e-2, 1e-2));
815+
assert(isClose(auroc([2,7,1,8,2,8,1,8], [3,1,4,1,5,9,2,6]), 0.546875, 1e-2, 1e-2));
816816
}
817817

818818
///
@@ -848,8 +848,8 @@ unittest {
848848
assert(cast(uint) round(exp(logFactorial(7)))==5040);
849849
assert(cast(uint) round(exp(logFactorial(3)))==6);
850850
// Gamma branch.
851-
assert(approxEqual(logFactorial(12000), 1.007175584216837e5, 1e-14));
852-
assert(approxEqual(logFactorial(14000), 1.196610688711534e5, 1e-14));
851+
assert(isClose(logFactorial(12000), 1.007175584216837e5, 1e-14, 1e-2));
852+
assert(isClose(logFactorial(14000), 1.196610688711534e5, 1e-14, 1e-2));
853853
}
854854

855855
///Log of (n choose k).
@@ -1481,13 +1481,13 @@ unittest {
14811481
scope(exit) std.file.remove("NumericFileTestDeleteMe.txt");
14821482
auto myFile = File("NumericFileTestDeleteMe.txt");
14831483
auto rng = toNumericRange(myFile.byLine());
1484-
assert(approxEqual(rng.front, 3.14));
1484+
assert(isClose(rng.front, 3.14));
14851485
rng.popFront;
1486-
assert(approxEqual(rng.front, 2.71));
1486+
assert(isClose(rng.front, 2.71));
14871487
rng.popFront;
1488-
assert(approxEqual(rng.front, 8.67));
1488+
assert(isClose(rng.front, 8.67));
14891489
rng.popFront;
1490-
assert(approxEqual(rng.front, 362435));
1490+
assert(isClose(rng.front, 362435, 1e-2, 1e-2));
14911491
assert(!rng.empty);
14921492
rng.popFront;
14931493
assert(rng.empty);
@@ -1622,9 +1622,9 @@ version(scid) {
16221622
auto mat = [[1.0, 2, 3], [4.0, 7, 6], [7.0, 8, 9]];
16231623
auto toMat = [new double[3], new double[3], new double[3]];
16241624
invert(mat, toMat);
1625-
assert(approxEqual(toMat[0], [-0.625, -0.25, 0.375]));
1626-
assert(approxEqual(toMat[1], [-0.25, 0.5, -0.25]));
1627-
assert(approxEqual(toMat[2], [0.708333, -0.25, 0.041667]));
1625+
assert(isClose(toMat[0], [-0.625, -0.25, 0.375], 1e-2, 1e-2));
1626+
assert(isClose(toMat[1], [-0.25, 0.5, -0.25], 1e-2, 1e-2));
1627+
assert(isClose(toMat[2], [0.708333, -0.25, 0.041667], 1e-2, 1e-2));
16281628
}
16291629

16301630
void solve(DoubleMatrix mat, double[] vec) {
@@ -1685,12 +1685,12 @@ version(scid) {
16851685
auto mat = [[2.0, 1, -1], [-3.0, -1, 2], [-2.0, 1, 2]];
16861686
auto vec = [8.0, -11, -3];
16871687
solve(mat, vec);
1688-
assert(approxEqual(vec, [2, 3, -1]));
1688+
assert(isClose(vec, [2, 3, -1], 1e-2, 1e-2));
16891689

16901690
auto mat2 = [[1.0, 2, 3], [4.0, 7, 6], [7.0, 8, 9]];
16911691
auto vec2 = [8.0, 6, 7];
16921692
solve(mat2, vec2);
1693-
assert(approxEqual(vec2, [-3.875, -0.75, 4.45833]));
1693+
assert(isClose(vec2, [-3.875, -0.75, 4.45833], 1e-2, 1e-2));
16941694
}
16951695

16961696
// Cholesky decomposition functions adapted from Don Clugston's MathExtra

source/dstats/cor.d

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ if(doubleInput!(T) && doubleInput!(U)) {
134134
}
135135

136136
unittest {
137-
assert(approxEqual(pearsonCor([1,2,3,4,5][], [1,2,3,4,5][]).cor, 1));
138-
assert(approxEqual(pearsonCor([1,2,3,4,5][], [10.0, 8.0, 6.0, 4.0, 2.0][]).cor, -1));
139-
assert(approxEqual(pearsonCor([2, 4, 1, 6, 19][], [4, 5, 1, 3, 2][]).cor, -.2382314));
137+
assert(isClose(pearsonCor([1,2,3,4,5][], [1,2,3,4,5][]).cor, 1, 1e-2, 1e-2));
138+
assert(isClose(pearsonCor([1,2,3,4,5][], [10.0, 8.0, 6.0, 4.0, 2.0][]).cor, -1, 1e-2, 1e-2));
139+
assert(isClose(pearsonCor([2, 4, 1, 6, 19][], [4, 5, 1, 3, 2][]).cor, -.2382314, 1e-2, 1e-2));
140140

141141
// Make sure everything works with lowest common denominator range type.
142142
static struct Count {
@@ -156,7 +156,7 @@ unittest {
156156
Count a, b;
157157
a.upTo = 100;
158158
b.upTo = 100;
159-
assert(approxEqual(pearsonCor(a, b).cor, 1));
159+
assert(isClose(pearsonCor(a, b).cor, 1, 1e-2, 1e-2));
160160

161161
PearsonCor cor1 = pearsonCor([1,2,4][], [2,3,5][]);
162162
PearsonCor cor2 = pearsonCor([4,2,9][], [2,8,7][]);
@@ -165,11 +165,11 @@ unittest {
165165
cor1.put(cor2);
166166

167167
foreach(ti, elem; cor1.tupleof) {
168-
assert(approxEqual(elem, combined.tupleof[ti]));
168+
assert(isClose(elem, combined.tupleof[ti], 1e-2, 1e-2));
169169
}
170170

171-
assert(approxEqual(pearsonCor([1,2,3,4,5,6,7,8,9,10][],
172-
[8,6,7,5,3,0,9,3,6,2][]).cor, -0.4190758));
171+
assert(isClose(pearsonCor([1,2,3,4,5,6,7,8,9,10][],
172+
[8,6,7,5,3,0,9,3,6,2][]).cor, -0.4190758, 1e-2, 1e-2));
173173

174174
foreach(iter; 0..1000) {
175175
// Make sure results for the ILP-optimized and non-optimized versions
@@ -183,7 +183,7 @@ unittest {
183183
}
184184

185185
foreach(ti, elem; res1.tupleof) {
186-
assert(approxEqual(elem, res2.tupleof[ti]));
186+
assert(isClose(elem, res2.tupleof[ti], 1e-2, 1e-2));
187187
}
188188

189189
PearsonCor resCornerCase; // Test where one N is zero.
@@ -313,7 +313,7 @@ if(doubleInput!(T) && doubleInput!(U)) {
313313
}
314314

315315
unittest {
316-
assert(approxEqual(covariance([1,4,2,6,3].dup, [3,1,2,6,2].dup), 2.05));
316+
assert(isClose(covariance([1,4,2,6,3].dup, [3,1,2,6,2].dup), 2.05, 1e-2, 1e-2));
317317
}
318318

319319
/**Spearman's rank correlation. Non-parametric. This is essentially the
@@ -358,18 +358,18 @@ is(typeof(input2.front < input2.front) == bool)) {
358358

359359
unittest {
360360
//Test against a few known values.
361-
assert(approxEqual(spearmanCor([1,2,3,4,5,6].dup, [3,1,2,5,4,6].dup), 0.77143));
362-
assert(approxEqual(spearmanCor([3,1,2,5,4,6].dup, [1,2,3,4,5,6].dup ), 0.77143));
363-
assert(approxEqual(spearmanCor([3,6,7,35,75].dup, [1,63,53,67,3].dup), 0.3));
364-
assert(approxEqual(spearmanCor([1,63,53,67,3].dup, [3,6,7,35,75].dup), 0.3));
365-
assert(approxEqual(spearmanCor([1.5,6.3,7.8,4.2,1.5].dup, [1,63,53,67,3].dup), .56429));
366-
assert(approxEqual(spearmanCor([1,63,53,67,3].dup, [1.5,6.3,7.8,4.2,1.5].dup), .56429));
367-
assert(approxEqual(spearmanCor([1.5,6.3,7.8,7.8,1.5].dup, [1,63,53,67,3].dup), .79057));
368-
assert(approxEqual(spearmanCor([1,63,53,67,3].dup, [1.5,6.3,7.8,7.8,1.5].dup), .79057));
369-
assert(approxEqual(spearmanCor([1.5,6.3,7.8,6.3,1.5].dup, [1,63,53,67,3].dup), .63246));
370-
assert(approxEqual(spearmanCor([1,63,53,67,3].dup, [1.5,6.3,7.8,6.3,1.5].dup), .63246));
371-
assert(approxEqual(spearmanCor([3,4,1,5,2,1,6,4].dup, [1,3,2,6,4,2,6,7].dup), .6829268));
372-
assert(approxEqual(spearmanCor([1,3,2,6,4,2,6,7].dup, [3,4,1,5,2,1,6,4].dup), .6829268));
361+
assert(isClose(spearmanCor([1,2,3,4,5,6].dup, [3,1,2,5,4,6].dup), 0.77143, 1e-2, 1e-2));
362+
assert(isClose(spearmanCor([3,1,2,5,4,6].dup, [1,2,3,4,5,6].dup ), 0.77143, 1e-2, 1e-2));
363+
assert(isClose(spearmanCor([3,6,7,35,75].dup, [1,63,53,67,3].dup), 0.3, 1e-2, 1e-2));
364+
assert(isClose(spearmanCor([1,63,53,67,3].dup, [3,6,7,35,75].dup), 0.3, 1e-2, 1e-2));
365+
assert(isClose(spearmanCor([1.5,6.3,7.8,4.2,1.5].dup, [1,63,53,67,3].dup), .56429, 1e-2, 1e-2));
366+
assert(isClose(spearmanCor([1,63,53,67,3].dup, [1.5,6.3,7.8,4.2,1.5].dup), .56429, 1e-2, 1e-2));
367+
assert(isClose(spearmanCor([1.5,6.3,7.8,7.8,1.5].dup, [1,63,53,67,3].dup), .79057, 1e-2, 1e-2));
368+
assert(isClose(spearmanCor([1,63,53,67,3].dup, [1.5,6.3,7.8,7.8,1.5].dup), .79057, 1e-2, 1e-2));
369+
assert(isClose(spearmanCor([1.5,6.3,7.8,6.3,1.5].dup, [1,63,53,67,3].dup), .63246, 1e-2, 1e-2));
370+
assert(isClose(spearmanCor([1,63,53,67,3].dup, [1.5,6.3,7.8,6.3,1.5].dup), .63246, 1e-2, 1e-2));
371+
assert(isClose(spearmanCor([3,4,1,5,2,1,6,4].dup, [1,3,2,6,4,2,6,7].dup), .6829268, 1e-2, 1e-2));
372+
assert(isClose(spearmanCor([1,3,2,6,4,2,6,7].dup, [3,4,1,5,2,1,6,4].dup), .6829268, 1e-2, 1e-2));
373373
uint[] one = new uint[1000], two = new uint[1000];
374374
foreach(i; 0..100) { //Further sanity checks for things like commutativity.
375375
size_t lowerBound = uniform(0, one.length);
@@ -396,10 +396,10 @@ unittest {
396396
two[lowerBound..upperBound].reverse();
397397
double sFive =
398398
spearmanCor(one[lowerBound..upperBound], two[lowerBound..upperBound]);
399-
assert(approxEqual(sOne, sTwo) || (isNaN(sOne) && isNaN(sTwo)));
400-
assert(approxEqual(sTwo, sThree) || (isNaN(sThree) && isNaN(sTwo)));
401-
assert(approxEqual(sThree, sFour) || (isNaN(sThree) && isNaN(sFour)));
402-
assert(approxEqual(sFour, sFive) || (isNaN(sFour) && isNaN(sFive)));
399+
assert(isClose(sOne, sTwo, 1e-2, 1e-2) || (isNaN(sOne) && isNaN(sTwo)));
400+
assert(isClose(sTwo, sThree, 1e-2, 1e-2) || (isNaN(sThree) && isNaN(sTwo)));
401+
assert(isClose(sThree, sFour, 1e-2, 1e-2) || (isNaN(sThree) && isNaN(sFour)));
402+
assert(isClose(sFour, sFive, 1e-2, 1e-2) || (isNaN(sFour) && isNaN(sFive)));
403403
}
404404

405405
// Test input ranges.
@@ -420,7 +420,7 @@ unittest {
420420
Count a, b;
421421
a.upTo = 100;
422422
b.upTo = 100;
423-
assert(approxEqual(spearmanCor(a, b), 1));
423+
assert(isClose(spearmanCor(a, b), 1));
424424
}
425425

426426
version(unittest) {
@@ -759,9 +759,9 @@ in {
759759

760760
unittest {
761761
//Test against known values.
762-
assert(approxEqual(kendallCor([1,2,3,4,5].dup, [3,1,7,4,3].dup), 0.1054093));
763-
assert(approxEqual(kendallCor([3,6,7,35,75].dup,[1,63,53,67,3].dup), 0.2));
764-
assert(approxEqual(kendallCor([1.5,6.3,7.8,4.2,1.5].dup, [1,63,53,67,3].dup), .3162287));
762+
assert(isClose(kendallCor([1,2,3,4,5].dup, [3,1,7,4,3].dup), 0.1054093, 1e-2, 1e-2));
763+
assert(isClose(kendallCor([3,6,7,35,75].dup,[1,63,53,67,3].dup), 0.2, 1e-2, 1e-2));
764+
assert(isClose(kendallCor([1.5,6.3,7.8,4.2,1.5].dup, [1,63,53,67,3].dup), .3162287, 1e-2, 1e-2));
765765

766766
static void doKendallTest(T)() {
767767
T[] one = new T[1000], two = new T[1000];
@@ -781,7 +781,7 @@ unittest {
781781
kendallCor(one[lowerBound..upperBound], two[lowerBound..upperBound]);
782782
double kTwo =
783783
kendallCorSmallN(one[lowerBound..upperBound], two[lowerBound..upperBound]);
784-
assert(approxEqual(kOne, kTwo) || (isNaN(kOne) && isNaN(kTwo)));
784+
assert(isClose(kOne, kTwo) || (isNaN(kOne) && isNaN(kTwo)));
785785
}
786786
}
787787

@@ -807,12 +807,12 @@ unittest {
807807
Count a, b;
808808
a.upTo = 100;
809809
b.upTo = 100;
810-
assert(approxEqual(kendallCor(a, b), 1));
810+
assert(isClose(kendallCor(a, b), 1));
811811

812812
// This test will fail if there are overflow bugs, especially in tie
813813
// handling.
814814
auto rng = chain(repeat(0, 100_000), repeat(1, 100_000));
815-
assert(approxEqual(kendallCor(rng, rng), 1));
815+
assert(isClose(kendallCor(rng, rng), 1));
816816

817817
// Test the case where we have one range sorted already.
818818
assert(kendallCor(iota(5), [3, 1, 2, 5, 4]) ==
@@ -823,7 +823,7 @@ unittest {
823823
kendallCor([3, 1, 2, 5, 4], assumeSorted(iota(5)))
824824
);
825825

826-
assert(approxEqual(
826+
assert(isClose(
827827
kendallCor(assumeSorted(iota(5)), assumeSorted(iota(5))), 1
828828
));
829829

@@ -929,11 +929,11 @@ unittest {
929929
uint[] consumerFear = [1, 2, 3, 4, 5, 6, 7];
930930
double partialCor =
931931
partial!pearsonCor(stock1Price, stock2Price, [economicHealth, consumerFear][]);
932-
assert(approxEqual(partialCor, -0.857818));
932+
assert(isClose(partialCor, -0.857818, 1e-2, 1e-2));
933933

934934
double spearmanPartial =
935935
partial!spearmanCor(stock1Price, stock2Price, economicHealth, consumerFear);
936-
assert(approxEqual(spearmanPartial, -0.7252));
936+
assert(isClose(spearmanPartial, -0.7252, 1e-2, 1e-2));
937937
}
938938

939939
private __gshared TaskPool emptyPool;
@@ -1001,7 +1001,7 @@ auto input = [[8.0, 6, 7, 5],
10011001
[3.0, 0, 9, 3],
10021002
[1.0, 4, 1, 5]];
10031003
auto pearson = pearsonMatrix(input);
1004-
assert(approxEqual(pearson[0, 0], 1));
1004+
assert(isClose(pearson[0, 0], 1, 1e-2, 1e-2));
10051005
---
10061006
*/
10071007
SymmetricMatrix!double pearsonMatrix(RoR)(RoR mat, TaskPool pool = null)
@@ -1058,7 +1058,7 @@ Examples:
10581058
---
10591059
auto pearsonRoR = [[0.0], [0.0, 0.0], [0.0, 0.0, 0.0]];
10601060
pearsonMatrix(input, pearsonRoR);
1061-
assert(approxEqual(pearsonRoR[1][1], 1));
1061+
assert(isClose(pearsonRoR[1][1], 1, 1e-2, 1e-2));
10621062
---
10631063
*/
10641064
void pearsonMatrix(RoR, Ret)(RoR mat, ref Ret ans, TaskPool pool = null)
@@ -1298,39 +1298,38 @@ unittest {
12981298

12991299
// Values from R.
13001300

1301-
alias approxEqual ae; // Save typing.
1302-
assert(ae(pearsonRoR[0][0], 1));
1303-
assert(ae(pearsonRoR[1][1], 1));
1304-
assert(ae(pearsonRoR[2][2], 1));
1305-
assert(ae(pearsonRoR[1][0], 0.3077935));
1306-
assert(ae(pearsonRoR[2][0], -0.9393364));
1307-
assert(ae(pearsonRoR[2][1], -0.6103679));
1308-
1309-
assert(ae(spearmanRoR[0][0], 1));
1310-
assert(ae(spearmanRoR[1][1], 1));
1311-
assert(ae(spearmanRoR[2][2], 1));
1312-
assert(ae(spearmanRoR[1][0], 0.3162278));
1313-
assert(ae(spearmanRoR[2][0], -0.9486833));
1314-
assert(ae(spearmanRoR[2][1], -0.5));
1315-
1316-
assert(ae(kendallRoR[0][0], 1));
1317-
assert(ae(kendallRoR[1][1], 1));
1318-
assert(ae(kendallRoR[2][2], 1));
1319-
assert(ae(kendallRoR[1][0], 0.1825742));
1320-
assert(ae(kendallRoR[2][0], -0.9128709));
1321-
assert(ae(kendallRoR[2][1], -0.4));
1322-
1323-
assert(ae(covRoR[0][0], 1.66666));
1324-
assert(ae(covRoR[1][1], 14.25));
1325-
assert(ae(covRoR[2][2], 4.25));
1326-
assert(ae(covRoR[1][0], 1.5));
1327-
assert(ae(covRoR[2][0], -2.5));
1328-
assert(ae(covRoR[2][1], -4.75));
1301+
assert(isClose(pearsonRoR[0][0], 1, 1e-2, 1e-2));
1302+
assert(isClose(pearsonRoR[1][1], 1, 1e-2, 1e-2));
1303+
assert(isClose(pearsonRoR[2][2], 1, 1e-2, 1e-2));
1304+
assert(isClose(pearsonRoR[1][0], 0.3077935, 1e-2, 1e-2));
1305+
assert(isClose(pearsonRoR[2][0], -0.9393364, 1e-2, 1e-2));
1306+
assert(isClose(pearsonRoR[2][1], -0.6103679, 1e-2, 1e-2));
1307+
1308+
assert(isClose(spearmanRoR[0][0], 1, 1e-2, 1e-2));
1309+
assert(isClose(spearmanRoR[1][1], 1, 1e-2, 1e-2));
1310+
assert(isClose(spearmanRoR[2][2], 1, 1e-2, 1e-2));
1311+
assert(isClose(spearmanRoR[1][0], 0.3162278, 1e-2, 1e-2));
1312+
assert(isClose(spearmanRoR[2][0], -0.9486833, 1e-2, 1e-2));
1313+
assert(isClose(spearmanRoR[2][1], -0.5, 1e-2, 1e-2));
1314+
1315+
assert(isClose(kendallRoR[0][0], 1, 1e-2, 1e-2));
1316+
assert(isClose(kendallRoR[1][1], 1, 1e-2, 1e-2));
1317+
assert(isClose(kendallRoR[2][2], 1, 1e-2, 1e-2));
1318+
assert(isClose(kendallRoR[1][0], 0.1825742, 1e-2, 1e-2));
1319+
assert(isClose(kendallRoR[2][0], -0.9128709, 1e-2, 1e-2));
1320+
assert(isClose(kendallRoR[2][1], -0.4, 1e-2, 1e-2));
1321+
1322+
assert(isClose(covRoR[0][0], 1.66666, 1e-2, 1e-2));
1323+
assert(isClose(covRoR[1][1], 14.25, 1e-2, 1e-2));
1324+
assert(isClose(covRoR[2][2], 4.25, 1e-2, 1e-2));
1325+
assert(isClose(covRoR[1][0], 1.5, 1e-2, 1e-2));
1326+
assert(isClose(covRoR[2][0], -2.5, 1e-2, 1e-2));
1327+
assert(isClose(covRoR[2][1], -4.75, 1e-2, 1e-2));
13291328

13301329
version(scid) {
13311330
static bool test(double[][] a, SymmetricMatrix!double b) {
13321331
foreach(i; 0..3) foreach(j; 0..i + 1) {
1333-
if(!ae(a[i][j], b[i, j])) return false;
1332+
if(!isClose(a[i][j], b[i, j], 1e-2, 1e-2)) return false;
13341333
}
13351334

13361335
return true;

0 commit comments

Comments
 (0)