Skip to content
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

Fix bugs with report generation across platforms #302

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cellbender/remove_background/posterior.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,9 @@ def __repr__(self):
f'\n\ttotal_n_genes: {self.total_n_genes}'
f'\n\tmatrix_shape: {self.matrix_shape}')

def get_m_indices(self, cell_inds: np.ndarray, gene_inds: np.ndarray) -> np.ndarray:
def get_m_indices(self,
cell_inds: Union[np.ndarray, torch.Tensor],
gene_inds: Union[np.ndarray, torch.Tensor]) -> Union[np.ndarray, torch.Tensor]:
"""Given arrays of cell indices and gene indices, suitable for a sparse matrix,
convert them to 'm' index values.
"""
Expand All @@ -1528,7 +1530,12 @@ def get_m_indices(self, cell_inds: np.ndarray, gene_inds: np.ndarray) -> np.ndar
if not ((gene_inds >= 0) & (gene_inds < self.total_n_genes)).all():
raise ValueError(f'Requested gene_inds out of range: '
f'{gene_inds[(gene_inds < 0) | (gene_inds >= self.total_n_genes)]}')
return cell_inds.astype(np.uint64) * self.total_n_genes + gene_inds.astype(np.uint64)
if type(cell_inds) == np.ndarray:
return cell_inds.astype(np.uint64) * self.total_n_genes + gene_inds.astype(np.uint64)
elif type(cell_inds) == torch.Tensor:
return cell_inds.type(torch.int64) * self.total_n_genes + gene_inds.type(torch.int64)
else:
raise ValueError('IndexConverter.get_m_indices received cell_inds of unkown object type')

def get_ng_indices(self, m_inds: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""Given a list of 'm' index values, return two arrays: cell index values
Expand Down
25 changes: 15 additions & 10 deletions cellbender/remove_background/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import subprocess
import datetime
import os
import shutil
import logging
from typing import Dict, Optional

Expand All @@ -42,26 +43,30 @@


def _run_notebook(file):
subprocess.run(f'cp {file} tmp.report.ipynb', shell=True)
shutil.copy(file, 'tmp.report.ipynb')
subprocess.run(run_notebook_str(file='tmp.report.ipynb'), shell=True)
subprocess.run(f'rm tmp.report.ipynb', shell=True)
os.remove('tmp.report.ipynb')
return 'tmp.report.nbconvert.ipynb'


def _to_html(file, output) -> str:
subprocess.run(to_html_str(file=file, output=output), shell=True)
subprocess.run(f'mv {file.replace(".ipynb", ".html")} {output}', shell=True)
subprocess.run(f'rm {file}', shell=True)
os.replace(file.replace(".ipynb", ".html"), output)
os.remove(file)
return output


def _postprocess_html(file: str, title: str):
with open(file, mode='r') as f:
html = f.read()
html = html.replace('<title>tmp.report.nbconvert</title>',
f'<title>{title}</title>')
with open(file, mode='w') as f:
f.write(html)
try:
with open(file, mode='r', encoding="utf8", errors="surrogateescape") as f:
html = f.read()
html = html.replace('<title>tmp.report.nbconvert</title>',
f'<title>{title}</title>')
with open(file, mode='w', encoding="utf8", errors="surrogateescape") as f:
f.write(html)
except:
logger.warning('Failed to overwrite default HTML report title. '
'This is purely aesthetic and does not affect output.')


def run_notebook_make_html(file, output) -> str:
Expand Down
1 change: 1 addition & 0 deletions cellbender/remove_background/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from typing import Tuple, Optional, Dict, Union

import matplotlib
import matplotlib.backends.backend_pdf # issue #287
matplotlib.use('Agg')
import matplotlib.pyplot as plt # This needs to be after matplotlib.use('Agg')

Expand Down