Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ date) and use that section as the basis for the GitHub release notes.
`F`) instead of relying on ruff's shipped default. (#594)

### Changed
- `TDL_refinement.fit()` (`ontolearn/learners/tree_learner_refinement_inherit.py`)
duplicated `TDL.fit()` (`ontolearn/learners/tree_learner.py`) almost
line-for-line for grid search, classifier training, classification
reporting, and plotting, with only the SHAP-based refinement loop spliced
in; likewise `create_training_data` was a byte-for-byte copy of `TDL`'s.
Extracted the shared steps into `_fit_decision_tree`/`_report_and_plot`/
`_finalize_predictions`/`_maybe_plot_embeddings` hooks on the base `TDL`
class, and `TDL_refinement` now inherits `create_training_data` instead of
redefining it. (#616)
- `quality_funcs.f1`/`acc` reimplemented the tp/tn/fp/fn and precision/recall
arithmetic already implemented in `metrics.py`'s `F1`/`Accuracy` classes; now
delegate to those classes instead of duplicating the math.
Expand All @@ -35,6 +44,14 @@ date) and use that section as the basis for the GitHub release notes.
by default, matching the other learners. (#585)

### Fixed
- `TDL_refinement.classification_report` always raised `AttributeError`:
`TDL_refinement.fit()` set `self.__classification_report` from within its
own class body, which Python name-mangles to a different attribute than
the one the `classification_report` property (defined on `TDL`) reads.
Fixed as part of deduplicating `fit()` into shared `TDL` hooks, above; the
report is now always set from the base class's `_report_and_plot`, so both
classes agree on the mangled name. `tests/test_tdl_refinement.py`'s
`test_classification_report` re-enabled. (#616)
- `NCESTrainer.train()` created a fresh `DataLoader(num_workers=...)` worker
pool per architecture in a loop; the existing `os.cpu_count()`-based cap on
`num_workers` only engaged when `cpu_count <= num_workers`, so it silently
Expand Down
4 changes: 4 additions & 0 deletions Improvements.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ remain open as of this writing.

## 2. Code duplication

- **`ontolearn/quality_funcs.py:31-56`** duplicates the tp/tn/fp/fn and
precision/recall math already implemented in `ontolearn/metrics.py:67-92`'s
`F1`/`Accuracy` classes. Have `quality_funcs.py` delegate to those classes
instead of reimplementing the arithmetic.
- **`ontolearn/learners/tree_learner_refinement_inherit.py` vs
`tree_learner.py`** — `TDL_refinement(TDL)` is ~70% copy-pasted rather than
extended. `fit()` (tree_learner_refinement_inherit.py:604-761) duplicates
Expand Down
83 changes: 50 additions & 33 deletions ontolearn/learners/tree_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,49 +661,28 @@ def construct_owl_expression_from_tree(self, X: pd.DataFrame, y: pd.DataFrame) -
prediction_per_example = {pred for pred, positive_example in prediction_per_example}
return list(prediction_per_example)

def fit(self, learning_problem: PosNegLPStandard = None, max_runtime: int = None):
"""Fit the learner to the given learning problem

(1) Extract multi-hop information about E^+ and E^-.
(2) Create OWL Class Expressions from (1)
(3) Build a binary sparse training data X where
first |E+| rows denote the binary representations of positives
Remaining rows denote the binary representations of E⁻
(4) Create binary labels.
(4) Construct a set of DL concept for each e \in E^+
(5) Union (4)

:param learning_problem: The learning problem
:param max_runtime:total runtime of the learning

"""
assert learning_problem is not None, "Learning problem cannot be None."
assert isinstance(
learning_problem, PosNegLPStandard
), f"Learning problem must be PosNegLPStandard. Currently:{learning_problem}."

if max_runtime is not None:
self.max_runtime = max_runtime

start_time = time.time()
X: pd.DataFrame
y: Union[pd.DataFrame, pd.Series]
X, y = self.create_training_data(learning_problem=learning_problem, start_time=start_time)
# CD: Remember so that if user wants to use them
self.X, self.y = X, y
def _maybe_plot_embeddings(self, X: pd.DataFrame, y: pd.DataFrame) -> None:
"""Plot a UMAP-reduced view of the training features, if enabled. Shared by TDL and TDL_refinement."""
if self.plot_embeddings:
plot_umap_reduced_embeddings(X, y.label.to_list(), "umap_visualization.pdf")

def _fit_decision_tree(self, X: pd.DataFrame, y: pd.DataFrame) -> None:
"""Optionally grid-search classifier hyperparameters, then train self.clf on (X, y).
Shared by TDL and TDL_refinement."""
if self.grid_search_over:
grid_search = sklearn.model_selection.GridSearchCV(
tree.DecisionTreeClassifier(**self.kwargs_classifier),
param_grid=self.grid_search_over, **self.kwargs_grid_search).fit(X.values, y.values)
print(grid_search.best_params_)
self.kwargs_classifier.update(grid_search.best_params_)
# Training
if self.verbose>0:
if self.verbose > 0:
print("Training starts!")
self.clf = tree.DecisionTreeClassifier(**self.kwargs_classifier).fit(X=X.values, y=y.values)

def _report_and_plot(self, X: pd.DataFrame, y: pd.DataFrame) -> None:
"""Report classification metrics and render the configured tree/feature-importance plots.
Shared by TDL and TDL_refinement."""
if self.report_classification:
self.__classification_report = "Classification Report: Negatives: -1 and Positives 1 \n"
self.__classification_report += sklearn.metrics.classification_report(y.values,
Expand All @@ -719,23 +698,61 @@ def fit(self, learning_problem: PosNegLPStandard = None, max_runtime: int = None
plot_topk_feature_importance(feature_names=[owl_expression_to_dl(f) for f in self.features],
cart_tree=self.clf)

def _finalize_predictions(self, X: pd.DataFrame, y: pd.DataFrame) -> None:
"""Build the conjunctive/disjunctive OWL predictions from the trained tree and optionally verbalize them.
Shared by TDL and TDL_refinement."""
self.owl_class_expressions.clear()
# Each item can be considered is a path of OWL Class Expressions
# starting from the root node in the decision tree and
# ending in a leaf node.
self.conjunctive_concepts: List[OWLObjectIntersectionOf]
if self.verbose >0:
if self.verbose > 0:
print("Computing conjunctive_concepts...")
self.conjunctive_concepts = self.construct_owl_expression_from_tree(X, y)
for i in self.conjunctive_concepts:
self.owl_class_expressions.add(i)
if self.verbose >0:
if self.verbose > 0:
print("Computing disjunction_of_conjunctive_concepts...")
self.disjunction_of_conjunctive_concepts = concepts_reducer(concepts=self.conjunctive_concepts, reduced_cls=OWLObjectUnionOf)

if self.verbalize:
verbalize_learner_prediction(self.disjunction_of_conjunctive_concepts)

def fit(self, learning_problem: PosNegLPStandard = None, max_runtime: int = None):
"""Fit the learner to the given learning problem

(1) Extract multi-hop information about E^+ and E^-.
(2) Create OWL Class Expressions from (1)
(3) Build a binary sparse training data X where
first |E+| rows denote the binary representations of positives
Remaining rows denote the binary representations of E⁻
(4) Create binary labels.
(4) Construct a set of DL concept for each e \in E^+
(5) Union (4)

:param learning_problem: The learning problem
:param max_runtime:total runtime of the learning

"""
assert learning_problem is not None, "Learning problem cannot be None."
assert isinstance(
learning_problem, PosNegLPStandard
), f"Learning problem must be PosNegLPStandard. Currently:{learning_problem}."

if max_runtime is not None:
self.max_runtime = max_runtime

start_time = time.time()
X: pd.DataFrame
y: Union[pd.DataFrame, pd.Series]
X, y = self.create_training_data(learning_problem=learning_problem, start_time=start_time)
# CD: Remember so that if user wants to use them
self.X, self.y = X, y
self._maybe_plot_embeddings(X, y)
self._fit_decision_tree(X, y)
self._report_and_plot(X, y)
self._finalize_predictions(X, y)

return self

@property
Expand Down
Loading
Loading