Skip to content

Commit

Permalink
Working on #17, fixing some ruff and black warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tomarnepedersen committed Jan 19, 2024
1 parent 009a403 commit d0bce34
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 25 deletions.
9 changes: 2 additions & 7 deletions src/trafficgen/encounter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import random
from operator import pos
from typing import List, Optional, Tuple, Union

import numpy as np
Expand Down Expand Up @@ -123,9 +122,7 @@ def generate_encounter(
else:
target_ship.initial.sog = relative_sog * own_ship.initial.sog

target_ship.initial.sog = np.minimum(
target_ship.initial.sog, target_ship.static.speed_max
)
target_ship.initial.sog = np.minimum(target_ship.initial.sog, target_ship.static.speed_max)

target_ship_vector_length = knot_2_m_pr_min(target_ship.initial.sog) * vector_time
start_position_target_ship, position_found = find_start_position_target_ship(
Expand Down Expand Up @@ -535,8 +532,7 @@ def calculate_ship_cog(pos_0: Position, pos_1: Position) -> float:
-------
cog: Ship cog [deg]
"""
cog: float = np.arctan2(pos_1.east - pos_0.east,
pos_1.north - pos_0.north)
cog: float = np.arctan2(pos_1.east - pos_0.east, pos_1.north - pos_0.north)
if cog < 0.0:
cog += 2 * np.pi
return round(np.rad2deg(cog), 1)
Expand Down Expand Up @@ -717,7 +713,6 @@ def update_position_data_own_ship(
ship_position_future.latitude = round(rad_2_deg(lat_future), 6)
ship_position_future.longitude = round(rad_2_deg(lon_future), 6)


ship.waypoints = [
Waypoint(position=ship.initial.position.model_copy()),
Waypoint(position=ship_position_future),
Expand Down
4 changes: 1 addition & 3 deletions src/trafficgen/plot_traffic_situation.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ def add_ship_to_map(
vector_length = vector_time * knot_2_m_pr_min(ship.initial.sog)
_ = map_plot.add_child(
Polygon(
calculate_vector_arrow(
ship.initial.position, ship.initial.cog, vector_length, lat_lon_0
),
calculate_vector_arrow(ship.initial.position, ship.initial.cog, vector_length, lat_lon_0),
fill=True,
fill_opacity=1,
color=color,
Expand Down
10 changes: 5 additions & 5 deletions src/trafficgen/read_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
from pathlib import Path
from typing import List
from uuid import UUID, uuid1, uuid4
from uuid import UUID, uuid4

from trafficgen.types import EncounterSettings, OwnShip, TargetShip, TrafficSituation

Expand Down Expand Up @@ -46,9 +46,9 @@ def read_own_ship_file(own_ship_file: Path) -> OwnShip:
with open(own_ship_file, encoding="utf-8") as f:
data = json.load(f)

if 'static' in data and 'id' not in data['static']:
if "static" in data and "id" not in data["static"]:
ship_id: UUID = uuid4()
data['static'].update({'id':ship_id})
data["static"].update({"id": ship_id})

ship: OwnShip = OwnShip(**data)
return ship
Expand All @@ -71,9 +71,9 @@ def read_target_ship_files(target_ship_folder: Path) -> List[TargetShip]:
with open(file_path, encoding="utf-8") as f:
data = json.load(f)

if 'static' in data and 'id' not in data['static']:
if "static" in data and "id" not in data["static"]:
ship_id: UUID = uuid4()
data['static'].update({'id':ship_id})
data["static"].update({"id": ship_id})
target_ship: TargetShip = TargetShip(**data)
target_ships.append(target_ship)
return target_ships
Expand Down
14 changes: 9 additions & 5 deletions src/trafficgen/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

from enum import Enum
from typing import List, Union
from uuid import UUID, uuid4
from uuid import UUID

from pydantic import BaseModel, Field
from pydantic import BaseModel


def to_camel(string: str) -> str:
"""Return a camel case formated string from snake case string."""

words = string.split('_')
return words[0] + ''.join(word.capitalize() for word in words[1:])
words = string.split("_")
return words[0] + "".join(word.capitalize() for word in words[1:])


class Position(BaseModel):
"""Data type for a ship's position with attributes north, east in [m]."""
Expand Down Expand Up @@ -65,7 +66,6 @@ class GeneralShipType(str, Enum):
OTHER_TYPE = "Other Type"



class ShipStatic(BaseModel):
"""Static ship data that will not change during the scenario."""

Expand Down Expand Up @@ -98,6 +98,7 @@ class OwnShip(Ship):

pass


class TargetShip(Ship):
"""Data type for a target ship."""

Expand Down Expand Up @@ -129,6 +130,7 @@ class Config:
alias_generator = to_camel
populate_by_name = True


class TrafficSituation(BaseModel):
"""Data type for a traffic situation."""

Expand All @@ -147,6 +149,7 @@ class Config:
alias_generator = to_camel
populate_by_name = True


class EncounterClassification(BaseModel):
"""Data type for the encounter classification."""

Expand Down Expand Up @@ -177,6 +180,7 @@ class Config:
alias_generator = to_camel
populate_by_name = True


class EncounterSettings(BaseModel):
"""Data type for encounter settings."""

Expand Down
6 changes: 1 addition & 5 deletions src/trafficgen/write_traffic_situation_to_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ def write_traffic_situations_to_json_file(situations: List[TrafficSituation], wr
file_number: int = i + 1
output_file_path: Path = write_folder / f"traffic_situation_{file_number:02d}.json"
data: str = situation.model_dump_json(
by_alias=True,
indent=4,
exclude_unset=True,
exclude_defaults=False,
exclude_none=True
by_alias=True, indent=4, exclude_unset=True, exclude_defaults=False, exclude_none=True
)
with open(output_file_path, "w", encoding="utf-8") as outfile:
_ = outfile.write(data)

0 comments on commit d0bce34

Please sign in to comment.