Exemplo 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)        
Exemplo 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)
Exemplo n.º 3
0
    def vstack(cls, space1, space2):
        """
        Classmethod. Stacks two semantic spaces.

        The rows in the two spaces are concatenated.

        Args:
            space1, space2: spaces to be stacked, of type Space

        Returns:
            Stacked space, type Space.

        Raises:
            ValueError: if the spaces have different number of columns
                        or their columns are not identical

        """
        if space1.cooccurrence_matrix.shape[
                1] != space2.cooccurrence_matrix.shape[1]:
            raise ValueError("Inconsistent shapes: %s, %s" %
                             (space1.cooccurrence_matrix.shape[1],
                              space2.cooccurrence_matrix.shape[1]))

        if space1.id2column != space2.id2column:
            raise ValueError("Identical columns required")

        new_row2id = add_items_to_dict(space1.row2id.copy(), space2.id2row)
        new_id2row = space1.id2row + space2.id2row

        matrix_type = get_type_of_largest(
            [space1.cooccurrence_matrix, space2.cooccurrence_matrix])
        [new_mat1, new_mat2] = resolve_type_conflict(
            [space1.cooccurrence_matrix, space2.cooccurrence_matrix],
            matrix_type)

        new_mat = new_mat1.vstack(new_mat2)

        log.print_info(logger, 1, "\nVertical stack of two spaces")
        log.print_matrix_info(logger, space1.cooccurrence_matrix, 2,
                              "Semantic space 1:")
        log.print_matrix_info(logger, space2.cooccurrence_matrix, 2,
                              "Semantic space 2:")
        log.print_matrix_info(logger, new_mat, 2, "Resulted semantic space:")

        return Space(new_mat,
                     new_id2row,
                     list(space1.id2column),
                     new_row2id,
                     space1.column2id.copy(),
                     operations=[])
Exemplo n.º 4
0
 def vstack(cls, space1, space2):
     """
     Classmethod. Stacks two semantic spaces.
     
     The rows in the two spaces are concatenated.
         
     Args:
         space1, space2: spaces to be stacked, of type Space
         
     Returns:
         Stacked space, type Space.
         
     Raises:
         ValueError: if the spaces have different number of columns
                     or their columns are not identical
         
     """
     if space1.cooccurrence_matrix.shape[1] != space2.cooccurrence_matrix.shape[1]:
         raise ValueError("Inconsistent shapes: %s, %s" 
                          % (space1.cooccurrence_matrix.shape[1], 
                             space2.cooccurrence_matrix.shape[1]))
     
     if space1.id2column != space2.id2column:
         raise ValueError("Identical columns required")
     
     new_row2id = add_items_to_dict(space1.row2id.copy(), space2.id2row)
     new_id2row = space1.id2row + space2.id2row
     
     matrix_type = get_type_of_largest([space1.cooccurrence_matrix,
                                        space2.cooccurrence_matrix])
     [new_mat1, new_mat2] = resolve_type_conflict([space1.cooccurrence_matrix, 
                                                   space2.cooccurrence_matrix],
                                                  matrix_type)
     
     new_mat = new_mat1.vstack(new_mat2)
     
     log.print_info(logger, 1, "\nVertical stack of two spaces")
     log.print_matrix_info(logger, space1.cooccurrence_matrix, 2, 
                           "Semantic space 1:")
     log.print_matrix_info(logger, space2.cooccurrence_matrix, 2, 
                           "Semantic space 2:")
     log.print_matrix_info(logger, new_mat, 2, "Resulted semantic space:")
     
     return Space(new_mat, new_id2row, list(space1.id2column), new_row2id, 
                  space1.column2id.copy(), operations=[])