Skip to content

Commit

Permalink
BUG: Allow tuples instead of lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Leengit committed Jan 31, 2024
1 parent 923d215 commit 46019e5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
30 changes: 14 additions & 16 deletions al_bench/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,17 @@ def set_all_dictionaries(self, dictionaries: Iterable[Mapping]) -> None:
Set the entire database of dictionaries -- one per feature vector -- from a
supplied list of Python dict objects.
N.B. if we were handed a (read-only) tuple then we cannot change a subset of
them. If this functionality is needed then supply `list(dictionaries)` to this
function.
N.B. if we store a (read-only) tuple of dictionaries then we cannot change a
subset of them, so we convert any such tuple to a list.
"""
if isinstance(dictionaries, list) and all(
if isinstance(dictionaries, (list, tuple)) and all(
isinstance(e, dict) for e in dictionaries
):
self.dictionaries: List[Mapping] = dictionaries
self.dictionaries: List[Mapping] = (
dictionaries.copy()
if isinstance(dictionaries, list)
else list(dictionaries)
)
else:
raise ValueError(
"The argument to set_all_dictionaries must be a list of Python"
Expand Down Expand Up @@ -524,16 +527,7 @@ def set_some_dictionaries(
use dictionary_indices=5, dictionaries={'a': 1, 'b': 2} OR use
dictionary_indices=[5], dictionaries=[{'a': 1, 'b': 2}].
N.B. This will fail if the intially supplied value for dictionaries was a
*tuple* of dictionaries rather than a *list* of dictionaries, because tuples are
read-only.
"""
if isinstance(self.dictionaries, tuple):
raise ValueError(
"set_some_dictionaries cannot be used unless the initially supplied"
" dictionaries are supplied in a list instead of a tuple"
)

for k, v in zip(dictionary_indices, dictionaries):
self.dictionaries[k] = v

Expand Down Expand Up @@ -571,10 +565,14 @@ def set_all_label_definitions(self, label_definitions: Iterable[Mapping]) -> Non
}
"""

if isinstance(label_definitions, list) and all(
if isinstance(label_definitions, (list, tuple)) and all(
isinstance(e, dict) for e in label_definitions
):
self.label_definitions: List[Mapping] = label_definitions
self.label_definitions: List[Mapping] = (
label_definitions.copy()
if isinstance(label_definitions, list)
else list(label_definitions)
)
else:
raise ValueError(
"The argument to set_all_label_definitions must be a list of Python"
Expand Down
2 changes: 1 addition & 1 deletion al_bench/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, certainty_type, percentiles, cutoffs) -> None:

if cutoffs is None:
cutoffs = {}
if len(certainty_type) == 1 and isinstance(cutoffs, list):
if len(certainty_type) == 1 and isinstance(cutoffs, (list, tuple)):
cutoffs = {certainty_type[0]: cutoffs}
# If we have no information for a certainty type, default to no cutoffs.
cutoffs = {**{k: [] for k in certainty_type}, **cutoffs}
Expand Down
4 changes: 2 additions & 2 deletions test/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def create_dataset(
def create_dataset_4598_1280_4(
number_of_superpixels: int,
number_of_features: int,
number_of_categories_by_label: int,
number_of_categories_by_label,
**kwargs,
) -> Tuple[NDArrayFloat, List[Mapping], NDArrayInt]:
import h5py as h5
Expand All @@ -98,7 +98,7 @@ def create_dataset_4598_1280_4(
]
assert number_of_superpixels == my_feature_vectors.shape[0]
assert number_of_features == my_feature_vectors.shape[1]
assert isinstance(number_of_categories_by_label, list)
assert isinstance(number_of_categories_by_label, (list, tuple))
assert len(number_of_categories_by_label) == len(my_label_definitions)
assert number_of_categories_by_label[0] == len(my_label_definitions[0])

Expand Down

0 comments on commit 46019e5

Please sign in to comment.