def test_random_visualization_plot_feature(self): """`visualization.Visualization.plot_feature`: Randomized Validator. Tests the behavior of `Visualization.plot_feature` by feeding it randomly generated arguments. Raises: AssertionError: If `Visualization.plot_feature` needs debugging. """ for i in range(self.n_tests): for ModelWrapper in self.wrappers.values(): X = _random_matrix(self.data_shape) """np.matrix: Random-valued feature set.""" Y = _random_matrix((self.data_shape[0], 1)) """np.matrix: Random-valued observation set.""" v = Visualization("Title", (3, 3)) """Visualization: Plotter instance.""" # Intialize model parameters to random values. ModelWrapper.model = dict(X=X) for i in range(9): x, y, error = v._plot_feature(X, Y, i, ModelWrapper) """(list of float, :obj:`list of float`): X- and y-values to plot.""" # `x` should be a list of floats. self.assertIsInstance(x, list) map(_appendargs(self.assertIsInstance, float), x) # Number of `x` values should match number of data points in # `X`. self.assertEqual(len(x), X.shape[0]) # `x` values should match all values in `X`. self.assertEqual(*map(_np.linalg.norm, [x, X[:, i]])) # `y` should be a dict. self.assertIsInstance(y, dict) for j, values in _compose(enumerate, y.values)(): # `values` should be a list of floats. self.assertIsInstance(values, list) map(_appendargs(self.assertIsInstance, float), values) # Number of values in `values` should match number of # data points in `Y`. self.assertEqual(len(values), Y.shape[0]) if j == 0: # Observation values should match all values in `Y`. self.assertEqual( *map(_np.linalg.norm, [values, Y[:, 0]])) v.close()
def test_edge_cases_visualization_plot_feature(self): """`visualization.Visualization.plot_feature`: Edge Case Validator. Tests the behavior of `Visualization.plot_feature` with edge cases. Raises: Exception: If at least one `Exception` raised is not of the expected kind. """ X = _random_matrix(self.data_shape) """np.matrix: Random-valued feature set.""" Y = _random_matrix((self.data_shape[0], 1)) """np.matrix: Random-valued observation set.""" v = Visualization("Title", (1, 1)) """Visualization: Plotter instance.""" for ModelWrapper in self.wrappers.values(): with self.assertRaises(_InvalidFeatureSetError): # Empty feature set. v._plot_feature(_np.matrix([[]]), Y, 0, ModelWrapper) with self.assertRaises(_InvalidObservationSetError): # Empty observation set. v._plot_feature(X, _np.matrix([[]]), 0, ModelWrapper) with self.assertRaises(IndexError): # Feature index out of range. v._plot_feature(X, Y, self.data_shape[1], ModelWrapper) v.close()
def test_invalid_args_visualization_plot_feature(self): """`visualization.Visualization.plot_feature`: Argument Validator. Tests the behavior of `Visualization.plot_feature` with invalid argument counts and values. Raises: Exception: If at least one `Exception` raised is not of the expected kind. """ X = _random_matrix(self.data_shape) """np.matrix: Random-valued feature set.""" Y = _random_matrix((self.data_shape[0], 1)) """np.matrix: Random-valued observation set.""" v = Visualization("Title", (1, 1)) """Visualization: Plotter instance.""" with self.assertRaises(TypeError): # No arguments. v._plot_feature() with self.assertRaises(TypeError): # Only one argument. v._plot_feature(X) with self.assertRaises(TypeError): # Only two arguments. v._plot_feature(X, Y) with self.assertRaises(TypeError): # Only three arguments. v._plot_feature(X, Y, 0) with self.assertRaises(TypeError): # `None` instead of model wrapper. v._plot_feature(X, Y, 0, None) with self.assertRaises(TypeError): # `Visualization` instead of `ModelWrapper`. v._plot_feature(X, Y, 0, v) for ModelWrapper in self.wrappers.values(): with self.assertRaises(TypeError): # Too many arguments. v._plot_feature(X, Y, 0, ModelWrapper, "extra") with self.assertRaises(TypeError): # With keyword. v._plot_feature(X, Y, 0, ModelWrapper, key="value") with self.assertRaises(_InvalidFeatureSetError): # Non-matrix feature set `X`. v._plot_feature(None, Y, 0, ModelWrapper) with self.assertRaises(_InvalidFeatureSetError): # ndarray instead of feature set `X`. v._plot_feature(_np.zeros(self.data_shape), Y, 0, ModelWrapper) with self.assertRaises(_InvalidObservationSetError): # Non-matrix feature set `X`. v._plot_feature(X, None, 0, ModelWrapper) with self.assertRaises(_InvalidObservationSetError): # ndarray instead of feature set `X`. v._plot_feature(X, _np.zeros((self.data_shape[0], 1)), 0, ModelWrapper) with self.assertRaises(_IncompatibleDataSetsError): # Incompatible datasets. v._plot_feature(X, _random_matrix((self.data_shape[0] + 1, 1)), 0, ModelWrapper) with self.assertRaises(TypeError): # Non-integer index `feature`. v._plot_feature(X, Y, None, ModelWrapper) with self.assertRaises(ValueError): # Negative integer instead of `feature`. v._plot_feature(X, Y, -2, ModelWrapper) v.close()