def test_normalization_twice_not_same_result(self): epsilon = 0.001 normalized = normalize_matrix(self.net_d) normalized_twice = normalize_matrix(normalized) self.assertFalse( self.compare_matrices_epsilon(normalized, normalized_twice, epsilon=epsilon))
def load_test_data(self): # symmetric, normal self.net_a = np.matrix([[0, 1, 0, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 0, 1], [0, 0, 1, 0, 0, 1, 1], [1, 0, 0, 0, 1, 0, 1], [0, 0, 0, 1, 1, 1, 0]]) # non symmetric, empty row self.net_b = np.matrix([[0, 0, 1, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 1], [0, 0, 1, 0, 0, 1, 1], [1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 1, 1, 1, 0]]) # weighted, asymmetric self.net_c = np.matrix([[0, 0, 0, 0.2, 0.8, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0.1, 0, 0, 0, 0, 0.4], [0, 0.01, 0, 0, 0.3, 0, 0.4], [0, 0, 0.3, 0, 0, 0, 0], [0, 0, 0, 0.1, 0, 1, 0], [0.3, 0, 4, 0, 0, 0, 0]]) self.net_d = np.matrix([[0.00, 0.50, 0.00, 0.00, 0.65, 0.89, 0.00], [0.50, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00], [0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00], [0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00], [0.65, 0.00, 0.00, 0.00, 0.00, 0.00, 2.10], [0.89, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00], [0.00, 0.00, 0.00, 0.00, 2.10, 0.00, 0.00]]) self.net_d_precomp = [[ 0.34702097, 0.15462088, 0., 0., 0.22469187, 0.20628999, 0.1767149 ], [ 0.15462088, 0.16889387, 0., 0., 0.10011515, 0.09191589, 0.07873822 ], [0., 0., 0.52631579, 0.47368421, 0., 0., 0.], [0., 0., 0.47368421, 0.52631579, 0., 0., 0.], [ 0.22469187, 0.10011515, 0., 0., 0.4076397, 0.13357027, 0.32059908 ], [ 0.20628999, 0.09191589, 0., 0., 0.13357027, 0.22263109, 0.10504989 ], [ 0.1767149, 0.07873822, 0., 0., 0.32059909, 0.10504989, 0.35214368 ]] self.python_normalized = normalize_matrix(self.net_a) self.python_normalized_twice = normalize_matrix(self.python_normalized) self.python_normalized_b = normalize_matrix(self.net_b) self.python_normalized_c = normalize_matrix(self.net_c)
def subset(self, row_list, column_list, normalize=False, precompute=True): new_matrix = self.matrix[row_list, :][:, column_list] if normalize: new_matrix_norm = preprocessing.normalize_matrix(new_matrix) new_matrix = new_matrix_norm return RelationNet(new_matrix, self.name)
def subset(self, node_list, precompute=True): """ Generate a network that contains a subset of the nodes. Args: node_list: List of integers to retrieve. precompute: Whether or not to generate a precomputed matrix. WARNING: This is very time consuming if the net is big. WARNING: Perform this on RAW matrices, THEN normalize. Normalizing twice causes error. Returns: A new, reduced EntityNet. """ reduced_matrix = self.matrix[node_list, :][:, node_list] reduced_names = [self.node_names[i] for i in node_list] precomputed = None if precompute: normalized_reduced_matrix = preprocessing.normalize_matrix( reduced_matrix) precomputed = preprocessing.precompute_matrix( normalized_reduced_matrix) return EntityNet(reduced_matrix, self.name, reduced_names, precomputed=precomputed)
def experiment(self, extra_params): """ Run the experiment. All config overriding and stuff are performed in the Experiment class. """ self.log.info("Running subset experiment.") self.log.info("Parsing from config file.") required = ['matfile', 'key'] if self._are_required_parameters_valid(self.config, required): cfg_params = self._load_parameters(self.params_section) matfile_content = sio.loadmat(cfg_params['matfile']) mat_id = cfg_params['key'] normalized_matrix = None if not cfg_params['normalized']: self.log.info("Normalizing matrix") normalized_matrix = preprocessing.normalize_matrix( matfile_content[mat_id]) else: self.log.info("Matrix already normalized") normalized_matrix = matfile_content[mat_id] self.log.info("Precomputing matrix") precomputed_matrix = preprocessing.precompute_matrix( normalized_matrix) mat_id_precomputed = '{}_precomputed'.format(mat_id) matfile_content[mat_id] = normalized_matrix matfile_content[mat_id_precomputed] = precomputed_matrix self.log.info( "Overwriting matrix file with precomputed and normalized matrices" ) sio.savemat(cfg_params['matfile'], matfile_content)
def from_raw_matrix(cls, matrix, net_name, node_names): """ Creates an EntityNet object considering it raw: i.e. normalizes first and builds precomputed matrix. Warning: precomputing a matrix is time consuming. However, it is necessary for propagation methods to work. """ norm_matrix = preprocessing.normalize_matrix(matrix) norm_prec_matrix = preprocessing.precompute_matrix(norm_matrix) return cls(norm_matrix, net_name, node_names, precomputed=norm_prec_matrix)
def from_raw_matrix(cls, matrix, net_name): """ Creates a RelationNet object considering it raw: i.e. normalizes first. """ normalized_matrix = preprocessing.normalize_matrix(matrix) return cls(normalized_matrix, net_name)
def test_normalize_matrix_keeps_sparse_result(self): sparse_d = sparse.csr_matrix(self.net_d) normalized = normalize_matrix(sparse_d) self.assertTrue(sparse.issparse(normalized))
def test_normalize_matrix_keeps_dense_result(self): normalized = normalize_matrix(self.net_d) self.assertFalse(sparse.issparse(normalized))
def test_precompute_matrix_returns_dense_result(self): normalized = normalize_matrix(self.net_d) precomputed = precompute_matrix(normalized) self.assertFalse(sparse.issparse(precomputed))