Skip to content

Commit f847f1c

Browse files
committed
Merge branch 'main' of https://github.com/ptiede/Comrade.jl
2 parents 3bd332e + 0e1eedc commit f847f1c

File tree

15 files changed

+313
-310
lines changed

15 files changed

+313
-310
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Comrade"
22
uuid = "99d987ce-9a1e-4df8-bc0b-1ea019aa547b"
33
authors = ["Paul Tiede <ptiede91@gmail.com>"]
4-
version = "0.11.17"
4+
version = "0.11.18"
55

66
[deps]
77
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
@@ -82,7 +82,7 @@ FillArrays = "1"
8282
HypercubeTransform = "^0.4.11"
8383
IntervalSets = "0.6, 0.7"
8484
LogDensityProblems = "2"
85-
Makie = "0.22"
85+
Makie = "0.24"
8686
Measurements = "2"
8787
NamedTupleTools = "0.13,0.14"
8888
Optimization = "4"
@@ -103,7 +103,7 @@ Tables = "1"
103103
TransformVariables = "0.8"
104104
VLBIImagePriors = "0.10"
105105
VLBILikelihoods = "^0.2.6"
106-
VLBISkyModels = "0.6"
106+
VLBISkyModels = "^0.6.15"
107107
julia = "1.10"
108108

109109
[extras]

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using Pyehtim, VLBISkyModels, InteractiveUtils
1010
using AbstractMCMC, Random, HypercubeTransform
1111
using CairoMakie
1212

13+
const ComradeMakieExt = Base.get_extension(Comrade, :ComradeMakieExt)
1314

1415
Documenter.DocMeta.setdocmeta!(Comrade, :DocTestSetup, :(using Comrade); recursive = true)
1516

docs/src/api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ A user must first load `Makie` or a `Makie` backend, e.g., `CairoMakie` to use t
201201

202202
```@docs
203203
Comrade.plotfields
204+
Comrade.plotfields!
204205
Comrade.axisfields
205206
Comrade.plotcaltable
206207
Comrade.baselineplot
208+
Comrade.baselineplot!
209+
ComradeMakieExt.BaselinePlot
207210
```

examples/advanced/HybridImaging/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1313
VLBIImagePriors = "b1ba175b-8447-452c-b961-7db2d6f7a029"
1414

1515
[compat]
16-
CairoMakie = "0.13"
16+
CairoMakie = "0.15"
1717
Optimization = "4"
1818
Pyehtim = "0.2"
1919
StableRNGs = "1"

examples/advanced/HybridImaging/main.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ using Pyehtim
3838

3939
# For reproducibility we use a stable random number genreator
4040
using StableRNGs
41-
rng = StableRNG(11)
41+
rng = StableRNG(12)
4242

4343

4444
# To download the data visit https://doi.org/10.25739/g85n-f134
@@ -149,13 +149,13 @@ skym = SkyModel(sky, skyprior, g; metadata = skymetadata)
149149
# This is everything we need to specify our posterior distribution, which our is the main
150150
# object of interest in image reconstructions when using Bayesian inference.
151151
using Enzyme
152-
post = VLBIPosterior(skym, intmodel, dvis; admode = set_runtime_activity(Enzyme.Reverse))
152+
post = VLBIPosterior(skym, intmodel, dvis)
153153

154154
# We can sample from the prior to see what the model looks like
155155
using DisplayAs #hide
156156
using CairoMakie
157157
xrand = prior_sample(rng, post)
158-
gpl = imagepixels(μas2rad(200.0), μas2rad(200.0), 128, 128)
158+
gpl = refinespatial(g, 3)
159159
fig = imageviz(intensitymap(skymodel(post, xrand), gpl));
160160
fig |> DisplayAs.PNG |> DisplayAs.Text #hide
161161

@@ -168,9 +168,8 @@ fig |> DisplayAs.PNG |> DisplayAs.Text #hide
168168
# For optimization we will use the `Optimization.jl` package and the LBFGS optimizer.
169169
# To use this we use the [`comrade_opt`](@ref) function
170170
using Optimization
171-
using OptimizationOptimJL
172171
xopt, sol = comrade_opt(
173-
post, LBFGS();
172+
post, Optimization.LBFGS();
174173
initial_params = xrand, maxiters = 2000, g_tol = 1.0e0
175174
);
176175

@@ -249,12 +248,10 @@ figd |> DisplayAs.PNG |> DisplayAs.Text #hide
249248

250249
# Now let's check the residuals using draws from the posterior
251250
fig = Figure(; size = (600, 400))
252-
ax, = baselineplot(fig[1, 1], res[1], :uvdist, :res, label = "MAP", color = :blue, colorim = :red, marker = :circle)
251+
ax, = plotfields!(fig[1, 1], res[1], :uvdist, :res, scatter_kwargs = (; label = "MAP", color = :blue, colorim = :red, marker = :circle), legend = false)
253252
for s in sample(chain, 10)
254253
baselineplot!(ax, residuals(post, s)[1], :uvdist, :res, alpha = 0.2, label = "Draw")
255254
end
256-
ax.xlabel = "uv-distance (λ)"
257-
ax.ylabel = "Normalized Residuals"
258255
axislegend(ax, merge = true)
259256
fig |> DisplayAs.PNG |> DisplayAs.Text #hide
260257

examples/beginner/GeometricModeling/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
1414
VLBIImagePriors = "b1ba175b-8447-452c-b961-7db2d6f7a029"
1515

1616
[compat]
17-
CairoMakie = "0.12, 0.13"
17+
CairoMakie = "0.15"
1818
Distributions = "0.25"
1919
Optimization = "4"
2020
Pigeons = "0.4"

examples/beginner/GeometricModeling/main.jl

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ using Distributions, VLBIImagePriors
7878
prior = (
7979
radius = Uniform(μas2rad(10.0), μas2rad(30.0)),
8080
width = Uniform(μas2rad(1.0), μas2rad(10.0)),
81-
ma = (Uniform(0.0, 0.5), Uniform(0.0, 0.5)),
82-
mp = (Uniform(0, 2π), Uniform(0, 2π)),
81+
ma = (Uniform(0.0, 0.5),),
82+
mp = (Uniform(0, 2π),),
8383
τ = Uniform(0.0, 1.0),
8484
ξτ = Uniform(0.0, π),
8585
f = Uniform(0.0, 1.0),
@@ -119,8 +119,8 @@ logdensityof(
119119
sky = (
120120
radius = μas2rad(20.0),
121121
width = μas2rad(10.0),
122-
ma = (0.3, 0.3),
123-
mp =/ 2, π),
122+
ma = (0.3,),
123+
mp =/ 2,),
124124
τ = 0.1,
125125
ξτ = π / 2,
126126
f = 0.6,
@@ -218,16 +218,14 @@ DisplayAs.Text(DisplayAs.PNG(fig))
218218
# model is fitting the data we can plot the model and data products. As of Comrade 0.11.7 Makie
219219
# is the preferred plotting tool. For plotting data there are two classes of functions
220220
# - `baselineplot` which gives complete control of plotting
221-
# - `plotfields, axisfields` which are more automated and limited but will automatically add
221+
# - `plotfields, plotfields!` which are more automated and limited but will automatically add
222222
# labels, legends, titles etc.
223-
# We will demonstrate both below.
223+
# A reasonable workflow is to use `plotfields` to set up the initial figure and axis labels and then
224+
# then use `baselineplot!` to add additional plots to the axis. For example,
224225
lcsim, cpsim = simulate_observation(post, xopt; add_thermal_noise = false)
225-
fig = Figure(; size = (800, 300))
226-
ax1 = Axis(fig[1, 1], xlabel = "√Quadrangle Area", ylabel = "Log Closure Amplitude")
227-
baselineplot!(ax1, lcsim, uvdist, measwnoise, marker = :circle, label = "MAP", error = true)
228-
baselineplot!(ax1, dlcamp, uvdist, Comrade.measurement, marker = :+, color = :black, label = "Data")
229-
ax2 = Axis(fig[1, 2], xlabel = "√Triangle Area", ylabel = "Closure Phase")
230-
baselineplot!(ax2, cpsim, uvdist, mod2pi measwnoise, marker = :circle, label = "MAP", error = true)
226+
fig, ax1 = plotfields(lcsim, uvdist, measwnoise, scatter_kwargs = (; marker = :circle, label = "MAP"), figure_kwargs = (; size = (800, 300)), legend = false);
227+
baselineplot!(ax1, dlcamp, uvdist, measurement, marker = :+, color = :black, label = "Data")
228+
ax2, = plotfields!(fig[1, 2], cpsim, uvdist, mod2pi measwnoise, scatter_kwargs = (; marker = :circle, label = "MAP"), axis_kwargs = (ylabel = "Closure Phase (rad)",))
231229
baselineplot!(ax2, dcphase, uvdist, mod2pi measurement, marker = :+, color = :black, label = "Data")
232230
axislegend(ax1, framevisible = false)
233231
DisplayAs.Text(DisplayAs.PNG(fig))
@@ -237,7 +235,7 @@ DisplayAs.Text(DisplayAs.PNG(fig))
237235
# are marginalized over the posterior.
238236
fig = Figure(; size = (800, 300))
239237
ax1 = Axis(fig[1, 1], xlabel = "√Quadrangle Area", ylabel = "Log Closure Amplitude")
240-
ax2 = Axis(fig[1, 2], xlabel = "√Triangle Area", ylabel = "Closure Phase")
238+
ax2 = Axis(fig[1, 2], xlabel = "√Triangle Area", ylabel = "Closure Phase (rad)")
241239
for i in 1:10
242240
mobs = simulate_observation(post, sample(chain, 1)[1])
243241
mlca = mobs[1]
@@ -254,7 +252,6 @@ DisplayAs.Text(DisplayAs.PNG(fig))
254252
# The normalied residuals are the difference between the data
255253
# and the model, divided by the data's error:
256254
rd = residuals(post, chain[end])
257-
fig = Figure(; size = (800, 300))
258-
axisfields(fig[1, 1], rd[1], uvdist, :res)
259-
axisfields(fig[1, 2], rd[2], uvdist, :res)
255+
fig, ax = plotfields(rd[1], uvdist, :res, axis_kwargs = (; ylabel = "Norm. Res. LCA"))
256+
plotfields!(fig[2, 1], rd[2], uvdist, :res, axis_kwargs = (; ylabel = "Norm. Res. CP"))
260257
DisplayAs.Text(DisplayAs.PNG(fig))

examples/beginner/LoadingData/main.jl

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,39 @@ coh = extract_table(obs, Coherencies())
5858
#-
5959
# We can also recover the array used in the observation using
6060
using DisplayAs
61-
plotfields(coh, :U, :V, axis_kwargs = (xreversed = true,)) |> DisplayAs.PNG |> DisplayAs.Text # Plot the baseline coverage
61+
plotfields(coh, U, V, axis_kwargs = (xreversed = true,)) |> DisplayAs.PNG |> DisplayAs.Text # Plot the baseline coverage
6262

6363
# As of Comrade 0.11.7 Makie is the preferred plotting tool. For plotting data there are two
6464
# classes of functions:
6565
# - `baselineplot` which gives complete control of plotting
6666
# - `plotfields, axisfields` which are more automated and limited but will automatically add
6767
# labels, legends, titles etc.
6868
fig = Figure(; size = (800, 600))
69-
axisfields(fig[1, 1], vis, :uvdist, :measurement)
70-
axisfields(fig[1, 2], amp, :uvdist, :measurement)
71-
axisfields(fig[2, 1], cphase, :uvdist, :measurement)
72-
axisfields(fig[2, 2], lcamp, :uvdist, :measurement)
69+
plotfields!(fig[1, 1], vis, uvdist, measurement)
70+
plotfields!(fig[1, 2], amp, uvdist, measurement)
71+
plotfields!(fig[2, 1], cphase, uvdist, measurement)
72+
plotfields!(fig[2, 2], lcamp, uvdist, measurement)
7373
fig |> DisplayAs.PNG |> DisplayAs.Text
7474

75-
# And also the coherency matrices. Here we show how to use `baselineplot` to plot the data
75+
# And also the coherency matrices. Since the data products are a matrix we need to plot each one separately.
7676
fig = Figure(; size = (800, 600))
77-
baselineplot(fig[1, 1], coh, x -> uvdist(x) / 1.0e9, x -> abs(measwnoise(x)[1, 1]), error = true, axis = (ylabel = "RR", xlabel = "uv distance (Gλ)"))
78-
baselineplot(fig[2, 1], coh, x -> uvdist(x) / 1.0e9, x -> abs(measwnoise(x)[2, 1]), error = true, axis = (ylabel = "LR", xlabel = "uv distance (Gλ)"))
79-
baselineplot(fig[1, 2], coh, x -> uvdist(x) / 1.0e9, x -> abs(measwnoise(x)[1, 2]), error = true, axis = (ylabel = "RL", xlabel = "uv distance (Gλ)"))
80-
baselineplot(fig[2, 2], coh, x -> uvdist(x) / 1.0e9, x -> abs(measwnoise(x)[2, 2]), error = true, axis = (ylabel = "LL", xlabel = "uv distance (Gλ)"))
77+
plotfields!(fig[1, 1], coh, uvdist, x -> measwnoise(x)[1, 1], axis_kwargs = (ylabel = "RR", xlabel = "uv distance (Gλ)"))
78+
plotfields!(fig[2, 1], coh, uvdist, x -> measwnoise(x)[2, 1], axis_kwargs = (ylabel = "LR", xlabel = "uv distance (Gλ)"))
79+
plotfields!(fig[1, 2], coh, uvdist, x -> measwnoise(x)[1, 2], axis_kwargs = (ylabel = "RL", xlabel = "uv distance (Gλ)"))
80+
plotfields!(fig[2, 2], coh, uvdist, x -> measwnoise(x)[2, 2], axis_kwargs = (ylabel = "LL", xlabel = "uv distance (Gλ)"))
81+
fig
82+
83+
# You can also plot a single baseline
84+
fig, ax = plotfields(coh, (:AA, :LM), Ti, x -> abs(measwnoise(x)[1, 1]), axis_kwargs = (; ylabel = "|RR|"))
85+
ax2 = plotfields!(fig[1, 2], coh, (:LM, :AZ), Ti, x -> abs(measwnoise(x)[1, 1]), axis_kwargs = (; ylabel = "|RR|"))
86+
fig
87+
88+
# Finally, we provide a more low-level plotting function `baselineplot` which allows you to plot
89+
# any field against any other field. This is what `plotfields` calls under the hood. However, it
90+
# does not automatically add labels, legends, titles etc, but can add multiple baselines to the same plot.
91+
fig, ax = baselineplot(coh, (:AA, :LM), Ti, x -> abs(measwnoise(x)[1, 1]), label = "AA-LM")
92+
baselineplot!(ax, coh, (:LM, :AZ), Ti, x -> abs(measwnoise(x)[1, 1]), label = "LM-AZ")
93+
ax.ylabel = "|RR|"
94+
ax.xlabel = "Time (UTC)"
95+
axislegend(ax)
8196
fig

examples/intermediate/ClosureImaging/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ VLBIImagePriors = "b1ba175b-8447-452c-b961-7db2d6f7a029"
1717
VLBILikelihoods = "90db92cd-0007-4c0a-8e51-dbf0782ce592"
1818

1919
[compat]
20-
CairoMakie = "0.13"
20+
CairoMakie = "0.15"
2121
DisplayAs = "0.1"
2222
Distributions = "0.25"
2323
Optimization = "4"

examples/intermediate/ClosureImaging/main.jl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ skym = SkyModel(sky, prior, grid; metadata = skymeta)
127127
# Since we are fitting closures we do not need to include an instrument model, since
128128
# the closure likelihood is approximately independent of gains in the high SNR limit.
129129
using Enzyme
130-
post = VLBIPosterior(skym, dlcamp, dcphase; admode = set_runtime_activity(Enzyme.Reverse))
130+
post = VLBIPosterior(skym, dlcamp, dcphase)
131131

132132
# ## Reconstructing the Image
133133

@@ -138,13 +138,12 @@ post = VLBIPosterior(skym, dlcamp, dcphase; admode = set_runtime_activity(Enzyme
138138

139139
# To optimize our posterior `Comrade` provides the `comrade_opt` function. To use this
140140
# functionality a user first needs to import `Optimization.jl` and the optimizer of choice.
141-
# In this tutorial we will use Optim.jl's L-BFGS optimizer, which is defined in the sub-package
142-
# OptimizationOptimJL. We also need to import Enzyme to allow for automatic differentiation.
141+
# In this tutorial we will use Optiizations LBFGS optimizer.
142+
# We also need to import Enzyme to allow for automatic differentiation.
143143
using Optimization
144-
using OptimizationOptimJL
145144
xopt, sol = comrade_opt(
146-
post, LBFGS();
147-
maxiters = 1000, initial_params = prior_sample(rng, post)
145+
post, Optimization.LBFGS();
146+
maxiters = 2000, initial_params = prior_sample(rng, post)
148147
);
149148

150149

@@ -153,8 +152,8 @@ using CairoMakie
153152
using DisplayAs #hide
154153
res = residuals(post, xopt)
155154
fig = Figure(; size = (800, 300))
156-
axisfields(fig[1, 1], res[1], :uvdist, :res) |> DisplayAs.PNG |> DisplayAs.Text
157-
axisfields(fig[1, 2], res[2], :uvdist, :res) |> DisplayAs.PNG |> DisplayAs.Text
155+
plotfields!(fig[1, 1], res[1], :uvdist, :res);
156+
plotfields!(fig[1, 2], res[2], :uvdist, :res);
158157
fig |> DisplayAs.PNG |> DisplayAs.Text
159158

160159
# Now let's plot the MAP estimate.
@@ -177,8 +176,8 @@ DisplayAs.Text(DisplayAs.PNG(fig)) #hide
177176
# For our `metric` we use a diagonal matrix due to easier tuning.
178177
#-
179178
using AdvancedHMC
180-
chain = sample(rng, post, NUTS(0.8), 700; n_adapts = 500, progress = false, initial_params = xopt);
181-
179+
out = sample(rng, post, NUTS(0.8), 700; n_adapts = 500, saveto = DiskStore(), initial_params = xopt);
180+
chain = load_samples(out)
182181

183182
# !!! warning
184183
# This should be run for longer!

0 commit comments

Comments
 (0)