Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11 add domain specific data types for ship situation etc using pydantic models #12

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
fba9f06
Added domain specific data types for Ship, Situation etc. using pydan…
ClaasRostock Nov 24, 2023
185418a
Changed all code to use the new data types. Added type hints.
ClaasRostock Nov 24, 2023
2a9dc0b
Sphinx documentation: resolved one issue raised by pyright in conf.py
ClaasRostock Nov 24, 2023
3ed5fec
refactored tests to work with the new data types
ClaasRostock Nov 24, 2023
fa5dc40
added tests for reading files
ClaasRostock Nov 24, 2023
3e50900
added test for writing files
ClaasRostock Nov 24, 2023
e7325a1
types.py: Changed enum classes to subclass from Enum instead of StrEn…
ClaasRostock Nov 24, 2023
8d12f24
types.py: Changed declaration of optional fields
ClaasRostock Nov 24, 2023
c6ba720
types.py: Changed declaration of optional fields
ClaasRostock Nov 24, 2023
23d96c3
Changed declaration of optional fields
ClaasRostock Nov 24, 2023
5bca51a
test_read_files.py : Improved tests to be more robust to random chang…
ClaasRostock Nov 25, 2023
95137d6
test_write_files.py : Improved tests to be more robust to random chan…
ClaasRostock Nov 25, 2023
db91fb5
test_trafficgen.py: test_gen_situations_1_ts_partly_spec_cli()
ClaasRostock Nov 25, 2023
edd9e7c
write_traffic_situation_to_file.py: exclude fields the value of which…
ClaasRostock Nov 25, 2023
e0e7793
write_traffic_situation_to_file.py: exclude fields the value of which…
ClaasRostock Nov 25, 2023
7483674
Commit 185418af (open)
ClaasRostock Nov 25, 2023
34368fe
test_trafficgen.py : removed one ruff exemption
ClaasRostock Nov 25, 2023
99df3c6
test_trafficgen.py: test_gen_situations_1_ts_full_spec_cli()
ClaasRostock Nov 25, 2023
73c75af
Fixed todo's, wrong order of outputs from calculate_relative_bearing.…
tomarnepedersen Nov 27, 2023
509ff30
fixed a ruff error.
tomarnepedersen Nov 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import sys

try:
import tomli as tomli
import tomli as tomli # pyright: ignore
except ImportError:
# for Python >= 3.11
import tomllib as tomli
import tomllib as tomli # pyright: ignore

with open("../pyproject.toml", "rb") as f:
toml = tomli.load(f)
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ basemap = "^1.3.8"
basemap-data-hires = "^1.3.2"
global-land-mask = "^1.0.0"
folium = "^0.14.0"
pydantic = "^2.5"

[tool.poetry.group.dev]
optional = true
Expand Down Expand Up @@ -128,7 +129,7 @@ extraPaths = ["./src"]
typeCheckingMode = "basic"
useLibraryCodeForTypes = true
# Activate the following rules step by step to (step by step..) improve code quality
# reportMissingParameterType = "error"
reportMissingParameterType = "error"
# reportUnknownParameterType = "warning"
# reportUnknownMemberType = "warning"
# reportMissingTypeArgument = "error"
Expand Down
8 changes: 2 additions & 6 deletions src/trafficgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from .check_land_crossing import path_crosses_land

from .write_traffic_situation_to_file import write_traffic_situations_to_json_file
from .write_traffic_situation_to_file import clean_traffic_situation_data

from .encounter import generate_encounter
from .encounter import check_encounter_evolvement
Expand All @@ -39,10 +38,9 @@
from .read_files import read_situation_files
from .read_files import read_own_ship_file
from .read_files import read_target_ship_files
from .read_files import read_encounter_setting_file
from .read_files import read_encounter_settings_file

from .ship_traffic_generator import generate_traffic_situations
from .ship_traffic_generator import find_value


__all__ = [
Expand All @@ -60,7 +58,6 @@
"ssa",
"path_crosses_land",
"write_traffic_situations_to_json_file",
"clean_traffic_situation_data",
"generate_encounter",
"check_encounter_evolvement",
"find_start_position_target_ship",
Expand All @@ -75,11 +72,10 @@
"update_position_data_own_ship",
"update_position_data_target_ship",
"decide_target_ship",
"find_value",
"read_situation_files",
"read_own_ship_file",
"read_target_ship_files",
"read_encounter_setting_file",
"read_encounter_settings_file",
"plot_traffic_situations",
"plot_specific_traffic_situation",
]
22 changes: 15 additions & 7 deletions src/trafficgen/check_land_crossing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
"""Module with helper functions to determine if a generated path is crossing land."""

from typing import List

from global_land_mask import globe

from trafficgen.types import Position

from . import calculate_position_at_certain_time, deg_2_rad, flat2llh, rad_2_deg


def path_crosses_land(position_1, speed, course, lat_lon_0, time_interval=50):
def path_crosses_land(
position_1: Position,
speed: float,
course: float,
lat_lon_0: List[float],
time_interval: float = 50.0,
) -> bool:
"""
Find if path is crossing land.

Expand All @@ -21,19 +31,17 @@ def path_crosses_land(position_1, speed, course, lat_lon_0, time_interval=50):
is_on_land: True if parts of the path crosses land.
"""

north_1 = position_1["north"]
east_1 = position_1["east"]
north_1 = position_1.north
east_1 = position_1.east
lat_0 = lat_lon_0[0]
lon_0 = lat_lon_0[1]

num_checks = 10
for i in range(int(time_interval / num_checks)):
position_2 = calculate_position_at_certain_time(
{"north": north_1, "east": east_1}, speed, course, i * time_interval / num_checks
)
lat, lon, _ = flat2llh(
position_2["north"], position_2["east"], deg_2_rad(lat_0), deg_2_rad(lon_0)
Position(north=north_1, east=east_1), speed, course, i * time_interval / num_checks
)
lat, lon, _ = flat2llh(position_2.north, position_2.east, deg_2_rad(lat_0), deg_2_rad(lon_0))
lat = rad_2_deg(lat)
lon = rad_2_deg(lon)
if globe.is_land(lat, lon):
Expand Down
41 changes: 22 additions & 19 deletions src/trafficgen/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# pyright: reportMissingParameterType=false
"""CLI for trafficgen package."""

import contextlib
import logging
import sys
from os.path import join
from pathlib import Path

import click
Expand All @@ -20,12 +22,12 @@
# if you change the below defaults, then remember to change the description of
# the default values in below @click.option descriptions,
# and docs/usage.rst
default_data_path = Path(__file__).parent.parent.parent / "data"
sit_folder = join(default_data_path, "baseline_situations_input/")
own_ship_file = join(default_data_path, "own_ship/own_ship.json")
target_ship_folder = join(default_data_path, "target_ships/")
settings_file = Path(__file__).parent / "settings" / "encounter_settings.json"
output_folder = join(default_data_path, "test_output/")
default_data_path: Path = Path(__file__).parent.parent.parent / "data"
situation_folder: Path = default_data_path / "baseline_situations_input"
own_ship_file: Path = default_data_path / "own_ship/own_ship.json"
target_ship_folder: Path = default_data_path / "target_ships"
settings_file: Path = Path(__file__).parent / "settings" / "encounter_settings.json"
output_folder: Path = default_data_path / "test_output"


@click.group()
Expand All @@ -43,9 +45,9 @@ def main(args=None):
@click.option(
"-s",
"--situations",
help="Folders with situations (default=./baseline_situations_input/)",
help="Path to folder with situations (default=./baseline_situations_input/)",
type=click.Path(exists=True),
default=sit_folder,
default=situation_folder,
show_default=True,
)
@click.option(
Expand Down Expand Up @@ -123,10 +125,10 @@ def gen_situation(
"""
click.echo("Generating traffic situations")
generated_traffic_situations = generate_traffic_situations(
situation_folder=situations,
own_ship_file=own_ship,
target_ship_folder=targets,
settings_file=settings,
situation_folder=Path(situations),
own_ship_file=Path(own_ship),
target_ship_folder=Path(targets),
settings_file=Path(settings),
)

if visualize:
Expand All @@ -137,20 +139,21 @@ def gen_situation(
# so it can safely be ignored by users without generating an error msg,
# and so that if a user specifies a value of zero or negative number,
# the user will get an error message.
try:

# Ignore TypeError
# TypeError is thrown in case a value for a parameter is not defined.
# In such case, though, we safely ignore that parameter :)
with contextlib.suppress(TypeError):
if visualize_situation > 0:
click.echo("Plotting a specific traffic situation")
plot_specific_traffic_situation(generated_traffic_situations, visualize_situation)
else:
click.echo(
"Invalid traffic situation number specified, not creating map plot. See --help for more info."
) # noqa: E501
except TypeError:
pass # do nothing, value not defined, so we safely ignore this parameter :)

)
if output is not None:
click.echo("Writing traffic situations to files")
write_traffic_situations_to_json_file(generated_traffic_situations, write_folder=output)
write_traffic_situations_to_json_file(generated_traffic_situations, write_folder=Path(output))


main.add_command(gen_situation)
Expand Down
Loading