Skip to content

Commit f1a09fb

Browse files
committed
update docstrings and examples
1 parent 092a34e commit f1a09fb

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

docs/src/api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ PSFModels.Normal
2020
```
2121

2222
```@example plots
23-
model = Gaussian(10)
23+
model = Gaussian(fwhm=10)
2424
plot(model; title="Gaussian(fwhm=10)")
2525
```
2626

@@ -31,7 +31,7 @@ PSFModels.AiryDisk
3131
```
3232

3333
```@example plots
34-
model = AiryDisk(10)
34+
model = AiryDisk(fwhm=10)
3535
plot(model; title="AiryDisk(fwhm=10)")
3636
```
3737

@@ -42,6 +42,6 @@ PSFModels.Moffat
4242
```
4343

4444
```@example plots
45-
model = Moffat(10)
45+
model = Moffat(fwhm=10)
4646
plot(model; title="Moffat(fwhm=10)")
4747
```

docs/src/examples.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function model(X::AbstractVector{T}) where T
3232
position = @view X[1:2] # x, y position
3333
fwhm = @view X[3:4] # fwhm_x, fwhm_y
3434
amp = X[5] # amplitude
35-
return amp * Gaussian{T}(position, fwhm)
35+
return Gaussian(T, pos=position, fwhm=fwhm, amp=amp)
3636
end
3737
3838
# objective function
@@ -41,13 +41,13 @@ function loss(X::AbstractVector{T}, target) where T
4141
all(>(0), X) || return T(Inf)
4242
# get generative model
4343
m = model(X)
44-
# l2-distance loss (χ² loss) (LossFunctions.jl)
44+
# l2-distance loss (χ² loss) (LossFunctions.jl) without cutting out
4545
stamp = @view m[axes(target)...]
4646
return value(L2DistLoss(), target, stamp, AggMode.Sum())
4747
end
4848
4949
# params are [x, y, fwhm_x, fwhm_y, amp]
50-
test_params = Float32[20, 20, 5, 5, 1]
50+
test_params = eltype(psf)[20, 20, 5, 5, 1]
5151
loss(test_params, psf)
5252
```
5353

@@ -79,7 +79,7 @@ synth_psf = model(best_fit_params)
7979
8080
plot(
8181
imshow(psf, title="Data"),
82-
plot(synth_psf, axes(psf); title="Model"),
82+
plot(synth_psf, reverse(axes(psf)); title="Model"),
8383
cbar=false,
8484
ticks=false,
8585
layout=2,

docs/src/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ To import the library
2828
julia> using PSFModels
2929
```
3030

31-
None of the models are exported to avoid namespace clashes, but it can be verbose. You can either import names directly
31+
None of the models are exported to avoid namespace clashes, but it can be verbose to continuously rewrite `PSFModels`. You can either import names directly
3232

3333
```julia
3434
julia> using PSFModels: Gaussian
3535

36-
julia> model = Gaussian(8)
36+
julia> model = Gaussian(fwhm=8)
3737
```
3838

3939
or you can create an alias for `PSFModels`
@@ -43,9 +43,9 @@ or you can create an alias for `PSFModels`
4343
using PSFModels
4444
const M = PSFModels
4545
# julia version 1.6 or above
46-
using PSFModels as M
46+
import PSFModels as M
4747

48-
model = M.Gaussian(10)
48+
model = M.Gaussian(fwhm=10)
4949
```
5050

5151
```@docs

src/PSFModels.jl

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ Statistical models for constructing point-spread functions (PSFs). These models
66
## Models
77
88
The following models are currently implemented
9-
* [`PSFModels.Gaussian`](@ref)
9+
* [`PSFModels.Gaussian`](@ref)/[`PSFModels.Normal`](@ref)
1010
* [`PSFModels.AiryDisk`](@ref)
1111
* [`PSFModels.Moffat`](@ref)
1212
1313
## Parameters
1414
15-
In general, the PSFs have a position, a full-width at half-maximum (FWHM) measure, and an amplitude. The position a 1-based pixel coordinate system, where `(1, 1)` represents the *center* of the bottom left pixel. This matches the indexing style of Julia as well as DS9, IRAF, SourceExtractor, and WCS. If a position is not specified, it is set to `(0, 0)`. The FWHM is a consistent scale parameter for the models. All models support a scalar (isotropic) FWHM and a FWHM for each axis (diagonal).
15+
In general, the PSFs have a position, a full-width at half-maximum (FWHM) measure, and an amplitude. The position follows a 1-based pixel coordinate system, where `(1, 1)` represents the *center* of the bottom left pixel. This matches the indexing style of Julia as well as DS9, IRAF, SourceExtractor, and WCS. If a position is not specified, it is set to `(0, 0)`. The FWHM is a consistent scale parameter for the models. All models support a scalar (isotropic) FWHM and a FWHM for each axis (diagonal).
1616
1717
## Usage
1818
19-
Using the models should feel just like an array. In fact, `PSFModels.PSFModel <: AbstractMatrix`. However, no data is stored and no allocations have to be made. In other words, representing the models as matrices is merely a convenience, since typically astronomical data is stored in dense arrays.
19+
Using the models should feel just like an array. In fact, `PSFModels.PSFModel <: AbstractMatrix`. However, no data is stored and no allocations have to be made. In other words, representing the models as matrices is merely a convenience, since typically astronomical data is stored in dense arrays. Another way of thinking of these is a lazy array that applies a function when indexed, rather than returning data stored in memory.
2020
2121
```jldoctest model
22-
julia> m = PSFModels.Gaussian(5); # fwhm of 5 pixels, centered at (0, 0)
22+
julia> m = PSFModels.Gaussian(fwhm=3); # centered at (0, 0)
23+
2324
2425
julia> m[0, 0] # [y, x] for indexing
2526
1.0
@@ -32,20 +33,21 @@ julia> m(0, 0) # (x, y) for evaluating
3233
3334
It's important to note the difference in the axis ordering between the index-style calls and the function-style calls. The index-style calls are reverse cartesian order (e.g., `(z, y, x)`), while function calls are the typical cartesian order `(x, y, z)`. Regardless, the constructors are always in cartesian order (`(x, y, z)`).
3435
35-
Because the model is a matrix, it needs to have a size. In this case, the size is `maxsize * FWHM` pixels, centered around the origin, and rounded up. We can see how this alters the indices from a typical `Matrix`
36+
Because the model is a matrix, it needs to have a size. Each model has a bounding box which can be controlled with the `extent` keyword. By default the extent is set by a scalar factor of the FWHM (e.g., `maxsize * FWHM` pixels), centered around the PSF, and rounded up. We can see how this alters the indices from a typical `Matrix`
3637
3738
```jldoctest model
38-
julia> size(m)
39-
(17, 17)
39+
julia> size(m) # default 'stamp' size is fwhm * 3
40+
(11, 11)
4041
4142
julia> axes(m)
42-
(-8:8, -8:8)
43+
(-5:5, -5:5)
4344
```
4445
4546
if we want to collect the model into a dense matrix, regardless of the indexing (e.g. to prepare for cross-correlation), we can simply
4647
4748
```jldoctest model
4849
julia> stamp = collect(m);
50+
4951
```
5052
5153
these axes are merely a convenience for bounding the model, since they accept any real number as input.
@@ -55,21 +57,22 @@ julia> m[100, 10000] # index-like inputs [y, x]
5557
0.0
5658
5759
julia> m(2.4, 1.7) # valid for any real (x, y)
58-
0.38315499005194587
60+
0.0696156536973086
5961
```
6062
6163
By bounding the model, we get a cutout which can be applied to arrays with much larger dimensions without having to iterate over the whole matrix
6264
6365
```jldoctest
64-
julia> big_mat = ones(101, 101);
66+
julia> big_mat = ones(1001, 1001);
67+
68+
julia> model = PSFModels.Gaussian(x=51, y=51, fwhm=2);
6569
66-
julia> model = PSFModels.Gaussian(51, 51, 2); # center of big_mat, fwhm=2
6770
6871
julia> ax = map(intersect, axes(big_mat), axes(model))
6972
(48:54, 48:54)
7073
7174
julia> cutout = @view big_mat[ax...]
72-
7×7 view(::Array{Float64,2}, 48:54, 48:54) with eltype Float64:
75+
7×7 view(::Matrix{Float64}, 48:54, 48:54) with eltype Float64:
7376
1.0 1.0 1.0 1.0 1.0 1.0 1.0
7477
1.0 1.0 1.0 1.0 1.0 1.0 1.0
7578
1.0 1.0 1.0 1.0 1.0 1.0 1.0
@@ -80,18 +83,20 @@ julia> cutout = @view big_mat[ax...]
8083
8184
julia> stamp = @view model[ax...];
8285
86+
8387
julia> photsum = sum(cutout .* stamp)
8488
4.5322418212890625
8589
```
86-
Nice- we only had to reduce ~50 pixels instead of ~10,000 to calculate the aperture sum, all in under a microsecond (on my machine).
90+
Nice- we only had to reduce ~50 pixels instead of ~1,000,000 to calculate the aperture sum, all in under a microsecond (on my machine).
8791
8892
Since the models are lazy, that means the type of the output can be specified, as long as it can be converted to from a real number (so no integer types).
8993
9094
```jldoctest
91-
julia> mbig = PSFModels.Gaussian{BigFloat}(12);
95+
julia> mbig = PSFModels.Gaussian(BigFloat, fwhm=12);
96+
9297
9398
julia> sum(mbig)
94-
163.07467408408593790971336380361822460116627553361468017101287841796875
99+
163.07467408408593885562554918859656805096847165259532630443572998046875
95100
```
96101
97102
finally, we provide plotting recipes from [RecipesBase.jl](https://github.com/JuliaPlots/RecipesBase.jl), which can be seen in use in the [API/Reference](@ref) section.
@@ -100,16 +105,9 @@ finally, we provide plotting recipes from [RecipesBase.jl](https://github.com/Ju
100105
using Plots
101106
model = PSFModels.Gaussian(8)
102107
plot(model) # default axes
103-
plot(model, 1:5, 1:5) # custom axes
108+
plot(model, :, 1:5) # custom axes (y, x)
104109
plot(model, axes(other)) # use axes from other array
105110
```
106-
107-
!!! tip "Tip: Automatic Differentation"
108-
Forward-mode AD libraries tend to use dual numbers, which can cause headaches getting the types correct. We recommend using the *primal vector*'s element type to avoid these headaches
109-
```julia
110-
# example generative model for position and scalar fwhm
111-
model(X::AbstractVector{T}) where {T} = PSFModels.Gaussian{T}(X...)
112-
```
113111
"""
114112
module PSFModels
115113

0 commit comments

Comments
 (0)