Skip to content

Commit 27aa735

Browse files
authored
Move tests to TestItems (#55)
* Move tests to TestItems * Fix test compat issues * Improves doc * Update CI. New version.
1 parent 3803fe2 commit 27aa735

File tree

14 files changed

+274
-202
lines changed

14 files changed

+274
-202
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- os: windows-latest
3737
arch: x86
3838
steps:
39-
- uses: actions/checkout@v2
39+
- uses: actions/checkout@v3
4040
- uses: julia-actions/setup-julia@v1
4141
with:
4242
version: ${{ matrix.version }}
@@ -63,7 +63,7 @@ jobs:
6363
name: Documentation
6464
runs-on: ubuntu-latest
6565
steps:
66-
- uses: actions/checkout@v2
66+
- uses: actions/checkout@v3
6767
- uses: julia-actions/setup-julia@v1
6868
with:
6969
version: "1"

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ _git2_*
77
.gitignore
88

99
*Manifest.toml
10-
test_dummy.jl
1110
.vscode/*

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CompositionalNetworks"
22
uuid = "4b67e4b5-442d-4ef5-b760-3f5df3a57537"
33
authors = ["Jean-François Baffier"]
4-
version = "0.5.3"
4+
version = "0.5.4"
55

66
[deps]
77
ConstraintCommons = "e37357d9-0691-492f-a822-e5ea6a920954"
@@ -11,6 +11,8 @@ Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
1111
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
1212
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
1313
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
14+
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
15+
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"
1416
ThreadSafeDicts = "4239201d-c60e-5e0a-9702-85d713665ba7"
1517
Unrolled = "9602ed7d-8fef-5bc8-8597-8f21381861e8"
1618

@@ -21,6 +23,8 @@ Dictionaries = "0.3"
2123
Distances = "0.10"
2224
JuliaFormatter = "0.22, 1"
2325
OrderedCollections = "1"
26+
TestItemRunner = "0.2"
27+
TestItems = "0.1"
2428
ThreadSafeDicts = "0.1"
2529
Unrolled = "0.1"
2630
julia = "1.6"

src/CompositionalNetworks.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Distances
88
using JuliaFormatter
99
using OrderedCollections
1010
using Random
11+
using TestItemRunner
12+
using TestItems
1113
using ThreadSafeDicts
1214
using Unrolled
1315

@@ -47,10 +49,10 @@ include("metrics.jl")
4749

4850
# Includes layers
4951
include("layer.jl")
50-
include("transformation.jl")
51-
include("arithmetic.jl")
52-
include("aggregation.jl")
53-
include("comparison.jl")
52+
include("layers/transformation.jl")
53+
include("layers/arithmetic.jl")
54+
include("layers/aggregation.jl")
55+
include("layers/comparison.jl")
5456

5557
# Include ICN
5658
include("icn.jl")

src/composition.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
1+
"""
2+
struct Composition{F<:Function}
3+
4+
Store the all the information of a composition learned by an ICN.
5+
"""
16
struct Composition{F<:Function}
27
code::Dict{Symbol,String}
38
f::F
49
symbols::Vector{Vector{Symbol}}
510
end
611

12+
"""
13+
Composition(f::F, symbols) where {F<:Function}
14+
15+
Construct a `Composition`.
16+
"""
717
function Composition(f::F, symbols) where {F<:Function}
818
code = Dict{Symbol,String}()
919
return Composition{F}(code, f, symbols)
1020
end
1121

22+
"""
23+
code(c::Composition, lang=:maths; name="composition")
24+
25+
Access the code of a composition `c` in a given language `lang`. The name of the generated method is optional.
26+
"""
1227
function code(c::Composition, lang=:maths; name="composition")
1328
return get!(c.code, lang, generate(c, name, Val(lang)))
1429
end
30+
31+
"""
32+
composition(c::Composition)
33+
34+
Access the actual method of an ICN composition `c`.
35+
"""
1536
composition(c::Composition) = c.f
37+
38+
"""
39+
symbols(c::Composition)
40+
41+
Output the composition as a layered collection of `Symbol`s.
42+
"""
1643
symbols(c::Composition) = c.symbols
1744

1845
"""
@@ -25,6 +52,11 @@ function compose(icn::ICN, weigths::BitVector=BitVector())
2552
return Composition(composition, symbols)
2653
end
2754

55+
"""
56+
generate(c::Composition, name, lang)
57+
58+
Generates the code of `c` in a specific language `lang`.
59+
"""
2860
function generate(c::Composition, name, ::Val{:maths})
2961
aux = map(s -> reduce_symbols(s, ", ", length(s) > 1), symbols(c))
3062
def = reduce_symbols(aux, "", false)
@@ -62,6 +94,11 @@ function generate(c::Composition, name, ::Val{:Julia})
6294
return documentation * format_text(output, BlueStyle(); pipe_to_function_call=false)
6395
end
6496

97+
"""
98+
composition_to_file!(c::Composition, path, name, language=:Julia)
99+
100+
Write the composition code in a given `language` into a file at `path`.
101+
"""
65102
function composition_to_file!(c::Composition, path, name, language=:Julia)
66103
output = code(c, language; name)
67104
write(path, output)

src/aggregation.jl renamed to src/layers/aggregation.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,20 @@ function aggregation_layer()
2222

2323
return Layer(true, aggregations, Vector{Symbol}())
2424
end
25+
26+
## SECTION - Test Items
27+
@testitem "Aggregation Layer" tags = [:aggregation, :layer] begin
28+
CN = CompositionalNetworks
29+
30+
data = [
31+
[1, 5, 2, 4, 3] => 2,
32+
[1, 2, 3, 2, 1] => 2,
33+
]
34+
35+
@test CN.ag_sum(data[1].first) == 15
36+
@test CN.ag_sum(data[2].first) == 9
37+
38+
@test CN.ag_count_positive(data[1].first) == 5
39+
@test CN.ag_count_positive(data[2].first) == 5
40+
@test CN.ag_count_positive([1, 0, 1, 0, 1]) == 3
41+
end

src/arithmetic.jl renamed to src/layers/arithmetic.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,17 @@ function arithmetic_layer()
2222

2323
return Layer(true, arithmetics, Vector{Symbol}())
2424
end
25+
26+
## SECTION - Test Items
27+
@testitem "Arithmetic Layer" tags = [:arithmetic, :layer] begin
28+
CN = CompositionalNetworks
29+
30+
data = [
31+
[1, 5, 2, 4, 3] => 2,
32+
[1, 2, 3, 2, 1] => 2,
33+
]
34+
35+
@test CN.ar_sum(map(p -> p.first, data)) == [2, 7, 5, 6, 4]
36+
@test CN.ar_prod(map(p -> p.first, data)) == [1, 10, 6, 8, 3]
37+
38+
end

src/comparison.jl renamed to src/layers/comparison.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,66 @@ function comparison_layer(parameters = Vector{Symbol}())
9494

9595
return Layer(true, comparisons, parameters)
9696
end
97+
98+
## SECTION - Test Items
99+
@testitem "Comparison Layer" tags = [:comparison, :layer] begin
100+
CN = CompositionalNetworks
101+
102+
data = [3 => (1, 5), 5 => (10, 5)]
103+
104+
funcs = [
105+
CN.co_identity => [3, 5],
106+
]
107+
108+
# test no param/vars
109+
for (f, results) in funcs
110+
for (key, vals) in enumerate(data)
111+
@test f(vals.first) == results[key]
112+
end
113+
end
114+
115+
funcs_param = [
116+
CN.co_abs_diff_val_param => [2, 5],
117+
CN.co_val_minus_param => [2, 0],
118+
CN.co_param_minus_val => [0, 5],
119+
]
120+
121+
for (f, results) in funcs_param
122+
for (key, vals) in enumerate(data)
123+
@test f(vals.first; param=vals.second[1]) == results[key]
124+
end
125+
end
126+
127+
funcs_vars = [
128+
CN.co_abs_diff_val_vars => [2, 0],
129+
CN.co_val_minus_vars => [0, 0],
130+
CN.co_vars_minus_val => [2, 0],
131+
]
132+
133+
for (f, results) in funcs_vars
134+
for (key, vals) in enumerate(data)
135+
@test f(vals.first, nvars=vals.second[2]) == results[key]
136+
end
137+
end
138+
139+
funcs_param_dom = [
140+
CN.co_euclidian_param => [1.4, 2.0],
141+
]
142+
143+
for (f, results) in funcs_param_dom
144+
for (key, vals) in enumerate(data)
145+
@test f(vals.first, param=vals.second[1], dom_size=vals.second[2]) results[key]
146+
end
147+
end
148+
149+
funcs_dom = [
150+
CN.co_euclidian => [1.6, 2.0],
151+
]
152+
153+
for (f, results) in funcs_dom
154+
for (key, vals) in enumerate(data)
155+
@test f(vals.first, dom_size=vals.second[2]) results[key]
156+
end
157+
end
158+
159+
end

src/transformation.jl renamed to src/layers/transformation.jl

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,110 @@ function transformation_layer(parameters = Vector{Symbol}())
262262

263263
return Layer(false, transformations, parameters)
264264
end
265+
266+
## SECTION - Test Items
267+
@testitem "Arithmetic Layer" tags = [:arithmetic, :layer] begin
268+
CN = CompositionalNetworks
269+
270+
data = [
271+
[1, 5, 2, 4, 3] => 2,
272+
[1, 2, 3, 2, 1] => 2,
273+
]
274+
275+
# Test transformations without parameters
276+
funcs = Dict(
277+
CN.tr_identity => [
278+
data[1].first,
279+
data[2].first,
280+
],
281+
CN.tr_count_eq => [
282+
[0, 0, 0, 0, 0],
283+
[1, 1, 0, 1, 1],
284+
],
285+
CN.tr_count_eq_right => [
286+
[0, 0, 0, 0, 0],
287+
[1, 1, 0, 0, 0],
288+
],
289+
CN.tr_count_eq_left => [
290+
[0, 0, 0, 0, 0],
291+
[0, 0, 0, 1, 1],
292+
],
293+
CN.tr_count_greater => [
294+
[4, 0, 3, 1, 2],
295+
[3, 1, 0, 1, 3],
296+
],
297+
CN.tr_count_lesser => [
298+
[0, 4, 1, 3, 2],
299+
[0, 2, 4, 2, 0],
300+
],
301+
CN.tr_count_g_left => [
302+
[0, 0, 1, 1, 2],
303+
[0, 0, 0, 1, 3],
304+
],
305+
CN.tr_count_l_left => [
306+
[0, 1, 1, 2, 2],
307+
[0, 1, 2, 1, 0],
308+
],
309+
CN.tr_count_g_right => [
310+
[4, 0, 2, 0, 0],
311+
[3, 1, 0, 0, 0],
312+
],
313+
CN.tr_count_l_right => [
314+
[0, 3, 0, 1, 0],
315+
[0, 1, 2, 1, 0],
316+
],
317+
CN.tr_contiguous_vals_minus => [
318+
[0, 3, 0, 1, 0],
319+
[0, 0, 1, 1, 0],
320+
],
321+
CN.tr_contiguous_vals_minus_rev => [
322+
[4, 0, 2, 0, 0],
323+
[1, 1, 0, 0, 0],
324+
],
325+
)
326+
327+
for (f, results) in funcs
328+
@info f
329+
for (key, vals) in enumerate(data)
330+
@test f(vals.first) == results[key]
331+
foreach(i -> f(i, vals.first), vals.first)
332+
end
333+
end
334+
335+
# Test transformations with parameter
336+
funcs_param = Dict(
337+
CN.tr_count_eq_param => [
338+
[1, 0, 1, 0, 1],
339+
[1, 0, 0, 0, 1],
340+
],
341+
CN.tr_count_l_param => [
342+
[2, 5, 3, 5, 4],
343+
[4, 5, 5, 5, 4],
344+
],
345+
CN.tr_count_g_param => [
346+
[2, 0, 1, 0, 0],
347+
[0, 0, 0, 0, 0],
348+
],
349+
CN.tr_count_bounding_param => [
350+
[3, 1, 3, 2, 3],
351+
[5, 3, 1, 3, 5],
352+
],
353+
CN.tr_val_minus_param => [
354+
[0, 3, 0, 2, 1],
355+
[0, 0, 1, 0, 0],
356+
],
357+
CN.tr_param_minus_val => [
358+
[1, 0, 0, 0, 0],
359+
[1, 0, 0, 0, 1],
360+
],
361+
)
362+
363+
for (f, results) in funcs_param
364+
@info f
365+
for (key, vals) in enumerate(data)
366+
@test f(vals.first; param=vals.second) == results[key]
367+
foreach(i -> f(i, vals.first; param=vals.second), vals.first)
368+
end
369+
end
370+
371+
end

src/utils.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ function reduce_symbols(symbols, sep, parenthesis=true; prefix="")
7777
return parenthesis ? "[$str]" : str
7878
end
7979

80+
"""
81+
tr_in(tr, X, x, param)
82+
83+
Application of an operation from the transformation layer. Used to generate more efficient code for all compositions.
84+
"""
8085
@unroll function tr_in(tr, X, x, param)
8186
@unroll for i in 1:length(tr)
8287
tr[i](x, @view(X[:, i]); param)

0 commit comments

Comments
 (0)