Skip to content

MultipleSeries.pca(): raise on unequal lengths instead of returning None - #705

Open
vaibhav8a wants to merge 1 commit into
LinkedEarth:masterfrom
vaibhav8a:fix/pca-unequal-lengths-raise
Open

MultipleSeries.pca(): raise on unequal lengths instead of returning None#705
vaibhav8a wants to merge 1 commit into
LinkedEarth:masterfrom
vaibhav8a:fix/pca-unequal-lengths-raise

Conversation

@vaibhav8a

Copy link
Copy Markdown

Fixes #702.

Problem

MultipleSeries.pca() guarded unequal series lengths with a bare print() and then fell off the end of the function:

flag, lengths = self.equal_lengths()

if flag==False:
    print('All Time Series should be of same length. Apply common_time() first')
else: # if all series have equal length
    ...
    return res

So the caller got None back. The failure surfaced later as AttributeError: 'NoneType' object has no attribute 'screeplot' — far from the actual cause — and the message itself couldn't be filtered, captured by logging, or suppressed the way warnings.warn can.

Change

Raise a ValueError naming common_time() as the fix, and include the lengths that disagreed:

if flag == False:
    raise ValueError(
        "All series must be of the same length to perform PCA; "
        f"got lengths {lengths}. Apply common_time() first."
    )

The else branch is untouched, so the equal-length path is byte-for-byte the same.

Testing

Added TestMultipleSeriesPca::test_pca_t4, which builds a MultipleSeries of unequal length and asserts a ValueError mentioning common_time. Confirmed it guards the change: with the multipleseries.py hunk stashed the test fails with Failed: DID NOT RAISE ValueError, and passes with it applied.

pytest pyleoclim/tests/test_core_MultipleSeries.py — 100 passed.

Scoped to pca() only. The other sites in the #697 survey depend on the unified-policy decision that's still open, so I left them alone; this one is the case #702 calls out as a bug in its own right regardless of how that lands.

pca() printed a plain string and fell off the end of the function when the
series had different lengths, so callers silently got None back and failed
later with an AttributeError far from the cause. Raise a ValueError naming
common_time() as the fix, and report the offending lengths.

Fixes LinkedEarth#702
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.

MultipleSeries.pca(): silently returns None instead of raising when series lengths differ

1 participant