Repository navigation
Conversation
sohailshk
requested review from
ashwinvaidya17,
rajeshgangireddy and
samet-akcay
as code owners
November 13, 2026 06:39
sohailshk
added a commit
to sohailshk/anomalib
that referenced
this pull request
Nov 13, 2026
…ructor dataset GaussianKDE.cov centered the fitted tensor in place, so densities were evaluated against centered training data but uncentered queries, and the constructor fitted before registering the buffers, which raised a KeyError. Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
sohailshk
force-pushed
the
fix/gaussian-kde-fit
branch
from
November 13, 2026 16:39
7e62d07 to
53e1028
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Description
Two bugs in
anomalib.models.components.stats.GaussianKDE:GaussianKDE.covdoestensor -= mean[:, None]ondataset.T, which is a view of the dataset. Sofitcenters the caller's tensor in place and stores centered training points, while the queries passed toforwardare not centered. With the test data (200 samples of a 3-d Gaussian with mean (4, -2, 1), queries = the first five samples + 0.1), the densities are[1.3e-07, 9.0e-07, 2.5e-15, 7.1e-05, 3.1e-09]on main and[0.0250, 0.0307, 0.0107, 0.0274, 0.0148]fromscipy.stats.gaussian_kde(same Scott's-rule bandwidth). With this fix they match scipy within a relative 1.5e-7. Data that is only slightly off-center is affected too: for 100 standard-normal 2-d samples (sample mean (-0.018, -0.027)) the densities at the first five samples are up to 3.6% off on main.GaussianKDE(dataset=x), the one-step usage in the class docstring, callsfitbefore the buffers are registered.fitassigns plain attributes, so the followingregister_bufferraisesKeyError: "attribute 'bw_transform' already exists".The fix makes
covnon-mutating and fits after the buffers are registered.forwardis unchanged, so saved models give the same scores as before.Effect on DFKDE: with the default
feature_scaling_method="scale", the KDE input is zero-mean PCA output, so scores do not change (max log-density difference 1.9e-6 on synthetic embeddings). Withfeature_scaling_method="norm", each row is rescaled after PCA, so the input mean is no longer zero and scores change slightly (max log-density difference 0.095 on the same embeddings).Tests: the density equals a reference KDE (Scott's rule) for off-center data,
fitleaves its input unchanged, and the constructor dataset gives the same model asfit. All three fail on main.✨ Changes
✅ Checklist
Checks run:
pytest tests/unit/models/components/stats(3 passed; all 3 fail on main),ruff checkandruff format --check(0.12.0),mypy(1.16.1) on kde.py. Not run: the full suite, DFKDE integration tests.