Skip to content

Commit

Permalink
Merge pull request #79 from abailoni/add_delete_image
Browse files Browse the repository at this point in the history
Add possibility to remove image from QuPath project
  • Loading branch information
ap-- committed Nov 20, 2022
2 parents de5f916 + f976e4d commit c457b3a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
29 changes: 28 additions & 1 deletion paquo/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Tuple
from typing import Union
from typing import overload
from urllib.parse import urlsplit

if sys.version_info >= (3, 8):
from typing import Literal
Expand Down Expand Up @@ -89,7 +90,6 @@ def refresh(self):
if removed:
for key in removed: # pragma: no cover
_ = self._images.pop(key)
raise NotImplementedError("removal not yet implemented")

def __iter__(self) -> Iterator[QuPathProjectImageEntry]:
return iter(self._images.values())
Expand Down Expand Up @@ -431,6 +431,33 @@ def update_image_paths(self, *, try_relative: bool = False, **rebase_kwargs) ->
for image in self.images:
image.java_object.updateServerURIs(uri2uri)

@redirect(stderr=True, stdout=True)
def remove_image(
self,
image_entry: Union[QuPathProjectImageEntry, int],
) -> None:
"""
Delete an image from the QuPath project.
Parameters
----------
image_entry:
the image entry to be removed
"""
if isinstance(image_entry, int):
image_entry = self.images[image_entry]
if not isinstance(image_entry, QuPathProjectImageEntry):
raise TypeError(
f"expected QuPathProjectImageEntry, got: {type(image_entry).__name__!r}"
)
if self._readonly:
raise OSError("project in readonly mode")
try:
self.java_object.removeImage(image_entry.java_object, False)
finally:
self._image_entries_proxy.refresh()
self.save(images=False)

@property
def uri(self) -> str:
"""the uri identifying the project location"""
Expand Down
13 changes: 13 additions & 0 deletions paquo/tests/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,19 @@ def test_project_add_unsupported_image(new_project, tmp_path):
new_project.add_image(image)


def test_project_remove_image(new_project, svs_small):
# Add an image to the project and then remove it:
new_project.add_image(svs_small)
assert len(new_project.images) == 1
new_project.remove_image(new_project.images[0])
assert len(new_project.images) == 0


def test_project_remove_image_wrong_type(new_project):
with pytest.raises(TypeError):
new_project.remove_image("some/file.svs")


def test_project_image_slicing(new_project):
_ = new_project.images[slice(None, None, None)]

Expand Down
3 changes: 3 additions & 0 deletions paquo/tests/test_readonly.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def test_project_attrs_and_methods(readonly_project, copy_svs_small):
a.callmethod("add_image", copy_svs_small, allow_duplicates=True)
with pytest.raises(IOError):
a.callmethod("save")
with pytest.raises(IOError):
image_entry = qp.images[0]
a.callmethod("remove_image", image_entry)

# test that we can reassign uri's even in readonly mode
cur_uri = qp.images[0].uri
Expand Down

0 comments on commit c457b3a

Please sign in to comment.