-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converted paste into a command #68
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1c43d09
added io sub module to process files and respective test cases for it
anushka255 32d3681
refactored paste_cmd_line to provide support for paste2
anushka255 e9a4fd0
renamed pate_cmd_line to align
anushka255 7e6f4aa
created paste command and refactored alignn
anushka255 02ab1fe
added __main__
anushka255 628a234
minor update;
anushka255 68cdfb6
parameterzing align script to make sure csv and h5ad files with same …
anushka255 0d8dbd5
omiting __main__ from coverage
anushka255 1c6570b
Merge branch 'main' into aa/api
anushka255 8142f61
testing version of the package
anushka255 9b27360
minor update
anushka255 34b0dbe
removed h5ad files after successful check
vineetbansal bcebd81
minor fix
anushka255 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logging.config | ||
|
||
|
||
# The _version.py file is managed by setuptools-scm | ||
# and is not in version control. | ||
try: | ||
from paste3._version import version as __version__ # type: ignore | ||
except ModuleNotFoundError: | ||
# We're likely running as a source package without installation | ||
__version__ = "src" | ||
|
||
|
||
logging.config.dictConfig( | ||
{ | ||
"version": 1, | ||
"formatters": { | ||
"standard": { | ||
"format": "(%(levelname)s) (%(filename)s) (%(asctime)s) %(message)s", | ||
"datefmt": "%d-%b-%y %H:%M:%S", | ||
} | ||
}, | ||
"handlers": { | ||
"default": { | ||
"level": "NOTSET", | ||
"formatter": "standard", | ||
"class": "logging.StreamHandler", | ||
"stream": "ext://sys.stdout", | ||
} | ||
}, | ||
"loggers": {"": {"handlers": ["default"], "level": "INFO"}}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import logging | ||
import argparse | ||
import os | ||
from paste3 import align | ||
import paste3 | ||
|
||
logger = logging.getLogger("paste3") | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument("--version", action="version", version=paste3.__version__) | ||
|
||
modules = [align] | ||
|
||
subparsers = parser.add_subparsers(title="Choose a command") | ||
subparsers.required = True | ||
|
||
def get_str_name(module): | ||
return os.path.splitext(os.path.basename(module.__file__))[0] | ||
|
||
for module in modules: | ||
this_parser = subparsers.add_parser( | ||
get_str_name(module), description=module.__doc__ | ||
) | ||
this_parser.add_argument( | ||
"-v", "--verbose", action="store_true", help="Increase verbosity" | ||
) | ||
|
||
module.add_args(this_parser) | ||
this_parser.set_defaults(func=module.main) | ||
|
||
args = parser.parse_args() | ||
if args.verbose: | ||
logger.setLevel(logging.DEBUG) | ||
|
||
args.func(args) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
import ot.backend | ||
import numpy as np | ||
from pathlib import Path | ||
|
||
import pandas as pd | ||
|
||
from paste3.io import process_files | ||
import logging | ||
from paste3.paste import pairwise_align, center_align | ||
from paste3.visualization import stack_slices_pairwise, stack_slices_center | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def align( | ||
mode, | ||
gene_fpath, | ||
spatial_fpath=None, | ||
output_directory="", | ||
alpha=0.1, | ||
cost="kl", | ||
n_components=15, | ||
lmbda=None, | ||
initial_slice=1, | ||
threshold=0.001, | ||
coordinates=False, | ||
weight_fpath=None, | ||
overlap_fraction=None, | ||
start=None, | ||
seed=None, | ||
cost_matrix=None, | ||
max_iter=10, | ||
norm=False, | ||
numItermax=200, | ||
use_gpu=False, | ||
return_obj=False, | ||
optimizeTheta=True, | ||
eps=1e-4, | ||
is_histology=False, | ||
armijo=False, | ||
): | ||
slices = process_files(gene_fpath, spatial_fpath, weight_fpath) | ||
n_slices = len(slices) | ||
|
||
if not (mode == "pairwise" or mode == "center"): | ||
raise (ValueError("Please select either pairwise or center alignment mode.")) | ||
|
||
if alpha < 0 or alpha > 1: | ||
raise (ValueError("Alpha specified outside of 0-1 range.")) | ||
|
||
if initial_slice < 1 or initial_slice > n_slices: | ||
raise (ValueError("Initial specified outside of 0 - n range")) | ||
|
||
if overlap_fraction: | ||
if overlap_fraction < 0 or overlap_fraction > 1: | ||
raise (ValueError("Overlap fraction specified outside of 0-1 range.")) | ||
|
||
if lmbda is None: | ||
lmbda = n_slices * [1 / n_slices] | ||
elif len(lmbda) != n_slices: | ||
raise (ValueError("Length of lambda doesn't equal number of files")) | ||
else: | ||
if not all(i >= 0 for i in lmbda): | ||
raise (ValueError("lambda includes negative weights")) | ||
else: | ||
print("Normalizing lambda weights into probability vector.") | ||
lmbda = [float(i) / sum(lmbda) for i in lmbda] | ||
|
||
if cost_matrix: | ||
cost_matrix = np.genfromtxt(cost_matrix, delimiter=",", dtype="float64") | ||
|
||
if start is None: | ||
pis_init = [None] * (n_slices - 1) if mode == "pairwise" else None | ||
elif mode == "pairwise" and not (len(start) == n_slices - 1): | ||
raise ValueError( | ||
f"Number of slices {n_slices} is not equal to number of start pi files {len(start)}" | ||
) | ||
else: | ||
pis_init = [np.genfromtxt(pi, delimiter=",") for pi in start] | ||
|
||
# make output directory if it doesn't exist | ||
output_directory = Path(output_directory) | ||
Path.mkdir(output_directory, exist_ok=True) | ||
|
||
if mode == "pairwise": | ||
logger.info("Computing Pairwise Alignment ") | ||
pis = [] | ||
for i in range(n_slices - 1): | ||
pi = pairwise_align( | ||
sliceA=slices[i], | ||
sliceB=slices[i + 1], | ||
s=overlap_fraction, | ||
M=cost_matrix, | ||
alpha=alpha, | ||
dissimilarity=cost, | ||
use_rep=None, | ||
G_init=pis_init[i], | ||
a_distribution=slices[i].obsm["weights"], | ||
b_distribution=slices[i + 1].obsm["weights"], | ||
norm=norm, | ||
numItermax=numItermax, | ||
backend=ot.backend.NumpyBackend(), | ||
use_gpu=use_gpu, | ||
return_obj=return_obj, | ||
maxIter=max_iter, | ||
optimizeTheta=optimizeTheta, | ||
eps=eps, | ||
is_histology=is_histology, | ||
armijo=armijo, | ||
) | ||
pis.append(pi) | ||
pd.DataFrame( | ||
pi, index=slices[i].obs.index, columns=slices[i + 1].obs.index | ||
).to_csv(output_directory / f"slice_{i+1}_{i+2}_pairwise.csv") | ||
|
||
if coordinates: | ||
new_slices = stack_slices_pairwise( | ||
slices, pis, is_partial=overlap_fraction is not None | ||
) | ||
|
||
elif mode == "center": | ||
logger.info("Computing Center Alignment") | ||
initial_slice = slices[initial_slice - 1].copy() | ||
|
||
center_slice, pis = center_align( | ||
A=initial_slice, | ||
slices=slices, | ||
lmbda=lmbda, | ||
alpha=alpha, | ||
n_components=n_components, | ||
threshold=threshold, | ||
max_iter=max_iter, | ||
dissimilarity=cost, | ||
norm=norm, | ||
random_seed=seed, | ||
pis_init=pis_init, | ||
distributions=[slice.obsm["weights"] for slice in slices], | ||
backend=ot.backend.NumpyBackend(), | ||
use_gpu=use_gpu, | ||
) | ||
|
||
center_slice.write(output_directory / "center_slice.h5ad") | ||
for i in range(len(pis) - 1): | ||
pd.DataFrame( | ||
pis[i], index=center_slice.obs.index, columns=slices[i].obs.index | ||
).to_csv(output_directory / f"slice_{i}_{i+1}_pairwise.csv") | ||
|
||
if coordinates: | ||
new_slices = stack_slices_center(center_slice, slices, pis) | ||
|
||
if coordinates: | ||
if mode == "center": | ||
center, new_slices = new_slices | ||
center.write(output_directory / "new_center.h5ad") | ||
|
||
for i, slice in enumerate(new_slices, start=1): | ||
slice.write(output_directory / f"new_slices_{i}.h5ad") | ||
|
||
|
||
def add_args(parser): | ||
parser.add_argument( | ||
"mode", type=str, help="Alignment type: 'pairwise' or 'center'." | ||
) | ||
parser.add_argument( | ||
"--g_fpath", type=str, nargs="+", help="Paths to gene exp files (.csv/ .h5ad)." | ||
) | ||
parser.add_argument( | ||
"--s_fpath", type=str, nargs="*", help="Paths to spatial data files (.csv)." | ||
) | ||
parser.add_argument( | ||
"--w_fpath", type=str, nargs="*", help="Paths to spot weight files (.csv)." | ||
) | ||
parser.add_argument( | ||
"--output_dir", default="./output", help="Directory to save output files." | ||
) | ||
parser.add_argument( | ||
"--alpha", type=float, default=0.1, help="Alpha param for alignment (0 to 1)." | ||
) | ||
parser.add_argument( | ||
"--cost", | ||
choices=["kl", "euc", "gkl", "selection_kl", "pca", "glmpca"], | ||
default="kl", | ||
help="Expression dissimilarity cost", | ||
) | ||
|
||
parser.add_argument( | ||
"--cost_mat", type=str, help="Paths to exp dissimilarity cost matrix." | ||
) | ||
parser.add_argument( | ||
"--n_comp", type=int, default=15, help="Components for NMF in center alignment." | ||
) | ||
parser.add_argument( | ||
"--lmbda", type=float, nargs="+", help="Weight vector for each slice." | ||
) | ||
parser.add_argument( | ||
"--init_slice", type=int, default=1, help="First slice for alignment (1 to n)." | ||
) | ||
parser.add_argument( | ||
"--thresh", | ||
type=float, | ||
default=1e-3, | ||
help="Convergence threshold for alignment.", | ||
) | ||
|
||
parser.add_argument( | ||
"--coor", action="store_true", help="Compute and save new coordinates." | ||
) | ||
parser.add_argument( | ||
"--ovlp_frac", type=float, default=None, help="Overlap fraction (0-1)." | ||
) | ||
parser.add_argument( | ||
"--start", type=str, nargs="+", help="Paths to initial alignment files." | ||
) | ||
parser.add_argument( | ||
"--norm", action="store_true", help="Normalize expression data if True." | ||
) | ||
parser.add_argument("--max_iter", type=int, help="Maximum number of iterations.") | ||
parser.add_argument( | ||
"--gpu", action="store_true", help="Use GPU for processing if True." | ||
) | ||
parser.add_argument("--r_info", action="store_true", help="Returns log if True.") | ||
parser.add_argument( | ||
"--hist", action="store_true", help="Use histological images if True." | ||
) | ||
parser.add_argument( | ||
"--armijo", action="store_true", help="Run Armijo line search if True." | ||
) | ||
parser.add_argument( | ||
"--seed", type=int, default=0, help="Random seed for reproducibility." | ||
) | ||
return parser | ||
|
||
|
||
def main(args): | ||
align( | ||
mode=args.mode, | ||
gene_fpath=args.g_fpath, | ||
spatial_fpath=args.s_fpath, | ||
output_directory=args.output_dir, | ||
alpha=args.alpha, | ||
cost=args.cost, | ||
n_components=args.n_comp, | ||
lmbda=args.lmbda, | ||
initial_slice=args.init_slice, | ||
threshold=args.thresh, | ||
coordinates=args.coor, | ||
weight_fpath=args.w_fpath, | ||
overlap_fraction=args.ovlp_frac, | ||
start=args.start, | ||
seed=args.seed, | ||
cost_matrix=args.cost_mat, | ||
norm=args.norm, | ||
numItermax=args.max_iter, | ||
use_gpu=args.gpu, | ||
return_obj=args.r_info, | ||
is_histology=args.hist, | ||
armijo=args.armijo, | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we just add a test to check the execution of the
main
function in__main__.py
(or even just if{sys.executable} paste3 --version
will work? In any case, let's not make exceptions like this (let coverage go down if it must).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test to check version number and got rid of the
--omit...
.