Skip to content

Commit

Permalink
Simplify code with SIM
Browse files Browse the repository at this point in the history
  • Loading branch information
Daraan committed Oct 25, 2024
1 parent 2460452 commit 6f030d1
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 71 deletions.
5 changes: 1 addition & 4 deletions srunner/metrics/tools/metrics_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,7 @@ def is_vehicle_light_active(self, light, vehicle_id, frame):
"""
lights = self.get_vehicle_lights(vehicle_id, frame)

if light in lights:
return True

return False
return light in lights

# Scene lights
def get_scene_light_state(self, light_id, frame):
Expand Down
2 changes: 1 addition & 1 deletion srunner/metrics/tools/metrics_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def parse_recorder_info(self):

elif name == "use_gear_auto_box":
name = "use_gear_autobox"
value = True if elements[1] == "true" else False
value = elements[1] == "true"
setattr(physics_control, name, value)

elif "forward_gears" in name or "wheels" in name:
Expand Down
5 changes: 1 addition & 4 deletions srunner/metrics/tools/osc2_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,7 @@ def is_vehicle_light_active(self, light, vehicle_id, frame):
"""
lights = self.get_vehicle_lights(vehicle_id, frame)

if light in lights:
return True

return False
return light in lights

# Scene lights
def get_scene_light_state(self, light_id, frame):
Expand Down
2 changes: 1 addition & 1 deletion srunner/metrics/tools/osc2_trace_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def parse_recorder_info(self):

elif name == "use_gear_auto_box":
name = "use_gear_autobox"
value = True if elements[1] == "true" else False
value = elements[1] == "true"
setattr(physics_control, name, value)

elif "forward_gears" in name or "wheels" in name:
Expand Down
22 changes: 11 additions & 11 deletions srunner/osc2/osc2_parser/OpenSCENARIO2Parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12858,16 +12858,16 @@ def sIBaseExponent(self):
else localctx.sIBaseUnitName.text
)
if (
not (unitName == "kg")
and not (unitName == "m")
and not (unitName == "s")
and not (unitName == "A")
and not (unitName == "K")
and not (unitName == "mol")
and not (unitName == "cd")
and not (unitName == "factor")
and not (unitName == "offset")
and not (unitName == "rad")
unitName != "kg"
and unitName != "m"
and unitName != "s"
and unitName != "A"
and unitName != "K"
and unitName != "mol"
and unitName != "cd"
and unitName != "factor"
and unitName != "offset"
and unitName != "rad"
):
print("unit name %s is not supported" % unitName)
raise NoViableAltException(self)
Expand Down Expand Up @@ -16692,7 +16692,7 @@ def everyExpression(self):
offsetName = (
None if localctx._Identifier is None else localctx._Identifier.text
)
if not (offsetName == "offset"):
if offsetName != "offset":
print("%s must be offset" % offsetName)
raise NoViableAltException(self)

Expand Down
4 changes: 1 addition & 3 deletions srunner/osc2/osc_preprocess/import_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def get_path(self):

# Verify that it is the same file by comparing whether the paths are the same
def same_as(self, another_file):
if self.__base_path == another_file.get_path():
return True
return False
return self.__base_path == another_file.get_path()

def get_true_path(self, import_file_path):
"""
Expand Down
12 changes: 5 additions & 7 deletions srunner/osc2/osc_preprocess/pre_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ def __init__(self, current_path):

# Determine whether it is recorded
def exit(self, current, note):
for member in note:
if current.same_as(member):
return True
return False
return any(current.same_as(member) for member in note)

# Return import preprocessing results and import information
def import_process(self):
self.file = open(self.result, "w+", encoding="utf-8")
current = ImportFile(self.current_path)
self.__import_process(current)
with open(self.result, "w+", encoding="utf-8") as f:
self.file = f
current = ImportFile(self.current_path)
self.__import_process(current)
self.file.close()
return self.result, self.import_msg

Expand Down
5 changes: 1 addition & 4 deletions srunner/osc2/symbol_manager/local_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ def __init__(self, scope):

# For local scopes, only internal naming conflicts are found
def is_key_found(self, sym):
if sym.name and sym.name in self.symbols:
return True
else:
return False
return bool(sym.name and sym.name in self.symbols)

def define(self, sym, ctx):
if issubclass(type(sym), LocalScope):
Expand Down
5 changes: 1 addition & 4 deletions srunner/osc2/symbol_manager/qualifiedBehavior_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ def __init__(self, name, scope):
def is_actor_name_defined(self):
if self.actor_name is None:
return True
elif self.enclosing_scope.symbols.get(self.actor_name) is not None:
return True
else:
return False
return self.enclosing_scope.symbols.get(self.actor_name) is not None

def is_qualified_behavior_name_valid(self, ctx):
if self.actor_name == self.behavior_name and self.actor_name:
Expand Down
10 changes: 2 additions & 8 deletions srunner/osc2_dm/physical_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ def __neg__(self):
return self

def is_in_range(self, num) -> bool:
if num >= self.start and num < self.end:
return True
else:
return False
return num >= self.start and num < self.end

def gen_single_value(self):
return random.uniform(self.start, self.end)
Expand Down Expand Up @@ -82,10 +79,7 @@ def is_in_range(self, value) -> bool:
return value == self.num

def is_single_value(self) -> bool:
if isinstance(self.num, Range):
return False
else:
return True
return isinstance(self.num, Range)

def gen_single_value(self):
if self.is_single_value():
Expand Down
5 changes: 1 addition & 4 deletions srunner/scenariomanager/lights_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ def _get_night_mode(self, weather):
joined_threshold += int(cloudiness_dist < self.COMBINED_THRESHOLD)
joined_threshold += int(fog_density_dist < self.COMBINED_THRESHOLD)

if joined_threshold >= 2:
return True

return False
return joined_threshold >= 2

def _turn_close_lights_on(self, location):
"""Turns on the lights of all the objects close to the ego vehicle"""
Expand Down
21 changes: 5 additions & 16 deletions srunner/scenarios/background_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ def contains_wp(self, wp):
if not wp.is_junction:
return False
other_id = wp.get_junction().id
for junction in self.junctions:
if other_id == junction.id:
return True
return False

return any(other_id == junction.id for junction in self.junctions)

class BackgroundBehavior(AtomicBehavior):
"""
Expand Down Expand Up @@ -2208,9 +2204,8 @@ def _is_location_behind_ego(self, location):
ego_transform = self._route[self._route_index].transform
ego_heading = ego_transform.get_forward_vector()
ego_actor_vec = location - ego_transform.location
if ego_heading.dot(ego_actor_vec) < - 0.17: # 100º
return True
return False
return ego_heading.dot(ego_actor_vec) < - 0.17 # 100º


def _update_road_actors(self):
"""
Expand Down Expand Up @@ -2327,21 +2322,15 @@ def is_remapping_needed(current_wp, prev_wp):
# Some roads have starting / ending lanes in the middle. Remap if that is detected
prev_wps = get_same_dir_lanes(prev_wp)
current_wps = get_same_dir_lanes(current_wp)
if len(prev_wps) != len(current_wps):
return True

return False
return len(prev_wps) != len(current_wps)

def is_road_dict_unchanging(wp_pairs):
"""Sometimes 'monitor_topology_changes' has already done the necessary changes"""
road_dict_keys = list(self._road_dict)
if len(wp_pairs) != len(road_dict_keys):
return False

for _, new_wp in wp_pairs:
if get_lane_key(new_wp) not in road_dict_keys:
return False
return True
return all(get_lane_key(new_wp) in road_dict_keys for _, new_wp in wp_pairs)

if prev_route_index == self._route_index:
return
Expand Down
2 changes: 1 addition & 1 deletion srunner/scenarios/cut_in_with_static_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _initialize_actors(self, config):
blocker_wp = next_wps[0]

# Spawn the vehicles in front of the cut in one
for i in range(self._front_vehicles):
for _ in range(self._front_vehicles):
# Move to the side
side_wp = blocker_wp.get_left_lane() if self._direction == 'left' else blocker_wp.get_right_lane()
if not side_wp:
Expand Down
4 changes: 2 additions & 2 deletions srunner/scenarios/osc2_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,10 +1268,10 @@ def visit_method_body(self, node: ast_node.MethodBody):
exec_context = ""
module_name = None
for elem in external_list:
if "module" == elem[0]:
if elem[0] == "module":
exec_context += "import " + str(elem[1]) + "\n"
module_name = str(elem[1])
elif "name" == elem[0]:
elif elem[0] == "name":
exec_context += "ret = "
if module_name is not None:
exec_context += module_name + "." + str(elem[1]) + "("
Expand Down
2 changes: 1 addition & 1 deletion srunner/tools/scenario_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def get_distance_between_actors(current, target, distance_type="euclidianDistanc
extent_sum_x = target.bounding_box.extent.x + current.bounding_box.extent.x
extent_sum_y = target.bounding_box.extent.y + current.bounding_box.extent.y
if distance_type == "longitudinal":
if not current_wp.road_id == target_wp.road_id:
if current_wp.road_id != target_wp.road_id:
distance = 0
# Get the route
route = global_planner.trace_route(current_transform.location, target_transform.location)
Expand Down

0 comments on commit 6f030d1

Please sign in to comment.