Skip to content

Commit

Permalink
added test case for command line code (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
anushka255 authored Aug 27, 2024
1 parent 475136c commit c76ceac
Show file tree
Hide file tree
Showing 7 changed files with 1,117 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/paste/paste-cmd-line.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def main(args):
print("Computing center alignment.")
initial_slice = slices[args.initial_slice - 1].copy()
# compute center align
center_slice, pis = center_align(initial_slice, slices, lmbda, args.alpha, args.n_components, args.threshold, dissimilarity=args.cost, distributions=[slices[i].obsm['weights'] for i in range(n_slices)], pis_init=pis_init)
center_slice, pis = center_align(initial_slice, slices, lmbda, args.alpha, args.n_components, args.threshold, random_seed=args.seed, dissimilarity=args.cost, distributions=[slices[i].obsm['weights'] for i in range(n_slices)], pis_init=pis_init)
W = pd.DataFrame(center_slice.uns['paste_W'], index = center_slice.obs.index)
H = pd.DataFrame(center_slice.uns['paste_H'], columns = center_slice.var.index)
W.to_csv(os.path.join(args.direc, "paste_output/W_center"))
Expand Down Expand Up @@ -112,5 +112,6 @@ def main(args):
parser.add_argument("-x","--coordinates", help="output new coordinates", action='store_true', default = False)
parser.add_argument("-w","--weights", help="path to files containing weights of spots in each slice. The format of the files is the same as the coordinate files used as input",type=str, default=[], nargs='+')
parser.add_argument("-s","--start", help="path to files containing initial starting alignmnets. If not given the OT starts the search with uniform alignments. The format of the files is the same as the alignments files output by PASTE",type=str, default=[], nargs='+')
parser.add_argument("--seed", help="random seed for reproducibility",type=int,default=None)
args = parser.parse_args()
main(args)
16 changes: 16 additions & 0 deletions tests/data/output/H_center

Large diffs are not rendered by default.

255 changes: 255 additions & 0 deletions tests/data/output/W_center

Large diffs are not rendered by default.

255 changes: 255 additions & 0 deletions tests/data/output/slice_center_slice1_pairwise.csv

Large diffs are not rendered by default.

255 changes: 255 additions & 0 deletions tests/data/output/slice_center_slice2_pairwise.csv

Large diffs are not rendered by default.

255 changes: 255 additions & 0 deletions tests/data/output/slice_center_slice3_pairwise.csv

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions tests/test_paste_cmd_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pandas as pd
from pandas.testing import assert_frame_equal
from pathlib import Path
import subprocess

test_dir = Path(__file__).parent
input_dir = test_dir / "data/input"
output_dir = test_dir / "data/output"


def test_cmd_line_center(tmp_path):
print(f"Running command in {tmp_path}")
result = subprocess.run(
[
"python",
f"{test_dir.parent}/src/paste/paste-cmd-line.py",
"-m",
"center",
"--seed",
"0",
"-f",
f"{input_dir}/slice1.csv",
f"{input_dir}/slice1_coor.csv",
f"{input_dir}/slice2.csv",
f"{input_dir}/slice2_coor.csv",
f"{input_dir}/slice3.csv",
f"{input_dir}/slice3_coor.csv",
"-d",
f"{tmp_path}",
]
)
assert result.returncode == 0
assert_frame_equal(
pd.read_csv(tmp_path / "paste_output/W_center"),
pd.read_csv(output_dir / "W_center"),
check_names=False,
rtol=1e-05,
atol=1e-08,
)
assert_frame_equal(
pd.read_csv(tmp_path / "paste_output/H_center"),
pd.read_csv(output_dir / "H_center"),
rtol=1e-05,
atol=1e-08,
)

for i, pi in enumerate(range(3)):
assert_frame_equal(
pd.read_csv(
tmp_path / f"paste_output/slice_center_slice{i + 1}_pairwise.csv"
),
pd.read_csv(output_dir / f"slice_center_slice{i + 1}_pairwise.csv"),
)


def test_cmd_line_pairwise(tmp_path):
print(f"Running command in {tmp_path}")
result = subprocess.run(
[
"python",
f"{test_dir.parent}/src/paste/paste-cmd-line.py",
"-m",
"pairwise",
"-f",
f"{input_dir}/slice1.csv",
f"{input_dir}/slice1_coor.csv",
f"{input_dir}/slice2.csv",
f"{input_dir}/slice2_coor.csv",
f"{input_dir}/slice3.csv",
f"{input_dir}/slice3_coor.csv",
"-d",
f"{tmp_path}",
]
)
assert result.returncode == 0
assert_frame_equal(
pd.read_csv(tmp_path / f"paste_output/slice1_slice2_pairwise.csv"),
pd.read_csv(output_dir / "slices_1_2_pairwise.csv"),
)

0 comments on commit c76ceac

Please sign in to comment.