Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_anndata(self):
ad = self.ds.to_anndata()
ad.write_h5ad("./test.h5ad")
ds = cb.data.ExprDataSet.from_anndata(anndata.read_h5ad("./test.h5ad"))
self._compare_datasets(self.ds, ds)
with open(post_type_path, "r") as post_file:
post_class_str = post_file.readline()
str_to_classes = dict(
TotalPosterior=TotalPosterior,
JPosterior=JPosterior,
Posterior=Posterior,
AnnotationPosterior=AnnotationPosterior,
)
if post_class_str not in str_to_classes:
raise ValueError(
"Posterior type {} not eligible for loading".format(post_class_str)
)
post_class = str_to_classes[post_class_str]
# Loading dataset and associated measurements
ad = anndata.read_h5ad(filename=dataset_path)
key = "cell_measurements_col_mappings"
if key in ad.uns:
cell_measurements_col_mappings = ad.uns[key]
else:
cell_measurements_col_mappings = dict()
dataset = AnnDatasetFromAnnData(
ad=ad, cell_measurements_col_mappings=cell_measurements_col_mappings
)
# Loading scVI model
if use_cuda == "auto":
use_cuda = torch.cuda.is_available()
use_cuda = use_cuda and torch.cuda.is_available()
if use_cuda:
model.load_state_dict(torch.load(model_path))
model.cuda()
def _download_adata(url) -> AnnData:
response = requests.get(url)
f = BytesIO(response.content)
return read_h5ad(f)
def _load_data(self, data_locator):
# as of AnnData 0.6.19, backed mode performs initial load fast, but at the
# cost of significantly slower access to X data.
try:
# there is no guarantee data_locator indicates a local file. The AnnData
# API will only consume local file objects. If we get a non-local object,
# make a copy in tmp, and delete it after we load into memory.
with data_locator.local_handle() as lh:
# as of AnnData 0.6.19, backed mode performs initial load fast, but at the
# cost of significantly slower access to X data.
backed = 'r' if self.config['backed'] else None
self.data = anndata.read_h5ad(lh, backed=backed)
except ValueError:
raise ScanpyFileError(
"File must be in the .h5ad format. Please read "
"https://github.com/theislab/scanpy_usage/blob/master/170505_seurat/info_h5ad.md to "
"learn more about this format. You may be able to convert your file into this format "
"using `cellxgene prepare`, please run `cellxgene prepare --help` for more "
"information."
)
except MemoryError:
raise ScanpyFileError("Out of memory - file is too large for available memory.")
except Exception as e:
raise ScanpyFileError(
f"{e} - file not found or is inaccessible. File must be an .h5ad object. "
f"Please check your input and try again."
def populate(self):
ad = anndata.read_h5ad(
os.path.join(self.save_path, self.filenames[0])
) # obs = cells, var = genes
# extract GeneExpressionDataset relevant attributes
# and provide access to annotations from the underlying AnnData object.
(
X,
batch_indices,
labels,
gene_names,
cell_types,
obs,
obsm,
var,
_,
uns,