File size: 7,281 Bytes
9079f0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import json

import numpy as np
import torch

from transformers import PreTrainedTokenizer
from transformers.tokenization_utils_base import BatchEncoding


class YieldTokenizer(PreTrainedTokenizer):
    """
    Adapter tokenizer for FlexServe's built-in text-classification pipeline.

    This does NOT tokenize natural language.

    It accepts:
      - a JSON string, or
      - the yield input dictionary

    and converts it into:
      weather:     [B, 52, 6]
      soil:        [B, 66]
      crop_id:     [B]
      horizon_idx: [B]

    Normalization is intentionally NOT performed here.
    The sequence-classification model wrapper performs normalization
    using statistics stored in config.json.
    """

    vocab_files_names = {}
    model_input_names = [
        "weather",
        "soil",
        "crop_id",
        "horizon_idx",
    ]

    def __init__(
        self,
        weather_vars=None,
        soil_vars=None,
        K=52,
        eval_cutoffs=None,
        **kwargs,
    ):
        self.weather_vars = list(weather_vars or [])
        self.soil_vars = list(soil_vars or [])
        self.K = int(K)
        self.eval_cutoffs = list(
            eval_cutoffs
            or [20, 24, 28, 32, 36, 40, 44, 48, 52]
        )

        super().__init__(
            pad_token="[PAD]",
            unk_token="[UNK]",
            **kwargs,
        )

    @property
    def vocab_size(self):
        return 2

    def get_vocab(self):
        return {
            "[PAD]": 0,
            "[UNK]": 1,
        }

    def _tokenize(self, text, **kwargs):
        return ["[UNK]"]

    def _convert_token_to_id(self, token):
        return 0 if token == "[PAD]" else 1

    def _convert_id_to_token(self, index):
        return "[PAD]" if index == 0 else "[UNK]"

    def save_vocabulary(self, save_directory, filename_prefix=None):
        return ()

    def _parse_sample(self, sample):

        if isinstance(sample, str):
            sample = sample.strip()

            try:
                sample = json.loads(sample)
            except json.JSONDecodeError as exc:
                raise ValueError(
                    "Input must be a valid JSON string."
                ) from exc

            # Handle a JSON string containing another JSON string.
            if isinstance(sample, str):
                try:
                    sample = json.loads(sample)
                except json.JSONDecodeError as exc:
                    raise ValueError(
                        "Input string does not contain valid yield JSON."
                    ) from exc

        if not isinstance(sample, dict):
            raise ValueError(
                "Yield input must be a JSON object/dictionary."
            )

        crop = str(
            sample.get("crop", "corn")
        ).strip().lower()

        if crop not in ("corn", "maize"):
            raise ValueError(
                "This released model supports corn only."
            )

        if "weather" not in sample:
            raise ValueError(
                "Missing 'weather' object."
            )

        if "soil" not in sample:
            raise ValueError(
                "Missing 'soil' object."
            )

        weather_dict = sample["weather"]
        soil_dict = sample["soil"]

        # --------------------------------------------
        # Weather: [52, 6]
        # --------------------------------------------

        weather_cols = []

        for var in self.weather_vars:

            if var not in weather_dict:
                raise ValueError(
                    f"Missing weather variable '{var}'."
                )

            values = np.asarray(
                weather_dict[var],
                dtype=np.float32,
            )

            if values.ndim != 1:
                raise ValueError(
                    f"Weather '{var}' must be one-dimensional."
                )

            if len(values) != self.K:
                raise ValueError(
                    f"Weather '{var}' requires exactly "
                    f"{self.K} weekly values; received {len(values)}."
                )

            weather_cols.append(values)

        weather = np.stack(
            weather_cols,
            axis=1,
        ).astype(np.float32)

        # --------------------------------------------
        # Soil: [66]
        # --------------------------------------------

        soil = []

        for var in self.soil_vars:

            if var not in soil_dict:
                raise ValueError(
                    f"Missing soil variable '{var}'."
                )

            soil.append(
                float(soil_dict[var])
            )

        soil = np.asarray(
            soil,
            dtype=np.float32,
        )

        # --------------------------------------------
        # Cutoff
        # --------------------------------------------

        cutoff = int(
            sample.get(
                "cutoff",
                max(self.eval_cutoffs),
            )
        )

        if cutoff not in self.eval_cutoffs:
            raise ValueError(
                f"Unsupported cutoff {cutoff}. "
                f"Supported cutoffs are {self.eval_cutoffs}."
            )

        return {
            "weather": weather,
            "soil": soil,
            "crop_id": 0,
            "horizon_idx": cutoff,
        }

    def __call__(
        self,
        text=None,
        text_pair=None,
        return_tensors=None,
        **kwargs,
    ):
        # --------------------------------------------------
        # FlexServe/HF may call tokenizer(**input_dict)
        # instead of tokenizer(json_string).
        # --------------------------------------------------

        if text is None and "weather" in kwargs and "soil" in kwargs:

            sample = {
                "crop": kwargs.pop("crop", "corn"),
                "weather": kwargs.pop("weather"),
                "soil": kwargs.pop("soil"),
                "cutoff": kwargs.pop(
                    "cutoff",
                    max(self.eval_cutoffs),
                ),
            }

            samples = [sample]

        elif isinstance(text, (list, tuple)):

            samples = list(text)

        else:

            samples = [text]

        parsed = [
            self._parse_sample(sample)
            for sample in samples
        ]

        weather = torch.tensor(
            np.stack(
                [x["weather"] for x in parsed],
                axis=0,
            ),
            dtype=torch.float32,
        )

        soil = torch.tensor(
            np.stack(
                [x["soil"] for x in parsed],
                axis=0,
            ),
            dtype=torch.float32,
        )

        crop_id = torch.tensor(
            [
                x["crop_id"]
                for x in parsed
            ],
            dtype=torch.long,
        )

        horizon_idx = torch.tensor(
            [
                x["horizon_idx"]
                for x in parsed
            ],
            dtype=torch.long,
        )

        return BatchEncoding(
            {
                "weather": weather,
                "soil": soil,
                "crop_id": crop_id,
                "horizon_idx": horizon_idx,
            }
        )