def convert(source, destination, sourceFormat, destinationFormat): # converts the matrix at source in sourceFormat to destinationFormat # then writes out at destination if not os.path.exists(source): print("ERROR: load from nonexistent path") return if not os.path.isfile(source): print("ERROR: {0} is not a file".format(source)) return HERCMATRIX = libHercmIO.readMatrix(source, sourceFormat) HERCMATRIX.verification = libBXF.generateVerificationSum(HERCMATRIX) libHercmIO.writeMatrix(destination, destinationFormat, HERCMATRIX)
def readMatrix(filename, form, showProgress=False): HERCMATRIX = libHercMatrix.hercMatrix() logging.info("reading matrix {0} in format {1}".format(filename, form)) if (form == 'hercm') or (form == 'bxf'): # TODO: exception handling HERCMATRIX = libBXF.read(filename) elif form == 'mtx': from scipy import io from scipy.sparse import csr_matrix from numpy import array # reads in an MTX file and converts it to hercm try: if showProgress: print("reading data from file...") rawMatrix = scipy.sparse.coo_matrix(scipy.io.mmread(filename)) if 'symmetric' in io.mminfo(filename): HERCMATRIX.symmetry = "SYM" else: HERCMATRIX.symmetry = "ASYM" hercm = {} # needed to generate verification if showProgress: print("generating header data..") hercm['val'] = rawMatrix.data hercm['col'] = rawMatrix.col.tolist() hercm['row'] = rawMatrix.row.tolist() (matrixWidth, matrixHeight) = rawMatrix.shape HERCMATRIX.height = int(matrixHeight) HERCMATRIX.width = int(matrixWidth) vs = libBXF.generateVerificationSum(hercm) HERCMATRIX.verification = vs HERCMATRIX.remarks = [] # I'm not sure why has to be hard... # http://stackoverflow.com/questions/26018781/numpy-is-it-possible-to-preserve-the-dtype-of-columns-when-using-column-stack if showProgress: print("preparing matrix data...") val = numpy.asarray(hercm['val'], dtype='float64') col = numpy.asarray(hercm['col'], dtype='int32') row = numpy.asarray(hercm['row'], dtype='int32') val = numpy.rec.array(val, dtype=[(('val'), numpy.float64)]) col = numpy.rec.array(col, dtype=[(('col'), numpy.int32)]) row = numpy.rec.array(row, dtype=[(('row'), numpy.int32)]) HERCMATRIX.elements = append_fields(row, 'col', col, usemask=False, dtypes=[numpy.int32]) HERCMATRIX.elements = append_fields(HERCMATRIX.elements, 'val', val, usemask=False, dtypes=[numpy.float64]) HERCMATRIX.nzentries = len(HERCMATRIX.elements['val']) HERCMATRIX.verification = libBXF.generateVerificationSum( HERCMATRIX) if showProgress: print("finished reading matrix") except IOError as e: # make sure the file exists and is readable logging.warning("(lsc-480) could not open matrix file") raise IOError("could not open matrix file for writing...", str(e)) elif form == 'mat': # matlab matrices from scipy import io from scipy import sparse from numpy import array try: rawMatrix = scipy.sparse.coo_matrix( scipy.io.loadmat(filename)['matrix']) hercm = {} # needed to generate verification hercm['val'] = rawMatrix.data hercm['col'] = rawMatrix.col.tolist() hercm['row'] = rawMatrix.row.tolist() (matrixWidth, matrixHeight) = rawMatrix.shape HERCMATRIX.height = int(matrixHeight) HERCMATRIX.width = int(matrixWidth) vs = libBXF.generateVerificationSum(hercm) HERCMATRIX.verification = vs HERCMATRIX.remarks = [] # I'm not sure why has to be hard... # http://stackoverflow.com/questions/26018781/numpy-is-it-possible-to-preserve-the-dtype-of-columns-when-using-column-stack val = numpy.asarray(hercm['val'], dtype='float64') col = numpy.asarray(hercm['col'], dtype='int32') row = numpy.asarray(hercm['row'], dtype='int32') val = numpy.rec.array(val, dtype=[(('val'), numpy.float64)]) col = numpy.rec.array(col, dtype=[(('col'), numpy.int32)]) row = numpy.rec.array(row, dtype=[(('row'), numpy.int32)]) HERCMATRIX.elements = append_fields(row, 'col', col, usemask=False, dtypes=[numpy.int32]) HERCMATRIX.elements = append_fields(HERCMATRIX.elements, 'val', val, usemask=False, dtypes=[numpy.float64]) HERCMATRIX.nzentries = len(HERCMATRIX.elements['val']) if HERCMATRIX.checkSymmetry(): HERCMATRIX.symmetry = 'SYM' HERCMATRIX.verification = libBXF.generateVerificationSum( HERCMATRIX) except IOError as e: # make sure the file exists and is readable logging.warning("(lsc-536)could not open matrix file") raise IOError("could not open matrix file for writing...", str(e)) elif form == 'valcol': HERCMATRIX = libValcolIO.read(filename) else: logging.warning("(lsc-545) format {0} is not valid".format(form)) if showProgress: print("converting matrix to row-major...") logging.info("converting matrix to row-major") HERCMATRIX.makeRowMajor() if showProgress: print("matrix is now row major") if HERCMATRIX.symmetry == 'SYM': logging.info("matrix is symmetric, truncating upper triangle") if showProgress: print("matrix is symmetric, truncating upper triangle...") HERCMATRIX.makeSymmetrical('truncate') if showProgress: print("upper triangle truncated") return HERCMATRIX
def execute(this, arguments, WORKINGMATRIX): try: newSum = libBXF.generateVerificationSum(WORKINGMATRIX) except TypeError: print("ERROR: could not generate verification sum of empty matrix") WORKINGMATRIX.verification = newSum