Skip to content

Commit

Permalink
implemented defaultdict solution for mapAnnDict
Browse files Browse the repository at this point in the history
  • Loading branch information
JensWendt committed Aug 22, 2024
1 parent 970ed3e commit 5f5ac55
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
14 changes: 5 additions & 9 deletions ezomero/_gets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import numpy as np
from collections import defaultdict
from typing import Optional, List, Union, Tuple, Literal
from typing import Any
from ._ezomero import do_across_groups
Expand Down Expand Up @@ -1152,22 +1153,17 @@ def get_map_annotation(conn: BlitzGateway, map_ann_id: int,
--------
>>> ma_dict = get_map_annotation(conn, 62)
>>> print(ma_dict)
{'testkey': 'testvalue', 'testkey2': ['testvalue2'. 'testvalue3']}
{'testkey': ['testvalue'], 'testkey2': ['testvalue2'. 'testvalue3']}
"""
if type(map_ann_id) is not int:
raise TypeError('Map annotation ID must be an integer')

map_annotation_dict = {}

map_annotation = conn.getObject('MapAnnotation', map_ann_id).getValue()

map_annotation_dict = defaultdict(list)

for item in map_annotation:
if item[0] in map_annotation_dict:
if not isinstance(map_annotation_dict[item[0]], list):
map_annotation_dict[item[0]] = [map_annotation_dict[item[0]]]
map_annotation_dict[item[0]].append(item[1])
else:
map_annotation_dict[item[0]] = item[1]
map_annotation_dict[item[0]].append(item[1])

return map_annotation_dict

Expand Down
4 changes: 2 additions & 2 deletions tests/test_gets.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ def test_get_map_annotation_and_ids(conn, project_structure):
with pytest.raises(TypeError):
_ = ezomero.get_map_annotation(conn, '10')
mpann = ezomero.get_map_annotation(conn, map_ann_ids[0])
assert mpann["key1"] == kv["key1"]
assert mpann["key2"] == kv["key2"]
assert mpann["key1"][0] == kv["key1"]
assert mpann["key2"][0] == kv["key2"]
assert sorted(mpann["key3"]) == sorted(kv["key3"])
conn.deleteObjects("Annotation",
[map_ann_id, map_ann_id2, map_ann_id3, map_ann_id4],
Expand Down
4 changes: 2 additions & 2 deletions tests/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def test_post_get_map_annotation(conn, project_structure, users_groups):

map_ann_id = ezomero.post_map_annotation(conn, "Image", im_id, kv, ns)
kv_pairs = ezomero.get_map_annotation(conn, map_ann_id)
assert kv_pairs["key2"] == "value2"
assert kv_pairs["key2"][0] == "value2"
assert sorted(kv_pairs["key3"]) == sorted(["value3", "123"])

# Test posting to non-existing object
Expand All @@ -213,7 +213,7 @@ def test_post_get_map_annotation(conn, project_structure, users_groups):
map_ann_id3 = ezomero.post_map_annotation(current_conn, "Image", im_id3,
kv, ns)
kv_pairs3 = ezomero.get_map_annotation(current_conn, map_ann_id3)
assert kv_pairs3["key2"] == "value2"
assert kv_pairs3["key2"][0] == "value2"
current_conn.close()

# Test posting to an invalid cross-group
Expand Down
6 changes: 3 additions & 3 deletions tests/test_puts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_put_map_annotation(conn, project_structure, users_groups):
"key2": "value2"}
ezomero.put_map_annotation(conn, map_ann_id, kv)
kv_pairs = ezomero.get_map_annotation(conn, map_ann_id)
assert sorted(kv_pairs['key1']) == sorted(kv['key1'])
assert kv_pairs['key1'][0] == kv['key1']

# test cross-group
kv = {"key1": "value1",
Expand All @@ -39,7 +39,7 @@ def test_put_map_annotation(conn, project_structure, users_groups):
"key2": "value2"}
ezomero.put_map_annotation(current_conn, map_ann_id2, kv)
kv_pairs = ezomero.get_map_annotation(current_conn, map_ann_id2)
assert kv_pairs['key1'] == kv['key1']
assert kv_pairs['key1'][0] == kv['key1']
current_conn.close()

# test cross-group, across_groups unset
Expand All @@ -58,7 +58,7 @@ def test_put_map_annotation(conn, project_structure, users_groups):
ezomero.put_map_annotation(current_conn, map_ann_id3, kv_changed,
across_groups=False)
kv_pairs = ezomero.get_map_annotation(current_conn, map_ann_id3)
assert kv_pairs['key1'] == kv['key1']
assert kv_pairs['key1'][0] == kv['key1']
current_conn.close()

# test non-existent ID
Expand Down

0 comments on commit 5f5ac55

Please sign in to comment.