def load_sparse_matrix(input_format,filepath): """ Load a scipy.sparse.csr_matrix from an input file of the specified format. Parameters ---------- input_format : str Specifies the file format: - tsv - csv - mm (MatrixMarket) - npz (scipy.sparse.csr_matrix serialized with mrec.sparse.savez()) - fsm (mrec.sparse.fast_sparse_matrix) filepath : str The file to load. """ if input_format == 'tsv': return loadtxt(filepath) elif input_format == 'csv': return loadtxt(filepath,delimiter=',') elif input_format == 'mm': return mmread(filepath).tocsr() elif input_format == 'npz': return loadz(filepath).tocsr() elif input_format == 'fsm': return fast_sparse_matrix.load(filepath).X raise ValueError('unknown input format: {0}'.format(input_format))
def load_fast_sparse_matrix(input_format,filepath): """ Load a fast_sparse_matrix from an input file of the specified format, by delegating to the appropriate static method. Parameters ---------- input_format : str Specifies the file format: - tsv - csv - mm (MatrixMarket) - fsm (mrec.sparse.fast_sparse_matrix) filepath : str The file to load. """ if input_format == 'tsv': return fast_sparse_matrix.loadtxt(filepath) elif input_format == 'csv': return fast_sparse_matrix.loadtxt(filepath,delimiter=',') elif input_format == 'mm': return fast_sparse_matrix.loadmm(filepath) elif input_format == 'fsm': return fast_sparse_matrix.load(filepath) raise ValueError('unknown input format: {0}'.format(input_format))
def load_fast_sparse_matrix(input_format, filepath): """ Load a fast_sparse_matrix from an input file of the specified format, by delegating to the appropriate static method. Parameters ---------- input_format : str Specifies the file format: - tsv - csv - mm (MatrixMarket) - fsm (mrec.sparse.fast_sparse_matrix) filepath : str The file to load. """ if input_format == 'tsv': return fast_sparse_matrix.loadtxt(filepath) elif input_format == 'csv': return fast_sparse_matrix.loadtxt(filepath, delimiter=',') elif input_format == 'mm': return fast_sparse_matrix.loadmm(filepath) elif input_format == 'fsm': return fast_sparse_matrix.load(filepath) raise ValueError('unknown input format: {0}'.format(input_format))
def load_sparse_matrix(input_format, filepath): """ Load a scipy.sparse.csr_matrix from an input file of the specified format. Parameters ---------- input_format : str Specifies the file format: - tsv - csv - mm (MatrixMarket) - npz (scipy.sparse.csr_matrix serialized with mrec.sparse.savez()) - fsm (mrec.sparse.fast_sparse_matrix) filepath : str The file to load. """ if input_format == 'tsv': return loadtxt(filepath) elif input_format == 'csv': return loadtxt(filepath, delimiter=',') elif input_format == 'mm': return mmread(filepath).tocsr() elif input_format == 'npz': return loadz(filepath).tocsr() elif input_format == 'fsm': return fast_sparse_matrix.load(filepath).X raise ValueError('unknown input format: {0}'.format(input_format))
def save_sparse_matrix(data,fmt,filepath): """ Save a scipy sparse matrix in the specified format. Row and column indices will be converted to 1-indexed if you specify a plain text format (tsv, csv, mm). Parameters ---------- data : scipy sparse matrix to save fmt : str Specifies the file format to write: - tsv - csv - mm (MatrixMarket) - npz (save as npz archive of numpy arrays) - fsm (mrec.sparse.fast_sparse_matrix) filepath : str The file to load. """ if fmt == 'tsv': row,col = data.nonzero() out = open(filepath,'w') for u,i,v in izip(row,col,data.data): print >>out,'{0}\t{1}\t{2}'.format(u+1,i+1,v) elif fmt == 'csv': row,col = data.nonzero() out = open(filepath,'w') for u,i,v in izip(row,col,data.data): print >>out,'{0},{1},{2}'.format(u+1,i+1,v) elif fmt == 'mm': mmwrite(filepath,data) elif fmt == 'npz': return savez(data,filepath) elif fmt == 'fsm': fast_sparse_matrix.load(dataset).save(filepath) else: raise ValueError('unknown output format: {0}'.format(fmt))