Skip to content

Commit

Permalink
Merge pull request #46 from DSD-DBS/statsmodule
Browse files Browse the repository at this point in the history
Statsmodule
  • Loading branch information
tklockau authored Sep 28, 2023
2 parents 79c79e6 + 94ade2f commit c9efb03
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ ignore_missing_imports = true
convention = "numpy"
add-select = [
"D212", # Multi-line docstring summary should start at the first line
"D402", # First line should not be the function’s “signature
"D402", # First line should not be the functions "signature"
"D417", # Missing argument descriptions in the docstring
]
add-ignore = [
Expand Down
2 changes: 1 addition & 1 deletion raillabel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""Devkit for working with recorded and annotated train ride data from DB."""
from importlib import metadata

from . import _util, format
from . import _util, format, stats
from .exceptions import *
from .filter.filter import filter
from .format import Scene
Expand Down
5 changes: 5 additions & 0 deletions raillabel/stats/__init__.py
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
36 changes: 36 additions & 0 deletions raillabel/stats/generate_timespan.py
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)
71 changes: 71 additions & 0 deletions tests/test_raillabel/stats/test_generate_timespan.py
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"])

0 comments on commit c9efb03

Please sign in to comment.