Skip to content

Warn when cluster studio sample_size exceeds available clusters (#1026) - #3227

Open
Aleksandrs Drozdovs (aleks-drozy) wants to merge 1 commit into
moj-analytical-services:masterfrom
aleks-drozy:explore/1026-cluster-studio-sample-warning
Open

Warn when cluster studio sample_size exceeds available clusters (#1026)#3227
Aleksandrs Drozdovs (aleks-drozy) wants to merge 1 commit into
moj-analytical-services:masterfrom
aleks-drozy:explore/1026-cluster-studio-sample-warning

Conversation

@aleks-drozy

Copy link
Copy Markdown

Addresses #1026.

The original DuckDB crash reported in this issue (Sample sample_size 133.3 out of range) looks like it's already fixed on masterSplinkDialect.proportion_sample_sql now returns an empty WHERE filter for proportion >= 1.0, so it no longer errors when sample_size exceeds the number of available clusters.

What's still missing, and what this PR adds, is the actual feedback the issue asks for: previously that case silently returned all clusters with no indication anything was off. Now:

  • _get_random_cluster_ids logs a warning naming the requested sample_size, the actual number of distinct clusters, and notes this can indicate an over-low clustering threshold or an overly "matchy" model — then clamps proportion to min(sample_size / cluster_count, 1.0) so the intent is explicit locally rather than relying on the dialect-level guard.
  • The same silent-shortfall case in _get_cluster_ids's by_cluster_size / lowest_density_clusters_by_size branches (which also returned fewer clusters than requested with no signal) gets a matching warning.

Testing: added test_random_sample_size_exceeds_cluster_count_warns and test_random_sample_size_within_cluster_count_no_warning to tests/test_cluster_studio.py, covering both the warning and the "no false-positive warning" case. Existing test_density_sample untouched. ruff check/format and mypy both clean.

I noticed the issue still carries a several-years-old self-assignment — flagging that here in case it's stale and someone else should take a look, happy to have this superseded if there's already work in progress I didn't find.

…analytical-services#1026)

The DuckDB "Sample sample_size ... out of range" crash from moj-analytical-services#1026 is
already fixed on master via SplinkDialect.proportion_sample_sql
returning an empty filter for proportion >= 1.0. What was still
missing, and what the issue explicitly asks for, is user-facing
feedback: previously an oversized sample_size would silently return
every cluster with no explanation, which can also mask a too-low
clustering threshold or an overly-matchy model.

- Add a module logger to cluster_studio.py (matching the convention
  in expectation_maximisation.py).
- In _get_random_cluster_ids, warn when sample_size >= cluster_count
  and clamp proportion to at most 1.0 so intent is explicit locally.
- Mirror a warning in the by_cluster_size and
  lowest_density_clusters_by_size branches of _get_cluster_ids, which
  silently return fewer clusters than requested in the same situation.
- Add regression tests covering the warning (and its absence when
  sample_size is satisfiable) for _get_random_cluster_ids.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant