Skip to content

Commit

Permalink
Add script to subset an h5ad file, use it to reduce the size of the s…
Browse files Browse the repository at this point in the history
…ample.h5ad to make loading faster initially from github pages
  • Loading branch information
rcurrie committed Dec 6, 2024
1 parent 0e2fb69 commit 52cfde2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
serve:
python -m http.server 3000

check-model:
python -m onnxruntime.tools.check_onnx_model_mobile_usability --log_level debug data/sims.onnx

deploy:
rsync -v ./*.{js,html} [email protected]:~/public_html/sims/
rsync -avz models [email protected]:~/public_html/sims/
# rsync -avz data [email protected]:~/public_html/sims/
git checkout gh-pages
git merge main
git push origin gh-pages
git checkout main
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ <h2 class="mb-0" style="font-weight: 300">SIMS Web</h2>
</div>
<div class="form-group">
<label for="cellRange">% Of Cells To Process:</label>
<input type="range" class="custom-range" id="cellRange" min="0" max="100" step="1" value="1">
<small id="cellRangeValue" class="form-text text-muted">1%</small>
<input type="range" class="custom-range" id="cellRange" min="0" max="100" step="1" value="10">
<small id="cellRangeValue" class="form-text text-muted">10%</small>
</div>
<button id="predict_btn" class="btn btn-primary" autofocus>Predict</button>
<button id="stop_btn" class="btn btn-danger">Stop</button>
Expand Down
Binary file modified sample.h5ad
Binary file not shown.
53 changes: 53 additions & 0 deletions subset-anndata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3

import anndata
import numpy as np
import argparse
import sys

def sample_anndata(input_file: str, output_file: str, n_cells: int):
"""
Reads an .h5ad AnnData file, randomly selects a subset of cells, and saves a new .h5ad file.
Parameters:
input_file (str): Path to the input .h5ad file.
output_file (str): Path where the output .h5ad file will be saved.
n_cells (int): Number of cells to randomly select.
"""
# Read the input AnnData file
adata = anndata.read_h5ad(input_file)

# Total number of cells in the dataset
total_cells = adata.n_obs

# Check if requested number of cells is valid
if n_cells > total_cells:
raise ValueError(f"Requested number of cells ({n_cells}) exceeds total cells in the dataset ({total_cells}).")

# Randomly select indices of cells to keep
np.random.seed(42) # Optional: Set seed for reproducibility
selected_cells = np.random.choice(total_cells, size=n_cells, replace=False)

# Subset the AnnData object to the selected cells
adata_subset = adata[selected_cells, :].copy()

# Save the subsetted AnnData object to a new file
adata_subset.write_h5ad(output_file)

def main():
parser = argparse.ArgumentParser(description='Sample a subset of cells from an AnnData .h5ad file.')
parser.add_argument('input_file', type=str, help='Path to the input .h5ad file.')
parser.add_argument('output_file', type=str, help='Path to the output .h5ad file.')
parser.add_argument('--n_cells', '-n', type=int, default=1000, help='Number of cells to sample (default: 1000).')

args = parser.parse_args()

try:
sample_anndata(args.input_file, args.output_file, args.n_cells)
print(f"Successfully saved {args.n_cells} cells to {args.output_file}")
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)

if __name__ == '__main__':
main()

0 comments on commit 52cfde2

Please sign in to comment.