-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from DSD-DBS/statsmodule
Statsmodule
- Loading branch information
Showing
5 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright DB Netz AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
"""Statsmodule provides statistics.""" | ||
|
||
from .generate_timespan import generate_timespan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright DB Netz AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import typing as t | ||
from decimal import Decimal | ||
|
||
from ..format import Scene | ||
|
||
|
||
def generate_timespan(scene: Scene) -> t.Tuple[t.Optional[Decimal], t.Optional[Decimal]]: | ||
"""Return start and end timestamp of the scene. | ||
Parameters | ||
---------- | ||
scene: raillabel.format.Scene | ||
Scene the timespan should be based off. | ||
Returns | ||
------- | ||
decimal.Decimal or None | ||
Start timestamp of the scene. Is None if the scene has no frames. | ||
decimal.Decimal or None | ||
End timestamp of the scene. Is None if the scene has no frames. | ||
""" | ||
start_timestamp = None | ||
end_timestamp = None | ||
|
||
for frame in scene.frames.values(): | ||
|
||
if start_timestamp == None or frame.timestamp < start_timestamp: | ||
start_timestamp = frame.timestamp | ||
|
||
if end_timestamp == None or frame.timestamp > end_timestamp: | ||
end_timestamp = frame.timestamp | ||
|
||
return (start_timestamp, end_timestamp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright DB Netz AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
import sys | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
sys.path.insert(1, str(Path(__file__).parent.parent.parent.parent)) | ||
|
||
import raillabel | ||
|
||
# == Fixtures ============================ | ||
|
||
@pytest.fixture | ||
def metadata() -> raillabel.format.Metadata: | ||
return raillabel.format.Metadata(schema_version="1.0.0") | ||
|
||
# == Tests ============================ | ||
|
||
def test_simple_timespan(metadata): | ||
scene = raillabel.Scene( | ||
metadata=metadata, | ||
frames={ | ||
0: raillabel.format.Frame( | ||
uid=0, | ||
timestamp=100 | ||
), | ||
1: raillabel.format.Frame( | ||
uid=1, | ||
timestamp=105 | ||
), | ||
2: raillabel.format.Frame( | ||
uid=0, | ||
timestamp=110 | ||
), | ||
} | ||
) | ||
|
||
assert raillabel.stats.generate_timespan(scene) == (100, 110) | ||
|
||
def test_unordered_timspan(metadata): | ||
scene = raillabel.Scene( | ||
metadata=metadata, | ||
frames={ | ||
0: raillabel.format.Frame( | ||
uid=0, | ||
timestamp=110 | ||
), | ||
1: raillabel.format.Frame( | ||
uid=1, | ||
timestamp=100 | ||
), | ||
} | ||
) | ||
|
||
assert raillabel.stats.generate_timespan(scene) == (100, 110) | ||
|
||
def test_empty_timespan(metadata): | ||
scene = raillabel.Scene( | ||
metadata=metadata, | ||
frames={} | ||
) | ||
|
||
assert raillabel.stats.generate_timespan(scene) == (None, None) | ||
|
||
|
||
if __name__ == "__main__": | ||
os.system("clear") | ||
pytest.main([__file__, "--disable-pytest-warnings", "--cache-clear", "-v"]) |