Skip to content

Commit

Permalink
Merge pull request #83 from Herb-AI/dev
Browse files Browse the repository at this point in the history
HerbGrammar `v0.4.0`
  • Loading branch information
THinnerichs authored Jul 25, 2024
2 parents aca61f9 + 671a302 commit ce15a3a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HerbGrammar"
uuid = "4ef9e186-2fe5-4b24-8de7-9f7291f24af7"
authors = ["Sebastijan Dumancic <s.dumancic@tudelft.nl>", "Jaap de Jong <J.deJong-18@student.tudelft.nl>", "Nicolae Filat <N.Filat@student.tudelft.nl>", "Piotr Cichoń <gitlab@gitlab.ewi.tudelft.nl>"]
version = "0.3.0"
version = "0.4.0"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
4 changes: 2 additions & 2 deletions src/csg/csg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ end
- [`@pcsgrammar`](@ref) uses a similar syntax to create probabilistic [`ContextSensitiveGrammar`](@ref)s.
"""
macro csgrammar(ex)
return expr2csgrammar(ex)
return :(expr2csgrammar($(QuoteNode(ex))))
end


Expand All @@ -126,7 +126,7 @@ end
This macro is deprecated and will be removed in future versions. Use [`@csgrammar`](@ref) instead.
"""
macro cfgrammar(ex)
return expr2csgrammar(ex)
return :(expr2csgrammar($(QuoteNode(ex))))
end

parse_rule!(v::Vector{Any}, r) = push!(v, r)
Expand Down
18 changes: 4 additions & 14 deletions src/csg/probabilistic_csg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ function expr2pcsgrammar(ex::Expr)::ContextSensitiveGrammar
end
end
alltypes = collect(keys(bytype))
# Normalize probabilities for each type
for t alltypes
total_prob = sum(probabilities[i] for i bytype[t])
if !(total_prob 1)
@warn "The probabilities for type $t don't add up to 1, so they will be normalized."
for i bytype[t]
probabilities[i] /= total_prob
end
end
end

log_probabilities = [log(x) for x probabilities]
is_terminal = [isterminal(rule, alltypes) for rule in rules]
is_eval = [iseval(rule) for rule in rules]
Expand All @@ -63,13 +52,14 @@ Returns `nothing` if the rule is not probabilistic, otherwise a `Tuple` of its t
`Vector` of probability-rule pairs it expands into.
"""
function parse_probabilistic_rule(e::Expr)
e = Base.remove_linenums!(e)
prvec = Tuple{Real, Any}[]
if e.head == :(=)
left = e.args[1] # name of return type and probability
if left isa Expr && left.head == :call && left.args[1] == :(:)
p = left.args[2] # Probability
s = left.args[3] # Return type
rule = e.args[2].args[2] # extract rule from block expr
rule = e.args[2].args[1] # extract rule from block expr

rvec = Any[]
parse_rule!(rvec, rule)
Expand Down Expand Up @@ -149,9 +139,9 @@ The probabilities are automatically scaled if this isn't the case.
- [`@csgrammar`](@ref) uses a similar syntax to create non-probabilistic [`ContextSensitiveGrammar`](@ref)s.
"""
macro pcsgrammar(ex)
return expr2pcsgrammar(ex)
return :(expr2pcsgrammar($(QuoteNode(ex))))
end

macro pcfgrammar(ex)
return expr2pcsgrammar(ex)
return :(expr2pcsgrammar($(QuoteNode(ex))))
end
6 changes: 3 additions & 3 deletions src/rulenode_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function rulenode2expr(rulenode::AbstractRuleNode, grammar::AbstractGrammar)
if !isfilled(rulenode)
return _get_hole_type(rulenode, grammar)
end
root = hasdynamicvalue(rulenode) ? rulenode._val : deepcopy(grammar.rules[get_rule(rulenode)])
root = deepcopy(grammar.rules[get_rule(rulenode)])
if !grammar.isterminal[get_rule(rulenode)] # not terminal
root,_ = _rulenode2expr(root, rulenode, grammar)
end
Expand All @@ -183,7 +183,7 @@ function _rulenode2expr(expr::Expr, rulenode::AbstractRuleNode, grammar::Abstrac
elseif haskey(grammar.bytype, arg)
child = rulenode.children[j+=1]
if isfilled(child)
expr.args[k] = hasdynamicvalue(child) ? child._val : deepcopy(grammar.rules[get_rule(child)])
expr.args[k] = deepcopy(grammar.rules[get_rule(child)])
if !isterminal(grammar, child)
expr.args[k],_ = _rulenode2expr(expr.args[k], child, grammar, 0)
end
Expand All @@ -202,7 +202,7 @@ function _rulenode2expr(typ::Symbol, rulenode::AbstractRuleNode, grammar::Abstra
retval = typ
if haskey(grammar.bytype, typ)
child = rulenode.children[1]
retval = hasdynamicvalue(rulenode) ? child._val : deepcopy(grammar.rules[get_rule(child)])
retval = deepcopy(grammar.rules[get_rule(child)])
if !grammar.isterminal[get_rule(child)]
retval,_ = _rulenode2expr(retval, child, grammar, 0)
end
Expand Down
28 changes: 28 additions & 0 deletions test/test_csg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@
@test sum(map(exp, g.log_probabilities[g.bytype[:R]])) 1.0
@test sum(map(exp, g.log_probabilities[g.bytype[:B]])) 1.0
end

@testset "Creating a non-probabilistic rule in a PCSG" begin
expected_log = (
:error,
"Rule without probability encountered in probabilistic grammar. Rule ignored."
)

@test_logs expected_log match_mode=:any begin
@pcsgrammar begin
0.5 : R = x
R = R + R
end
end
end

@testset "Test that strict equality is used during rule creation" begin
g₁ = @csgrammar begin
Expand Down Expand Up @@ -227,4 +241,18 @@
@test g.bychildtypes[8] == [0, 0, 0, 0, 0, 0, 1, 1] # 7, 8
end

@testset "Check that macros return an expr, not an object" begin
@test typeof(@macroexpand @csgrammar begin
A = 1
end) == Expr
@test typeof(@macroexpand @cfgrammar begin
A = 1
end) == Expr
@test typeof(@macroexpand @pcsgrammar begin
1.0 : A = 1
end) == Expr
@test typeof(@macroexpand @pcfgrammar begin
1.0 : A = 1
end) == Expr
end
end

2 comments on commit ce15a3a

@ReubenJ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/111746

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.0 -m "<description of version>" ce15a3adf31a5fd2cd35aa7ba370c7bc37560b1a
git push origin v0.4.0

Please sign in to comment.