File size: 4,236 Bytes
4191737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from __future__ import annotations

from pathlib import Path

import numpy as np
import pandas as pd
import torch
from torch.utils.data import Dataset

N_WEEKS = 22


def week_after_planting(date, planting_date) -> int:
    return (pd.Timestamp(date) - pd.Timestamp(planting_date)).days // 7


def images_to_weeks(dates, feats, planting_date, n_weeks: int = N_WEEKS):
    """Place image features on the weekly grid.

    Each week gets the latest image taken at or before it (forward-fill); weeks
    before the first image are zeros with mask 0. img_age is the number of weeks
    since that image was taken (0 = new image this week; 0 where mask is 0).
    Images before planting or after week n_weeks-1 are dropped.
    Returns image [n_weeks, D], img_mask [n_weeks], img_age [n_weeks], n_used.
    """
    D = feats.shape[1] if len(feats) else 0
    image = np.zeros((n_weeks, D), dtype=np.float32)
    img_mask = np.zeros(n_weeks, dtype=np.float32)
    img_age = np.zeros(n_weeks, dtype=np.float32)

    per_week = {}
    for date, i in sorted(zip(pd.to_datetime(list(dates)), range(len(dates)))):
        w = week_after_planting(date, planting_date)
        if 0 <= w < n_weeks:
            per_week[w] = i  

    last = last_week = None
    for w in range(n_weeks):
        if w in per_week:
            last, last_week = per_week[w], w
        if last is not None:
            image[w] = feats[last]
            img_mask[w] = 1.0
            img_age[w] = w - last_week
    return image, img_mask, img_age, len(per_week)


def load_npz(path: str | Path) -> dict:
    z = np.load(path, allow_pickle=False)
    return {
        "text": z["text"],
        "image": z["image"],
        "img_mask": z["img_mask"],
        "img_age": z["img_age"],
        "yield": z["yield_"],
        "farm_field": z["farm_field"],
        "plot": z["plot"],
        "planting_date": z["planting_date"],
        "row_key": z["row_key"],
    }


def field_split(data: dict, test_field: str | None = None, seed: int = 1234, val_frac: float = 0.5):
    """Hold out one farm_field (default: the last one alphabetically) and split its
    plots into val / test; all other fields are used for training.
    Returns train_idx, val_idx, test_idx, train_fields, test_field."""
    fields = data["farm_field"]
    all_fields = sorted(set(fields.tolist()))
    test_field = test_field or all_fields[-1]
    train_fields = [f for f in all_fields if f != test_field]
    train_idx = np.where(np.isin(fields, train_fields))[0]
    held = np.where(fields == test_field)[0]
    assert len(train_idx) and len(held), f"fields in data: {sorted(set(fields))}"
    rng = np.random.default_rng(seed)
    held = rng.permutation(held)
    n_val = int(round(len(held) * val_frac))
    return train_idx, np.sort(held[:n_val]), np.sort(held[n_val:]), train_fields, test_field


def compute_norm_stats(data: dict, idx: np.ndarray, eps: float = 1e-6):
    
    t = data["text"][idx].reshape(-1, data["text"].shape[-1])
    m = data["img_mask"][idx].reshape(-1) > 0
    im = data["image"][idx].reshape(-1, data["image"].shape[-1])[m]
    y = data["yield"][idx]
    return {
        "text_mean": t.mean(0).tolist(),
        "text_std": (t.std(0) + eps).tolist(),
        "image_mean": im.mean(0).tolist(),
        "image_std": (im.std(0) + eps).tolist(),
        "y_mean": float(y.mean()),
        "y_std": float(y.std()),
    }


class MultimodalYieldDataset(Dataset):
    

    def __init__(self, data: dict, idx: np.ndarray):
        self.idx = np.asarray(idx)
        self.text = torch.from_numpy(data["text"][self.idx])
        self.image = torch.from_numpy(data["image"][self.idx])
        self.img_mask = torch.from_numpy(data["img_mask"][self.idx])
        self.img_age = torch.from_numpy(data["img_age"][self.idx])
        self.y = torch.from_numpy(data["yield"][self.idx])
        self.meta = {k: data[k][self.idx] for k in ["farm_field", "plot", "planting_date", "row_key"]}

    def __len__(self):
        return len(self.idx)

    def __getitem__(self, i):
        return {
            "text": self.text[i],
            "image": self.image[i],
            "img_mask": self.img_mask[i],
            "img_age": self.img_age[i],
            "yield": self.y[i],
        }