def wrapMatrixFile(matrixFile, protocol, dfsDir=None, maxCores=1): """ Factory method for wrapping a matrix stored in a file in coordinate format. """ if protocol == MatrixWrapper.RAW: raws = [[] for i in range(0, maxCores)] i = 0 infile = file(matrixFile, "r") for line in infile: for elem in line.strip().split(";"): raws[i%maxCores].append(elem) i += 1 infile.close() return MatrixWrapper(["raw://"+";".join(elems) for elems in raws if len(elems) > 0]) elif protocol == MatrixWrapper.DFS: if not os.path.exists(dfsDir): os.mkdir(dfsDir) # delete any previous gluster files in the directory [os.remove(dfsDir+"/"+f) for f in os.listdir(dfsDir)] splitFileToFiles(matrixFile, dfsDir, "", maxCores) return MatrixWrapper(files(dfsDir)) else: raise Exception('only dfs or raw protocol are supported')
def wrapMatrix(A, protocol, dfsDir=None, maxCores=1): """ Factory method for wrapping a numpy/scipy matrix object. """ if type(A) == coo_matrix: Acoo = A.tocoo() if protocol == MatrixWrapper.RAW: raws = [[] for i in range(0, maxCores)] for i in range(0, len(Acoo.row)): raws[i%maxCores].append("%d,%d,%.14f" % (Acoo.row[i], Acoo.col[i], Acoo.data[i])) return MatrixWrapper(["raw://"+";".join(elems) for elems in raws if len(elems) > 0]) elif protocol == MatrixWrapper.DFS: import os.path if not os.path.exists(dfsDir): os.mkdir(dfsDir) # delete any previous gluster files in the directory [os.remove(dfsDir+"/"+f) for f in os.listdir(dfsDir)] # save in coordinate format and split matrix to multiple files splitMatrixToFiles(A, dfsDir, "", maxCores) return MatrixWrapper(files(dfsDir)) else: raise Exception('only dfs or raw protocol are supported') else: raise Exception('only coo_matrix is supported for now')