Skip to content

Commit

Permalink
Merge pull request #79 from CUQI-DTU/sprint22_update_components
Browse files Browse the repository at this point in the history
Sprint22 update notebooks to reflect the change to get_components
  • Loading branch information
chaozg authored Nov 1, 2023
2 parents 8453b39 + 38d1b04 commit 92e0929
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 37 deletions.
4 changes: 2 additions & 2 deletions training/CUQIpy-CIL/demo_CT.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@
"metadata": {},
"outputs": [],
"source": [
"A, y_data, info = cuqipy_cil.testproblem.ParallelBeam2D.get_components(\n",
"A, y_data, info = cuqipy_cil.testproblem.ParallelBeam2D(\n",
" im_size=(90, 90),\n",
" det_count=128,\n",
" angles=np.linspace(0, np.pi, 90),\n",
" phantom=\"shepp-logan\"\n",
")"
").get_components()"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions training/CUQIpy-PyTorch/HMC.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@
"metadata": {},
"outputs": [],
"source": [
"A, y_data, probinfo = cuqi.testproblem.Deconvolution1D.get_components(dim=50, phantom=\"sinc\")\n",
"A, y_data, probinfo = cuqi.testproblem.Deconvolution1D(dim=50, phantom=\"sinc\").get_components()\n",
"\n",
"# CUQIpy Bayesian model\n",
"x = cuqi.distribution.Gaussian(np.zeros(A.domain_dim), cov=0.2)\n",
Expand Down Expand Up @@ -792,7 +792,7 @@
"outputs": [],
"source": [
"# CUQIpy test problem\n",
"A, y_data, probinfo = cuqi.testproblem.Deconvolution1D.get_components(dim=50, phantom=\"sinc\")\n",
"A, y_data, probinfo = cuqi.testproblem.Deconvolution1D(dim=50, phantom=\"sinc\").get_components()\n",
"\n",
"# Add forward model to PyTorch automatic differentiation framework\n",
"A = cuqipy_pytorch.model.add_to_autograd(A)\n",
Expand Down Expand Up @@ -831,7 +831,7 @@
"outputs": [],
"source": [
"# CUQIpy test problem\n",
"A, y_data, probinfo = cuqi.testproblem.Deconvolution1D.get_components(dim=50, phantom=\"sinc\")\n",
"A, y_data, probinfo = cuqi.testproblem.Deconvolution1D(dim=50, phantom=\"sinc\").get_components()\n",
"\n",
"# CUQIpy Bayesian model\n",
"x = cuqi.distribution.Gaussian(np.zeros(A.domain_dim), cov=0.2)\n",
Expand Down Expand Up @@ -859,7 +859,7 @@
"outputs": [],
"source": [
"# CUQIpy test problem\n",
"A, y_data, probinfo = cuqi.testproblem.Deconvolution1D.get_components(dim=50, phantom=\"sinc\")\n",
"A, y_data, probinfo = cuqi.testproblem.Deconvolution1D(dim=50, phantom=\"sinc\").get_components()\n",
"\n",
"# Add forward model to PyTorch automatic differentiation framework\n",
"A = cuqipy_pytorch.model.add_to_autograd(A)\n",
Expand Down
8 changes: 4 additions & 4 deletions training/Exercise01_Minimal_UQ.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"metadata": {},
"outputs": [],
"source": [
"A, y_data, probInfo = Deconvolution1D.get_components(phantom=\"sinc\")"
"A, y_data, probInfo = Deconvolution1D(phantom=\"sinc\").get_components()"
]
},
{
Expand Down Expand Up @@ -276,7 +276,7 @@
"metadata": {},
"outputs": [],
"source": [
"A, y_data, probInfo = Deconvolution1D.get_components(phantom=\"sinc\")"
"A, y_data, probInfo = Deconvolution1D(phantom=\"sinc\").get_components()"
]
},
{
Expand Down Expand Up @@ -320,7 +320,7 @@
"source": [
"and their calling signature is the same as for the `Deconvolution1D` problem, e.g. \n",
"```\n",
"model, data, probInfo = Heat1D.get_components()\n",
"model, data, probInfo = Heat1D().get_components()\n",
"```\n",
"Input arguments vary and default values are provided if left empty. Instead of checking the onlinen docs, calling help of each testproblem, e.g., `help(Heat1D)` will also describe the test problem and the inputs it accepts"
]
Expand Down Expand Up @@ -658,7 +658,7 @@
"metadata": {},
"outputs": [],
"source": [
"A, y_data, probInfo = Deconvolution1D.get_components(phantom=\"square\", PSF_param=5)"
"A, y_data, probInfo = Deconvolution1D(phantom=\"square\", PSF_param=5).get_components()"
]
},
{
Expand Down
10 changes: 5 additions & 5 deletions training/Exercise03_Forward_Models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"metadata": {},
"outputs": [],
"source": [
"model1, data1, probInfo1 = cuqi.testproblem.Deconvolution1D.get_components(dim=64, phantom=\"sinc\")"
"model1, data1, probInfo1 = cuqi.testproblem.Deconvolution1D(dim=64, phantom=\"sinc\").get_components()"
]
},
{
Expand Down Expand Up @@ -201,7 +201,7 @@
"metadata": {},
"outputs": [],
"source": [
"model2, data2, probInfo2 = cuqi.testproblem.Deconvolution2D.get_components()"
"model2, data2, probInfo2 = cuqi.testproblem.Deconvolution2D().get_components()"
]
},
{
Expand Down Expand Up @@ -301,7 +301,7 @@
"metadata": {},
"outputs": [],
"source": [
"modelH, dataH, probInfoH = cuqi.testproblem.Heat1D.get_components()"
"modelH, dataH, probInfoH = cuqi.testproblem.Heat1D().get_components()"
]
},
{
Expand Down Expand Up @@ -422,7 +422,7 @@
"metadata": {},
"outputs": [],
"source": [
"A,_,probInfo = cuqi.testproblem.Deconvolution1D.get_components(dim=64, phantom=\"sinc\")\n",
"A,_,probInfo = cuqi.testproblem.Deconvolution1D(dim=64, phantom=\"sinc\").get_components()\n",
"x_exact = probInfo.exactSolution"
]
},
Expand Down Expand Up @@ -733,7 +733,7 @@
"outputs": [],
"source": [
"n = 64\n",
"A, _, probInfo = cuqi.testproblem.Deconvolution1D.get_components(dim=n, phantom=\"sinc\")\n",
"A, _, probInfo = cuqi.testproblem.Deconvolution1D(dim=n, phantom=\"sinc\").get_components()\n",
"x_exact = probInfo.exactSolution"
]
},
Expand Down
11 changes: 6 additions & 5 deletions training/Exercise04_Bayesian_Inverse_Problems.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"outputs": [],
"source": [
"# Load forward model, data and problem information\n",
"A, y_data, probInfo = Deconvolution1D.get_components(phantom=\"sinc\")\n",
"A, y_data, probInfo = Deconvolution1D(phantom=\"sinc\").get_components()\n",
"\n",
"# For convenience, we define the dimension of the domain of A\n",
"n = A.domain_dim\n",
Expand Down Expand Up @@ -380,6 +380,7 @@
{
"attachments": {},
"cell_type": "markdown",
"id": "09194832",
"metadata": {},
"source": [
"For the purpose of this notebook we are not going to dive further into the details of the joint distribution, and simply use it to define the posterior."
Expand Down Expand Up @@ -640,7 +641,7 @@
"outputs": [],
"source": [
"# Deterministic forward model\n",
"A, y_data, probInfo = Deconvolution1D.get_components(phantom=\"sinc\")\n",
"A, y_data, probInfo = Deconvolution1D(phantom=\"sinc\").get_components()\n",
"\n",
"# Distributions for each parameter\n",
"x = GMRF(np.zeros(A.domain_dim), 50)\n",
Expand Down Expand Up @@ -703,7 +704,7 @@
"# You can modify this code or write your own from scratch\n",
"\n",
"# 1. Forward model and data\n",
"A, y_data, probInfo = Deconvolution1D.get_components(phantom=\"sinc\")\n",
"A, y_data, probInfo = Deconvolution1D(phantom=\"sinc\").get_components()\n",
"\n",
"# 2. Distributions\n",
"x = GMRF(np.zeros(A.domain_dim), 50) # Try e.g. LMRF or CMRF (also update parameters!)\n",
Expand Down Expand Up @@ -755,7 +756,7 @@
"# We redefine in case something was changed\n",
"\n",
"# Deterministic forward model\n",
"A, y_data, probInfo = Deconvolution1D.get_components(phantom=\"sinc\")\n",
"A, y_data, probInfo = Deconvolution1D(phantom=\"sinc\").get_components()\n",
"\n",
"# Distributions for each parameter\n",
"x = GMRF(np.zeros(A.domain_dim), 50)\n",
Expand Down Expand Up @@ -838,7 +839,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.11.4"
},
"vscode": {
"interpreter": {
Expand Down
28 changes: 14 additions & 14 deletions training/Exercise05_PDE.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
"source": [
"While it is possible to create a PDE model from scratch, for this notebook we use the `Heat1D` test problem that is already implemented in CUQIpy.\n",
"\n",
"We load `Heat1D` using the `get_components` method. We can explore `Heat1D` initialization parameters (which are the same parameters that can be passed to `get_components` method) by calling `Heat1D?`. "
"We may extract components of a `Heat1D` instance by calling the `get_components` method."
]
},
{
Expand All @@ -162,12 +162,12 @@
"metadata": {},
"outputs": [],
"source": [
"model, data, problemInfo = Heat1D.get_components(\n",
"model, data, problemInfo = Heat1D(\n",
" dim=N, \n",
" endpoint=L, \n",
" max_time=T, \n",
" exactSolution=myExactSolution\n",
")"
").get_components()"
]
},
{
Expand Down Expand Up @@ -307,12 +307,12 @@
"N = 30 # Number of finite difference nodes \n",
"L = 1 # Length of the domain\n",
"T = 0.02 # Final time\n",
"model, data, problemInfo = Heat1D.get_components(\n",
"model, data, problemInfo = Heat1D(\n",
" dim=N, \n",
" endpoint=L, \n",
" max_time=T, \n",
" exactSolution=myExactSolution\n",
")"
").get_components()"
]
},
{
Expand Down Expand Up @@ -504,9 +504,9 @@
"source": [
"One way to improve the solution of this Bayesian problem is to use better prior information. Here we assume the prior is a step function with three pieces (which is exactly how we created the exact solution). This also makes the Bayesian problem simpler because now we only have three Bayesian parameters to infer.\n",
"\n",
"To test this case we pass `field_type='Step'` to `Heat1D.get_components`, which creates a `StepExpansion` domain geometry for the model during initializing the `Heat1D` test problem. It is also possible to change the domain geometry manually after initializing the test problem, but we will not do that here. \n",
"To test this case we pass `field_type='Step'` to the constructor of `Heat1D`, which creates a `StepExpansion` domain geometry for the model during initializing the `Heat1D` test problem. It is also possible to change the domain geometry manually after initializing the test problem, but we will not do that here. \n",
"\n",
"The `Heat1D.get_components` parameter `field_params` is a dictionary that is used to pass keyword arguments that the underlying domain field geometry accepts. For example `StepExpansion` has a keyword argument `n_steps` and thus we can set `field_params={'n_steps': 3}`."
"The parameter `field_params` is a dictionary that is used to pass keyword arguments that the underlying domain field geometry accepts. For example `StepExpansion` has a keyword argument `n_steps` and thus we can set `field_params={'n_steps': 3}`."
]
},
{
Expand All @@ -517,14 +517,14 @@
"source": [
"n_steps = 3 # number of steps in the step expansion domain geometry\n",
"N = 30\n",
"model, data, problemInfo = Heat1D.get_components(\n",
"model, data, problemInfo = Heat1D(\n",
" dim=N,\n",
" endpoint=L,\n",
" max_time=T,\n",
" field_type=\"Step\",\n",
" field_params={\"n_steps\": n_steps},\n",
" exactSolution=myExactSolution,\n",
")\n"
").get_components()\n"
]
},
{
Expand Down Expand Up @@ -736,7 +736,7 @@
"metadata": {},
"source": [
"#### Try it yourself (optional):\n",
"* For this step function parametrization, try to enforce positivity of prior and the posterior samples via log parametrization which can be done by passing `map = lambda x : np.exp(x)` to the `Heat1D.get_components` method. Then run the Metropolis Hastings sampler again (similar to part 3)."
"* For this step function parametrization, try to enforce positivity of prior and the posterior samples via log parametrization which can be done by initialing `Heat1D` with `map = lambda x : np.exp(x)`. Then run the Metropolis Hastings sampler again (similar to part 3)."
]
},
{
Expand Down Expand Up @@ -815,14 +815,14 @@
"metadata": {},
"outputs": [],
"source": [
"model, data, problemInfo = Heat1D.get_components(\n",
"model, data, problemInfo = Heat1D(\n",
" dim=N,\n",
" endpoint=L,\n",
" max_time=T,\n",
" field_type=\"Step\",\n",
" field_params={\"n_steps\": n_steps},\n",
" observation_grid_map=observation_nodes,\n",
")\n"
").get_components()\n"
]
},
{
Expand Down Expand Up @@ -964,12 +964,12 @@
"outputs": [],
"source": [
"N = 35\n",
"model, data, problemInfo = Heat1D.get_components(\n",
"model, data, problemInfo = Heat1D(\n",
" dim=N,\n",
" endpoint=L,\n",
" max_time=T,\n",
" field_type=\"KL\"\n",
")\n"
").get_components()\n"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions training/Exercise06_Gibbs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@
"outputs": [],
"source": [
"# Model and data\n",
"A, y_data, probinfo = Deconvolution1D.get_components(phantom='sinc', noise_std=0.005, PSF_param=6)\n",
"A, y_data, probinfo = Deconvolution1D(phantom='sinc', noise_std=0.005, PSF_param=6).get_components()\n",
"\n",
"# Get dimension of signal\n",
"n = A.domain_dim\n",
Expand Down Expand Up @@ -682,7 +682,7 @@
"outputs": [],
"source": [
"# Deterministic forward model + data\n",
"A, y_data, probinfo = Deconvolution1D.get_components(phantom='square')\n",
"A, y_data, probinfo = Deconvolution1D(phantom='square').get_components()\n",
"\n",
"# Bayesian model\n",
"d = Gamma(1, 1e-4)\n",
Expand Down Expand Up @@ -943,7 +943,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.4"
},
"vscode": {
"interpreter": {
Expand Down

0 comments on commit 92e0929

Please sign in to comment.