Skip to content

Commit 7dbec79

Browse files
authored
Merge pull request #17 from icweaver/tests
Moar tests
2 parents 502e115 + 5a8deec commit 7dbec79

File tree

4 files changed

+229
-84
lines changed

4 files changed

+229
-84
lines changed

src/Spectra.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ function spectrum(wave::AbstractMatrix{<:Real}, flux::AbstractMatrix{<:Real}; kw
7575
EchelleSpectrum(wave, flux, Dict{Symbol,Any}(kwds))
7676
end
7777

78+
function spectrum(wave::AbstractMatrix{<:Quantity}, flux::AbstractMatrix{<:Quantity}; kwds...)
79+
@assert size(wave) == size(flux) "wave and flux must have equal size"
80+
@assert dimension(eltype(wave)) == u"𝐋" "wave not recognized as having dimensions of wavelengths"
81+
EchelleSpectrum(wave, flux, Dict{Symbol,Any}(kwds))
82+
end
83+
7884
# tools
7985
include("utils.jl")
8086
include("transforms/transforms.jl")

src/transforms/kernels.jl

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
1-
using Distributions
2-
3-
export GaussianKernel, BoxKernel, RotationalKernel, evaluate
4-
5-
"""
6-
abstract type Kernel
7-
8-
Kernels serve as the backbone of broadening and convolution operations.
9-
All kernels have a common interface:
10-
11-
evaluate(::Kernel; maxwidth=8, length=100)
12-
13-
which provides the evaluated PDF of the kernel. Note that the user should not
14-
have to provide the evaluated points, so each kernel should have a way of
15-
automatically choosing its range. For instance, a Gaussian kernel may choose to
16-
automatically evaluate 1000 points from -8σ to 8σ.
17-
"""
18-
abstract type Kernel end
19-
20-
evaluate(k::Kernel; maxwidth = 8, length = 101) = k.(_bounds(k, maxwidth, length))
21-
22-
## Implementation Note
23-
# For each new kernel, implement a _pdf function and _bounds function as below
24-
# which serves as the functional form of your kernel this way, we can limit the
25-
# amount of redefinitions of evaluate.
26-
27-
"""
28-
GaussianKernel(σ::Number) <: Kernel
29-
30-
This is a simple Gaussian kernel which is evaluated as a normal distribution with variance σ^2 and a pdf of
31-
32-
```math
33-
p(x) = \\frac{1}{σ\\sqrt{2π}} exp\\left(-\\frac{x^2}{2σ^2}\\right)
34-
```
35-
36-
The default evaluation width is ± 4σ.
37-
"""
38-
struct GaussianKernel <: Kernel
39-
σ::Number
40-
end
41-
(k::GaussianKernel)(x) = Normal(0, k.σ).pdf(x)
42-
_bounds(k::GaussianKernel, width, n) = range(-width / 2 * k.σ, width / 2 * k.σ, length = n)
43-
44-
"""
45-
BoxKernel(width::Number) <: Kernel
46-
47-
This is a simple Box (top-hat) kernel with a pdf of
48-
49-
```math
50-
p(x) = \\begin{cases}
51-
1/width & \\abs{x} < width/2 \\
52-
0 & \\
53-
\\end{cases}
54-
```
55-
56-
The default evaluation width is `± 4*width`
57-
"""
58-
struct BoxKernel <: Kernel
59-
width::Number
60-
end
61-
(k::BoxKernel)(x) = Uniform(-k.width / 2, k.width / 2).pdf(x)
62-
_bounds(k::BoxKernel, width, n) = range(-width / 2 * k.width, width / 2 * k.width, length = n)
63-
64-
struct RotationalKernel{T <: Number} <: Kernel
65-
dv::T
66-
vsini::T
67-
end
68-
69-
RotationalKernel(dv, vsini) = RotationalKernel(promote(dv, vsini)...)
1+
#using Distributions
2+
#
3+
#export GaussianKernel, BoxKernel, RotationalKernel, evaluate
4+
#
5+
#"""
6+
# abstract type Kernel
7+
#
8+
#Kernels serve as the backbone of broadening and convolution operations.
9+
#All kernels have a common interface:
10+
#
11+
# evaluate(::Kernel; maxwidth=8, length=100)
12+
#
13+
#which provides the evaluated PDF of the kernel. Note that the user should not
14+
#have to provide the evaluated points, so each kernel should have a way of
15+
#automatically choosing its range. For instance, a Gaussian kernel may choose to
16+
#automatically evaluate 1000 points from -8σ to 8σ.
17+
#"""
18+
#abstract type Kernel end
19+
#
20+
#evaluate(k::Kernel; maxwidth = 8, length = 101) = k.(_bounds(k, maxwidth, length))
21+
#
22+
### Implementation Note
23+
## For each new kernel, implement a _pdf function and _bounds function as below
24+
## which serves as the functional form of your kernel this way, we can limit the
25+
## amount of redefinitions of evaluate.
26+
#
27+
#"""
28+
# GaussianKernel(σ::Number) <: Kernel
29+
#
30+
#This is a simple Gaussian kernel which is evaluated as a normal distribution with variance σ^2 and a pdf of
31+
#
32+
#```math
33+
#p(x) = \\frac{1}{σ\\sqrt{2π}} exp\\left(-\\frac{x^2}{2σ^2}\\right)
34+
#```
35+
#
36+
#The default evaluation width is ± 4σ.
37+
#"""
38+
#struct GaussianKernel <: Kernel
39+
# σ::Number
40+
#end
41+
#(k::GaussianKernel)(x) = Normal(0, k.σ).pdf(x)
42+
#_bounds(k::GaussianKernel, width, n) = range(-width / 2 * k.σ, width / 2 * k.σ, length = n)
43+
#
44+
#"""
45+
# BoxKernel(width::Number) <: Kernel
46+
#
47+
#This is a simple Box (top-hat) kernel with a pdf of
48+
#
49+
#```math
50+
#p(x) = \\begin{cases}
51+
# 1/width & \\abs{x} < width/2 \\
52+
# 0 & \\
53+
#\\end{cases}
54+
#```
55+
#
56+
#The default evaluation width is `± 4*width`
57+
#"""
58+
#struct BoxKernel <: Kernel
59+
# width::Number
60+
#end
61+
#(k::BoxKernel)(x) = Uniform(-k.width / 2, k.width / 2).pdf(x)
62+
#_bounds(k::BoxKernel, width, n) = range(-width / 2 * k.width, width / 2 * k.width, length = n)
63+
#
64+
#struct RotationalKernel{T <: Number} <: Kernel
65+
# dv::T
66+
# vsini::T
67+
#end
68+
#
69+
#RotationalKernel(dv, vsini) = RotationalKernel(promote(dv, vsini)...)

test/plotting.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using RecipesBase, Unitful
22
import Measurements: value
33

4-
@testset "Plotting" begin
4+
@testset "Plotting - Single" begin
55
wave = range(1e4, 3e4, length = 1000)
66
sigma = 0.1 .* sin.(wave)
77
T = 6700
@@ -11,8 +11,8 @@ import Measurements: value
1111
spec = spectrum(wave, flux, name = "Test Spectrum")
1212

1313
rec = RecipesBase.apply_recipe(Dict{Symbol,Any}(), spec)
14-
@test getfield(rec[1], 1) == Dict{Symbol,Any}(:label => "",
15-
:xlabel => "wave",
14+
@test getfield(rec[1], 1) == Dict{Symbol,Any}(:label => "",
15+
:xlabel => "wave",
1616
:ylabel => "flux density",
1717
:seriestype => :step)
1818
@test rec[1].args == (ustrip.(spec.wave), value.(ustrip.(spec.flux)))
@@ -21,9 +21,24 @@ import Measurements: value
2121
strip_spec = ustrip(spec)
2222

2323
rec = RecipesBase.apply_recipe(Dict{Symbol,Any}(), strip_spec)
24-
@test getfield(rec[1], 1) == Dict{Symbol,Any}(:label => "",
25-
:xlabel => "wave",
24+
@test getfield(rec[1], 1) == Dict{Symbol,Any}(:label => "",
25+
:xlabel => "wave",
2626
:ylabel => "flux density",
2727
:seriestype => :step)
2828
@test rec[1].args == (strip_spec.wave, value.(strip_spec.flux))
29-
end
29+
end
30+
31+
@testset "Plotting - Echelle" begin
32+
n_orders = 10
33+
wave = reshape(range(100, 1e4, length=1000), 100, n_orders)'
34+
flux = ones(n_orders, 100) .* collect(1:n_orders)
35+
spec = spectrum(wave, flux, name = "Test Echelle Spectrum")
36+
rec = RecipesBase.apply_recipe(Dict{Symbol,Any}(), spec)
37+
@test getfield(rec[1], 1) == Dict{Symbol,Any}(
38+
:label => ["Order $(i)" for i in 1:n_orders],
39+
:xlabel => "wave",
40+
:ylabel => "flux density",
41+
:seriestype => :step,
42+
)
43+
@test rec[1].args == (spec.wave', spec.flux')
44+
end

0 commit comments

Comments
 (0)