Skip to content

Commit

Permalink
Allow using growth rate, and disable saving OT map
Browse files Browse the repository at this point in the history
  • Loading branch information
ShouWenWang committed Oct 28, 2021
1 parent 9eb4288 commit 188a116
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ docs/build/
docs/source/cospar.*
#cospar/help_functions/_additional_analysis.py
.DS_Store
*.pyc
*.py[cod]
__pycache__
.eggs/
tests/data_derivative/
Expand Down
52 changes: 38 additions & 14 deletions cospar/tmap/map_reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1829,12 +1829,21 @@ def infer_Tmap_from_optimal_transport(

##################
logg.info("Compute new custom OT matrix")

t = time.time()
mu1 = np.ones(len(cell_id_array_t1))
nu1 = np.ones(len(cell_id_array_t2))
input_mu = mu1 # initial distribution
input_nu = nu1 # final distribution

if 'cell_growth_rate' not in adata.obs.keys():
logg.info('Use uniform growth rate')
adata.obs["cell_growth_rate"] = np.ones(len(time_info))
else:
logg.info('Use pre-supplied cell_grwoth_rate')
x0=np.mean(adata.obs["cell_growth_rate"])
y0=np.std(adata.obs["cell_growth_rate"])
print(f"Use pre-supplied cell_grwoth_rate (mean: {x0:.2f}; std {y0:.2f})")

# mu1 = np.ones(len(cell_id_array_t1))
# nu1 = np.ones(len(cell_id_array_t2))
# input_mu = mu1 # initial distribution
# input_nu = nu1 # final distribution

######### We have tested that it is at least 3 times slower than WOT's built-in method,
#### although the results are the same
Expand All @@ -1850,7 +1859,7 @@ def infer_Tmap_from_optimal_transport(
): # This takes 50s for the subsampled hematopoietic data. The result is the same.
ot_config = {
"C": OT_cost_matrix,
"G": mu1,
"G": np.array(adata.obs["cell_growth_rate"])[cell_id_array_t1],
"epsilon": OT_epsilon,
"lambda1": 1,
"lambda2": 50,
Expand All @@ -1868,7 +1877,7 @@ def infer_Tmap_from_optimal_transport(
): # This takes 30s for the subsampled hematopoietic data. The result is the same.
ot_config = {
"C": OT_cost_matrix,
"G": mu1,
"G": np.array(adata.obs["cell_growth_rate"])[cell_id_array_t1],
"epsilon": OT_epsilon,
"lambda1": 1,
"lambda2": 50,
Expand All @@ -1889,7 +1898,7 @@ def infer_Tmap_from_optimal_transport(
)
if not ssp.issparse(OT_transition_map):
OT_transition_map = ssp.csr_matrix(OT_transition_map)
ssp.save_npz(CustomOT_file_name, OT_transition_map)
#ssp.save_npz(CustomOT_file_name, OT_transition_map)

logg.info(
f"Finishing computing optial transport map, used time {time.time()-t}"
Expand All @@ -1906,15 +1915,22 @@ def infer_Tmap_from_optimal_transport(
def infer_Tmap_from_optimal_transport_v0(
adata,
OT_epsilon=0.02,
OT_dis_KNN=5,
OT_solver="duality_gap",
OT_cost="SPD",
compute_new=True,
use_existing_KNN_graph=False,
OT_dis_KNN=None,
OT_solver=None,
OT_cost=None,
compute_new=None,
use_existing_KNN_graph=None,
):
"""
Test WOT
In order to use non-uniform cell_grwoth_rate,
we implicitly assume that you should pass a variable
to adata at .obs['cell_growth_rate']
Also note that only the variable `adata`, `OT_epsilon`, and .obs['cell_growth_rate']
affects the result.
Returns
-------
None. Results are stored at adata.uns['OT_transition_map'].
Expand All @@ -1933,7 +1949,15 @@ def infer_Tmap_from_optimal_transport_v0(
time_info[cell_id_array_t1] = 1
time_info[cell_id_array_t2] = 2
adata.obs["day"] = time_info
adata.obs["cell_growth_rate"] = np.ones(len(time_info))

if 'cell_growth_rate' not in adata.obs.keys():
print('Use uniform growth rate')
adata.obs["cell_growth_rate"] = np.ones(len(time_info))
else:
x0=np.mean(adata.obs["cell_growth_rate"])
y0=np.std(adata.obs["cell_growth_rate"])
print(f"Use pre-supplied cell_grwoth_rate (mean: {x0:.2f}; std {y0:.2f})")

ot_model = wot.ot.OTModel(adata, epsilon=OT_epsilon, lambda1=1, lambda2=50)
OT_transition_map = ot_model.compute_transport_map(1, 2).X

Expand Down

0 comments on commit 188a116

Please sign in to comment.