def setUp(self): self.m = Matrix(5) # Pull the matrix out of the docstring for the class. I'm lazy. rows = [x.strip() for x in self.__doc__.split('\n')] rows = [x for x in rows if x[0] == '|'] rows = [[float(x) for x in y.strip('|').split()] for y in rows] for i in xrange(len(rows)): self.m.set_row(i, rows[i])
def __init__(self, a_link_matrix, unreachable_distance=None): self._matrix = Matrix(len(a_link_matrix)) if unreachable_distance is None: unreachable_distance = len(a_link_matrix) self._unreachable = unreachable_distance logging.log(ULTRADEBUG, "Getting C versions of the matrices.") transposed = a_link_matrix.transpose() transposed_c = transposed.as_binary_c_matrix() c_link_matrix = a_link_matrix.as_binary_c_matrix() to_fill_in = self._matrix.as_binary_c_matrix() logging.log(ULTRADEBUG, "Going into C to perform the search.") DISTLIB.fill_distance_matrix(byref(to_fill_in), byref(c_link_matrix), byref(transposed_c), len(a_link_matrix), self._unreachable) logging.log(ULTRADEBUG, "Back from C. Filling the matrix in.") self._matrix.fill_from_c_matrix(to_fill_in) self._converted_distance = None
def setUp(self): self.m=Matrix(5) # Pull the matrix out of the docstring for the class. I'm lazy. rows=[x.strip() for x in self.__doc__.split('\n')] rows=[x for x in rows if x[0]=='|'] rows=[[float(x) for x in y.strip('|').split()] for y in rows] for i in xrange(len(rows)): self.m.set_row(i, rows[i])
def __init__(self, a_link_matrix, unreachable_distance=None): self._matrix=Matrix(len(a_link_matrix)) if unreachable_distance is None: unreachable_distance=len(a_link_matrix) self._unreachable=unreachable_distance logging.log(ULTRADEBUG, "Getting C versions of the matrices.") transposed=a_link_matrix.transpose() transposed_c=transposed.as_binary_c_matrix() c_link_matrix=a_link_matrix.as_binary_c_matrix() to_fill_in=self._matrix.as_binary_c_matrix() logging.log(ULTRADEBUG, "Going into C to perform the search.") DISTLIB.fill_distance_matrix(byref(to_fill_in), byref(c_link_matrix), byref(transposed_c), len(a_link_matrix), self._unreachable) logging.log(ULTRADEBUG, "Back from C. Filling the matrix in.") self._matrix.fill_from_c_matrix(to_fill_in) self._converted_distance=None
class DistanceMatrix(object): """Represents a distance matrix, in which each C[i, j] encodes the distance from i to j in a graph. Pass the value you plan on using as an unreachable distance to the constructor. If you omit it, it will default to the link matrix's size (reasonable in most cases). The distance matrix is meant to compute stats on, so it's immutable by design. """ def __init__(self, a_link_matrix, unreachable_distance=None): self._matrix = Matrix(len(a_link_matrix)) if unreachable_distance is None: unreachable_distance = len(a_link_matrix) self._unreachable = unreachable_distance logging.log(ULTRADEBUG, "Getting C versions of the matrices.") transposed = a_link_matrix.transpose() transposed_c = transposed.as_binary_c_matrix() c_link_matrix = a_link_matrix.as_binary_c_matrix() to_fill_in = self._matrix.as_binary_c_matrix() logging.log(ULTRADEBUG, "Going into C to perform the search.") DISTLIB.fill_distance_matrix(byref(to_fill_in), byref(c_link_matrix), byref(transposed_c), len(a_link_matrix), self._unreachable) logging.log(ULTRADEBUG, "Back from C. Filling the matrix in.") self._matrix.fill_from_c_matrix(to_fill_in) self._converted_distance = None def __getitem__(self, key): return self._matrix[key] def __len__(self): return len(self._matrix) def out_distance(self, i): """The out-distance of a term i.""" return self._matrix.rowsum(i) def in_distance(self, j): """The in-distance of a term j.""" return self._matrix.colsum(j) def compute_converted_distance(self): """Calculate the converted distance, for the internal use of other functions.""" total = 0.0 for i in xrange(len(self)): total += self.out_distance(i) self._converted_distance = total def relative_out_centrality(self, i): """Computes relative out centrality.""" if self._converted_distance is None: self.compute_converted_distance() return self._converted_distance / float(self.out_distance(i)) def relative_in_centrality(self, j): """Computes relative in centrality.""" if self._converted_distance is None: self.compute_converted_distance() return self._converted_distance / float(self.in_distance(j)) def max_centrality_norm_factor(self): """The normalization factor corresponding to the maximal centrality. Used in the compactness calculation""" return float(len(self)**2 - len(self)) * self._unreachable def min_centrality_norm_factor(self): """The normalization factor corresponding to the minimal centrality. Used in the compactness calculation""" return float(len(self)**2 - len(self)) def compactness(self): "Measures how connected the graph is" if self._converted_distance is None: self.compute_converted_distance() return (self.max_centrality_norm_factor()-self._converted_distance) \ /(self.max_centrality_norm_factor()- self.min_centrality_norm_factor()) def stratum(self): "Measures the linearity of the graph" lap=(len(self)**3)/4.0 if len(self)%2==0 \ else (len(self)**3-len(self))/4.0 total = 0.0 for i in xrange(len(self)): # We can't use the regular implementations, because we must # invalidate the unconnected nodes. status = 0.0 contrastatus = 0.0 for j in xrange(len(self)): rowval = self._matrix[i, j] colval = self._matrix[j, i] status += 0.0 if rowval == self._unreachable else rowval contrastatus += 0.0 if colval == self._unreachable else colval total += abs(status - contrastatus) return total / lap
def transpose(self): # Needs a special transpose operation because it must return a # LinkMatrix return Matrix.transpose(self, LinkMatrix)
def __init__(self, matrix_size): Matrix.__init__(self, matrix_size)
class test_base_matrix(unittest.TestCase): """The default matrix is a 5x5 matrix with the following setup: | 1.0 0.0 1.0 -1.0 2.5 | | 0.0 3.1 0.1 0.0 -7.9 | | -9.8 1.1 6.7 2.2 -4.0 | | 19.3 1.1 1.1 4.5 -9.9 | | 2.7 0.1 0.1 -3.0 1.0 |""" def setUp(self): self.m=Matrix(5) # Pull the matrix out of the docstring for the class. I'm lazy. rows=[x.strip() for x in self.__doc__.split('\n')] rows=[x for x in rows if x[0]=='|'] rows=[[float(x) for x in y.strip('|').split()] for y in rows] for i in xrange(len(rows)): self.m.set_row(i, rows[i]) def testConstruction(self): self.assert_(isinstance(self.m, Matrix)) self.assertEquals(self.m[1, 1], 3.1) self.assertEquals(self.m[4, 1], 0.1) self.assertEquals(self.m[3, 0], 19.3) self.assertEquals(self.m[4, 3], -3.0) def testIndexError(self): self.assertRaises(IndexError, self.m.__getitem__, (7, 8)) def testRowSum(self): self.assertAlmostEquals(self.m.rowsum(0), 3.5) self.assertAlmostEquals(self.m.rowsum(2), -3.8) def testColSum(self): self.assertAlmostEquals(self.m.colsum(3), 2.7) def testMax(self): self.assertAlmostEquals(self.m.max(), 19.3) def testSize(self): self.assertEquals(len(self.m), 5) def testRowNonZero(self): self.assertEquals(self.m.row_nonzero(2), 5) self.assertEquals(self.m.row_nonzero(1), 3) def testColNonZero(self): self.assertEquals(self.m.col_nonzero(0), 4) self.assertEquals(self.m.row_nonzero(4), 5) def testTranspose(self): t=self.m.transpose() for j in xrange(len(self.m)): for i in xrange(len(self.m)): self.assertEquals(self.m[i, j], t[j, i]) def testEquals(self): t=self.m.transpose() tt=t.transpose() self.assertEquals(self.m, tt) def testNormalize(self): norm=self.m.normalize() self.assertAlmostEquals(norm[3, 0], 1.0) self.assertAlmostEquals(norm[3, 2], 0.05699481865285)
class test_base_matrix(unittest.TestCase): """The default matrix is a 5x5 matrix with the following setup: | 1.0 0.0 1.0 -1.0 2.5 | | 0.0 3.1 0.1 0.0 -7.9 | | -9.8 1.1 6.7 2.2 -4.0 | | 19.3 1.1 1.1 4.5 -9.9 | | 2.7 0.1 0.1 -3.0 1.0 |""" def setUp(self): self.m = Matrix(5) # Pull the matrix out of the docstring for the class. I'm lazy. rows = [x.strip() for x in self.__doc__.split('\n')] rows = [x for x in rows if x[0] == '|'] rows = [[float(x) for x in y.strip('|').split()] for y in rows] for i in xrange(len(rows)): self.m.set_row(i, rows[i]) def testConstruction(self): self.assert_(isinstance(self.m, Matrix)) self.assertEquals(self.m[1, 1], 3.1) self.assertEquals(self.m[4, 1], 0.1) self.assertEquals(self.m[3, 0], 19.3) self.assertEquals(self.m[4, 3], -3.0) def testIndexError(self): self.assertRaises(IndexError, self.m.__getitem__, (7, 8)) def testRowSum(self): self.assertAlmostEquals(self.m.rowsum(0), 3.5) self.assertAlmostEquals(self.m.rowsum(2), -3.8) def testColSum(self): self.assertAlmostEquals(self.m.colsum(3), 2.7) def testMax(self): self.assertAlmostEquals(self.m.max(), 19.3) def testSize(self): self.assertEquals(len(self.m), 5) def testRowNonZero(self): self.assertEquals(self.m.row_nonzero(2), 5) self.assertEquals(self.m.row_nonzero(1), 3) def testColNonZero(self): self.assertEquals(self.m.col_nonzero(0), 4) self.assertEquals(self.m.row_nonzero(4), 5) def testTranspose(self): t = self.m.transpose() for j in xrange(len(self.m)): for i in xrange(len(self.m)): self.assertEquals(self.m[i, j], t[j, i]) def testEquals(self): t = self.m.transpose() tt = t.transpose() self.assertEquals(self.m, tt) def testNormalize(self): norm = self.m.normalize() self.assertAlmostEquals(norm[3, 0], 1.0) self.assertAlmostEquals(norm[3, 2], 0.05699481865285)
class DistanceMatrix(object): """Represents a distance matrix, in which each C[i, j] encodes the distance from i to j in a graph. Pass the value you plan on using as an unreachable distance to the constructor. If you omit it, it will default to the link matrix's size (reasonable in most cases). The distance matrix is meant to compute stats on, so it's immutable by design. """ def __init__(self, a_link_matrix, unreachable_distance=None): self._matrix=Matrix(len(a_link_matrix)) if unreachable_distance is None: unreachable_distance=len(a_link_matrix) self._unreachable=unreachable_distance logging.log(ULTRADEBUG, "Getting C versions of the matrices.") transposed=a_link_matrix.transpose() transposed_c=transposed.as_binary_c_matrix() c_link_matrix=a_link_matrix.as_binary_c_matrix() to_fill_in=self._matrix.as_binary_c_matrix() logging.log(ULTRADEBUG, "Going into C to perform the search.") DISTLIB.fill_distance_matrix(byref(to_fill_in), byref(c_link_matrix), byref(transposed_c), len(a_link_matrix), self._unreachable) logging.log(ULTRADEBUG, "Back from C. Filling the matrix in.") self._matrix.fill_from_c_matrix(to_fill_in) self._converted_distance=None def __getitem__(self, key): return self._matrix[key] def __len__(self): return len(self._matrix) def out_distance(self, i): """The out-distance of a term i.""" return self._matrix.rowsum(i) def in_distance(self, j): """The in-distance of a term j.""" return self._matrix.colsum(j) def compute_converted_distance(self): """Calculate the converted distance, for the internal use of other functions.""" total=0.0 for i in xrange(len(self)): total+=self.out_distance(i) self._converted_distance=total def relative_out_centrality(self, i): """Computes relative out centrality.""" if self._converted_distance is None: self.compute_converted_distance() return self._converted_distance/float(self.out_distance(i)) def relative_in_centrality(self, j): """Computes relative in centrality.""" if self._converted_distance is None: self.compute_converted_distance() return self._converted_distance/float(self.in_distance(j)) def max_centrality_norm_factor(self): """The normalization factor corresponding to the maximal centrality. Used in the compactness calculation""" return float(len(self)**2 - len(self))*self._unreachable def min_centrality_norm_factor(self): """The normalization factor corresponding to the minimal centrality. Used in the compactness calculation""" return float(len(self)**2 - len(self)) def compactness(self): "Measures how connected the graph is" if self._converted_distance is None: self.compute_converted_distance() return (self.max_centrality_norm_factor()-self._converted_distance) \ /(self.max_centrality_norm_factor()- self.min_centrality_norm_factor()) def stratum(self): "Measures the linearity of the graph" lap=(len(self)**3)/4.0 if len(self)%2==0 \ else (len(self)**3-len(self))/4.0 total=0.0 for i in xrange(len(self)): # We can't use the regular implementations, because we must # invalidate the unconnected nodes. status=0.0 contrastatus=0.0 for j in xrange(len(self)): rowval=self._matrix[i, j] colval=self._matrix[j, i] status+=0.0 if rowval==self._unreachable else rowval contrastatus+=0.0 if colval==self._unreachable else colval total+=abs(status-contrastatus) return total/lap