CUQIpy 1.1.0.post0.dev32__tar.gz → 1.1.0.post0.dev39__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of CUQIpy might be problematic. Click here for more details.

Files changed (119) hide show
  1. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/CUQIpy.egg-info/PKG-INFO +1 -1
  2. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/PKG-INFO +1 -1
  3. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/_version.py +3 -3
  4. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/utilities/__init__.py +2 -0
  5. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/utilities/_utilities.py +116 -0
  6. cuqipy-1.1.0.post0.dev39/tests/test_utilities.py +68 -0
  7. cuqipy-1.1.0.post0.dev32/tests/test_utilities.py +0 -20
  8. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/CUQIpy.egg-info/SOURCES.txt +0 -0
  9. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/CUQIpy.egg-info/dependency_links.txt +0 -0
  10. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/CUQIpy.egg-info/requires.txt +0 -0
  11. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/CUQIpy.egg-info/top_level.txt +0 -0
  12. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/LICENSE +0 -0
  13. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/README.md +0 -0
  14. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/__init__.py +0 -0
  15. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/_messages.py +0 -0
  16. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/array/__init__.py +0 -0
  17. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/array/_array.py +0 -0
  18. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/config.py +0 -0
  19. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/data/__init__.py +0 -0
  20. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/data/_data.py +0 -0
  21. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/data/astronaut.npz +0 -0
  22. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/data/camera.npz +0 -0
  23. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/data/cat.npz +0 -0
  24. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/data/cookie.png +0 -0
  25. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/data/satellite.mat +0 -0
  26. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/density/__init__.py +0 -0
  27. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/density/_density.py +0 -0
  28. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/diagnostics.py +0 -0
  29. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/__init__.py +0 -0
  30. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_beta.py +0 -0
  31. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_cauchy.py +0 -0
  32. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_cmrf.py +0 -0
  33. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_custom.py +0 -0
  34. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_distribution.py +0 -0
  35. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_gamma.py +0 -0
  36. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_gaussian.py +0 -0
  37. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_gmrf.py +0 -0
  38. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_inverse_gamma.py +0 -0
  39. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_joint_distribution.py +0 -0
  40. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_laplace.py +0 -0
  41. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_lmrf.py +0 -0
  42. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_lognormal.py +0 -0
  43. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_modifiedhalfnormal.py +0 -0
  44. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_normal.py +0 -0
  45. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_posterior.py +0 -0
  46. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_smoothed_laplace.py +0 -0
  47. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/distribution/_uniform.py +0 -0
  48. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/__init__.py +0 -0
  49. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/__init__.py +0 -0
  50. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_conjugate.py +0 -0
  51. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_conjugate_approx.py +0 -0
  52. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_cwmh.py +0 -0
  53. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_direct.py +0 -0
  54. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_gibbs.py +0 -0
  55. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_hmc.py +0 -0
  56. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_langevin_algorithm.py +0 -0
  57. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_laplace_approximation.py +0 -0
  58. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_mh.py +0 -0
  59. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_pcn.py +0 -0
  60. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_rto.py +0 -0
  61. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_sampler.py +0 -0
  62. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/experimental/mcmc/_utilities.py +0 -0
  63. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/geometry/__init__.py +0 -0
  64. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/geometry/_geometry.py +0 -0
  65. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/implicitprior/__init__.py +0 -0
  66. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/implicitprior/_regularizedGMRF.py +0 -0
  67. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/implicitprior/_regularizedGaussian.py +0 -0
  68. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/implicitprior/_regularizedUnboundedUniform.py +0 -0
  69. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/likelihood/__init__.py +0 -0
  70. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/likelihood/_likelihood.py +0 -0
  71. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/model/__init__.py +0 -0
  72. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/model/_model.py +0 -0
  73. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/operator/__init__.py +0 -0
  74. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/operator/_operator.py +0 -0
  75. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/pde/__init__.py +0 -0
  76. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/pde/_pde.py +0 -0
  77. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/problem/__init__.py +0 -0
  78. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/problem/_problem.py +0 -0
  79. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/__init__.py +0 -0
  80. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_conjugate.py +0 -0
  81. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_conjugate_approx.py +0 -0
  82. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_cwmh.py +0 -0
  83. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_gibbs.py +0 -0
  84. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_hmc.py +0 -0
  85. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_langevin_algorithm.py +0 -0
  86. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_laplace_approximation.py +0 -0
  87. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_mh.py +0 -0
  88. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_pcn.py +0 -0
  89. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_rto.py +0 -0
  90. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/sampler/_sampler.py +0 -0
  91. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/samples/__init__.py +0 -0
  92. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/samples/_samples.py +0 -0
  93. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/solver/__init__.py +0 -0
  94. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/solver/_solver.py +0 -0
  95. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/testproblem/__init__.py +0 -0
  96. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/testproblem/_testproblem.py +0 -0
  97. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/cuqi/utilities/_get_python_variable_name.py +0 -0
  98. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/pyproject.toml +0 -0
  99. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/requirements.txt +0 -0
  100. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/setup.cfg +0 -0
  101. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/setup.py +0 -0
  102. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_MRFs.py +0 -0
  103. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_abstract_distribution_density.py +0 -0
  104. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_bayesian_inversion.py +0 -0
  105. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_density.py +0 -0
  106. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_distribution.py +0 -0
  107. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_distributions_shape.py +0 -0
  108. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_geometry.py +0 -0
  109. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_implicit_priors.py +0 -0
  110. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_joint_distribution.py +0 -0
  111. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_likelihood.py +0 -0
  112. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_model.py +0 -0
  113. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_pde.py +0 -0
  114. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_posterior.py +0 -0
  115. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_problem.py +0 -0
  116. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_sampler.py +0 -0
  117. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_samples.py +0 -0
  118. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_solver.py +0 -0
  119. {cuqipy-1.1.0.post0.dev32 → cuqipy-1.1.0.post0.dev39}/tests/test_testproblem.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: CUQIpy
3
- Version: 1.1.0.post0.dev32
3
+ Version: 1.1.0.post0.dev39
4
4
  Summary: Computational Uncertainty Quantification for Inverse problems in Python
5
5
  Maintainer-email: "Nicolai A. B. Riis" <nabr@dtu.dk>, "Jakob S. Jørgensen" <jakj@dtu.dk>, "Amal M. Alghamdi" <amaal@dtu.dk>, Chao Zhang <chaz@dtu.dk>
6
6
  License: Apache License
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: CUQIpy
3
- Version: 1.1.0.post0.dev32
3
+ Version: 1.1.0.post0.dev39
4
4
  Summary: Computational Uncertainty Quantification for Inverse problems in Python
5
5
  Maintainer-email: "Nicolai A. B. Riis" <nabr@dtu.dk>, "Jakob S. Jørgensen" <jakj@dtu.dk>, "Amal M. Alghamdi" <amaal@dtu.dk>, Chao Zhang <chaz@dtu.dk>
6
6
  License: Apache License
@@ -8,11 +8,11 @@ import json
8
8
 
9
9
  version_json = '''
10
10
  {
11
- "date": "2024-08-26T11:16:28+0200",
11
+ "date": "2024-08-29T10:46:43+0300",
12
12
  "dirty": false,
13
13
  "error": null,
14
- "full-revisionid": "0a3eb53894cc988444a61e5b895cee57b66747a9",
15
- "version": "1.1.0.post0.dev32"
14
+ "full-revisionid": "9d8920342c73b1bd8e7a47a216eb34056ea2f5d6",
15
+ "version": "1.1.0.post0.dev39"
16
16
  }
17
17
  ''' # END VERSION_JSON
18
18
 
@@ -11,6 +11,8 @@ from ._utilities import (
11
11
  sparse_cholesky,
12
12
  approx_derivative,
13
13
  check_if_conditional_from_attr,
14
+ plot_1D_density,
15
+ plot_2D_density
14
16
  )
15
17
 
16
18
  from ._get_python_variable_name import _get_python_variable_name
@@ -1,4 +1,5 @@
1
1
  from cuqi.array import CUQIarray
2
+ from cuqi.density import Density
2
3
  import numpy as np
3
4
  import inspect
4
5
  from numbers import Number
@@ -7,6 +8,7 @@ from scipy.sparse import linalg as spslinalg
7
8
  from dataclasses import dataclass
8
9
  from abc import ABCMeta
9
10
  import copy
11
+ import matplotlib.pyplot as plt
10
12
 
11
13
 
12
14
  def force_ndarray(value,flatten=False):
@@ -229,3 +231,117 @@ def approx_gradient(func, x, epsilon= 0.000001):
229
231
  eps_vec[i] = 0.0
230
232
 
231
233
  return FD_gradient
234
+
235
+
236
+ # Function for plotting 1D density functions
237
+ def plot_1D_density(density:Density,
238
+ v_min, v_max,
239
+ N=501, log_scale=False,
240
+ **kwargs):
241
+ """ Plot 1D density function
242
+
243
+ Parameters
244
+ ----------
245
+ density : CUQIpy Density
246
+ The density to be plotted.
247
+
248
+ v_min : float
249
+ Minimum value for the variable.
250
+
251
+ v_max : float
252
+ Maximum value for the variable.
253
+
254
+ N : int
255
+ Number of grid points for the variable.
256
+
257
+ log_scale : bool
258
+ If True, the density is plotted in log scale.
259
+
260
+ kwargs : dict
261
+ Additional keyword arguments for the plot that are passed to the
262
+ underlying plotting method: `matplotlib.pyplot.plot` function
263
+ in this case.
264
+
265
+ """
266
+ # Assert that the density is 1D
267
+ assert density.dim == 1, "The density must be for a scalar variable"
268
+ ls = np.linspace(v_min, v_max, N)
269
+
270
+ # Create a map to evaluate density
271
+ density_map = (lambda x: x) if log_scale else (lambda x: np.exp(x))
272
+
273
+ # Evaluate density on grid
274
+ y = [density_map(density.logd(grid_point)) for grid_point in ls]
275
+ p = plt.plot(ls, y, **kwargs)
276
+ return p
277
+
278
+ # Function for plotting 2D density functions
279
+ def plot_2D_density(density: Density,
280
+ v1_min, v1_max,
281
+ v2_min, v2_max,
282
+ N1=201, N2=201,
283
+ log_scale=False,
284
+ **kwargs):
285
+ """ Plot 2D density function
286
+
287
+ Parameters
288
+ ----------
289
+ density : CUQIpy Density
290
+ The density to be plotted.
291
+
292
+ v1_min : float
293
+ Minimum value for the first variable.
294
+
295
+ v1_max : float
296
+ Maximum value for the first variable.
297
+
298
+ v2_min : float
299
+ Minimum value for the second variable.
300
+
301
+ v2_max : float
302
+ Maximum value for the second variable.
303
+
304
+ N1 : int
305
+ Number of grid points for the first variable.
306
+
307
+ N2 : int
308
+ Number of grid points for the second variable.
309
+
310
+ log_scale : bool
311
+ If True, the density is plotted in log scale.
312
+
313
+ kwargs : dict
314
+ Additional keyword arguments for the plot that are passed to the
315
+ underlying plotting method: `matplotlib.pyplot.imshow` function
316
+ in this case.
317
+
318
+ """
319
+ # Assert that the density is 2D
320
+ assert density.dim == 2,\
321
+ "The density must be for a two-dimensional variable"
322
+ # Create grid
323
+ ls1 = np.linspace(v1_min, v1_max, N1)
324
+ ls2 = np.linspace(v2_min, v2_max, N2)
325
+ grid1, grid2 = np.meshgrid(ls1, ls2)
326
+
327
+ # Create a map to evaluate density
328
+ density_map = (lambda x: x) if log_scale else (lambda x: np.exp(x))
329
+
330
+ # Evaluate density on grid
331
+ evaluated_density = np.zeros((N1, N2))
332
+ for ii in range(N1):
333
+ for jj in range(N2):
334
+ evaluated_density[ii,jj] = density_map(
335
+ density.logd([grid1[ii,jj], grid2[ii,jj]]))
336
+
337
+ # Plot
338
+ pixelwidth_x = (v1_max-v1_min)/(N2-1)
339
+ pixelwidth_y = (v2_max-v2_min)/(N2-1)
340
+
341
+ hp_x = 0.5*pixelwidth_x
342
+ hp_y = 0.5*pixelwidth_y
343
+
344
+ extent = (v1_min-hp_x, v1_max+hp_x, v2_min-hp_y, v2_max+hp_y)
345
+
346
+ im = plt.imshow(evaluated_density, origin='lower', extent=extent, **kwargs)
347
+ return im
@@ -0,0 +1,68 @@
1
+ import pytest
2
+ from scipy.linalg import cholesky
3
+ from scipy.sparse import diags
4
+ from cuqi.utilities import sparse_cholesky, plot_1D_density, plot_2D_density
5
+ from cuqi.model import LinearModel
6
+ from cuqi.distribution import Gaussian, Uniform, JointDistribution
7
+ import numpy as np
8
+ import matplotlib.pyplot as plt
9
+
10
+
11
+ @pytest.mark.parametrize("P", [
12
+ diags([-1, 2, -1], [-1, 0, 1], shape=(128, 128)),
13
+ diags([1, -4, 6, -4, 1], [-2, -1, 0, 1, 2], shape=(128, 128))
14
+ ])
15
+ def test_sparse_cholesky(P):
16
+ """ Test the sparse Cholesky decomposition. P is a sparse matrix (often precision). """
17
+ # Scipy version (on dense only)
18
+ L1 = cholesky(P.toarray())
19
+
20
+ # Scipy-based version from CUQIpy (on sparse)
21
+ L2 = sparse_cholesky(P)
22
+
23
+ assert np.allclose(L1, L2.toarray()) # Convert to dense to compare
24
+
25
+
26
+ def posterior_2D_components():
27
+ """A function to create and return 2D Bayesian posterior components"""
28
+ # Create a CUQIpy model
29
+ A = LinearModel(np.array([[1.0, 1.0]]))
30
+ # Create a CUQIpy prior
31
+ x = Gaussian(mean=np.array([0.0, 0.0]), cov=np.array([1.0, 1.0]))
32
+ # Create a CUQIpy likelihood
33
+ y = Gaussian(A@x, cov=0.1)
34
+ return x, y
35
+
36
+
37
+ def likelihood_2D():
38
+ """A function to create and return a 2D likelihood"""
39
+ x, y = posterior_2D_components()
40
+ likelihood = y.to_likelihood(2)
41
+ return likelihood
42
+
43
+
44
+ def posterior_2D():
45
+ """A function to create and return a 2D posterior"""
46
+ x, y = posterior_2D_components()
47
+ joint = JointDistribution(x, y)
48
+ posterior = joint(y=2)
49
+ return posterior
50
+
51
+
52
+ @pytest.mark.parametrize("density", [
53
+ Gaussian(mean=0.0, cov=1.0),
54
+ Uniform(low=0.0, high=1.0),
55
+ Gaussian(mean=np.array([0.0, 0.0]), cov=np.array([1.0, 1.0])),
56
+ likelihood_2D(),
57
+ posterior_2D()
58
+ ])
59
+ @pytest.mark.parametrize("log_scale", [True, False])
60
+ def test_plot_density(density, log_scale):
61
+ """ Test the 1D and 2D density plot. """
62
+ if density.dim == 1:
63
+ plot_1D_density(density, -1, 1, color='green', log_scale=log_scale)
64
+ elif density.dim == 2:
65
+ plot_2D_density(
66
+ density, -3, 3, -3, 3, 60, 60, cmap='gray', log_scale=log_scale)
67
+ else:
68
+ raise ValueError("Density must be 1D or 2D.")
@@ -1,20 +0,0 @@
1
- import pytest
2
- from scipy.linalg import cholesky
3
- from scipy.sparse import diags
4
- from cuqi.utilities import sparse_cholesky
5
- import numpy as np
6
-
7
-
8
- @pytest.mark.parametrize("P", [
9
- diags([-1, 2, -1], [-1, 0, 1], shape=(128, 128)),
10
- diags([1, -4, 6, -4, 1], [-2, -1, 0, 1, 2], shape=(128, 128))
11
- ])
12
- def test_sparse_cholesky(P):
13
- """ Test the sparse Cholesky decomposition. P is a sparse matrix (often precision). """
14
- # Scipy version (on dense only)
15
- L1 = cholesky(P.toarray())
16
-
17
- # Scipy-based version from CUQIpy (on sparse)
18
- L2 = sparse_cholesky(P)
19
-
20
- assert np.allclose(L1, L2.toarray()) # Convert to dense to compare