Пример #1
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],)
Пример #2
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],)
Пример #3
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],)    
Пример #4
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], )