Skip to content

Commit

Permalink
feat(#1225): create iob tags from record spans (#1226)
Browse files Browse the repository at this point in the history
* feat(#1225): create iob tags from record spans

* test: add tests

* refactor: dynamic tokens map with text/tokens mutability

* chore: naming

* feat: make text and tokens immutable

* chore: adapt to inmutable text and tokens

* test: fix tests

* test: fixing tests

Co-authored-by: dcfidalgo <david@recogn.ai>

(cherry picked from commit 07b895d)
  • Loading branch information
frascuchon committed Mar 28, 2022
1 parent 76272ef commit ecbdd78
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/rubrix/client/datasets.py
Expand Up @@ -877,7 +877,7 @@ def parse_tags_from_example(example):
return dataset.map(parse_tags_from_example)

@classmethod
def _from_pandas(cls, dataframe: pd.DataFrame) -> "DatasetForTextClassification":
def _from_pandas(cls, dataframe: pd.DataFrame) -> "DatasetForTokenClassification":
return cls(
[TokenClassificationRecord(**row) for row in dataframe.to_dict("records")]
)
Expand Down
2 changes: 2 additions & 0 deletions src/rubrix/client/models.py
Expand Up @@ -21,6 +21,7 @@
import logging
import warnings
from collections import defaultdict
from functools import lru_cache
from typing import Any, Dict, List, Optional, Tuple, Union

import pandas as pd
Expand Down Expand Up @@ -381,6 +382,7 @@ def chars2tokens_index(text_, tokens_):
current_token += 1
current_token_char_start += relative_idx
chars_map[idx] = current_token

return chars_map

def tokens2chars_index(
Expand Down
5 changes: 4 additions & 1 deletion tests/client/test_asgi.py
Expand Up @@ -72,10 +72,13 @@ def __call__(self, records, name: str, **kwargs):
],
)

time.sleep(0.2)
assert mock_log.was_called
time.sleep(0.200)

mock_log.was_called = False
mock.get("/another/predict/route")

time.sleep(0.2)
assert not mock_log.was_called


Expand Down
12 changes: 6 additions & 6 deletions tests/client/test_models.py
Expand Up @@ -63,15 +63,15 @@ def test_text_classification_input_string():


@pytest.mark.parametrize(
("annotation", "status", "expected_status"),
("annotation", "status", "expected_status", "expected_iob"),
[
(None, None, "Default"),
([("test", 0, 5)], None, "Validated"),
(None, "Discarded", "Discarded"),
([("test", 0, 5)], "Discarded", "Discarded"),
(None, None, "Default", None),
([("test", 0, 4)], None, "Validated", ["B-test", "O"]),
(None, "Discarded", "Discarded", None),
([("test", 0, 9)], "Discarded", "Discarded", ["B-test", "I-test"]),
],
)
def test_token_classification_record(annotation, status, expected_status):
def test_token_classification_record(annotation, status, expected_status, expected_iob):
"""Just testing its dynamic defaults"""
record = TokenClassificationRecord(
text="test text", tokens=["test", "text"], annotation=annotation, status=status
Expand Down
Expand Up @@ -23,7 +23,7 @@ def test_log_with_empty_tokens_list(mocked_client):
rubrix.delete(dataset)
with pytest.raises(
Exception,
match="ensure this value has at least 1 items",
match="At least one token should be provided",
):
rubrix.log(
TokenClassificationRecord(id=0, text=text, tokens=[]),
Expand Down

0 comments on commit ecbdd78

Please sign in to comment.