Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DIA-1489: Multiskill prompt autorefinement fixes #345

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/label_studio_sdk/label_interface/control_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,23 @@ def label(
)
else:
return self._label_simple(to_name=to_name, *args, **kwargs)

def get_labels(self, regions: List[Dict]):
"""
Returns the simplified representation of the label. Sort of a reverse to label() method to retrieve an input `label` from an output regions
Comment on lines +467 to +468
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you put an example in this docstring? It's hard to picture what the output looks like

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or add a test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure will do both

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"""
values = [region.get('value') for region in regions if region.get('from_name') == self.name]
values = list(filter(lambda x: x is not None, values))
if not hasattr(self, "_label_attr_name"):
return values
labels = []
for value in values:
if len(value) == 1 and self._label_attr_name in value:
v = value[self._label_attr_name]
labels.append(v[0] if len(v) == 1 else v)
else:
labels.append(value)
return labels[0] if len(labels) == 1 else labels

def as_tuple(self):
""" """
Expand Down
53 changes: 52 additions & 1 deletion tests/custom/test_interface/test_control_tags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import pytest
from lxml.etree import Element

from label_studio_sdk.label_interface import LabelInterface
from label_studio_sdk.label_interface.control_tags import ControlTag
from label_studio_sdk.label_interface.control_tags import ControlTag, ChoicesTag, LabelsTag, RectangleLabelsTag

from . import configs as c

Expand Down Expand Up @@ -53,3 +54,53 @@ def test_label_with_choices():

assert "choices" in rpy.get("value")
assert c.LABEL1 in rpy["value"]["choices"]


@pytest.mark.parametrize("tag, regions, expected", [
# Test case 1: ChoicesTag with single choice
(
ChoicesTag(name="choices", to_name=["text"], tag="Choices"),
[{"from_name": "choices", "value": {"choices": ["positive"]}}],
"positive"
),

# Test case 2: ChoicesTag with multiple choices
(
ChoicesTag(name="choices", to_name=["text"], tag="Choices"),
[{"from_name": "choices", "value": {"choices": ["positive", "negative"]}}],
["positive", "negative"]
),

# Test case 3: Multiple regions with labels
(
LabelsTag(name="label", to_name=["text"], tag="Labels"),
[
{"from_name": "label", "to_name": "text", "value": {"labels": ["positive"], "start": 0, "end": 1}},
{"from_name": "label", "to_name": "text", "value": {"labels": ["negative"], "start": 2, "end": 3}}
],
[{"start": 0, "end": 1, "labels": ["positive"]}, {"start": 2, "end": 3, "labels": ["negative"]}]
),

# Test case 4: Empty regions
(
ChoicesTag(name="choices", to_name=["text"], tag="Choices"),
[],
[]
),

# Test case 5: Regions with different from_name
(
ChoicesTag(name="choices", to_name=["text"], tag="Choices"),
[{"from_name": "other_tag", "value": {"choices": ["positive"]}}],
[]
),

# Test case 6: Tag without label_attr_name
(
ControlTag(name="base", to_name=["text"], tag="BaseTag"),
[{"from_name": "base", "value": {"some_value": 42}}],
[{"some_value": 42}]
),
])
def test_control_tag_get_labels(tag, regions, expected):
assert tag.get_labels(regions) == expected