Skip to content

Commit 26f8189

Browse files
authored
Bump DimensionalData to fix broadcasting (#34)
* Bump compat with DimensionalData * Fix ambiguity error with newer DimensionalData * Skip right to DimensionalData 0.23 * Fix dims after imview. * header now shared more often when taking views of data * restored imview live update functionality (actually behave like a view) * Removed ability to call imview then implot and keep axes. This was never going to work in the long run. * Fix tests * Align composecolors with new behavior of imview
1 parent 47c3cc7 commit 26f8189

File tree

7 files changed

+60
-36
lines changed

7 files changed

+60
-36
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ WCS = "15f3aee2-9e10-537f-b834-a6fb8bdb944d"
2828
AbstractFFTs = "1.1"
2929
AstroAngles = "0.1"
3030
ColorSchemes = "3.18"
31-
DimensionalData = "0.20.8"
31+
DimensionalData = "0.23"
3232
FITSIO = "0.16, 0.17"
3333
FileIO = "1.15"
3434
ImageAxes = "0.6"

src/AstroImages.jl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,19 @@ end
240240
)
241241
return AstroImage(data, dims, refdims, header, wcs, Ref(wcs_stale), wcsdims)
242242
end
243-
@inline DimensionalData.rebuildsliced(
243+
244+
@inline function DimensionalData.rebuildsliced(
244245
f::Function,
245-
img::AstroImage,
246-
data,
247-
I,
248-
header=deepcopy(header(img)),
249-
wcs=getfield(img, :wcs),
250-
wcs_stale=getfield(img, :wcs_stale)[],
251-
wcsdims=getfield(img, :wcsdims),
252-
) = rebuild(img, data, DimensionalData.slicedims(f, img, I)..., nothing, nothing, header, wcs, wcs_stale, wcsdims)
246+
A::AstroImage,
247+
data::AbstractArray,
248+
I::Tuple,
249+
header=header(A),
250+
wcs=getfield(A, :wcs),
251+
wcs_stale=getfield(A, :wcs_stale)[],
252+
wcsdims=getfield(A, :wcsdims),)
253+
sd = DimensionalData.slicedims(f, A, I)
254+
rebuild(A; data, dims=sd[1], refdims=sd[2], header, wcs, wcs_stale, wcsdims)
255+
end
253256

254257
# Return result wrapped in AstroImage
255258
# For these functions that return lazy wrappers, we want to share header
@@ -448,6 +451,8 @@ See also: [`shareheader`](@ref).
448451
"""
449452
copyheader(img::AstroImage, data::AbstractArray) =
450453
AstroImage(data, dims(img), refdims(img), deepcopy(header(img)), copy(getfield(img, :wcs)), Ref(getfield(img, :wcs_stale)[]), getfield(img,:wcsdims))
454+
copyheader(img::AstroImage, data::AstroImage) =
455+
AstroImage(data, dims(data), refdims(data), deepcopy(header(img)), copy(getfield(img, :wcs)), Ref(getfield(img, :wcs_stale)[]), getfield(img,:wcsdims))
451456

452457
"""
453458
shareheader(img::AstroImage, data) -> imgnew
@@ -457,6 +462,7 @@ synchronized header; modifying one also affects the other.
457462
See also: [`copyheader`](@ref).
458463
"""
459464
shareheader(img::AstroImage, data::AbstractArray) = AstroImage(data, dims(img), refdims(img), header(img), getfield(img, :wcs), Ref(getfield(img, :wcs_stale)[]), getfield(img,:wcsdims))
465+
shareheader(img::AstroImage, data::AstroImage) = AstroImage(data, dims(data), refdims(data), header(img), getfield(img, :wcs), Ref(getfield(img, :wcs_stale)[]), getfield(img,:wcsdims))
460466
# Share header if an AstroImage, do nothing if AbstractArray
461467
maybe_shareheader(img::AstroImage, data) = shareheader(img, data)
462468
maybe_shareheader(::AbstractArray, data) = data

src/ccd2rgb.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ function composecolors(
7474
clamp(pxblended.alpha,0,1)
7575
)
7676
end
77-
return copyheader(first(images), combined)
77+
return combined
7878
end
7979
export composechannels

src/imview.jl

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -240,21 +240,22 @@ function imview(
240240
# Origin is centre of pixel (1,1) at bottom left.
241241
if ndims(img) == 2
242242
imgT = view(
243-
permutedims(img,(2,1)),
243+
permuteddimsview(img,(2,1)),
244244
reverse(axes(img,2)),
245245
:,
246246
)
247247
elseif ndims(img) >= 3
248248
newdims = (2,1, 3:ndims(img)...)
249249
ds = Tuple(((:) for _ in 2:ndims(img)))
250250
imgT = view(
251-
permutedims(img,newdims),
251+
permuteddimsview(img,newdims),
252252
reverse(axes(img,2)),
253253
ds...,
254254
)
255255
else
256256
imgT = img
257257
end
258+
258259
isempt = isempty(imgT)
259260
if isempt
260261
@warn "imview called with empty argument"
@@ -273,18 +274,7 @@ function imview(
273274
return _imview(imgT, normed, stretch, _lookup_cmap(cmap), contrast, bias)
274275
end
275276

276-
# Unwrap AstroImages before view, then rebuild.
277-
# We have to permute the dimensions of the image to get the origin at the bottom left.
278-
# But we don't want this to affect the dimensions of the array.
279-
# Also, this reduces the number of methods we need to compile for imview by standardizing types
280-
# earlier on. The compiled code for showing an array is the same as an array wrapped by an
281-
# AstroImage, except for one unwrapping step.
282-
function imview(
283-
img::AstroImage;
284-
kwargs...
285-
)
286-
return shareheader(img, imview(parent(img); kwargs...))
287-
end
277+
288278

289279
# Special handling for complex images
290280
"""
@@ -307,8 +297,26 @@ function imview(img::AbstractArray{T}; kwargs...) where {T<:Complex}
307297
vcat(mag_view,angle_view)
308298
end
309299

310-
function _imview(img, normed::AbstractArray{T}, stretch, cmap, contrast, bias) where T
300+
# Unwrap AstroImages before view, then rebuild.
301+
# We have to permute the dimensions of the image to get the origin at the bottom left.
302+
# But we don't want this to affect the dimensions of the array.
303+
# Also, this reduces the number of methods we need to compile for imview by standardizing types
304+
# earlier on. The compiled code for showing an array is the same as an array wrapped by an
305+
# AstroImage, except for one unwrapping step.
306+
function _imview(
307+
img::AstroImage, normed::AbstractArray{T}, stretch, cmap, contrast, bias
308+
) where T
311309

310+
p = parent(img)
311+
out = _imview(p, normed, stretch, cmap, contrast, bias)
312+
# out = shareheader(img, v)
313+
return out
314+
end
315+
316+
317+
318+
319+
function _imview(img, normed::AbstractArray{T}, stretch, cmap, contrast, bias) where T
312320
function colormap(pixr, pixn)::RGBA{N0f8}
313321
if ismissing(pixr) || !isfinite(pixr) || ismissing(pixn) || !isfinite(pixn)
314322
# We check pixr in addition to pixn because we want to preserve if the pixels
@@ -341,8 +349,7 @@ function _imview(img, normed::AbstractArray{T}, stretch, cmap, contrast, bias) w
341349
return pix
342350
end
343351
mapper = mappedarray(colormap, img, normed)
344-
345-
return maybe_copyheader(img, mapper)
352+
return mapper
346353
end
347354

348355

src/io.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ function AstroImage(filename::AbstractString, exts::Union{NTuple{N,<:Integer},Ab
4545
end
4646
end
4747
end
48-
function AstroImage(filename::AbstractString; kwargs...) where {N}
48+
function AstroImage(filename::AbstractString; kwargs...)
4949
return FITS(filename, "r") do fits
5050
ext = indexer(fits)
5151
return AstroImage(fits[ext]; kwargs...)
5252
end
5353
end
54-
function AstroImage(filename::AbstractString, ::Colon, args...; kwargs...) where {N}
54+
function AstroImage(filename::AbstractString, ::Colon, args...; kwargs...)
5555
return FITS(filename, "r") do fits
5656
return map(fits) do hdu
5757
return AstroImage(hdu, args...; kwargs...)
@@ -84,7 +84,7 @@ returned as AstroImage, and TableHDUs are returned as column tables.
8484
8585
!!! Currently any header on TableHDUs are not supported and are ignored.
8686
"""
87-
function fileio_load(f::File{format"FITS"}, ext::Union{Int,Nothing}=nothing, args...; kwargs...) where {N}
87+
function fileio_load(f::File{format"FITS"}, ext::Union{Int,Nothing}=nothing, args...; kwargs...)
8888
return FITS(f.filename, "r") do fits
8989
if isnothing(ext)
9090
ext = indexer(fits)
@@ -99,7 +99,7 @@ function fileio_load(f::File{format"FITS"}, exts::Union{NTuple{N,<:Integer},Abst
9999
end
100100
end
101101
end
102-
function fileio_load(f::File{format"FITS"}, ::Colon, args...; kwargs...) where {N}
102+
function fileio_load(f::File{format"FITS"}, ::Colon, args...; kwargs...)
103103
return FITS(f.filename, "r") do fits
104104
exts_resolved = 1:length(fits)
105105
map(exts_resolved) do ext

src/plot-recipes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
img = data
7272
end
7373
imgv = imview(img; clims, stretch, cmap, contrast, bias)
74+
imgv = shareheader(img, imgv)
7475
end
7576

7677
# Reduce large images using the same heuristic as Images.jl
@@ -516,7 +517,6 @@ function WCSGrid(img::AstroImageMat, wcsn=1)
516517
miny = first(dims(img,1))
517518
maxy = last(dims(img,1))
518519
extent = (minx-0.5, maxx+0.5, miny-0.5, maxy+0.5)
519-
@show extent
520520
return WCSGrid(img, extent, wcsn)
521521
end
522522

test/runtests.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,17 @@ end
211211
##
212212
@testset "imview" begin
213213

214-
arr1 = permutedims(reshape(1:9,3,3))
214+
arr1 = collect(permutedims(reshape(1:9,3,3)))
215215
img = AstroImage(arr1)
216216

217217
@test imview(arr1) == imview(img)
218-
@test imview(img) isa AstroImage
219-
@test !(imview(arr1) isa AstroImage)
220218

219+
## Test view functionality
220+
ivimg = imview(img, clims=(0,9))
221+
img[1] = 0
222+
@test imview(img, clims=(0,9)) == ivimg # Should have updated
223+
img[1] = 1
224+
221225
img_rendered_1 = imview(img, clims=(1,9), stretch=identity, contrast=1, bias=0.5, cmap=nothing)
222226

223227
# Image Orientation
@@ -312,6 +316,13 @@ end
312316
6 5 4
313317
3 2 1
314318
]
319+
320+
321+
# https://github.com/JuliaAstro/AstroImages.jl/issues/33
322+
dark = AstroImage(zeros(1, 10, 10));
323+
raw = AstroImage(ones(5, 10, 10));
324+
@test size(dark .- raw) == size(raw)
325+
315326
end
316327
##
317328

0 commit comments

Comments
 (0)