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], }