Skip to content

Commit f2e028a

Browse files
authored
feat: add DB status (#166)
* feat: add DB status Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> * refactor: not to raise exceptions or warnings Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> * docs: update document Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> --------- Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp>
1 parent 298529e commit f2e028a

4 files changed

Lines changed: 74 additions & 35 deletions

File tree

docs/tutorials/cli/t4sanity.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,28 @@ As an example, we have the following the dataset structure:
4545
...
4646
```
4747
48+
Then, you can run sanity checks with `t4sanity <DATA_ROOT>`:
49+
50+
```shell
51+
>>>Sanity checking...: 1it [00:00, 9.70it/s]
52+
✅ No exceptions occurred!!
53+
```
54+
4855
### Exclude Warnings
4956
5057
To run sanity check ignoring warnings, providing the path to the parent directory of the datasets:
5158
5259
```shell
5360
$ t4sanity <DATA_ROOT>
5461

55-
>>> Sanity checking...: 97it [00:03, 26.60it/s]
56-
+--------------------------------------+---------+------------------------------------------------------------------------------------------------+
57-
| DatasetID | Version | Message |
58-
+--------------------------------------+---------+------------------------------------------------------------------------------------------------+
59-
| 96200480-ae59-44cb-9e4e-dd9021e250e8 | 2 | bbox must be (xmin, ymin, xmax, ymax) and xmin <= xmax && ymin <= ymax: (1671, 198, 1440, 229) |
60-
| ca346afb-ea1a-4c5c-8117-544bd9ff6aca | 2 | bbox must be (xmin, ymin, xmax, ymax) and xmin <= xmax && ymin <= ymax: (1793, 99, 1440, 222) |
61-
...
62+
>>>Sanity checking...: 2it [00:00, 18.69it/s]
63+
⚠️ Encountered some exceptions!!
64+
+-----------+---------+--------+------------------------------------------------------------------------------------------------+
65+
| DatasetID | Version | status | Message |
66+
+-----------+---------+--------+------------------------------------------------------------------------------------------------+
67+
| dataset1 | 2 | ERROR | bbox must be (xmin, ymin, xmax, ymax) and xmin <= xmax && ymin <= ymax: (1532, 198, 1440, 265) |
68+
| dataset2 | 1 | OK | |
69+
+-----------+---------+--------+------------------------------------------------------------------------------------------------+
6270
```
6371
6472
### Include Warnings
@@ -68,12 +76,12 @@ To run sanity check and report any warnings, use the `-iw; --include-warning` op
6876
```shell
6977
$ t4sanity <DATA_ROOT> -iw
7078

71-
>>> Sanity checking...: 97it [00:03, 29.31it/s]
72-
+--------------------------------------+---------+------------------------------------------------------------------------------------------------+
73-
| DatasetID | Version | Message |
74-
+--------------------------------------+---------+------------------------------------------------------------------------------------------------+
75-
| 96200480-ae59-44cb-9e4e-dd9021e250e8 | 2 | bbox must be (xmin, ymin, xmax, ymax) and xmin <= xmax && ymin <= ymax: (1671, 198, 1440, 229) |
76-
| ca346afb-ea1a-4c5c-8117-544bd9ff6aca | 2 | bbox must be (xmin, ymin, xmax, ymax) and xmin <= xmax && ymin <= ymax: (1793, 99, 1440, 222) |
77-
| ed96b707-e7f4-4a71-9e6b-571ffd56c4c4 | 2 | level: Not available is not supported, Visibility.UNAVAILABLE will be assigned. |
78-
...
79+
>>>Sanity checking...: 2it [00:00, 21.54it/s]
80+
⚠️ Encountered some exceptions!!
81+
+-----------+---------+---------+------------------------------------------------------------------------------------------------+
82+
| DatasetID | Version | status | Message |
83+
+-----------+---------+---------+------------------------------------------------------------------------------------------------+
84+
| dataset1 | 2 | ERROR | bbox must be (xmin, ymin, xmax, ymax) and xmin <= xmax && ymin <= ymax: (1532, 198, 1440, 265) |
85+
| dataset2 | 1 | WARNING | Category token is empty for surface ann: 0c15d9c143fb2723c16ac7e0c735b0a8 |
86+
+-----------+---------+---------+------------------------------------------------------------------------------------------------+
7987
```

t4_devkit/cli/sanity.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ def _run_sanity_check(
2424
revision: str | None = None,
2525
include_warning: bool = False,
2626
) -> list[DBException]:
27-
exceptions: list[DBException] = []
28-
29-
for db_root in tqdm(Path(db_parent).glob("*"), desc=">>>Sanity checking..."):
30-
result = sanity_check(db_root, revision=revision, include_warning=include_warning)
31-
if result:
32-
exceptions.append(result)
33-
return exceptions
27+
return [
28+
sanity_check(db_root, revision=revision, include_warning=include_warning)
29+
for db_root in tqdm(Path(db_parent).glob("*"), desc=">>>Sanity checking...")
30+
]
3431

3532

3633
@cli.command()
@@ -53,10 +50,10 @@ def main(
5350
) -> None:
5451
exceptions = _run_sanity_check(db_parent, revision=revision, include_warning=include_warning)
5552

56-
if not exceptions:
53+
if all(e.is_ok() for e in exceptions):
5754
print("✅ No exceptions occurred!!")
5855
else:
5956
print("⚠️ Encountered some exceptions!!")
60-
headers = ["DatasetID", "Version", "Message"]
61-
table = [[e.dataset_id, e.version, e.message] for e in exceptions]
57+
headers = ["DatasetID", "Version", "status", "Message"]
58+
table = [[e.dataset_id, e.version, e.status, e.message] for e in exceptions]
6259
print(tabulate(table, headers=headers, tablefmt="pretty"))

t4_devkit/common/sanity.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import warnings
4+
from enum import Enum, unique
45
from pathlib import Path
56

67
from attrs import define
@@ -12,19 +13,38 @@
1213

1314
@define
1415
class DBException:
15-
"""A dataclass to store error message of the corresponding dataset."""
16+
"""A dataclass to store error message of the corresponding dataset.
17+
18+
Attributes:
19+
dataset_id (str): Dataset ID.
20+
version (str | None): Dataset version.
21+
status (DBStatus): Status of the dataset.
22+
message (str): Error or warning message.
23+
"""
1624

1725
dataset_id: str
1826
version: str | None
19-
message: str
27+
status: DBStatus
28+
message: str | None = None
29+
30+
def is_ok(self) -> bool:
31+
"""Return True if the status is OK."""
32+
return self.status == DBStatus.OK
33+
34+
35+
@unique
36+
class DBStatus(str, Enum):
37+
OK = "OK"
38+
WARNING = "WARNING"
39+
ERROR = "ERROR"
2040

2141

2242
def sanity_check(
2343
db_root: str | Path,
2444
*,
2545
revision: str | None = None,
2646
include_warning: bool = False,
27-
) -> DBException | None:
47+
) -> DBException:
2848
"""Perform sanity check and report exception or warning encountered while loading the dataset.
2949
3050
Args:
@@ -44,14 +64,28 @@ def sanity_check(
4464
warnings.filterwarnings("ignore")
4565

4666
try:
47-
_ = Tier4(data_root=db_root, revision=revision, verbose=False)
48-
exception = None
67+
t4 = Tier4(data_root=db_root, revision=revision, verbose=False)
68+
exception = DBException(
69+
dataset_id=t4.dataset_id,
70+
version=t4.version,
71+
status=DBStatus.OK,
72+
)
73+
except Warning as w:
74+
metadata = load_metadata(db_root)
75+
76+
exception = DBException(
77+
dataset_id=metadata.dataset_id,
78+
version=metadata.version,
79+
status=DBStatus.WARNING,
80+
message=str(w),
81+
)
4982
except Exception as e:
5083
metadata = load_metadata(db_root)
5184

5285
exception = DBException(
5386
dataset_id=metadata.dataset_id,
5487
version=metadata.version,
88+
status=DBStatus.ERROR,
5589
message=str(e),
5690
)
5791
return exception

t4_devkit/tier4.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,9 @@ def load_metadata(db_root: str, revision: str | None = None) -> DBMetadata:
8686
version = None
8787
data_root = db_root_path.as_posix()
8888
else:
89-
if int(revision) not in version_candidates:
90-
raise ValueError(f"The version: {revision} is not included in {dataset_id}")
9189
version = revision
9290
data_root = db_root_path.joinpath(version).as_posix()
9391

94-
if version is None:
95-
warnings.warn(f"{dataset_id} does't contain any versions.", DeprecationWarning)
96-
9792
return DBMetadata(data_root=data_root, dataset_id=dataset_id, version=version)
9893

9994

@@ -149,6 +144,11 @@ def __init__(
149144
if not osp.exists(self.data_root):
150145
raise FileNotFoundError(f"Database directory is not found: {self.data_root}")
151146

147+
if self.version is None:
148+
warnings.warn(
149+
f"DatasetID: {self.dataset_id} does't contain any versions.", DeprecationWarning
150+
)
151+
152152
start_time = time.time()
153153
if verbose:
154154
print(f"======\nLoading T4 tables in `{self.schema_dir}`...")

0 commit comments

Comments
 (0)