From 0359e33f2e5ea6184c303c9e724fed4ca6f270c0 Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:25:13 +0100 Subject: [PATCH 01/12] pyproject.toml: Activated ruff category "D" rules (docstring checks) --- pyproject.toml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9d6aa9d..b19bc79 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,7 +54,7 @@ pythonpath = ["src"] [tool.black] line-length = 105 -target-version = ["py39", "py310"] +target-version = ["py39", "py310", "py311"] [tool.ruff] exclude = [ @@ -69,11 +69,11 @@ exclude = [ src = ["src"] ignore = [ "E501", # Line length too long - "D100", # Missing docstring in public module - "D101", # Missing docstring in public class - "D102", # Missing docstring in public method - "D103", # Missing docstring in public function - "D104", # Missing docstring in public package + # "D100", # Missing docstring in public module + # "D101", # Missing docstring in public class + # "D102", # Missing docstring in public method + # "D103", # Missing docstring in public function + # "D104", # Missing docstring in public package "D105", # Missing docstring in magic method "D107", # Missing docstring in __init__ "D202", # No blank lines allowed after function docstring @@ -91,7 +91,7 @@ line-length = 105 # Activate the following rules step by step to (step by step..) improve code quality select = [ "E", - # "D", + "D", # "F", # "N", # "W", From 6313a1d1f56f774df00405e694e71053697f57ad Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:28:32 +0100 Subject: [PATCH 02/12] renamed module util_methods.py to utils.py ("methods" I think is misleading, as in fact what is contained in the module are formally functions, not methods. Second, "utils" is very commonly used as name for a module or subpackage containing utility functions.) --- src/trafficgen/__init__.py | 18 +++++----- src/trafficgen/{util_methods.py => utils.py} | 35 ++++++++++++-------- 2 files changed, 31 insertions(+), 22 deletions(-) rename src/trafficgen/{util_methods.py => utils.py} (87%) diff --git a/src/trafficgen/__init__.py b/src/trafficgen/__init__.py index 17c0492..1f56a9c 100644 --- a/src/trafficgen/__init__.py +++ b/src/trafficgen/__init__.py @@ -1,14 +1,14 @@ """Top-level package for Traffic Generator.""" -from .util_methods import knot_2_m_pr_min -from .util_methods import m_pr_min_2_knot -from .util_methods import m2nm -from .util_methods import nm_2_m -from .util_methods import deg_2_rad -from .util_methods import rad_2_deg -from .util_methods import convert_angle_minus_180_to_180_to_0_to_360 -from .util_methods import convert_angle_0_to_360_to_minus_180_to_180 -from .util_methods import calculate_position_at_certain_time +from .utils import knot_2_m_pr_min +from .utils import m_pr_min_2_knot +from .utils import m2nm +from .utils import nm_2_m +from .utils import deg_2_rad +from .utils import rad_2_deg +from .utils import convert_angle_minus_180_to_180_to_0_to_360 +from .utils import convert_angle_0_to_360_to_minus_180_to_180 +from .utils import calculate_position_at_certain_time from .marine_system_simulator import flat2llh from .marine_system_simulator import llh2flat diff --git a/src/trafficgen/util_methods.py b/src/trafficgen/utils.py similarity index 87% rename from src/trafficgen/util_methods.py rename to src/trafficgen/utils.py index 78b080a..7c51ffe 100644 --- a/src/trafficgen/util_methods.py +++ b/src/trafficgen/utils.py @@ -1,4 +1,4 @@ -"""This module includes utillities methods that is used by several other functions.""" +"""Utility functions that are used by several other functions.""" import numpy as np @@ -10,7 +10,8 @@ def m_pr_min_2_knot(speed_in_m_pr_min): Params: speed_in_m_pr_min: Ship speed in meters pr second - Returns: + Returns + ------- speed_in_knot: Ship speed given in knots """ @@ -25,7 +26,8 @@ def knot_2_m_pr_min(speed_in_knot): Params: speed_in_knot: Ship speed given in knots - Returns: + Returns + ------- speed_in_m_pr_min: Ship speed in meters pr minutes """ @@ -40,7 +42,8 @@ def m2nm(length_in_m): Params: length_in_m: Length given in meters - Returns: + Returns + ------- length_in_nm: Length given in nautical miles """ @@ -55,7 +58,8 @@ def nm_2_m(val): Params: length_in_nm: Length given in nautical miles - Returns: + Returns + ------- length_in_m: Length given in meters """ @@ -70,7 +74,8 @@ def deg_2_rad(angle_in_degrees): Params: angle_in_degrees: Angle given in degrees - Returns: + Returns + ------- angle given in radians: Angle given in radians """ @@ -84,7 +89,8 @@ def rad_2_deg(angle_in_radians): Params: angle_in_degrees: Angle given in degrees - Returns: + Returns + ------- angle given in radians: Angle given in radians """ @@ -95,12 +101,13 @@ def rad_2_deg(angle_in_radians): def convert_angle_minus_180_to_180_to_0_to_360(angle_180): """ Convert an angle given in the region -180 to 180 degrees to an - angle given in the region 0 to 360 degrees + angle given in the region 0 to 360 degrees. Params: angle_180: Angle given in the region -180 to 180 degrees - Returns: + Returns + ------- angle_360: Angle given in the region 0 to 360 degrees """ @@ -111,12 +118,13 @@ def convert_angle_minus_180_to_180_to_0_to_360(angle_180): def convert_angle_0_to_360_to_minus_180_to_180(angle_360): """ Convert an angle given in the region 0 to 360 degrees to an - angle given in the region -180 to 180 degrees + angle given in the region -180 to 180 degrees. Params: angle_360: Angle given in the region 0 to 360 degrees - Returns: + Returns + ------- angle_180: Angle given in the region -180 to 180 degrees """ @@ -126,7 +134,7 @@ def convert_angle_0_to_360_to_minus_180_to_180(angle_360): def calculate_position_at_certain_time(position, speed, course, delta_time): """ - Calculates the position of the ship at a given time based on initial position + Calculate the position of the ship at a given time based on initial position and delta time, and constand speed and course. Params: @@ -135,7 +143,8 @@ def calculate_position_at_certain_time(position, speed, course, delta_time): course: Ship course [deg] delta_time: Delta time from now to the time new position is being calculated [minutes] - Returns: + Returns + ------- position{north, east}: Dict, north and east position given in meters """ From 4003654c476be40ff8b039216af9061df5bfc301 Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:28:56 +0100 Subject: [PATCH 03/12] adapted docs to the renamed utils.py module --- docs/trafficgen.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/trafficgen.rst b/docs/trafficgen.rst index da5f234..a5a5fbf 100644 --- a/docs/trafficgen.rst +++ b/docs/trafficgen.rst @@ -63,7 +63,7 @@ trafficgen.ship\_traffic\_generator module trafficgen.util\_methods module ------------------------------- -.. automodule:: trafficgen.util_methods +.. automodule:: trafficgen.utils :members: :undoc-members: :show-inheritance: From 1ad403c61b7172b07295cee21e4c9f9fb6c5e914 Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:29:26 +0100 Subject: [PATCH 04/12] resolved issues raised by ruff's category "D" rules (docstring checks) --- src/trafficgen/check_land_crossing.py | 10 +- src/trafficgen/cli.py | 10 +- src/trafficgen/encounter.py | 94 +++++++++++-------- src/trafficgen/marine_system_simulator.py | 26 +++-- src/trafficgen/plot_traffic_situation.py | 40 ++++---- src/trafficgen/read_files.py | 24 ++--- src/trafficgen/ship_traffic_generator.py | 25 +++-- .../write_traffic_situation_to_file.py | 11 ++- 8 files changed, 140 insertions(+), 100 deletions(-) diff --git a/src/trafficgen/check_land_crossing.py b/src/trafficgen/check_land_crossing.py index 3613ad8..9fb5899 100644 --- a/src/trafficgen/check_land_crossing.py +++ b/src/trafficgen/check_land_crossing.py @@ -1,9 +1,8 @@ -"""This module finds if the generated path is crossing land.""" +"""Module with helper functions to determine if a generated path is crossing land.""" from global_land_mask import globe -from . import flat2llh -from . import deg_2_rad, rad_2_deg -from . import calculate_position_at_certain_time + +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): @@ -17,7 +16,8 @@ def path_crosses_land(position_1, speed, course, lat_lon_0, time_interval=50): lat_lon_0: Reference point, latitudinal [degree] and longitudinal [degree]. time_interval: The time interval the vessel should travel without crossing land [minutes] - Returns: + Returns + ------- is_on_land: True if parts of the path crosses land. """ diff --git a/src/trafficgen/cli.py b/src/trafficgen/cli.py index 573cdeb..4f40d71 100644 --- a/src/trafficgen/cli.py +++ b/src/trafficgen/cli.py @@ -31,6 +31,10 @@ @click.group() @click_log.simple_verbosity_option(logger) def main(args=None): + """Entry point for console script as configured in pyproject.toml. + + Runs the command line interface and parses arguments and options entered on the console. + """ return 0 @@ -112,9 +116,11 @@ def gen_situation( visualize_situation, output, ): - """Console script for trafficgen. Example: \n + r"""Console script for trafficgen. + Example: \n trafficgen gen-situation -s ./data/example_situations_input - -o ./data/test_output_1""" + -o ./data/test_output_1. + """ click.echo("Generating traffic situations") generated_traffic_situations = generate_traffic_situations( situation_folder=situations, diff --git a/src/trafficgen/encounter.py b/src/trafficgen/encounter.py index 8c5401a..046b609 100644 --- a/src/trafficgen/encounter.py +++ b/src/trafficgen/encounter.py @@ -1,22 +1,25 @@ """ -This module generates encounters consisting of one own ship and one to many target ships. +Functions to generate encounters consisting of one own ship and one to many target ships. The generated encounters may be of type head-on, overtaking give-way and stand-on and crossing give-way and stand-on. """ -import random import copy +import random + import numpy as np -from . import convert_angle_minus_180_to_180_to_0_to_360 -from . import knot_2_m_pr_min -from . import m_pr_min_2_knot -from . import nm_2_m -from . import deg_2_rad -from . import rad_2_deg -from . import calculate_position_at_certain_time -from . import path_crosses_land -from . import flat2llh +from . import ( + calculate_position_at_certain_time, + convert_angle_minus_180_to_180_to_0_to_360, + deg_2_rad, + flat2llh, + knot_2_m_pr_min, + m_pr_min_2_knot, + nm_2_m, + path_crosses_land, + rad_2_deg, +) def generate_encounter( @@ -30,7 +33,7 @@ def generate_encounter( settings, ): """ - Generates an encounter. + Generate an encounter. Params: * desired_encounter_type: Desired encounter to be generated @@ -43,7 +46,8 @@ def generate_encounter( * vector_time_default: User defined vector time. If not set, this is None. * settings: Encounter settings - Returns: + Returns + ------- target_ship: target ship information, such as initial position, speed and course encounter_found: 0=encounter not found, 1=encounter found """ @@ -158,7 +162,7 @@ def check_encounter_evolvement( settings, ): """ - Checks encounter evolvement. The generated encounter should be the same type of + Check encounter evolvement. The generated encounter should be the same type of encounter (head-on, crossing, give-way) also some time before the encounter is started. Params: @@ -167,7 +171,8 @@ def check_encounter_evolvement( * desired_encounter_type: Desired type of encounter to be generated * settings: Encounter settings - Returns: + Returns + ------- * returns 0 if encounter not ok, 1 if encounter ok """ theta13_criteria = settings["classification"]["theta13_criteria"] @@ -206,7 +211,7 @@ def calculate_min_vector_length_target_ship( own_ship_position, own_ship_course, target_ship_position_future, desired_beta ): """ - Calculates minimum vector length (target ship speed x vector). This will + Calculate minimum vector length (target ship speed x vector). This will ensure that ship speed is high enough to find proper situation. Params: @@ -247,7 +252,8 @@ def find_start_position_target_ship( * desired_encounter_type: Desired type of encounter to be generated * settings: Encounter settings - Returns: + Returns + ------- * start_position_target_ship: Dict, initial position of target ship {north, east} [m] * start_position_found: 0=position not found, 1=position found """ @@ -338,7 +344,8 @@ def assign_future_position_to_target_ship(own_ship_position_future, max_meeting_ * max_meeting_distance: Maximum distance between own ship and target ship at a given time in the future [nm] - Returns: + Returns + ------- future_position_target_ship: Dict, future position of target ship {north, east} [m] """ random_angle = random.uniform(0, 1) * 2 * np.pi @@ -351,7 +358,7 @@ def assign_future_position_to_target_ship(own_ship_position_future, max_meeting_ def determine_colreg(alpha, beta, theta13_criteria, theta14_criteria, theta15_criteria, theta15): """ - Determines the colreg type based on alpha, relative bearing between target ship and own + Determine the colreg type based on alpha, relative bearing between target ship and own ship seen from target ship, and beta, relative bearing between own ship and target ship seen from own ship. @@ -365,7 +372,8 @@ def determine_colreg(alpha, beta, theta13_criteria, theta14_criteria, theta15_cr * theta15: 22.5 deg aft of the beam, used for classifying a crossing and an overtaking encounter - Returns: + Returns + ------- * encounter classification """ # Mapping @@ -395,7 +403,7 @@ def calculate_relative_bearing( position_own_ship, heading_own_ship, position_target_ship, heading_target_ship ): """ - Calculates relative bearing between own ship and target ship, both seen from + Calculate relative bearing between own ship and target ship, both seen from own ship and seen from target ship. Params: @@ -404,7 +412,8 @@ def calculate_relative_bearing( * position_target_ship: Dict, own ship position {north, east} [m] * heading_target_ship: Target ship course [deg] - Returns: + Returns + ------- * alpha: relative bearing between target ship and own ship seen from target ship [deg] * beta: relative bearing between own ship and target ship seen from own ship [deg] """ @@ -467,13 +476,14 @@ def calculate_relative_bearing( def calculate_ship_course(waypoint_0, waypoint_1): """ - Calculates ship course between two waypoints + Calculate ship course between two waypoints. Params: * waypoint_0: Dict, waypoint {north, east} [m] * waypoint_1: Dict, waypoint {north, east} [m] - Returns: + Returns + ------- course: Ship course [deg] """ course = np.arctan2( @@ -486,12 +496,13 @@ def calculate_ship_course(waypoint_0, waypoint_1): def assign_vector_time(setting_vector_time): """ - Randomly (uniform) assigns vector time + Assign random (uniform) vector time. Params: * setting_vector_time: Minimum and maximum value for vector time - Returns: + Returns + ------- vector_time: Vector time [min] """ return setting_vector_time[0] + random.uniform(0, 1) * ( @@ -503,7 +514,7 @@ def assign_speed_to_target_ship( encounter_type, own_ship_speed, min_target_ship_speed, relative_speed_setting ): """ - Randomly (uniform) assigns speed to target ship depending on type of encounter + Assign random (uniform) speed to target ship depending on type of encounter. Params: * encounter_type: Type of encounter @@ -511,7 +522,8 @@ def assign_speed_to_target_ship( * min_target_ship_speed: Minimum target ship speed [knot] * relative_speed_setting: Relative speed setting dependent on encounter [-] - Returns: + Returns + ------- speed_target_ship: Target ship speed [knot] """ if encounter_type.lower() == "overtaking-stand-on": @@ -541,14 +553,15 @@ def assign_speed_to_target_ship( def assign_beta(encounter_type, settings): """ - Randomly (uniform) assigns relative bearing beta between own ship - and target ship depending on type of encounter + Assign random (uniform) relative bearing beta between own ship + and target ship depending on type of encounter. Params: * encounter_type: Type of encounter * settings: Encounter settings - Returns: + Returns + ------- Relative bearing between own ship and target ship seen from own ship [deg] """ theta13_crit = settings["classification"]["theta13_criteria"] @@ -573,14 +586,15 @@ def assign_beta(encounter_type, settings): def update_position_data_target_ship(ship, lat_lon_0): """ - Updating position data of the target ship to also include latitude and longitude - position of the target ship + Update position data of the target ship to also include latitude and longitude + position of the target ship. Params: * ship: Target ship data * lat_lon_0: Reference point, latitudinal [degree] and longitudinal [degree] - Returns: + Returns + ------- ship: Updated target ship data """ lat_0 = lat_lon_0[0] @@ -603,15 +617,16 @@ def update_position_data_target_ship(ship, lat_lon_0): def update_position_data_own_ship(ship, lat_lon_0, delta_time): """ - Updating position data of the target ship to also include latitude and longitude - position of the target ship + Update position data of the target ship to also include latitude and longitude + position of the target ship. Params: * ship: Own ship data * lat_lon_0: Reference point, latitudinal [degree] and longitudinal [degree] * delta_time: Delta time from now to the time new position is being calculated [minutes] - Returns: + Returns + ------- ship: Updated own ship data """ lat_0 = lat_lon_0[0] @@ -649,12 +664,13 @@ def update_position_data_own_ship(ship, lat_lon_0, delta_time): def decide_target_ship(target_ships): """ - Randomly picks a target ship from a dict of target ships + Randomly pick a target ship from a dict of target ships. Params: * target_ships: dict of target ships - Returns: + Returns + ------- The target ship, info of type, size etc. """ num_target_ships = len(target_ships) diff --git a/src/trafficgen/marine_system_simulator.py b/src/trafficgen/marine_system_simulator.py index e3e39b3..7ab1188 100644 --- a/src/trafficgen/marine_system_simulator.py +++ b/src/trafficgen/marine_system_simulator.py @@ -1,6 +1,7 @@ """ -The Marine Systems Simulator (MSS) is a Matlab and Simulink library for marine systems. It includes -models for ships, underwater vehicles, unmanned surface vehicles, and floating structures. +The Marine Systems Simulator (MSS) is a Matlab and Simulink library for marine systems. + +It includes models for ships, underwater vehicles, unmanned surface vehicles, and floating structures. The library also contains guidance, navigation, and control (GNC) blocks for real-time simulation. The algorithms are described in: @@ -14,6 +15,9 @@ def flat2llh(x_n, y_n, lat_0, lon_0, z_n=0.0, height_ref=0.0): """ + Compute longitude lon (rad), latitude lat (rad) and height h (m) for the + NED coordinates (xn,yn,zn). + Method taken from the MSS (Marine System Simulator) toolbox which is a Matlab/Simulink library for marine systems. @@ -34,7 +38,8 @@ def flat2llh(x_n, y_n, lat_0, lon_0, z_n=0.0, height_ref=0.0): h_ref=0.0: Flat earth coordinate with reference h_ref in meters above the surface of the ellipsoid - Returns: + Returns + ------- lat: Latitude [rad] lon: Longitude [rad] h: Height [m] @@ -60,6 +65,9 @@ def flat2llh(x_n, y_n, lat_0, lon_0, z_n=0.0, height_ref=0.0): def llh2flat(lat, lon, lat_0, lon_0, height=0.0, height_ref=0.0): """ + Compute (north, east) for a flat Earth coordinate system from longitude + lon (rad) and latitude lat (rad). + Method taken from the MSS (Marine System Simulator) toolbox which is a Matlab/Simulink library for marine systems. @@ -78,7 +86,8 @@ def llh2flat(lat, lon, lat_0, lon_0, height=0.0, height_ref=0.0): h_ref=0.0: Flat earth coordinate with reference h_ref in meters above the surface of the ellipsoid - Returns: + Returns + ------- x_n: Ship position, north [m] y_n: Ship position, east [m] z_n: Ship position, down [m] @@ -104,11 +113,13 @@ def llh2flat(lat, lon, lat_0, lon_0, height=0.0, height_ref=0.0): def ssa(angle): """ + Return the "smallest signed angle" (SSA) or the smallest difference between two angles. + Method taken from the MSS (Marine System Simulator) toolbox which is a Matlab/Simulink library for marine systems. - SSA is the "smallest signed angle" or the smallest difference between two - angles. Examples: + Examples + -------- angle = ssa(angle) maps an angle in rad to the interval [-pi pi) Author: Thor I. Fossen @@ -117,7 +128,8 @@ def ssa(angle): Param: angle: angle given in radius - Returns: + Returns + ------- smallest_angle: "smallest signed angle" or the smallest difference between two angles """ diff --git a/src/trafficgen/plot_traffic_situation.py b/src/trafficgen/plot_traffic_situation.py index 6c4ff05..d0ba948 100644 --- a/src/trafficgen/plot_traffic_situation.py +++ b/src/trafficgen/plot_traffic_situation.py @@ -1,23 +1,17 @@ -""" -This module includes functions to prepare and plot traffic situations -""" +"""Functions to prepare and plot traffic situations.""" import math -from matplotlib.patches import Circle -from folium import Map, Polygon -import numpy as np import matplotlib.pyplot as plt +import numpy as np +from folium import Map, Polygon +from matplotlib.patches import Circle -from . import knot_2_m_pr_min -from . import m2nm -from . import flat2llh -from . import deg_2_rad -from . import rad_2_deg +from . import deg_2_rad, flat2llh, knot_2_m_pr_min, m2nm, rad_2_deg def calculate_vector_arrow(position, direction, vector_length, lat_lon_0): """ - Calculates the arrow with length vector pointing in the direction of ship course + Calculate the arrow with length vector pointing in the direction of ship course. Params: position: {north}, {east} position of the ship [m] @@ -25,7 +19,8 @@ def calculate_vector_arrow(position, direction, vector_length, lat_lon_0): vector_length: length of vector lat_lon_0: Reference point, latitudinal [degree] and longitudinal [degree] - Returns: + Returns + ------- arrow_points: Polygon points to draw the arrow """ north_start = position["north"] @@ -63,7 +58,7 @@ def calculate_vector_arrow(position, direction, vector_length, lat_lon_0): def calculate_ship_outline(position, course, lat_lon_0, ship_length=100, ship_width=15): """ - Calculates the outline of the ship pointing in the direction of ship course + Calculate the outline of the ship pointing in the direction of ship course. Params: position: {north}, {east} position of the ship [m] @@ -72,7 +67,8 @@ def calculate_ship_outline(position, course, lat_lon_0, ship_length=100, ship_wi ship_length: Ship length. If not given, ship length is set to 100 ship_width: Ship width. If not given, ship width is set to 15 - Returns: + Returns + ------- ship_outline_points: Polygon points to draw the ship """ north_start = position["north"] @@ -155,7 +151,7 @@ def calculate_ship_outline(position, course, lat_lon_0, ship_length=100, ship_wi def plot_specific_traffic_situation(traffic_situations, situation_number): """ - Plots a specific situation in map + Plot a specific situation in map. Params: traffic_situations: Generated traffic situations @@ -192,7 +188,7 @@ def plot_specific_traffic_situation(traffic_situations, situation_number): def add_ship_to_map(ship, vector_time, lat_lon_0, map_plot=None, color="black"): """ - Adds the ship to the map + Add the ship to the map. Params: ship: Ship information @@ -202,6 +198,7 @@ def add_ship_to_map(ship, vector_time, lat_lon_0, map_plot=None, color="black"): color: Color of the ship. If not set, color is 'black' Returns + ------- m: Updated instance of Map. """ if map_plot is None: @@ -233,7 +230,7 @@ def add_ship_to_map(ship, vector_time, lat_lon_0, map_plot=None, color="black"): def plot_traffic_situations(traffic_situations, col, row): """ - Plots the traffic situations in one more figures + Plot the traffic situations in one more figures. Params: traffic_situations: Traffic situations to be plotted @@ -297,13 +294,14 @@ def plot_traffic_situations(traffic_situations, col, row): def find_max_value_for_plot(ship, max_value): """ - Finds the maximum deviation from the Reference point in north and east direction + Find the maximum deviation from the Reference point in north and east direction. Params: ship: Ship information max_value: maximum deviation in north, east direction - Returns: + Returns + ------- max_value: updated maximum deviation in north, east direction """ max_value = np.max( @@ -318,7 +316,7 @@ def find_max_value_for_plot(ship, max_value): def add_ship_to_plot(ship, vector_time, axes=None, color="black"): """ - Adds the ship to the plot + Add the ship to the plot. Params: ship: Ship information diff --git a/src/trafficgen/read_files.py b/src/trafficgen/read_files.py index e6a7904..3ea3be7 100644 --- a/src/trafficgen/read_files.py +++ b/src/trafficgen/read_files.py @@ -1,6 +1,4 @@ -""" -This module is used to read the files needed to build one or more traffic situations -""" +"""Functions to read the files needed to build one or more traffic situations.""" import json import os @@ -8,12 +6,13 @@ def read_situation_files(situation_folder): """ - Reads traffic situation files. + Read traffic situation files. Params: situation_folder: Path to the folder where situation files are found - Returns: + Returns + ------- situations: List of desired traffic situations """ situations = [] @@ -28,12 +27,13 @@ def read_situation_files(situation_folder): def read_own_ship_file(own_ship_file): """ - Reads the own ship file. + Read own ship file. Params: own_ship_file: Path to the own_ship_file file - Returns: + Returns + ------- own_ship information """ with open(own_ship_file, encoding="utf-8") as user_file: @@ -42,12 +42,13 @@ def read_own_ship_file(own_ship_file): def read_target_ship_files(target_ship_folder): """ - Reads target ship files. + Read target ship files. Params: target_ship_folder: Path to the folder where target ships are found - Returns: + Returns + ------- target_ships: List of different target ships """ target_ships = [] @@ -60,12 +61,13 @@ def read_target_ship_files(target_ship_folder): def read_encounter_setting_file(settings_file): """ - Reads the encounter setting file. + Read encounter settings file. Params: settings_file: Path to the encounter setting file - Returns: + Returns + ------- Encounter settings """ with open(settings_file, encoding="utf-8") as user_file: diff --git a/src/trafficgen/ship_traffic_generator.py b/src/trafficgen/ship_traffic_generator.py index 8821c90..cb3ea5c 100644 --- a/src/trafficgen/ship_traffic_generator.py +++ b/src/trafficgen/ship_traffic_generator.py @@ -1,17 +1,20 @@ -"""This is the main module for generating traffic situations""" +"""Functions to generate traffic situations.""" import copy -from . import generate_encounter -from . import update_position_data_own_ship -from . import read_situation_files -from . import read_own_ship_file -from . import read_target_ship_files -from . import read_encounter_setting_file +from . import ( + generate_encounter, + read_encounter_setting_file, + read_own_ship_file, + read_situation_files, + read_target_ship_files, + update_position_data_own_ship, +) def generate_traffic_situations(situation_folder, own_ship_file, target_ship_folder, settings_file): """ + Generate a set of traffic situations using input files. This is the main function for generating a set of traffic situations using input files specifying number and type of encounter, type of target ships etc. @@ -20,7 +23,8 @@ def generate_traffic_situations(situation_folder, own_ship_file, target_ship_fol * target_ship_folder: Path to where different type of target ships is found * settings_file: Path to settings file - Returns: + Returns + ------- traffic_situations: List of generated traffic situations. One situation may consist of one or more encounters. """ @@ -80,14 +84,15 @@ def generate_traffic_situations(situation_folder, own_ship_file, target_ship_fol def find_value(parameters, parameter): """ - Finds a key, value pair in a dict. If the key is there, + Find a key, value pair in a dict. If the key is there, the value is returned. If not, None is returned. Params: * parameters: Dict of parameters * parameter: Parameter key to look for in parameters - Returns: + Returns + ------- value: value of the key parameter """ return parameters[parameter] if parameter in parameters else None diff --git a/src/trafficgen/write_traffic_situation_to_file.py b/src/trafficgen/write_traffic_situation_to_file.py index 709470b..b06d764 100644 --- a/src/trafficgen/write_traffic_situation_to_file.py +++ b/src/trafficgen/write_traffic_situation_to_file.py @@ -1,13 +1,13 @@ -"""This module cleans traffic situations data before writing it to json-file.""" +"""Functions to clean traffic situations data before writing it to a json file.""" import json -from pathlib import Path import os +from pathlib import Path def write_traffic_situations_to_json_file(traffic_situations, write_folder): """ - Write traffic situations to json file + Write traffic situations to json file. Params: traffic_situations: Traffic situations to be written to file @@ -25,12 +25,13 @@ def write_traffic_situations_to_json_file(traffic_situations, write_folder): def clean_traffic_situation_data(traffic_situation): """ - Clean traffic situation data to json file + Clean traffic situation data to json file. Params: traffic_situation: Traffic situation to be cleaned - Returns: + Returns + ------- traffic_situation: Cleaned traffic situation """ From 6e70d63888666aea78ccf00feee6e6ec85e5e871 Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:30:46 +0100 Subject: [PATCH 05/12] pyproject.toml: Activated ruff category "F" rules --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b19bc79..edb9f4c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -92,7 +92,7 @@ line-length = 105 select = [ "E", "D", - # "F", + "F", # "N", # "W", # "I", From a19e3906dea708e0573888d46bcb6e458b6632fd Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:33:49 +0100 Subject: [PATCH 06/12] resolved issues raised by ruff's category "F" rules --- src/trafficgen/encounter.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/trafficgen/encounter.py b/src/trafficgen/encounter.py index 046b609..d9bf6f2 100644 --- a/src/trafficgen/encounter.py +++ b/src/trafficgen/encounter.py @@ -78,7 +78,9 @@ def generate_encounter( own_ship["start_pose"]["course"], vector_time, ) - own_ship_vector_length = knot_2_m_pr_min(own_ship["start_pose"]["speed"]) * vector_time + # @TODO: @TomArne: this variable is declared and assigned to but nowhere used. Delete? + # Claas, 2023-11-24 + # own_ship_vector_length = knot_2_m_pr_min(own_ship["start_pose"]["speed"]) * vector_time # Target ship target_ship["id"] = target_ship_id @@ -317,9 +319,7 @@ def find_start_position_target_ship( start_position_found = 1 elif desired_encounter_type.lower() == colreg_state2 and np.abs( beta1 - desired_beta % 360 - ) < deg_2_rad( - 0.1 - ): # noqa: E127 + ) < deg_2_rad(0.1): # noqa: E127 start_position_target_ship = {"north": n_32, "east": e_32} start_position_found = 1 else: From 67a12a7463d7c25e060a1d656f15d35e81cb72e2 Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:35:31 +0100 Subject: [PATCH 07/12] encounter.py : minor reformatting with black --- src/trafficgen/encounter.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/trafficgen/encounter.py b/src/trafficgen/encounter.py index d9bf6f2..41f33ba 100644 --- a/src/trafficgen/encounter.py +++ b/src/trafficgen/encounter.py @@ -319,7 +319,9 @@ def find_start_position_target_ship( start_position_found = 1 elif desired_encounter_type.lower() == colreg_state2 and np.abs( beta1 - desired_beta % 360 - ) < deg_2_rad(0.1): # noqa: E127 + ) < deg_2_rad( + 0.1 + ): # noqa: E127 start_position_target_ship = {"north": n_32, "east": e_32} start_position_found = 1 else: From 5154136a7a31576a59503c281a987306b0c95482 Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:35:57 +0100 Subject: [PATCH 08/12] pyproject.toml: Activated ruff category "N" rules --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index edb9f4c..df29aac 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,7 +93,7 @@ select = [ "E", "D", "F", - # "N", + "N", # "W", # "I", # "B", From e649186543d6e3cd03b42b4ff412490db9f19bc7 Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:36:47 +0100 Subject: [PATCH 09/12] pyproject.toml: Activated ruff category "W" rules --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index df29aac..4780d62 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -94,7 +94,7 @@ select = [ "D", "F", "N", - # "W", + "W", # "I", # "B", ] From 7be55359c71a21b4cfffdb358800177bc805a0c0 Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:38:27 +0100 Subject: [PATCH 10/12] pyproject.toml: Activated ruff category "I" rules (import block formatting) --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 4780d62..355b549 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -95,7 +95,7 @@ select = [ "F", "N", "W", - # "I", + "I", # "B", ] target-version = "py39" From 5468f2dbaf14f7dfdc9b1dc1c9738243a8ab6ee3 Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:38:51 +0100 Subject: [PATCH 11/12] resolved issues raised by ruff category "I" rules (import block formatting) --- tests/conftest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 874af64..6474a8e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- """Root level fixtures for `trafficgen`""" -import pytest from pathlib import Path +import pytest + @pytest.fixture def your_fixture(): From e4e98291591c94384fc0a2df3cb8d7fd2b0bf35c Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 24 Nov 2023 11:39:48 +0100 Subject: [PATCH 12/12] pyproject.toml: Activated ruff category "B" rules --- pyproject.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 355b549..9569afd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -88,7 +88,6 @@ ignore = [ # "N816", # Variable in global scope should not be mixedCase (uncomment if you want to allow mixedCase variable names in global scope) ] line-length = 105 -# Activate the following rules step by step to (step by step..) improve code quality select = [ "E", "D", @@ -96,7 +95,7 @@ select = [ "N", "W", "I", - # "B", + "B", ] target-version = "py39"