From d34831b5f450dcc980ea7a0bb14dbcb48adf29c3 Mon Sep 17 00:00:00 2001 From: aquatiko Date: Thu, 8 Aug 2019 23:31:46 +0530 Subject: [PATCH 1/5] Support for RGB --- src/AstroImages.jl | 33 ++++++++++++++++++++++++++------- test/ccd2rgb.jl | 7 +++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/AstroImages.jl b/src/AstroImages.jl index c1d7cfb4..402be58a 100644 --- a/src/AstroImages.jl +++ b/src/AstroImages.jl @@ -85,9 +85,21 @@ for n in (8, 16, 32, 64) end end +mutable struct properties + rgb_image::AbstractArray + function properties(;kvs...) + obj = new() + for (k,v) in kvs + setproperty!(obj, k, v) + end + return obj + end +end + struct AstroImage{T<:Real,C<:Color, N} data::NTuple{N, Matrix{T}} wcs::NTuple{N, WCSTransform} + property::properties end """ @@ -97,13 +109,18 @@ end Construct an `AstroImage` object of `data`, using `color` as color map, `Gray` by default. """ AstroImage(color::Type{<:Color}, data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = - AstroImage{T,color, 1}((data,), (wcs,)) -AstroImage(color::Type{<:Color}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = - AstroImage{T,color, N}(data, wcs) -AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1}((data,), (WCSTransform(2),)) -AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, ntuple(i-> WCSTransform(2), N)) -AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1}((data,), (wcs,)) -AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, wcs) + AstroImage{T,color, 1}((data,), (wcs,), properties()) +function AstroImage(color::Type{<:Color}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} + if N == 3 && color == RGB + return AstroImage{T,color,N}(data, wcs, properties(rgb_image = ccd2rgb((data[1], wcs[1]),(data[2], wcs[2]),(data[3], wcs[3])))) + else + return AstroImage{T,color, N}(data, wcs, properties()) + end +end +AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1}((data,), (WCSTransform(2),), properties()) +AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, ntuple(i-> WCSTransform(2), N), properties()) +AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1}((data,), (wcs,), properties()) +AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, wcs, properties()) """ AstroImage([color=Gray,] filename::String, n::Int=1) @@ -130,6 +147,8 @@ AstroImage(color::Type{<:Color}, fits::NTuple{N, FITS}, ext::NTuple{N, Int}) whe AstroImage(files::NTuple{N,String}) where {N} = AstroImage(Gray, load(files)...) +AstroImage(color::Type{<:Color}, files::NTuple{N,String}) where {N} = + AstroImage(color, load(files)...) AstroImage(file::String) = AstroImage((file,)) # Lazily render the image as a Matrix{Color}, upon request. diff --git a/test/ccd2rgb.jl b/test/ccd2rgb.jl index 1652e9a2..7d880dba 100644 --- a/test/ccd2rgb.jl +++ b/test/ccd2rgb.jl @@ -44,4 +44,11 @@ end @test isapprox(red.(asinh_res), red.(asinh_ans), nans = true, rtol = 3e-5) @test isapprox(blue.(asinh_res), blue.(asinh_ans), nans = true, rtol = 3e-5) @test isapprox(green.(asinh_res), green.(asinh_ans), nans = true, rtol = 3e-5) + + @testset "AstroImage using ccd2rgb" begin + img = AstroImage(RGB, (joinpath("data","casa_0.5-1.5keV.fits"), joinpath("data","casa_1.5-3.0keV.fits"), + joinpath("data","casa_4.0-6.0keV.fits"))) + + @test RGB.(img.property.rgb_image) isa Array{RGB{Float64},2} + end end From bf2cf6ab147e2e16b460fb7467b231f9d6360a69 Mon Sep 17 00:00:00 2001 From: aquatiko Date: Tue, 13 Aug 2019 00:12:53 +0530 Subject: [PATCH 2/5] parametric inner constructor --- src/AstroImages.jl | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/AstroImages.jl b/src/AstroImages.jl index 402be58a..1430610f 100644 --- a/src/AstroImages.jl +++ b/src/AstroImages.jl @@ -85,10 +85,10 @@ for n in (8, 16, 32, 64) end end -mutable struct properties - rgb_image::AbstractArray - function properties(;kvs...) - obj = new() +mutable struct properties{T <: AbstractArray} + rgb_image::T + function properties{T}(;kvs...) where T + obj = new{T}() for (k,v) in kvs setproperty!(obj, k, v) end @@ -109,18 +109,19 @@ end Construct an `AstroImage` object of `data`, using `color` as color map, `Gray` by default. """ AstroImage(color::Type{<:Color}, data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = - AstroImage{T,color, 1}((data,), (wcs,), properties()) + AstroImage{T,color, 1}((data,), (wcs,), properties{AbstractArray{T,3}}()) function AstroImage(color::Type{<:Color}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} if N == 3 && color == RGB - return AstroImage{T,color,N}(data, wcs, properties(rgb_image = ccd2rgb((data[1], wcs[1]),(data[2], wcs[2]),(data[3], wcs[3])))) + img = ccd2rgb((data[1], wcs[1]),(data[2], wcs[2]),(data[3], wcs[3])) + return AstroImage{T,color,N}(data, wcs, properties{typeof(img)}(rgb_image = img)) else - return AstroImage{T,color, N}(data, wcs, properties()) + return AstroImage{T,color, N}(data, wcs, properties{AbstractArray{T,3}}()) end end -AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1}((data,), (WCSTransform(2),), properties()) -AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, ntuple(i-> WCSTransform(2), N), properties()) -AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1}((data,), (wcs,), properties()) -AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, wcs, properties()) +AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1}((data,), (WCSTransform(2),), properties{AbstractArray{T,3}}()) +AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, ntuple(i-> WCSTransform(2), N), properties{AbstractArray{T,3}}()) +AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1}((data,), (wcs,), properties{AbstractArray{T,3}}()) +AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, wcs, properties{AbstractArray{T,3}}()) """ AstroImage([color=Gray,] filename::String, n::Int=1) From d02ea786680af1662476131700c8aa406db79d06 Mon Sep 17 00:00:00 2001 From: aquatiko Date: Sun, 18 Aug 2019 00:37:02 +0530 Subject: [PATCH 3/5] MappedArrays dependency --- Project.toml | 1 + src/AstroImages.jl | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Project.toml b/Project.toml index 1b7fc9c9..bfa26bab 100644 --- a/Project.toml +++ b/Project.toml @@ -12,6 +12,7 @@ Interact = "c601a237-2ae4-5e1e-952c-7a85b0c7eef1" RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" Reproject = "d1dcc2e6-806e-11e9-2897-3f99785db2ae" WCS = "15f3aee2-9e10-537f-b834-a6fb8bdb944d" +MappedArrays = "dbb5928d-eab1-5f90-85c2-b9b0edb7c900" [compat] julia = "^1.0.0" diff --git a/src/AstroImages.jl b/src/AstroImages.jl index 1430610f..4503f80e 100644 --- a/src/AstroImages.jl +++ b/src/AstroImages.jl @@ -2,7 +2,7 @@ __precompile__() module AstroImages -using FITSIO, FileIO, Images, Interact, Reproject, WCS +using FITSIO, FileIO, Images, Interact, Reproject, WCS, MappedArrays export load, AstroImage, ccd2rgb @@ -85,9 +85,9 @@ for n in (8, 16, 32, 64) end end -mutable struct properties{T <: AbstractArray} - rgb_image::T - function properties{T}(;kvs...) where T +mutable struct Properties{T <: Real} + rgb_image::MappedArrays.MultiMappedArray{RGB{T},2,Tuple{Array{T,2},Array{Float64,2},Array{T,2}},Type{RGB{T}},typeof(ImageCore.extractchannels)} + function Properties{T}(;kvs...) where T obj = new{T}() for (k,v) in kvs setproperty!(obj, k, v) @@ -99,7 +99,7 @@ end struct AstroImage{T<:Real,C<:Color, N} data::NTuple{N, Matrix{T}} wcs::NTuple{N, WCSTransform} - property::properties + property::Properties{T} end """ @@ -109,19 +109,19 @@ end Construct an `AstroImage` object of `data`, using `color` as color map, `Gray` by default. """ AstroImage(color::Type{<:Color}, data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = - AstroImage{T,color, 1}((data,), (wcs,), properties{AbstractArray{T,3}}()) + AstroImage{T,color, 1}((data,), (wcs,), Properties{T}()) function AstroImage(color::Type{<:Color}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} if N == 3 && color == RGB img = ccd2rgb((data[1], wcs[1]),(data[2], wcs[2]),(data[3], wcs[3])) - return AstroImage{T,color,N}(data, wcs, properties{typeof(img)}(rgb_image = img)) + return AstroImage{T,color,N}(data, wcs, Properties{T}(rgb_image = img)) else - return AstroImage{T,color, N}(data, wcs, properties{AbstractArray{T,3}}()) + return AstroImage{T,color, N}(data, wcs, Properties{T}()) end end -AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1}((data,), (WCSTransform(2),), properties{AbstractArray{T,3}}()) -AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, ntuple(i-> WCSTransform(2), N), properties{AbstractArray{T,3}}()) -AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1}((data,), (wcs,), properties{AbstractArray{T,3}}()) -AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, wcs, properties{AbstractArray{T,3}}()) +AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1}((data,), (WCSTransform(2),), Properties{T}()) +AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, ntuple(i-> WCSTransform(2), N), Properties{T}()) +AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1}((data,), (wcs,), Properties{T}()) +AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, wcs, Properties{T}()) """ AstroImage([color=Gray,] filename::String, n::Int=1) From afd6a1867c3bd57780ccf69a76a6e76c0fa8f2ca Mon Sep 17 00:00:00 2001 From: aquatiko Date: Sun, 18 Aug 2019 20:20:17 +0530 Subject: [PATCH 4/5] refactoring --- src/AstroImages.jl | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/AstroImages.jl b/src/AstroImages.jl index 4503f80e..5849e8c4 100644 --- a/src/AstroImages.jl +++ b/src/AstroImages.jl @@ -85,10 +85,10 @@ for n in (8, 16, 32, 64) end end -mutable struct Properties{T <: Real} - rgb_image::MappedArrays.MultiMappedArray{RGB{T},2,Tuple{Array{T,2},Array{Float64,2},Array{T,2}},Type{RGB{T}},typeof(ImageCore.extractchannels)} - function Properties{T}(;kvs...) where T - obj = new{T}() +mutable struct Properties{P <: Union{AbstractFloat, FixedPoint}} + rgb_image::MappedArrays.MultiMappedArray{RGB{P},2,Tuple{Array{P,2},Array{Float64,2},Array{P,2}},Type{RGB{P}},typeof(ImageCore.extractchannels)} + function Properties{P}(;kvs...) where P + obj = new{P}() for (k,v) in kvs setproperty!(obj, k, v) end @@ -96,10 +96,10 @@ mutable struct Properties{T <: Real} end end -struct AstroImage{T<:Real,C<:Color, N} +struct AstroImage{T<:Real,C<:Color, N, P} data::NTuple{N, Matrix{T}} wcs::NTuple{N, WCSTransform} - property::Properties{T} + property::Properties{P} end """ @@ -109,19 +109,22 @@ end Construct an `AstroImage` object of `data`, using `color` as color map, `Gray` by default. """ AstroImage(color::Type{<:Color}, data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = - AstroImage{T,color, 1}((data,), (wcs,), Properties{T}()) + AstroImage{T,color, 1, AbstractFloat}((data,), (wcs,), Properties{AbstractFloat}()) function AstroImage(color::Type{<:Color}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} - if N == 3 && color == RGB + if N == 3 && color == RGB && T <: Union{AbstractFloat, FixedPoint} img = ccd2rgb((data[1], wcs[1]),(data[2], wcs[2]),(data[3], wcs[3])) - return AstroImage{T,color,N}(data, wcs, Properties{T}(rgb_image = img)) + return AstroImage{T,color,N, widen(T)}(data, wcs, Properties{widen(T)}(rgb_image = img)) + elseif N == 3 && color == RGB + img = ccd2rgb((data[1], wcs[1]),(data[2], wcs[2]),(data[3], wcs[3])) + return AstroImage{T,color,N, AbstractFloat}(data, wcs, Properties{AbstractFloat}(rgb_image = img)) else - return AstroImage{T,color, N}(data, wcs, Properties{T}()) + return AstroImage{T,color, N, AbstractFloat}(data, wcs, Properties{AbstractFloat}()) end end -AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1}((data,), (WCSTransform(2),), Properties{T}()) -AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, ntuple(i-> WCSTransform(2), N), Properties{T}()) -AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1}((data,), (wcs,), Properties{T}()) -AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, wcs, Properties{T}()) +AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1, AbstractFloat}((data,), (WCSTransform(2),), Properties{AbstractFloat}()) +AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N, AbstractFloat}(data, ntuple(i-> WCSTransform(2), N), Properties{AbstractFloat}()) +AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1, AbstractFloat}((data,), (wcs,), Properties{AbstractFloat}()) +AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N, AbstractFloat}(data, wcs, Properties{AbstractFloat}()) """ AstroImage([color=Gray,] filename::String, n::Int=1) From 4b2a7e6e90ac8eaaf2f003cfd6d130b0e3ecc233 Mon Sep 17 00:00:00 2001 From: aquatiko Date: Tue, 20 Aug 2019 12:48:38 +0530 Subject: [PATCH 5/5] minor changes --- src/AstroImages.jl | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/AstroImages.jl b/src/AstroImages.jl index 5849e8c4..07dd90bd 100644 --- a/src/AstroImages.jl +++ b/src/AstroImages.jl @@ -86,7 +86,7 @@ for n in (8, 16, 32, 64) end mutable struct Properties{P <: Union{AbstractFloat, FixedPoint}} - rgb_image::MappedArrays.MultiMappedArray{RGB{P},2,Tuple{Array{P,2},Array{Float64,2},Array{P,2}},Type{RGB{P}},typeof(ImageCore.extractchannels)} + rgb_image::MappedArrays.MultiMappedArray{RGB{P},2,Tuple{Array{P,2},Array{P,2},Array{P,2}},Type{RGB{P}},typeof(ImageCore.extractchannels)} function Properties{P}(;kvs...) where P obj = new{P}() for (k,v) in kvs @@ -109,22 +109,26 @@ end Construct an `AstroImage` object of `data`, using `color` as color map, `Gray` by default. """ AstroImage(color::Type{<:Color}, data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = - AstroImage{T,color, 1, AbstractFloat}((data,), (wcs,), Properties{AbstractFloat}()) -function AstroImage(color::Type{<:Color}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} - if N == 3 && color == RGB && T <: Union{AbstractFloat, FixedPoint} + AstroImage{T,color, 1, Float64}((data,), (wcs,), Properties{Float64}()) +function AstroImage(color::Type{<:AbstractRGB}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T <: Union{AbstractFloat, FixedPoint}, N} + if N == 3 img = ccd2rgb((data[1], wcs[1]),(data[2], wcs[2]),(data[3], wcs[3])) return AstroImage{T,color,N, widen(T)}(data, wcs, Properties{widen(T)}(rgb_image = img)) - elseif N == 3 && color == RGB + end +end +function AstroImage(color::Type{<:AbstractRGB}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} + if N == 3 img = ccd2rgb((data[1], wcs[1]),(data[2], wcs[2]),(data[3], wcs[3])) - return AstroImage{T,color,N, AbstractFloat}(data, wcs, Properties{AbstractFloat}(rgb_image = img)) - else - return AstroImage{T,color, N, AbstractFloat}(data, wcs, Properties{AbstractFloat}()) + return AstroImage{T,color,N, Float64}(data, wcs, Properties{Float64}(rgb_image = img)) end end -AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1, AbstractFloat}((data,), (WCSTransform(2),), Properties{AbstractFloat}()) -AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N, AbstractFloat}(data, ntuple(i-> WCSTransform(2), N), Properties{AbstractFloat}()) -AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1, AbstractFloat}((data,), (wcs,), Properties{AbstractFloat}()) -AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N, AbstractFloat}(data, wcs, Properties{AbstractFloat}()) +function AstroImage(color::Type{<:Color}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} + return AstroImage{T,color, N, Float64}(data, wcs, Properties{Float64}()) +end +AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1, Float64}((data,), (WCSTransform(2),), Properties{Float64}()) +AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N, Float64}(data, ntuple(i-> WCSTransform(2), N), Properties{Float64}()) +AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1, Float64}((data,), (wcs,), Properties{Float64}()) +AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N, Float64}(data, wcs, Properties{Float64}()) """ AstroImage([color=Gray,] filename::String, n::Int=1)