def test_predict_before_fit(self): # predict before fit assert_raises_message( ValueError, "Estimator is not fit. Call VertexFrequencyCluster.fit().", meld.VertexFrequencyCluster().predict, )
def test_sample_indicator_invalid(self): # sample_indicator not array-like assert_raises_message( TypeError, "`sample_indicator` must be array-like", meld.VertexFrequencyCluster().fit_transform, G=self.G, sample_indicator="invalid", )
def test_transform_before_fit(self): # Transform before fit assert_raises_message( ValueError, "Estimator must be `fit` before running `transform`.", meld.VertexFrequencyCluster().transform, sample_indicator=self.sample_indicators["expt"], likelihood=self.likelihoods["expt"], )
def test_likelihood_wrong_length(self): # likelihood and n mismatch assert_raises_message( ValueError, "At least one axis of `likelihood` must be of length `N`.", meld.VertexFrequencyCluster().fit_transform, G=self.G, sample_indicator=self.sample_indicators["expt"], likelihood=np.ones(7), )
def test_likelihood_invalid(self): # likelihood not array-like assert_raises_message( TypeError, "`likelihood` must be array-like", meld.VertexFrequencyCluster().fit_transform, G=self.G, sample_indicator=self.sample_indicators["expt"], likelihood="invalid", )
def test_predict_before_transform(self): vfc_op = meld.VertexFrequencyCluster(window_sizes=self.window_sizes) vfc_op.fit(self.G) # predict before transform assert_raises_message( ValueError, "Estimator is not transformed. " "Call VertexFrequencyCluster.transform().", vfc_op.predict, )
def test_check_pygsp_graph(): # _check_pygsp_graph data = np.random.normal(0, 2, (10, 2)) G = gt.Graph(data, use_pygsp=False) assert isinstance(meld.utils._check_pygsp_graph(G), pygsp.graphs.Graph) assert_raises_message( TypeError, "Input graph should be of type graphtools.base.BaseGraph. " "With graphtools, use the `use_pygsp=True` flag.", meld.utils._check_pygsp_graph, G="hello world", )
def test_sample_indicator_likelihood_shape(self): vfc_op = meld.VertexFrequencyCluster(window_sizes=self.window_sizes) assert_raises_message( ValueError, "`sample_indicator` and `likelihood` must have the same shape. " "Got sample_indicator: {} and likelihood: {}".format( str(self.sample_indicators["expt"].shape), str(self.likelihoods.shape)), vfc_op.fit_predict, G=self.G, sample_indicator=self.sample_indicators["expt"], likelihood=self.likelihoods, )
def test_benchmarker_set_params(): benchmarker = meld.Benchmarker() benchmarker.set_seed(0) with assert_raises_message(ValueError, "data_phate must have 3 dimensions"): benchmarker.set_phate(np.random.normal(0, 2, (10, 2)))
def test_benchmarker_calc_pdf_before_fit_phate(): benchmarker = meld.Benchmarker() with assert_raises_message( ValueError, "data_phate must be set prior to running generate_ground_truth_pdf().", ): benchmarker.generate_ground_truth_pdf()
def test_sample_labels_2d(): labels = np.ones((10, 2)) with assert_raises_message( ValueError, "sample_labels must be a single column. Got" "shape={}".format(labels.shape), ): meld.MELD()._create_sample_indicators(labels)
def test_sample_labels_one_sample(): data = np.random.normal(size=(100, 2)) labels = np.ones(100) with assert_raises_message( ValueError, "Found only one unqiue sample label. Cannot estimate density " "of a single sample.", ): meld.MELD().fit_transform(data, labels)
def test_meld_invalid_lap_type(): data = np.random.normal(0, 2, (1000, 2)) # lap type TypeError lap_type = "hello world" with assert_raises_message( ValueError, "lap_type value {} not recognized. " "Choose from ['combinatorial', 'normalized']".format(lap_type), ): meld.MELD(verbose=0, lap_type=lap_type).fit(data)
def test_meld_labels_wrong_shape(): data = np.random.normal(0, 2, (100, 2)) # sample_indicator wrong shape sample_labels = np.ones([101, 2], dtype=str) with assert_raises_message( ValueError, "Input data ({}) and input graph ({}) " "are not of the same size".format(sample_labels.shape, data.shape[0]), ): meld.MELD(verbose=0).fit_transform( X=data, sample_labels=sample_labels, )
def test_cluster(self): vfc_op = meld.VertexFrequencyCluster(window_sizes=self.window_sizes) spectrogram = vfc_op.fit_transform( self.G, sample_indicator=self.sample_indicators["expt"], likelihood=self.likelihoods["expt"], ) # test sparse window for t in self.window_sizes: np.testing.assert_allclose( vfc_op._compute_window(self.G.diff_op, t).toarray(), vfc_op._compute_window(self.G.diff_op.toarray(), t), ) # test sparse spectrogram for window in vfc_op.windows: np.testing.assert_allclose( vfc_op._compute_spectrogram(self.sample_indicators["expt"], window), vfc_op._compute_spectrogram(self.sample_indicators["expt"], sparse.csr_matrix(window)), ) # test full sparse computation vfc_op.sparse = True sparse_spectrogram = vfc_op.fit_transform( self.G, sample_indicator=self.sample_indicators["expt"], likelihood=self.likelihoods["expt"], ) assert sparse_spectrogram.shape == spectrogram.shape assert sparse.issparse(vfc_op._basewindow) # test _compute_spectrogram wrong size signal with assert_raises_message( ValueError, "sample_indicator must be 1-dimensional. Got shape: {}".format( self.data.shape), ): vfc_op._compute_spectrogram(self.data, window)
def test_benchmarker_calc_MELD_without_graph_or_data(): benchmarker = meld.Benchmarker() with assert_raises_message( NameError, "Must pass `data` unless graph has already been fit"): benchmarker.calculate_MELD_likelihood()