Skip to content

Commit 3b1ff90

Browse files
committed
docstring change and test formatting
1 parent 13bce2f commit 3b1ff90

File tree

2 files changed

+119
-122
lines changed

2 files changed

+119
-122
lines changed

src/padding.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ of some length `2n` that specifies the left and right padding size
169169
for each of the dimensions in `dims`. If `dims` is not given,
170170
it defaults to the first `n` dimensions.
171171
172-
For integer `pad` input instead, it is applied on both sides
172+
If `pad` is an integer, it is applied on both sides
173173
on every dimension in `dims`. In this case, `dims`
174174
defaults to the first `ndims(x)-2` dimensions
175175
(i.e. excludes the channel and batch dimension).
@@ -230,10 +230,10 @@ of some length `2n` that specifies the left and right padding size
230230
for each of the dimensions in `dims`. If `dims` is not given,
231231
it defaults to the first `n` dimensions.
232232
233-
For integer `pad` input instead, it is applied on both sides
233+
If `pad` is an integer, it is applied on both sides
234234
on every dimension in `dims`. In this case, `dims`
235235
defaults to the first `ndims(x)-2` dimensions
236-
(i.e. excludes the channel and batch dimension).
236+
(i.e. excludes the channel and batch dimension).
237237
238238
See also [`pad_repeat`](@ref), [`pad_symmetric`](@ref), [`pad_circular`](@ref), and [`pad_constant`](@ref).
239239
@@ -288,10 +288,10 @@ of some length `2n` that specifies the left and right padding size
288288
for each of the dimensions in `dims`. If `dims` is not given,
289289
it defaults to the first `n` dimensions.
290290
291-
For integer `pad` input instead, it is applied on both sides
291+
If `pad` is an integer, it is applied on both sides
292292
on every dimension in `dims`. In this case, `dims`
293293
defaults to the first `ndims(x)-2` dimensions
294-
(i.e. excludes the channel and batch dimension).
294+
(i.e. excludes the channel and batch dimension).
295295
296296
See also [`pad_repeat`](@ref), [`pad_reflect`](@ref), [`pad_circular`](@ref), and [`pad_constant`](@ref).
297297
@@ -302,7 +302,7 @@ julia> r = reshape(1:9, 3, 3)
302302
2 5 8
303303
3 6 9
304304
305-
julia> NNlib.pad_symmetric(r, (1,2,1,2))
305+
julia> pad_symmetric(r, (1,2,1,2))
306306
6×6 Matrix{Int64}:
307307
1 1 4 7 7 4
308308
1 1 4 7 7 4
@@ -343,13 +343,13 @@ of some length `2n` that specifies the left and right padding size
343343
for each of the dimensions in `dims`. If `dims` is not given,
344344
it defaults to the first `n` dimensions.
345345
346-
For integer `pad` input instead, it is applied on both sides
346+
If `pad` is an integer, it is applied on both sides
347347
on every dimension in `dims`. In this case, `dims`
348348
defaults to the first `ndims(x)-2` dimensions
349-
(i.e. excludes the channel and batch dimension).
349+
(i.e. excludes the channel and batch dimension).
350350
351-
The pad length in any dimension must not exceed the
352-
size of `x` in that dimension.
351+
The pad length on either side in any dimension must not exceed the
352+
size of `x` in that dimension, i.e. `pad_circular` is not able to create abitrary sized tilings of `x`.
353353
354354
See also [`pad_repeat`](@ref), [`pad_reflect`](@ref), [`pad_symmetric`](@ref), and [`pad_constant`](@ref).
355355
@@ -360,7 +360,7 @@ julia> r = reshape(1:9, 3, 3)
360360
2 5 8
361361
3 6 9
362362
363-
julia> NNlib.pad_circular(r, (1,2,1,2))
363+
julia> pad_circular(r, (1,2,1,2))
364364
6×6 Matrix{Int64}:
365365
9 3 6 9 3 6
366366
7 1 4 7 1 4

test/padding.jl

Lines changed: 108 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,148 @@
1-
using NNlib: pad_constant, pad_repeat, pad_zeros, pad_reflect
1+
using NNlib: pad_constant, pad_repeat, pad_zeros, pad_reflect, pad_symmetric, pad_circular
22

33
@testset "padding constant" begin
4-
x = rand(2, 2, 2)
5-
6-
p = NNlib.gen_pad((1,2,3,4,5,6), (1,2,3), 4)
7-
@test p == ((1, 2), (3, 4), (5, 6), (0, 0))
8-
9-
@test_throws ArgumentError NNlib.gen_pad((1,2,3,4,5,), (1,2,3), 4)
10-
11-
p = NNlib.gen_pad((1,3), (1,3), 4)
12-
@test p == ((1, 1), (0, 0), (3, 3), (0, 0))
13-
14-
p = NNlib.gen_pad(1, (1,2,3), 4)
15-
@test p == ((1, 1), (1, 1), (1, 1), (0, 0))
16-
17-
p = NNlib.gen_pad(3, :, 2)
18-
@test p == ((3, 3), (3, 3))
19-
20-
y = pad_constant(x, (3, 2, 4))
21-
@test size(y) == (8, 6, 10)
22-
@test y[4:5, 3:4, 5:6] x
23-
y[4:5, 3:4, 5:6] .= 0
24-
@test all(y .== 0)
25-
26-
@test pad_constant(x, (3, 2, 4)) pad_zeros(x, (3, 2, 4))
27-
@test pad_zeros(x, 2) pad_zeros(x, (2,2,2))
28-
29-
y = pad_constant(x, (3, 2, 4, 5), 1.2, dims = (1,3))
30-
@test size(y) == (7, 2, 11)
31-
@test y[4:5, 1:2, 5:6] x
32-
y[4:5, 1:2, 5:6] .= 1.2
33-
@test all(y .== 1.2)
34-
35-
@test pad_constant(x, (2,2,2,2), 1.2, dims = (1,3))
36-
pad_constant(x, 2, 1.2, dims = (1,3))
37-
38-
@test pad_constant(x, 1, dims = 1:2) ==
39-
pad_constant(x, 1, dims = (1,2))
40-
41-
@test size(pad_constant(x, 1, dims = 1)) == (4,2,2)
42-
43-
@test all(pad_zeros(randn(2), (1, 2))[[1, 4, 5]] .== 0)
44-
45-
gradtest(x -> pad_constant(x, 2), rand(2,2,2))
46-
gradtest(x -> pad_constant(x, (2, 1, 1, 2)), rand(2,2))
47-
gradtest(x -> pad_constant(x, (2, 1,)), rand(2))
4+
x = rand(2, 2, 2)
5+
6+
p = NNlib.gen_pad((1,2,3,4,5,6), (1,2,3), 4)
7+
@test p == ((1, 2), (3, 4), (5, 6), (0, 0))
8+
9+
@test_throws ArgumentError NNlib.gen_pad((1,2,3,4,5,), (1,2,3), 4)
10+
11+
p = NNlib.gen_pad((1,3), (1,3), 4)
12+
@test p == ((1, 1), (0, 0), (3, 3), (0, 0))
13+
14+
p = NNlib.gen_pad(1, (1,2,3), 4)
15+
@test p == ((1, 1), (1, 1), (1, 1), (0, 0))
16+
17+
p = NNlib.gen_pad(3, :, 2)
18+
@test p == ((3, 3), (3, 3))
19+
20+
y = pad_constant(x, (3, 2, 4))
21+
@test size(y) == (8, 6, 10)
22+
@test y[4:5, 3:4, 5:6] x
23+
y[4:5, 3:4, 5:6] .= 0
24+
@test all(y .== 0)
25+
26+
@test pad_constant(x, (3, 2, 4)) pad_zeros(x, (3, 2, 4))
27+
@test pad_zeros(x, 2) pad_zeros(x, (2,2,2))
28+
29+
y = pad_constant(x, (3, 2, 4, 5), 1.2, dims = (1,3))
30+
@test size(y) == (7, 2, 11)
31+
@test y[4:5, 1:2, 5:6] x
32+
y[4:5, 1:2, 5:6] .= 1.2
33+
@test all(y .== 1.2)
34+
35+
@test pad_constant(x, (2,2,2,2), 1.2, dims = (1,3))
36+
pad_constant(x, 2, 1.2, dims = (1,3))
37+
38+
@test pad_constant(x, 1, dims = 1:2) ==
39+
pad_constant(x, 1, dims = (1,2))
40+
41+
@test size(pad_constant(x, 1, dims = 1)) == (4,2,2)
42+
43+
@test all(pad_zeros(randn(2), (1, 2))[[1, 4, 5]] .== 0)
44+
45+
gradtest(x -> pad_constant(x, 2), rand(2,2,2))
46+
gradtest(x -> pad_constant(x, (2, 1, 1, 2)), rand(2,2))
47+
gradtest(x -> pad_constant(x, (2, 1,)), rand(2))
4848
end
4949

5050
@testset "padding repeat" begin
51-
x = rand(2, 2, 2)
52-
53-
# y = @inferred pad_repeat(x, (3, 2, 4, 5))
54-
y = pad_repeat(x, (3, 2, 4, 5))
55-
@test size(y) == (7, 11, 2)
56-
@test y[4:5, 5:6, :] x
57-
58-
# y = @inferred pad_repeat(x, (3, 2, 4, 5), dims=(1,3))
59-
y = pad_repeat(x, (3, 2, 4, 5), dims=(1,3))
60-
@test size(y) == (7, 2, 11)
61-
@test y[4:5, :, 5:6] x
62-
63-
@test pad_repeat(reshape(1:9, 3, 3), (1,2)) ==
51+
x = rand(2, 2, 2)
52+
53+
# y = @inferred pad_repeat(x, (3, 2, 4, 5))
54+
y = pad_repeat(x, (3, 2, 4, 5))
55+
@test size(y) == (7, 11, 2)
56+
@test y[4:5, 5:6, :] x
57+
58+
# y = @inferred pad_repeat(x, (3, 2, 4, 5), dims=(1,3))
59+
y = pad_repeat(x, (3, 2, 4, 5), dims=(1,3))
60+
@test size(y) == (7, 2, 11)
61+
@test y[4:5, :, 5:6] x
62+
63+
@test pad_repeat(reshape(1:9, 3, 3), (1,2)) ==
6464
[1 4 7
65-
1 4 7
66-
2 5 8
67-
3 6 9
68-
3 6 9
69-
3 6 9]
70-
71-
@test pad_repeat(reshape(1:9, 3, 3), (2,2), dims=2) ==
72-
[1 1 1 4 7 7 7
73-
2 2 2 5 8 8 8
74-
3 3 3 6 9 9 9]
75-
76-
@test pad_repeat(x, (2, 2, 2, 2), dims=(1,3))
77-
pad_repeat(x, 2, dims=(1,3))
78-
79-
gradtest(x -> pad_repeat(x, (2,2,2,2)), rand(2,2,2))
65+
1 4 7
66+
2 5 8
67+
3 6 9
68+
3 6 9
69+
3 6 9]
70+
71+
@test pad_repeat(reshape(1:9, 3, 3), (2,2), dims=2) ==
72+
[1 1 1 4 7 7 7
73+
2 2 2 5 8 8 8
74+
3 3 3 6 9 9 9]
75+
76+
@test pad_repeat(x, (2, 2, 2, 2), dims=(1,3))
77+
pad_repeat(x, 2, dims=(1,3))
78+
79+
gradtest(x -> pad_repeat(x, (2,2,2,2)), rand(2,2,2))
8080
end
8181

8282
@testset "padding reflect" begin
83-
y = pad_reflect(reshape(1:9, 3, 3), (2,2), dims=2)
84-
@test y == [ 7 4 1 4 7 4 1
83+
y = pad_reflect(reshape(1:9, 3, 3), (2,2), dims=2)
84+
@test y == [7 4 1 4 7 4 1
8585
8 5 2 5 8 5 2
8686
9 6 3 6 9 6 3]
87-
88-
y = pad_reflect(reshape(1:9, 3, 3), (2,2,2,2))
89-
@test y == [9 6 3 6 9 6 3
87+
88+
y = pad_reflect(reshape(1:9, 3, 3), (2,2,2,2))
89+
@test y == [9 6 3 6 9 6 3
9090
8 5 2 5 8 5 2
9191
7 4 1 4 7 4 1
9292
8 5 2 5 8 5 2
9393
9 6 3 6 9 6 3
9494
8 5 2 5 8 5 2
9595
7 4 1 4 7 4 1]
96-
97-
x = rand(4, 4, 4)
98-
99-
@test pad_reflect(x, (2, 2, 2, 2), dims=(1,3))
100-
pad_reflect(x, 2, dims=(1,3))
101-
102-
# pad_reflect needs larger test input as padding must
103-
# be strictly less than array size in that dimension
104-
gradtest(x -> pad_reflect(x, (2,2,2,2)), rand(3,3,3))
96+
97+
x = rand(4, 4, 4)
98+
@test pad_reflect(x, (2, 2, 2, 2), dims=(1,3))
99+
pad_reflect(x, 2, dims=(1,3))
100+
101+
# pad_reflect needs larger test input as padding must
102+
# be strictly less than array size in that dimension
103+
gradtest(x -> pad_reflect(x, (2,2,2,2)), rand(3,3,3))
105104
end
106105

107106
@testset "padding symmetric" begin
108-
y = pad_symmetric(reshape(1:9, 3, 3), (2,2), dims=2)
109-
@test y == [ 4 1 1 4 7 7 4
107+
y = pad_symmetric(reshape(1:9, 3, 3), (2,2), dims=2)
108+
@test y == [4 1 1 4 7 7 4
110109
5 2 2 5 8 8 5
111110
6 3 3 6 9 9 6]
112-
113-
y = pad_symmetric(reshape(1:9, 3, 3), (2,2,2,2))
114-
@test y == [5 2 2 5 8 8 5
111+
112+
y = pad_symmetric(reshape(1:9, 3, 3), (2,2,2,2))
113+
@test y == [5 2 2 5 8 8 5
115114
4 1 1 4 7 7 4
116115
4 1 1 4 7 7 4
117116
5 2 2 5 8 8 5
118117
6 3 3 6 9 9 6
119118
6 3 3 6 9 9 6
120119
5 2 2 5 8 8 5]
121-
122-
x = rand(4, 4, 4)
123-
124-
@test pad_symmetric(x, (2, 2, 2, 2), dims=(1,3))
125-
pad_symmetric(x, 2, dims=(1,3))
126-
127-
gradtest(x -> pad_symmetric(x, (2,2,2,2)), rand(2,2,2))
120+
121+
x = rand(4, 4, 4)
122+
@test pad_symmetric(x, (2, 2, 2, 2), dims=(1,3))
123+
pad_symmetric(x, 2, dims=(1,3))
124+
125+
gradtest(x -> pad_symmetric(x, (2,2,2,2)), rand(2,2,2))
128126
end
129127

130128
@testset "padding circular" begin
131-
y = pad_circular(reshape(1:9, 3, 3), (2,2), dims=2)
132-
@test y == [ 4 7 1 4 7 1 4
129+
y = pad_circular(reshape(1:9, 3, 3), (2,2), dims=2)
130+
@test y == [4 7 1 4 7 1 4
133131
5 8 2 5 8 2 5
134132
6 9 3 6 9 3 6]
135-
136-
y = pad_circular(reshape(1:9, 3, 3), (2,2,2,2))
137-
@test y == [5 8 2 5 8 2 5
133+
134+
y = pad_circular(reshape(1:9, 3, 3), (2,2,2,2))
135+
@test y == [5 8 2 5 8 2 5
138136
6 9 3 6 9 3 6
139137
4 7 1 4 7 1 4
140138
5 8 2 5 8 2 5
141139
6 9 3 6 9 3 6
142140
4 7 1 4 7 1 4
143141
5 8 2 5 8 2 5]
144-
145-
x = rand(4, 4, 4)
146-
147-
@test pad_circular(x, (2, 2, 2, 2), dims=(1,3))
148-
pad_circular(x, 2, dims=(1,3))
149-
150-
gradtest(x -> pad_circular(x, (2,2,2,2)), rand(2,2,2))
142+
143+
x = rand(4, 4, 4)
144+
@test pad_circular(x, (2, 2, 2, 2), dims=(1,3))
145+
pad_circular(x, 2, dims=(1,3))
146+
147+
gradtest(x -> pad_circular(x, (2,2,2,2)), rand(2,2,2))
151148
end

0 commit comments

Comments
 (0)