Skip to content

Commit f330e56

Browse files
committed
clean up a little
1 parent a9a9e01 commit f330e56

2 files changed

Lines changed: 33 additions & 13 deletions

File tree

src/valor_lite/cache.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99
import pyarrow as pa
1010
import pyarrow.dataset as ds
11+
import pyarrow.lib as pl
1112
import pyarrow.parquet as pq
1213

1314

@@ -40,6 +41,27 @@ def to_arrow(self):
4041
return pa.timestamp("us")
4142

4243

44+
def convert_type_mapping_to_schema(
45+
type_mapping: dict[str, DataType] | None
46+
) -> list[tuple[str, pl.DataType]]:
47+
"""
48+
Convert type mapping to a pyarrow schema input.
49+
50+
Parameters
51+
----------
52+
type_mapping : dict[str, DataType] | None
53+
A map from string key to datatype. Treats input of `None` as empty mapping.
54+
55+
Returns
56+
-------
57+
list[tuple[str, pyarrow.lib.DataType]]
58+
A list of field name, field type pairs that can be used as input to pyarrow.schema.
59+
"""
60+
if not type_mapping:
61+
return []
62+
return [(k, v.to_arrow()) for k, v in type_mapping.items()]
63+
64+
4365
class CacheReader:
4466
def __init__(self, where: str | Path):
4567
self._dir = Path(where)

src/valor_lite/object_detection/loader.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
from numpy.typing import NDArray
77
from tqdm import tqdm
88

9-
from valor_lite.cache import CacheWriter, DataType
9+
from valor_lite.cache import (
10+
CacheWriter,
11+
DataType,
12+
convert_type_mapping_to_schema,
13+
)
1014
from valor_lite.exceptions import EmptyCacheError
1115
from valor_lite.object_detection.annotation import (
1216
Bitmask,
@@ -56,20 +60,14 @@ def __init__(
5660
}
5761
json.dump(types, f, indent=2)
5862

59-
datum_metadata_schema = (
60-
[(k, v.to_arrow()) for k, v in datum_metadata_types.items()]
61-
if datum_metadata_types
62-
else []
63+
datum_metadata_schema = convert_type_mapping_to_schema(
64+
datum_metadata_types
6365
)
64-
groundtruth_metadata_schema = (
65-
[(k, v.to_arrow()) for k, v in groundtruth_metadata_types.items()]
66-
if groundtruth_metadata_types
67-
else []
66+
groundtruth_metadata_schema = convert_type_mapping_to_schema(
67+
groundtruth_metadata_types
6868
)
69-
prediction_metadata_schema = (
70-
[(k, v.to_arrow()) for k, v in prediction_metadata_types.items()]
71-
if prediction_metadata_types
72-
else []
69+
prediction_metadata_schema = convert_type_mapping_to_schema(
70+
prediction_metadata_types
7371
)
7472

7573
self._null_gt_metadata = {

0 commit comments

Comments
 (0)