Skip to content

Commit 2acaa0f

Browse files
authored
Merge branch 'develop' into worktree-fix-tree-learner-refinement-dup
2 parents 104bdf3 + 802d86a commit 2acaa0f

4 files changed

Lines changed: 36 additions & 35 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ date) and use that section as the basis for the GitHub release notes.
3333
`_finalize_predictions`/`_maybe_plot_embeddings` hooks on the base `TDL`
3434
class, and `TDL_refinement` now inherits `create_training_data` instead of
3535
redefining it. (#616)
36+
- `quality_funcs.f1`/`acc` reimplemented the tp/tn/fp/fn and precision/recall
37+
arithmetic already implemented in `metrics.py`'s `F1`/`Accuracy` classes; now
38+
delegate to those classes instead of duplicating the math.
3639
- Ruff upgraded to `>=0.16.0`. (#594)
3740
- `TDL`/`TDL_refinement` data-property filtering extended to exclude boolean
3841
data properties, with a toggle to enable/disable boolean data properties.
@@ -66,6 +69,10 @@ date) and use that section as the basis for the GitHub release notes.
6669
`EvoLearner` instances (e.g. `fit()` calls interleaved across threads) could
6770
silently clobber each other's DEAP type definitions. Each instance now
6871
registers its own uniquely-named types. (#609)
72+
- `TDL`'s "no features extracted" error message referenced `self.use_data_properties`,
73+
an attribute `TDL.__init__` never sets, raising `AttributeError` instead of the
74+
intended descriptive error; now reports the actual
75+
`use_data_properties_boolean/string/date/numeric` flags.
6976
- `DRILLSearchTreePriorityQueue.get_top_n()` raised `AttributeError`/`TypeError`
7077
on the never-assigned `self.refined_nodes` attribute; now builds its node
7178
list from `self.nodes.values()`. (#605)

‎Improvements.md‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,27 @@ Prioritized roughly high → low impact within each section.
1010
These are actual defects, not style nits — worth fixing regardless of any
1111
broader refactor.
1212

13-
- **`ontolearn/learners/tree_learner.py:441`** — the "no features extracted"
14-
error message references `self.use_data_properties`, an attribute
15-
`TDL.__init__` never sets (only `use_data_properties_boolean/string/date/numeric`
16-
exist). Hitting this branch raises `AttributeError` instead of the intended
17-
descriptive error, hiding the real problem from users. (`TDL_refinement`
18-
does define this attribute, so only the base `TDL` class is affected.)
13+
All correctness bugs originally found in this pass have been fixed; none
14+
remain open as of this writing.
1915

2016
## 2. Code duplication
2117

2218
- **`ontolearn/quality_funcs.py:31-56`** duplicates the tp/tn/fp/fn and
2319
precision/recall math already implemented in `ontolearn/metrics.py:67-92`'s
2420
`F1`/`Accuracy` classes. Have `quality_funcs.py` delegate to those classes
2521
instead of reimplementing the arithmetic.
22+
- **`ontolearn/learners/tree_learner_refinement_inherit.py` vs
23+
`tree_learner.py`** — `TDL_refinement(TDL)` is ~70% copy-pasted rather than
24+
extended. `fit()` (tree_learner_refinement_inherit.py:604-761) duplicates
25+
`TDL.fit()` (tree_learner.py:661-736) almost line-for-line (grid search,
26+
classifier training, `report_classification`, plotting) with only the
27+
refinement loop spliced in; `extract_expressions_from_owl_individuals`,
28+
`create_training_data`, and `_extract_data_property_features` are likewise
29+
wholesale overrides. A template-method refactor — pulling
30+
`_train_classifier`/`_report`/`_plot` into the base `TDL` as hooks — would
31+
remove on the order of 300 duplicated lines. (`OCEL(CELOE)` in
32+
`ontolearn/learners/celoe.py`/`ocel.py` is a good counter-example of doing
33+
this inheritance correctly — worth using as the template.)
2634
- **`ontolearn/knowledge_base.py:191-262`** — `abox()` repeats the same
2735
triple-traversal ~70 lines of near-identical branching, once per
2836
`mode in {"native", "iri", "axiom"}`, differing only in output formatting.
@@ -158,8 +166,7 @@ broader refactor.
158166

159167
## Suggested priority order
160168

161-
1. Fix the remaining correctness bug in §1 (cheap, high-value, likely a
162-
one-liner).
169+
1. All three correctness bugs originally listed in §1 are fixed; none remain.
163170
2. Fix the two mutable-default-argument bugs in §4 (cheap, real risk).
164171
3. Delete or restore the two fully-commented-out test files in §6.
165172
4. Tackle the `tree_learner_refinement_inherit.py` duplication in §2 as a

‎ontolearn/learners/tree_learner.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,10 @@ def extract_expressions_from_owl_individuals(self, individuals: List[OWLNamedInd
438438
f" - Number of individuals processed: {num_individuals}\n"
439439
" - Number of features extracted: 0\n"
440440
f" - use_inverse: {self.use_inverse}\n"
441-
f" - use_data_properties: {self.use_data_properties}\n"
441+
f" - use_data_properties_boolean: {self.use_data_properties_boolean}\n"
442+
f" - use_data_properties_string: {self.use_data_properties_string}\n"
443+
f" - use_data_properties_date: {self.use_data_properties_date}\n"
444+
f" - use_data_properties_numeric: {self.use_data_properties_numeric}\n"
442445
f" - use_card_restrictions: {self.use_card_restrictions}\n"
443446
"Possible causes:\n"
444447
" - The knowledge base is empty or contains no relevant axioms about the individuals.\n"

‎ontolearn/quality_funcs.py‎

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,32 @@
2525
from typing import Set
2626
from owlapy.class_expression import OWLClassExpression
2727
from ontolearn.abstracts import EncodedLearningProblem, AbstractScorer, AbstractKnowledgeBase
28+
from ontolearn.metrics import F1 as _F1, Accuracy as _Accuracy
2829
from ontolearn.search import EvaluatedConcept
2930

3031

31-
def f1(*, individuals: Set, pos: Set, neg: Set):
32+
def _tp_tn_fp_fn(individuals: Set, pos: Set, neg: Set):
3233
assert isinstance(individuals, set)
3334
assert isinstance(pos, set)
3435
assert isinstance(neg, set)
3536

3637
tp = len(pos.intersection(individuals))
3738
tn = len(neg.difference(individuals))
38-
3939
fp = len(neg.intersection(individuals))
4040
fn = len(pos.difference(individuals))
41+
return tp, tn, fp, fn
4142

42-
try:
43-
recall = tp / (tp + fn)
44-
except ZeroDivisionError:
45-
return 0
46-
47-
try:
48-
precision = tp / (tp + fp)
49-
except ZeroDivisionError:
50-
return 0
51-
52-
if precision == 0 or recall == 0:
53-
return 0
5443

55-
f_1 = 2 * ((precision * recall) / (precision + recall))
56-
return f_1
44+
def f1(*, individuals: Set, pos: Set, neg: Set):
45+
tp, tn, fp, fn = _tp_tn_fp_fn(individuals, pos, neg)
46+
applicable, score = _F1().score2(tp=tp, fn=fn, fp=fp, tn=tn)
47+
return score if applicable else 0
5748

5849

5950
def acc(*, individuals: Set, pos: Set, neg: Set):
60-
assert isinstance(individuals, set)
61-
assert isinstance(pos, set)
62-
assert isinstance(neg, set)
63-
64-
tp = len(pos.intersection(individuals))
65-
tn = len(neg.difference(individuals))
66-
67-
fp = len(neg.intersection(individuals))
68-
fn = len(pos.difference(individuals))
69-
return (tp + tn) / (tp + tn + fp + fn)
51+
tp, tn, fp, fn = _tp_tn_fp_fn(individuals, pos, neg)
52+
_, score = _Accuracy().score2(tp=tp, fn=fn, fp=fp, tn=tn)
53+
return score
7054

7155

7256
def evaluate_concept(kb: AbstractKnowledgeBase, concept: OWLClassExpression, quality_func: AbstractScorer,

0 commit comments

Comments
 (0)