Skip to content

Commit 58e808f

Browse files
committed
Minor adjustments to listrules
1 parent c0e6e56 commit 58e808f

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

src/evaluate.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function readmetrics(m::Rule{L}; round_digits = nothing, class_share_map = nothi
134134
coverage = cons_metrics.coverage * (length(_gts_leaf)/length(_gts))
135135
confidence = cons_metrics.confidence
136136
metrics = (;
137-
ninstances = length(_gts),
137+
ninstances = length(_gts),
138138
ncovered = length(_gts_leaf),
139139
coverage = _metround(coverage, round_digits),
140140
confidence = confidence,
@@ -149,8 +149,8 @@ function readmetrics(m::Rule{L}; round_digits = nothing, class_share_map = nothi
149149
metrics
150150
elseif haskey(info(m), :supporting_labels)
151151
return (; ninstances = length(info(m).supporting_labels), additional_metrics...)
152-
elseif haskey(info(consequent(m)), :supporting_labels)
153-
return (; ninstances = length(info(m).supporting_labels), additional_metrics...)
152+
# elseif haskey(info(consequent(m)), :supporting_labels)
153+
# return (; ninstances = length(info(consequent(m)).supporting_labels), additional_metrics...)
154154
else
155155
return (;)
156156
end
@@ -196,7 +196,7 @@ metricstable(
196196
)
197197
198198
Compute metrics for a rule with respect to a labeled dataset and returns a `NamedTuple` consisting of:
199-
- `support`: number of instances satisfying the antecedent of the rule divided by
199+
- `coverage`: number of instances satisfying the antecedent of the rule divided by
200200
the total number of instances;
201201
- `error`:
202202
- For classification problems: number of instances that were not classified
@@ -218,13 +218,12 @@ function rulemetrics(
218218
Y::AbstractVector{<:Label};
219219
kwargs...,
220220
)
221-
eval_result = evaluaterule(rule, X, Y; kwargs...)
222221
ys = apply(rule,X)
223-
checkmask = eval_result[:checkmask]
222+
checkmask = checkantecedent(rule, X; kwargs...)
224223
n_instances = ninstances(X)
225224
n_satisfy = sum(checkmask)
226225

227-
rule_support = n_satisfy / n_instances
226+
rule_coverage = n_satisfy / n_instances
228227

229228
rule_error = begin
230229
if outcometype(consequent(rule)) <: CLabel
@@ -241,9 +240,9 @@ function rulemetrics(
241240

242241
return (;
243242
checkmask = checkmask,
244-
support = rule_support,
243+
coverage = rule_coverage,
245244
error = rule_error,
246-
length = natoms(antecedent(rule)),
245+
natoms = natoms(antecedent(rule)),
247246
)
248247
end
249248

src/rule-extraction.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ function join_antecedents(assumed_formulas::Vector{<:SoleLogics.Formula})
7171
push!(new_assumed_formulas, φ)
7272
end
7373
end
74+
# @show typeof.(new_assumed_formulas)
75+
# @show syntaxstring.(new_assumed_formulas)
76+
# @show LeftmostConjunctiveForm(new_assumed_formulas)
7477
LeftmostConjunctiveForm(new_assumed_formulas)
7578
else
7679
(length(assumed_formulas) == 1 ? first(assumed_formulas) : (filter(!istop, assumed_formulas)...))
@@ -252,22 +255,24 @@ function _listrules(
252255
min_confidence::Union{Nothing,Number} = nothing,
253256
min_coverage::Union{Nothing,Number} = nothing,
254257
min_ninstances::Union{Nothing,Number} = nothing,
258+
flip_atoms::Bool = true,
255259
kwargs...,
256260
) where {O}
257261
use_leftmostlinearform = !isnothing(use_leftmostlinearform) ? use_leftmostlinearform : (antecedent(m) isa SoleLogics.AbstractSyntaxStructure) # TODO default to true
258262

259263
subkwargs = (;
260264
use_shortforms = use_shortforms,
261265
use_leftmostlinearform = use_leftmostlinearform,
262-
normalize = false,
266+
# normalize = false, TODO?
267+
normalize = normalize,
263268
normalize_kwargs = normalize_kwargs,
264269
scalar_simplification = false,
265270
force_syntaxtree = force_syntaxtree,
266271
min_confidence = min_confidence,
267272
min_coverage = min_coverage,
268273
min_ninstances = min_ninstances,
269274
kwargs...)
270-
275+
# @show normalize, normalize_kwargs
271276
_subrules = []
272277
if isnothing(min_ninstances) || (haskey(info(m), :supporting_labels) && length(info(m, :supporting_labels)) >= min_ninstances)
273278
# if (haskey(info(m), :supporting_labels) && length(info(m, :supporting_labels)) >= min_ninstances) &&
@@ -303,7 +308,10 @@ function _listrules(
303308
if (use_shortforms && haskey(info(subrule), :shortform))
304309
info(subrule)[:shortform], true
305310
else
306-
(flag ? antecedent(m) : ¬antecedent(m)), false
311+
# Automatic flip.
312+
smart_neg(f) = (f isa Atom && flip_atoms && SoleLogics.hasdual(f) ? SoleLogics.dual(f) : ¬f)
313+
_antd = antecedent(m)
314+
(flag ? _antd : smart_neg(_antd)), false
307315
end
308316
end
309317
antformula = force_syntaxtree ? tree(antformula) : antformula
@@ -334,9 +342,14 @@ function _listrules(
334342
φ
335343
end
336344
end
345+
# @show normalize, normalize_kwargs
337346
normalize && (ant = SoleLogics.normalize(ant; normalize_kwargs...))
347+
# @show 1
338348
# @show ant
339349
ant = _scalar_simplification(ant, scalar_simplification)
350+
# @show 2
351+
# @show ant
352+
# readline()
340353
Rule(ant, consequent(subrule), merge(info(subrule), _info))
341354
else
342355
error("Unexpected rule type: $(typeof(subrule)).")

src/types/api.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ This function translates a symbolic model to a symbolic model using the structur
1919
See also [`AbstractModel`](@ref), [`ConstantModel`](@ref), [`FunctionModel`](@ref),
2020
[`LeafModel`](@ref).
2121
"""
22-
function solemodel(o::Any, args...; kwargs...)
22+
function solemodel(o::Any, FM::Type{<:AbstractModel} = AbstractModel, args...; kwargs...)
2323
try
24-
convert(FM, wrap(o))
2524
# FM TODO
25+
convert(FM, wrap(o))
2626
catch e
2727
if e isa MethodError
2828
throw(MethodError("Please, provide solemodel(::$(typeof(o)))"))

src/utils/models/other.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,24 @@ function listimmediaterules(
106106
ant = antecedent(rule)
107107
force_syntaxtree && (ant = tree(ant))
108108
# @show ant
109-
nant = SoleLogics.NEGATION(ant)
109+
# println()
110+
# @show typeof(ant)
111+
nant = SoleLogics.NEGATION(tree(ant))
112+
# @show typeof(nant)
110113
# @show typeof(nant)
111114
normalize && (nant = SoleLogics.normalize(nant; normalize_kwargs...))
112115
# @show typeof(nant)
113116
nant = _scalar_simplification(nant, scalar_simplification)
114117
# @show typeof(nant)
118+
# readline()
115119
assumed_formulas = push!(assumed_formulas, nant)
116120
end
117121
# @show eltype(assumed_formulas)
122+
# @show assumed_formulas
118123
default_φ = join_antecedents(assumed_formulas)
119124
# @show default_φ
120125
default_φ = _scalar_simplification(default_φ, scalar_simplification)
126+
# @show default_φ
121127
# normalize && (default_φ = SoleLogics.normalize(default_φ; normalize_kwargs...))
122128
push!(normalized_rules, Rule(default_φ, defaultconsequent(m), info(defaultconsequent(m))))
123129
normalized_rules

0 commit comments

Comments
 (0)