File: ArrayConvert.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 (151 lines) | stat: -rw-r--r-- 6,208 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
151
import core.modules
import core.modules.module_registry
from core.modules.vistrails_module import Module, ModuleError
from scipy import sparse
from Array import *
from Matrix import *

class ArrayConvertModule(object):
    my_namespace = 'numpy|array|convert'

class ArrayDumpToFile(ArrayConvertModule, Module):
    """ Pickle the input array and dump it to the specified file.  This
    array can then be read in via pickle.load or numpy.load """
    def compute(self):
        a = self.getInputFromPort("Array")
        fn = self.getInputFromPort("Filename")
        a.dump_to_file(fn)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, name="ArrayToPickledFile", namespace=cls.my_namespace)
        reg.add_input_port(cls, "Array", (NDArray, 'Input Array'))
        reg.add_input_port(cls, "Filename", (basic.String, 'Filename'))

class ArrayDumpToString(ArrayConvertModule, Module):
    """ Pickle the input array and dump it to a string.  This array
    can then be read in via pickle.loads or numpy.loads """
    def compute(self):
        a = self.getInputFromPort("Array")
        self.setResult("Output String", a.dump_to_string())

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, name="ArrayToPickledString", namespace=cls.my_namespace)
        reg.add_input_port(cls, "Array", (NDArray, 'Input Array'))
        reg.add_output_port(cls, "Output String", (basic.String, 'Output String'))

class ArrayToFile(ArrayConvertModule, Module):
    """ Write the data to a file.  If a separator char is given, the file
    will be written in ASCII with the given char acting as a delimiter.  If
    no separator is given, the file is written in Binary.  The array
    is always written in row-major format regardless of the order of the
    input array. """
    def compute(self):
        a = self.getInputFromPort("Array")
        fn = self.getInputFromPort("Filename")
        sep = ""
        if self.hasInputFromPort("Separator"):
            sep = self.getInputFromPort("Separator")
        a.tofile(fn, sep)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Array", (NDArray, 'Input Array'))
        reg.add_input_port(cls, "Filename", (basic.String, 'Filename'))
        reg.add_input_port(cls, "Separator", (basic.String, 'Separator'), True)
        
class ArrayToString(ArrayConvertModule, Module):
    """ Convert the array to a Python string.  The output string will
    be represented in row-major form regardless of the ordering of the
    input array. """
    def compute(self):
        a = self.getInputFromPort("Array")
        self.setResult("Output String", a.tostring())

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Array", (NDArray, 'Input Array'))
        reg.add_output_port(cls, "Output String", (basic.String, 'Output String'))

class ArrayToMatrix(ArrayConvertModule, Module):
    """ Convert the input Numpy Array to a Scipy Matrix.  The input array
    must be no more than 2-dimensional """
    def compute(self):
        a = self.getInputFromPort("Array")
        try:
            mat = sparse.csc_matrix(a.get_array())
            out_mat = Matrix()
            out_mat.set_matrix(mat)
            self.setResult("Output Matrix", out_mat)
            
        except:
            raise ModuleError("Could not convert input array to matrix")

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Array", (NDArray, 'Input Array'))
        reg.add_output_port(cls, "Output Matrix", (Matrix, 'Output Matrix'))

class ArrayToVTKImageData(ArrayConvertModule, Module):
    """ Convert the array to a vtImageData dataset.  This works well for
    arrays up to (and including) rank 3.  Behavior is undefined for array of
    rank > 3."""
    def compute(self):
        import vtk
        
        a = self.getInputFromPort("Array")
        sh = a.get_shape()

        if len(sh) < 2:
            sh = tuple([sh[0], 1, 0])
        if len(sh) < 3:
            sh = tuple([sh[0], sh[1], 0])

        (num_sigs, num_times, num_freqs) = sh
        num_pts = a.get_num_elements()
        vtk_set = core.modules.module_registry.registry.get_descriptor_by_name('edu.utah.sci.vistrails.vtk', 'vtkStructuredPoints').module()
        vtk_set.vtkInstance = vtk.vtkImageData()
        vtk_set.vtkInstance.SetDimensions(sh[0], sh[1], sh[2]+1)
        vtk_set.vtkInstance.SetScalarTypeToFloat()
        scalars = vtk.vtkFloatArray()

        ar = a.get_array()
        for ar_x in xrange(sh[0]):
            for ar_y in xrange(sh[1]):
                if sh[2] == 0:
                    val = ar[ar_x, ar_y]
                    vtk_set.vtkInstance.SetScalarComponentFromFloat(ar_x, ar_y, 0, 0, val)
                else:
                    
                    for ar_z in xrange(sh[2]):
                        val = ar[ar_x, ar_y, ar_z]
                        vtk_set.vtkInstance.SetScalarComponentFromFloat(ar_x, ar_y, ar_z, 0, val)

        if self.hasInputFromPort("SpacingX"):
            x = self.getInputFromPort("SpacingX")
        else:
            x = 1.0
        if self.hasInputFromPort("SpacingY"):
            y = self.getInputFromPort("SpacingY")
        else:
            y = 1.0
        if self.hasInputFromPort("SpacingZ"):
            z = self.getInputFromPort("SpacingZ")
        else:
            z = 1.0
        vtk_set.vtkInstance.SetSpacing(x,y,z)

        self.setResult("vtkImageData", vtk_set)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Array", (NDArray, 'Input Array Volume'))
        reg.add_input_port(cls, "SpacingX", (basic.Float, 'X Spacing'))
        reg.add_input_port(cls, "SpacingY", (basic.Float, 'Y Spacing'))
        reg.add_input_port(cls, "SpacingZ", (basic.Float, 'Z Spacing'))
        reg.add_output_port(cls, "vtkImageData", (reg.registry.get_descriptor_by_name('edu.utah.sci.vistrails.vtk', 'vtkImageData').module))