diff --git a/.travis.yml b/.travis.yml index f41d046..1c9ddf2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,8 @@ os: - linux - osx julia: - - 0.6 + - 1.0 + - 1.1 - nightly notifications: email: false diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..5305019 --- /dev/null +++ b/Project.toml @@ -0,0 +1,16 @@ +name = "Unwrap" +uuid = "66ceed60-733c-11e9-33de-751f162d5676" +authors = ["platawiec"] +version = "0.1.0" + +[deps] +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[extras] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test=["Test"] + +[compat] +julia = "^1.0" \ No newline at end of file diff --git a/REQUIRE b/REQUIRE deleted file mode 100644 index 137767a..0000000 --- a/REQUIRE +++ /dev/null @@ -1 +0,0 @@ -julia 0.6 diff --git a/src/interface.jl b/src/interface.jl index 2e6a84a..d37653a 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -11,18 +11,18 @@ Unwraps the values in A modulo 2π, where A is a 1-, 2-, or 3- dimensional array - `seed::Int`: Unwrapping of 2D or 3D images uses a random initialization. This sets the seed of the RNG. """ -unwrap +function unwrap end """ unwrap!(A[, wrap_around, seed]) In-place version of unwrap. """ -unwrap! +function unwrap! end -function unwrap{T, N}(A::AbstractArray{T, N}, +function unwrap(A::AbstractArray{T, N}, wrap_around::NTuple{N, Bool}=tuple(zeros(Bool, N)...), - seed::Int=-1) + seed::Int=-1) where {T,N} A_copy = copy(A) unwrap!(A_copy, wrap_around, seed) return A_copy diff --git a/src/unwrap_common.jl b/src/unwrap_common.jl index 880292f..f47778f 100644 --- a/src/unwrap_common.jl +++ b/src/unwrap_common.jl @@ -1,3 +1,5 @@ +using Random + mutable struct Pixel{T} periods::Int val::T @@ -5,7 +7,7 @@ mutable struct Pixel{T} groupsize::Int head::Pixel{T} last::Pixel{T} - next::Union{Void, Pixel{T}} + next::Union{Nothing, Pixel{T}} function Pixel{T}(periods, val, rel, gs) where T pixel = new(periods, val, rel, gs) pixel.head = pixel @@ -38,7 +40,7 @@ function unwrap!(wrapped_image::AbstractArray{T, N}, seed::Int=-1) where {T, N} if seed != -1 - srand(seed) + Random.seed!(seed) end pixel_image = init_pixels(wrapped_image) @@ -162,7 +164,7 @@ function populate_edges!(edges, pixel_image::Array{T, N}, dim, connected) where idx_step[dim] += 1 idx_step_cart = CartesianIndex{N}(idx_step...) idx_size = CartesianIndex{N}(size_img...) - for i in CartesianRange(idx_size) + for i in CartesianIndices((:).(Tuple(CartesianIndex{N}()),Tuple(idx_size))) push!(edges, Edge{N}(pixel_image, i, i+idx_step_cart)) end if connected @@ -172,7 +174,7 @@ function populate_edges!(edges, pixel_image::Array{T, N}, dim, connected) where edge_begin = fill(1, N) edge_begin[dim] = size(pixel_image)[dim] edge_begin_cart = CartesianIndex{N}(edge_begin...) - for i in CartesianRange(edge_begin_cart, CartesianIndex(size(pixel_image))) + for i in CartesianIndices((:).(Tuple(edge_begin_cart), Tuple(CartesianIndex(size(pixel_image))))) push!(edges, Edge{N}(pixel_image, i, i+idx_step_cart)) end end @@ -181,11 +183,10 @@ end function calculate_reliability(pixel_image::AbstractArray{T, N}, wrap_around) where {T, N} # get the shifted pixel indices in CartesinanIndex form # This gets all the nearest neighbors (CartesionIndex{N}() = one(CartesianIndex{N})) - pixel_shifts = collect(CartesianRange(-CartesianIndex{N}(), - CartesianIndex{N}())) + pixel_shifts = collect(CartesianIndices(tuple(repeat([-1:1],N)...))) size_img = size(pixel_image) # inner loop - for i in CartesianRange((CartesianIndex{N}()+1), (CartesianIndex{N}(size_img)-1)) + for i in CartesianIndices((:).(tuple(fill(2,length(size_img))...),size_img.-1)) @inbounds pixel_image[i].reliability = calculate_pixel_reliability(pixel_image, i, pixel_shifts) end @@ -204,14 +205,14 @@ function calculate_reliability(pixel_image::AbstractArray{T, N}, wrap_around) wh border_begin = fill(2, N) border_begin[idx_dim] = size_img[idx_dim] border_begin = CartesianIndex{N}(border_begin...) - border_end = collect(size_img)-1 + border_end = collect(size_img).-1 border_end[idx_dim] = size_img[idx_dim] border_end = CartesianIndex{N}(border_end...) - for i in CartesianRange(border_begin, border_end) + for i in CartesianIndices((:).(Tuple(border_begin), Tuple(border_end))) @inbounds pixel_image[i].reliability = calculate_pixel_reliability(pixel_image, i, pixel_shifts_border) end # second border - pixel_shifts_border = copy!(pixel_shifts_border, pixel_shifts) + pixel_shifts_border = copyto!(pixel_shifts_border, pixel_shifts) for (idx_ps, ps) in enumerate(pixel_shifts_border) # if the pixel shift goes out of bounds, we make the shift wrap, this time to the other side if ps[idx_dim] == -1 @@ -223,10 +224,10 @@ function calculate_reliability(pixel_image::AbstractArray{T, N}, wrap_around) wh border_begin = fill(2, N) border_begin[idx_dim] = 1 border_begin = CartesianIndex{N}(border_begin...) - border_end = collect(size_img)-1 + border_end = collect(size_img).-1 border_end[idx_dim] = 1 border_end = CartesianIndex{N}(border_end...) - for i in CartesianRange(border_begin, border_end) + for i in CartesianIndices((:).(Tuple(border_begin), Tuple(border_end))) @inbounds pixel_image[i].reliability = calculate_pixel_reliability(pixel_image, i, pixel_shifts_border) end end diff --git a/test/runtests.jl b/test/runtests.jl index b3a9efb..c81571b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,5 @@ using Unwrap -using Base.Test +using Test @time @testset "Unwrap 1D" begin include("test_unwrap_1D.jl") end @time @testset "Unwrap 2D" begin include("test_unwrap_2D.jl") end diff --git a/test/test_unwrap_1D.jl b/test/test_unwrap_1D.jl index bd1c24e..5075557 100644 --- a/test/test_unwrap_1D.jl +++ b/test/test_unwrap_1D.jl @@ -1,4 +1,4 @@ -A_unwrapped = collect(linspace(0, 10π, 100)) +A_unwrapped = collect(range(0, stop=10π, length=100)) A_wrapped = A_unwrapped .% (2π) @test unwrap(A_wrapped) == A_unwrapped @@ -7,10 +7,10 @@ A_wrapped = A_unwrapped .% (2π) unwrap!(A_wrapped) @test A_wrapped == A_unwrapped -A_bf_uw = collect(linspace(0, 10*BigFloat(π), 100)) +A_bf_uw = collect(range(0, stop=10*BigFloat(π), length=100)) A_bf_w = A_bf_uw .% (2*BigFloat(π)) @test unwrap(A_bf_w) ≈ A_bf_uw -A_f32_uw = collect(linspace(0, 10*Float32(π), 100)) +A_f32_uw = collect(range(0, stop=10*Float32(π), length=100)) A_f32_w = A_bf_uw .% (2*Float32(π)) @test unwrap(A_bf_w) ≈ A_bf_uw diff --git a/test/test_unwrap_2D.jl b/test/test_unwrap_2D.jl index 2cfd0f0..bfe8823 100644 --- a/test/test_unwrap_2D.jl +++ b/test/test_unwrap_2D.jl @@ -1,37 +1,37 @@ -vec_unwrapped = linspace(0, 5π, 20) +vec_unwrapped = range(0, stop=5π, length=20) mat_unwrapped = vec_unwrapped .+ vec_unwrapped' mat_wrapped = mat_unwrapped .% (2π) uw_test = unwrap(mat_wrapped) difference = mat_unwrapped[1,1] - uw_test[1,1] -@test (uw_test + difference) ≈ mat_unwrapped +@test (uw_test .+ difference) ≈ mat_unwrapped # test in-place version unwrap!(mat_wrapped) difference = mat_unwrapped[1,1] - mat_wrapped[1,1] -@test (mat_wrapped + difference) ≈ mat_unwrapped +@test (mat_wrapped .+ difference) ≈ mat_unwrapped # Test BigFloat -vec_bf = linspace(0, 5*BigFloat(π), 20) +vec_bf = range(0, stop=5*BigFloat(π), length=20) mat_bf_uw = vec_bf .+ vec_bf' mat_bf_w = mat_bf_uw .% (2*BigFloat(π)) uw_bf_test = unwrap(mat_bf_w) difference = mat_bf_uw[1,1] - uw_bf_test[1,1] @test eltype(uw_bf_test) == BigFloat -@test (uw_bf_test + difference) ≈ mat_bf_uw +@test (uw_bf_test .+ difference) ≈ mat_bf_uw # Test Float32 -vec_f32 = linspace(0, 5*Float32(π), 20) +vec_f32 = range(0, stop=5*Float32(π), length=20) mat_f32_uw = vec_f32 .+ vec_f32' mat_f32_w = mat_f32_uw .% (2*Float32(π)) uw_f32_test = unwrap(mat_f32_w) difference = mat_f32_uw[1,1] - uw_f32_test[1,1] @test eltype(uw_f32_test) == Float32 -@test (uw_f32_test + difference) ≈ mat_f32_uw +@test (uw_f32_test .+ difference) ≈ mat_f32_uw # Test wrap_around # after unwrapping, pixels at borders should be equal to corresponding pixels # on other side wrap_around = (true, true) -wa_vec = linspace(0, 5π, 20) +wa_vec = range(0, stop=5π, length=20) wa_uw = wa_vec .+ zeros(20)' # make periodic wa_uw[end, :] = wa_uw[1, :] diff --git a/test/test_unwrap_3D.jl b/test/test_unwrap_3D.jl index fdc144c..129fe95 100644 --- a/test/test_unwrap_3D.jl +++ b/test/test_unwrap_3D.jl @@ -3,34 +3,34 @@ f(x, y, z) = 0.5x^2 - 0.1y^3 + z f_wraparound2(x, y, z) = 5*sin(x) + 2*cos(y) + z f_wraparound3(x, y, z) = 5*sin(x) + 2*cos(y) - 3*cos(z) for T in Types - grid = linspace(zero(T), 2π*one(T), 40) + grid = range(zero(T), stop=2π*one(T), length=40) f_uw = f.(grid, grid', reshape(grid, 1, 1, :)) f_wr = f_uw .% (2π) uw_test = unwrap(f_wr) difference = first(f_uw) - first(uw_test) - @test isapprox(uw_test + difference, f_uw, atol=1e-8) + @test isapprox(uw_test .+ difference, f_uw, atol=1e-8) # test in-place version unwrap!(f_wr) difference = first(f_uw) - first(f_wr) - @test isapprox(f_wr + difference, f_uw, atol=1e-8) + @test isapprox(f_wr .+ difference, f_uw, atol=1e-8) f_uw = f_wraparound2.(grid, grid', reshape(grid, 1, 1, :)) f_wr = f_uw .% (2π) uw_test = unwrap(f_wr, (true, true, false)) difference = first(f_uw) - first(uw_test) - @test isapprox(uw_test + difference, f_uw, atol=1e-8) + @test isapprox(uw_test .+ difference, f_uw, atol=1e-8) # test in-place version unwrap!(f_wr, (true, true, false)) difference = first(f_uw) - first(f_wr) - @test isapprox(f_wr + difference, f_uw, atol=1e-8) + @test isapprox(f_wr .+ difference, f_uw, atol=1e-8) f_uw = f_wraparound3.(grid, grid', reshape(grid, 1, 1, :)) f_wr = f_uw .% (2π) uw_test = unwrap(f_wr, (true, true, true)) difference = first(f_uw) - first(uw_test) - @test isapprox(uw_test + difference, f_uw, atol=1e-8) + @test isapprox(uw_test .+ difference, f_uw, atol=1e-8) # test in-place version unwrap!(f_wr, (true, true, true)) difference = first(f_uw) - first(f_wr) - @test isapprox(f_wr + difference, f_uw, atol=1e-8) + @test isapprox(f_wr .+ difference, f_uw, atol=1e-8) end