|
| 1 | +import networkx as nx |
1 | 2 | import numpy as np |
2 | 3 | import pytest |
3 | 4 | import scipy as sp |
4 | 5 | from graphite_maps import precision_estimation as precest |
5 | 6 | from scipy.optimize import minimize |
6 | 7 |
|
7 | 8 |
|
| 9 | +def get_precision_data(): |
| 10 | + rng = np.random.default_rng(8) |
| 11 | + n = 10 # Size |
| 12 | + density = 0.4 |
| 13 | + |
| 14 | + # Create G indicating sparsity pattern |
| 15 | + G = rng.uniform(size=(n, n)) < (density / 2) |
| 16 | + G = G.T + G |
| 17 | + np.fill_diagonal(G, G.diagonal() + 1) |
| 18 | + G_matrix = (G > 0).astype(int) |
| 19 | + Graph_u = nx.from_scipy_sparse_array(sp.sparse.csc_array(G_matrix)) |
| 20 | + |
| 21 | + # Create data U |
| 22 | + U = rng.normal(size=(999, n)) |
| 23 | + |
| 24 | + return U, Graph_u, G_matrix |
| 25 | + |
| 26 | + |
| 27 | +def test_snapshot_fit_precision_cholesky(): |
| 28 | + |
| 29 | + U, Graph_u, G_matrix = get_precision_data() |
| 30 | + |
| 31 | + # Estimate precision with fit_precision_cholesky. |
| 32 | + # Cannot use METIS (not reproducible across OSes), use 'natural' |
| 33 | + Prec_est, *_ = precest.fit_precision_cholesky( |
| 34 | + U=U, Graph_u=Graph_u, ordering_method="natural" |
| 35 | + ) |
| 36 | + Prec_est = Prec_est.todense() |
| 37 | + |
| 38 | + entries_at_one = Prec_est[G_matrix > 0] |
| 39 | + entries_at_zero = Prec_est[G_matrix == 0] |
| 40 | + |
| 41 | + desired = np.array([1.03393041, -0.05494438, 1.02092228, 1.00923821]) |
| 42 | + np.testing.assert_allclose(entries_at_one[::9], desired, atol=1e-8) |
| 43 | + |
| 44 | + desired = np.array([0.0, 0.0, 0.0, 0.01763788, -0.03135188, 0.0, 0.0, 0.0]) |
| 45 | + np.testing.assert_allclose(entries_at_zero[::9], desired, atol=1e-8) |
| 46 | + |
| 47 | + |
| 48 | +def test_snapshot_fit_precision_cholesky_approximate(): |
| 49 | + |
| 50 | + U, Graph_u, G_matrix = get_precision_data() |
| 51 | + # Estimate precision with fit_precision_cholesky |
| 52 | + Prec_est = precest.fit_precision_cholesky_approximate( |
| 53 | + U=U, Graph_u=Graph_u, neighbourhood_expansion=2 |
| 54 | + ) |
| 55 | + Prec_est = Prec_est.todense() |
| 56 | + |
| 57 | + entries_at_one = Prec_est[G_matrix > 0] |
| 58 | + entries_at_zero = Prec_est[G_matrix == 0] |
| 59 | + |
| 60 | + desired = np.array([1.03392773, -0.05606645, 1.022626, 1.00883855]) |
| 61 | + np.testing.assert_allclose(entries_at_one[::9], desired, atol=1e-8) |
| 62 | + |
| 63 | + desired = np.array( |
| 64 | + [0.0, -0.04588378, -0.00330536, -0.00124644, -0.0301345, -0.02301515, 0.0, 0.0] |
| 65 | + ) |
| 66 | + np.testing.assert_allclose(entries_at_zero[::9], desired, atol=1e-8) |
| 67 | + |
| 68 | + |
8 | 69 | def test_objective_twice(): |
9 | 70 | # A regression test: ensure that two calls return the same result. |
10 | 71 | rng = np.random.default_rng(42) |
|
0 commit comments