Skip to content

Commit f130aa9

Browse files
authored
Use package extensions, not Requires; to v0.5 (#66)
* Extensions for plots and units * import * Update import etc. * Purge Requires
1 parent ec86c79 commit f130aa9

File tree

17 files changed

+141
-120
lines changed

17 files changed

+141
-120
lines changed

Project.toml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
name = "Sinograms"
22
uuid = "02a14def-c6e6-4ab0-b2df-0ab64bc8cdd7"
33
authors = ["fessler <[email protected]>"]
4-
version = "0.4.1"
4+
version = "0.5"
55

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

1312
[compat]
14-
FFTW = "1.4.5, 1.5"
15-
ImageGeoms = "0.9, 0.10"
13+
FFTW = "1.5"
14+
ImageGeoms = "0.11"
1615
Interpolations = "0.14, 0.15"
17-
LazyGrids = "0.4, 0.5, 1"
18-
Requires = "1.3"
16+
LazyGrids = "1"
17+
Unitful = "1.19"
1918
julia = "1.10"
19+
20+
[weakdeps]
21+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
22+
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
23+
24+
[extensions]
25+
Sinograms_Plots = "Plots"
26+
Sinograms_Units = "Unitful"

docs/lit/examples/04-fan-arc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ anti-scatter grids.
2222

2323
# Packages needed here.
2424

25-
using Plots: plot, gui # these 2 must precede Sinograms for Requires to work!
25+
using Plots: plot, gui
2626
using Unitful: mm
2727
using Sinograms: SinoFanArc, rays, plan_fbp, fbp, sino_geom_plot!
2828
using ImageGeoms: ImageGeom, fovs, MaskCircle

docs/lit/examples/05-fan-flat.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ as used in many cone-beam CT (CBCT) systems.
1717

1818
# Packages needed here.
1919

20-
using Plots: plot, gui # these 2 must precede Sinograms for Requires to work!
20+
using Plots: plot, gui
2121
using Unitful: cm
2222
using Sinograms: SinoFanFlat, rays, plan_fbp, Window, Hamming, fbp, sino_geom_plot!
2323
using ImageGeoms: ImageGeom, fovs, MaskCircle

docs/lit/examples/06-fan-short.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ anti-scatter grids.
2626

2727
# Packages needed here.
2828

29-
using Plots: plot, gui # these 2 must precede Sinograms for Requires to work!
29+
using Plots: plot, gui
3030
using Unitful: mm
3131
using Sinograms: SinoFanArc, rays, plan_fbp, fbp, sino_geom_plot!
3232
using ImageGeoms: ImageGeom, fovs, MaskCircle

docs/lit/examples/07-fdk.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using the Julia package
1414

1515
# Packages needed here.
1616

17-
using Plots: plot, gui # these 2 must precede Sinograms for Requires to work!
17+
using Plots: plot, gui
1818
using Unitful: cm
1919
using Sinograms: CtFanArc, CtFanFlat # CtPar
2020
using Sinograms: rays, plan_fbp, Window, Hamming, fdk, ct_geom_plot3

ext/Sinograms_Plots.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Sinograms_Plots.jl
2+
# support plots (possibly with units) iff user has loaded Plots
3+
4+
module Sinograms_Plots
5+
6+
include("sino-plot.jl") # fbp2
7+
include("ct-plot.jl") # fbp3
8+
9+
end # module

ext/Sinograms_Units.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Sinograms_Units.jl
2+
# Support data with units iff user has loaded Unitful
3+
4+
module Sinograms_Units
5+
6+
# extended:
7+
import Sinograms: fft_filter, to_radians, _unit_precision
8+
import FFTW: fft, ifft
9+
import Unitful: uconvert
10+
11+
using Sinograms: _fft_filter, _reale
12+
using Unitful: °, rad, NoDims, unit
13+
using Unitful: Units, Quantity, convfact, ustrip
14+
15+
16+
#=
17+
https://github.com/PainterQubits/Unitful.jl/issues/375
18+
=#
19+
function uconvert(a::Units, x::Quantity{T,D,U}) where {T<:AbstractFloat,D,U}
20+
return Quantity(x.val * T(convfact(a, U())), a)
21+
end
22+
23+
24+
"""
25+
to_radians(angles::AbstractArray{Unitful.Quantity})
26+
27+
Convert `Unitful` quantity array to radians.
28+
"""
29+
function to_radians(aa::AbstractArray{<: Quantity{T}}) where {T <: AbstractFloat}
30+
U = eltype(aa)
31+
c = rad(oneunit(U)) / oneunit(U)
32+
return aa * c
33+
end
34+
35+
36+
_unit_precision(x::Quantity{T}) where {T <: Number} = "Unit{$T}"
37+
38+
39+
# generic unitless linear operation applied to data with units
40+
function _linear_fun(fun::Function,
41+
x::AbstractArray{<: Quantity},
42+
args...
43+
)
44+
x0 = ustrip(x) # unitless view into x data
45+
y0 = fun(x0, args...)
46+
u = unit(eltype(x))
47+
Tu = typeof(one(eltype(y0)) * unit(eltype(x)))
48+
return reinterpret(Tu, y0)
49+
end
50+
51+
# fft for data with units
52+
fft(x::AbstractArray{<: Quantity}, args...) =
53+
_linear_fun(fft, x, args...)
54+
ifft(x::AbstractArray{<: Quantity}, args...) =
55+
_linear_fun(ifft, x, args...)
56+
57+
fft_filter(data::AbstractArray{<: Quantity{<:Complex}}, args...) =
58+
_fft_filter(data, args...)
59+
60+
fft_filter(data::AbstractArray{<: Quantity{<:Real}}, args...) =
61+
_reale(_fft_filter(data, args...))
62+
63+
end # module
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Show CT geometries.
44
2022-10-01, Jeff Fessler
55
=#
66

7-
export ct_geom_plot2!, ct_geom_plot3
7+
import Sinograms: ct_geom_plot2!, ct_geom_plot3 # extended here
88

9-
#using Sinograms: CtGeom, RealU, source_zs
10-
using .Plots: plot, plot!, default
9+
using Sinograms: CtGeom, CtFan, RealU, _t, _source_zs
10+
using Plots: plot, plot!, default
1111
using ImageGeoms: axes
1212

1313

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ Show 2D sinogram geometries.
44
2022-01-23, Jeff Fessler
55
=#
66

7-
export sino_plot_rays, sino_geom_plot!
7+
import Sinograms: sino_plot_rays, sino_geom_plot! # extended here
88

9-
#using Sinograms: SinoGeom, RealU
10-
using .Plots: scatter, plot, plot!, default, xlims!
9+
using Sinograms: SinoGeom, RealU, rays, SinoPar, SinoFan, SinoMoj
10+
using Sinograms: _ar, _rfov, _dso, _xds, _yds, _d_moj
11+
using Plots: scatter, plot, plot!, default, xlims!
12+
using ImageGeoms: ImageGeom
1113

1214

1315
"""
1416
sino_plot_rays(rg::SinoGeom ; kwargs...)
1517
1618
Make a scatter plot of the `(r, ϕ)` sample locations for all rays.
17-
Requires `Plots`.
19+
Needs `Plots`.
1820
"""
1921
function sino_plot_rays(rg::SinoGeom; kwargs...)
2022
# r, phi = rays(rg)
@@ -101,12 +103,12 @@ function sino_geom_plot_fan!(
101103
ylims = (-1,1) .* dso
102104
tmp = ar .+ π/2 # trick: angle beta defined ccw from y axis
103105
scat!([p0[1]], [p0[2]], color=:blue, marker=:square, # source
104-
; xlims, ylims)
105-
plot!(dso * cos.(t), dso * sin.(t), color=:cyan) # source circle
106+
; xlims, ylims, label="")
107+
plot!(dso * cos.(t), dso * sin.(t), color=:cyan, label="") # source circle
106108
scat!(dso * cos.(tmp), dso * sin.(tmp),
107-
color=:blue, marker=:circle, markersize=2) # source points
109+
color=:blue, marker=:circle, markersize=2, label="") # source points
108110
scat!(vec(pd[1,:]), vec(pd[2,:]), marker=:circle,
109-
color=:orange, markersize=1) # detectors
111+
color=:orange, markersize=1, label="") # detectors
110112

111113
plot!([pd[1,1], p0[1], pd[1,end]], [pd[2,1], p0[2], pd[2,end]],
112114
color=:red, label="")

src/Sinograms.jl

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"""
44
module Sinograms
55

6-
using Requires: @require
7-
86
const RealU = Number # Real or Unitful
97

8+
include("unit.jl")
9+
include("exts.jl")
10+
1011
include("geom/util.jl")
1112
include("geom/ct-source.jl")
1213
include("geom/type2.jl")
@@ -43,28 +44,4 @@ include("sys2/footprint.jl")
4344
include("sys2/zwart_powell.jl")
4445
include("sys2/bdd_2d.jl")
4546

46-
47-
"""
48-
to_radians(angles::AbstractArray{<:AbstractFloat})
49-
When Unitful package not loaded,
50-
assume `angles` are in degrees and convert to radians.
51-
"""
52-
to_radians(aa::AbstractArray{T}) where {T <: AbstractFloat} = aa * T(deg2rad(1))
53-
54-
_unit_precision(x::T) where {T <: Number} = T
55-
56-
57-
# support Plots (with units) iff user has loaded the relevant packages
58-
function __init__()
59-
@require Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" begin
60-
include("fbp2/sino-plot.jl")
61-
include("fbp3/ct-plot.jl")
62-
end
63-
64-
@require Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" begin
65-
include("units.jl")
66-
end
67-
end
68-
69-
7047
end # module

0 commit comments

Comments
 (0)