File size: 2,396 Bytes
ae853c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The Deck — a shared-state equilibrium summary across Play and Lab modes.
A 'current view of everything' built from the session catalog: counts,
generations, stage/type breakdowns, trait averages, color gamut, recent crosses.
"""
from collections import defaultdict

import lab


def _mean(vals):
    vals = [v for v in vals if isinstance(v, (int, float))]
    return round(sum(vals) / len(vals), 1) if vals else 0.0


def summary(seeds):
    records = lab.germplasm_records(seeds)
    by_id = {r["germplasmDbId"]: r for r in records}

    by_stage = defaultdict(int)
    by_type = defaultdict(int)
    by_gen = defaultdict(int)
    by_family = defaultdict(int)
    for r in records:
        by_stage[r["stage"]] += 1
        by_type[r["germplasmType"]] += 1
        by_gen[r["generation"]] += 1
        by_family[r["budFamily"]] += 1

    breedable = [r for r in records if r["canAttempt"] and r["stage"] == "MATURE"]
    growing = [r for r in records if r["stage"] != "MATURE"]

    # recent crosses = newest offspring (have two real parents), best-effort order
    crosses = [r for r in records if all(r["parents"]) ]
    recent = [
        {
            "id": r["germplasmDbId"],
            "name": r["germplasmName"],
            "generation": r["generation"],
            "pedigree": r["pedigree"],
            "budColor": r["budColor"],
            "leafColor": r["leafColor"],
            "THC": r["THC"],
        }
        for r in crosses[-6:][::-1]
    ]

    return {
        "accessions": len(records),
        "byStage": dict(by_stage),
        "byType": dict(sorted(by_type.items())),
        "byGeneration": {f"F{k}": v for k, v in sorted(by_gen.items())},
        "byBudFamily": dict(sorted(by_family.items())),
        "maxGeneration": max(by_gen) if by_gen else 0,
        "breedableCount": len(breedable),
        "growingCount": len(growing),
        "traitAverages": {
            "THC": _mean([r["THC"] for r in records]),
            "CBD": _mean([r["CBD"] for r in records]),
            "Yield": _mean([r["Yield"] for r in records]),
            "GrowTime": _mean([r["GrowTime"] for r in records]),
            "Stability": _mean([r["Stability"] for r in records]),
        },
        "gamut": [r["budColor"] for r in records],
        "recentCrosses": recent,
        "breedable": [{"id": r["germplasmDbId"], "name": r["germplasmName"]} for r in breedable],
    }