From f1c39de85604ec0987738c6f5d4a8631ea5fa486 Mon Sep 17 00:00:00 2001 From: Jean-Francois Baffier Date: Mon, 17 Jun 2024 21:47:57 +0900 Subject: [PATCH 1/3] Extend the parametric keywords to include anything (#66) * Extend the parametric keywords to include anything * Fix spelling an format --- src/composition.jl | 8 +- src/icn.jl | 11 +-- src/layer.jl | 2 +- src/layers/comparison.jl | 80 ++++++++--------- src/layers/transformation.jl | 166 +++++++++++++++++------------------ src/utils.jl | 46 ++++++---- test/icn.jl | 8 +- 7 files changed, 160 insertions(+), 161 deletions(-) diff --git a/src/composition.jl b/src/composition.jl index aa00907..2a27ef2 100644 --- a/src/composition.jl +++ b/src/composition.jl @@ -75,7 +75,7 @@ function generate(c::Composition, name, ::Val{:Julia}) co = reduce_symbols(symbs[4], ", ", false; prefix = CN * "co_") documentation = """\"\"\" - $name(x; X = zeros(length(x), $tr_length), param=nothing, dom_size) + $name(x; X = zeros(length(x), $tr_length), params...) Composition `$name` generated by CompositionalNetworks.jl. ``` @@ -85,10 +85,10 @@ function generate(c::Composition, name, ::Val{:Julia}) """ output = """ - function $name(x; X = zeros(length(x), $tr_length), param=nothing, dom_size) - $(CN)tr_in(Tuple($tr), X, x, param) + function $name(x; X = zeros(length(x), $tr_length), dom_size, params...) + $(CN)tr_in(Tuple($tr), X, x; params) X[1:length(x), 1] .= 1:length(x) .|> (i -> $ar(@view X[i, 1:$tr_length])) - return $ag(@view X[:, 1]) |> (y -> $co(y; param, dom_size, nvars=length(x))) + return $ag(@view X[:, 1]) |> (y -> $co(y; dom_size, nvars=length(x), params...)) end """ return documentation * format_text(output, BlueStyle(); pipe_to_function_call = false) diff --git a/src/icn.jl b/src/icn.jl index 1831eb6..a0047eb 100644 --- a/src/icn.jl +++ b/src/icn.jl @@ -151,16 +151,11 @@ function _compose(icn::ICN) end end - function composition( - x; - X = zeros(length(x), length(funcs[1])), - param = nothing, - dom_size, - ) - tr_in(Tuple(funcs[1]), X, x, param) + function composition(x; X = zeros(length(x), length(funcs[1])), dom_size, params...) + tr_in(Tuple(funcs[1]), X, x; params...) X[1:length(x), 1] .= 1:length(x) .|> (i -> funcs[2][1](@view X[i, 1:length(funcs[1])])) - return (y -> funcs[4][1](y; param, dom_size, nvars = length(x)))( + return (y -> funcs[4][1](y; dom_size, nvars = length(x), params...))( funcs[3][1](@view X[:, 1]), ) end diff --git a/src/layer.jl b/src/layer.jl index 98da34f..53646f0 100644 --- a/src/layer.jl +++ b/src/layer.jl @@ -74,7 +74,7 @@ end """ generate_exclusive_operation(max_op_number) -Generates the operations (weigths) of a layer with exclusive operations. +Generates the operations (weights) of a layer with exclusive operations. """ function generate_exclusive_operation(max_op_number) op = rand(1:max_op_number) diff --git a/src/layers/comparison.jl b/src/layers/comparison.jl index 4b70983..78bf860 100644 --- a/src/layers/comparison.jl +++ b/src/layers/comparison.jl @@ -2,61 +2,59 @@ co_identity(x) Identity function. Already defined in Julia as `identity`, specialized for scalars in the `comparison` layer. """ -co_identity(x; param = nothing, dom_size = 0, nvars = 0) = identity(x) +co_identity(x; params...) = identity(x) """ - co_abs_diff_val_param(x; param) -Return the absolute difference between `x` and `param`. + co_abs_diff_var_val(x; val) +Return the absolute difference between `x` and `val`. """ -co_abs_diff_val_param(x; param, dom_size = 0, nvars = 0) = abs(x - param) +co_abs_diff_var_val(x; val, params...) = abs(x - val) """ - co_val_minus_param(x; param) -Return the difference `x - param` if positive, `0.0` otherwise. + co_var_minus_val(x; val) +Return the difference `x - val` if positive, `0.0` otherwise. """ -co_val_minus_param(x; param, dom_size = 0, nvars = 0) = max(0.0, x - param) +co_var_minus_val(x; val, params...) = max(0.0, x - val) """ - co_param_minus_val(x; param) -Return the difference `param - x` if positive, `0.0` otherwise. + co_val_minus_var(x; val) +Return the difference `val - x` if positive, `0.0` otherwise. """ -co_param_minus_val(x; param, dom_size = 0, nvars = 0) = max(0.0, param - x) +co_val_minus_var(x; val, params...) = max(0.0, val - x) """ - co_euclidean_param(x; param, dom_size) -Compute an euclidean norm with domain size `dom_size`, weighted by `param`, of a scalar. + co_euclidean_val(x; val, dom_size) +Compute an euclidean norm with domain size `dom_size`, weighted by `val`, of a scalar. """ -function co_euclidean_param(x; param, dom_size, nvars = 0) - return x == param ? 0.0 : (1.0 + abs(x - param) / dom_size) +function co_euclidean_val(x; val, dom_size, params...) + return x == val ? 0.0 : (1.0 + abs(x - val) / dom_size) end """ co_euclidean(x; dom_size) Compute an euclidean norm with domain size `dom_size` of a scalar. """ -function co_euclidean(x; param = nothing, dom_size, nvars = 0) - return co_euclidean_param(x; param = 0.0, dom_size = dom_size) +function co_euclidean(x; dom_size, params...) + return co_euclidean_val(x; val = 0.0, dom_size) end """ - co_abs_diff_val_vars(x; nvars) + co_abs_diff_var_vars(x; nvars) Return the absolute difference between `x` and the number of variables `nvars`. """ -co_abs_diff_val_vars(x; param = nothing, dom_size = 0, nvars) = abs(x - nvars) +co_abs_diff_var_vars(x; nvars, params...) = abs(x - nvars) """ - co_val_minus_vars(x; nvars) + co_var_minus_vars(x; nvars) Return the difference `x - nvars` if positive, `0.0` otherwise, where `nvars` denotes the numbers of variables. """ -co_val_minus_vars(x; param = nothing, dom_size = 0, nvars) = - co_val_minus_param(x; param = nvars) +co_var_minus_vars(x; nvars, params...) = co_var_minus_val(x; val = nvars) """ - co_vars_minus_val(x; nvars) + co_vars_minus_var(x; nvars) Return the difference `nvars - x` if positive, `0.0` otherwise, where `nvars` denotes the numbers of variables. """ -co_vars_minus_val(x; param = nothing, dom_size = 0, nvars) = - co_param_minus_val(x; param = nvars) +co_vars_minus_var(x; nvars, params...) = co_val_minus_var(x; val = nvars) # Parametric layers @@ -66,18 +64,18 @@ function make_comparisons(::Val{:none}) return LittleDict{Symbol,Function}( :identity => co_identity, :euclidean => co_euclidean, - :abs_diff_val_vars => co_abs_diff_val_vars, - :val_minus_vars => co_val_minus_vars, - :vars_minus_val => co_vars_minus_val, + :abs_diff_var_vars => co_abs_diff_var_vars, + :var_minus_vars => co_var_minus_vars, + :vars_minus_var => co_vars_minus_var, ) end function make_comparisons(::Val{:val}) return LittleDict{Symbol,Function}( - :abs_diff_val_param => co_abs_diff_val_param, - :val_minus_param => co_val_minus_param, - :param_minus_val => co_param_minus_val, - :euclidean_param => co_euclidean_param, + :abs_diff_var_val => co_abs_diff_var_val, + :var_minus_val => co_var_minus_val, + :val_minus_var => co_val_minus_var, + :euclidean_val => co_euclidean_val, ) end @@ -113,21 +111,21 @@ end end funcs_param = [ - CN.co_abs_diff_val_param => [2, 5], - CN.co_val_minus_param => [2, 0], - CN.co_param_minus_val => [0, 5], + CN.co_abs_diff_var_val => [2, 5], + CN.co_var_minus_val => [2, 0], + CN.co_val_minus_var => [0, 5], ] for (f, results) in funcs_param for (key, vals) in enumerate(data) - @test f(vals.first; param = vals.second[1]) == results[key] + @test f(vals.first; val = vals.second[1]) == results[key] end end funcs_vars = [ - CN.co_abs_diff_val_vars => [2, 0], - CN.co_val_minus_vars => [0, 0], - CN.co_vars_minus_val => [2, 0], + CN.co_abs_diff_var_vars => [2, 0], + CN.co_var_minus_vars => [0, 0], + CN.co_vars_minus_var => [2, 0], ] for (f, results) in funcs_vars @@ -136,11 +134,11 @@ end end end - funcs_param_dom = [CN.co_euclidean_param => [1.4, 2.0]] + funcs_val_dom = [CN.co_euclidean_val => [1.4, 2.0]] - for (f, results) in funcs_param_dom + for (f, results) in funcs_val_dom for (key, vals) in enumerate(data) - @test f(vals.first, param = vals.second[1], dom_size = vals.second[2]) ≈ + @test f(vals.first, val = vals.second[1], dom_size = vals.second[2]) ≈ results[key] end end diff --git a/src/layers/transformation.jl b/src/layers/transformation.jl index cd46261..b9f33d6 100644 --- a/src/layers/transformation.jl +++ b/src/layers/transformation.jl @@ -8,7 +8,7 @@ Identity function. Already defined in Julia as `identity`, specialized for vectors. When `X` is provided, the result is computed without allocations. """ -tr_identity(i, x) = identity(x[i]) +tr_identity(i, x; params...) = identity(x[i]) lazy(tr_identity) # Count equalities @@ -21,7 +21,7 @@ lazy(tr_identity) Count the number of elements equal to `x[i]`. Extended method to vector with sig `(x)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_eq(i, x) = count(y -> x[i] == y, x) - 1 +tr_count_eq(i, x; params...) = count(y -> x[i] == y, x) - 1 """ tr_count_eq_right(i, x) @@ -31,7 +31,7 @@ tr_count_eq(i, x) = count(y -> x[i] == y, x) - 1 Count the number of elements to the right of and equal to `x[i]`. Extended method to vector with sig `(x)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_eq_right(i, x) = tr_count_eq(1, @view x[i:end]) +tr_count_eq_right(i, x; params...) = tr_count_eq(1, @view x[i:end]) """ tr_count_eq_left(i, x) @@ -41,7 +41,7 @@ tr_count_eq_right(i, x) = tr_count_eq(1, @view x[i:end]) Count the number of elements to the left of and equal to `x[i]`. Extended method to vector with sig `(x)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_eq_left(i, x) = tr_count_eq(i, @view x[1:i]) +tr_count_eq_left(i, x; params...) = tr_count_eq(i, @view x[1:i]) # Generating vetorized versions lazy(tr_count_eq, tr_count_eq_left, tr_count_eq_right) @@ -56,7 +56,7 @@ lazy(tr_count_eq, tr_count_eq_left, tr_count_eq_right) Count the number of elements greater than `x[i]`. Extended method to vector with sig `(x)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_greater(i, x) = count(y -> x[i] < y, x) +tr_count_greater(i, x; params...) = count(y -> x[i] < y, x) """ tr_count_lesser(i, x) @@ -66,7 +66,7 @@ tr_count_greater(i, x) = count(y -> x[i] < y, x) Count the number of elements lesser than `x[i]`. Extended method to vector with sig `(x)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_lesser(i, x) = count(y -> x[i] > y, x) +tr_count_lesser(i, x; params...) = count(y -> x[i] > y, x) """ tr_count_g_left(i, x) @@ -76,7 +76,7 @@ tr_count_lesser(i, x) = count(y -> x[i] > y, x) Count the number of elements to the left of and greater than `x[i]`. Extended method to vector with sig `(x)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_g_left(i, x) = tr_count_greater(i, @view x[1:i]) +tr_count_g_left(i, x; params...) = tr_count_greater(i, @view x[1:i]) """ tr_count_l_left(i, x) @@ -86,7 +86,7 @@ tr_count_g_left(i, x) = tr_count_greater(i, @view x[1:i]) Count the number of elements to the left of and lesser than `x[i]`. Extended method to vector with sig `(x)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_l_left(i, x) = tr_count_lesser(i, @view x[1:i]) +tr_count_l_left(i, x; params...) = tr_count_lesser(i, @view x[1:i]) """ tr_count_g_right(i, x) @@ -95,7 +95,7 @@ tr_count_l_left(i, x) = tr_count_lesser(i, @view x[1:i]) Count the number of elements to the right of and greater than `x[i]`. Extended method to vector with sig `(x)` are generated. """ -tr_count_g_right(i, x) = tr_count_greater(1, @view x[i:end]) +tr_count_g_right(i, x; params...) = tr_count_greater(1, @view x[i:end]) """ tr_count_l_right(i, x) @@ -105,113 +105,113 @@ tr_count_g_right(i, x) = tr_count_greater(1, @view x[i:end]) Count the number of elements to the right of and lesser than `x[i]`. Extended method to vector with sig `(x)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_l_right(i, x) = tr_count_lesser(1, @view x[i:end]) +tr_count_l_right(i, x; params...) = tr_count_lesser(1, @view x[i:end]) # Generating vetorized versions lazy(tr_count_greater, tr_count_g_left, tr_count_g_right) lazy(tr_count_lesser, tr_count_l_left, tr_count_l_right) -# Count param +# Count val parameter """ - tr_count_eq_param(i, x; param) - tr_count_eq_param(x; param) - tr_count_eq_param(x, X::AbstractVector; param) + tr_count_eq_val(i, x; val) + tr_count_eq_val(x; val) + tr_count_eq_val(x, X::AbstractVector; val) -Count the number of elements equal to `x[i] + param`. Extended method to vector with sig `(x, param)` are generated. +Count the number of elements equal to `x[i] + val`. Extended method to vector with sig `(x, val)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_eq_param(i, x; param) = count(y -> y == x[i] + param, x) +tr_count_eq_val(i, x; val, params...) = count(y -> y == x[i] + val, x) """ - tr_count_l_param(i, x; param) - tr_count_l_param(x; param) - tr_count_l_param(x, X::AbstractVector; param) + tr_count_l_val(i, x; val) + tr_count_l_val(x; val) + tr_count_l_val(x, X::AbstractVector; val) -Count the number of elements lesser than `x[i] + param`. Extended method to vector with sig `(x, param)` are generated. +Count the number of elements lesser than `x[i] + val`. Extended method to vector with sig `(x, val)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_l_param(i, x; param) = count(y -> y < x[i] + param, x) +tr_count_l_val(i, x; val, params...) = count(y -> y < x[i] + val, x) """ - tr_count_g_param(i, x; param) - tr_count_g_param(x; param) - tr_count_g_param(x, X::AbstractVector; param) + tr_count_g_val(i, x; val) + tr_count_g_val(x; val) + tr_count_g_val(x, X::AbstractVector; val) -Count the number of elements greater than `x[i] + param`. Extended method to vector with sig `(x, param)` are generated. +Count the number of elements greater than `x[i] + val`. Extended method to vector with sig `(x, val)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_count_g_param(i, x; param) = count(y -> y > x[i] + param, x) +tr_count_g_val(i, x; val, params...) = count(y -> y > x[i] + val, x) -# Generating vetorized versions -lazy_param(tr_count_eq_param, tr_count_l_param, tr_count_g_param) +# Generating vectorized versions +lazy_param(tr_count_eq_val, tr_count_l_val, tr_count_g_val) -# Bounding param +# Bounding val parameter """ - tr_count_bounding_param(i, x; param) - tr_count_bounding_param(x; param) - tr_count_bounding_param(x, X::AbstractVector; param) + tr_count_bounding_val(i, x; val) + tr_count_bounding_val(x; val) + tr_count_bounding_val(x, X::AbstractVector; val) -Count the number of elements bounded (not strictly) by `x[i]` and `x[i] + param`. An extended method to vector with sig `(x, param)` is generated. +Count the number of elements bounded (not strictly) by `x[i]` and `x[i] + val`. An extended method to vector with sig `(x, val)` is generated. When `X` is provided, the result is computed without allocations. """ -tr_count_bounding_param(i, x; param) = count(y -> x[i] ≤ y ≤ x[i] + param, x) +tr_count_bounding_val(i, x; val, params...) = count(y -> x[i] ≤ y ≤ x[i] + val, x) -# Generating vetorized versions -lazy_param(tr_count_bounding_param) +# Generating vectorized versions +lazy_param(tr_count_bounding_val) -# Val/param subtractions +# Variable/parameter values subtractions """ - tr_val_minus_param(i, x; param) - tr_val_minus_param(x; param) - tr_val_minus_param(x, X::AbstractVector; param) + tr_var_minus_val(i, x; val) + tr_var_minus_val(x; val) + tr_var_minus_val(x, X::AbstractVector; val) -Return the difference `x[i] - param` if positive, `0.0` otherwise. Extended method to vector with sig `(x, param)` are generated. +Return the difference `x[i] - val` if positive, `0.0` otherwise. Extended method to vector with sig `(x, val)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_val_minus_param(i, x; param) = max(0, x[i] - param) +tr_var_minus_val(i, x; val, params...) = max(0, x[i] - val) """ - tr_param_minus_val(i, x; param) - tr_param_minus_val(x; param) - tr_param_minus_val(x, X::AbstractVector; param) + tr_val_minus_var(i, x; val) + tr_val_minus_var(x; val) + tr_val_minus_var(x, X::AbstractVector; val) -Return the difference `param - x[i]` if positive, `0.0` otherwise. Extended method to vector with sig `(x, param)` are generated. +Return the difference `val - x[i]` if positive, `0.0` otherwise. Extended method to vector with sig `(x, val)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_param_minus_val(i, x; param) = max(0, param - x[i]) +tr_val_minus_var(i, x; val, params...) = max(0, val - x[i]) # Generating vetorized versions -lazy_param(tr_val_minus_param, tr_param_minus_val) +lazy_param(tr_var_minus_val, tr_val_minus_var) # Continuous values subtraction """ - tr_contiguous_vals_minus(i, x) - tr_contiguous_vals_minus(x) - tr_contiguous_vals_minus(x, X::AbstractVector) + tr_contiguous_vars_minus(i, x) + tr_contiguous_vars_minus(x) + tr_contiguous_vars_minus(x, X::AbstractVector) Return the difference `x[i] - x[i + 1]` if positive, `0.0` otherwise. Extended method to vector with sig `(x)` are generated. When `X` is provided, the result is computed without allocations. """ -tr_contiguous_vals_minus(i, x; param = nothing) = - length(x) == i ? 0 : tr_val_minus_param(i, x; param = x[i+1]) +tr_contiguous_vars_minus(i, x; params...) = + length(x) == i ? 0 : tr_var_minus_val(i, x; val = x[i+1]) """ - tr_contiguous_vals_minus_rev(i, x) - tr_contiguous_vals_minus_rev(x) - tr_contiguous_vals_minus_rev(x, X::AbstractVector) + tr_contiguous_vars_minus_rev(i, x) + tr_contiguous_vars_minus_rev(x) + tr_contiguous_vars_minus_rev(x, X::AbstractVector) Return the difference `x[i + 1] - x[i]` if positive, `0.0` otherwise. Extended method to vector with sig `(x)` are generated. When `X` is provided, the result is computed without allocations. """ -function tr_contiguous_vals_minus_rev(i, x; param = nothing) - return length(x) == i ? 0 : tr_param_minus_val(i, x; param = x[i+1]) +function tr_contiguous_vars_minus_rev(i, x; params...) + return length(x) == i ? 0 : tr_val_minus_var(i, x; val = x[i+1]) end # Generating vetorized versions -lazy(tr_contiguous_vals_minus, tr_contiguous_vals_minus_rev) +lazy(tr_contiguous_vars_minus, tr_contiguous_vars_minus_rev) """ make_transformations(param::Symbol) @@ -274,19 +274,19 @@ function make_transformations(::Val{:none}) :count_l_left => tr_count_l_left, :count_g_right => tr_count_g_right, :count_l_right => tr_count_l_right, - :contiguous_vals_minus => tr_contiguous_vals_minus, - :contiguous_vals_minus_rev => tr_contiguous_vals_minus_rev, + :contiguous_vars_minus => tr_contiguous_vars_minus, + :contiguous_vars_minus_rev => tr_contiguous_vars_minus_rev, ) end function make_transformations(::Val{:val}) return LittleDict{Symbol,Function}( - :count_eq_param => tr_count_eq_param, - :count_l_param => tr_count_l_param, - :count_g_param => tr_count_g_param, - :count_bounding_param => tr_count_bounding_param, - :val_minus_param => tr_val_minus_param, - :param_minus_val => tr_param_minus_val, + :count_eq_val => tr_count_eq_val, + :count_l_val => tr_count_l_val, + :count_g_val => tr_count_g_val, + :count_bounding_val => tr_count_bounding_val, + :var_minus_val => tr_var_minus_val, + :val_minus_var => tr_val_minus_var, ) end @@ -296,8 +296,8 @@ end """ - transformation_layer(param = false) -Generate the layer of transformations functions of the ICN. Iff `param` value is true, also includes all the parametric transformations. + transformation_layer(param = Vector{Symbol}()) +Generate the layer of transformations functions of the ICN. Iff `param` value is non empty, also includes all the related parametric transformations. """ function transformation_layer(parameters = Vector{Symbol}()) transformations = make_transformations(:none) @@ -311,7 +311,7 @@ function transformation_layer(parameters = Vector{Symbol}()) end ## SECTION - Test Items -@testitem "Arithmetic Layer" tags = [:arithmetic, :layer] begin +@testitem "Transformation Layer" tags = [:transformation, :layer] begin CN = CompositionalNetworks data = [[1, 5, 2, 4, 3] => 2, [1, 2, 3, 2, 1] => 2] @@ -328,12 +328,11 @@ end CN.tr_count_l_left => [[0, 1, 1, 2, 2], [0, 1, 2, 1, 0]], CN.tr_count_g_right => [[4, 0, 2, 0, 0], [3, 1, 0, 0, 0]], CN.tr_count_l_right => [[0, 3, 0, 1, 0], [0, 1, 2, 1, 0]], - CN.tr_contiguous_vals_minus => [[0, 3, 0, 1, 0], [0, 0, 1, 1, 0]], - CN.tr_contiguous_vals_minus_rev => [[4, 0, 2, 0, 0], [1, 1, 0, 0, 0]], + CN.tr_contiguous_vars_minus => [[0, 3, 0, 1, 0], [0, 0, 1, 1, 0]], + CN.tr_contiguous_vars_minus_rev => [[4, 0, 2, 0, 0], [1, 1, 0, 0, 0]], ) for (f, results) in funcs - @info f for (key, vals) in enumerate(data) @test f(vals.first) == results[key] foreach(i -> f(i, vals.first), vals.first) @@ -341,20 +340,19 @@ end end # Test transformations with parameter - funcs_param = Dict( - CN.tr_count_eq_param => [[1, 0, 1, 0, 1], [1, 0, 0, 0, 1]], - CN.tr_count_l_param => [[2, 5, 3, 5, 4], [4, 5, 5, 5, 4]], - CN.tr_count_g_param => [[2, 0, 1, 0, 0], [0, 0, 0, 0, 0]], - CN.tr_count_bounding_param => [[3, 1, 3, 2, 3], [5, 3, 1, 3, 5]], - CN.tr_val_minus_param => [[0, 3, 0, 2, 1], [0, 0, 1, 0, 0]], - CN.tr_param_minus_val => [[1, 0, 0, 0, 0], [1, 0, 0, 0, 1]], + funcs_val = Dict( + CN.tr_count_eq_val => [[1, 0, 1, 0, 1], [1, 0, 0, 0, 1]], + CN.tr_count_l_val => [[2, 5, 3, 5, 4], [4, 5, 5, 5, 4]], + CN.tr_count_g_val => [[2, 0, 1, 0, 0], [0, 0, 0, 0, 0]], + CN.tr_count_bounding_val => [[3, 1, 3, 2, 3], [5, 3, 1, 3, 5]], + CN.tr_var_minus_val => [[0, 3, 0, 2, 1], [0, 0, 1, 0, 0]], + CN.tr_val_minus_var => [[1, 0, 0, 0, 0], [1, 0, 0, 0, 1]], ) - for (f, results) in funcs_param - @info f + for (f, results) in funcs_val for (key, vals) in enumerate(data) - @test f(vals.first; param = vals.second) == results[key] - foreach(i -> f(i, vals.first; param = vals.second), vals.first) + @test f(vals.first; val = vals.second) == results[key] + foreach(i -> f(i, vals.first; val = vals.second), vals.first) end end diff --git a/src/utils.jl b/src/utils.jl index 6251684..50cb474 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -2,22 +2,22 @@ map_tr!(f, x, X, param) Return an anonymous function that applies `f` to all elements of `x` and store the result in `X`, with a parameter `param` (which is set to `nothing` for function with no parameter). """ -function map_tr!(f, x, X, p) - return ((g, y, Y; param) -> map!(i -> g(i, y; param), Y, 1:length(y)))( +function map_tr!(f, x, X; p...) + return ((g, y, Y; params...) -> map!(i -> g(i, y; params...), Y, 1:length(y)))( f, x, X; - param = p, - ) -end -function map_tr!(f, x, X) - return ((g, y, Y; param) -> map!(i -> g(i, y), Y, 1:length(y)))( - f, - x, - X; - param = nothing, + p..., ) end +# function map_tr!(f, x, X) +# return ((g, y, Y; param) -> map!(i -> g(i, y), Y, 1:length(y)))( +# f, +# x, +# X; +# param=nothing, +# ) +# end """ lazy(funcs::Function...) @@ -25,10 +25,13 @@ Generate methods extended to a vector instead of one of its components. A functi """ function lazy(funcs::Function...) for f in Iterators.map(Symbol, funcs) - eval(:(function $f(x::V, X; param = nothing) where {V<:AbstractVector} - return map_tr!($f, x, X) - end)) - eval(:($f(x; param = nothing) = $f(x, similar(x); param))) + eval( + :( + $f(x::V, X; params...) where {V<:AbstractVector} = + map_tr!($f, x, X; params...) + ), + ) + eval(:($f(x; params...) = $f(x, similar(x); params...))) end return nothing end @@ -39,8 +42,13 @@ Generate methods extended to a vector instead of one of its components. A functi """ function lazy_param(funcs::Function...) for f in Iterators.map(Symbol, funcs) - eval(:($f(x::V, X; param) where {V<:AbstractVector} = map_tr!($f, x, X, param))) - eval(:($f(x; param) = $f(x, similar(x); param))) + eval( + :( + $f(x::V, X; params...) where {V<:AbstractVector} = + map_tr!($f, x, X; params...) + ), + ) + eval(:($f(x; params...) = $f(x, similar(x); params...))) end return nothing end @@ -88,8 +96,8 @@ end Application of an operation from the transformation layer. Used to generate more efficient code for all compositions. """ -@unroll function tr_in(tr, X, x, param) +@unroll function tr_in(tr, X, x; params...) @unroll for i = 1:length(tr) - tr[i](x, @view(X[:, i]); param) + tr[i](x, @view(X[:, i]); params...) end end diff --git a/test/icn.jl b/test/icn.jl index e992037..a821f53 100644 --- a/test/icn.jl +++ b/test/icn.jl @@ -22,14 +22,14 @@ compo = compose(icn) @test code(compo; name = "test_composition") == - "test_composition = identity ∘ sum ∘ sum ∘ [param_minus_val, val_minus_param" * - ", count_bounding_param, count_g_param, count_l_param, count_eq_param," * - " contiguous_vals_minus_rev, contiguous_vals_minus, count_l_right, count_g_right" * + "test_composition = identity ∘ sum ∘ sum ∘ [val_minus_var, var_minus_val" * + ", count_bounding_val, count_g_val, count_l_val, count_eq_val," * + " contiguous_vars_minus_rev, contiguous_vars_minus, count_l_right, count_g_right" * ", count_l_left, count_g_left, count_lesser, count_greater, count_eq_right, " * "count_eq_left, count_eq, identity]" v = [1, 2, 4, 3] - @test composition(compo)(v; param = 2, dom_size = 4) == 67 + @test composition(compo)(v; val = 2, dom_size = 4) == 67 CompositionalNetworks.generate_weights(icn) From d229a0d4b2d05ea14dc3cf614eae44895851fcbe Mon Sep 17 00:00:00 2001 From: Jean-Francois Baffier Date: Sun, 23 Jun 2024 09:02:26 +0900 Subject: [PATCH 2/3] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 885221d..09b86f2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "CompositionalNetworks" uuid = "4b67e4b5-442d-4ef5-b760-3f5df3a57537" authors = ["Jean-François Baffier"] -version = "0.5.6" +version = "0.5.7" [deps] ConstraintCommons = "e37357d9-0691-492f-a822-e5ea6a920954" From a92226541d25a6d1695b416ee895129d12401a2e Mon Sep 17 00:00:00 2001 From: Azzaare Date: Fri, 12 Jul 2024 03:03:13 +0000 Subject: [PATCH 3/3] Compat and CI updates --- .github/workflows/ci.yml | 4 ++-- Project.toml | 10 +++++----- src/CompositionalNetworks.jl | 1 - 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2fd83f0..18ae322 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,7 +52,7 @@ jobs: version: - "1.8" - "1" # automatically expands to the latest stable 1.x release of Julia - - nightly + - "pre" os: - ubuntu-latest arch: @@ -71,7 +71,7 @@ jobs: version: 1 steps: - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v1 + - uses: julia-actions/setup-julia@v2 with: version: ${{ matrix.version }} arch: ${{ matrix.arch }} diff --git a/Project.toml b/Project.toml index 09b86f2..a432cb2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "CompositionalNetworks" uuid = "4b67e4b5-442d-4ef5-b760-3f5df3a57537" authors = ["Jean-François Baffier"] -version = "0.5.7" +version = "0.5.8" [deps] ConstraintCommons = "e37357d9-0691-492f-a822-e5ea6a920954" @@ -11,7 +11,6 @@ Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" -TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe" ThreadSafeDicts = "4239201d-c60e-5e0a-9702-85d713665ba7" Unrolled = "9602ed7d-8fef-5bc8-8597-8f21381861e8" @@ -24,8 +23,8 @@ Distances = "0.10" JuliaFormatter = "1" OrderedCollections = "1" Random = "1" -TestItemRunner = "0.2" -TestItems = "0.1" +TestItemRunner = "0.2, 1" +TestItems = "0.1, 1" ThreadSafeDicts = "0.1" Unrolled = "0.1" julia = "1.8" @@ -35,7 +34,8 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" Evolutionary = "86b6b26d-c046-49b6-aa0b-5f0f74682bd6" Memoization = "6fafb56a-5788-4b4e-91ca-c0cea6611c73" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" ThreadPools = "b189fb0b-2eb5-4ed4-bc0c-d34c51242431" [targets] -test = ["Aqua", "Evolutionary", "Memoization", "Test", "ThreadPools"] +test = ["Aqua", "Evolutionary", "Memoization", "Test", "TestItemRunner", "ThreadPools"] diff --git a/src/CompositionalNetworks.jl b/src/CompositionalNetworks.jl index 9242c00..2bc3939 100644 --- a/src/CompositionalNetworks.jl +++ b/src/CompositionalNetworks.jl @@ -8,7 +8,6 @@ import Distances using JuliaFormatter using OrderedCollections using Random -using TestItemRunner using TestItems using ThreadSafeDicts using Unrolled