File: MatrixConvert.py

package info (click to toggle)
vistrails 2.1.1-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 74,208 kB
  • ctags: 46,250
  • sloc: python: 316,267; xml: 52,512; sql: 3,627; php: 731; sh: 260; makefile: 108
file content (150 lines) | stat: -rw-r--r-- 5,386 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
############################################################################
##
## Copyright (C) 2006-2007 University of Utah. All rights reserved.
##
## This file is part of VisTrails.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file.  Please review the following to ensure GNU General Public
## Licensing requirements will be met:
## http://www.opensource.org/licenses/gpl-license.php
##
## If you are unsure which license is appropriate for your use (for
## instance, you are interested in developing a commercial derivative
## of VisTrails), please contact us at contact@vistrails.org.
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
############################################################################
import core.modules
import core.modules.module_registry
from core.modules.vistrails_module import Module, ModuleError
from SciPy import SciPy
from Matrix import Matrix, COOMatrix, SparseMatrix, CSRMatrix
from scipy import sparse
import numpy, scipy

class MatrixConvert(SciPy):

    def compute(self):
        m = self.getInputFromPort("InputMatrix")
        to = self.getInputFromPort("OutputType")

        to = to.upper()
        if to == 'Dense':
            self.matrix = DenseMatrix(m.matrix.todense())
            self.setResult("SparseOutput", self.matrix)
        else:
            self.matrix = SparseMatrix(m.matrix.tocsc())
            self.setResult("SparseOutput", self.matrix)

class vtkDataSetToMatrix(SciPy):
    ''' In some cases, particularly in terms of user-defined VTK Filters, the
        output of the filter is a vtk datatype representing '''
    
    def from_unstructured_grid(self, vtkalgout):
        import vtk
        prod = vtkalgout.vtkInstance.GetProducer()
        prod.Update()
        grid = prod.GetOutput()
        pt_set = grid.GetPoints()
        scalars = grid.GetPointData().GetScalars()

        ''' Points in vtk are always 3D... so we must assume this. '''
        self.matrix_ = SparseMatrix()
        self.matrix_.matrix = sparse.csc_matrix((grid.GetNumberOfPoints(), 3))

        i = 0
        while i < grid.GetNumberOfPoints():
            (x,y,z) = pt_set.GetPoint(i)
            self.matrix_.matrix[i,0] = x
            self.matrix_.matrix[i,1] = y
            self.matrix_.matrix[i,2] = z
            print x, y, z
            i += 1
            
        
    def compute(self):
        if self.hasInputFromPort("vtkUnstructuredGrid"):
            self.from_unstructured_grid(self.getInputFromPort("vtkUnstructuredGrid"))
        else:
            pass

        self.setResult("Output Matrix", self.matrix_)

class PhaseHistogramToVTKPoints(SciPy):

    def form_point_set(self, histo, point_set):
        (slices, numbins) = histo.shape
        phases = numpy.arange(numbins)
        phases = phases * (360. / numbins)
        phases += phases[1] / 2.
        phi_step = phases[0]
        
        for time in xrange(slices):
            z = float(time)
            for bin in xrange(numbins):
                r = histo[time,bin]
                theta = phi_step * (bin+1)
                theta *= (scipy.pi / 180.)
                x = r*scipy.cos(theta)
                y = r*scipy.sin(theta)
                point_set.InsertNextPoint(x, y, z)

            for bin in xrange(numbins):
                curbin = bin
                lastbin = bin-1
                if lastbin < 0:
                    lastbin = numbins-1

                r = (histo[time,bin] -  histo[time,lastbin]) / 2.
                theta = curbin * 360. / numbins
                x = r*scipy.cos(theta)
                y = r*scipy.sin(theta)
                point_set.InsertNextPoint(x, y, z)
                

    def compute(self):
        import vtk
        
        phasors = self.getInputFromPort("FFT Input")
        numbins = self.getInputFromPort("Num Bins")
        phasor_matrix = phasors.matrix.toarray()
        (timeslices,phases) = phasor_matrix.shape

        point_set = vtk.vtkPoints()

        histo = numpy.zeros((timeslices, numbins))

        for time in xrange(timeslices):
            phase_slice = phasor_matrix[time,:]
            reals = phase_slice.real
            imaginary = phase_slice.imag
            phases = scipy.arctan2(imaginary, reals)
            phases = phases * (180. / scipy.pi)
            bins = phases % numbins
            for b in bins:
                histo[time,b] += 1

        self.form_point_set(histo, point_set)

        pointdata = vtk.vtkUnstructuredGrid()
        pointdata.SetPoints(point_set)

        self.surf_filter = vtk.vtkSurfaceReconstructionFilter()

        self.surf_filter.SetInput(0,pointdata)
#        self.surf_filter.Update()
        reg = core.modules.module_registry
        vtk_set = reg.registry.get_descriptor_by_name('edu.utah.sci.vistrails.vtk', 'vtkAlgorithmOutput').module()
        vtk_set.vtkInstance = self.surf_filter.GetOutputPort()

        histo_mat = SparseMatrix()
        histo_mat.matrix = sparse.csc_matrix(histo)

        self.setResult("Num Slices", timeslices)
        self.setResult("Phase Histogram", histo_mat)
        self.setResult("Phase Geometry", vtk_set)