plots for rq2#2
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds a new script, scripts/plotsforrq2.py, for generating standardized bar plots from research data. The review feedback recommends several improvements: updating the default output directory for consistency with the project structure, sanitizing generated filenames by converting them to lowercase and replacing spaces with underscores, and wrapping the script's execution logic in a main block to adhere to Python best practices.
| # ----------------------------- | ||
| # Plotting function | ||
| # ----------------------------- | ||
| def make_bar_plot(title, counts, output_dir="barplots"): |
There was a problem hiding this comment.
The project structure defined in the README.md indicates that generated results and figures should be stored in the output/ directory. Updating the default output_dir to a subdirectory within output/ ensures consistency with the repository's organization.
| def make_bar_plot(title, counts, output_dir="barplots"): | |
| def make_bar_plot(title, counts, output_dir="output/rq2_plots"): |
|
|
||
| plt.tight_layout() | ||
|
|
||
| filename = os.path.join(output_dir, f"{title}.png") |
There was a problem hiding this comment.
Using titles with spaces and mixed casing directly as filenames can lead to issues in some environments or when using command-line tools. It is recommended to sanitize the filename by converting it to lowercase and replacing spaces with underscores.
| filename = os.path.join(output_dir, f"{title}.png") | |
| filename = os.path.join(output_dir, f"{title.lower().replace(' ', '_')}.png") |
| for rq_name, counts in standardized_data.items(): | ||
| make_bar_plot(rq_name, counts) | ||
|
|
||
| # ----------------------------- | ||
| # Print standardized totals | ||
| # ----------------------------- | ||
| print("\nStandardized totals:") | ||
| for rq_name, counts in standardized_data.items(): | ||
| print(f"{rq_name}: total = {sum(counts.values())}, counts = {counts}") No newline at end of file |
There was a problem hiding this comment.
It is a best practice in Python to wrap the script's execution logic within an if __name__ == "__main__": block. This prevents the code from running automatically if the script is imported as a module elsewhere.
| for rq_name, counts in standardized_data.items(): | |
| make_bar_plot(rq_name, counts) | |
| # ----------------------------- | |
| # Print standardized totals | |
| # ----------------------------- | |
| print("\nStandardized totals:") | |
| for rq_name, counts in standardized_data.items(): | |
| print(f"{rq_name}: total = {sum(counts.values())}, counts = {counts}") | |
| if __name__ == "__main__": | |
| for rq_name, counts in standardized_data.items(): | |
| make_bar_plot(rq_name, counts) | |
| # ----------------------------- | |
| # Print standardized totals | |
| # ----------------------------- | |
| print("\\nStandardized totals:") | |
| for rq_name, counts in standardized_data.items(): | |
| print(f"{rq_name}: total = {sum(counts.values())}, counts = {counts}") |
added plots for rq2