Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tutorial_nifty_resolve
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ift
tutorial_nifty_resolve
Commits
ab243150
Commit
ab243150
authored
2 years ago
by
Philipp Frank
Browse files
Options
Downloads
Plain Diff
Merge branch 'misc_update' into 'main'
Misc update See merge request
!7
parents
a28324c5
60e5ddc5
No related branches found
No related tags found
1 merge request
!7
Misc update
Pipeline
#149176
passed
2 years ago
Stage: test
Changes
4
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitlab-ci.yml
+1
-1
1 addition, 1 deletion
.gitlab-ci.yml
demo_poisson.ipynb
+30
-10
30 additions, 10 deletions
demo_poisson.ipynb
demo_poisson_solution.ipynb
+0
-230
0 additions, 230 deletions
demo_poisson_solution.ipynb
variational_inference_visualized.py
+1
-1
1 addition, 1 deletion
variational_inference_visualized.py
with
32 additions
and
242 deletions
.gitlab-ci.yml
+
1
−
1
View file @
ab243150
...
...
@@ -6,5 +6,5 @@ run:
-
pip install matplotlib nifty8 ift-resolve
-
jupyter nbconvert --execute --ExecutePreprocessor.timeout=None --to html demo_CorrelatedFields.ipynb
-
jupyter nbconvert --execute --ExecutePreprocessor.timeout=None --to html demo_radio.ipynb
-
jupyter nbconvert --execute --ExecutePreprocessor.timeout=None --to html demo_poisson.ipynb
-
jupyter nbconvert --execute --ExecutePreprocessor.timeout=None --to html demo_wiener_filter.ipynb
This diff is collapsed.
Click to expand it.
demo_poisson.ipynb
+
30
−
10
View file @
ab243150
...
...
@@ -38,7 +38,9 @@
"outputs": [],
"source": [
"# Load data and visualize\n",
"data = np.load('data/poisson.npz')\n"
"data = np.load('data/poisson.npz')\n",
"print(data['data'].shape)\n",
"plot_2D(data['data'], 'Data')"
]
},
{
...
...
@@ -50,7 +52,10 @@
"position_space = ift.RGSpace([128, 128])\n",
"\n",
"# Homogeneous poisson process\n",
"\n",
"projection = ift.VdotOperator(ift.full(position_space, 1.)).adjoint\n",
"model1 = ift.FieldAdapter(projection.domain, 'hom')\n",
"model1 = ift.exp(5. * model1)\n",
"model1 = projection @ model1\n",
"\n",
"print(model1)\n"
]
...
...
@@ -61,7 +66,12 @@
"metadata": {},
"outputs": [],
"source": [
"# Set up likelihood & PSF\n"
"# Set up likelihood & PSF\n",
"d = ift.makeField(position_space, data['data'])\n",
"likelihood = ift.PoissonianEnergy(d)\n",
"PSF_op, psf = load_psf(position_space)\n",
"plot_2D(psf, 'PSF')\n",
"likelihood = likelihood @ PSF_op"
]
},
{
...
...
@@ -71,8 +81,8 @@
"outputs": [],
"source": [
"# Inference model 1\n",
"\n",
"\n",
"
samples, evidence = geovi_sampling(likelihood @ model1)
\n",
"
plot_2D(samples.average(model1).val, 'model1 posterior mean')
\n",
"print(evidence)\n",
"evidences[0] = evidence"
]
...
...
@@ -84,10 +94,12 @@
"outputs": [],
"source": [
"# Independent poisson process\n",
"model2 = ift.InverseGammaOperator(position_space, 2., 3.).ducktape('independent')\n",
"\n",
"# Inference model 2\n",
"\n",
"\n",
"samples, evidence = geovi_sampling(likelihood @ model2)\n",
"plot_2D(samples.average(model2).val, 'model2 posterior mean')\n",
"print(evidence)\n",
"evidences[1] = evidence"
]
},
...
...
@@ -129,7 +141,9 @@
"}\n",
"correlated_field = ift.SimpleCorrelatedField(position_space, **args)\n",
"pspec = correlated_field.power_spectrum\n",
"\n"
"diffuse = correlated_field.exp()\n",
"\n",
"model3 = diffuse + model2"
]
},
{
...
...
@@ -138,7 +152,11 @@
"metadata": {},
"outputs": [],
"source": [
"# Prior samples\n"
"# Prior samples\n",
"pl = ift.Plot()\n",
"for _ in range(9):\n",
" pl.add(model3(ift.from_random(model3.domain)))\n",
"pl.output()"
]
},
{
...
...
@@ -148,7 +166,9 @@
"outputs": [],
"source": [
"# Inference model 3\n",
"\n",
"samples, evidence = geovi_sampling(likelihood @ model3)\n",
"plot_2D(samples.average(model3).val, 'model3 posterior mean')\n",
"print(evidence)\n",
"evidences[2] = evidence"
]
},
...
...
%% Cell type:markdown id: tags:
Nifty tutorial for Poisson count data
=====================================
%% Cell type:markdown id: tags:
Setup
-----
%% Cell type:code id: tags:
```
python
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
nifty8
as
ift
from
utils
import
plot_2D
,
load_psf
,
geovi_sampling
,
plot_posterior
ift
.
random
.
push_sseq_from_seed
(
42
)
evidences
=
np
.
zeros
(
3
)
```
%% Cell type:code id: tags:
```
python
# Load data and visualize
data
=
np
.
load
(
'
data/poisson.npz
'
)
print
(
data
[
'
data
'
].
shape
)
plot_2D
(
data
[
'
data
'
],
'
Data
'
)
```
%% Cell type:code id: tags:
```
python
position_space
=
ift
.
RGSpace
([
128
,
128
])
# Homogeneous poisson process
projection
=
ift
.
VdotOperator
(
ift
.
full
(
position_space
,
1.
)).
adjoint
model1
=
ift
.
FieldAdapter
(
projection
.
domain
,
'
hom
'
)
model1
=
ift
.
exp
(
5.
*
model1
)
model1
=
projection
@
model1
print
(
model1
)
```
%% Cell type:code id: tags:
```
python
# Set up likelihood & PSF
d
=
ift
.
makeField
(
position_space
,
data
[
'
data
'
])
likelihood
=
ift
.
PoissonianEnergy
(
d
)
PSF_op
,
psf
=
load_psf
(
position_space
)
plot_2D
(
psf
,
'
PSF
'
)
likelihood
=
likelihood
@
PSF_op
```
%% Cell type:code id: tags:
```
python
# Inference model 1
samples
,
evidence
=
geovi_sampling
(
likelihood
@
model1
)
plot_2D
(
samples
.
average
(
model1
).
val
,
'
model1 posterior mean
'
)
print
(
evidence
)
evidences
[
0
]
=
evidence
```
%% Cell type:code id: tags:
```
python
# Independent poisson process
model2
=
ift
.
InverseGammaOperator
(
position_space
,
2.
,
3.
).
ducktape
(
'
independent
'
)
# Inference model 2
samples
,
evidence
=
geovi_sampling
(
likelihood
@
model2
)
plot_2D
(
samples
.
average
(
model2
).
val
,
'
model2 posterior mean
'
)
print
(
evidence
)
evidences
[
1
]
=
evidence
```
%% Cell type:code id: tags:
```
python
# Compare evidence
print
(
evidences
[:
2
])
```
%% Cell type:code id: tags:
```
python
# Diffuse poisson process
args
=
{
'
offset_mean
'
:
.
5
,
'
offset_std
'
:
(
1.
,
1E-5
),
# Amplitude of field fluctuations
'
fluctuations
'
:
(
1.5
,
0.5
),
# 1.0, 1e-2
# Exponent of power law power spectrum component
'
loglogavgslope
'
:
(
-
4.
,
1
),
# -6.0, 1
# Amplitude of integrated Wiener process power spectrum component
'
flexibility
'
:
(
1.
,
0.2
),
# 2.0, 1.0
# How ragged the integrated Wiener process component is
'
asperity
'
:
(
0.1
,
0.01
),
# 0.1, 0.5
# Name of the input keys
'
prefix
'
:
'
diffuse
'
}
correlated_field
=
ift
.
SimpleCorrelatedField
(
position_space
,
**
args
)
pspec
=
correlated_field
.
power_spectrum
diffuse
=
correlated_field
.
exp
()
model3
=
diffuse
+
model2
```
%% Cell type:code id: tags:
```
python
# Prior samples
pl
=
ift
.
Plot
()
for
_
in
range
(
9
):
pl
.
add
(
model3
(
ift
.
from_random
(
model3
.
domain
)))
pl
.
output
()
```
%% Cell type:code id: tags:
```
python
# Inference model 3
samples
,
evidence
=
geovi_sampling
(
likelihood
@
model3
)
plot_2D
(
samples
.
average
(
model3
).
val
,
'
model3 posterior mean
'
)
print
(
evidence
)
evidences
[
2
]
=
evidence
```
%% Cell type:code id: tags:
```
python
# Compare evidence
print
(
evidences
)
print
(
evidences
-
evidences
[
-
1
])
```
%% Cell type:markdown id: tags:
Posterior visualization
-----------------------
%% Cell type:code id: tags:
```
python
plot_posterior
(
samples
,
data
,
model3
,
diffuse
,
model2
,
pspec
)
```
...
...
This diff is collapsed.
Click to expand it.
demo_poisson_solution.ipynb
deleted
100644 → 0
+
0
−
230
View file @
a28324c5
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nifty tutorial for Poisson count data\n",
"====================================="
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setup\n",
"-----"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import nifty8 as ift\n",
"from utils import plot_2D, load_psf, geovi_sampling, plot_posterior\n",
"\n",
"ift.random.push_sseq_from_seed(42)\n",
"evidences = np.zeros(3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load data and visualize\n",
"data = np.load('data/poisson.npz')\n",
"print(data['data'].shape)\n",
"plot_2D(data['data'], 'Data')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"position_space = ift.RGSpace([128, 128])\n",
"\n",
"# Homogeneous poisson process\n",
"projection = ift.VdotOperator(ift.full(position_space, 1.)).adjoint\n",
"model1 = ift.FieldAdapter(projection.domain, 'hom')\n",
"model1 = ift.exp(5. * model1)\n",
"model1 = projection @ model1\n",
"\n",
"print(model1)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Set up likelihood & PSF\n",
"d = ift.makeField(position_space, data['data'])\n",
"likelihood = ift.PoissonianEnergy(d)\n",
"PSF_op, psf = load_psf(position_space)\n",
"plot_2D(psf, 'PSF')\n",
"likelihood = likelihood @ PSF_op"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Inference model 1\n",
"samples, evidence = geovi_sampling(likelihood @ model1)\n",
"plot_2D(samples.average(model1).val, 'model1 posterior mean')\n",
"print(evidence)\n",
"evidences[0] = evidence"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Independent poisson process\n",
"model2 = ift.InverseGammaOperator(position_space, 2., 3.).ducktape('independent')\n",
"\n",
"# Inference model 2\n",
"samples, evidence = geovi_sampling(likelihood @ model2)\n",
"plot_2D(samples.average(model2).val, 'model2 posterior mean')\n",
"print(evidence)\n",
"evidences[1] = evidence"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compare evidence\n",
"print(evidences[:2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Diffuse poisson process\n",
"args = {\n",
" 'offset_mean': .5,\n",
" 'offset_std': (1., 1E-5),\n",
"\n",
" # Amplitude of field fluctuations\n",
" 'fluctuations': (1.5, 0.5), # 1.0, 1e-2\n",
"\n",
" # Exponent of power law power spectrum component\n",
" 'loglogavgslope': (-4., 1), # -6.0, 1\n",
"\n",
" # Amplitude of integrated Wiener process power spectrum component\n",
" 'flexibility': (1., 0.2), # 2.0, 1.0\n",
"\n",
" # How ragged the integrated Wiener process component is\n",
" 'asperity': (0.1, 0.01), # 0.1, 0.5\n",
"\n",
" # Name of the input keys\n",
" 'prefix' : 'diffuse'\n",
"}\n",
"correlated_field = ift.SimpleCorrelatedField(position_space, **args)\n",
"pspec = correlated_field.power_spectrum\n",
"diffuse = correlated_field.exp()\n",
"\n",
"model3 = diffuse + model2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Prior samples\n",
"pl = ift.Plot()\n",
"for _ in range(9):\n",
" pl.add(model3(ift.from_random(model3.domain)))\n",
"pl.output()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Inference model 3\n",
"samples, evidence = geovi_sampling(likelihood @ model3)\n",
"plot_2D(samples.average(model3).val, 'model3 posterior mean')\n",
"print(evidence)\n",
"evidences[2] = evidence"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compare evidence\n",
"print(evidences)\n",
"print(evidences - evidences[-1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Posterior visualization\n",
"-----------------------"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_posterior(samples, data, model3, diffuse, model2, pspec)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
},
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
Nifty tutorial for Poisson count data
=====================================
%% Cell type:markdown id: tags:
Setup
-----
%% Cell type:code id: tags:
```
python
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
nifty8
as
ift
from
utils
import
plot_2D
,
load_psf
,
geovi_sampling
,
plot_posterior
ift
.
random
.
push_sseq_from_seed
(
42
)
evidences
=
np
.
zeros
(
3
)
```
%% Cell type:code id: tags:
```
python
# Load data and visualize
data
=
np
.
load
(
'
data/poisson.npz
'
)
print
(
data
[
'
data
'
].
shape
)
plot_2D
(
data
[
'
data
'
],
'
Data
'
)
```
%% Cell type:code id: tags:
```
python
position_space
=
ift
.
RGSpace
([
128
,
128
])
# Homogeneous poisson process
projection
=
ift
.
VdotOperator
(
ift
.
full
(
position_space
,
1.
)).
adjoint
model1
=
ift
.
FieldAdapter
(
projection
.
domain
,
'
hom
'
)
model1
=
ift
.
exp
(
5.
*
model1
)
model1
=
projection
@
model1
print
(
model1
)
```
%% Cell type:code id: tags:
```
python
# Set up likelihood & PSF
d
=
ift
.
makeField
(
position_space
,
data
[
'
data
'
])
likelihood
=
ift
.
PoissonianEnergy
(
d
)
PSF_op
,
psf
=
load_psf
(
position_space
)
plot_2D
(
psf
,
'
PSF
'
)
likelihood
=
likelihood
@
PSF_op
```
%% Cell type:code id: tags:
```
python
# Inference model 1
samples
,
evidence
=
geovi_sampling
(
likelihood
@
model1
)
plot_2D
(
samples
.
average
(
model1
).
val
,
'
model1 posterior mean
'
)
print
(
evidence
)
evidences
[
0
]
=
evidence
```
%% Cell type:code id: tags:
```
python
# Independent poisson process
model2
=
ift
.
InverseGammaOperator
(
position_space
,
2.
,
3.
).
ducktape
(
'
independent
'
)
# Inference model 2
samples
,
evidence
=
geovi_sampling
(
likelihood
@
model2
)
plot_2D
(
samples
.
average
(
model2
).
val
,
'
model2 posterior mean
'
)
print
(
evidence
)
evidences
[
1
]
=
evidence
```
%% Cell type:code id: tags:
```
python
# Compare evidence
print
(
evidences
[:
2
])
```
%% Cell type:code id: tags:
```
python
# Diffuse poisson process
args
=
{
'
offset_mean
'
:
.
5
,
'
offset_std
'
:
(
1.
,
1E-5
),
# Amplitude of field fluctuations
'
fluctuations
'
:
(
1.5
,
0.5
),
# 1.0, 1e-2
# Exponent of power law power spectrum component
'
loglogavgslope
'
:
(
-
4.
,
1
),
# -6.0, 1
# Amplitude of integrated Wiener process power spectrum component
'
flexibility
'
:
(
1.
,
0.2
),
# 2.0, 1.0
# How ragged the integrated Wiener process component is
'
asperity
'
:
(
0.1
,
0.01
),
# 0.1, 0.5
# Name of the input keys
'
prefix
'
:
'
diffuse
'
}
correlated_field
=
ift
.
SimpleCorrelatedField
(
position_space
,
**
args
)
pspec
=
correlated_field
.
power_spectrum
diffuse
=
correlated_field
.
exp
()
model3
=
diffuse
+
model2
```
%% Cell type:code id: tags:
```
python
# Prior samples
pl
=
ift
.
Plot
()
for
_
in
range
(
9
):
pl
.
add
(
model3
(
ift
.
from_random
(
model3
.
domain
)))
pl
.
output
()
```
%% Cell type:code id: tags:
```
python
# Inference model 3
samples
,
evidence
=
geovi_sampling
(
likelihood
@
model3
)
plot_2D
(
samples
.
average
(
model3
).
val
,
'
model3 posterior mean
'
)
print
(
evidence
)
evidences
[
2
]
=
evidence
```
%% Cell type:code id: tags:
```
python
# Compare evidence
print
(
evidences
)
print
(
evidences
-
evidences
[
-
1
])
```
%% Cell type:markdown id: tags:
Posterior visualization
-----------------------
%% Cell type:code id: tags:
```
python
plot_posterior
(
samples
,
data
,
model3
,
diffuse
,
model2
,
pspec
)
```
This diff is collapsed.
Click to expand it.
variational_inference_visualized.py
+
1
−
1
View file @
ab243150
...
...
@@ -36,7 +36,7 @@ def main():
dom
=
ift
.
UnstructuredDomain
(
1
)
n_samples
=
20
show_geo
=
Fals
e
show_geo
=
Tru
e
scale
=
10.
def
transformation
(
x
,
y
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment