Python scipy.io.mmwrite() Examples
The following are 4
code examples of scipy.io.mmwrite().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
scipy.io
, or try the search function
.
Example #1
Source File: mtx.py From scprep with GNU General Public License v3.0 | 6 votes |
def save_mtx(data, destination, cell_names=None, gene_names=None): """Save a mtx file Parameters ---------- data : array-like, shape=[n_samples, n_features] Input data, saved to destination/matrix.mtx destination : str Directory in which to save the data cell_names : list-like, shape=[n_samples], optional (default: None) Cell names associated with rows, saved to destination/cell_names.tsv. If `data` is a pandas DataFrame and `cell_names` is None, these are autopopulated from `data.index`. gene_names : list-like, shape=[n_features], optional (default: None) Cell names associated with rows, saved to destination/gene_names.tsv. If `data` is a pandas DataFrame and `gene_names` is None, these are autopopulated from `data.columns`. Examples -------- >>> import scprep >>> scprep.io.save_mtx(data, destination="my_data") >>> reload = scprep.io.load_mtx("my_data/matrix.mtx", ... cell_names="my_data/cell_names.tsv", ... gene_names="my_data/gene_names.tsv") """ if isinstance(data, pd.DataFrame): if cell_names is None: cell_names = data.index if gene_names is None: gene_names = data.columns data = utils.to_array_or_spmatrix(data) data = sparse.coo_matrix(data) # handle ~/ and relative paths destination = os.path.expanduser(destination) if not os.path.isdir(destination): os.mkdir(destination) if cell_names is not None: with open(os.path.join(destination, "cell_names.tsv"), "w") as handle: for name in cell_names: handle.write("{}\n".format(name)) if gene_names is not None: with open(os.path.join(destination, "gene_names.tsv"), "w") as handle: for name in gene_names: handle.write("{}\n".format(name)) sio.mmwrite(os.path.join(destination, "matrix.mtx"), data)
Example #2
Source File: io.py From CITE-seq-Count with MIT License | 6 votes |
def write_to_files(sparse_matrix, top_cells, ordered_tags_map, data_type, outfolder): """Write the umi and read sparse matrices to file in gzipped mtx format. Args: sparse_matrix (dok_matrix): Results in a sparse matrix. top_cells (set): Set of cells that are selected for output. ordered_tags_map (dict): Tags in order with indexes as values. data_type (string): A string definning if the data is umi or read based. outfolder (string): Path to the output folder. """ prefix = os.path.join(outfolder,data_type + '_count') os.makedirs(prefix, exist_ok=True) io.mmwrite(os.path.join(prefix,'matrix.mtx'),sparse_matrix) with gzip.open(os.path.join(prefix,'barcodes.tsv.gz'), 'wb') as barcode_file: for barcode in top_cells: barcode_file.write('{}\n'.format(barcode).encode()) with gzip.open(os.path.join(prefix,'features.tsv.gz'), 'wb') as feature_file: for feature in ordered_tags_map: feature_file.write('{}\n'.format(feature).encode()) with open(os.path.join(prefix,'matrix.mtx'),'rb') as mtx_in: with gzip.open(os.path.join(prefix,'matrix.mtx') + '.gz','wb') as mtx_gz: shutil.copyfileobj(mtx_in, mtx_gz) os.remove(os.path.join(prefix,'matrix.mtx'))
Example #3
Source File: util.py From EDeN with MIT License | 5 votes |
def store_matrix(matrix='', output_dir_path='', out_file_name='', output_format=''): """store_matrix.""" if not os.path.exists(output_dir_path): os.mkdir(output_dir_path) full_out_file_name = os.path.join(output_dir_path, out_file_name) if output_format == "MatrixMarket": if len(matrix.shape) == 1: raise Exception( "'MatrixMarket' format supports only 2D dimensional array\ and not vectors") else: io.mmwrite(full_out_file_name, matrix, precision=None) elif output_format == "numpy": np.save(full_out_file_name, matrix) elif output_format == "joblib": joblib.dump(matrix, full_out_file_name) elif output_format == "text": with open(full_out_file_name, "w") as f: if len(matrix.shape) == 1: for x in matrix: f.write("%s\n" % (x)) else: raise Exception( "'text' format supports only mono dimensional array\ and not matrices") logger.info("Written file: %s" % full_out_file_name)
Example #4
Source File: train_funcs.py From BootEA with MIT License | 5 votes |
def generate_related_mat(folder, triples1, triples2, ref_ent1, ref_ent2): t = time.time() if "15" in folder: out_related_file = folder + "out_related_mat.npy" in_related_file = folder + "in_related_mat.npy" if os.path.exists(out_related_file): out_related_mat = np.load(out_related_file) else: out_related_mat = generate_out_related_mat(triples1, triples2, ref_ent1, ref_ent2) np.save(out_related_file, out_related_mat) if os.path.exists(in_related_file): in_related_mat = np.load(in_related_file) else: in_related_mat = generate_in_related_mat(triples1, triples2, ref_ent1, ref_ent2) np.save(in_related_file, in_related_mat) related_mat1 = out_related_mat # related_mat2 = out_related_mat + in_related_mat print("load related mat", round(time.time() - t, 2)) return related_mat1 else: out_related_file = folder + "out_related_mat.mtx" in_related_file = folder + "in_related_mat.mtx" if os.path.exists(out_related_file): out_related_mat = io.mmread(out_related_file) else: out_related_mat = generate_out_related_mat(triples1, triples2, ref_ent1, ref_ent2) io.mmwrite(out_related_file, sp.sparse.lil_matrix(out_related_mat)) if os.path.exists(in_related_file): in_related_mat = io.mmread(in_related_file) else: in_related_mat = generate_in_related_mat(triples1, triples2, ref_ent1, ref_ent2) io.mmwrite(in_related_file, in_related_mat) related_mat1 = out_related_mat # related_mat2 = out_related_mat + in_related_mat print("load related mat", round(time.time() - t, 2)) return related_mat1