Skip to content

UnboundLocalError when req_clust exactly matches a partition size #38

Description

@saveli

Bug

FINCH() raises UnboundLocalError: cannot access local variable 'requested_c' where it is not associated with a value when req_clust is not None and the requested number of clusters is already exactly present in num_clust.

Root cause

In finch.py (lines 363–373 in v0.2.2), the req_clust not in num_clust branch only handles the mismatch cases (too large, or needs sub-clustering). When req_clust is an exact match, the entire inner block is skipped and requested_c is never assigned before the return statement.

if req_clust is not None:
    if req_clust not in num_clust:          # False when exact match → block skipped
        if req_clust > num_clust[0]:
            requested_c = c[:, 0]
        else:
            requested_c = req_numclust(...)
    # no else → requested_c undefined when req_clust in num_clust
else:
    requested_c = None

return c, num_clust, requested_c           # UnboundLocalError

Fix

Add an else branch to handle the exact-match case:

if req_clust is not None:
    if req_clust not in num_clust:
        if req_clust > num_clust[0]:
            requested_c = c[:, 0]
        else:
            requested_c = req_numclust(...)
    else:
        requested_c = c[:, num_clust.index(req_clust)]
else:
    requested_c = None

Reproduction

Any dataset where FINCH produces a partition with exactly req_clust clusters will trigger this. Confirmed on v0.2.2 (latest).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions