Skip to content

Commit

Permalink
post_file_annotation can now create orphaned FileAnnotations (#92)
Browse files Browse the repository at this point in the history
* updating omero-py version

* post_file_annotation can do orphans
  • Loading branch information
erickmartins committed Jan 8, 2024
1 parent 7e7c7f1 commit 914699b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 34 deletions.
26 changes: 14 additions & 12 deletions ezomero/_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ def post_comment_annotation(conn: BlitzGateway, object_type: str,


@do_across_groups
def post_file_annotation(conn: BlitzGateway, object_type: str, object_id: int,
def post_file_annotation(conn: BlitzGateway,
file_path: str, ns: str,
object_type: Optional[str] = None,
object_id: Optional[int] = None,
mimetype: Optional[str] = None,
description: Optional[str] = None,
across_groups: Optional[bool] = True
Expand All @@ -397,18 +399,18 @@ def post_file_annotation(conn: BlitzGateway, object_type: str, object_id: int,
----------
conn : ``omero.gateway.BlitzGateway`` object
OMERO connection.
object_type : str
OMERO object type, passed to ``BlitzGateway.getObjects``
object_ids : int
ID of object to which the new MapAnnotation will be linked.
file_path : string
local path to file to be added as FileAnnotation
ns : str
Namespace for the FileAnnotation
mimetype : str
object_type : str, optional
OMERO object type, passed to ``BlitzGateway.getObject``
object_id : int, optional
ID of object to which the new FileAnnotation will be linked.
mimetype : str, optional
String of the form 'type/subtype', usable for a MIME content-type
header.
description : str
description : str, optional
File description to be added to FileAnnotation
across_groups : bool, optional
Defines cross-group behavior of function - set to
Expand All @@ -427,15 +429,15 @@ def post_file_annotation(conn: BlitzGateway, object_type: str, object_id: int,
--------
>>> ns = 'jax.org/jax/example/namespace'
>>> path = '/home/user/Downloads/file_ann.txt'
>>> post_file_annotation(conn, "Image", 56, path, ns)
>>> post_file_annotation(conn, path, ns, "Image", 56)
234
"""

if type(file_path) is not str:
raise TypeError('file_path must be of type `str`')

obj = None
if object_id is not None:
if object_id is not None and object_type is not None:
if type(object_id) is not int:
raise TypeError('object_ids must be integer')
obj = conn.getObject(object_type, object_id)
Expand All @@ -450,13 +452,13 @@ def post_file_annotation(conn: BlitzGateway, object_type: str, object_id: int,
'(check if you have permissions to it)')
return None
else:
raise TypeError('Object ID cannot be empty')
set_group(conn, conn.getGroupFromContext().id)
if not mimetype:
mimetype, _ = mimetypes.guess_type(file_path)
file_ann = conn.createFileAnnfromLocalFile(
file_path, mimetype=mimetype, ns=ns, desc=description)
obj.linkAnnotation(file_ann)

if object_id is not None and object_type is not None:
obj.linkAnnotation(file_ann)
return file_ann.getId()


Expand Down
20 changes: 12 additions & 8 deletions tests/test_gets.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,19 @@ def test_get_file_annotation_and_ids(conn, project_structure, tmp_path):
file_path.write_text("hello world!")
file_ann = str(file_path)
ns = "jax.org/omeroutils/tests/v0"
file_ann_id = ezomero.post_file_annotation(conn, "Image", im_id,
file_ann, ns)
file_ann_id2 = ezomero.post_file_annotation(conn, "Image", im_id,
file_ann, ns)
file_ann_id3 = ezomero.post_file_annotation(conn, "Image", im_id,
file_ann, ns)
file_ann_id = ezomero.post_file_annotation(conn,
file_ann, ns,
"Image", im_id)
file_ann_id2 = ezomero.post_file_annotation(conn,
file_ann, ns,
"Image", im_id)
file_ann_id3 = ezomero.post_file_annotation(conn,
file_ann, ns,
"Image", im_id)
ns2 = "different namespace"
file_ann_id4 = ezomero.post_file_annotation(conn, "Image", im_id,
file_ann, ns2)
file_ann_id4 = ezomero.post_file_annotation(conn,
file_ann, ns2,
"Image", im_id)

# Test sanitizing inputs
with pytest.raises(TypeError):
Expand Down
52 changes: 38 additions & 14 deletions tests/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,31 +318,31 @@ def test_post_get_file_annotation(conn, project_structure, users_groups,
ns = "jax.org/omeroutils/tests/v0"
# test sanitized input on post
with pytest.raises(TypeError):
_ = ezomero.post_file_annotation(conn, "Image", im_id, 10, ns)
_ = ezomero.post_file_annotation(conn, 10, ns, "Image", im_id)
with pytest.raises(TypeError):
_ = ezomero.post_file_annotation(conn, "Image", '10', file_ann, ns)
with pytest.raises(TypeError):
_ = ezomero.post_file_annotation(conn, "Image", None, file_ann, ns)
_ = ezomero.post_file_annotation(conn, file_ann, ns, "Image", '10')

file_ann_id = ezomero.post_file_annotation(conn, "Image", im_id, file_ann,
ns)
file_ann_id = ezomero.post_file_annotation(conn, file_ann, ns,
"Image", im_id)
return_ann = ezomero.get_file_annotation(conn, file_ann_id)
assert filecmp.cmp(return_ann, file_ann)
os.remove(return_ann)

# Test posting to non-existing object
im_id2 = 999999999
file_ann_id2 = ezomero.post_file_annotation(conn, "Image", im_id2,
file_ann, ns)
file_ann_id2 = ezomero.post_file_annotation(conn,
file_ann, ns,
"Image", im_id2)
assert file_ann_id2 is None

# Test posting cross-group
username = users_groups[1][0][0] # test_user1
groupname = users_groups[0][0][0] # test_group_1
current_conn = conn.suConn(username, groupname)
im_id3 = image_info[2][1] # im2, in test_group_2
file_ann_id3 = ezomero.post_file_annotation(current_conn, "Image", im_id3,
file_ann, ns)
file_ann_id3 = ezomero.post_file_annotation(current_conn,
file_ann, ns,
"Image", im_id3)
return_ann3 = ezomero.get_file_annotation(current_conn, file_ann_id3)
assert filecmp.cmp(return_ann3, file_ann)
os.remove(return_ann3)
Expand All @@ -353,8 +353,9 @@ def test_post_get_file_annotation(conn, project_structure, users_groups,
groupname = users_groups[0][1][0] # test_group_2
current_conn = conn.suConn(username, groupname)
im_id4 = image_info[1][1] # im1(in test_group_1)
file_ann_id4 = ezomero.post_file_annotation(current_conn, "Image", im_id4,
file_ann, ns)
file_ann_id4 = ezomero.post_file_annotation(current_conn,
file_ann, ns,
"Image", im_id4)
assert file_ann_id4 is None
current_conn.close()

Expand All @@ -363,13 +364,36 @@ def test_post_get_file_annotation(conn, project_structure, users_groups,
groupname = users_groups[0][0][0] # test_group_1
current_conn = conn.suConn(username, groupname)
im_id5 = image_info[2][1] # im2, in test_group_2
file_ann_id5 = ezomero.post_file_annotation(current_conn, "Image", im_id5,
file_ann_id5 = ezomero.post_file_annotation(current_conn,
file_ann, ns,
"Image", im_id5,
across_groups=False)
assert file_ann_id5 is None
current_conn.close()

conn.deleteObjects("Annotation", [file_ann_id, file_ann_id3],
# Test posting orphaned
current_conn = conn.suConn(username, groupname)
file_ann_id6 = ezomero.post_file_annotation(current_conn, file_ann, ns)
print(file_ann_id6)
return_ann6 = ezomero.get_file_annotation(current_conn, file_ann_id6)
assert filecmp.cmp(return_ann6, file_ann)
os.remove(return_ann6)

# Test posting orphaned, partial completion
file_ann_id7 = ezomero.post_file_annotation(conn, file_ann, ns, "Image")
return_ann7 = ezomero.get_file_annotation(conn, file_ann_id7)
assert filecmp.cmp(return_ann7, file_ann)
os.remove(return_ann7)

# Test posting orphaned, partial completion
file_ann_id8 = ezomero.post_file_annotation(conn, file_ann, ns,
object_id=10)
return_ann8 = ezomero.get_file_annotation(conn, file_ann_id8)
assert filecmp.cmp(return_ann8, file_ann)
os.remove(return_ann8)

conn.deleteObjects("Annotation", [file_ann_id, file_ann_id3, file_ann_id6,
file_ann_id7, file_ann_id8],
deleteAnns=True, deleteChildren=True, wait=True)


Expand Down

0 comments on commit 914699b

Please sign in to comment.