CUQIpy 1.3.0.post0.dev237__py3-none-any.whl → 1.3.0.post0.dev266__py3-none-any.whl

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.

cuqi/_version.py CHANGED
@@ -8,11 +8,11 @@ import json
8
8
 
9
9
  version_json = '''
10
10
  {
11
- "date": "2025-05-15T09:06:20+0300",
11
+ "date": "2025-06-23T11:22:39+0300",
12
12
  "dirty": false,
13
13
  "error": null,
14
- "full-revisionid": "b9a5dd37abfe2ee51d5594630de54dd3469bfeca",
15
- "version": "1.3.0.post0.dev237"
14
+ "full-revisionid": "0ad94a56cb0776e3b0964f9aebbc01c1b7b41ac1",
15
+ "version": "1.3.0.post0.dev266"
16
16
  }
17
17
  ''' # END VERSION_JSON
18
18
 
@@ -2,3 +2,4 @@
2
2
  from . import mcmc
3
3
  from . import algebra
4
4
  from . import geometry
5
+ from ._recommender import SamplerRecommender
@@ -0,0 +1,200 @@
1
+ import cuqi
2
+ import inspect
3
+ import numpy as np
4
+
5
+ # This import makes suggest_sampler easier to read
6
+ import cuqi.experimental.mcmc as samplers
7
+
8
+
9
+ class SamplerRecommender(object):
10
+ """
11
+ This class can be used to automatically choose a sampler.
12
+
13
+ Parameters
14
+ ----------
15
+ target: Density or JointDistribution
16
+ Distribution to get sampler recommendations for.
17
+
18
+ exceptions: list[cuqi.experimental.mcmc.Sampler], *optional*
19
+ Samplers not to be recommended.
20
+ """
21
+
22
+ def __init__(self, target:cuqi.density.Density, exceptions = []):
23
+ self._target = target
24
+ self._exceptions = exceptions
25
+ self._create_ordering()
26
+
27
+ @property
28
+ def target(self) -> cuqi.density.Density:
29
+ """ Return the target Distribution. """
30
+ return self._target
31
+
32
+ @target.setter
33
+ def target(self, value:cuqi.density.Density):
34
+ """ Set the target Distribution. Runs validation of the target. """
35
+ if value is None:
36
+ raise ValueError("Target needs to be of type cuqi.density.Density.")
37
+ self._target = value
38
+
39
+ def _create_ordering(self):
40
+ """
41
+ Every element in the ordering consists of a tuple:
42
+ (
43
+ Sampler: Class
44
+ boolean: additional conditions on the target
45
+ parameters: additional parameters to be passed to the sampler once initialized
46
+ )
47
+ """
48
+ number_of_components = np.sum(self._target.dim)
49
+
50
+ self._ordering = [
51
+ # Direct and Conjugate samplers
52
+ (samplers.Direct, True, {}),
53
+ (samplers.Conjugate, True, {}),
54
+ (samplers.ConjugateApprox, True, {}),
55
+ # Specialized samplers
56
+ (samplers.LinearRTO, True, {}),
57
+ (samplers.RegularizedLinearRTO, True, {}),
58
+ (samplers.UGLA, True, {}),
59
+ # Gradient.based samplers (Hamiltonian and Langevin)
60
+ (samplers.NUTS, True, {}),
61
+ (samplers.MALA, True, {}),
62
+ (samplers.ULA, True, {}),
63
+ # Gibbs and Componentwise samplers
64
+ (samplers.HybridGibbs, True, {"sampling_strategy" : self.recommend_HybridGibbs_sampling_strategy(as_string = False)}),
65
+ (samplers.CWMH, number_of_components <= 100, {"scale" : 0.05*np.ones(number_of_components),
66
+ "initial_point" : 0.5*np.ones(number_of_components)}),
67
+ # Proposal based samplers
68
+ (samplers.PCN, True, {"scale" : 0.02}),
69
+ (samplers.MH, number_of_components <= 1000, {}),
70
+ ]
71
+
72
+ @property
73
+ def ordering(self):
74
+ """ Returns the ordered list of recommendation rules used by the recommender. """
75
+ return self._ordering
76
+
77
+ def valid_samplers(self, as_string = True):
78
+ """
79
+ Finds all possible samplers that can be used for sampling from the target distribution.
80
+
81
+ Parameters
82
+ ----------
83
+
84
+ as_string : boolean
85
+ Whether to return the name of the sampler as a string instead of instantiating a sampler. *Optional*
86
+
87
+ """
88
+
89
+ all_samplers = [(name, cls) for name, cls in inspect.getmembers(cuqi.experimental.mcmc, inspect.isclass) if issubclass(cls, cuqi.experimental.mcmc.Sampler)]
90
+ valid_samplers = []
91
+
92
+ for name, sampler in all_samplers:
93
+ try:
94
+ sampler(self.target)
95
+ valid_samplers += [name if as_string else sampler]
96
+ except:
97
+ pass
98
+
99
+ # Need a separate case for HybridGibbs
100
+ if self.valid_HybridGibbs_sampling_strategy() is not None:
101
+ valid_samplers += [cuqi.experimental.mcmc.HybridGibbs.__name__ if as_string else cuqi.experimental.mcmc.HybridGibbs]
102
+
103
+ return valid_samplers
104
+
105
+
106
+ def valid_HybridGibbs_sampling_strategy(self, as_string = True):
107
+ """
108
+ Find all possible sampling strategies to be used with the HybridGibbs sampler.
109
+ Returns None if no sampler could be suggested for at least one conditional distribution.
110
+
111
+ Parameters
112
+ ----------
113
+
114
+ as_string : boolean
115
+ Whether to return the name of the samplers in the sampling strategy as a string instead of instantiating samplers. *Optional*
116
+
117
+
118
+ """
119
+
120
+ if not isinstance(self.target, cuqi.distribution.JointDistribution):
121
+ return None
122
+
123
+ par_names = self.target.get_parameter_names()
124
+
125
+ valid_samplers = dict()
126
+ for par_name in par_names:
127
+ conditional_params = {par_name_: np.ones(self.target.dim[i]) for i, par_name_ in enumerate(par_names) if par_name_ != par_name}
128
+ conditional = self.target(**conditional_params)
129
+
130
+ recommender = SamplerRecommender(conditional)
131
+ samplers = recommender.valid_samplers(as_string)
132
+ if len(samplers) == 0:
133
+ return None
134
+
135
+ valid_samplers[par_name] = samplers
136
+
137
+ return valid_samplers
138
+
139
+
140
+ def recommend(self, as_string = False):
141
+ """
142
+ Suggests a possible sampler that can be used for sampling from the target distribution.
143
+ Return None if no sampler could be suggested.
144
+
145
+ Parameters
146
+ ----------
147
+
148
+ as_string : boolean
149
+ Whether to return the name of the sampler as a string instead of instantiating a sampler. *Optional*
150
+
151
+ """
152
+
153
+ valid_samplers = self.valid_samplers(as_string = False)
154
+
155
+ for suggestion, flag, values in self._ordering:
156
+ if flag and (suggestion in valid_samplers) and (suggestion not in self._exceptions):
157
+ # Sampler found
158
+ if as_string:
159
+ return suggestion.__name__
160
+ else:
161
+ return suggestion(self.target, **values)
162
+
163
+ # No sampler can be suggested
164
+ raise ValueError("Cannot suggest any sampler. Either the provided distribution is incorrectly defined or there are too many exceptions provided.")
165
+
166
+ def recommend_HybridGibbs_sampling_strategy(self, as_string = False):
167
+ """
168
+ Suggests a possible sampling strategy to be used with the HybridGibbs sampler.
169
+ Returns None if no sampler could be suggested for at least one conditional distribution.
170
+
171
+ Parameters
172
+ ----------
173
+
174
+ target : `cuqi.distribution.JointDistribution`
175
+ The target distribution get a sampling strategy for.
176
+
177
+ as_string : boolean
178
+ Whether to return the name of the samplers in the sampling strategy as a string instead of instantiating samplers. *Optional*
179
+
180
+ """
181
+
182
+ if not isinstance(self.target, cuqi.distribution.JointDistribution):
183
+ return None
184
+
185
+ par_names = self.target.get_parameter_names()
186
+
187
+ suggested_samplers = dict()
188
+ for par_name in par_names:
189
+ conditional_params = {par_name_: np.ones(self.target.dim[i]) for i, par_name_ in enumerate(par_names) if par_name_ != par_name}
190
+ conditional = self.target(**conditional_params)
191
+
192
+ recommender = SamplerRecommender(conditional, exceptions = self._exceptions.copy())
193
+ sampler = recommender.recommend(as_string = as_string)
194
+
195
+ if sampler is None:
196
+ return None
197
+
198
+ suggested_samplers[par_name] = sampler
199
+
200
+ return suggested_samplers
@@ -120,4 +120,3 @@ from ._gibbs import HybridGibbs
120
120
  from ._conjugate import Conjugate
121
121
  from ._conjugate_approx import ConjugateApprox
122
122
  from ._direct import Direct
123
- from ._utilities import find_valid_samplers
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: CUQIpy
3
- Version: 1.3.0.post0.dev237
3
+ Version: 1.3.0.post0.dev266
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
  cuqi/__init__.py,sha256=LsGilhl-hBLEn6Glt8S_l0OJzAA1sKit_rui8h-D-p0,488
2
2
  cuqi/_messages.py,sha256=fzEBrZT2kbmfecBBPm7spVu7yHdxGARQB4QzXhJbCJ0,415
3
- cuqi/_version.py,sha256=T-xOiDXchZq_KrCheA_86YhVx3HMHSXQflIX3VRywVQ,510
3
+ cuqi/_version.py,sha256=Fy9rwbT3nuGJzIHPUCFz7PEcw7IArqAqvMtmm3okneE,510
4
4
  cuqi/config.py,sha256=wcYvz19wkeKW2EKCGIKJiTpWt5kdaxyt4imyRkvtTRA,526
5
5
  cuqi/diagnostics.py,sha256=5OrbJeqpynqRXOe5MtOKKhe7EAVdOEpHIqHnlMW9G_c,3029
6
6
  cuqi/array/__init__.py,sha256=-EeiaiWGNsE3twRS4dD814BIlfxEsNkTCZUc5gjOXb0,30
@@ -34,14 +34,15 @@ cuqi/distribution/_posterior.py,sha256=zAfL0GECxekZ2lBt1W6_LN0U_xskMwK4VNce5xAF7
34
34
  cuqi/distribution/_smoothed_laplace.py,sha256=p-1Y23mYA9omwiHGkEuv3T2mwcPAAoNlCr7T8osNkjE,2925
35
35
  cuqi/distribution/_truncated_normal.py,sha256=_ez3MmO6qpBeP6BKCUlW3IgxuF7k--A7jPGPUhtYK0g,4240
36
36
  cuqi/distribution/_uniform.py,sha256=fVgj_4SBav8JMc1pNAO1l_CZ9ZwdoMIpN9iQ3i9_Z0Q,3255
37
- cuqi/experimental/__init__.py,sha256=bIQ9OroeitHbwgNe3wI_JvzkILK0N25Tt7wpquPoU3w,129
37
+ cuqi/experimental/__init__.py,sha256=9DidfQuoFPr8DnhYzI78N2J0fT4pp-jNle0Rou1fcrM,174
38
+ cuqi/experimental/_recommender.py,sha256=IawSXwsaYs0T7t6SIXjchX4sj5D1rwbs3bs_nMXAxD0,7400
38
39
  cuqi/experimental/algebra/__init__.py,sha256=btRAWG58ZfdtK0afXKOg60AX7d76KMBjlZa4AWBCCgU,81
39
40
  cuqi/experimental/algebra/_ast.py,sha256=PdPz19cJMjvnMx4KEzhn4gvxIZX_UViE33Mbttj_5Xw,9873
40
41
  cuqi/experimental/algebra/_orderedset.py,sha256=fKysh4pmI4xF7Y5Z6O86ABzg20o4uBs-v8jmLBMrdpo,2849
41
42
  cuqi/experimental/algebra/_randomvariable.py,sha256=isbFtIWsWXF-yF5Vb56nLy4MCkQM6akjd-dQau4wfbE,19725
42
43
  cuqi/experimental/geometry/__init__.py,sha256=kgoKegfz3Jhr7fpORB_l55z9zLZRtloTLyXFDh1oF2o,47
43
44
  cuqi/experimental/geometry/_productgeometry.py,sha256=IlBmmKsWE-aRZHp6no9gUXGRfkHlgM0CdPBx1hax9HI,7199
44
- cuqi/experimental/mcmc/__init__.py,sha256=zSqLZmxOqQ-F94C9-gPv7g89TX1XxlrlNm071Eb167I,4487
45
+ cuqi/experimental/mcmc/__init__.py,sha256=hTAssTgtgLhdVZLFX7hpLJYtWmXrmfXb4bWee6ELqwE,4443
45
46
  cuqi/experimental/mcmc/_conjugate.py,sha256=vqucxC--pihBCUcupdcIo4ymDPPjmMKGb7OL1THjaKE,22059
46
47
  cuqi/experimental/mcmc/_conjugate_approx.py,sha256=jmxe2FEbO9fwpc8opyjJ2px0oed3dGyj0qDwyHo4aOk,3545
47
48
  cuqi/experimental/mcmc/_cwmh.py,sha256=cAvtc3tex53ZUKPMGwj2RIkHAZurpqphko8nk8_DmJs,7340
@@ -54,7 +55,6 @@ cuqi/experimental/mcmc/_mh.py,sha256=MXo0ahXP4KGFkaY4HtvcBE-TMQzsMlTmLKzSvpz7drU
54
55
  cuqi/experimental/mcmc/_pcn.py,sha256=wqJBZLuRFSwxihaI53tumAg6AWVuceLMOmXssTetd1A,3374
55
56
  cuqi/experimental/mcmc/_rto.py,sha256=BY55Njw3-dcmjd-V1vQ58CisEDllQ8zaEj92pWB6LCM,15158
56
57
  cuqi/experimental/mcmc/_sampler.py,sha256=VK-VsPRaYET43C5quhu2f1OstEX5DKYKVyjKABTRHZE,20288
57
- cuqi/experimental/mcmc/_utilities.py,sha256=kUzHbhIS3HYZRbneNBK41IogUYX5dS_bJxqEGm7TQBI,525
58
58
  cuqi/geometry/__init__.py,sha256=Tz1WGzZBY-QGH3c0GiyKm9XHN8MGGcnU6TUHLZkzB3o,842
59
59
  cuqi/geometry/_geometry.py,sha256=W-oQTZPelVS7fN9qZj6bNBuh-yY0eqOHJ39UwB-WmQY,47562
60
60
  cuqi/implicitprior/__init__.py,sha256=6z3lvw-tWDyjZSpB3pYzvijSMK9Zlf1IYqOVTtMD2h4,309
@@ -93,8 +93,8 @@ cuqi/testproblem/_testproblem.py,sha256=EJWG_zXUtmo6GlHBZFqHlRpDC_48tE0XZEu0_C66
93
93
  cuqi/utilities/__init__.py,sha256=d5QXRzmI6EchS9T4b7eTezSisPWuWklO8ey4YBx9kI0,569
94
94
  cuqi/utilities/_get_python_variable_name.py,sha256=wxpCaj9f3ZtBNqlGmmuGiITgBaTsY-r94lUIlK6UAU4,2043
95
95
  cuqi/utilities/_utilities.py,sha256=as8cFswoxROS0Z7WUKzLIE-ZtEKCXes5M3Gdmmb47No,18414
96
- cuqipy-1.3.0.post0.dev237.dist-info/licenses/LICENSE,sha256=kJWRPrtRoQoZGXyyvu50Uc91X6_0XRaVfT0YZssicys,10799
97
- cuqipy-1.3.0.post0.dev237.dist-info/METADATA,sha256=ZXvYGwj_lUbv3_vDiKRWtkRpjlAH5PfpaZqx-aMgwJg,18624
98
- cuqipy-1.3.0.post0.dev237.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
99
- cuqipy-1.3.0.post0.dev237.dist-info/top_level.txt,sha256=AgmgMc6TKfPPqbjV0kvAoCBN334i_Lwwojc7HE3ZwD0,5
100
- cuqipy-1.3.0.post0.dev237.dist-info/RECORD,,
96
+ cuqipy-1.3.0.post0.dev266.dist-info/licenses/LICENSE,sha256=kJWRPrtRoQoZGXyyvu50Uc91X6_0XRaVfT0YZssicys,10799
97
+ cuqipy-1.3.0.post0.dev266.dist-info/METADATA,sha256=m3jmxO185qu_BLJWaQh31JKe5alD02bvk7BIKSR0AOk,18624
98
+ cuqipy-1.3.0.post0.dev266.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
99
+ cuqipy-1.3.0.post0.dev266.dist-info/top_level.txt,sha256=AgmgMc6TKfPPqbjV0kvAoCBN334i_Lwwojc7HE3ZwD0,5
100
+ cuqipy-1.3.0.post0.dev266.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.7.1)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,17 +0,0 @@
1
- import cuqi
2
- import inspect
3
-
4
- def find_valid_samplers(target):
5
- """ Finds all samplers in the cuqi.experimental.mcmc module that accept the provided target. """
6
-
7
- all_samplers = [(name, cls) for name, cls in inspect.getmembers(cuqi.experimental.mcmc, inspect.isclass) if issubclass(cls, cuqi.experimental.mcmc.Sampler)]
8
- valid_samplers = []
9
-
10
- for name, sampler in all_samplers:
11
- try:
12
- sampler(target)
13
- valid_samplers += [name]
14
- except:
15
- pass
16
-
17
- return valid_samplers