Repository: lisa-lab/pylearn2

Issue:
Make DenseDesignMatrixPyTables use x_labels and y_labels
Its constructor should accept keywords for its parent contructor.



Discussion:

Resolving patch:
diff --git a/pylearn2/datasets/dense_design_matrix.py b/pylearn2/datasets/dense_design_matrix.py
--- a/pylearn2/datasets/dense_design_matrix.py
+++ b/pylearn2/datasets/dense_design_matrix.py
@@ -1126,6 +1126,16 @@ class DenseDesignMatrixPyTables(DenseDesignMatrix):
     rng : object, optional
         A random number generator used for picking random indices into the
         design matrix when choosing minibatches.
+    X_labels : int, optional
+        If X contains labels then X_labels must be passed to indicate the
+        total number of possible labels e.g. the size of a the vocabulary
+        when X contains word indices. This will make the set use
+        IndexSpace.
+    y_labels : int, optional
+        If y contains labels then y_labels must be passed to indicate the
+        total number of possible labels e.g. 10 for the MNIST dataset
+        where the targets are numbers. This will make the set use
+        IndexSpace.
     """
 
     _default_seed = (17, 2, 946)
@@ -1136,18 +1146,34 @@ def __init__(self,
                  y=None,
                  view_converter=None,
                  axes=('b', 0, 1, 'c'),
-                 rng=_default_seed):
+                 rng=_default_seed,
+                 X_labels=None,
+                 y_labels=None):
         super_self = super(DenseDesignMatrixPyTables, self)
         super_self.__init__(X=X,
                             topo_view=topo_view,
                             y=y,
                             view_converter=view_converter,
                             axes=axes,
-                            rng=rng)
+                            rng=rng,
+                            X_labels=X_labels,
+                            y_labels=y_labels)
+        self._check_labels()
         ensure_tables()
         if not hasattr(self, 'filters'):
             self.filters = tables.Filters(complib='blosc', complevel=5)
 
+    def _check_labels(self):
+        """Sanity checks for X_labels and y_labels."""
+        if self.X_labels is not None:
+            assert self.X is not None
+            assert self.view_converter is None
+            assert self.X.ndim <= 2
+
+        if self.y_labels is not None:
+            assert self.y is not None
+            assert self.y.ndim <= 2
+
     def set_design_matrix(self, X, start=0):
         """
         .. todo::


Test patch:
diff --git a/pylearn2/datasets/tests/test_dense_design_matrix.py b/pylearn2/datasets/tests/test_dense_design_matrix.py
--- a/pylearn2/datasets/tests/test_dense_design_matrix.py
+++ b/pylearn2/datasets/tests/test_dense_design_matrix.py
@@ -78,6 +78,23 @@ def test_pytables():
     it.next()
 
 
+def test_init_pytables_with_labels():
+    """
+    Test whether DenseDesignMatrixPytables can be constructed with X_labels
+    and y_labels.
+    """
+
+    rng = np.random.RandomState([34, 22, 89])
+    X = rng.randn(2, 3)
+    y = rng.randint(low=0, high=5, size=(2,))
+    ds = DenseDesignMatrixPyTables(
+        X=X,
+        y=y,
+        X_labels=len(np.unique(X).flat),
+        y_labels=np.max(y)+1
+    )
+
+
 def test_from_dataset():
     """
     Tests whether it supports integer labels.