Skip to content

Commit e75e5d8

Browse files
author
Matt Sokoloff
committed
wip
1 parent d5a8379 commit e75e5d8

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

labelbox/data/annotation_types/data/raster.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def value(self) -> np.ndarray:
8181
with open(self.file_path, "rb") as img:
8282
im_bytes = img.read()
8383
self.im_bytes = im_bytes
84-
return self.bytes_to_np(im_bytes)
84+
arr = self.bytes_to_np(im_bytes)
85+
return arr
8586
elif self.url is not None:
8687
im_bytes = self.fetch_remote()
8788
self.im_bytes = im_bytes
@@ -92,7 +93,7 @@ def value(self) -> np.ndarray:
9293
def set_fetch_fn(self, fn):
9394
object.__setattr__(self, 'fetch_remote', lambda: fn(self))
9495

95-
@retry.Retry(deadline=15.)
96+
@retry.Retry(deadline=60.)
9697
def fetch_remote(self) -> bytes:
9798
"""
9899
Method for accessing url.
@@ -104,7 +105,7 @@ def fetch_remote(self) -> bytes:
104105
response.raise_for_status()
105106
return response.content
106107

107-
@retry.Retry(deadline=15.)
108+
@retry.Retry(deadline=30.)
108109
def create_url(self, signer: Callable[[bytes], str]) -> str:
109110
"""
110111
Utility for creating a url from any of the other image representations.

labelbox/data/annotation_types/metrics/scalar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ class CustomScalarMetric(BaseModel):
1616
subclass_name: Optional[str] = None
1717
aggregation: MetricAggregation = MetricAggregation.ARITHMETIC_MEAN
1818
extra: Dict[str, Any] = {}
19+
20+
21+
# TODO: Create a metric type that is used once....

labelbox/data/serialization/ndjson/metric.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
from labelbox.data.annotation_types.data import TextData, ImageData
1+
from labelbox.data.annotation_types.metrics.scalar import CustomScalarMetric
2+
from typing import Literal, Union
3+
4+
from fiftyone.core.collections import aggregation
5+
6+
from labelbox.data.annotation_types.data import ImageData, TextData
27
from labelbox.data.annotation_types.metrics import ScalarMetric
38
from labelbox.data.serialization.ndjson.base import NDJsonBase
4-
from typing import Union
59

610

711
class NDDataRowScalarMetric(NDJsonBase):
@@ -19,16 +23,47 @@ def from_common(
1923
data_row={'id': data.uid})
2024

2125

26+
class NDCustomScalarMetric(NDJsonBase):
27+
metric_name: float
28+
metric_value: float
29+
sublcass_name: float
30+
aggregation: Union[Literal["ARITHMETIC_MEAN"], Literal["GEOMETRIC_MEAN"],
31+
Literal["HARMONIC_MEAN"], Literal["SUM"]]
32+
33+
def to_common(self) -> CustomScalarMetric:
34+
return ScalarMetric(value=self.metric_value, extra={'uuid': self.uuid})
35+
36+
@classmethod
37+
def from_common(cls, metric: ScalarMetric,
38+
data: Union[TextData, ImageData]) -> "NDCustomScalarMetric":
39+
return NDCustomScalarMetric(uuid=metric.extra.get('uuid'),
40+
metric_value=metric.value,
41+
data_row={'id': data.uid})
42+
43+
2244
class NDMetricAnnotation:
2345

2446
@classmethod
25-
def to_common(cls, annotation: NDDataRowScalarMetric) -> ScalarMetric:
47+
def to_common(cls, annotation: "NDMetricType") -> ScalarMetric:
48+
2649
return annotation.to_common()
2750

2851
@classmethod
29-
def from_common(cls, annotation: ScalarMetric,
30-
data: Union[TextData, ImageData]) -> NDDataRowScalarMetric:
52+
def from_common(cls, annotation: Union[ScalarMetric, CustomScalarMetric],
53+
data: Union[TextData, ImageData]) -> "NDMetricType":
3154
return NDDataRowScalarMetric.from_common(annotation, data)
3255

56+
@staticmethod
57+
def lookup_object(
58+
metric: Union[ScalarMetric, CustomScalarMetric]) -> "NDMetricType":
59+
result = {
60+
ScalarMetric: NDDataRowScalarMetric,
61+
CustomScalarMetric: NDCustomScalarMetric,
62+
}.get(type(metric))
63+
if result is None:
64+
raise TypeError(
65+
f"Unable to convert object to MAL format. `{type(metric)}`")
66+
return result
67+
3368

34-
NDMetricType = NDDataRowScalarMetric
69+
NDMetricType = Union[NDDataRowScalarMetric, NDCustomScalarMetric]

tests/data/annotation_types/test_metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import pytest
2+
13
from labelbox.data.annotation_types.metrics.aggregations import MetricAggregation
24
from labelbox.data.annotation_types.metrics.scalar import CustomScalarMetric
35
from labelbox.data.annotation_types.collection import LabelList
46
from labelbox.data.annotation_types import ScalarMetric, Label, ImageData
57

6-
import pytest
7-
88

99
def test_scalar_metric():
1010
value = 10

tests/integration/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ def client(environ: str):
134134

135135

136136
@pytest.fixture(scope="session")
137-
def image_url(client):
137+
def image_url(client, environ: str):
138+
if environ == Environ.LOCAL:
139+
return IMG_URL
138140
return client.upload_data(requests.get(IMG_URL).content, sign=True)
139141

140142

0 commit comments

Comments
 (0)