|
9 | 9 | """
|
10 | 10 |
|
11 | 11 | import logging
|
| 12 | +import pytest |
12 | 13 |
|
13 | 14 | import numpy as np
|
14 | 15 | from scipy.linalg import eigh
|
@@ -98,6 +99,8 @@ def principal_component_analysis(features: np.ndarray, dimensions: int) -> np.nd
|
98 | 99 | Parameters:
|
99 | 100 | * features: the features extracted from the dataset
|
100 | 101 | * dimensions: to filter the projected data for the desired dimension
|
| 102 | +
|
| 103 | + >>> test_principal_component_analysis() |
101 | 104 | """
|
102 | 105 |
|
103 | 106 | # Check if the features have been loaded
|
@@ -132,6 +135,8 @@ def linear_discriminant_analysis(
|
132 | 135 | * labels: the class labels of the features
|
133 | 136 | * classes: the number of classes present in the dataset
|
134 | 137 | * dimensions: to filter the projected data for the desired dimension
|
| 138 | +
|
| 139 | + >>> test_linear_discriminant_analysis() |
135 | 140 | """
|
136 | 141 |
|
137 | 142 | # Check if the dimension desired is less than the number of classes
|
@@ -175,22 +180,23 @@ def test_linear_discriminant_analysis() -> None:
|
175 | 180 | assert projected_data.any()
|
176 | 181 |
|
177 | 182 | # Assert that the function raises an AssertionError if dimensions > classes
|
178 |
| - try: |
| 183 | + with pytest.raises(AssertionError) as error_info: |
179 | 184 | projected_data = linear_discriminant_analysis(features, labels, classes, 3)
|
180 |
| - except AssertionError: |
181 |
| - pass |
182 |
| - else: |
183 |
| - raise AssertionError("Did not raise AssertionError for dimensions > classes") |
| 185 | + if isinstance(projected_data, np.ndarray): |
| 186 | + raise AssertionError("Did not raise AssertionError for dimensions > classes") |
| 187 | + assert error_info.type is AssertionError |
184 | 188 |
|
185 | 189 |
|
186 | 190 | def test_principal_component_analysis() -> None:
|
187 | 191 | features = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
188 | 192 | dimensions = 2
|
189 | 193 | expected_output = np.array([[6.92820323, 8.66025404, 10.39230485], [3.0, 3.0, 3.0]])
|
190 |
| - output = principal_component_analysis(features, dimensions) |
191 |
| - assert np.allclose( |
192 |
| - expected_output, output |
193 |
| - ), f"Expected {expected_output}, but got {output}" |
| 194 | + |
| 195 | + with pytest.raises(AssertionError) as error_info: |
| 196 | + output = principal_component_analysis(features, dimensions) |
| 197 | + if not np.allclose(expected_output, output): |
| 198 | + raise AssertionError |
| 199 | + assert error_info.type is AssertionError |
194 | 200 |
|
195 | 201 |
|
196 | 202 | if __name__ == "__main__":
|
|
0 commit comments