Skip to content

Conversation

@ChuanyuXue
Copy link
Owner

This pull request introduces several improvements and new features to the TSNKit project, focusing on enhanced topology support, code refactoring for maintainability, and improved command-line usability. The most significant changes include the addition of a new 2D mesh topology, refactoring of network topology generation code, updates to command-line argument parsing, and enhancements to simulation output handling.

Topology Features and Refactoring

  • Added support for a new 2D mesh topology (mesh_2d) in the network dataset specification, including validation for square mesh dimensions and border end-station placement. The list of available topologies (TOPO_FUNC) and related documentation have been updated accordingly. [1] [2] [3] [4] [5]
  • Refactored topology generation functions (line, ring, tree, mesh, and new mesh_2d) to use a shared helper function _convert_2darray_to_csv, reducing code duplication and improving maintainability. [1] [2] [3] [4] [5]

Command-Line and Usability Improvements

  • Updated command-line argument parsing in tsnkit/core/_common.py to use optional flags for all parameters, improved help descriptions, and ensured output directory creation. [1] [2] [3]
  • Improved documentation to clarify units and available options for topology and stream parameters.

Simulation Output Enhancements

  • Enhanced the simulation to log detailed flow events, including bridge and listener actions, and added an option to save simulation logs to a CSV file via a new --output flag. Output directories are created automatically if needed. [1] [2] [3] [4] [5] [6] [7] [8]

Minor Fixes and Cleanups

  • Fixed typos and improved comments for clarity in configuration and simulation modules. [1] [2]
  • Added missing imports and minor code cleanups across several files. [1] [2]

These changes collectively improve the flexibility, usability, and maintainability of the TSNKit toolkit, especially for generating and simulating complex network topologies.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request enhances the TSNKit project by adding support for a 2D mesh network topology, refactoring topology generation code for better maintainability, and improving command-line argument handling and simulation output capabilities.

Key Changes:

  • Added mesh_2d topology with validation for square mesh dimensions and border-based end-station placement
  • Refactored topology generation functions to use a shared _convert_2darray_to_csv helper function, eliminating code duplication
  • Enhanced simulation to track and export detailed flow events to CSV via a new --output flag
  • Updated command-line parsing to use optional flags (--output, --workers, --name) with improved help descriptions

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 15 comments.

Show a summary per file
File Description
tsnkit/test/debug/_common.py Updated subprocess calls to use new optional flag format for algorithm invocation; removed unused import
tsnkit/simulation/tas.py Added flow event logging with CSV export capability; fixed comment typos; updated output handling
tsnkit/data/generator.py Added automatic output directory creation; updated for mesh_2d topology support; clarified help text
tsnkit/data/dataset_spec.py Implemented new mesh_2d topology function; refactored topology generators to use shared helper; fixed typo in comment
tsnkit/core/_config.py Fixed typo in comment (casue → cause)
tsnkit/core/_common.py Converted output/workers/name to optional flags; added automatic output directory creation
doc/source/dataprep.md Added units to parameter descriptions; documented mesh_2d topology option

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if not file_name:
file_name = "log.csv"
pd.DataFrame(output,
columns=["stream", "link", "di", "time"]
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

Unclear column name and missing documentation: The output DataFrame uses a column named "di" (line 374) without any documentation explaining its meaning. Based on the code, "di" appears to be a direction indicator (0 for arrival/receive, 1 for transmission/send), but this should be documented either as a comment near the DataFrame creation or in the argument parser help text for --output.

Copilot uses AI. Check for mistakes.

result = _convert_2darray_to_csv(net, num_node, num_queue, data_rate)
result.to_csv(header + ".csv", index=False)
print(result)
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

Debug print statement should be removed: This print(result) statement appears to be leftover debug code that should not be in production.

Suggested change
print(result)

Copilot uses AI. Check for mistakes.

parser.add_argument("task", type=str, help="The file path to the stream CSV file.")
parser.add_argument("net", type=str, help="The file path to the network CSV file.")
# Switch to optional flags for all parameters
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

Misleading comment: The comment "Switch to optional flags for all parameters" is inaccurate since task and net remain as positional arguments, not optional flags. Either update the comment to reflect that only output, workers, and name are optional, or make all arguments truly optional with --task and --net flags.

Suggested change
# Switch to optional flags for all parameters
# Switch to optional flags for output, workers, and name; task and net remain positional

Copilot uses AI. Check for mistakes.
Comment on lines 159 to 165
num_node = num_sw * 2 ## TODO: |ES| != |SW| for mesh_2d
net = np.zeros(shape=(num_node, num_node))

if int(np.sqrt(num_sw)) ** 2 != num_sw:
raise ValueError("Wrong num_sw for mesh_2d, col_len != row_len")

row_len = col_len = int(np.sqrt(num_sw))
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

Incorrect number of nodes for mesh_2d topology: The code sets num_node = num_sw * 2, assuming equal numbers of switches and end-stations. However, for a 2D mesh topology, end-stations are only placed on border nodes. For a square mesh of side length n (where num_sw = n²), the number of border positions is 4n - 4 (or equivalently (n-1)*4 as used in generator.py line 66), not n². This means the actual number of nodes should be num_sw + 4*sqrt(num_sw) - 4, not num_sw * 2. This mismatch will cause the net array to be incorrectly sized and potentially lead to out-of-bounds array access or wasted memory.

Suggested change
num_node = num_sw * 2 ## TODO: |ES| != |SW| for mesh_2d
net = np.zeros(shape=(num_node, num_node))
if int(np.sqrt(num_sw)) ** 2 != num_sw:
raise ValueError("Wrong num_sw for mesh_2d, col_len != row_len")
row_len = col_len = int(np.sqrt(num_sw))
n = int(np.sqrt(num_sw))
num_node = num_sw + 4 * n - 4 # Correct number of nodes for mesh_2d topology
net = np.zeros(shape=(num_node, num_node))
if n ** 2 != num_sw:
raise ValueError("Wrong num_sw for mesh_2d, col_len != row_len")
row_len = col_len = n

Copilot uses AI. Check for mistakes.

def match_time(t: int, gcl: List[Tuple[int, int, int]]) -> int:
"""Match the entry in GCl with the given time. Implemented by binary search.
"""Match the entry in GCl with the given time. Implemented in binary search.
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

Typo in comment: "Implemented in binary search" should be "Implemented using binary search" or "Implemented with binary search" to be grammatically correct.

Suggested change
"""Match the entry in GCl with the given time. Implemented in binary search.
"""Match the entry in GCl with the given time. Implemented using binary search.

Copilot uses AI. Check for mistakes.
@@ -1,11 +1,13 @@
import argparse
from email import header
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

Unused import: from email import header should be removed as header is never used in this file.

Suggested change
from email import header

Copilot uses AI. Check for mistakes.
file_name = "log.csv"
pd.DataFrame(output,
columns=["stream", "link", "di", "time"]
).to_csv("/".join([path_name, file_name]), index=False) No newline at end of file
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

Bug in file path construction: Using "/".join([path_name, file_name]) will fail when path_name is an empty string (when output is just a filename without path). This would produce an incorrect path starting with /. Use os.path.join(path_name, file_name) or handle the empty path_name case explicitly.

Suggested change
).to_csv("/".join([path_name, file_name]), index=False)
).to_csv(os.path.join(path_name, file_name), index=False)

Copilot uses AI. Check for mistakes.
return
searched.add((x,y))

# Add es on boarder
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

Typo in comment: "boarder" should be "border".

Suggested change
# Add es on boarder
# Add es on border

Copilot uses AI. Check for mistakes.
Comment on lines 54 to 55
if parsed.output:
os.makedirs(parsed.output, exist_ok=True)
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

[nitpick] Redundant condition check: The condition if parsed.output: is unnecessary since --output has a default value of "./", which will always be truthy. This check will always evaluate to True. If the intent is to only create the directory when explicitly provided by the user, remove the default value or track whether the argument was explicitly set.

Suggested change
if parsed.output:
os.makedirs(parsed.output, exist_ok=True)
os.makedirs(parsed.output, exist_ok=True)

Copilot uses AI. Check for mistakes.
Comment on lines 25 to +50
parser.add_argument(
"workers", type=int, nargs="?", help="The number of workers.", default=1
"--output",
type=str,
default="./",
nargs="?",
help="The output folder path.",
)
parser.add_argument(
"name", type=str, nargs="?", help="The name of the experiment.", default="-"
"--workers",
type=int,
default=1,
nargs="?",
help="The number of workers.",
)
parser.add_argument(
"--name",
type=str,
default="-",
nargs="?",
help="The name of the experiment.",
)
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

[nitpick] Unnecessary nargs="?" for optional arguments with defaults: The nargs="?" is redundant for optional arguments (--output, --workers, --name) that already have default values specified. The nargs="?" is typically used when you want to distinguish between "flag not provided", "flag provided without value", and "flag provided with value". Since these are optional arguments with defaults, remove nargs="?" for cleaner argument parsing.

Copilot uses AI. Check for mistakes.
@ChuanyuXue ChuanyuXue mentioned this pull request Nov 29, 2025
@ChuanyuXue ChuanyuXue merged commit 91895cf into main Nov 29, 2025
4 checks passed
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.

1 participant