From 92dbdb0b14874184a3d4b065414b3caa848a9ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Wed, 14 Oct 2026 10:35:39 +0200 Subject: [PATCH 01/17] Remove dead code from parse_url Removed 'image_ids = ...' is immediately overwriten by the subsequent line. Removed lines ('image-' URL handling) are handled in the 'elif' before. --- src/imcflibs/imagej/omerotools.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index 856c5a57..cc0ce524 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -110,11 +110,6 @@ def parse_url(client, omero_str): image_ids.pop(0) image_ids = [s.split("%")[0].replace("|", "") for s in image_ids] else: - image_ids = ( - [s.split("%")[0].replace("|", "") for s in omero_str.split("image-")[1:]] - if "image-" in omero_str - else omero_str.split(",") - ) # If it is a list of IDs separated by commas image_ids = omero_str.split(",") From 3a678d7beefd1edd1a9f212756a124c3ab669270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Wed, 14 Oct 2026 10:58:34 +0200 Subject: [PATCH 02/17] Update parse_url description Clarified that it works for Dataset IDs or Image IDs URLs. Removed that it handles Dataset IDs directly (which it does not to my knowledge. --- src/imcflibs/imagej/omerotools.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index cc0ce524..41ea99c0 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -32,9 +32,9 @@ def parse_url(client, omero_str): Assemble a list of ImageWrapper objects from one of the following inputs: - - An OMERO URL (as retrieved e.g. from OMERO.web). + - An OMERO URL for Dataset(s) or Image(s) + (as retrieved e.g. from OMERO.web). - One or more OMERO image IDs. - - An OMERO Dataset ID. Parameters ---------- From 76203995c0d5b1d15cf155837ec2f862c8c17628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Wed, 14 Oct 2026 11:02:40 +0200 Subject: [PATCH 03/17] Fix typos Fixed 'anotations' -> 'annotations' twice in function descriptions --- src/imcflibs/imagej/omerotools.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index 41ea99c0..915a8df8 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -196,7 +196,7 @@ def add_keyvalue_annotation(client, repository_wpr, annotations, header): client : fr.igred.omero.Client The client object used to connect to the OMERO server. repository_wpr : fr.igred.omero.repositor.GenericRepositoryObjectWrapper - Wrapper to the object for the anotation. + Wrapper to the object for the annotation. annotations : dict Dictionary with the annotation to add. header : str @@ -217,7 +217,7 @@ def delete_keyvalue_annotations(user_client, object_wrapper): user_client : fr.igred.omero.Client Client used for login to OMERO. object_wrapper : fr.igred.omero.repositor.GenericRepositoryObjectWrapper - Wrapper to the object for the anotation. + Wrapper to the object for the annotation. """ kv_pairs = object_wrapper.getMapAnnotations(user_client) From 6e719a0f27af60c233dabb51ff7e68b7ecd72272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Wed, 14 Oct 2026 14:26:09 +0200 Subject: [PATCH 04/17] Add FileNotFoundError in upload_image_to_omero Import image did not raise file error if it did not exist. --- src/imcflibs/imagej/omerotools.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index 915a8df8..803e78f7 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -14,6 +14,8 @@ [omero-insight]: https://github.com/ome/omero-insight """ +import os + from fr.igred.omero import Client from fr.igred.omero.annotations import ( MapAnnotationWrapper, @@ -185,6 +187,8 @@ def upload_image_to_omero(user_client, path, dataset_id): Long ID of the uploaded image """ + if not os.path.exists(path): + raise FileNotFoundError(f"Could not find file: {path}") return user_client.getDataset(Long(dataset_id)).importImage(user_client, path)[0] From 20855cb4641a06486fe0bf12abe1681510da9713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Wed, 14 Oct 2026 14:58:56 +0200 Subject: [PATCH 05/17] Add test_omerotools.py with test_upload_image_to_omero --- tests/test_omerotools.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tests/test_omerotools.py diff --git a/tests/test_omerotools.py b/tests/test_omerotools.py new file mode 100644 index 00000000..69f36f08 --- /dev/null +++ b/tests/test_omerotools.py @@ -0,0 +1,16 @@ +"""Tests for the imcflibs.imagej.omerotools module.""" + +import pytest + +from imcflibs.imagej.omerotools import ( + upload_image_to_omero, +) + +def test_upload_image_to_omero(): + """Test `upload_image_to_omero()`. + + Tests only the case for non-existing files. + """ + path = "/usr/random/path/questionable/file.tiff" + with pytest.raises(FileNotFoundError, match="Could not find file*"): + upload_image_to_omero(None, path, None) From 1493079d1a679dbf90995adefdbdc3823f845d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Wed, 14 Oct 2026 16:47:43 +0200 Subject: [PATCH 06/17] Add extract_ids_from_string function This function is part the refactoring of parse_url. It takes an URL and OMERO type to return a list of string IDs. Along with a test. --- src/imcflibs/imagej/omerotools.py | 35 +++++++++++++++++++++++++++++++ tests/test_omerotools.py | 27 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index 803e78f7..c5b66e4c 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -29,6 +29,41 @@ from omero.gateway.model import TableData, TableDataColumn +def extract_ids_from_string(omero_str, omero_type): + """Extract IDs from an OMERO URL for a given OMERO type. + + Parameters + ---------- + omero_str : str + OMERO URL, e.g. from the webclient. + omero_type : str + OMERO type, i.e. "project", "Dataset", or "Image" + + Returns + ------- + list[str] + List of IDs extracted from the OMERO URL. + """ + omero_type = omero_type.lower().strip("-") + if omero_type not in ["project", "dataset", "image"]: + raise ValueError(f"Unknown OMERO type: {omero_type}") + + omero_ids = [] + omero_type = omero_type + "-" + if omero_type in omero_str: + # If there are multiple types + if "|" in omero_str: + parts = omero_str.split("|") + for part in parts: + if omero_type in part: + omero_id = part.split(omero_type)[1].split("/")[0] + omero_ids.append(omero_id) + else: + # If there is only one dataset + omero_id = omero_str.split(omero_type)[1].split("/")[0] + omero_ids.append(omero_id) + return omero_ids + def parse_url(client, omero_str): """Parse an OMERO URL / image IDs into the respective ImageWrapper objects. diff --git a/tests/test_omerotools.py b/tests/test_omerotools.py index 69f36f08..3ff1c531 100644 --- a/tests/test_omerotools.py +++ b/tests/test_omerotools.py @@ -3,6 +3,7 @@ import pytest from imcflibs.imagej.omerotools import ( + extract_ids_from_string, upload_image_to_omero, ) @@ -14,3 +15,29 @@ def test_upload_image_to_omero(): path = "/usr/random/path/questionable/file.tiff" with pytest.raises(FileNotFoundError, match="Could not find file*"): upload_image_to_omero(None, path, None) + + +def test_extract_ids_from_string(): + """Test `extract_ids_from_string()`.""" + + url_proj = "webclient/?show=project-25406" + # Expect error with wrong omero_type + with pytest.raises(ValueError, match="Unknown OMERO type*"): + extract_ids_from_string(url_proj, "unicorn") + + assert len(extract_ids_from_string(url_proj, "Project")) == 1 + assert len(extract_ids_from_string(url_proj, "Project-")) == 1 + assert len(extract_ids_from_string(url_proj, "Dataset")) == 0 + + url_dataset = "webclient/?show=dataset-82753" + assert len(extract_ids_from_string(url_dataset, "dataset")) == 1 + + url_image = "webclient/?show=image-3598144" + assert len(extract_ids_from_string(url_image, "image")) == 1 + + url_multi_image = "webclient/?show=image-3598144|image-3598145|image-3598146|image-3598147" + assert len(extract_ids_from_string(url_multi_image, "image-")) == 4 + assert len(extract_ids_from_string(url_multi_image, "Project")) == 0 + + url_bad = "https://github.com/imcf/python-imcflibs" + assert len(extract_ids_from_string(url_bad, "image")) == 0 From 2a452c7c3017106c5805c896e08120fdcf01e2d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Wed, 14 Oct 2026 16:50:55 +0200 Subject: [PATCH 07/17] Add parse_url_string function This function is part the refactoring of parse_url. It takes an URL and return a list of string IDs and the OMERO type (Project, Dataset or ID). Along with a test. --- src/imcflibs/imagej/omerotools.py | 28 ++++++++++++++++++++++++ tests/test_omerotools.py | 36 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index c5b66e4c..af31c055 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -29,6 +29,33 @@ from omero.gateway.model import TableData, TableDataColumn +def parse_url_string(url_str): + """Extract IDs from an OMERO URL. + + Info: URLs can only be of the same type (Dataset, Project, Images). + Returns tuple[empty list, ""] if input str (URL) is not an OMERO URL. + + Parameters + ---------- + url_str : str + OMERO URL, e.g. from the webclient. + + Returns + ------- + tuple[list[str], str] + - List of IDs extracted from the OMERO URL. + - Type of IDs (i.e. "project", "dataset", or "image") + """ + omero_types = ["dataset-", "project-", "image-"] + + for omero_type in omero_types: + omero_ids = extract_ids_from_string(url_str, omero_type) + if len(omero_ids) > 0: + return omero_ids, omero_type.strip("-") + # If no IDs identified + return [], "" + + def extract_ids_from_string(omero_str, omero_type): """Extract IDs from an OMERO URL for a given OMERO type. @@ -64,6 +91,7 @@ def extract_ids_from_string(omero_str, omero_type): omero_ids.append(omero_id) return omero_ids + def parse_url(client, omero_str): """Parse an OMERO URL / image IDs into the respective ImageWrapper objects. diff --git a/tests/test_omerotools.py b/tests/test_omerotools.py index 3ff1c531..16bf5501 100644 --- a/tests/test_omerotools.py +++ b/tests/test_omerotools.py @@ -4,6 +4,7 @@ from imcflibs.imagej.omerotools import ( extract_ids_from_string, + parse_url_string, upload_image_to_omero, ) @@ -41,3 +42,38 @@ def test_extract_ids_from_string(): url_bad = "https://github.com/imcf/python-imcflibs" assert len(extract_ids_from_string(url_bad, "image")) == 0 + + +def test_parse_url_string(): + """Test `parse_url_string()`.""" + url_proj = "webclient/?show=project-25406" + omero_ids, omero_type = parse_url_string(url_proj) + assert len(omero_ids) == 1 + assert omero_type == "project" + assert "25406" in omero_ids + + url_dataset = "webclient/?show=dataset-82753" + omero_ids, omero_type = parse_url_string(url_dataset) + assert len(omero_ids) == 1 + assert omero_type == "dataset" + assert "82753" in omero_ids + + url_image = "webclient/?show=image-3598144" + omero_ids, omero_type = parse_url_string(url_image) + assert len(omero_ids) == 1 + assert omero_type == "image" + assert "3598144" in omero_ids + + url_multi_image = "webclient/?show=image-3598144|image-3598145|image-3598146|image-3598147" + omero_ids, omero_type = parse_url_string(url_multi_image) + assert len(omero_ids) == 4 + assert omero_type == "image" + assert "3598144" in omero_ids + assert "3598145" in omero_ids + assert "3598146" in omero_ids + assert "3598147" in omero_ids + + url_bad = "https://github.com/imcf/python-imcflibs" + omero_ids, omero_type = parse_url_string(url_bad) + assert len(omero_ids) == 0 + assert omero_type == "" From da475eef0a3db4666f08f46698944efed00e0127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Wed, 14 Oct 2026 17:14:26 +0200 Subject: [PATCH 08/17] Refactor parse_url The string handling of parse_url happens now in other functions. The logic stays the same. In addition, it raises an NotImplementedError for Project-URLs. --- src/imcflibs/imagej/omerotools.py | 62 +++++++++---------------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index af31c055..6a94d920 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -128,57 +128,31 @@ def parse_url(client, omero_str): # Sanitize the string omero_str = omero_str.strip() - image_ids = [] - dataset_ids = [] - image_wpr_list = [] + # Get a list of str IDs and their OMERO type + omero_ids, omero_type = parse_url_string(omero_str) - # Check if the url is a dataset - if "dataset-" in omero_str: - # If there are multiple datasets - if "|" in omero_str: - parts = omero_str.split("|") - for part in parts: - if "dataset-" in part: - image_ids.extend( - [ - image - for image in client.getDataset( - Long(part.split("dataset-")[1].split("/")[0]) - ).getImages() - ] - ) - dataset_id = Long(part.split("dataset-")[1].split("/")[0]) - dataset_ids.append(dataset_id) - else: - image_ids.extend( - [ - image - for image in client.getDataset( - Long(omero_str.split("dataset-")[1].split("/")[0]) - ).getImages() - ] - ) - # If there is only one dataset - dataset_id = Long(omero_str.split("dataset-")[1].split("/")[0]) - dataset_ids.append(dataset_id) + if omero_type == "project": + raise NotImplementedError( + "Getting Image IDs from Project(s) is not implemented." + ) - # Get the images from the dataset - for dataset_id in dataset_ids: - dataset_wpr = client.getDataset(dataset_id) + elif omero_type == "dataset": + image_wpr_list = [] + # Get the images from the dataset(s) + for omero_id in omero_ids: + dataset_wpr = client.getDataset(omero_id) image_wpr_list.extend(dataset_wpr.getImages()) - return image_wpr_list - # Check if the url is an image - elif "image-" in omero_str: - image_ids = omero_str.split("image-") - image_ids.pop(0) - image_ids = [s.split("%")[0].replace("|", "") for s in image_ids] + elif omero_type == "image": + # omero_ids will be processed at the end + pass + else: - # If it is a list of IDs separated by commas - image_ids = omero_str.split(",") + # (omero_type == "") supposedly a list of IDs separated by commas + omero_ids = omero_str.split(",") - return [client.getImage(Long(image_id)) for image_id in image_ids] + return [client.getImage(Long(image_id)) for image_id in omero_ids] def connect(host, port, username, password): From a5f89eaa840c955bf68b3f1797ecfb45d3fff528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Wed, 14 Oct 2026 17:28:55 +0200 Subject: [PATCH 09/17] Add test for parse_url --- tests/test_omerotools.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_omerotools.py b/tests/test_omerotools.py index 16bf5501..3109fbb0 100644 --- a/tests/test_omerotools.py +++ b/tests/test_omerotools.py @@ -4,6 +4,7 @@ from imcflibs.imagej.omerotools import ( extract_ids_from_string, + parse_url, parse_url_string, upload_image_to_omero, ) @@ -77,3 +78,22 @@ def test_parse_url_string(): omero_ids, omero_type = parse_url_string(url_bad) assert len(omero_ids) == 0 assert omero_type == "" + + +def test_parse_url(): + """Test `parse_url()`. + + Only tests the first part of the function + (NotImplementedError on Project-URLs). + Since the reset of the function depends on OMERO libraries. + """ + url_empty = "" + with pytest.raises(ValueError, match="No OMERO link or image ID prov*"): + parse_url(None, url_empty) + + url_proj = "webclient/?show=project-25406" + with pytest.raises( + NotImplementedError, + match="Getting Image IDs from Project(s) is*", + ): + parse_url(None, url_proj) From c19082a2e37bc28a7a118767232874a7f7616e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Wed, 14 Oct 2026 17:35:21 +0200 Subject: [PATCH 10/17] Use raw string for `pytest.raises` match --- tests/test_omerotools.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_omerotools.py b/tests/test_omerotools.py index 3109fbb0..ee165be3 100644 --- a/tests/test_omerotools.py +++ b/tests/test_omerotools.py @@ -15,7 +15,7 @@ def test_upload_image_to_omero(): Tests only the case for non-existing files. """ path = "/usr/random/path/questionable/file.tiff" - with pytest.raises(FileNotFoundError, match="Could not find file*"): + with pytest.raises(FileNotFoundError, match=r"Could not find file*"): upload_image_to_omero(None, path, None) @@ -24,7 +24,7 @@ def test_extract_ids_from_string(): url_proj = "webclient/?show=project-25406" # Expect error with wrong omero_type - with pytest.raises(ValueError, match="Unknown OMERO type*"): + with pytest.raises(ValueError, match=r"Unknown OMERO type*"): extract_ids_from_string(url_proj, "unicorn") assert len(extract_ids_from_string(url_proj, "Project")) == 1 @@ -88,12 +88,12 @@ def test_parse_url(): Since the reset of the function depends on OMERO libraries. """ url_empty = "" - with pytest.raises(ValueError, match="No OMERO link or image ID prov*"): + with pytest.raises(ValueError, match=r"No OMERO link or image ID prov*"): parse_url(None, url_empty) url_proj = "webclient/?show=project-25406" with pytest.raises( NotImplementedError, - match="Getting Image IDs from Project(s) is*", + match=r"Getting Image IDs from Project*", ): parse_url(None, url_proj) From 08cfd8a5f43f10bc7f576bd10700b2ff65906a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Thu, 15 Oct 2026 12:08:18 +0200 Subject: [PATCH 11/17] Lint / Fix formatting --- src/imcflibs/imagej/omerotools.py | 2 +- tests/test_omerotools.py | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index 6a94d920..28fbcbe1 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -64,7 +64,7 @@ def extract_ids_from_string(omero_str, omero_type): omero_str : str OMERO URL, e.g. from the webclient. omero_type : str - OMERO type, i.e. "project", "Dataset", or "Image" + OMERO type, i.e. "project", "Dataset", or "Image" # FIXME Returns ------- diff --git a/tests/test_omerotools.py b/tests/test_omerotools.py index ee165be3..909d19ba 100644 --- a/tests/test_omerotools.py +++ b/tests/test_omerotools.py @@ -9,6 +9,7 @@ upload_image_to_omero, ) + def test_upload_image_to_omero(): """Test `upload_image_to_omero()`. @@ -37,7 +38,9 @@ def test_extract_ids_from_string(): url_image = "webclient/?show=image-3598144" assert len(extract_ids_from_string(url_image, "image")) == 1 - url_multi_image = "webclient/?show=image-3598144|image-3598145|image-3598146|image-3598147" + url_multi_image = ( + "webclient/?show=image-3598144|image-3598145|image-3598146|image-3598147" + ) assert len(extract_ids_from_string(url_multi_image, "image-")) == 4 assert len(extract_ids_from_string(url_multi_image, "Project")) == 0 @@ -65,7 +68,9 @@ def test_parse_url_string(): assert omero_type == "image" assert "3598144" in omero_ids - url_multi_image = "webclient/?show=image-3598144|image-3598145|image-3598146|image-3598147" + url_multi_image = ( + "webclient/?show=image-3598144|image-3598145|image-3598146|image-3598147" + ) omero_ids, omero_type = parse_url_string(url_multi_image) assert len(omero_ids) == 4 assert omero_type == "image" @@ -93,7 +98,7 @@ def test_parse_url(): url_proj = "webclient/?show=project-25406" with pytest.raises( - NotImplementedError, - match=r"Getting Image IDs from Project*", - ): + NotImplementedError, + match=r"Getting Image IDs from Project*", + ): parse_url(None, url_proj) From b5c896014f2622f5931180824cc0aaba523581fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Thu, 15 Oct 2026 16:11:43 +0200 Subject: [PATCH 12/17] Fix f-string call / make Python 2.7 compatible --- src/imcflibs/imagej/omerotools.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index 28fbcbe1..11a6b12e 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -73,7 +73,7 @@ def extract_ids_from_string(omero_str, omero_type): """ omero_type = omero_type.lower().strip("-") if omero_type not in ["project", "dataset", "image"]: - raise ValueError(f"Unknown OMERO type: {omero_type}") + raise ValueError("Unknown OMERO type: {}".format(omero_type)) omero_ids = [] omero_type = omero_type + "-" @@ -225,7 +225,7 @@ def upload_image_to_omero(user_client, path, dataset_id): ID of the uploaded image """ if not os.path.exists(path): - raise FileNotFoundError(f"Could not find file: {path}") + raise FileNotFoundError("Could not find file: {}".format(path)) return user_client.getDataset(Long(dataset_id)).importImage(user_client, path)[0] From 923f4cd3b54affa96821e40dcaf48b9cb86c8000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Thu, 15 Oct 2026 16:18:58 +0200 Subject: [PATCH 13/17] Fix regex 'pytest.raises' match parameters --- tests/test_omerotools.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_omerotools.py b/tests/test_omerotools.py index 909d19ba..053cbc50 100644 --- a/tests/test_omerotools.py +++ b/tests/test_omerotools.py @@ -16,7 +16,7 @@ def test_upload_image_to_omero(): Tests only the case for non-existing files. """ path = "/usr/random/path/questionable/file.tiff" - with pytest.raises(FileNotFoundError, match=r"Could not find file*"): + with pytest.raises(FileNotFoundError, match=r"Could not find file"): upload_image_to_omero(None, path, None) @@ -25,7 +25,7 @@ def test_extract_ids_from_string(): url_proj = "webclient/?show=project-25406" # Expect error with wrong omero_type - with pytest.raises(ValueError, match=r"Unknown OMERO type*"): + with pytest.raises(ValueError, match=r"Unknown OMERO type"): extract_ids_from_string(url_proj, "unicorn") assert len(extract_ids_from_string(url_proj, "Project")) == 1 @@ -93,12 +93,12 @@ def test_parse_url(): Since the reset of the function depends on OMERO libraries. """ url_empty = "" - with pytest.raises(ValueError, match=r"No OMERO link or image ID prov*"): + with pytest.raises(ValueError, match=r"No OMERO link or image ID prov"): parse_url(None, url_empty) url_proj = "webclient/?show=project-25406" with pytest.raises( NotImplementedError, - match=r"Getting Image IDs from Project*", + match=r"Getting Image IDs from Project", ): parse_url(None, url_proj) From 1edba0828c1ac0995de5032c0547171cafb247b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Thu, 15 Oct 2026 16:49:58 +0200 Subject: [PATCH 14/17] Fix 'getDataset' from 'Long' not 'str' --- src/imcflibs/imagej/omerotools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index 11a6b12e..0207a4c3 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -140,7 +140,7 @@ def parse_url(client, omero_str): image_wpr_list = [] # Get the images from the dataset(s) for omero_id in omero_ids: - dataset_wpr = client.getDataset(omero_id) + dataset_wpr = client.getDataset(Long(omero_id)) image_wpr_list.extend(dataset_wpr.getImages()) return image_wpr_list From 40e4d07a4a9f4e2f267bb0d968a2edd4c91b74e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Thu, 15 Oct 2026 17:05:14 +0200 Subject: [PATCH 15/17] Add test script for parse_url to interactive-imageJ --- .../omerotools/parse_url/test_parse_url_01.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/interactive-imagej/omerotools/parse_url/test_parse_url_01.py diff --git a/tests/interactive-imagej/omerotools/parse_url/test_parse_url_01.py b/tests/interactive-imagej/omerotools/parse_url/test_parse_url_01.py new file mode 100644 index 00000000..327eeb6e --- /dev/null +++ b/tests/interactive-imagej/omerotools/parse_url/test_parse_url_01.py @@ -0,0 +1,58 @@ +# @ String (label="OMERO HOST") host +# @ Integer (label="OMERO PORT", value=4064) port +# @ String (label="OMERO User") user +# @ String (label="OMERO Password", style="password") pwd +# @ String (label="Provide OMERO Project URL") url_project +# @ String (label="Provide OMERO 'Dataset A' URL") url_dataset +# @ String (label="Provide OMERO URL for ALL images in 'Dataset A'") url_images +# Examples +# url_project: https://omero.biozentrum.unibas.ch/webclient/?show=project-25155 +# url_dataset: https://omero.biozentrum.unibas.ch/webclient/?show=dataset-82170 +# url_images: https://omero.biozentrum.unibas.ch/webclient/?show=image-3454870|image-3454871|image-3454872|image-3454873|image-3454874 + + +from imcflibs.imagej import omerotools + +# Parse the url_images to get the IDs +image_ids = [] +parts = url_images.split("|") +for part in parts: + if "image-" in part: + image_id = part.split("image-")[1].split("/")[0] + image_ids.append(image_id) +assert len(image_ids) > 0, "No image ids in image url!" + +# Test +try: + user_client = omerotools.connect(host, port, user, pwd) + + # Project URLs are not implemented + ids = None + try: + ids = omerotools.parse_url(user_client, url_project) + except NotImplementedError: + pass + assert ids is None + + # Single Dataset + ids = omerotools.parse_url(user_client, url_dataset) + assert len(ids) == len(image_ids) + + # Images url + ids = omerotools.parse_url(user_client, url_images) + assert len(ids) == len(image_ids) + for cur_id in ids: + cur_id = cur_id.getId() + assert str(cur_id) in image_ids + + # Image IDs as comma-separated string + ids = omerotools.parse_url(user_client, ",".join(image_ids)) + assert len(ids) == len(image_ids) + for cur_id in ids: + cur_id = cur_id.getId() + assert str(cur_id) in image_ids +finally: + if user_client is not user_client: + user_client.disconnect() + +print("Test completed!") From 4b9f9e70e66c60dd50897e67e81c9256ae4eabe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Thu, 15 Oct 2026 17:25:53 +0200 Subject: [PATCH 16/17] Fix raising FileNotFoundError conditionally for Python 2.7 Raise FileNotFoundError for Python 3.x and IOError for Python 2.7 --- src/imcflibs/imagej/omerotools.py | 6 +++++- tests/test_omerotools.py | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/imcflibs/imagej/omerotools.py b/src/imcflibs/imagej/omerotools.py index 0207a4c3..1bc91cd6 100644 --- a/src/imcflibs/imagej/omerotools.py +++ b/src/imcflibs/imagej/omerotools.py @@ -225,7 +225,11 @@ def upload_image_to_omero(user_client, path, dataset_id): ID of the uploaded image """ if not os.path.exists(path): - raise FileNotFoundError("Could not find file: {}".format(path)) + try: + raise FileNotFoundError("Could not find file: {}".format(path)) + except NameError: + raise IOError("Could not find file: {}".format(path)) + return user_client.getDataset(Long(dataset_id)).importImage(user_client, path)[0] diff --git a/tests/test_omerotools.py b/tests/test_omerotools.py index 053cbc50..1b1dc7b5 100644 --- a/tests/test_omerotools.py +++ b/tests/test_omerotools.py @@ -9,6 +9,12 @@ upload_image_to_omero, ) +# Enusre Python 2.7 and 3.x compability +try: + FileNotFoundError +except NameError: + FileNotFoundError = IOError + def test_upload_image_to_omero(): """Test `upload_image_to_omero()`. From eaa86eaadc33a4584c543089c533efeb1b85fcf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sauteur?= Date: Thu, 15 Oct 2026 17:42:24 +0200 Subject: [PATCH 17/17] Add test script for upload_image_to_omero to interactive-imageJ --- .../test_upload_image_to_omero_01.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/interactive-imagej/omerotools/upload_image_to_omero/test_upload_image_to_omero_01.py diff --git a/tests/interactive-imagej/omerotools/upload_image_to_omero/test_upload_image_to_omero_01.py b/tests/interactive-imagej/omerotools/upload_image_to_omero/test_upload_image_to_omero_01.py new file mode 100644 index 00000000..f5735c11 --- /dev/null +++ b/tests/interactive-imagej/omerotools/upload_image_to_omero/test_upload_image_to_omero_01.py @@ -0,0 +1,19 @@ +from imcflibs.imagej import omerotools + + +error_type = "FileNotFoundError" +try: + FileNotFoundError +except NameError: + FileNotFoundError = IOError + error_type = "IOError" + +path = "/usr/fancy/path/pony-with-one-horn.tiff" + +try: + # No client needed. Only testing the exception + omerotools.upload_image_to_omero(None, path, None) +except FileNotFoundError: + print("Got the expected {}".format(error_type)) + +print("Test completed!")