CUQIpy 0.2.0.post0.dev116__py3-none-any.whl → 0.3.0__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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: CUQIpy
3
- Version: 0.2.0.post0.dev116
3
+ Version: 0.3.0
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>
6
6
  License: Apache License
@@ -1,8 +1,10 @@
1
- cuqi/__init__.py,sha256=rgJFSXHzrZVL95CCUjgm5dmkCaG6xrnp0fS-4edSKnc,413
1
+ cuqi/__init__.py,sha256=K0ss2HNqoLUX7wGpSZdaPKxIaKdRS452fcJm4D0pcEs,433
2
2
  cuqi/_messages.py,sha256=fzEBrZT2kbmfecBBPm7spVu7yHdxGARQB4QzXhJbCJ0,415
3
- cuqi/_version.py,sha256=SNjEw5IPKfnKQGBCskGB4jWsZDXL2xTa402h6cL-sdI,510
3
+ cuqi/_version.py,sha256=1VECjRV5lN0Bx-UB6WKu0FNCJQAq7St7aI6zsiVNhvU,497
4
4
  cuqi/config.py,sha256=wcYvz19wkeKW2EKCGIKJiTpWt5kdaxyt4imyRkvtTRA,526
5
5
  cuqi/diagnostics.py,sha256=5OrbJeqpynqRXOe5MtOKKhe7EAVdOEpHIqHnlMW9G_c,3029
6
+ cuqi/array/__init__.py,sha256=-EeiaiWGNsE3twRS4dD814BIlfxEsNkTCZUc5gjOXb0,30
7
+ cuqi/array/_array.py,sha256=s2dGA-MyrPhCqT4E0-nnglLmYL1HhbBYvhPdgC8RdFc,3968
6
8
  cuqi/data/__init__.py,sha256=EtM27CtziMif8yWpxMNoqwmvIOAhnNBjTd25H8oh9m0,161
7
9
  cuqi/data/_data.py,sha256=ou2CvDqp_HDtW8XXEQUGf5ptoAdPGwrAXubI8A9KRkM,9276
8
10
  cuqi/data/astronaut.npz,sha256=vVTb6eJLMZhrEZuOYzQWN3V2EhhVH6sHzrrf_7mstcw,786696
@@ -15,7 +17,7 @@ cuqi/distribution/__init__.py,sha256=kvEzw1EOF38GnviUv4Bq8l3IZshXvwZEqNKgdN9YHEk
15
17
  cuqi/distribution/_beta.py,sha256=xQ6nURJqB20j1A8YNnpKO9BUcb-kKUdq8QCmljlm9l4,2980
16
18
  cuqi/distribution/_cauchy_diff.py,sha256=Ka8KgnBGz7CrJL9n7pNp3aOKfZ7sxbGP8dvxzBHo5Yw,3672
17
19
  cuqi/distribution/_custom.py,sha256=-HcuBFfudA6Ntt0Vs6cixJs3vJngpOiK2hmj0Q8ozK0,10488
18
- cuqi/distribution/_distribution.py,sha256=2kdwzmvee1b8JsJEJUPRLUm38R6KU2pMpfXZCuwB9XQ,15851
20
+ cuqi/distribution/_distribution.py,sha256=8Nttupt-8kp03uiZad-IBQXqnhWRXVO4yL4xEp9sKsI,15873
19
21
  cuqi/distribution/_gamma.py,sha256=GGsbIeHQhzUb1eTNeARcLXjJqcZ5iZWvaDaNsfJv9N0,1303
20
22
  cuqi/distribution/_gaussian.py,sha256=3Lvpl_ErUpQB07AVHybdjo4IH9pUjG7dPjKq4MveBao,32588
21
23
  cuqi/distribution/_gmrf.py,sha256=2SUIEFqIkEhHLJ7oSsWXXPMIeeTBkFkGm6rFcmehJDg,10382
@@ -31,15 +33,15 @@ cuqi/distribution/_uniform.py,sha256=7xJmCZH_LPhuGkwEDGh-_CTtzcWKrXMOxtTJUFb7Ydo
31
33
  cuqi/geometry/__init__.py,sha256=2vypOJ-vRIZtbgUS1a57XSjOJq2vu11x_E5MFaE9Xm0,772
32
34
  cuqi/geometry/_geometry.py,sha256=kmzDaNqSr1WjoGo5B1TlOY2YCLOJrSzaMiT0Wc2qZI8,39613
33
35
  cuqi/likelihood/__init__.py,sha256=ZUuc7ysBoFvx-L_IKetlKgqht9k8x4kMA_iQA0yuQFM,1774
34
- cuqi/likelihood/_likelihood.py,sha256=f2CLzkr2Cp2SfMoHFU8-WXs5V_6pz6gtSkD4HHDsahw,6553
36
+ cuqi/likelihood/_likelihood.py,sha256=_eY3kKcviV7klOALGRAjVKhh1BcbhjyP4qPAJXsaJoM,6551
35
37
  cuqi/model/__init__.py,sha256=IcN4aZCnyp9o-8TNIoZ8vew99QQgi0EmZvnsIuR6qYI,49
36
- cuqi/model/_model.py,sha256=ywCTO2UulsYWJ3yF3iRVMPcnDOaB6roV_KQCgOaADKk,21525
38
+ cuqi/model/_model.py,sha256=F0_X_ZHN-GQ9Sp7HCPy8O0BsLn4epPVh9Ns_IRadeEs,21525
37
39
  cuqi/operator/__init__.py,sha256=0pc9p-KPyl7KtPV0noB0ddI0CP2iYEHw5rbw49D8Njk,136
38
40
  cuqi/operator/_operator.py,sha256=yNwPTh7jR07AiKMbMQQ5_54EgirlKFsbq9JN1EODaQI,8856
39
41
  cuqi/pde/__init__.py,sha256=NyS_ZYruCvy-Yg24qKlwm3ZIX058kLNQX9bqs-xg4ZM,99
40
42
  cuqi/pde/_pde.py,sha256=wWKwLCLQfJMcpelW0VC5S265dR8RAJEHkKKRs7Ss5rI,10337
41
43
  cuqi/problem/__init__.py,sha256=JxJty4JqHTOqSG6NeTGiXRQ7OLxiRK9jvVq3lXLeIRw,38
42
- cuqi/problem/_problem.py,sha256=KaxiWY5Og8e4SdQcYwSMre9MKzuSYj2jn_JUxtWov_8,29079
44
+ cuqi/problem/_problem.py,sha256=wWjY4aoCXP8iYiiMerpIDVUorVk2iOfPrmcukAXHvmk,29073
43
45
  cuqi/sampler/__init__.py,sha256=G2j-yknEfY-GYqCea3MRQbKBTGdRCpcgEhajtSvmn2Q,403
44
46
  cuqi/sampler/_conjugate.py,sha256=Ip3HM12j8Bq9T0925N007_o-2xrKsn6q-KMf6vqYPVs,1514
45
47
  cuqi/sampler/_conjugate_approx.py,sha256=n8kN-EQCYXAjBPEOFOjDcIqTn-zYCaGcKy30R4o-opI,1970
@@ -52,17 +54,17 @@ cuqi/sampler/_mh.py,sha256=34sgy_GHwTgtmO4tFJv_c0ujMPNj_8MveFlI0tCP6a0,7094
52
54
  cuqi/sampler/_pcn.py,sha256=0dqf-YjmEzswHJGNgneyEpMpmHZxwVGUpeiqLvQybxU,8602
53
55
  cuqi/sampler/_rto.py,sha256=kqNj3aEE0b-ZSU2gs3XOJSFrespETqH34DLEnllDV0Q,6158
54
56
  cuqi/sampler/_sampler.py,sha256=ar_DcGnvu7y4XWOkdqduBJTGvCtWzibVm2fKReWRE8Y,5709
55
- cuqi/samples/__init__.py,sha256=a1XXucotE84BqKTlDbXDFSznb7AcUCA1wERT_wUMAPY,47
56
- cuqi/samples/_samples.py,sha256=IE5ZCt-y1bISlJQ3OB2Lg4eZk6isc7Z-4DCeqkciYxc,31100
57
+ cuqi/samples/__init__.py,sha256=E7B9IBUsiOBr-HAKH9o3_Lqhq4KeWO87hmesHkdbwTY,30
58
+ cuqi/samples/_samples.py,sha256=b2XLSjVFPgjzXxWUn7ggqCIULsYH3xQBJvEUiddL790,25759
57
59
  cuqi/solver/__init__.py,sha256=K3FwAG9Z0t2893Vn50SvTmjhxy5C1whroNspyf2Xm4M,102
58
- cuqi/solver/_solver.py,sha256=hn9KUxgAauTXdk8PM0jUWgDdAexYibwSfvS10HQKuq4,19378
60
+ cuqi/solver/_solver.py,sha256=89K3aAmAWL9WBtZCOexkx6b141xQaUDu7_bvpdH-8f8,19377
59
61
  cuqi/testproblem/__init__.py,sha256=jcBGNZR3n7iJkoWmm-5zm5SDDt2UVrx8nbBV31hqH-o,205
60
- cuqi/testproblem/_testproblem.py,sha256=-d-KLv9PS_QI-uxzH3GEiJ562gKfJTISU6lPLiHMiTg,52311
62
+ cuqi/testproblem/_testproblem.py,sha256=9ZZl1Q007ZpHFTWrpk9MDymWLkAderZhkRbJOLHUkiM,52310
61
63
  cuqi/utilities/__init__.py,sha256=EfxHLdsyDNugbmbzs43nV_AeKcycM9sVBjG9WZydagA,351
62
64
  cuqi/utilities/_get_python_variable_name.py,sha256=QwlBVj2koJRA8s8pWd554p7-ElcI7HUwY32HknaR92E,1827
63
- cuqi/utilities/_utilities.py,sha256=qJhqhD4kYxgtuHM8uykxf-IxIE_W5ZHht2yij1Z4P4k,7975
64
- CUQIpy-0.2.0.post0.dev116.dist-info/LICENSE,sha256=kJWRPrtRoQoZGXyyvu50Uc91X6_0XRaVfT0YZssicys,10799
65
- CUQIpy-0.2.0.post0.dev116.dist-info/METADATA,sha256=TUC32pKap7AQknBo9T_lfE9Grndv3nAdVgqRCbmp1Ys,16480
66
- CUQIpy-0.2.0.post0.dev116.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
67
- CUQIpy-0.2.0.post0.dev116.dist-info/top_level.txt,sha256=AgmgMc6TKfPPqbjV0kvAoCBN334i_Lwwojc7HE3ZwD0,5
68
- CUQIpy-0.2.0.post0.dev116.dist-info/RECORD,,
65
+ cuqi/utilities/_utilities.py,sha256=rjycaxDWExdskIfYXV1z5ZlB0JTlqv3tCmKf08i6U5c,7973
66
+ CUQIpy-0.3.0.dist-info/LICENSE,sha256=kJWRPrtRoQoZGXyyvu50Uc91X6_0XRaVfT0YZssicys,10799
67
+ CUQIpy-0.3.0.dist-info/METADATA,sha256=RR3_xhUO4mqnzJS8UOKLaXmka4Pbwuf-mMcS-HHpVLE,16467
68
+ CUQIpy-0.3.0.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
69
+ CUQIpy-0.3.0.dist-info/top_level.txt,sha256=AgmgMc6TKfPPqbjV0kvAoCBN334i_Lwwojc7HE3ZwD0,5
70
+ CUQIpy-0.3.0.dist-info/RECORD,,
cuqi/__init__.py CHANGED
@@ -9,6 +9,7 @@ from . import operator
9
9
  from . import pde
10
10
  from . import problem
11
11
  from . import sampler
12
+ from . import array
12
13
  from . import samples
13
14
  from . import solver
14
15
  from . import testproblem
cuqi/_version.py CHANGED
@@ -8,11 +8,11 @@ import json
8
8
 
9
9
  version_json = '''
10
10
  {
11
- "date": "2023-02-22T10:07:14+0100",
11
+ "date": "2023-02-22T10:23:18+0100",
12
12
  "dirty": false,
13
13
  "error": null,
14
- "full-revisionid": "336a1e76c4a7d7896ccb72c51fc5a4487c00eb1b",
15
- "version": "0.2.0.post0.dev116"
14
+ "full-revisionid": "e122312d87e155e56afba81b81b0b880b545f99f",
15
+ "version": "0.3.0"
16
16
  }
17
17
  ''' # END VERSION_JSON
18
18
 
cuqi/array/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from ._array import CUQIarray
cuqi/array/_array.py ADDED
@@ -0,0 +1,108 @@
1
+ import numpy as np
2
+ from cuqi.geometry import _DefaultGeometry
3
+
4
+
5
+ class CUQIarray(np.ndarray):
6
+ """
7
+ A class to represent data arrays, subclassed from numpy array, along with geometry and plotting
8
+
9
+ Parameters
10
+ ----------
11
+ input_array : ndarray
12
+ A numpy array holding the parameter or function values.
13
+
14
+ is_par : bool, default True
15
+ Boolean flag whether input_array is to be interpreted as parameter (True) or function values (False).
16
+
17
+ geometry : cuqi.geometry.Geometry, default None
18
+ Contains the geometry related of the data
19
+
20
+ Attributes
21
+ ----------
22
+ funvals : CUQIarray
23
+ Returns itself as function values.
24
+
25
+ parameters : CUQIarray
26
+ Returns itself as parameters.
27
+
28
+ Methods
29
+ ----------
30
+ :meth:`plot`: Plots the data as function or parameters.
31
+ """
32
+
33
+ def __repr__(self) -> str:
34
+ return "CUQIarray: NumPy array wrapped with geometry.\n" + \
35
+ "---------------------------------------------\n\n" + \
36
+ "Geometry:\n {}\n\n".format(self.geometry) + \
37
+ "Parameters:\n {}\n\n".format(self.is_par) + \
38
+ "Array:\n" + \
39
+ super().__repr__()
40
+
41
+ def __new__(cls, input_array, is_par=True, geometry=None):
42
+ # Input array is an already formed ndarray instance
43
+ # We first cast to be our class type
44
+ obj = np.asarray(input_array).view(cls)
45
+ # add the new attribute to the created instance
46
+ obj.is_par = is_par
47
+ if (not is_par) and (geometry is None):
48
+ raise ValueError("geometry cannot be none when initializing a CUQIarray as function values (with is_par False).")
49
+ if is_par and (obj.ndim>1):
50
+ raise ValueError("input_array cannot be multidimensional when initializing CUQIarray as parameter (with is_par True).")
51
+ if geometry is None:
52
+ geometry = _DefaultGeometry(grid=obj.__len__())
53
+ obj.geometry = geometry
54
+ # Finally, we must return the newly created object:
55
+ return obj
56
+
57
+ def __array_finalize__(self, obj):
58
+ # see InfoArray.__array_finalize__ for comments
59
+ if obj is None: return
60
+ self.is_par = getattr(obj, 'is_par', True)
61
+ self.geometry = getattr(obj, 'geometry', None)
62
+
63
+ @property
64
+ def funvals(self):
65
+ if self.is_par is True:
66
+ vals = self.geometry.par2fun(self)
67
+ else:
68
+ vals = self
69
+
70
+ if isinstance(vals, np.ndarray):
71
+ if vals.dtype == np.dtype('O'):
72
+ # if vals is of type np.ndarray, but the data type of the array
73
+ # is object (e.g. FEniCS function), then extract the object and
74
+ # return it. reshape(1) is needed to convert the shape from
75
+ # () to (1,).
76
+ return self.reshape(1)[0]
77
+ else:
78
+ # else, cast the np.ndarray to a CUQIarray
79
+ return type(self)(vals,is_par=False,geometry=self.geometry) #vals.view(np.ndarray)
80
+ else:
81
+ return vals
82
+
83
+ @property
84
+ def parameters(self):
85
+ if self.is_par is False:
86
+ vals = self.geometry.fun2par(self)
87
+ else:
88
+ vals = self
89
+ return type(self)(vals,is_par=True,geometry=self.geometry)
90
+
91
+ def to_numpy(self):
92
+ """Return a numpy array of the CUQIarray data. If is_par is True, then
93
+ the parameters are returned as numpy.ndarray. If is_par is False, then
94
+ the function values are returned instead.
95
+ """
96
+ try:
97
+ return self.view(np.ndarray)
98
+ except:
99
+ raise ValueError(
100
+ f"Cannot convert {self.__class__.__name__} to numpy array")
101
+
102
+ def plot(self, plot_par=False, **kwargs):
103
+ if plot_par:
104
+ kwargs["is_par"]=True
105
+ return self.geometry.plot(self.parameters, plot_par=plot_par, **kwargs)
106
+ else:
107
+ kwargs["is_par"]=False
108
+ return self.geometry.plot(self.funvals, **kwargs)
@@ -5,7 +5,8 @@ from copy import copy
5
5
  from functools import partial
6
6
  from cuqi.density import Density, EvaluatedDensity
7
7
  from cuqi.likelihood import Likelihood
8
- from cuqi.samples import Samples, CUQIarray
8
+ from cuqi.samples import Samples
9
+ from cuqi.array import CUQIarray
9
10
  from cuqi.geometry import _DefaultGeometry, Geometry
10
11
  from cuqi.utilities import infer_len, get_writeable_attributes, get_writeable_properties, get_non_default_args, get_indirect_variables
11
12
  import numpy as np # To be replaced by cuqi.array_api
@@ -21,7 +21,7 @@ class Likelihood(Density):
21
21
  distribution : ~cuqi.distribution.Distribution
22
22
  | Distribution to create likelihood from.
23
23
 
24
- data : ~cuqi.samples.CUQIarray or array_like
24
+ data : ~cuqi.array.CUQIarray or array_like
25
25
  | Observation to create likelihood from.
26
26
 
27
27
  """
cuqi/model/_model.py CHANGED
@@ -2,7 +2,8 @@ import numpy as np
2
2
  from scipy.sparse import csc_matrix
3
3
  from scipy.sparse import hstack
4
4
  from scipy.linalg import solve
5
- from cuqi.samples import Samples, CUQIarray
5
+ from cuqi.samples import Samples
6
+ from cuqi.array import CUQIarray
6
7
  from cuqi.geometry import Geometry, _DefaultGeometry, _get_identity_geometries
7
8
  import cuqi
8
9
  import matplotlib.pyplot as plt
@@ -93,7 +94,7 @@ class Model(object):
93
94
 
94
95
  Parameters
95
96
  ----------
96
- x : ndarray or cuqi.samples.CUQIarray
97
+ x : ndarray or cuqi.array.CUQIarray
97
98
  The input value to be converted.
98
99
 
99
100
  geometry : cuqi.geometry.Geometry
@@ -105,7 +106,7 @@ class Model(object):
105
106
 
106
107
  Returns
107
108
  -------
108
- ndarray or cuqi.samples.CUQIarray
109
+ ndarray or cuqi.array.CUQIarray
109
110
  The input value represented as a function.
110
111
  """
111
112
  if type(x) is CUQIarray and not isinstance(x.geometry, _DefaultGeometry):
@@ -120,18 +121,18 @@ class Model(object):
120
121
 
121
122
  Parameters
122
123
  ----------
123
- out : ndarray or cuqi.samples.CUQIarray
124
+ out : ndarray or cuqi.array.CUQIarray
124
125
  The output value to be converted.
125
126
 
126
127
  geometry : cuqi.geometry.Geometry
127
128
  The geometry representing the argument `out`.
128
129
 
129
130
  to_CUQIarray : bool
130
- If True, the output is wrapped as a cuqi.samples.CUQIarray.
131
+ If True, the output is wrapped as a cuqi.array.CUQIarray.
131
132
 
132
133
  Returns
133
134
  -------
134
- ndarray or cuqi.samples.CUQIarray
135
+ ndarray or cuqi.array.CUQIarray
135
136
  The output value represented as parameters.
136
137
  """
137
138
  out = geometry.fun2par(out)
@@ -156,7 +157,7 @@ class Model(object):
156
157
  func_domain_geometry : cuqi.geometry.Geometry
157
158
  The geometry representing the function `func` domain.
158
159
 
159
- x : ndarray or cuqi.samples.CUQIarray
160
+ x : ndarray or cuqi.array.CUQIarray
160
161
  The input value to the operator.
161
162
 
162
163
  is_par : bool
@@ -165,7 +166,7 @@ class Model(object):
165
166
 
166
167
  Returns
167
168
  -------
168
- ndarray or cuqi.samples.CUQIarray
169
+ ndarray or cuqi.array.CUQIarray
169
170
  The output of the function `func` converted to parameters.
170
171
  """
171
172
  # If input x is Samples we apply func for each sample
@@ -211,7 +212,7 @@ class Model(object):
211
212
 
212
213
  Parameters
213
214
  ----------
214
- *args : ndarray or cuqi.samples.CUQIarray
215
+ *args : ndarray or cuqi.array.CUQIarray
215
216
  The model input.
216
217
 
217
218
  is_par : bool
@@ -223,7 +224,7 @@ class Model(object):
223
224
 
224
225
  Returns
225
226
  -------
226
- ndarray or cuqi.samples.CUQIarray
227
+ ndarray or cuqi.array.CUQIarray
227
228
  The model output. Always returned as parameters.
228
229
  """
229
230
 
@@ -426,12 +427,12 @@ class LinearModel(Model):
426
427
 
427
428
  Parameters
428
429
  ----------
429
- y : ndarray or cuqi.samples.CUQIarray
430
+ y : ndarray or cuqi.array.CUQIarray
430
431
  The adjoint model input.
431
432
 
432
433
  Returns
433
434
  -------
434
- ndarray or cuqi.samples.CUQIarray
435
+ ndarray or cuqi.array.CUQIarray
435
436
  The adjoint model output. Always returned as parameters.
436
437
  """
437
438
  return self._apply_func(self._adjoint_func,
cuqi/problem/_problem.py CHANGED
@@ -11,7 +11,7 @@ from cuqi.model import LinearModel, Model
11
11
  from cuqi.likelihood import Likelihood
12
12
  from cuqi.geometry import _DefaultGeometry
13
13
  from cuqi.utilities import ProblemInfo
14
- from cuqi.samples import CUQIarray
14
+ from cuqi.array import CUQIarray
15
15
 
16
16
  from copy import copy
17
17
 
@@ -228,7 +228,7 @@ class BayesianProblem(object):
228
228
  x_ML, solver_info = self._solve_max_point(self.likelihood, disp=disp, x0=x0)
229
229
 
230
230
  # Wrap the result in a CUQIarray and add solver info
231
- x_ML = cuqi.samples.CUQIarray(x_ML, geometry=self.likelihood.geometry)
231
+ x_ML = cuqi.array.CUQIarray(x_ML, geometry=self.likelihood.geometry)
232
232
  x_ML.info = solver_info
233
233
 
234
234
  return x_ML
@@ -285,7 +285,7 @@ class BayesianProblem(object):
285
285
  x_MAP, solver_info = self._solve_max_point(self.posterior, disp=disp, x0=x0)
286
286
 
287
287
  # Wrap the result in a CUQIarray and add solver info
288
- x_MAP = cuqi.samples.CUQIarray(x_MAP, geometry=self.posterior.geometry)
288
+ x_MAP = cuqi.array.CUQIarray(x_MAP, geometry=self.posterior.geometry)
289
289
  x_MAP.info = solver_info
290
290
  return x_MAP
291
291
 
cuqi/samples/__init__.py CHANGED
@@ -1 +1 @@
1
- from ._samples import CUQIarray, Samples, Data
1
+ from ._samples import Samples
cuqi/samples/_samples.py CHANGED
@@ -1,168 +1,12 @@
1
- import warnings
2
1
  import numpy as np
3
2
  import matplotlib.pyplot as plt
4
3
  from cuqi.diagnostics import Geweke
5
4
  from cuqi.geometry import _DefaultGeometry, Continuous2D, Image2D
5
+ from cuqi.array import CUQIarray
6
6
  from copy import copy
7
7
  import arviz # Plotting tool
8
8
 
9
9
 
10
- class CUQIarray(np.ndarray):
11
- """
12
- A class to represent data arrays, subclassed from numpy array, along with geometry and plotting
13
-
14
- Parameters
15
- ----------
16
- input_array : ndarray
17
- A numpy array holding the parameter or function values.
18
-
19
- is_par : bool, default True
20
- Boolean flag whether input_array is to be interpreted as parameter (True) or function values (False).
21
-
22
- geometry : cuqi.geometry.Geometry, default None
23
- Contains the geometry related of the data
24
-
25
- Attributes
26
- ----------
27
- funvals : CUQIarray
28
- Returns itself as function values.
29
-
30
- parameters : CUQIarray
31
- Returns itself as parameters.
32
-
33
- Methods
34
- ----------
35
- :meth:`plot`: Plots the data as function or parameters.
36
- """
37
-
38
- def __repr__(self) -> str:
39
- return "CUQIarray: NumPy array wrapped with geometry.\n" + \
40
- "---------------------------------------------\n\n" + \
41
- "Geometry:\n {}\n\n".format(self.geometry) + \
42
- "Parameters:\n {}\n\n".format(self.is_par) + \
43
- "Array:\n" + \
44
- super().__repr__()
45
-
46
- def __new__(cls, input_array, is_par=True, geometry=None):
47
- # Input array is an already formed ndarray instance
48
- # We first cast to be our class type
49
- obj = np.asarray(input_array).view(cls)
50
- # add the new attribute to the created instance
51
- obj.is_par = is_par
52
- if (not is_par) and (geometry is None):
53
- raise ValueError("geometry cannot be none when initializing a CUQIarray as function values (with is_par False).")
54
- if is_par and (obj.ndim>1):
55
- raise ValueError("input_array cannot be multidimensional when initializing CUQIarray as parameter (with is_par True).")
56
- if geometry is None:
57
- geometry = _DefaultGeometry(grid=obj.__len__())
58
- obj.geometry = geometry
59
- # Finally, we must return the newly created object:
60
- return obj
61
-
62
- def __array_finalize__(self, obj):
63
- # see InfoArray.__array_finalize__ for comments
64
- if obj is None: return
65
- self.is_par = getattr(obj, 'is_par', True)
66
- self.geometry = getattr(obj, 'geometry', None)
67
-
68
- @property
69
- def funvals(self):
70
- if self.is_par is True:
71
- vals = self.geometry.par2fun(self)
72
- else:
73
- vals = self
74
-
75
- if isinstance(vals, np.ndarray):
76
- if vals.dtype == np.dtype('O'):
77
- # if vals is of type np.ndarray, but the data type of the array
78
- # is object (e.g. FEniCS function), then extract the object and
79
- # return it. reshape(1) is needed to convert the shape from
80
- # () to (1,).
81
- return self.reshape(1)[0]
82
- else:
83
- # else, cast the np.ndarray to a CUQIarray
84
- return type(self)(vals,is_par=False,geometry=self.geometry) #vals.view(np.ndarray)
85
- else:
86
- return vals
87
-
88
- @property
89
- def parameters(self):
90
- if self.is_par is False:
91
- vals = self.geometry.fun2par(self)
92
- else:
93
- vals = self
94
- return type(self)(vals,is_par=True,geometry=self.geometry)
95
-
96
- def to_numpy(self):
97
- """Return a numpy array of the CUQIarray data. If is_par is True, then
98
- the parameters are returned as numpy.ndarray. If is_par is False, then
99
- the function values are returned instead.
100
- """
101
- try:
102
- return self.view(np.ndarray)
103
- except:
104
- raise ValueError(
105
- f"Cannot convert {self.__class__.__name__} to numpy array")
106
-
107
- def plot(self, plot_par=False, **kwargs):
108
- if plot_par:
109
- kwargs["is_par"]=True
110
- return self.geometry.plot(self.parameters, plot_par=plot_par, **kwargs)
111
- else:
112
- kwargs["is_par"]=False
113
- return self.geometry.plot(self.funvals, **kwargs)
114
-
115
-
116
- class Data(object):
117
- """
118
- An container type object to represent data objects equipped with geometry.
119
- """
120
-
121
- def __init__(self, parameters=None, geometry=None, funvals=None):
122
-
123
- # Allow setting either parameter or function values, but not both.
124
- # If both provided, function values take precedence (to be confirmed).
125
- if parameters is not None and funvals is not None:
126
- parameters = None
127
-
128
- if parameters is not None:
129
- self.parameters = parameters
130
-
131
- if funvals is not None:
132
- self.funvals = funvals
133
-
134
- self.geometry = geometry
135
-
136
- def plot(self, plot_par=False, **kwargs):
137
- if plot_par:
138
- self.geometry.plot(self.parameters, plot_par=plot_par, is_par=True, **kwargs)
139
- else:
140
- self.geometry.plot(self.funvals, is_par=False, **kwargs)
141
-
142
- @property
143
- def parameters(self):
144
- return self._parameters
145
-
146
- @parameters.setter
147
- def parameters(self, value):
148
- self._parameters = value
149
- self.has_parameters = True
150
- self.has_funvals = False
151
-
152
- @property
153
- def funvals(self):
154
- if self.has_funvals:
155
- return self._funvals
156
- else:
157
- return self.geometry.par2fun(self.parameters)
158
-
159
- @funvals.setter
160
- def funvals(self, value):
161
- self.has_funvals = True
162
- self.has_parameters = False
163
- self._funvals = value
164
-
165
-
166
10
  class Samples(object):
167
11
  """
168
12
  An object used to store samples from distributions.
cuqi/solver/_solver.py CHANGED
@@ -4,7 +4,7 @@ from scipy.optimize import fmin_l_bfgs_b, least_squares
4
4
  import scipy.optimize as opt
5
5
  import scipy.sparse as spa
6
6
 
7
- from cuqi.samples import CUQIarray
7
+ from cuqi.array import CUQIarray
8
8
  from cuqi import config
9
9
  eps = np.finfo(float).eps
10
10
 
@@ -564,4 +564,4 @@ class LM(object):
564
564
  class PDHG(object):
565
565
  """Primal-Dual Hybrid Gradient algorithm."""
566
566
  def __init__(self):
567
- raise NotImplementedError
567
+ raise NotImplementedError
@@ -10,10 +10,11 @@ from cuqi.model import LinearModel
10
10
  from cuqi.distribution import Gaussian
11
11
  from cuqi.problem import BayesianProblem
12
12
  from cuqi.geometry import Geometry, MappedGeometry, StepExpansion, KLExpansion, KLExpansion_Full, CustomKL, Continuous1D, Continuous2D, Image2D
13
- from cuqi.samples import CUQIarray
13
+ from cuqi.array import CUQIarray
14
14
  import warnings
15
15
 
16
16
 
17
+
17
18
  #=============================================================================
18
19
  class _Deblur(BayesianProblem):
19
20
  """
@@ -1,4 +1,4 @@
1
- from cuqi.samples import CUQIarray
1
+ from cuqi.array import CUQIarray
2
2
  import numpy as np
3
3
  import inspect
4
4
  from numbers import Number