Skip to content

Commit 0187fd6

Browse files
committed
feat: add tests and their fixtures
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: passed - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent bae0f1e commit 0187fd6

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed

lib/node_modules/@stdlib/stats/base/dists/bradford/entropy/test/fixtures/python/large_c.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2025 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
"""Generate fixtures."""
20+
21+
import os
22+
import json
23+
import numpy as np
24+
from scipy.stats import bradford
25+
26+
# Get the file path:
27+
FILE = os.path.realpath(__file__)
28+
29+
# Extract the directory in which this file resides:
30+
DIR = os.path.dirname(FILE)
31+
32+
33+
def gen(x, name):
34+
"""Generate fixture data and write to file.
35+
36+
# Arguments
37+
38+
* `x`: domain
39+
* `name::str`: output filename
40+
41+
# Examples
42+
43+
``` python
44+
python> x = linspace(0.1, 100, 2001)
45+
python> gen(x, './data.json')
46+
```
47+
"""
48+
y = bradford.entropy(x)
49+
50+
# Store data to be written to file as a dictionary:
51+
data = {
52+
"c": x.tolist(),
53+
"expected": y.tolist()
54+
}
55+
56+
# Based on the script directory, create an output filepath:
57+
filepath = os.path.join(DIR, name)
58+
59+
# Write the data to the output filepath as JSON:
60+
with open(filepath, "w", encoding="utf-8") as outfile:
61+
json.dump(data, outfile)
62+
63+
64+
def main():
65+
"""Generate fixture data."""
66+
# Small shape parameter:
67+
c = np.linspace(0.1, 1, 1000)
68+
gen(c, "small_c.json")
69+
70+
# Large shape parameter:
71+
c = np.linspace(1, 10, 1000)
72+
gen(c, "large_c.json")
73+
74+
75+
if __name__ == "__main__":
76+
main()

lib/node_modules/@stdlib/stats/base/dists/bradford/entropy/test/fixtures/python/small_c.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var tape = require( 'tape' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var abs = require( '@stdlib/math/base/special/abs' );
26+
var NINF = require( '@stdlib/constants/float64/ninf' );
27+
var EPS = require( '@stdlib/constants/float64/eps' );
28+
var entropy = require( './../lib' );
29+
30+
31+
// FIXTURES //
32+
33+
var smallC = require( './fixtures/python/small_c.json' );
34+
var largeC = require( './fixtures/python/large_c.json' );
35+
36+
37+
// TESTS //
38+
39+
tape( 'main export is a function', function test( t ) {
40+
t.ok( true, __filename );
41+
t.strictEqual( typeof entropy, 'function', 'main export is a function' );
42+
t.end();
43+
});
44+
45+
tape( 'if provided `NaN` for `c`, the function returns `NaN`', function test( t ) {
46+
var v = entropy( NaN );
47+
t.equal( isnan( v ), true, 'returns expected value' );
48+
t.end();
49+
});
50+
51+
tape( 'if provided `c <= 0`, the function returns `NaN`', function test( t ) {
52+
var v;
53+
54+
v = entropy( 0.0 );
55+
t.equal( isnan( v ), true, 'returns expected value' );
56+
57+
v = entropy( -1.0 );
58+
t.equal( isnan( v ), true, 'returns expected value' );
59+
60+
v = entropy( NINF );
61+
t.equal( isnan( v ), true, 'returns expected value' );
62+
63+
t.end();
64+
});
65+
66+
tape( 'the function returns the differential entropy of a Bradford distribution given small parameter `c`', function test( t ) {
67+
var expected;
68+
var delta;
69+
var tol;
70+
var i;
71+
var c;
72+
var y;
73+
74+
expected = smallC.expected;
75+
c = smallC.c;
76+
for ( i = 0; i < expected.length; i++ ) {
77+
y = entropy( c[i] );
78+
if ( y === expected[i] ) {
79+
t.equal( y, expected[i], 'c: '+c[i]+', y: '+y+', expected: '+expected[i] );
80+
} else {
81+
delta = abs( y - expected[ i ] );
82+
83+
/*
84+
* NOTE: the tolerance is set high in this case due to:
85+
*
86+
* 1. The shape parameter being very small which causes differences in the nested log expression compared to the reference (SciPy) implementation.
87+
* 2. The expected values being either very small.
88+
*/
89+
tol = 1523.0 * EPS * abs( expected[ i ] );
90+
t.ok( delta <= tol, 'within tolerance. c: '+c[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
91+
}
92+
}
93+
t.end();
94+
});
95+
96+
tape( 'the function returns the differential entropy of a Bradford distribution given large parameter `c`', function test( t ) {
97+
var expected;
98+
var delta;
99+
var tol;
100+
var i;
101+
var c;
102+
var y;
103+
104+
expected = largeC.expected;
105+
c = largeC.c;
106+
for ( i = 0; i < expected.length; i++ ) {
107+
y = entropy( c[i] );
108+
if ( y === expected[i] ) {
109+
t.equal( y, expected[i], 'c: '+c[i]+', y: '+y+', expected: '+expected[i] );
110+
} else {
111+
delta = abs( y - expected[ i ] );
112+
tol = 45.0 * EPS * abs( expected[ i ] );
113+
t.ok( delta <= tol, 'within tolerance. c: '+c[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
114+
}
115+
}
116+
t.end();
117+
});

0 commit comments

Comments
 (0)