Repository: graspologic-org/graspologic

Issue:
check that input graph is same size as P matrix when using score in the model selection code



Discussion:
https://github.com/neurodata/graspy/blob/065eccfbe585304275f72ae3e6f65f929cb26095/graspy/models/base.py#L83

in the function `score_sample`, the user inputs a graph to check whether the likelihood of the input graph given the fit model. As currently implemented, this only works when the input graph is the same size as `p_mat_`. There should be an input check that throws an error if input graph and `p_mat_` are not the same size. 

Resolving patch:
diff --git a/graspy/models/base.py b/graspy/models/base.py
--- a/graspy/models/base.py
+++ b/graspy/models/base.py
@@ -90,7 +90,7 @@ def score_samples(self, graph, clip=None):
         Parameters
         ----------
         graph : np.ndarray
-            input graph
+            Input graph. Must be same shape as model's ``p_mat_`` attribute
         
         clip : scalar or None, optional (default=None)
             Values for which to clip probability matrix, entries less than c or more
@@ -110,6 +110,9 @@ def score_samples(self, graph, clip=None):
             raise ValueError("Model only implemented for unweighted graphs")
         p_mat = self.p_mat_.copy()
 
+        if np.shape(p_mat) != np.shape(graph):
+            raise ValueError("Input graph size must be the same size as P matrix")
+
         inds = None
         if not self.directed and self.loops:
             inds = np.triu_indices_from(graph)  # ignore lower half of graph, symmetric
@@ -147,7 +150,7 @@ def score(self, graph):
         Parameters
         ----------
         graph : np.ndarray
-            input graph
+            Input graph. Must be same shape as model's ``p_mat_`` attribute
 
         Returns
         -------


Test patch:
diff --git a/tests/test_models.py b/tests/test_models.py
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -62,6 +62,9 @@ def test_ER_score(self):
         estimator = EREstimator(directed=False)
         _test_score(estimator, p_mat, graph)
 
+        with pytest.raises(ValueError):
+            estimator.score_samples(graph=er_np(500, 0.5))
+
     def test_ER_nparams(self):
         assert self.estimator._n_parameters() == 1
 
@@ -86,6 +89,9 @@ def test_DCER_score(self):
         estimator = DCEREstimator()
         _test_score(estimator, p_mat, graph)
 
+        with pytest.raises(ValueError):
+            estimator.score_samples(graph=graph[1:500, 1:500])
+
     def test_DCER_inputs(self):
         with pytest.raises(TypeError):
             DCEREstimator(directed="hey")
@@ -262,6 +268,9 @@ def test_SBM_score(self):
         estimator = SBMEstimator(max_comm=4)
         _test_score(estimator, p_mat, graph)
 
+        with pytest.raises(ValueError):
+            estimator.score_samples(graph=graph[1:100, 1:100])
+
     def test_SBM_nparams(self):
         e = self.estimator.fit(self.graph, y=self.labels)
         assert e._n_parameters() == (4)
@@ -302,6 +311,9 @@ def test_DCSBM_score(self):
         estimator = DCSBMEstimator()
         _test_score(estimator, p_mat, graph)
 
+        with pytest.raises(ValueError):
+            estimator.score_samples(graph=graph[1:100, 1:100])
+
     def test_DCSBM_fit_supervised(self):
         p_mat = self.p_mat
         labels = self.labels
@@ -491,6 +503,9 @@ def test_RDPG_score(self):
         estimator = RDPGEstimator()
         _test_score(estimator, p_mat, graph)
 
+        with pytest.raises(ValueError):
+            estimator.score_samples(graph=graph[1:100, 1:100])
+
     def test_RDPG_nparams(self):
         n_verts = 1000
         g = self.graph