本文整理匯總了Python中scipy.io.mmwrite方法的典型用法代碼示例。如果您正苦於以下問題:Python io.mmwrite方法的具體用法?Python io.mmwrite怎麽用?Python io.mmwrite使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.io
的用法示例。
在下文中一共展示了io.mmwrite方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: write_to_files
# 需要導入模塊: from scipy import io [as 別名]
# 或者: from scipy.io import mmwrite [as 別名]
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'))
示例2: store_matrix
# 需要導入模塊: from scipy import io [as 別名]
# 或者: from scipy.io import mmwrite [as 別名]
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)
示例3: generate_related_mat
# 需要導入模塊: from scipy import io [as 別名]
# 或者: from scipy.io import mmwrite [as 別名]
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
示例4: save_mtx
# 需要導入模塊: from scipy import io [as 別名]
# 或者: from scipy.io import mmwrite [as 別名]
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)