Skip to content

Commit

Permalink
Use package extensions, not Requires; to v0.5 (#66)
Browse files Browse the repository at this point in the history
* Extensions for plots and units

* import

* Update import etc.

* Purge Requires
  • Loading branch information
JeffFessler authored May 17, 2024
1 parent ec86c79 commit f130aa9
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 120 deletions.
19 changes: 13 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
name = "Sinograms"
uuid = "02a14def-c6e6-4ab0-b2df-0ab64bc8cdd7"
authors = ["fessler <[email protected]>"]
version = "0.4.1"
version = "0.5"

[deps]
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
ImageGeoms = "9ee76f2b-840d-4475-b6d6-e485c9297852"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
LazyGrids = "7031d0ef-c40d-4431-b2f8-61a8d2f650db"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[compat]
FFTW = "1.4.5, 1.5"
ImageGeoms = "0.9, 0.10"
FFTW = "1.5"
ImageGeoms = "0.11"
Interpolations = "0.14, 0.15"
LazyGrids = "0.4, 0.5, 1"
Requires = "1.3"
LazyGrids = "1"
Unitful = "1.19"
julia = "1.10"

[weakdeps]
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
Sinograms_Plots = "Plots"
Sinograms_Units = "Unitful"
2 changes: 1 addition & 1 deletion docs/lit/examples/04-fan-arc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ anti-scatter grids.

# Packages needed here.

using Plots: plot, gui # these 2 must precede Sinograms for Requires to work!
using Plots: plot, gui
using Unitful: mm
using Sinograms: SinoFanArc, rays, plan_fbp, fbp, sino_geom_plot!
using ImageGeoms: ImageGeom, fovs, MaskCircle
Expand Down
2 changes: 1 addition & 1 deletion docs/lit/examples/05-fan-flat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ as used in many cone-beam CT (CBCT) systems.

# Packages needed here.

using Plots: plot, gui # these 2 must precede Sinograms for Requires to work!
using Plots: plot, gui
using Unitful: cm
using Sinograms: SinoFanFlat, rays, plan_fbp, Window, Hamming, fbp, sino_geom_plot!
using ImageGeoms: ImageGeom, fovs, MaskCircle
Expand Down
2 changes: 1 addition & 1 deletion docs/lit/examples/06-fan-short.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ anti-scatter grids.

# Packages needed here.

using Plots: plot, gui # these 2 must precede Sinograms for Requires to work!
using Plots: plot, gui
using Unitful: mm
using Sinograms: SinoFanArc, rays, plan_fbp, fbp, sino_geom_plot!
using ImageGeoms: ImageGeom, fovs, MaskCircle
Expand Down
2 changes: 1 addition & 1 deletion docs/lit/examples/07-fdk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using the Julia package

# Packages needed here.

using Plots: plot, gui # these 2 must precede Sinograms for Requires to work!
using Plots: plot, gui
using Unitful: cm
using Sinograms: CtFanArc, CtFanFlat # CtPar
using Sinograms: rays, plan_fbp, Window, Hamming, fdk, ct_geom_plot3
Expand Down
9 changes: 9 additions & 0 deletions ext/Sinograms_Plots.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Sinograms_Plots.jl
# support plots (possibly with units) iff user has loaded Plots

module Sinograms_Plots

include("sino-plot.jl") # fbp2
include("ct-plot.jl") # fbp3

end # module
63 changes: 63 additions & 0 deletions ext/Sinograms_Units.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Sinograms_Units.jl
# Support data with units iff user has loaded Unitful

module Sinograms_Units

# extended:
import Sinograms: fft_filter, to_radians, _unit_precision
import FFTW: fft, ifft
import Unitful: uconvert

using Sinograms: _fft_filter, _reale
using Unitful: °, rad, NoDims, unit
using Unitful: Units, Quantity, convfact, ustrip


#=
https://github.com/PainterQubits/Unitful.jl/issues/375
=#
function uconvert(a::Units, x::Quantity{T,D,U}) where {T<:AbstractFloat,D,U}
return Quantity(x.val * T(convfact(a, U())), a)
end


"""
to_radians(angles::AbstractArray{Unitful.Quantity})
Convert `Unitful` quantity array to radians.
"""
function to_radians(aa::AbstractArray{<: Quantity{T}}) where {T <: AbstractFloat}
U = eltype(aa)
c = rad(oneunit(U)) / oneunit(U)
return aa * c
end


_unit_precision(x::Quantity{T}) where {T <: Number} = "Unit{$T}"


# generic unitless linear operation applied to data with units
function _linear_fun(fun::Function,
x::AbstractArray{<: Quantity},
args...
)
x0 = ustrip(x) # unitless view into x data
y0 = fun(x0, args...)
u = unit(eltype(x))
Tu = typeof(one(eltype(y0)) * unit(eltype(x)))
return reinterpret(Tu, y0)
end

# fft for data with units
fft(x::AbstractArray{<: Quantity}, args...) =
_linear_fun(fft, x, args...)
ifft(x::AbstractArray{<: Quantity}, args...) =
_linear_fun(ifft, x, args...)

fft_filter(data::AbstractArray{<: Quantity{<:Complex}}, args...) =
_fft_filter(data, args...)

fft_filter(data::AbstractArray{<: Quantity{<:Real}}, args...) =
_reale(_fft_filter(data, args...))

end # module
6 changes: 3 additions & 3 deletions src/fbp3/ct-plot.jl → ext/ct-plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Show CT geometries.
2022-10-01, Jeff Fessler
=#

export ct_geom_plot2!, ct_geom_plot3
import Sinograms: ct_geom_plot2!, ct_geom_plot3 # extended here

#using Sinograms: CtGeom, RealU, source_zs
using .Plots: plot, plot!, default
using Sinograms: CtGeom, CtFan, RealU, _t, _source_zs
using Plots: plot, plot!, default
using ImageGeoms: axes


Expand Down
18 changes: 10 additions & 8 deletions src/fbp2/sino-plot.jl → ext/sino-plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ Show 2D sinogram geometries.
2022-01-23, Jeff Fessler
=#

export sino_plot_rays, sino_geom_plot!
import Sinograms: sino_plot_rays, sino_geom_plot! # extended here

#using Sinograms: SinoGeom, RealU
using .Plots: scatter, plot, plot!, default, xlims!
using Sinograms: SinoGeom, RealU, rays, SinoPar, SinoFan, SinoMoj
using Sinograms: _ar, _rfov, _dso, _xds, _yds, _d_moj
using Plots: scatter, plot, plot!, default, xlims!
using ImageGeoms: ImageGeom


"""
sino_plot_rays(rg::SinoGeom ; kwargs...)
Make a scatter plot of the `(r, ϕ)` sample locations for all rays.
Requires `Plots`.
Needs `Plots`.
"""
function sino_plot_rays(rg::SinoGeom; kwargs...)
# r, phi = rays(rg)
Expand Down Expand Up @@ -101,12 +103,12 @@ function sino_geom_plot_fan!(
ylims = (-1,1) .* dso
tmp = ar .+ π/2 # trick: angle beta defined ccw from y axis
scat!([p0[1]], [p0[2]], color=:blue, marker=:square, # source
; xlims, ylims)
plot!(dso * cos.(t), dso * sin.(t), color=:cyan) # source circle
; xlims, ylims, label="")
plot!(dso * cos.(t), dso * sin.(t), color=:cyan, label="") # source circle
scat!(dso * cos.(tmp), dso * sin.(tmp),
color=:blue, marker=:circle, markersize=2) # source points
color=:blue, marker=:circle, markersize=2, label="") # source points
scat!(vec(pd[1,:]), vec(pd[2,:]), marker=:circle,
color=:orange, markersize=1) # detectors
color=:orange, markersize=1, label="") # detectors

plot!([pd[1,1], p0[1], pd[1,end]], [pd[2,1], p0[2], pd[2,end]],
color=:red, label="")
Expand Down
29 changes: 3 additions & 26 deletions src/Sinograms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"""
module Sinograms

using Requires: @require

const RealU = Number # Real or Unitful

include("unit.jl")
include("exts.jl")

include("geom/util.jl")
include("geom/ct-source.jl")
include("geom/type2.jl")
Expand Down Expand Up @@ -43,28 +44,4 @@ include("sys2/footprint.jl")
include("sys2/zwart_powell.jl")
include("sys2/bdd_2d.jl")


"""
to_radians(angles::AbstractArray{<:AbstractFloat})
When Unitful package not loaded,
assume `angles` are in degrees and convert to radians.
"""
to_radians(aa::AbstractArray{T}) where {T <: AbstractFloat} = aa * T(deg2rad(1))

_unit_precision(x::T) where {T <: Number} = T


# support Plots (with units) iff user has loaded the relevant packages
function __init__()
@require Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" begin
include("fbp2/sino-plot.jl")
include("fbp3/ct-plot.jl")
end

@require Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" begin
include("units.jl")
end
end


end # module
11 changes: 11 additions & 0 deletions src/exts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# exts.jl

# methods defined in the extension(s)
export sino_plot_rays, sino_geom_plot!
export ct_geom_plot2!, ct_geom_plot3

sino_plot_rays(::Nothing) = throw("Run `using Plots` first")
sino_geom_plot!(::Nothing) = throw("Run `using Plots` first")

ct_geom_plot2!(::Nothing) = throw("Run `using Plots` first")
ct_geom_plot3(::Nothing) = throw("Run `using Plots` first")
12 changes: 12 additions & 0 deletions src/unit.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# unit.jl

export to_radians

"""
to_radians(angles::AbstractArray{<:AbstractFloat})
When Unitful package not loaded,
assume `angles` are in degrees and convert to radians.
"""
to_radians(aa::AbstractArray{T}) where {T <: AbstractFloat} = aa * T(deg2rad(1))

_unit_precision(x::T) where {T <: Number} = T
55 changes: 0 additions & 55 deletions src/units.jl

This file was deleted.

1 change: 0 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
ImageGeoms = "9ee76f2b-840d-4475-b6d6-e485c9297852"
ImagePhantoms = "71a99df6-f52c-4da1-bd2a-69d6f37f3252"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LazyGrids = "7031d0ef-c40d-4431-b2f8-61a8d2f650db"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Expand Down
4 changes: 2 additions & 2 deletions test/geom/ct-geom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function values(rg::R) where {R <: CtGeom}
end


function _test_construct(geo ; ds=2mm, orbit=180.0°)
function _test_construct3(geo ; ds=2mm, orbit=180.0°)
rg = @inferred geo(; ds, orbit)
args = values(rg)
Td = typeof(ds)
Expand All @@ -48,7 +48,7 @@ end
ds, orbit = 2mm, 180.0° # stress
for geo in list
@test (@inferred geo(; ds, orbit)) isa CtGeom
@test _test_construct(geo)
@test _test_construct3(geo)
end

rg = @inferred ge1( ; unit = oneunit(ds), orbit=:short)
Expand Down
4 changes: 2 additions & 2 deletions test/geom/sino-geom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function values(rg::R) where {R <: SinoGeom}
end


function _test_construct(geo ; d=2mm, orbit=180.0°)
function _test_construct2(geo ; d=2mm, orbit=180.0°)
rg = @inferred geo(; d, orbit)
args = values(rg)
Td = typeof(d)
Expand All @@ -46,7 +46,7 @@ end
d, orbit = 2mm, 180.0° # stress
for geo in list
@test (@inferred geo(; d, orbit)) isa SinoGeom
@test _test_construct(geo)
@test _test_construct2(geo)
end

rg = @inferred ge1( ; unit = oneunit(d), orbit=:short)
Expand Down
Loading

2 comments on commit f130aa9

@JeffFessler
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator() register

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/107080

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.0 -m "<description of version>" f130aa9e1031196ae8afec7023aeac44bd9d09af
git push origin v0.5.0

Please sign in to comment.