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
|
import core.modules
import core.modules.module_registry
from core.modules.vistrails_module import Module, ModuleError
import scipy
import numpy
from scipy import sparse
from Array import *
class MatrixModule(object):
my_namespace = 'scipy|matrix'
class MatrixOp(object):
my_namespace = 'scipy|matrix|operation'
class Matrix(MatrixModule, Module):
""" Container class for the scipy.sparse.csc_matrix class """
def __init__(self):
Module.__init__(self)
self.matrix = None
def get_shape(self):
return self.matrix.shape
def get_conjugate(self):
return self.matrix.conjugate()
def get_column(self, colId):
return self.matrix.getcol(colId)
def get_row(self, rowId):
return self.matrix.getrow(rowId)
def set_diagonal(self, vals):
self.matrix.setdiag(vals)
def toarray(self):
return self.matrix.toarray()
def transpose(self):
return self.matrix.transpose()
def get_num_elements(self):
return self.matrix.getnnz()
def get_max_elements(self):
return self.matrix.nzmax
def get_reals(self):
return self.matrix._real()
def get_imaginaries(self):
return self.matrix._imag()
def get_matrix(self):
return self.matrix
def set_matrix(self, mat):
self.matrix = mat
class MatrixMultiply(MatrixOp, Module):
""" Multiply two matrices together """
def compute(self):
a = self.getInputFromPort("Matrix1")
b = self.getInputFromPort("Matrix2")
out = Matrix()
out.set_matrix(a.get_matrix() * b.get_matrix())
self.setResult("Matrix Output", out)
@classmethod
def register(cls, reg, basic):
reg.add_module(cls, namespace=cls.my_namespace)
reg.add_input_port(cls, "Matrix1", (Matrix, 'Input Matrix 1'))
reg.add_input_port(cls, "Matrix2", (Matrix, 'Input Matrix 2'))
reg.add_output_port(cls, "Matrix Output", (Matrix, 'Output Matrix'))
class MatrixConjugate(MatrixOp, Module):
""" Get the complex conjugate of the input matrix. """
def compute(self):
a = self.getInputFromPort("Matrix")
b = a.get_conjugate().copy()
out = Matrix()
out.set_matrix(b)
self.setResult("Output", out)
@classmethod
def register(cls, reg, basic):
reg.add_module(cls, namespace=cls.my_namespace)
reg.add_input_port(cls, "Matrix", (Matrix, 'Input Matrix'))
reg.add_output_port(cls, "Output", (Matrix, 'Output Matrix'))
class MatrixToArray(MatrixOp, Module):
""" Convert a SciPy matrix to a Numpy Array """
def compute(self):
m = self.getInputFromPort("Matrix")
a = m.toarray()
out = NDArray()
out.set_array(a)
self.setResult("Output Array", out)
@classmethod
def register(cls, reg, basic):
reg.add_module(cls, namespace=cls.my_namespace)
reg.add_input_port(cls, "Matrix", (Matrix, 'Input Matrix'))
reg.add_output_port(cls, "Output Array", (NDArray, 'Output Array'))
|