Skip to content

[Tools] Refactor common simulation-related constants and utilities - #160

Merged
tryuan99 merged 213 commits into
titan/hierarchicalfrom
titan/visualize_sim_constants
Dec 15, 2025
Merged

tryuan99 merged 213 commits into
titan/hierarchicalfrom
titan/visualize_sim_constants

Conversation

@tryuan99

Copy link
Copy Markdown
Member

This PR refactors common simulation-related constants and utilities into separate files for better reusability. In particular, all simulation-related constants, including the agent types, the event types, and the log file prefixes, are now defined in constants.py while utility functions, including finding the latest telemetry file and event log, are now defined in utils.py.

@stacklane-pr-stack-visualizer

stacklane-pr-stack-visualizer Bot commented Dec 10, 2025 •

Copy link
Copy Markdown

🧱 Stack PR · Base of stack

Stack Structure:

Comment thread Tools/utils.py Resolved
Comment thread Tools/constants.py Resolved
Comment thread Tools/utils.py Resolved
Base automatically changed from titan/run_manager to titan/hierarchical December 14, 2025 07:05

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2c304d4 and 9b19007.

📒 Files selected for processing (4)
  • Tools/constants.py (1 hunks)
  • Tools/unity_utils.py (1 hunks)
  • Tools/utils.py (1 hunks)
  • Tools/visualize_log.py (6 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
Tools/visualize_log.py (3)
Tools/constants.py (2)
  • AgentType (6-9)
  • EventType (12-17)
Tools/utils.py (4)
  • find_latest_telemetry_file (63-69)
  • find_latest_event_log (72-78)
  • read_telemetry_file (100-109)
  • read_event_log (112-124)
Tools/unity_utils.py (1)
  • get_persistent_data_directory (135-147)
🪛 Ruff (0.14.8)
Tools/unity_utils.py

127-128: Avoid specifying long messages outside the exception class

(TRY003)


127-127: f-string without any placeholders

Remove extraneous f prefix

(F541)


128-128: f-string without any placeholders

Remove extraneous f prefix

(F541)

Tools/visualize_log.py

151-151: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Test plugins on ubuntu-latest
  • GitHub Check: Test plugins on macos-latest
  • GitHub Check: Test plugins on windows-latest
🔇 Additional comments (7)
Tools/visualize_log.py (3)

18-38: LGTM - Well-structured visualization configuration.

The EventMarker dataclass and the mapping dictionaries provide clean, centralized configuration for visualization styles.


65-66: LGTM - Clean enum-based event filtering.

Using EventType enum constants for comparisons improves type safety and readability.


108-119: LGTM - Clean iteration over event markers.

The refactored plotting logic using EVENT_TYPE_TO_MARKER is clear and maintainable.

Tools/utils.py (3)

45-60: LGTM - Correct handling of empty results and modification time.

The function properly returns None when no files match and uses st_mtime for cross-platform consistency.


81-97: LGTM - Subdirectory discovery implementation is correct.

The generator is properly consumed in the list comprehension with the is_dir() filter.


100-123: LGTM - Clean data reading utilities.

The event log sanitization (uppercase + strip) ensures consistent comparison with EventType enum values.

Tools/constants.py (1)

1-24: LGTM - Clean enum definitions and constants.

The enums and constants are well-structured. Both TELEMETRY_FILE_PREFIX and EVENT_LOG_FILE_PREFIX are used in Tools/utils.py, and the enum types are used in Tools/visualize_log.py. Note that StrEnum requires Python 3.11+.

Comment thread Tools/unity_utils.py Resolved
Comment thread Tools/unity_utils.py
Comment on lines +135 to +146
def get_persistent_data_directory() -> str:
"""Returns the path to Unity's persistent data directory."""
system = platform.system()
if system == "Windows":
return os.path.expandvars(
r"%USERPROFILE%\AppData\LocalLow\BAMLAB\micromissiles\Logs")
elif system == "Darwin":
return os.path.expanduser(
"~/Library/Application Support/BAMLAB/micromissiles/Logs")
elif system == "Linux":
return os.path.expanduser("~/.config/unity3d/BAMLAB/micromissiles/Logs")
else:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

LGTM - Consider returning Path for consistency.

The platform-specific logic is correct. Minor suggestion: the rest of this file uses Path objects, but this function returns str. Consider returning Path for consistency, though this is optional.

-def get_persistent_data_directory() -> str:
+def get_persistent_data_directory() -> Path:
     """Returns the path to Unity's persistent data directory."""
     system = platform.system()
     if system == "Windows":
-        return os.path.expandvars(
-            r"%USERPROFILE%\AppData\LocalLow\BAMLAB\micromissiles\Logs")
+        return Path(os.path.expandvars(
+            r"%USERPROFILE%\AppData\LocalLow\BAMLAB\micromissiles\Logs"))
     elif system == "Darwin":
-        return os.path.expanduser(
-            "~/Library/Application Support/BAMLAB/micromissiles/Logs")
+        return Path(
+            "~/Library/Application Support/BAMLAB/micromissiles/Logs").expanduser()
     elif system == "Linux":
-        return os.path.expanduser("~/.config/unity3d/BAMLAB/micromissiles/Logs")
+        return Path("~/.config/unity3d/BAMLAB/micromissiles/Logs").expanduser()
     else:
         raise NotImplementedError(f"Unsupported platform: {system}.")
🤖 Prompt for AI Agents
In Tools/unity_utils.py around lines 135 to 146 the function
get_persistent_data_directory() currently returns a str while the rest of the
module uses Path objects; change the return type to pathlib.Path and construct
and return Path objects instead of raw strings (convert expanded/expandeduser
results with Path(...) or use Path.home()/os-specific Path joins), update the
function signature to -> Path and ensure pathlib.Path is imported at the top of
the file.

Comment thread Tools/utils.py Resolved
Comment thread Tools/visualize_log.py Resolved
Comment thread Tools/visualize_log.py Resolved

@daniellovell daniellovell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@tryuan99
tryuan99 merged commit 389327d into titan/hierarchical Dec 15, 2025
18 checks passed
@tryuan99
tryuan99 deleted the titan/visualize_sim_constants branch December 15, 2025 20:31
tryuan99 added a commit that referenced this pull request Dec 18, 2025
[All] Implement fully hierarchical defense strategy

* [Agent] Add agent-related interfaces and base implementations (#95)
* [Interceptor/Threat] Add interceptor and threat interfaces (#96)
* [Algorithm] Add algorithm interfaces and base implementations (#97)
* [Movements] Implement all aerial and ground-based movements (#99)
* [Gemini] Disable Gemini pull request summary (#142)
* [Sensor] Add sensor interface and ideal sensor implementation (#100)
* [Chore] Clean up private properties (#101)
* [Evasion] Implement evasion behaviors (#102)
* [Agent] Add agent-related interfaces and base implementations (#95)
* [Interceptor/Threat] Add interceptor and threat interfaces (#96)
* [Algorithm] Add algorithm interfaces and base implementations (#97)
* [Movements] Implement all aerial and ground-based movements (#99)
* [Sensor] Add sensor interface and ideal sensor implementation (#100)
* [Chore] Clean up private properties (#101)
* [Evasion] Implement evasion behaviors (#102)
* [Attack Behavior] Implement direct attack behavior (#103)
* [Controller] Add controller interface and implementations (#105)
* [Predictor/Planner] Clean up predictor and planner classes (#108)
* [Clustering] Change clustering algorithms to act on hierarchical objects (#111)
* [Utils] Clean up coordinates classes and tests (#112)
* [Assignment] Refactor assignment algorithms (#113)
* [Accessor] Use init accessor for read-only fields (#116)
* [Interceptors/Threats] Add Legacy prefix to existing interceptor and threat class names (#121)
* [Interceptors/Threats] Implement all interceptor and threat types (#122)
* [Proto/Configs] Add launcher types to proto definition and simulation configs (#123)
* [IADS] Clean up IADS and simulation manager (#124)
* [UI] Remove swarm status dialog and tactical panel (#125)
* [All] Remove all legacy code (#127)
* [Prefabs] Add prefabs for launchers (#128)
* [Release] Adjust minimum distance in proximity release (#129)
* [Configs] Add simulation configuration with 2 launchers (#130)
* [Simulation] Destroy terminated game objects (#132)
* [UI] Draw gizmos for agents (#133)
* [UI] Fix summary text label sizes (#134)
* [Clustering] Implement recursive clustering within hierarchy (#139)
* [UI] Clean up UI scripts (#140)
* [Reassignment] Assign new targets to excess interceptors (#141)
* [Evasion] Fix threat evasion of interceptors (#143)
* [Reassignment] Use capacity remaining to reassign interceptors (#144)
* [Agent] Add IsPursuable property (#145)
* [Reassignment] Implement threat reassignment (#146)
* [Escape] Add escape detectors (#147)
* [Escape] Check for escaping targets (#148)
* [Movement] Fix missile movement to avoid the ground (#149)
* [Reassignment] Fix relaunching multiple carrier interceptors (#150)
* [Unity] Fix serializable fields and references (#152)
* [Docs] Update docs with hierarchical strategy (#153)
* [Utils] Sample from normal distribution (#154)
* [Proto] Add run configuration proto (#155)
* [Visualization] Fix visualizing simulation monitor logs (#157)
* [Tools] Build standalone player (#158)
* [Manager] Add run manager to handle run configurations (#159)
* [Tools] Refactor common simulation-related constants and utilities (#160)
* [Tools] Add metric and aggregator classes (#161)
* [Tools] Process the aggregated logs of a simulation run (#162)
* [Docs] Update documentation with run configuration and logging (#163)
* [Tests] Remove unused Rigidbody variables from unit tests (#166)
* [Agent] Add target model API in Agent interface (#167)
* [Logging] Emit more detailed events (#168)
* [Metrics] Consider only missile interceptors in metrics (#169)
* [Logging] Remove logging from CP-SAT solver (#171)
* [Tools] Add script to animate telemetry log (#172)
* [Docs] Remove unnecessary docs image files (#174)
* [Interceptor] Rename IsPursuable to IsPursuer (#175)
* [UI] Reimplement tactical panel (#176)
* [UI] Add camera to follow and rotate around interceptors or threats (#177)
* [Configs] Add configuration for 5 swarms and 1000 UCAVs (#179)
* [Agent] Cache the agent transform (#180)
* [Agent] Clean up finding relative transformations (#181)
* [Configs] Exclude 5_swarms_1000_ucav configuration from CI (#182)
* [Agents] Fix collisions with terminated agents (#184)
* [Controls] Implement vector proportional navigation (#186)
* [Reassignment] Find subhierarchicals with at least one active target (#187)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants