Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions scripts/plotsforrq2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import os
import matplotlib.pyplot as plt

# -----------------------------
# Raw counts
# -----------------------------
data = {
"Data Collection Settings": {
"blank or not specified": 16,
"clinical": 75,
"mix": 6,
"lab": 28,
"at-home / naturalistic": 11,
"educational": 1,
},
"Existing vs. Newly Collected Datasets": {
"no data collection": 13,
"yes data collection": 107,
"not specified or blank": 17,
},
"Cross-sectional vs. Longitudinal Design": {
"not specified or blank": 16,
"mix": 1,
"single session": 110,
"longitudinal": 10,
},
"Dataset Access Availability": {
"not specified or blank": 11,
"on request": 4,
"no": 94,
"yes": 28,
},
"Open Source": {
"blank": 12,
"yes": 7,
"no": 118,
},
}

# -----------------------------
# Standardize totals
# Add missing amount to blank category
# -----------------------------
def total_count(d):
return sum(d.values())

max_total = max(total_count(d) for d in data.values())
print(f"Standardizing all plots to total N = {max_total}")

def blank_key_for_dict(d):
for k in d:
if "blank" in k.lower() or "not specified" in k.lower():
return k
return None

standardized_data = {}

for rq_name, counts in data.items():
counts = counts.copy()
current_total = total_count(counts)
diff = max_total - current_total

blank_key = blank_key_for_dict(counts)

if diff > 0:
if blank_key is not None:
counts[blank_key] += diff
else:
counts["blank or not specified"] = diff

standardized_data[rq_name] = counts

# -----------------------------
# Plotting function
# -----------------------------
def make_bar_plot(title, counts, output_dir="barplots"):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

Suggested change
def make_bar_plot(title, counts, output_dir="barplots"):
def make_bar_plot(title, counts, output_dir="output/rq2_plots"):

os.makedirs(output_dir, exist_ok=True)

labels = list(counts.keys())
values = list(counts.values())

# Size scales a bit with number of categories
fig_width = max(8, len(labels) * 1.3)
plt.figure(figsize=(fig_width, 6))
bars = plt.bar(labels, values)

plt.title(title.replace("_", " "), fontsize=13)
plt.ylabel("Count")
plt.xlabel("Category")
plt.xticks(rotation=30, ha="right")
plt.ylim(0, max(values) * 1.15)

# Add value labels above bars
for bar, val in zip(bars, values):
plt.text(
bar.get_x() + bar.get_width() / 2,
bar.get_height() + max(values) * 0.02,
str(val),
ha="center",
va="bottom",
fontsize=10,
)

plt.tight_layout()

filename = os.path.join(output_dir, f"{title}.png")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

Suggested change
filename = os.path.join(output_dir, f"{title}.png")
filename = os.path.join(output_dir, f"{title.lower().replace(' ', '_')}.png")

plt.savefig(filename, dpi=300, bbox_inches="tight")
plt.close()
print(f"Saved: {filename}")

# -----------------------------
# Create all plots
# -----------------------------
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}")
Comment on lines +114 to +122

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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}")