File size: 2,988 Bytes
09da05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""CLI utility for survey simulation workflows."""

from __future__ import annotations

import argparse
import json
from pathlib import Path

from adsp.config import PROCESSED_DATA_DIR
from adsp.core.survey import GroupGenerator, ResponseSynthesizer, SurveyManager


def _load_json(path: Path) -> dict:
    return json.loads(path.read_text(encoding="utf-8"))


def main() -> None:
    parser = argparse.ArgumentParser(description="Survey simulation workflow")
    parser.add_argument("--base-dir", type=Path, default=PROCESSED_DATA_DIR / "surveys")

    parser.add_argument("--create-survey", type=Path, help="Path to survey definition JSON")
    parser.add_argument("--create-group", type=Path, help="Path to group composition JSON")
    parser.add_argument("--simulate", action="store_true", help="Run simulation")
    parser.add_argument("--survey-id", type=str, help="Survey id for simulation/export")
    parser.add_argument("--group-id", type=str, help="Group id for simulation")
    parser.add_argument("--mode", type=str, default="random", choices=["random", "llm"])
    parser.add_argument("--sampling-ratio", type=float, default=0.7)
    parser.add_argument("--seed", type=int, default=None)
    parser.add_argument("--batch-size", type=int, default=10)
    parser.add_argument("--export-format", type=str, choices=["csv", "json"], default="csv")

    args = parser.parse_args()

    surveys = SurveyManager(base_dir=args.base_dir)
    groups = GroupGenerator(base_dir=args.base_dir)
    responses = ResponseSynthesizer(base_dir=args.base_dir)

    created_survey_id = args.survey_id
    created_group_id = args.group_id

    if args.create_survey:
        survey = surveys.create_survey(_load_json(args.create_survey))
        created_survey_id = survey.survey_id
        print(f"Created survey: {survey.survey_id}")

    if args.create_group:
        payload = _load_json(args.create_group)
        composition = payload.get("composition") if isinstance(payload, dict) else payload
        group = groups.create_group(
            composition,
            mode=args.mode,
            sampling_ratio=args.sampling_ratio,
            seed=args.seed,
        )
        created_group_id = group.group_id
        print(f"Created group: {group.group_id} ({len(group.respondents)} respondents)")

    if args.simulate:
        if not created_survey_id or not created_group_id:
            raise SystemExit("--simulate requires survey_id and group_id (or creation in the same command)")

        result = responses.run_simulation(
            survey_id=created_survey_id,
            group_id=created_group_id,
            batch_size=args.batch_size,
        )
        output = responses.export_responses(
            survey_id=created_survey_id,
            simulation_id=result.simulation_id,
            format=args.export_format,
        )
        print(f"Simulation: {result.simulation_id}")
        print(f"Responses exported: {output}")


if __name__ == "__main__":
    main()