From 1bee9c5442a118b39fb8addeef3884db64ed317b Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 27 Dec 2025 23:04:55 +0530 Subject: [PATCH 1/2] Fix reconstruct: throw ArgumentError on invalid input type --- docs/src/pca.md | 11 +++++++++++ src/types.jl | 8 +++++++- test/pca.jl | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/src/pca.md b/docs/src/pca.md index 6805e33..d50cf82 100644 --- a/docs/src/pca.md +++ b/docs/src/pca.md @@ -117,6 +117,10 @@ predict(::KernelPCA) predict(::KernelPCA, ::AbstractVecOrMat{T}) where {T<:Real} reconstruct(::KernelPCA, ::AbstractVecOrMat{T}) where {T<:Real} size(::KernelPCA) +``` +!!! note + If `reconstruct` is called with an input that is not an `AbstractVecOrMat` of real numbers, an `ArgumentError` is thrown indicating the invalid input type and the expected type. +``` projection(::KernelPCA) eigvals(::KernelPCA) eigvecs(::KernelPCA) @@ -164,6 +168,10 @@ fit size(::PPCA) mean(::PPCA) var(::PPCA) +``` +!!! note + If `reconstruct` is called with an input that is not an `AbstractVecOrMat` of real numbers, an `ArgumentError` is thrown indicating the invalid input type and the expected type. +``` cov(::PPCA) projection(::PPCA) loadings(::PPCA) @@ -188,6 +196,9 @@ Here, ``\mathbf{W}`` is the factor loadings or weight matrix. predict(::PPCA, ::AbstractVecOrMat{T}) where {T<:Real} reconstruct(::PPCA, ::AbstractVecOrMat{T}) where {T<:Real} ``` +``` +!!! note + If `reconstruct` is called with an input that is not an `AbstractVecOrMat` of real numbers, an `ArgumentError` is thrown indicating the invalid input type and the expected type. Auxiliary functions: diff --git a/src/types.jl b/src/types.jl index dbaecb1..012286e 100644 --- a/src/types.jl +++ b/src/types.jl @@ -23,8 +23,14 @@ projection(model::AbstractDimensionalityReduction) = error("'projection' is not reconstruct(model::AbstractDimensionalityReduction, y) Return the model response (a.k.a. the dependent variable). +Throws an `ArgumentError` if `y` is not an `AbstractVecOrMat` of real numbers, indicating the invalid +input type and the expected type. """ -reconstruct(model::AbstractDimensionalityReduction, y) = error("'reconstruct' is not defined for $(typeof(model)).") +function reconstruct(model::AbstractDimensionalityReduction, y) + throw(ArgumentError("Invalid input type $(typeof(y)) for reconstruct(::$(typeof(model))). " * + "Expected an AbstractVecOrMat of real numbers." + )) +end abstract type LinearDimensionalityReduction <: AbstractDimensionalityReduction end diff --git a/test/pca.jl b/test/pca.jl index 865b965..4d5bfe5 100644 --- a/test/pca.jl +++ b/test/pca.jl @@ -43,6 +43,12 @@ import SparseArrays @test reconstruct(M, Y[:,1]) ≈ P * Y[:,1] @test reconstruct(M, Y) ≈ P * Y + @test_throws ArgumentError reconstruct(M, 123) + @test_throws ArgumentError reconstruct(M, Dict()) + @test_throws ArgumentError reconstruct(M, "abc") + @test_throws ArgumentError reconstruct(M, 8923.29) + @test_throws ArgumentError reconstruct(M, 1+2im) + ## PCA with non-zero mean From 62dfcfff5a89a58d5131f0691f720fba76f44c1c Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 28 Dec 2025 00:05:32 +0530 Subject: [PATCH 2/2] Update CI workflow: use actions/cache@v3 to fix deprecation error --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 644faa5..b85578e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,7 +36,7 @@ jobs: with: version: ${{ matrix.version }} arch: ${{ matrix.arch }} - - uses: actions/cache@v1 + - uses: actions/cache@v3 env: cache-name: cache-artifacts with: