Ejemplo n.º 1
0
    def add_rows(self, matrix_, id2row):
        """
        Adds rows to a peripheral space.
        
        Args:
            matrix_: Matrix type, the matrix of the elements to be added.
            id2row: list, string identifiers of the rows to be added.
            
        Modifies the current space by appending the new rows.
        All operations of the core space are projected to the new rows.
        
        Raises:
            ValueError: if attempting to add row strings which are already 
                        in the space.
                        matrix of the new data is not consistent in shape 
                        with the current data matrix.
        """
        
        try:
            self._row2id = add_items_to_dict(self.row2id, id2row)
        except ValueError:
            raise ValueError("Found duplicate keys when appending rows to\
                            peripheral space.")
        
        if matrix_.mat.shape[0] != len(id2row):
            raise ValueError("Matrix shape inconsistent with no. of rows:%s %s"
                              % (matrix_.mat.shape, len(id2row)))
       
        self._id2row = self.id2row + id2row
        matrix_ = self._project_core_operations(matrix_)

        self._cooccurrence_matrix = self._cooccurrence_matrix.vstack(matrix_)
        assert_shape_consistent(self.cooccurrence_matrix, self.id2row,
                                 self.id2column, self.row2id, self.column2id)        
Ejemplo n.º 2
0
    def add_rows(self, matrix_, id2row):
        """
        Adds rows to a peripheral space.

        Args:
            matrix_: Matrix type, the matrix of the elements to be added.
            id2row: list, string identifiers of the rows to be added.

        Modifies the current space by appending the new rows.
        All operations of the core space are projected to the new rows.

        Raises:
            ValueError: if attempting to add row strings which are already
                        in the space.
                        matrix of the new data is not consistent in shape
                        with the current data matrix.
        """

        try:
            self._row2id = add_items_to_dict(self.row2id, id2row)
        except ValueError:
            raise ValueError("Found duplicate keys when appending rows to\
                            peripheral space.")

        if matrix_.mat.shape[0] != len(id2row):
            raise ValueError("Matrix shape inconsistent with no. of rows:%s %s"
                              % (matrix_.mat.shape, len(id2row)))

        self._id2row = self.id2row + id2row
        matrix_ = self._project_core_operations(matrix_)

        self._cooccurrence_matrix = self._cooccurrence_matrix.vstack(matrix_)
        assert_shape_consistent(self.cooccurrence_matrix, self.id2row,
                                 self.id2column, self.row2id, self.column2id)
Ejemplo n.º 3
0
 def __init__(self, core_space, matrix_, id2row, row2id=None):
     """
     Constructor.
     
     Args:
         core_space: Space type, the core space that this is peripheral to.
         matrix_: Matrix type, the data matrix of the space
         id2row: list, the row elements
         row2id: dictionary, maps row strings to ids. Optional, built from 
             id2row by default.
          
     Returns:
          A peripheral semantic space (type PeripheralSpace) on which the 
          core space operations have been projected. Column indexing structures 
          and operations are taken over from the core space.
     
     Raises:
         TypeError: if matrix_ or core_space are not of the correct type
         ValueError: if element shape is not consistent with 
                      the size of matrix rows
                     if the matrix and the provided row and column 
                      indexing structures are not of consistent shapes.
     """
     assert_is_instance(matrix_, Matrix)
     assert_is_instance(core_space, Space)
     assert_is_instance(id2row, list)
     # TODO: assert it is not a peripheral space here!
     
     if row2id is None:
         row2id = list2dict(id2row)
     else:
         assert_dict_match_list(row2id, id2row)    
         
     column2id = core_space.column2id
     id2column = core_space.id2column
     
     self._operations = list(core_space.operations)    
     self._row2id = row2id
     self._id2row = id2row
     self._column2id = column2id
     self._id2column = id2column
     
     self._cooccurrence_matrix = self._project_core_operations(matrix_)
     assert_shape_consistent(self.cooccurrence_matrix, self._id2row,
                              self._id2column, self._row2id, self._column2id)
     
     self._element_shape = (self._cooccurrence_matrix.shape[1],)
Ejemplo n.º 4
0
    def __init__(self, core_space, matrix_, id2row, row2id=None):
        """
        Constructor.

        Args:
            core_space: Space type, the core space that this is peripheral to.
            matrix_: Matrix type, the data matrix of the space
            id2row: list, the row elements
            row2id: dictionary, maps row strings to ids. Optional, built from
                id2row by default.

        Returns:
             A peripheral semantic space (type PeripheralSpace) on which the
             core space operations have been projected. Column indexing structures
             and operations are taken over from the core space.

        Raises:
            TypeError: if matrix_ or core_space are not of the correct type
            ValueError: if element shape is not consistent with
                         the size of matrix rows
                        if the matrix and the provided row and column
                         indexing structures are not of consistent shapes.
        """
        assert_is_instance(matrix_, Matrix)
        assert_is_instance(core_space, Space)
        assert_is_instance(id2row, list)
        # TODO: assert it is not a peripheral space here!

        if row2id is None:
            row2id = list2dict(id2row)
        else:
            assert_dict_match_list(row2id, id2row)

        column2id = core_space.column2id
        id2column = core_space.id2column

        self._operations = list(core_space.operations)
        self._row2id = row2id
        self._id2row = id2row
        self._column2id = column2id
        self._id2column = id2column

        self._cooccurrence_matrix = self._project_core_operations(matrix_)
        assert_shape_consistent(self.cooccurrence_matrix, self._id2row,
                                 self._id2column, self._row2id, self._column2id)

        self._element_shape = (self._cooccurrence_matrix.shape[1],)
Ejemplo n.º 5
0
    def __init__(self, matrix_, id2row, id2column, row2id=None, column2id=None,
                 **kwargs):
        """
        Constructor.
        
        Args:
            matrix_: Matrix type, the data matrix of the space
            id2row: list, the row elements
            id2column: list, the column elements
            row2id: dictionary, maps row strings to ids. Optional, built from 
                id2row by default.
            column2id: dictionary, maps col strings to ids. Optional, built
                from id2column by default
            operations: list of operations already performed on the input
                matrix, Optional, by default set to empty.
            element_shape: tuple of int, the shape on row elements. Optional, 
                by default row elements are one-dimensional and element_shape is
                (no_cols, ). Used in 3D composition.
             
         Returns:
             A semantic space (type Space)
             
         Raises:
             TypeError: if matrix_ is not of the correct type
             ValueError: if element shape is not consistent with 
                         the size of matrix rows
                         if the matrix and the provided row and column 
                         indexing structures are not of consistent shapes.
                 
        """
        assert_is_instance(matrix_, Matrix)
        assert_valid_kwargs(kwargs, ["operations", "element_shape"])
        assert_is_instance(id2row, list)
        assert_is_instance(id2column, list)
        
        if row2id is None:
            row2id = list2dict(id2row)
        else:    
            assert_dict_match_list(row2id, id2row)
            
        if column2id is None:
            column2id = list2dict(id2column)
        else:
            assert_dict_match_list(column2id, id2column)
            
        assert_shape_consistent(matrix_, id2row, id2column, row2id, column2id)
        
        self._cooccurrence_matrix = matrix_
        self._row2id = row2id
        self._id2row = id2row
        self._column2id = column2id
        self._id2column = id2column
        if "operations" in kwargs:
            self._operations = kwargs["operations"]
        else:
            self._operations = []

        if "element_shape" in kwargs:
            elem_shape = kwargs["element_shape"]
            if prod(elem_shape) != self._cooccurrence_matrix.shape[1]:
                raise ValueError("Trying to assign invalid element shape:\
                                    element_shape: %s, matrix columns: %s" 
                                    % (str(elem_shape), 
                                       str(self._cooccurrence_matrix.shape[1])))
          
        # NOTE: watch out here, can cause bugs, if we change the dimension 
        # of a regular space and we do not create a new space         
            self._element_shape = kwargs["element_shape"]
        else:    
            self._element_shape = (self._cooccurrence_matrix.shape[1],)    
Ejemplo n.º 6
0
 def set_cooccurrence_matrix(self, matrix_):
     assert_is_instance(matrix_, Matrix)
     assert_shape_consistent(matrix_, self.row2id, self.id2row,
                                    self.column2id, self.id2column)
     self._cooccurrence_matrix = matrix_
Ejemplo n.º 7
0
    def __init__(self,
                 matrix_,
                 id2row,
                 id2column,
                 row2id=None,
                 column2id=None,
                 operations=[],
                 element_shape=None):
        """
        Constructor.

        Args:
            matrix_: Matrix type, the data matrix of the space
            id2row: list, the row elements
            id2column: list, the column elements
            row2id: dictionary, maps row strings to ids. Optional, built from
                id2row by default.
            column2id: dictionary, maps col strings to ids. Optional, built
                from id2column by default
            operations: list of operations already performed on the input
                matrix, Optional, by default set to empty.
            element_shape: tuple of int, the shape on row elements. Optional,
                by default row elements are one-dimensional and element_shape is
                (no_cols, ). Used in 3D composition.

         Returns:
             A semantic space (type Space)

         Raises:
             TypeError: if matrix_ is not of the correct type
             ValueError: if element shape is not consistent with
                         the size of matrix rows
                         if the matrix and the provided row and column
                         indexing structures are not of consistent shapes.

        """
        assert_is_instance(matrix_, Matrix)
        assert_is_instance(id2row, list)
        assert_is_instance(id2column, list)

        if row2id is None:
            row2id = list2dict(id2row)
        else:
            assert_dict_match_list(row2id, id2row)

        if column2id is None:
            column2id = list2dict(id2column)
        else:
            assert_dict_match_list(column2id, id2column)

        assert_shape_consistent(matrix_, id2row, id2column, row2id, column2id)

        self._cooccurrence_matrix = matrix_
        self._row2id = row2id
        self._id2row = id2row
        self._column2id = column2id
        self._id2column = id2column
        self._operations = operations

        if element_shape:
            if prod(element_shape) != self._cooccurrence_matrix.shape[1]:
                raise ValueError("Trying to assign invalid element shape:\
                                    element_shape: %s, matrix columns: %s" %
                                 (str(element_shape),
                                  str(self._cooccurrence_matrix.shape[1])))

        # NOTE: watch out here, can cause bugs, if we change the dimension
        # of a regular space and we do not create a new space
            self._element_shape = element_shape
        else:
            self._element_shape = (self._cooccurrence_matrix.shape[1], )
Ejemplo n.º 8
0
 def set_cooccurrence_matrix(self, matrix_):
     assert_is_instance(matrix_, Matrix)
     assert_shape_consistent(matrix_, self.row2id, self.id2row,
                             self.column2id, self.id2column)
     self._cooccurrence_matrix = matrix_