Skip to content

Commit f7dc5b0

Browse files
committed
🧹 Move restrict to ImageAxes and ImageMetadata
We used to host `restrict` in ImageTransformations, which is a big dependency for ImageAxes and ImageMetadata. Thus specialized `restrict` methods for those data types are kept in Images. Now that we have `restrict` in ImageBase, we can move these specialized methods to ImageAxes and ImageMetadata. This commit requires three new versions: - ImageBase v0.1.2: this version fixes some of old `restrict` type inferrence issues. - ImageAxes v0.6.9: JuliaImages/ImageAxes.jl#59 - ImageMetadata v0.9.7: JuliaImages/ImageMetadata.jl#79 AxisArrays is no longer needed because the method is moved to ImageAxes. ImageTransformations <v0.9 compatibility is dropped so that we can avoid symbol conflict on `restrict`.
1 parent 000a4d1 commit f7dc5b0

File tree

7 files changed

+73
-119
lines changed

7 files changed

+73
-119
lines changed

‎Project.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ uuid = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
33
version = "0.24.1"
44

55
[deps]
6-
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"
76
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
87
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
98
Graphics = "a2bd30eb-e257-5431-a919-1863eab51364"
@@ -31,22 +30,21 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
3130
TiledIteration = "06e1c1a7-607b-532d-9fad-de7d9aa2abac"
3231

3332
[compat]
34-
AxisArrays = "0.3, 0.4"
3533
FileIO = "1"
3634
Graphics = "0.4, 1.0"
37-
ImageAxes = "0.6"
35+
ImageAxes = "0.6.9"
3836
ImageBase = "0.1.5"
3937
ImageContrastAdjustment = "0.3.3"
4038
ImageCore = "0.9.3"
4139
ImageDistances = "0.2.5"
4240
ImageFiltering = "0.6.3"
4341
ImageIO = "0.0.1, 0.3, 0.4, 0.5"
4442
ImageMagick = "0.7.5, 1"
45-
ImageMetadata = "0.6, 0.7, 0.8, 0.9"
43+
ImageMetadata = "0.9.7"
4644
ImageMorphology = "0.2.7"
4745
ImageQualityIndexes = "0.1.3, 0.2"
4846
ImageShow = "0.1, 0.2, 0.3"
49-
ImageTransformations = "0.4, 0.5, 0.6, 0.7, 0.8"
47+
ImageTransformations = "0.9"
5048
IndirectArrays = "0.5"
5149
OffsetArrays = "0.8, 0.9, 0.10, 0.11, 1.0.1"
5250
Reexport = "0.2, 1.0"

‎src/Images.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ end
9999
# in deprecations.jl
100100
import ImageContrastAdjustment: build_histogram, adjust_histogram, adjust_histogram!
101101

102-
using ImageMetadata: ImageMetaAxis
103102
import ImageMorphology: dilate, erode
104-
import ImageTransformations: restrict
105103
using TiledIteration: EdgeIterator
106104

107105
include("compat.jl")
@@ -131,7 +129,6 @@ export
131129
nimages,
132130
pixelspacing,
133131
properties,
134-
restrict,
135132
sdims,
136133
size_spatial,
137134
shareproperties,

‎src/algorithms.jl

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ using Base: axes1, tail
22
using OffsetArrays
33
import Statistics
44
using Statistics: mean, var
5-
import AxisArrays
65
using ImageMorphology: dilate!, erode!
76

87
function Statistics.var(A::AbstractArray{C}; kwargs...) where C<:AbstractGray
@@ -285,50 +284,6 @@ Like `findlocalmaxima`, but returns the coordinates of the smallest elements.
285284
findlocalminima(img::AbstractArray, region=coords_spatial(img), edges=true) =
286285
findlocalextrema(img, region, edges, Base.Order.Reverse)
287286

288-
# restrict for AxisArray and ImageMeta
289-
restrict(img::AxisArray, ::Tuple{}) = img
290-
restrict(img::ImageMeta, ::Tuple{}) = img
291-
292-
function restrict(img::ImageMeta, region::Dims)
293-
shareproperties(img, restrict(arraydata(img), region))
294-
end
295-
296-
function restrict(img::AxisArray{T,N}, region::Dims) where {T,N}
297-
inregion = falses(ndims(img))
298-
inregion[[region...]] .= true
299-
inregiont = (inregion...,)::NTuple{N,Bool}
300-
rdata = restrict(img.data, region)
301-
AxisArray(rdata, map(modax, AxisArrays.axes(img), axes(rdata), inregiont))
302-
end
303-
304-
# FIXME: this doesn't get inferred, but it should be (see issue #628)
305-
function restrict(img::Union{AxisArray,ImageMetaAxis}, ::Type{Ax}) where Ax
306-
dim = axisdim(img, Ax)
307-
A = restrict(img.data, dim)
308-
AxisArray(A, replace_axis(modax(img[Ax], axes(A)[dim]), AxisArrays.axes(img)))
309-
end
310-
311-
replace_axis(newax, axs) = _replace_axis(newax, axnametype(newax), axs...)
312-
@inline _replace_axis(newax, ::Type{Ax}, ax::Ax, axs...) where {Ax} = (newax, _replace_axis(newax, Ax, axs...)...)
313-
@inline _replace_axis(newax, ::Type{Ax}, ax, axs...) where {Ax} = (ax, _replace_axis(newax, Ax, axs...)...)
314-
_replace_axis(newax, ::Type{Ax}) where {Ax} = ()
315-
316-
axnametype(ax::Axis{name}) where {name} = Axis{name}
317-
318-
ofaxes(::Base.OneTo, r) = r
319-
ofaxes(axs::Any, r) = OffsetArray(r, axs)
320-
321-
function modax(ax, Aax)
322-
v = ax.val
323-
if iseven(length(v))
324-
return ax(ofaxes(Aax, range(first(v)-step(v)/2, stop=last(v)+step(v)/2, length=length(v)÷2 + 1)))
325-
else
326-
return ax(ofaxes(Aax, range(first(v)/1, stop=last(v)/1, length=length(v)÷2 + 1)))
327-
end
328-
end
329-
330-
modax(ax, Aax, inregion::Bool) = inregion ? modax(ax, Aax) : ax
331-
332287

333288
function imlineardiffusion(img::Array{T,2}, dt::AbstractFloat, iterations::Integer) where T
334289
u = img

‎test/algorithms.jl

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -211,72 +211,6 @@ using Test, Suppressor
211211
@test img2 ≈ A
212212
end
213213

214-
# functionality moved to ImageTransformations
215-
# tests are here as well to make sure everything
216-
# is exported properly.
217-
@testset "Restriction" begin
218-
imgcol = colorview(RGB, rand(3,5,6))
219-
A = reshape([convert(UInt16, i) for i = 1:60], 4, 5, 3)
220-
B = restrict(A, (1,2))
221-
Btarget = cat([ 0.96875 4.625 5.96875;
222-
2.875 10.5 12.875;
223-
1.90625 5.875 6.90625],
224-
[ 8.46875 14.625 13.46875;
225-
17.875 30.5 27.875;
226-
9.40625 15.875 14.40625],
227-
[15.96875 24.625 20.96875;
228-
32.875 50.5 42.875;
229-
16.90625 25.875 21.90625], dims=3)
230-
@test B ≈ Btarget
231-
Argb = reinterpretc(RGB, reinterpret(N0f16, permutedims(A, (3,1,2))))
232-
B = restrict(Argb)
233-
Bf = permutedims(reinterpretc(eltype(eltype(B)), B), (2,3,1))
234-
@test isapprox(Bf, Btarget/reinterpret(one(N0f16)), atol=1e-10)
235-
Argba = reinterpretc(RGBA{N0f16}, reinterpret(N0f16, A))
236-
B = restrict(Argba)
237-
@test isapprox(reinterpretc(eltype(eltype(B)), B), restrict(A, (2,3))/reinterpret(one(N0f16)), atol=1e-10)
238-
A = reshape(1:60, 5, 4, 3)
239-
B = restrict(A, (1,2,3))
240-
@test cat([ 2.6015625 8.71875 6.1171875;
241-
4.09375 12.875 8.78125;
242-
3.5390625 10.59375 7.0546875],
243-
[10.1015625 23.71875 13.6171875;
244-
14.09375 32.875 18.78125;
245-
11.0390625 25.59375 14.5546875], dims=3) ≈ B
246-
imgcolax = AxisArray(imgcol, :y, :x)
247-
imgr = restrict(imgcolax, (1,2))
248-
@test pixelspacing(imgr) == (2,2)
249-
@test pixelspacing(imgcolax) == (1,1) # issue #347
250-
@inferred(restrict(imgcolax, Axis{:y}))
251-
@inferred(restrict(imgcolax, Axis{:x}))
252-
restrict(imgcolax, Axis{:y}) # FIXME #628
253-
restrict(imgcolax, Axis{:x})
254-
imgmeta = ImageMeta(imgcol, myprop=1)
255-
@test isa(restrict(imgmeta, [1, 2]), ImageMeta)
256-
# Issue #395
257-
img1 = colorview(RGB, fill(0.9, 3, 5, 5))
258-
img2 = colorview(RGB, fill(N0f8(0.9), 3, 5, 5))
259-
@test isapprox(channelview(restrict(img1)), channelview(restrict(img2)), rtol=0.01)
260-
# Issue #655
261-
tmp = AxisArray(rand(1080,1120,5,10), (:x, :y, :z, :t), (0.577, 0.5770, 5, 2));
262-
@test size(restrict(tmp, 2), 2) == 561
263-
264-
# restricting OffsetArrays
265-
A = ones(4, 5)
266-
Ao = OffsetArray(A, 0:3, -2:2)
267-
Aor = restrict(Ao)
268-
@test axes(Aor) == axes(OffsetArray(restrict(A), 0:2, -1:1))
269-
270-
# restricting AxisArrays with offset axes
271-
AA = AxisArray(Ao, Axis{:y}(0:3), Axis{:x}(-2:2))
272-
@test axes(AA) === axes(Ao)
273-
AAr = restrict(AA)
274-
axs = axisvalues(AAr)
275-
@test axes(axs[1])[1] == axes(Aor)[1]
276-
@test axes(axs[2])[1] == axes(Aor)[2]
277-
AAA = AxisArray(Aor, axs) # just test that it's constructable (another way of enforcing agreement)
278-
end
279-
280214
@testset "Erode/ dilate" begin
281215
A = zeros(4,4,3)
282216
A[2,2,1] = 0.8

‎test/deprecated.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@
88
@test all(complement(img) .== 1 .- img)
99
end
1010

11+
@testset "Restrict with vector dimensions" begin
12+
imgcol = colorview(RGB, rand(3,5,6))
13+
imgmeta = ImageMeta(imgcol, myprop=1)
14+
@test isa(restrict(imgmeta, [1, 2]), ImageMeta)
15+
end
1116
end

‎test/legacy.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,68 @@
5252
dc = maxfinite(img)-RGB{Float32}(maximum(A, dims=(2,3))...)
5353
@test _abs(dc) < 1e-6
5454
end
55+
56+
# Once moved to ImageTransformations, and then moved to ImageBase
57+
@testset "Restriction" begin
58+
imgcol = colorview(RGB, rand(3,5,6))
59+
A = reshape([convert(UInt16, i) for i = 1:60], 4, 5, 3)
60+
B = restrict(A, (1,2))
61+
Btarget = cat([ 0.96875 4.625 5.96875;
62+
2.875 10.5 12.875;
63+
1.90625 5.875 6.90625],
64+
[ 8.46875 14.625 13.46875;
65+
17.875 30.5 27.875;
66+
9.40625 15.875 14.40625],
67+
[15.96875 24.625 20.96875;
68+
32.875 50.5 42.875;
69+
16.90625 25.875 21.90625], dims=3)
70+
@test B ≈ Btarget
71+
Argb = reinterpretc(RGB, reinterpret(N0f16, permutedims(A, (3,1,2))))
72+
B = restrict(Argb)
73+
Bf = permutedims(reinterpretc(eltype(eltype(B)), B), (2,3,1))
74+
@test isapprox(Bf, Btarget/reinterpret(one(N0f16)), atol=1e-10)
75+
Argba = reinterpretc(RGBA{N0f16}, reinterpret(N0f16, A))
76+
B = restrict(Argba)
77+
@test isapprox(reinterpretc(eltype(eltype(B)), B), restrict(A, (2,3))/reinterpret(one(N0f16)), atol=1e-10)
78+
A = reshape(1:60, 5, 4, 3)
79+
B = restrict(A, (1,2,3))
80+
@test cat([ 2.6015625 8.71875 6.1171875;
81+
4.09375 12.875 8.78125;
82+
3.5390625 10.59375 7.0546875],
83+
[10.1015625 23.71875 13.6171875;
84+
14.09375 32.875 18.78125;
85+
11.0390625 25.59375 14.5546875], dims=3) ≈ B
86+
imgcolax = AxisArray(imgcol, :y, :x)
87+
imgr = restrict(imgcolax, (1,2))
88+
@test pixelspacing(imgr) == (2,2)
89+
@test pixelspacing(imgcolax) == (1,1) # issue #347
90+
@inferred(restrict(imgcolax, Axis{:y}))
91+
@inferred(restrict(imgcolax, Axis{:x}))
92+
restrict(imgcolax, Axis{:y}) # FIXME #628
93+
restrict(imgcolax, Axis{:x})
94+
imgmeta = ImageMeta(imgcol, myprop=1)
95+
@test isa(restrict(imgmeta, (1, 2)), ImageMeta)
96+
# Issue #395
97+
img1 = colorview(RGB, fill(0.9, 3, 5, 5))
98+
img2 = colorview(RGB, fill(N0f8(0.9), 3, 5, 5))
99+
@test isapprox(channelview(restrict(img1)), channelview(restrict(img2)), rtol=0.01)
100+
# Issue #655
101+
tmp = AxisArray(rand(1080,1120,5,10), (:x, :y, :z, :t), (0.577, 0.5770, 5, 2));
102+
@test size(restrict(tmp, 2), 2) == 561
103+
104+
# restricting OffsetArrays
105+
A = ones(4, 5)
106+
Ao = OffsetArray(A, 0:3, -2:2)
107+
Aor = restrict(Ao)
108+
@test axes(Aor) == axes(OffsetArray(restrict(A), 0:2, -1:1))
109+
110+
# restricting AxisArrays with offset axes
111+
AA = AxisArray(Ao, Axis{:y}(0:3), Axis{:x}(-2:2))
112+
@test axes(AA) === axes(Ao)
113+
AAr = restrict(AA)
114+
axs = axisvalues(AAr)
115+
@test axes(axs[1])[1] == axes(Aor)[1]
116+
@test axes(axs[2])[1] == axes(Aor)[2]
117+
AAA = AxisArray(Aor, axs) # just test that it's constructable (another way of enforcing agreement)
118+
end
55119
end

‎test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using Images
22
using OffsetArrays
33
using Statistics
44
using Test
5+
using ImageBase
56
using Suppressor
67

78
@testset "Images" begin

0 commit comments

Comments
 (0)