HarvestForTwo / docs /prereg /make_prereg.py
princessamygdala's picture
Claude Opus 5
Prereg: separate task sections, the design decisions, and five figures
33d6500
Raw History Blame Contribute Delete
4.32 kB
"""Build the BEAR SONA pilot preregistration.
Kept as a generator with the prose in prereg_text.py, the same pattern as
docs/methods/make_methods.py, so the document rebuilds from the repository and
the writing diffs cleanly. The .docx is output and is gitignored.
python docs/prereg/make_prereg.py [outdir]
Section order follows OSF's template, matching the lab's PSL LD4
preregistration, so each field can be pasted into the OSF form.
"""
import os
import sys
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Inches, Pt, RGBColor
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from prereg_text import SECTIONS, TITLE # noqa: E402
BLACK = RGBColor(0, 0, 0)
RED = RGBColor(0xC0, 0x39, 0x2B)
#: OSF's own section banners, which take a heading; everything else is a field.
BANNERS = {"Metadata", "Study Information", "Design Plan", "Sampling Plan",
"Variables", "Analysis Plan"}
def style(doc):
n = doc.styles["Normal"]
n.font.name = "Arial"
n.font.size = Pt(11)
n.font.color.rgb = BLACK
n.paragraph_format.space_after = Pt(6)
n.paragraph_format.line_spacing = 1.15
def para(doc, text, bold=False, size=11, colour=BLACK, before=0, after=6):
p = doc.add_paragraph()
p.paragraph_format.space_before = Pt(before)
p.paragraph_format.space_after = Pt(after)
r = p.add_run(text)
r.bold = bold
r.font.size = Pt(size)
r.font.color.rgb = colour
r.font.name = "Arial"
return p
def body(doc, text):
"""One paragraph per blank-line-separated block. A TO CONFIRM block is
coloured, because it is a question for the researcher and must not be
mistaken for a decision already made."""
for block in [b.strip() for b in text.strip().split("\n\n") if b.strip()]:
flat = " ".join(block.split())
para(doc, flat, colour=RED if flat.startswith("TO CONFIRM") else BLACK)
#: Where each figure is dropped in, keyed by the section it follows.
FIGURES_AFTER = {
"Design: prosocial effort task (Harvest for Two)": [
("fig_pe_design.png", "Figure 1. The effort task's offers, and the "
"choices the model predicts."),
],
"Design: prosocial learning task (Treasure Hunt for Two)": [
("fig_sl_outcomes.png", "Figure 5. The learning task's outcome "
"structure, per chest kind, per 16-trial house."),
],
"Computational models": [
("fig_pe_param_recovery.png", "Figure 2. Parameter recovery, 48 "
"trials, 100 simulated participants."),
("fig_pe_model_recovery.png", "Figure 3. Model recovery, 48 trials."),
("fig_pe_trials.png", "Figure 4. Recovery against trial count."),
],
}
def figure(doc, name, caption, width=6.3):
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), name)
if not os.path.exists(path):
para(doc, f"[missing figure: {name}]", colour=RED)
return
p = doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.add_run().add_picture(path, width=Inches(width))
cap = para(doc, caption, size=9, colour=RGBColor(0x33, 0x33, 0x33), after=12)
cap.alignment = WD_ALIGN_PARAGRAPH.CENTER
def build(outdir):
doc = Document()
style(doc)
para(doc, "OSF Preregistration", bold=True, size=16, after=2)
para(doc, TITLE, size=12, after=2)
para(doc, "Adapted from the lab's PSL LD4 preregistration. Section order "
"follows OSF's template so each field can be pasted across. "
"Passages in red are decisions the researcher still has to make.",
size=9, colour=RGBColor(0x55, 0x55, 0x55), after=14)
for heading, text in SECTIONS:
if text is None:
para(doc, heading, bold=True, size=14, before=16, after=4)
continue
para(doc, heading, bold=True, before=10, after=2)
body(doc, text)
for name, caption in FIGURES_AFTER.get(heading, []):
figure(doc, name, caption)
os.makedirs(outdir, exist_ok=True)
out = os.path.join(outdir, "OSF Preregistration_ BEAR SONA Pilot.docx")
doc.save(out)
return out
if __name__ == "__main__":
here = os.path.dirname(os.path.abspath(__file__))
targets = sys.argv[1:] or [here]
for t in targets:
print("wrote", build(t))