Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ The repository includes a unit testing library with over 4,000 tests that also s

Contributions are welcome. Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on bug reports, feature requests, and pull requests.

## AI Disclosure

Frontier generative AI models and coding agents are used to assist with software development, documentation, testing, and verification workflows. Project maintainers remain responsible for reviewing contributions, validating scientific methods and results, and deciding what is accepted or released.

## License

See [LICENSE](LICENSE) for details.
1 change: 1 addition & 0 deletions python/pynumerics/tests/test_calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ def test_python_defaults_are_the_numerics_defaults(fn, type_name, member, count)

# Speed: a compiled integrand avoids the per-evaluation crossing into Python.

@pytest.mark.benchmark
@needs_roslyn
def test_compiled_integrand_is_faster_than_a_python_callback() -> None:
# Arrange
Expand Down
19 changes: 16 additions & 3 deletions python/pynumerics/tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,27 @@ def test_scalar_python_callback_integrates_like_scipy() -> None:


@needs_roslyn
def test_compiled_lambda_gives_the_python_callback_bits_and_runs_faster() -> None:
def test_compiled_lambda_gives_the_python_callback_bits() -> None:
# Arrange
python = callbacks.scalar_function(lambda x: math.exp(-x * x))
compiled = callbacks.compile_csharp("x => Math.Exp(-x * x)")
# Act
python_result, python_count, python_cost = _integrate(python)
compiled_result, compiled_count, compiled_cost = _integrate(compiled)
python_result, python_count, _ = _integrate(python)
compiled_result, compiled_count, _ = _integrate(compiled)
# Assert
assert compiled_result == python_result and compiled_count == python_count


@pytest.mark.benchmark
@needs_roslyn
def test_compiled_lambda_runs_faster_than_the_python_callback() -> None:
# Arrange
python = callbacks.scalar_function(lambda x: math.exp(-x * x))
compiled = callbacks.compile_csharp("x => Math.Exp(-x * x)")
# Act
_, _, python_cost = _integrate(python)
_, _, compiled_cost = _integrate(compiled)
# Assert
assert compiled_cost * 5.0 < python_cost, (compiled_cost, python_cost)


Expand Down Expand Up @@ -279,6 +291,7 @@ def _per_call(delegate: object, argument: object, calls: int = 20000) -> float:
return best


@pytest.mark.benchmark
def test_array_callback_costs_close_to_a_raw_pythonnet_delegate() -> None:
# Arrange: measured on a desktop, the overhead over a raw pythonnet delegate is about 0.4 us at
# 2 to 50 values and 1 us at 1,000; the bound leaves a wide margin for slower machines.
Expand Down
24 changes: 19 additions & 5 deletions python/pynumerics/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,32 @@ def test_downloaded_series_fields() -> None:
result.utc = True # frozen


def test_parse_usgs_bulk_speed() -> None:
# Arrange: 40,000 daily records with qualifiers, about a century of record
def _usgs_century_text() -> str:
"""Return 40,000 daily USGS records with qualifiers, about a century of record."""
days = np.arange(np.datetime64("1900-01-01"), np.datetime64("1900-01-01") + np.timedelta64(40000, "D"))
text = "x,y,time,value,approval_status,qualifier\n" + "".join(
return "x,y,time,value,approval_status,qualifier\n" + "".join(
f",,{d},{i % 997 + 0.5},Approved,{'ICE' if i % 50 == 0 else ''}\n" for i, d in enumerate(days))


def test_parse_usgs_reads_a_century_of_records_with_qualifiers() -> None:
# Arrange
text = _usgs_century_text()
# Act
result = download.parse_usgs(text, qualifiers=True)
# Assert
assert len(result.values) == 40000 and result.qualifiers.count("ICE") == 800


@pytest.mark.benchmark
def test_parse_usgs_bulk_speed() -> None:
# Arrange
text = _usgs_century_text()
download.parse_usgs(text[:2000]) # warm up
# Act
started = time.perf_counter()
result = download.parse_usgs(text, qualifiers=True)
download.parse_usgs(text, qualifiers=True)
elapsed = time.perf_counter() - started
# Assert: the parse runs in Numerics and the dates, values and qualifiers cross in bulk
assert len(result.values) == 40000 and result.qualifiers.count("ICE") == 800
assert elapsed < 1.0, f"{elapsed:.3f} s"


Expand Down
19 changes: 15 additions & 4 deletions python/pynumerics/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,31 @@ def test_array_arguments_cross_once_with_the_scalar_bits(case: int) -> None:
np.testing.assert_array_equal(_bits(array.ravel()), _bits(expected))


def test_link_arrays_equal_element_calls_and_scipy() -> None:
# Arrange
link = fn.LogitLink()
points = np.linspace(0.001, 0.999, 20000)
# Act
array = link.link(points)
elements = np.array([link.net.Link(p) for p in points.tolist()])
# Assert
np.testing.assert_array_equal(array, elements)
np.testing.assert_allclose(array, special.logit(points), rtol=1e-13, atol=1e-15)


@pytest.mark.benchmark
def test_link_arrays_are_faster_than_element_calls() -> None:
# Arrange
link = fn.LogitLink()
points = np.linspace(0.001, 0.999, 20000)
link.link(points[:5])
# Act
begin = time.perf_counter()
fast = link.link(points)
link.link(points)
fast_seconds = time.perf_counter() - begin
scalar = link.net.Link
begin = time.perf_counter()
slow = np.array([scalar(p) for p in points.tolist()])
_ = [scalar(p) for p in points.tolist()]
slow_seconds = time.perf_counter() - begin
# Assert
np.testing.assert_array_equal(fast, slow)
np.testing.assert_allclose(fast, special.logit(points), rtol=1e-13, atol=1e-15)
assert fast_seconds < slow_seconds / 3
13 changes: 11 additions & 2 deletions python/pynumerics/tests/test_goodness_of_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,25 @@ def test_distribution_statistics_equal_scipy() -> None:
gof.kolmogorov_smirnov([], model)


def test_paired_metrics_on_large_series_equal_sklearn() -> None:
# Arrange
o, m = _pair(500_000, 3)
# Act
nse = gof.nash_sutcliffe_efficiency(o, m)
# Assert
assert nse == pytest.approx(metrics.r2_score(o, m), rel=1e-10)


@pytest.mark.benchmark
def test_paired_metrics_cross_once_for_large_series() -> None:
# Arrange
o, m = _pair(500_000, 3)
gof.nash_sutcliffe_efficiency(o[:10], m[:10])
# Act
start = time.perf_counter()
nse = gof.nash_sutcliffe_efficiency(o, m)
gof.nash_sutcliffe_efficiency(o, m)
elapsed = time.perf_counter() - start
# Assert: bulk copies, not one crossing per value
assert nse == pytest.approx(metrics.r2_score(o, m), rel=1e-10)
assert elapsed < 0.5


Expand Down
14 changes: 12 additions & 2 deletions python/pynumerics/tests/test_hypothesis_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,27 @@ def test_multiple_grubbs_beck_matches_hec_ssp() -> None:
assert isinstance(ht.multiple_grubbs_beck_test(lognormal), int)


def test_hot_path_tests_on_large_samples_equal_scipy() -> None:
# Arrange
rng = np.random.default_rng(1)
a, b = rng.normal(size=200_000), rng.normal(0.01, 1.0, 200_000)
# Act
p = ht.equal_variance_t_test(a, b)
# Assert
assert p == pytest.approx(stats.ttest_ind(a, b).pvalue, rel=1e-8)


@pytest.mark.benchmark
def test_hot_path_tests_cross_once_for_large_samples() -> None:
# Arrange
rng = np.random.default_rng(1)
a, b = rng.normal(size=200_000), rng.normal(0.01, 1.0, 200_000)
ht.equal_variance_t_test(a[:10], b[:10])
# Act
start = time.perf_counter()
p = ht.equal_variance_t_test(a, b)
ht.equal_variance_t_test(a, b)
elapsed = time.perf_counter() - start
# Assert: one bulk copy each, not one crossing per value (a per-element path takes seconds)
assert p == pytest.approx(stats.ttest_ind(a, b).pvalue, rel=1e-8)
assert elapsed < 0.5


Expand Down
45 changes: 36 additions & 9 deletions python/pynumerics/tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,41 +180,68 @@ def test_search_functions_match_numpy_searchsorted(method: data.SearchMethod) ->
np.testing.assert_array_equal(descending, len(KNOTS) - 2 - expected)


def test_array_interpolation_gives_the_scalar_call_bits() -> None:
# Arrange
spline = data.CubicSpline(KNOTS, VALUES)
x = np.random.default_rng(4).uniform(0.0, 5.5, 20000)
# Act
bulk = spline(x)
scalar = np.array([spline(v) for v in x])
# Assert
np.testing.assert_array_equal(bulk.view(np.uint64), scalar.view(np.uint64))


@pytest.mark.benchmark
def test_array_interpolation_crosses_once_faster_than_scalar_calls() -> None:
# Arrange
spline = data.CubicSpline(KNOTS, VALUES)
x = np.random.default_rng(4).uniform(0.0, 5.5, 20000)
# Act
start = time.perf_counter()
bulk = spline(x)
spline(x)
bulk_time = time.perf_counter() - start
start = time.perf_counter()
scalar = np.array([spline(v) for v in x])
_ = [spline(v) for v in x]
scalar_time = time.perf_counter() - start
# Assert
np.testing.assert_array_equal(bulk.view(np.uint64), scalar.view(np.uint64))
assert bulk_time * 10.0 < scalar_time, (bulk_time, scalar_time)


# List overloads -------------------------------------------------------------------------------


def test_bilinear_arrays_cross_once_with_the_scalar_bits() -> None:
# Arrange
def _bilinear_grids() -> tuple[data.Bilinear, data.Bilinear, np.ndarray]:
"""Return two identical logarithmic-x1 bilinear grids and 3,000 query points."""
x1, x2 = np.array([1.0, 2.0, 4.0, 8.0]), np.array([10.0, 20.0, 35.0])
y = np.add.outer(np.log(x1), np.sqrt(x2))
points = np.random.default_rng(4).uniform([0.5, 5.0], [9.0, 40.0], (3000, 2))
grid = data.Bilinear(x1, x2, y, x1_transform="logarithmic")
reference = data.Bilinear(x1, x2, y, x1_transform="logarithmic")
return grid, reference, points


def test_bilinear_arrays_give_the_scalar_bits() -> None:
# Arrange
grid, reference, points = _bilinear_grids()
# Act
begin = time.perf_counter()
values = grid(points[:, 0], points[:, 1])
expected = np.array([reference.net.Interpolate(float(a), float(b)) for a, b in points])
# Assert
np.testing.assert_array_equal(values.view(np.uint64), expected.view(np.uint64))


@pytest.mark.benchmark
def test_bilinear_arrays_cross_once_faster_than_scalar_calls() -> None:
# Arrange
grid, reference, points = _bilinear_grids()
# Act
begin = time.perf_counter()
grid(points[:, 0], points[:, 1])
list_seconds = time.perf_counter() - begin
begin = time.perf_counter()
expected = np.array([reference.net.Interpolate(float(a), float(b)) for a, b in points])
_ = [reference.net.Interpolate(float(a), float(b)) for a, b in points]
loop_seconds = time.perf_counter() - begin
# Assert (one crossing gives the scalar calls' bits, in a fraction of the time)
np.testing.assert_array_equal(values.view(np.uint64), expected.view(np.uint64))
# Assert (one crossing takes a fraction of the time of the scalar calls)
assert list_seconds < loop_seconds / 3


Expand Down
13 changes: 11 additions & 2 deletions python/pynumerics/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,27 @@ def test_decomposition_method_enum_resolves_names() -> None:
assert linalg.DecompositionMethod("Cholesky") is linalg.DecompositionMethod.CHOLESKY


def test_large_matrices_round_trip_bit_exact() -> None:
# Arrange
a = np.random.default_rng(19).normal(size=(500, 500))
# Act
values = linalg.Matrix(a).to_array()
# Assert
np.testing.assert_array_equal(_bits(values), _bits(a))


@pytest.mark.benchmark
def test_large_matrices_cross_in_one_copy() -> None:
# Arrange
a = np.random.default_rng(19).normal(size=(500, 500))
m = linalg.Matrix(a)
# Act
start = time.perf_counter()
for _ in range(5):
values = m.to_array()
m.to_array()
linalg.Matrix(a)
elapsed = time.perf_counter() - start
# Assert: 250,000 doubles element by element would take seconds
np.testing.assert_array_equal(_bits(values), _bits(a))
assert elapsed < 0.5


Expand Down
15 changes: 13 additions & 2 deletions python/pynumerics/tests/test_machine_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,18 @@ def test_kriging_custom_distance_and_universal() -> None:
# to the Numerics call, never a per-row crossing.


def test_predict_returns_one_value_per_row_of_a_large_feature_matrix() -> None:
# Arrange
x, y = _regression(30, 200)
queries = np.random.default_rng(31).normal(size=(20_000, 3))
model = ml.KNearestNeighbors(x, y, 3)
# Act
wrapped = model.predict(queries)
# Assert
assert wrapped.shape == (20_000,)


@pytest.mark.benchmark
def test_predict_crosses_the_feature_matrix_once() -> None:
# Arrange
x, y = _regression(30, 200)
Expand All @@ -748,10 +760,9 @@ def test_predict_crosses_the_feature_matrix_once() -> None:
raw = time.perf_counter() - start
start = time.perf_counter()
for _ in range(3):
wrapped = model.predict(queries)
model.predict(queries)
total = time.perf_counter() - start
# Assert: the copies of 60,000 inputs and 20,000 outputs cost far less than the search itself
assert wrapped.shape == (20_000,)
assert total < 1.5 * raw + 0.05, f"wrapper {total:.3f} s against {raw:.3f} s for the bare call"


Expand Down
14 changes: 12 additions & 2 deletions python/pynumerics/tests/test_multivariate_copulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ def test_categorical_indices_refuse_invalid_tables_and_variates(table, uniforms,
multivariate.Multinomial.categorical_indices(table, uniforms)


def test_categorical_indices_return_one_index_per_uniform() -> None:
# Arrange
cumulative = np.cumsum(np.full(50, 0.02))
uniforms = np.random.default_rng(6).random(200000)
# Act
bulk = multivariate.Multinomial.categorical_indices(cumulative, uniforms)
# Assert
assert bulk.size == uniforms.size


@pytest.mark.benchmark
def test_categorical_indices_cross_once_and_beat_per_element_calls() -> None:
# Arrange
cumulative = np.cumsum(np.full(50, 0.02))
Expand All @@ -284,14 +295,13 @@ def test_categorical_indices_cross_once_and_beat_per_element_calls() -> None:
multivariate.Multinomial.categorical_indices(cumulative, uniforms[:10])
# Act
start = time.perf_counter()
bulk = multivariate.Multinomial.categorical_indices(cumulative, uniforms)
multivariate.Multinomial.categorical_indices(cumulative, uniforms)
bulk_seconds = time.perf_counter() - start
start = time.perf_counter()
for _ in range(2000):
nd.Multinomial.CategoricalIndices(net_table, one)
per_call = (time.perf_counter() - start) / 2000
# Assert: the whole array costs less than a twentieth of crossing per element
assert bulk.size == uniforms.size
assert bulk_seconds < per_call * uniforms.size / 20


Expand Down
Loading
Loading