Skip to content

Commit

Permalink
Allow passing a h5ad object directly into the constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
asistradition committed Aug 15, 2023
1 parent 97f9089 commit 16361c3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 26 deletions.
6 changes: 5 additions & 1 deletion inferelator/benchmarking/scenic.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class SCENICWorkflow(SingleCellWorkflow):

do_scenic = True

_do_preprocessing = True
_do_scaling = True

dask_temp_path = None
_tmp_handle = None

Expand Down Expand Up @@ -119,7 +122,8 @@ def startup_finish(self):

sc.pp.log1p(self.data._adata)

sc.pp.scale(self.data._adata, max_value=10)
if self._do_scaling:
sc.pp.scale(self.data._adata, max_value=10)

def create_feather_file_from_prior(self):

Expand Down
9 changes: 9 additions & 0 deletions inferelator/tests/test_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import tempfile
import pandas as pd
import anndata as ad
import numpy as np
import numpy.testing as npt
import pandas.testing as pdt
Expand Down Expand Up @@ -35,6 +36,14 @@ def test_h5ad(self):

npt.assert_array_almost_equal(data.values, self.worker.data.expression_data)

def test_h5ad_obj(self):
_, data = test_prebuilt.counts_yeast_single_cell_chr01(filetype='h5ad')

self.worker.set_expression_file(h5ad=ad.AnnData(data))
self.worker.read_expression()

npt.assert_array_almost_equal(data.values, self.worker.data.expression_data)

def test_hdf5(self):
file, data = test_prebuilt.counts_yeast_single_cell_chr01(filetype='hdf5')

Expand Down
45 changes: 25 additions & 20 deletions inferelator/utils/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,25 @@ def load_data_h5ad(
:rtype: InferelatorData
"""

if isinstance(h5ad_file, anndata.AnnData):
data = h5ad_file
Debug.vprint(
f"Using AnnData object {data.shape} "
f"(Layer: {use_layer if use_layer is not None else 'X'})",
level=0
)

# Read H5AD file
Debug.vprint(
f"Loading AnnData file {h5ad_file} "
f"(Layer: {use_layer if use_layer is not None else 'X'})",
level=0
)
else:
Debug.vprint(
f"Loading AnnData file {h5ad_file} "
f"(Layer: {use_layer if use_layer is not None else 'X'})",
level=0
)

with warnings.catch_warnings():
warnings.simplefilter("ignore", anndata.OldFormatWarning)
data = anndata.read_h5ad(self.input_path(h5ad_file))
with warnings.catch_warnings():
warnings.simplefilter("ignore", anndata.OldFormatWarning)
data = anndata.read_h5ad(self.input_path(h5ad_file))

# Read in meta data
if meta_data_file is None and data.obs.shape[1] > 0:
Expand All @@ -113,28 +122,24 @@ def load_data_h5ad(
f"Layer {use_layer} is not in {h5ad_file}"
)

# Build an InferelatorData object from a layer
elif use_layer is not None:
data = InferelatorData(
data.layers[use_layer].copy() if use_layer != "X" else data.X,
gene_names=data.var_names.copy(),
sample_names=data.obs_names.copy(),
meta_data=pd.concat((data.obs, meta_data), axis=1),
gene_data=pd.concat((data.var, gene_metadata), axis=1)
)
if use_layer != "X":
data_ref = data.layers[use_layer]

Check warning on line 126 in inferelator/utils/loader.py

View check run for this annotation

Codecov / codecov/patch

inferelator/utils/loader.py#L126

Added line #L126 was not covered by tests
else:
data_ref = data.X

# Build an InferelatorData object from everything
elif keep_other_data:
if keep_other_data:
data.X = data_ref

Check warning on line 132 in inferelator/utils/loader.py

View check run for this annotation

Codecov / codecov/patch

inferelator/utils/loader.py#L132

Added line #L132 was not covered by tests
data = InferelatorData(
data,
meta_data=meta_data,
gene_data=gene_metadata
)

# Build an InferelatorData object from only the `X` data
# Build an InferelatorData object from a layer
else:
data = InferelatorData(
data.X.copy(),
data_ref.copy(),
gene_names=data.var_names.copy(),
sample_names=data.obs_names.copy(),
meta_data=pd.concat((data.obs, meta_data), axis=1),
Expand Down
5 changes: 2 additions & 3 deletions inferelator/workflow.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""
Construct inferelator workflows from preprocessing, postprocessing, and regression modules
Construct inferelator workflows from preprocessing,
postprocessing, and regression modules
"""

import inspect

from inferelator.utils import is_string
from inferelator.workflows.workflow_base import (
WorkflowBase,
WorkflowBaseLoader
)
from inferelator.regression.base_regression import _RegressionWorkflowMixin

Expand Down Expand Up @@ -151,7 +151,6 @@ class InferelatorRegressWorkflow(workflow_class):

regression_class = AMUSRRegressionWorkflowMixin


elif regression == "stars":

from inferelator.regression.stability_selection import (
Expand Down
25 changes: 23 additions & 2 deletions inferelator/workflows/workflow_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def set_expression_file(
self._expression_loader = _HDF5
self._h5_layer = h5_layer
elif h5ad is not None:
self._set_file_name("expression_matrix_file", h5ad)
self._set_expression("expression_matrix_file", h5ad)
self._expression_loader = _H5AD
self._h5_layer = h5_layer
elif mtx is not None:
Expand Down Expand Up @@ -488,7 +488,7 @@ def _set_file_name(
"""

if file_name is None:
return
return None

if file_name not in self._file_format_settings:
self._file_format_settings[file_name] = copy.copy(
Expand All @@ -498,6 +498,27 @@ def _set_file_name(
self._check_file_exists(file_name)
self._set_with_warning(attr_name, file_name)

def _set_expression(
self,
attr_name,
file_or_object
):

if file_or_object is None:
return None

Check warning on line 508 in inferelator/workflows/workflow_base.py

View check run for this annotation

Codecov / codecov/patch

inferelator/workflows/workflow_base.py#L508

Added line #L508 was not covered by tests

if isinstance(file_or_object, str):
self._set_file_name(
attr_name,
file_or_object
)

else:
self._set_with_warning(
attr_name,
file_or_object
)

def _check_file_exists(
self,
file_name
Expand Down

0 comments on commit 16361c3

Please sign in to comment.