Module emobject.tests.test_emobject

Expand source code
from emobject import core
from emobject.emimage import EMImage
import numpy as np
import pandas as pd
from emobject.errors import EMObjectException
import pytest

#  All of these tests use a legacy zarr file.


def test_emobject_open():
    """Test opening an EMObject from a file"""

    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    assert E.var.index.to_list() == ["DAPI", "PANCK"]
    assert E.data.shape == (2537, 2)
    assert len(E.layers) == 1
    assert list(E.mask.mask_names) == ["Tumor", "segmentation_mask"]
    assert E.obs.shape == (2537, 6)
    assert E.img.img.shape == (2, 1440, 1920)
    # assert E.meta.shape == (23, 3)
    assert E.pos[list(E.pos.keys())[0]].shape == (2537, 2)
    assert isinstance(E.pos, dict)
    assert isinstance(E.data, pd.DataFrame)
    assert isinstance(E.var, pd.DataFrame)
    assert isinstance(E.obs, pd.DataFrame)
    assert isinstance(E.img, EMImage)


def test_add_single_annotation_to_obs():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E._obs_ax.shape[0])]
    E.add_anno(attr="obs", value=test_annotation, name="test_annotation")

    assert E.obs["test_annotation"].to_list() == test_annotation


def test_add_single_annotation_to_var():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E.n_var)]
    E.add_anno(attr="var", value=test_annotation, name="test_annotation")

    assert E.var["test_annotation"].to_list() == test_annotation


def test_add_df_annotation_to_var():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E.n_var)]
    test_annotation2 = [False for x in range(0, E.n_var)]
    test_annotation_df = pd.DataFrame(
        {"test1": test_annotation, "test2": test_annotation2}, index=E.var.index
    )

    E.add_anno(attr="var", value=test_annotation_df)

    assert np.array_equal(E.var["test1"].to_numpy(), np.array(test_annotation))
    assert np.array_equal(E.var["test2"].to_numpy(), np.array(test_annotation2))


def test_add_df_annotation_to_obs():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E.n_obs)]
    test_annotation2 = [False for x in range(0, E.n_obs)]
    test_annotation_df = pd.DataFrame(
        {"test1": test_annotation, "test2": test_annotation2}, index=E.obs.index
    )

    E.add_anno(attr="obs", value=test_annotation_df)

    assert np.array_equal(E.obs["test1"].to_numpy(), np.array(test_annotation))
    assert np.array_equal(E.obs["test2"].to_numpy(), np.array(test_annotation2))


def test_add_vector_annotation_to_sobs():
    """E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E.var_ax.shape[0])]
    E.add_anno(
        attr='sobs',
        value=test_annotation,
        name="test_annotation")"""
    pass


def test_add_multiple_annotation_to_obs():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    multiple_annos = np.ones((E.n_obs, 2))
    multiple_annos[:, 0] = 2
    E.add_anno(attr="obs", value=multiple_annos, name=["test1", "test2"])

    assert np.all(E.obs["test1"].to_list() == multiple_annos[:, 0])
    assert np.all(E.obs["test2"].to_list() == multiple_annos[:, 1])


def test_add_multiple_annotation_to_var():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    multiple_annos = np.ones((E.n_var, 2))
    multiple_annos[:, 0] = 2
    E.add_anno(attr="var", value=multiple_annos, name=["test1", "test2"])

    assert np.all(E.var["test1"].to_list() == multiple_annos[:, 0])
    assert np.all(E.var["test2"].to_list() == multiple_annos[:, 1])


def test_add_multiple_annotation_to_sobs():
    """E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    multiple_annos = np.ones(E.n_sobs, 2)
    multiple_annos[:, 0] = 2
    E.add_anno(
        attr='sobs',
        value=multiple_annos,
        name=['test1', 'test2'])

    assert E.sobs['test1'].to_list() == multiple_annos[:, 0]
    assert E.sobs['test2'].to_list() == multiple_annos[:, 1]"""
    pass


def test_remove_single_annotation_from_obs():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    anno_to_remove = "2177_BD-Updated-Labels---PD-Subsets"
    initial_shape = E.obs.shape
    E.del_anno(attr="obs", name=anno_to_remove)
    assert anno_to_remove not in E.obs.columns
    assert E.obs.shape[0] == initial_shape[0]
    assert E.obs.shape[1] == initial_shape[1] - 1


def test_remove_single_annotation_from_var():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E.n_var)]
    E.add_anno(attr="var", value=test_annotation, name="test_annotation")
    initial_shape = E.var.shape
    anno_to_remove = "test_annotation"
    E.del_anno(attr="var", name=anno_to_remove)
    assert anno_to_remove not in E.var.columns
    assert E.var.shape[0] == initial_shape[0]
    assert E.var.shape[1] == initial_shape[1] - 1


def test_remove_single_annotation_from_sobs():
    pass


def test_loc():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    # test var
    E2 = E.loc(var_subset=["DAPI"])
    assert E2.var.index.to_list() == ["DAPI"]
    del E2

    # test obs
    E2 = E.loc(obs_subset=[2531])
    assert E2.data["DAPI"].to_list() == [2768.3806640625]

    # TODO: test mask

    # TODO: test segments


def test_add_layer():
    pass


def test_set_layer():
    pass


def test_remove_layer():
    pass


def test_add_coordinate_system():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_key = list(E.pos.keys())[0]
    test_coords = np.ones(E.pos[test_key].shape)
    test_name = "test_coords"
    E.add_coordinate_system(label=test_name, coords=test_coords, cols=["x", "y"])
    assert len(E.pos) == 2
    assert test_name in E.pos.keys()
    assert np.sum(E.pos["test_coords"].to_numpy() == test_coords) == 5074


def test_X_attribute():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    assert np.all(E.X == E.data)


def test_mask_orientation():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    img_shape = E.img.img.shape
    for mask_name in E.mask.mask_names:
        mask = E.mask.mloc(mask_name)
        assert mask.shape == img_shape[1:]


def test_drop_var():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    assert "DAPI" in E.var.index.to_list()
    assert "DAPI" in E.data.columns.to_list()
    assert "DAPI" in E.var_ax
    E.drop_var(["DAPI"])
    assert "DAPI" not in E.var.index.to_list()
    assert "DAPI" not in E.data.columns.to_list()
    assert "DAPI" not in E.var_ax


def test_drop_obs():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    assert 323 in E.obs.index.to_list()
    assert 323 in E.data.index.to_list()
    assert 323 in E.obs_ax
    E.drop_obs([323])
    assert 323 not in E.obs.index.to_list()
    assert 323 not in E.data.index.to_list()
    assert 323 not in E.obs_ax


def test_add_measurements():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")

    new_measurements = pd.DataFrame(data={"test": np.ones(E.n_obs)}, index=E.obs.index)
    E.add_measurements(new_measurements, "test")
    assert "test" in E.data.columns.to_list()
    assert "test" in E.var_ax
    assert np.all(E.data["test"].to_numpy() == np.ones((E.n_obs, 1)))
    assert "test" in E.var.index.to_list()


def test_add_mask():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    E.mask.add_mask(mask_name="Ones", mask=np.ones(E.mask.dims))
    assert "Ones" in list(E.mask.mask_names)
    assert np.array_equal(E.mask.mloc(mask_name="Ones"), np.ones(E.mask.dims))


def test_add_mask_existing_name():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    with pytest.raises(EMObjectException, match="Mask name Tumor already exists"):
        E.mask.add_mask(mask_name="Tumor", mask=np.ones(E.mask.dims))


def test_add_mask_overwrite_existing():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    E.mask.add_mask(mask_name="Tumor", mask=np.ones(E.mask.dims), overwrite=True)
    assert np.array_equal(E.mask.mloc(mask_name="Tumor"), np.ones(E.mask.dims))


def test_remove_mask():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    E.mask.remove_mask(mask_name="Tumor")
    assert "Tumor" not in list(E.mask.mask_names)

Functions

def test_X_attribute()
Expand source code
def test_X_attribute():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    assert np.all(E.X == E.data)
def test_add_coordinate_system()
Expand source code
def test_add_coordinate_system():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_key = list(E.pos.keys())[0]
    test_coords = np.ones(E.pos[test_key].shape)
    test_name = "test_coords"
    E.add_coordinate_system(label=test_name, coords=test_coords, cols=["x", "y"])
    assert len(E.pos) == 2
    assert test_name in E.pos.keys()
    assert np.sum(E.pos["test_coords"].to_numpy() == test_coords) == 5074
def test_add_df_annotation_to_obs()
Expand source code
def test_add_df_annotation_to_obs():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E.n_obs)]
    test_annotation2 = [False for x in range(0, E.n_obs)]
    test_annotation_df = pd.DataFrame(
        {"test1": test_annotation, "test2": test_annotation2}, index=E.obs.index
    )

    E.add_anno(attr="obs", value=test_annotation_df)

    assert np.array_equal(E.obs["test1"].to_numpy(), np.array(test_annotation))
    assert np.array_equal(E.obs["test2"].to_numpy(), np.array(test_annotation2))
def test_add_df_annotation_to_var()
Expand source code
def test_add_df_annotation_to_var():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E.n_var)]
    test_annotation2 = [False for x in range(0, E.n_var)]
    test_annotation_df = pd.DataFrame(
        {"test1": test_annotation, "test2": test_annotation2}, index=E.var.index
    )

    E.add_anno(attr="var", value=test_annotation_df)

    assert np.array_equal(E.var["test1"].to_numpy(), np.array(test_annotation))
    assert np.array_equal(E.var["test2"].to_numpy(), np.array(test_annotation2))
def test_add_layer()
Expand source code
def test_add_layer():
    pass
def test_add_mask()
Expand source code
def test_add_mask():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    E.mask.add_mask(mask_name="Ones", mask=np.ones(E.mask.dims))
    assert "Ones" in list(E.mask.mask_names)
    assert np.array_equal(E.mask.mloc(mask_name="Ones"), np.ones(E.mask.dims))
def test_add_mask_existing_name()
Expand source code
def test_add_mask_existing_name():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    with pytest.raises(EMObjectException, match="Mask name Tumor already exists"):
        E.mask.add_mask(mask_name="Tumor", mask=np.ones(E.mask.dims))
def test_add_mask_overwrite_existing()
Expand source code
def test_add_mask_overwrite_existing():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    E.mask.add_mask(mask_name="Tumor", mask=np.ones(E.mask.dims), overwrite=True)
    assert np.array_equal(E.mask.mloc(mask_name="Tumor"), np.ones(E.mask.dims))
def test_add_measurements()
Expand source code
def test_add_measurements():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")

    new_measurements = pd.DataFrame(data={"test": np.ones(E.n_obs)}, index=E.obs.index)
    E.add_measurements(new_measurements, "test")
    assert "test" in E.data.columns.to_list()
    assert "test" in E.var_ax
    assert np.all(E.data["test"].to_numpy() == np.ones((E.n_obs, 1)))
    assert "test" in E.var.index.to_list()
def test_add_multiple_annotation_to_obs()
Expand source code
def test_add_multiple_annotation_to_obs():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    multiple_annos = np.ones((E.n_obs, 2))
    multiple_annos[:, 0] = 2
    E.add_anno(attr="obs", value=multiple_annos, name=["test1", "test2"])

    assert np.all(E.obs["test1"].to_list() == multiple_annos[:, 0])
    assert np.all(E.obs["test2"].to_list() == multiple_annos[:, 1])
def test_add_multiple_annotation_to_sobs()

E = core.load("./data/Charville_c001_v001_r001_reg002.zarr") multiple_annos = np.ones(E.n_sobs, 2) multiple_annos[:, 0] = 2 E.add_anno( attr='sobs', value=multiple_annos, name=['test1', 'test2'])

assert E.sobs['test1'].to_list() == multiple_annos[:, 0] assert E.sobs['test2'].to_list() == multiple_annos[:, 1]

Expand source code
def test_add_multiple_annotation_to_sobs():
    """E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    multiple_annos = np.ones(E.n_sobs, 2)
    multiple_annos[:, 0] = 2
    E.add_anno(
        attr='sobs',
        value=multiple_annos,
        name=['test1', 'test2'])

    assert E.sobs['test1'].to_list() == multiple_annos[:, 0]
    assert E.sobs['test2'].to_list() == multiple_annos[:, 1]"""
    pass
def test_add_multiple_annotation_to_var()
Expand source code
def test_add_multiple_annotation_to_var():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    multiple_annos = np.ones((E.n_var, 2))
    multiple_annos[:, 0] = 2
    E.add_anno(attr="var", value=multiple_annos, name=["test1", "test2"])

    assert np.all(E.var["test1"].to_list() == multiple_annos[:, 0])
    assert np.all(E.var["test2"].to_list() == multiple_annos[:, 1])
def test_add_single_annotation_to_obs()
Expand source code
def test_add_single_annotation_to_obs():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E._obs_ax.shape[0])]
    E.add_anno(attr="obs", value=test_annotation, name="test_annotation")

    assert E.obs["test_annotation"].to_list() == test_annotation
def test_add_single_annotation_to_var()
Expand source code
def test_add_single_annotation_to_var():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E.n_var)]
    E.add_anno(attr="var", value=test_annotation, name="test_annotation")

    assert E.var["test_annotation"].to_list() == test_annotation
def test_add_vector_annotation_to_sobs()

E = core.load("./data/Charville_c001_v001_r001_reg002.zarr") test_annotation = [True for x in range(0, E.var_ax.shape[0])] E.add_anno( attr='sobs', value=test_annotation, name="test_annotation")

Expand source code
def test_add_vector_annotation_to_sobs():
    """E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E.var_ax.shape[0])]
    E.add_anno(
        attr='sobs',
        value=test_annotation,
        name="test_annotation")"""
    pass
def test_drop_obs()
Expand source code
def test_drop_obs():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    assert 323 in E.obs.index.to_list()
    assert 323 in E.data.index.to_list()
    assert 323 in E.obs_ax
    E.drop_obs([323])
    assert 323 not in E.obs.index.to_list()
    assert 323 not in E.data.index.to_list()
    assert 323 not in E.obs_ax
def test_drop_var()
Expand source code
def test_drop_var():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    assert "DAPI" in E.var.index.to_list()
    assert "DAPI" in E.data.columns.to_list()
    assert "DAPI" in E.var_ax
    E.drop_var(["DAPI"])
    assert "DAPI" not in E.var.index.to_list()
    assert "DAPI" not in E.data.columns.to_list()
    assert "DAPI" not in E.var_ax
def test_emobject_open()

Test opening an EMObject from a file

Expand source code
def test_emobject_open():
    """Test opening an EMObject from a file"""

    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    assert E.var.index.to_list() == ["DAPI", "PANCK"]
    assert E.data.shape == (2537, 2)
    assert len(E.layers) == 1
    assert list(E.mask.mask_names) == ["Tumor", "segmentation_mask"]
    assert E.obs.shape == (2537, 6)
    assert E.img.img.shape == (2, 1440, 1920)
    # assert E.meta.shape == (23, 3)
    assert E.pos[list(E.pos.keys())[0]].shape == (2537, 2)
    assert isinstance(E.pos, dict)
    assert isinstance(E.data, pd.DataFrame)
    assert isinstance(E.var, pd.DataFrame)
    assert isinstance(E.obs, pd.DataFrame)
    assert isinstance(E.img, EMImage)
def test_loc()
Expand source code
def test_loc():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    # test var
    E2 = E.loc(var_subset=["DAPI"])
    assert E2.var.index.to_list() == ["DAPI"]
    del E2

    # test obs
    E2 = E.loc(obs_subset=[2531])
    assert E2.data["DAPI"].to_list() == [2768.3806640625]

    # TODO: test mask

    # TODO: test segments
def test_mask_orientation()
Expand source code
def test_mask_orientation():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    img_shape = E.img.img.shape
    for mask_name in E.mask.mask_names:
        mask = E.mask.mloc(mask_name)
        assert mask.shape == img_shape[1:]
def test_remove_layer()
Expand source code
def test_remove_layer():
    pass
def test_remove_mask()
Expand source code
def test_remove_mask():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    E.mask.remove_mask(mask_name="Tumor")
    assert "Tumor" not in list(E.mask.mask_names)
def test_remove_single_annotation_from_obs()
Expand source code
def test_remove_single_annotation_from_obs():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    anno_to_remove = "2177_BD-Updated-Labels---PD-Subsets"
    initial_shape = E.obs.shape
    E.del_anno(attr="obs", name=anno_to_remove)
    assert anno_to_remove not in E.obs.columns
    assert E.obs.shape[0] == initial_shape[0]
    assert E.obs.shape[1] == initial_shape[1] - 1
def test_remove_single_annotation_from_sobs()
Expand source code
def test_remove_single_annotation_from_sobs():
    pass
def test_remove_single_annotation_from_var()
Expand source code
def test_remove_single_annotation_from_var():
    E = core.load("./data/Charville_c001_v001_r001_reg002.zarr")
    test_annotation = [True for x in range(0, E.n_var)]
    E.add_anno(attr="var", value=test_annotation, name="test_annotation")
    initial_shape = E.var.shape
    anno_to_remove = "test_annotation"
    E.del_anno(attr="var", name=anno_to_remove)
    assert anno_to_remove not in E.var.columns
    assert E.var.shape[0] == initial_shape[0]
    assert E.var.shape[1] == initial_shape[1] - 1
def test_set_layer()
Expand source code
def test_set_layer():
    pass