File: spmatrix_util.py

package info (click to toggle)
python-scipy 0.5.2-0.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 33,888 kB
  • ctags: 44,231
  • sloc: ansic: 156,256; cpp: 90,347; python: 89,604; fortran: 73,083; sh: 1,318; objc: 424; makefile: 342
file content (128 lines) | stat: -rw-r--r-- 3,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import math, random
import spmatrix
import pysparse_version

def bytesToString(n):
    if n < 1024:
        return '%d Bytes' % n
    n /= 1024.0
    if n < 1024:
        return '%.1f Kbytes' % n
    n /= 1024.0
    if n < 1024:
        return '%.1f Mbytes' % n
    n /= 1024.0
    return '%.1f Gbytes' % n


def printInfo(mat, name):

    if type(mat) == spmatrix.LLMatType:
        if mat.issym:
            typeName = 'LL symmetric'
        else:
            typeName = 'LL general'
        storage = bytesToString(4*mat.shape[0] + 16*mat.nnz)
    elif type(mat) == spmatrix.SSSMatType:
        typeName = 'SSS'
        storage = bytesToString(12*mat.shape[0] + 12*(mat.nnz - mat.shape[0]))
    elif type(mat) == spmatrix.CSRMatType:
        typeName = 'CSR'
        storage = bytesToString(4*mat.shape[0] + 12*mat.nnz)
    else:
        typeName = 'Unknown'
        storage = 'Unknown'


    print 'Matrix statistics:'
    print '%-20s: %s' % ('name', name)
    print '%-20s: %s' % ('type', typeName)
    print '%-20s: %dx%d' % ('dimensions', mat.shape[0], mat.shape[1])
    print '%-20s: %d' % ('#non-zeros', mat.nnz)
    print '%-20s: %s' % ('storage', storage)
    print

def ll_mat_rand(n, m, density):
    """return a ll_mat object representing a general n-by-m sparse matrix filled with random non-zero values

    The number of non-zero entries is less or equal than
    n*m*density. The values of the non-zero entries are in the range
    [0.0, 1.0)."""
    nnz = int(density*n*m)
    A = spmatrix.ll_mat(n, m, nnz)
    for k in xrange(nnz):
        i = random.randrange(n)
        j = random.randrange(m)
        A[i, j] = random.random()
    return A

def exportVtk(mat, fileName):
    "export matrix to a VTK data file"

    print 'Write VTK file...'
    # write VTK file
    f = open(fileName, 'w')
    f.write('# vtk DataFile Version 3.0\n')
    comment = 'generated using pysparse-%s\n' % (pysparse_version.version, )
    f.write(comment[:256])
    f.write('ASCII\n\n')
    f.write('DATASET STRUCTURED_POINTS\n')
    f.write('DIMENSIONS %d %d 1\n' % (mat.shape[0], mat.shape[1]))
    f.write('ORIGIN 1 1 1\n')
    f.write('SPACING 1 1 1\n\n')
    f.write('\nPOINT_DATA %d\n' % (mat.shape[0]*mat.shape[1]))
    f.write('SCALARS entries float\n')
    f.write('LOOKUP_TABLE default\n\n')
    for i in xrange(mat.shape[0]):
        for j in xrange(mat.shape[1]):
            v = mat[i,j]
            if v <> 0.0:
                v = math.log(math.fabs(v))
            f.write('%lf\n' % v)
    f.close()

def viewVtk(mat):
    import vtk

    imageData = vtk.vtkImageData()
    imageData.SetDimensions(mat.shape[0], mat.shape[1], 1)
    imageData.SetScalarTypeToUnsignedShort()
    imageData.SetNumberOfScalarComponents(1)
    imageData.AllocateScalars()

    viewer = vtk.vtkImageViewer()
    viewer.SetInput(imageData)
    viewer.SetZSlice(0)

    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(viewer.GetRenderWindow())

    iren.Initialize()
    iren.Start()

    imageData = vtk.vtkImageSinusoidSource()
    imageData.SetWholeExtent(0, 300, 0, 300, 0, 10)
    imageData.SetAmplitude(63)
    imageData.SetDirection(1, 1, 0)
    imageData.SetPeriod(25)

    viewer = vtk.vtkImageViewer()
    viewer.SetInput(imageData.GetOutput())
    viewer.SetColorWindow(126)
    viewer.SetColorLevel(0)
    viewer.SetZSlice(0)

    def hamschti(obj, event):
        print 'Haam:'
        print obj
        print event

    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(viewer.GetRenderWindow())
    iren.Initialize()

    interactor = vtk.vtkInteractorStyleImage()
    ##interactor.AddObserver('LeftButtonPressEvent', hamschti)
    iren.SetInteractorStyle(interactor)

    iren.Start()