Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Towards supporting Unitful axes #10

Merged
merged 3 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MIRTjim"
uuid = "170b2178-6dee-4cb0-8729-b3e8b57834cc"
authors = ["Jeff Fessler <[email protected]> and contributors"]
version = "0.7.0"
version = "0.8.0"

[deps]
FFTViews = "4f61f5a4-77b1-5117-aa51-3ab5ef4ef0cd"
Expand All @@ -14,5 +14,5 @@ REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
FFTViews = "0.3"
LaTeXStrings = "1"
MosaicViews = "0.2, 0.3"
Plots = "1.15"
Plots = "1.16"
julia = "1.6"
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DocMeta.setdocmeta!(MIRTjim, :DocTestSetup, :(using MIRTjim); recursive=true)

makedocs(;
modules = [MIRTjim],
authors = "Jeff Fessler <[email protected]> and contributors",
authors = "Jeff Fessler and contributors",
repo = "https://github.com/JeffFessler/MIRTjim.jl/blob/{commit}{path}#{line}",
sitename = "MIRTjim.jl",
format = Documenter.HTML(;
Expand Down
50 changes: 38 additions & 12 deletions src/jim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export jim
using Plots: heatmap, plot, plot!, Plot
using MosaicViews: mosaicview
using FFTViews: FFTView
#using Unitful: unit


# global default key/values
Expand Down Expand Up @@ -98,8 +99,10 @@ function jim(z::AbstractArray{<:Real} ;
xlabel::AbstractString = jim_def[:xlabel],
ylabel::AbstractString = jim_def[:ylabel],
fft0::Bool = jim_def[:fft0],
x = fft0 ? ((-size(z,1)÷2):(size(z,1)÷2-1)) : collect(axes(z)[1]),
y = fft0 ? ((-size(z,2)÷2):(size(z,2)÷2-1)) : collect(axes(z)[2]),
x::AbstractVector{<:Real} =
fft0 ? ((-size(z,1)÷2):(size(z,1)÷2-1)) : collect(axes(z)[1]),
y::AbstractVector{<:Real} =
fft0 ? ((-size(z,2)÷2):(size(z,2)÷2-1)) : collect(axes(z)[2]),
xticks = (minimum(x) < 0 && maximum(x) > 0) ?
[minfloor(x),0,maxceil(x)] : [minfloor(x),maxceil(x)],
yticks = (minimum(y) < 0 && maximum(y) > 0) ?
Expand Down Expand Up @@ -192,18 +195,21 @@ function jim(z::AbstractArray{<:Real} ;
end # jim


# handle case of complex image
# complex image data
function jim(
z::AbstractArray{<:Number} ;
z::AbstractArray{<:Complex} ;
abswarn::Bool = jim_def[:abswarn],
kwargs...
)

if !(eltype(z) <: Real)
abswarn && (@warn "magnitude at $(caller_name())")
z = abs.(z)
end
jim(z ; kwargs...)
abswarn && (@warn "magnitude at $(caller_name())")
jim(abs.(z) ; kwargs...)
end


# other Numbers types (e.g., with units from Unitful)
function jim(z::AbstractArray{<:Number} ; kwargs...)
jim(z / oneunit(z[1]) ; kwargs...)
end


Expand All @@ -217,14 +223,34 @@ jim(z::AbstractArray{<:Number}, title::AbstractString ; kwargs...) =
"""
jim(x, y, z ; kwargs...)
"""
jim(x::AbstractVector{<:Number}, y, z ; kwargs...) = jim(z ; x, y, kwargs...)
jim(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}, z ; kwargs...) =
jim(z ; x, y, kwargs...)


# Unitful axes currently unsupported, thanks to
# https://github.com/PainterQubits/Unitful.jl/issues/455
function jim(x, y, z ; labelunits::Bool = true, kwargs...)
#=
if labelunits
xunit = labelunits ? " [" * unit(x[1]) * "]" : ""
yunit = labelunits ? " [" * unit(y[1]) * "]" : ""
kwargs[:xlabel] *= xunit
kwargs[:ylabel] *= yunit
end
=#
jim(z ;
x = x / oneunit(x[1]),
y = y / oneunit(y[1]),
kwargs...,
)
end


"""
jim(x, y, z, title::String ; kwargs...)
"""
jim(x::AbstractVector{<:Number}, y, z, title::AbstractString ; kwargs...) =
jim(z ; x, y, title, kwargs...)
jim(x::AbstractVector, y, z, title::AbstractString ; kwargs...) =
jim(x, y, z ; title, kwargs...)


"""
Expand Down
50 changes: 0 additions & 50 deletions test/Manifest.toml

This file was deleted.

4 changes: 4 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[deps]
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
LaTeXStrings = "1"
Unitful = "1"
Plots = "1"
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ include("prompt.jl")

@test length(detect_ambiguities(MIRTjim)) == 0
end

include("unit.jl")
15 changes: 15 additions & 0 deletions test/unit.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# unit.jl

using Unitful
using MIRTjim: jim
using Plots: Plot
using Test: @test, @testset, @inferred

@testset "unit" begin
x = 1u"mm" * (1:7)
y = 1u"s" * (1:5)
f = x * y'

# @test @inferred jim(x, y, f) # Plots.Plot{Plots.GRBackend} vs Plot
@test jim(x, y, f) isa Plot
end