Skip to content

Commit

Permalink
Merge pull request #8 from feanor12/update_for_1.1
Browse files Browse the repository at this point in the history
update so 1.1 should pass the tests
  • Loading branch information
platawiec authored May 27, 2019
2 parents 17ce3e6 + cb633f0 commit 9a7b0fb
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ os:
- linux
- osx
julia:
- 0.6
- 1.0
- 1.1
- nightly
notifications:
email: false
Expand Down
16 changes: 16 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 0 additions & 1 deletion REQUIRE

This file was deleted.

8 changes: 4 additions & 4 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 13 additions & 12 deletions src/unwrap_common.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Random

mutable struct Pixel{T}
periods::Int
val::T
reliability::Float64
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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/test_unwrap_1D.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
16 changes: 8 additions & 8 deletions test/test_unwrap_2D.jl
Original file line number Diff line number Diff line change
@@ -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, :]
Expand Down
14 changes: 7 additions & 7 deletions test/test_unwrap_3D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9a7b0fb

Please sign in to comment.