Example #1
0
def test_10X_zip_not_a_file():
    utils.assert_raises_message(
        FileNotFoundError,
        "No such file: 'not_a_file.zip'",
        scprep.io.load_10X_zip,
        "not_a_file.zip",
    )
Example #2
0
def test_10X_zip_url_404():
    utils.assert_raises_message(
        urllib.error.HTTPError,
        "HTTP Error 404: Not Found",
        scprep.io.load_10X_zip,
        "https://github.com/KrishnaswamyLab/scprep/invalid_url",
    )
Example #3
0
def test_check_numeric_inplace():
    X = data.load_10X()
    matrix.test_matrix_types(
        X,
        utils.assert_transform_unchanged,
        matrix._scipy_matrix_types + matrix._numpy_matrix_types +
        matrix._pandas_dense_matrix_types + [matrix.SparseDataFrame],
        transform=scprep.sanitize.check_numeric,
        copy=False,
    )
    if matrix._pandas_0:
        matrix._ignore_pandas_sparse_warning()
        utils.assert_raises_message(
            TypeError,
            "pd.SparseDataFrame does not support "
            "copy=False. Please use copy=True.",
            scprep.sanitize.check_numeric,
            data=matrix.SparseDataFrame_deprecated(X),
            copy=False,
        )
        matrix._reset_warnings()

    class TypeErrorClass(object):
        def astype(self, dtype):
            return

    X = TypeErrorClass()
    utils.assert_raises_message(
        TypeError,
        "astype() got an unexpected keyword argument 'copy'",
        scprep.sanitize.check_numeric,
        data=X,
        copy=None,
    )
Example #4
0
def test_mean_difference():
    X = data.load_10X()
    X = scprep.filter.filter_empty_genes(X)
    Y = scprep.stats.mean_difference(X.iloc[:20], X.iloc[20:100])
    assert np.allclose(np.max(Y), 16.8125)
    assert np.allclose(np.min(Y), -0.5625)

    def test_fun(X, **kwargs):
        return scprep.stats.mean_difference(
            scprep.select.select_rows(X, idx=np.arange(20)),
            scprep.select.select_rows(X, idx=np.arange(20, 100)),
            **kwargs,
        )

    matrix.test_all_matrix_types(
        X,
        utils.assert_transform_equals,
        Y=Y,
        transform=test_fun,
        check=utils.assert_all_close,
    )
    utils.assert_raises_message(
        ValueError,
        "Expected X and Y to have the same number of columns. "
        "Got shapes {}, {}".format(X.shape, X.iloc[:, :10].shape),
        scprep.stats.mean_difference,
        X,
        X.iloc[:, :10],
    )
Example #5
0
def test_check_numeric_bad_dtype():
    utils.assert_raises_message(
        ValueError,
        "could not convert string to float: ",
        scprep.sanitize.check_numeric,
        np.array(["hello", "world"]),
    )
Example #6
0
def test_10X_zip_url_not_a_zip():
    utils.assert_raises_message(
        zipfile.BadZipFile,
        "File is not a zip file",
        scprep.io.load_10X_zip,
        "https://github.com/KrishnaswamyLab/scprep/raw/master/data/test_data/test_10X",
    )
Example #7
0
def test_get_values_invalid():
    utils.assert_raises_message(
        TypeError,
        "Expected h5py.Dataset or tables.CArray. Got <class 'str'>",
        scprep.io.hdf5.get_values,
        "invalid",
    )
Example #8
0
 def test_subsample_n_too_large(self):
     utils.assert_raises_message(
         ValueError,
         "Expected n (101) <= n_samples (100)",
         scprep.select.subsample,
         self.X,
         n=self.X.shape[0] + 1,
     )
Example #9
0
 def test_select_cols_invalid_index(self):
     utils.assert_raises_message(
         KeyError,
         "'not_a_gene'",
         scprep.select.select_cols,
         self.X,
         idx="not_a_gene",
     )
Example #10
0
 def test_select_rows_invalid_index(self):
     utils.assert_raises_message(
         KeyError,
         "'not_a_cell'",
         scprep.select.select_rows,
         self.X,
         idx="not_a_cell",
     )
Example #11
0
def test_sqrt_negative_value():
    X = np.arange(10) * -1
    utils.assert_raises_message(
        ValueError,
        "Cannot square root transform negative values",
        scprep.transform.sqrt,
        data=X,
    )
Example #12
0
def test_list_nodes_invalid():
    utils.assert_raises_message(
        TypeError,
        "Expected h5py.File, tables.File, h5py.Group or "
        "tables.Group. Got <class 'str'>",
        scprep.io.hdf5.list_nodes,
        "invalid",
    )
Example #13
0
 def test_dyngen_backbone_not_in_list(self):
     utils.assert_raises_message(
         ValueError,
         "Input not in default backbone list. "
         "Choose backbone from get_backbones()",
         scprep.run.DyngenSimulate,
         backbone="not_a_backbone",
         verbose=False,
     )
Example #14
0
 def test_slingshot_distance(self):
     utils.assert_raises_message(
         NotImplementedError,
         "distance argument not currently implemented",
         scprep.run.Slingshot,
         self.X_pca,
         self.clusters,
         distance=lambda X, Y: np.sum(X - Y),
     )
Example #15
0
 def test_select_cols_2d_list_index(self):
     utils.assert_raises_message(
         ValueError,
         "Expected idx to be 1D. "
         "Got shape (2, {})".format(self.X.shape[1]),
         scprep.select.select_cols,
         self.X,
         idx=[self.X.columns, self.X.columns],
     )
Example #16
0
 def test_select_rows_2d_dataframe_index(self):
     utils.assert_raises_message(
         ValueError,
         "Expected idx to be 1D. "
         "Got shape (2, {})".format(self.X.shape[0]),
         scprep.select.select_rows,
         self.X,
         idx=pd.DataFrame([self.X.index, self.X.index]),
     )
Example #17
0
 def test_select_rows_unequal_rows(self):
     utils.assert_raises_message(
         ValueError,
         "Expected `data` and `extra_data` to have the same number of "
         "rows. Got [100, 50]",
         scprep.select.select_rows,
         self.X,
         self.X.to_numpy()[:50, :],
     )
Example #18
0
 def test_invalid_method(self):
     utils.assert_raises_message(
         ValueError,
         "Expected `method` in ['svd', 'orth_rproj', 'rproj']. "
         "Got 'invalid'",
         scprep.reduce.pca,
         self.X_sparse,
         method="invalid",
     )
Example #19
0
def test_10X_HDF5_invalid_gene_labels():
    h5_file = os.path.join(data.data_dir, "test_10X.h5")
    utils.assert_raises_message(
        ValueError,
        "gene_labels='invalid' not recognized. " "Choose from ['symbol', 'id', 'both']",
        scprep.io.load_10X_HDF5,
        filename=h5_file,
        gene_labels="invalid",
    )
Example #20
0
def test_10X_HDF5_invalid_genome():
    h5_file = os.path.join(data.data_dir, "test_10X.h5")
    utils.assert_raises_message(
        ValueError,
        "Genome invalid not found in {}. " "Available genomes: GRCh38".format(h5_file),
        scprep.io.load_10X_HDF5,
        filename=h5_file,
        genome="invalid",
    )
Example #21
0
def test_10X_HDF5_invalid_backend():
    h5_file = os.path.join(data.data_dir, "test_10X.h5")
    utils.assert_raises_message(
        ValueError,
        "Expected backend in ['tables', 'h5py']. Got invalid",
        scprep.io.load_10X_HDF5,
        filename=h5_file,
        backend="invalid",
    )
Example #22
0
 def test_select_rows_get_cell_set_ndarray_data(self):
     utils.assert_raises_message(
         ValueError,
         "Can only select based on row names with DataFrame input. "
         "Please set `idx` to select specific rows.",
         scprep.select.select_rows,
         self.X.to_numpy(),
         starts_with="A",
     )
Example #23
0
 def test_install_bioc():
     utils.assert_raises_message(
         rpy2.rinterface_lib.embedded.RRuntimeError,
         "Error: Bioconductor version '3.1' requires R version '3.2'; see",
         scprep.run.install_bioconductor,
         version="3.1",
         site_repository="https://bioconductor.org/packages/3.1/bioc",
         verbose=False,
     )
Example #24
0
 def test_get_cell_set_ndarray(self):
     utils.assert_raises_message(
         TypeError,
         "data must be a list of cell names or a pandas "
         "DataFrame. Got ndarray",
         scprep.select.get_cell_set,
         data=self.X.to_numpy(),
         regex="G\\-1$",
     )
Example #25
0
 def test_libsize_norm_rescale_invalid(self):
     utils.assert_raises_message(
         ValueError,
         "Expected rescale in ['median', 'mean'], a number or `None`. "
         "Got invalid",
         scprep.normalize.library_size_normalize,
         self.X,
         rescale="invalid",
     )
Example #26
0
 def test_select_rows_conflicting_data(self):
     utils.assert_raises_message(
         ValueError,
         "Expected `data` and `extra_data` pandas inputs to have the same "
         "index. Fix with "
         "`scprep.select.select_rows(*extra_data, idx=data.index)`",
         scprep.select.select_rows,
         self.X,
         self.X.iloc[::-1],
     )
Example #27
0
def test_fcs_naming_error():
    path = fcsparser.test_sample_path
    utils.assert_raises_message(
        ValueError,
        "Expected channel_naming in ['$PnS', '$PnN']. " "Got 'invalid'",
        scprep.io.load_fcs,
        path,
        override=True,
        channel_naming="invalid",
    )
Example #28
0
def test_matrix_elementwise_multiply_invalid_axis():
    X = data.generate_positive_sparse_matrix(shape=(50, 100))
    utils.assert_raises_message(
        ValueError,
        "Expected axis in [0, 1, None]. Got 5",
        scprep.utils.matrix_vector_elementwise_multiply,
        X,
        X[0],
        axis=5,
    )
Example #29
0
def test_matrix_elementwise_multiply_guess_wrong_size():
    X = data.generate_positive_sparse_matrix(shape=(50, 100))
    utils.assert_raises_message(
        ValueError,
        "Expected `multiplier` to be a vector of length `data.shape[0]` (50) "
        "or `data.shape[1]` (100). Got (10,)",
        scprep.utils.matrix_vector_elementwise_multiply,
        X,
        X[0, :10],
    )
Example #30
0
def test_failed_import_both():
    with mock.patch.dict(sys.modules, {"tables": None, "h5py": None}):
        utils.assert_raises_message(
            ImportError,
            "Found neither tables nor h5py. "
            "Please install one of them with e.g. "
            "`pip install --user tables` or "
            "`pip install --user h5py`",
            hdf5_available,
        )