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

Added utility functions to update and remove a view #368

Merged
merged 4 commits into from
Oct 2, 2024
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
87 changes: 87 additions & 0 deletions tests/test_config_updates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import pytest

from vitessce import (
VitessceConfig,
ViewType as cm,
)


@pytest.fixture
def vitessce_config():
vc = VitessceConfig(schema_version="1.0.15")
my_dataset = vc.add_dataset(name='My Dataset')
vc.add_view(cm.SPATIAL, dataset=my_dataset)
vc.add_view(cm.SCATTERPLOT, dataset=my_dataset)
vc.add_view(cm.SCATTERPLOT, dataset=my_dataset)
return vc


def test_get_views(vitessce_config):
views = vitessce_config.get_views()
assert len(views) == 3
assert views[0].view["component"].lower() == "spatial"
assert views[1].view["component"].lower() == "scatterplot"


def test_get_view_by_index(vitessce_config):
view = vitessce_config.get_view_by_index(0)
vc_dict = view.to_dict()
assert vc_dict["component"] == "spatial"

view = vitessce_config.get_view_by_index(1)
vc_dict = view.to_dict()
assert vc_dict["component"] == "scatterplot"

with pytest.raises(IndexError):
vitessce_config.get_view_by_index(5)


def test_get_view_by_component(vitessce_config):
view = vitessce_config.get_first_view_by_type("spatial")
vc_dict = view.to_dict()
assert vc_dict["component"] == "spatial"

view = vitessce_config.get_first_view_by_type("SCATTERPLOT")
vc_dict = view.to_dict()
assert vc_dict["component"] == "scatterplot"

with pytest.raises(ValueError):
vitessce_config.get_first_view_by_type("TEST")


def test_get_view_by_invalid_type(vitessce_config):
with pytest.raises(TypeError):
vitessce_config.get_first_view_by_type(3.5)


def test_remove_view_by_index(vitessce_config):
removed_view = vitessce_config.remove_view_by_index(0)
rv_dict = removed_view.to_dict()
assert rv_dict["component"] == "spatial"
assert len(vitessce_config.get_views()) == 2

removed_view = vitessce_config.remove_view_by_index(1)
rv_dict = removed_view.to_dict()
assert rv_dict["component"] == "scatterplot"
assert len(vitessce_config.get_views()) == 1

with pytest.raises(IndexError):
vitessce_config.remove_view_by_index(5)


def test_remove_view_by_component(vitessce_config):
removed_view = vitessce_config.remove_first_view_by_type("spatial")
rv_dict = removed_view.to_dict()
assert rv_dict["component"] == "spatial"
assert len(vitessce_config.get_views()) == 2

with pytest.raises(ValueError):
vitessce_config.remove_first_view_by_type("spatial")

with pytest.raises(ValueError):
vitessce_config.remove_first_view_by_type("TEST")


def test_remove_view_by_invalid_index(vitessce_config):
with pytest.raises(TypeError):
vitessce_config.remove_view_by_index(3.5)
83 changes: 83 additions & 0 deletions vitessce/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,89 @@ def to_dict(self, base_url=None):
"layout": [c.to_dict() for c in self.config["layout"]]
}

def get_views(self):
"""
Provides all the views in the config.layout object list

:returns: A list of VitessceConfigView objects.

"""
return self.config["layout"]

def get_view_by_index(self, index):
"""
Get a view from the layout by the index specified by the 'index' parameter.

:param index: Index (int) of the view in the Layout array.
:type index: int

:returns: The view corresponding to the provided index
:rtype: VitessceConfigView or None if not found
"""
if isinstance(index, int):
if 0 <= index < len(self.config["layout"]):
return self.config["layout"][index]
else:
raise IndexError("index out of range")
else:
raise TypeError("index must be an integer")

def get_first_view_by_type(self, view_type):
"""
Get a view from the layout by view type (component) specified by the 'view_type' parameter.

:param view_type: The view type (str) of the view in the Layout array.
:type view_type: str

:returns: The view corresponding to the provided view_type.
:rtype: VitessceConfigView or None if not found
"""
if isinstance(view_type, str):
for view in self.config["layout"]:
if view.view["component"].lower() == view_type.lower():
return view
raise ValueError(f"No view found with component view_type: {view_type}")
else:
raise TypeError("view_type must be a string representing the view type")

def remove_view_by_index(self, index):
"""
Removes a view from the layout by the index specified by the 'index' parameter.

:param index: the index (int) of the view
:type index: int

:returns: The layout component of the config corresponding to the specified index
:rtype: VitessceConfigView or None if not found

"""
if isinstance(index, int):
if 0 <= index < len(self.config["layout"]):
return self.config["layout"].pop(index)
else:
raise IndexError("Index out of range")
else:
raise TypeError("index must be an integer")

def remove_first_view_by_type(self, view_type):
"""
Removes a view from the layout by the view type (component) specified by the 'view_type' parameter.

:param view_by: A component view_type (str).
:type view_by: str

:returns: The layout component of the config corresponding to the specified view_type
:rtype: VitessceConfigView or None if not found

"""
if isinstance(view_type, str):
for i, view in enumerate(self.config["layout"]):
if view.view["component"].lower() == view_type.lower():
return self.config["layout"].pop(i)
raise ValueError(f"No view found with component type: {view_type}")
else:
raise TypeError("view_by must a string representing component type")

def get_routes(self):
"""
Convert the routes for this view config from the datasets.
Expand Down
Loading