Skip to content

Commit e4984d7

Browse files
committed
feat: able to load the specific version of the dataset
Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp>
1 parent f23bf60 commit e4984d7

6 files changed

Lines changed: 85 additions & 17 deletions

File tree

docs/tutorials/cli/t4sanity.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ $ t4sanity -h
1111
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
1212
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
1313
│ --version -v Show the application version and exit. │
14+
│ --revision -rv TEXT Specify if you want to load the specific version. [default: None] │
1415
│ --include-warning -iw Indicates whether to report any warnings. │
1516
│ --install-completion Install completion for the current shell. │
1617
│ --show-completion Show completion for the current shell, to copy it or customize the installation. │

docs/tutorials/initialize.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ Done loading in 0.061 seconds.
7171
======
7272
```
7373

74+
Note that if you doesn't specify `revision` parameter in construction, it searches the latest version of the dataset.
75+
By specifying `revision=<VERSION>`, you can load the specific version of the dataset.
76+
77+
```python
78+
>>> t4 = Tier4("data/tier4/", revision="2", verbose=True)
79+
```
80+
7481
## Access to Schema Fields
7582

7683
---

t4_devkit/cli/sanity.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
)
1919

2020

21-
def _run_sanity_check(db_parent: str, *, include_warning: bool = False) -> list[DBException]:
21+
def _run_sanity_check(
22+
db_parent: str,
23+
*,
24+
revision: str | None = None,
25+
include_warning: bool = False,
26+
) -> list[DBException]:
2227
exceptions: list[DBException] = []
2328

2429
db_dirs: list[Path] = Path(db_parent).glob("*")
2530

2631
for db_root in tqdm(db_dirs, desc=">>>Sanity checking..."):
27-
result = sanity_check(db_root, include_warning=include_warning)
32+
result = sanity_check(db_root, revision=revision, include_warning=include_warning)
2833
if result:
2934
exceptions.append(result)
3035
return exceptions
@@ -41,11 +46,14 @@ def main(
4146
is_eager=True,
4247
),
4348
db_parent: str = typer.Argument(..., help="Path to parent directory of the databases."),
49+
revision: str | None = typer.Option(
50+
None, "-rv", "--revision", help="Specify if you want to check the specific version."
51+
),
4452
include_warning: bool = typer.Option(
4553
False, "-iw", "--include-warning", help="Indicates whether to report any warnings."
4654
),
4755
) -> None:
48-
exceptions = _run_sanity_check(db_parent, include_warning=include_warning)
56+
exceptions = _run_sanity_check(db_parent, revision=revision, include_warning=include_warning)
4957

5058
headers = ["DatasetID", "Version", "Message"]
5159
table = [[e.dataset_id, e.version, e.message] for e in exceptions]

t4_devkit/cli/visualize.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
@cli.command("scene", help="Visualize a specific scene.")
2121
def scene(
2222
data_root: Annotated[str, typer.Argument(help="Root directory path to the dataset.")],
23+
revision: Annotated[
24+
str | None,
25+
typer.Option(
26+
..., "-rv", "--revision", help="Specify if you want to load the specific version."
27+
),
28+
] = None,
2329
future: Annotated[
2430
float,
2531
typer.Option(
@@ -41,14 +47,20 @@ def scene(
4147
) -> None:
4248
_create_dir(output)
4349

44-
t4 = Tier4(data_root, verbose=False)
50+
t4 = Tier4(data_root, revision=revision, verbose=False)
4551
t4.render_scene(future_seconds=future, save_dir=output)
4652

4753

4854
@cli.command("instance", help="Visualize a particular instance in the corresponding scene.")
4955
def instance(
5056
data_root: Annotated[str, typer.Argument(help="Root directory path to the dataset.")],
5157
instance: Annotated[list[str], typer.Argument(help="Instance token(s).")],
58+
revision: Annotated[
59+
str | None,
60+
typer.Option(
61+
..., "-rv", "--revision", help="Specify if you want to load the specific version."
62+
),
63+
] = None,
5264
future: Annotated[
5365
float,
5466
typer.Option(
@@ -70,13 +82,19 @@ def instance(
7082
) -> None:
7183
_create_dir(output)
7284

73-
t4 = Tier4(data_root, verbose=False)
85+
t4 = Tier4(data_root, revision=revision, verbose=False)
7486
t4.render_instance(instance_token=instance, future_seconds=future, save_dir=output)
7587

7688

7789
@cli.command("pointcloud", help="Visualize pointcloud in the corresponding scene.")
7890
def pointcloud(
7991
data_root: Annotated[str, typer.Argument(help="Root directory path to the dataset.")],
92+
revision: Annotated[
93+
str | None,
94+
typer.Option(
95+
..., "-rv", "--revision", help="Specify if you want to load the specific version."
96+
),
97+
] = None,
8098
ignore_distortion: Annotated[
8199
bool,
82100
typer.Option(
@@ -98,7 +116,7 @@ def pointcloud(
98116
) -> None:
99117
_create_dir(output)
100118

101-
t4 = Tier4(data_root, verbose=False)
119+
t4 = Tier4(data_root, revision=revision, verbose=False)
102120
t4.render_pointcloud(ignore_distortion=ignore_distortion, save_dir=output)
103121

104122

t4_devkit/common/sanity.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ class DBException:
1919
message: str
2020

2121

22-
def sanity_check(db_root: str | Path, *, include_warning: bool = False) -> DBException | None:
22+
def sanity_check(
23+
db_root: str | Path,
24+
*,
25+
revision: str | None = None,
26+
include_warning: bool = False,
27+
) -> DBException | None:
2328
"""Perform sanity check and report exception or warning encountered while loading the dataset.
2429
2530
Args:
2631
db_root (str | Path): Path to root directory of the dataset.
32+
revision (str | None, optional): Specific version of the dataset.
33+
If None, search the latest one.
2734
include_warning (bool, optional): Indicates whether to report warnings.
2835
2936
Returns:
@@ -37,7 +44,7 @@ def sanity_check(db_root: str | Path, *, include_warning: bool = False) -> DBExc
3744
warnings.filterwarnings("ignore")
3845

3946
try:
40-
_ = Tier4(data_root=db_root, verbose=False)
47+
_ = Tier4(data_root=db_root, revision=revision, verbose=False)
4148
exception = None
4249
except Exception as e:
4350
metadata = load_metadata(db_root)

t4_devkit/tier4.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,45 +46,72 @@
4646

4747
@define
4848
class DBMetadata:
49+
"""A dataclass to represent dataset metadata.
50+
51+
Attributes:
52+
data_root (str): Root directory path.
53+
dataset_id (str): Unique dataset ID.
54+
version (str | None): Dataset version.
55+
"""
56+
4957
data_root: str
5058
dataset_id: str
5159
version: str | None
5260

5361

54-
def load_metadata(db_root: str) -> DBMetadata:
62+
def load_metadata(db_root: str, revision: str | None = None) -> DBMetadata:
5563
"""Load metadata of T4 dataset including root directory path, dataset ID, and version.
5664
5765
Args:
5866
db_root (str): Path to root directory of database.
67+
revision (str | None, optional): Specify version of the dataset.
68+
If None, search the latest one.
5969
6070
Returns:
6171
Metadata of T4 dataset.
6272
"""
6373
db_root_path = Path(db_root)
74+
dataset_id = db_root_path.name
6475

6576
version_pattern = re.compile(r".*/\d+$")
6677
versions = [d.name for d in db_root_path.iterdir() if version_pattern.match(d.as_posix())]
6778

68-
if versions:
69-
version = sorted(versions)[-1]
70-
data_root = db_root_path.joinpath(version).as_posix()
79+
if revision is None:
80+
if versions:
81+
version = sorted(versions)[-1]
82+
data_root = db_root_path.joinpath(version).as_posix()
83+
else:
84+
version = None
85+
data_root = db_root_path.as_posix()
7186
else:
72-
version = None
73-
data_root = db_root_path.as_posix()
87+
if revision not in versions:
88+
raise ValueError(f"The version: {revision} is not included in {dataset_id}")
89+
version = revision
90+
data_root = db_root_path.joinpath(version).as_posix()
91+
92+
if version is None:
93+
warnings.warn(f"{dataset_id} does't contain any versions.", DeprecationWarning)
7494

75-
return DBMetadata(data_root=data_root, dataset_id=db_root_path.name, version=version)
95+
return DBMetadata(data_root=data_root, dataset_id=dataset_id, version=version)
7696

7797

7898
class Tier4:
7999
"""Database class for T4 dataset to help query and retrieve information from the database."""
80100

81101
schema_dir: str = "annotation"
82102

83-
def __init__(self, data_root: str, verbose: bool = True) -> None:
103+
def __init__(
104+
self,
105+
data_root: str,
106+
revision: str | None = None,
107+
verbose: bool = True,
108+
) -> None:
84109
"""Load database and creates reverse indexes and shortcuts.
85110
86111
Args:
87112
data_root (str): Path to the root directory of dataset.
113+
revision (str | None, optional): You can specify any specific version if you want.
114+
If None, search the latest one.
88115
verbose (bool, optional): Whether to display status during load.
89116
90117
Examples:
@@ -115,7 +142,7 @@ def __init__(self, data_root: str, verbose: bool = True) -> None:
115142
======
116143
117144
"""
118-
self._metadata = load_metadata(data_root)
145+
self._metadata = load_metadata(data_root, revision)
119146

120147
if not osp.exists(self.data_root):
121148
raise FileNotFoundError(f"Database directory is not found: {self.data_root}")

0 commit comments

Comments
 (0)