Skip to content

Commit ac8ede1

Browse files
authored
Merge pull request #35 from yha/offset-axes
Preserve offset axes in @progress with comprehension
2 parents 10208bc + 2afd01c commit ac8ede1

File tree

3 files changed

+98
-51
lines changed

3 files changed

+98
-51
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
1111
julia = "0.7, 1"
1212

1313
[extras]
14+
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
1415
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1516
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1617

18+
1719
[targets]
18-
test = ["Test", "Random"]
20+
test = ["OffsetArrays", "Random", "Test"]

src/ProgressLogging.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ end
452452

453453
function _progress(name, thresh, ex, target, result, loop, iter_vars, ranges, body)
454454
count_vars = [gensym(Symbol("i$k")) for k = 1:length(iter_vars)]
455-
iter_exprs = [:(($i, $v) = $enumerate($r)) for (i, v, r) in zip(
455+
iter_exprs = [:(($i, $v) = $zip($_linindex($r),$r)) for (i, v, r) in zip(
456456
count_vars,
457457
iter_vars,
458458
ranges,
@@ -496,14 +496,17 @@ end
496496

497497
function make_count_to_frac(iterators...)
498498
lens = map(length, iterators)
499+
firsts = map(first_linindex, iterators)
499500
n = prod(lens)
500501
strides = (1, taccumulate(*, Base.front(lens))...)
501502
function count_to_frac(idxs...)
502-
offsets = map(i -> i - 1, idxs)
503+
offsets = map(-, idxs, firsts)
503504
total = sum(map(*, offsets, strides)) + 1
504505
return total / n
505506
end
506507
return count_to_frac
507508
end
508509

510+
_linindex(a) = LinearIndices(axes(a))
511+
509512
end # module

test/test_progress_macro.jl

Lines changed: 90 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,105 @@
11
module TestProgressMacro
22

3-
using ProgressLogging: @progress
3+
using ProgressLogging: @progress, ProgressLevel
44
using Test
5+
using Test: collect_test_logs
6+
using OffsetArrays
57

6-
let i = 0, x
7-
x = @progress for _ = 1:100
8-
i += 1
9-
end
10-
@test i == 100
11-
@test x == nothing
12-
end
8+
@testset "@progress" begin
9+
let i = 0, x
10+
x = @progress for _ = 1:100
11+
i += 1
12+
end
13+
@test i == 100
14+
@test x == nothing
15+
end
1316

14-
let i = 0, r = -50:10:50, x
15-
x = @progress for _ in r
16-
i += 1
17-
end
18-
@test i == 11
19-
@test x == nothing
20-
end
17+
let i = 0, r = -50:10:50, x
18+
x = @progress for _ in r
19+
i += 1
20+
end
21+
@test i == 11
22+
@test x == nothing
23+
end
2124

22-
let i = 0, x
23-
x = @progress "named" for _ = 1:100
24-
i += 1
25-
end
26-
@test i == 100
27-
@test x == nothing
28-
end
25+
let i = 0, x
26+
x = @progress "named" for _ = 1:100
27+
i += 1
28+
end
29+
@test i == 100
30+
@test x == nothing
31+
end
2932

30-
let i = 0, j = 0, x
31-
x = @progress for _ = 1:10, __ = 1:20
32-
i += 1
33-
end
34-
@test i == 200
35-
@test x == nothing
36-
end
33+
let i = 0, j = 0, x
34+
x = @progress for _ = 1:10, __ = 1:20
35+
i += 1
36+
end
37+
@test i == 200
38+
@test x == nothing
39+
end
40+
41+
let i = 0, j = 0, x
42+
bar = "bar"
43+
x = @progress "foo $bar" for _ = 1:10
44+
i += 1
45+
end
46+
@test i == 10
47+
@test x == nothing
48+
end
49+
50+
let x, y
51+
x = @progress y = [i + 3j for i = 1:3, j = 1:4]
52+
@test y == reshape(4:15, 3, 4)
53+
@test x == y
54+
end
3755

38-
let i = 0, j = 0, x
39-
bar = "bar"
40-
x = @progress "foo $bar" for _ = 1:10
41-
i += 1
56+
let a = [], x
57+
x = @progress for i = 1:3, j in [-5, -2, -1, 8]
58+
j > 0 && continue
59+
push!(a, (i, j))
60+
i > 1 && break
61+
end
62+
@test a == [(1, -5), (1, -2), (1, -1), (2, -5)]
63+
@test x == nothing
64+
end
65+
66+
# Multi-dimensional arrays in comprehension and offset axes
67+
let off1 = -2, off2 = 21
68+
v1 = OffsetArray(2:3, off1)
69+
v2 = OffsetArray(-1:2, off2)
70+
logs, _ = collect_test_logs(min_level = ProgressLevel) do
71+
x = @progress y = [i*j for i in v1, j in v2]
72+
@test x == y == OffsetArray([-2 0 2 4; -3 0 3 6], off1, off2)
4273
end
43-
@test i == 10
44-
@test x == nothing
45-
end
74+
@test isequal(
75+
[l.kwargs[:progress] for l in logs],
76+
[nothing; (1:8)./8; "done"],
77+
)
78+
m = OffsetArray(reshape(2:7,2,3), off1, off2)
79+
x = @progress y = [i*j for i in v1, j in m]
80+
@test x == y == [i*j for i in v1, j in m]
81+
end
4682

47-
let x, y
48-
x = @progress y = [i + 3j for i = 1:3, j = 1:4]
49-
@test y == reshape(4:15, 3, 4)
50-
@test x == y
51-
end
83+
# non-indexable iterables with axes
84+
@testset "non-indexable" for off in (0,10)
85+
let r = OffsetVector(1:5, off)
86+
x1 = @progress y1 = [i for i in (x^2 for x in r)]
87+
x2 = @progress y2 = [i for i in zip(r,r)]
88+
@test x1 == y1 == r.^2
89+
@test x2 == y2 == collect(zip(r,r))
5290

53-
let a = [], x
54-
x = @progress for i = 1:3, j in [-5, -2, -1, 8]
55-
j > 0 && continue
56-
push!(a, (i, j))
57-
i > 1 && break
91+
y1, y2 = [], []
92+
x1 = @progress for i in (x^2 for x in r)
93+
push!(y1, i)
94+
end
95+
x2 = @progress for i in zip(r,r)
96+
push!(y2, i)
97+
end
98+
@test x1 == x2 == nothing
99+
@test OffsetVector(y1,off) == r.^2
100+
@test OffsetVector(y2,off) == collect(zip(r,r))
58101
end
59-
@test a == [(1, -5), (1, -2), (1, -1), (2, -5)]
60-
@test x == nothing
102+
end
61103
end
62104

63105
end # module

0 commit comments

Comments
 (0)