Major Restructure: redo nested dictionary code with better functions
[notebooks.git] / fmda / fmda_rnn_train_and_save.ipynb
blob773b2ef28cc64fa6a84f03882c6865e5b87ef3c3
2  "cells": [
3   {
4    "cell_type": "markdown",
5    "id": "83b774b3-ef55-480a-b999-506676e49145",
6    "metadata": {},
7    "source": [
8     "# v2.1 run RNN with Spatial Training\n",
9     "\n",
10     "This notebook is intended to set up a test where the RNN is run serial by location and compared to the spatial training scheme. Additionally, the ODE model with the augmented KF will be run as a comparison, but note that the RNN models will be predicting entirely without knowledge of the heldout locations, while the augmented KF will be run directly on the test locations.\n"
11    ]
12   },
13   {
14    "cell_type": "markdown",
15    "id": "bbd84d61-a9cd-47b4-b538-4986fb10b98d",
16    "metadata": {},
17    "source": [
18     "## Environment Setup"
19    ]
20   },
21   {
22    "cell_type": "code",
23    "execution_count": null,
24    "id": "83cc1dc4-3dcb-4325-9263-58101a3dc378",
25    "metadata": {},
26    "outputs": [],
27    "source": [
28     "import numpy as np\n",
29     "from utils import print_dict_summary, print_first, str2time, logging_setup\n",
30     "import pickle\n",
31     "import logging\n",
32     "import os.path as osp\n",
33     "import tensorflow as tf\n",
34     "from moisture_rnn_pkl import pkl2train\n",
35     "from moisture_rnn import RNNParams, RNNData, RNN \n",
36     "from utils import hash2, read_yml, read_pkl, retrieve_url, Dict\n",
37     "from moisture_rnn import RNN\n",
38     "import reproducibility\n",
39     "from data_funcs import rmse, to_json, combine_nested, subset_by_features, build_train_dict\n",
40     "from moisture_models import run_augmented_kf\n",
41     "import copy\n",
42     "import pandas as pd\n",
43     "import matplotlib.pyplot as plt\n",
44     "import yaml\n",
45     "import time"
46    ]
47   },
48   {
49    "cell_type": "code",
50    "execution_count": null,
51    "id": "17db9b90-a931-4674-a447-5b8ffbcdc86a",
52    "metadata": {},
53    "outputs": [],
54    "source": [
55     "logging_setup()"
56    ]
57   },
58   {
59    "cell_type": "code",
60    "execution_count": null,
61    "id": "35319c1c-7849-4b8c-8262-f5aa6656e0c7",
62    "metadata": {},
63    "outputs": [],
64    "source": [
65     "retrieve_url(\n",
66     "    url = \"https://demo.openwfm.org/web/data/fmda/dicts/fmda_rocky_202403-05_f05.pkl\", \n",
67     "    dest_path = \"data/fmda_rocky_202403-05_f05.pkl\")"
68    ]
69   },
70   {
71    "cell_type": "code",
72    "execution_count": null,
73    "id": "eabdbd9c-07d9-4bae-9851-cca79f321895",
74    "metadata": {},
75    "outputs": [],
76    "source": [
77     "file_paths = ['data/fmda_rocky_202403-05_f05.pkl']"
78    ]
79   },
80   {
81    "cell_type": "code",
82    "execution_count": null,
83    "id": "dcca6185-e799-4dd1-8acb-87ad33c411d7",
84    "metadata": {},
85    "outputs": [],
86    "source": [
87     "# read/write control\n",
88     "train_file='data/train.pkl'\n",
89     "train_create=True   # if false, read\n",
90     "train_write=False\n",
91     "train_read=False"
92    ]
93   },
94   {
95    "cell_type": "code",
96    "execution_count": null,
97    "id": "604388de-11ab-45c3-9f0d-80bdff0cca60",
98    "metadata": {},
99    "outputs": [],
100    "source": [
101     "# Params used for data filtering\n",
102     "params_data = read_yml(\"params_data.yaml\") \n",
103     "params_data"
104    ]
105   },
106   {
107    "cell_type": "code",
108    "execution_count": null,
109    "id": "211a1c2f-ba8d-40b8-b29c-daa38af97a26",
110    "metadata": {},
111    "outputs": [],
112    "source": [
113     "# Params used for setting up RNN\n",
114     "params = read_yml(\"params.yaml\", subkey='rnn') \n",
115     "params"
116    ]
117   },
118   {
119    "cell_type": "code",
120    "execution_count": null,
121    "id": "38e6bc61-e123-4cc9-bdee-54b051bbb352",
122    "metadata": {},
123    "outputs": [],
124    "source": [
125     "feats = ['Ed', 'Ew', 'solar', 'wind', 'elev', 'lon', 'lat', 'rain']\n",
126     "params.update({'features_list': feats})"
127    ]
128   },
129   {
130    "cell_type": "code",
131    "execution_count": null,
132    "id": "ef84104f-9898-4cd9-be54-7c480536ee0e",
133    "metadata": {
134     "scrolled": true
135    },
136    "outputs": [],
137    "source": [
138     "train = build_train_dict(file_paths, atm_source=\"RAWS\", params_data = params_data,\n",
139     "                         features_subset = feats, spatial=True, verbose=True)"
140    ]
141   },
142   {
143    "cell_type": "code",
144    "execution_count": null,
145    "id": "bc0a775b-b587-42ef-8576-e36dc0be3a75",
146    "metadata": {
147     "scrolled": true
148    },
149    "outputs": [],
150    "source": [
151     "# if train_create:\n",
152     "#     params_data.update({'hours': 1440})\n",
153     "#     logging.info('creating the training cases from files %s',file_paths)\n",
154     "#     # osp.join works on windows too, joins paths using \\ or /\n",
155     "#     train = process_train_dict(file_paths, atm_dict = \"RAWS\", params_data = params_data, verbose=True)\n",
156     "#     train = subset_by_features(train, feats)\n",
157     "#     train = combine_nested(train)\n",
158     "# if train_write:\n",
159     "#     with open(train_file, 'wb') as file:\n",
160     "#         logging.info('Writing the rain cases into file %s',train_file)\n",
161     "#         pickle.dump(train, file)\n",
162     "# if train_read:\n",
163     "#     logging.info('Reading the train cases from file %s',train_file)\n",
164     "#     train = read_pkl(train_file)"
165    ]
166   },
167   {
168    "cell_type": "markdown",
169    "id": "a24d76fc-6c25-43e7-99df-3cd5dbf84fc3",
170    "metadata": {},
171    "source": [
172     "## Spatial Data Training\n",
173     "\n",
174     "This method combines the training timeseries data into a single 3-d array, with timeseries at the same location arranged appropriately in the right order for a given `batch_size` hyperparameter. The hidden state of the recurrent layers are set up reset when the location changes. "
175    ]
176   },
177   {
178    "cell_type": "code",
179    "execution_count": null,
180    "id": "36823193-b93c-421e-b699-8c1ae5719309",
181    "metadata": {},
182    "outputs": [],
183    "source": [
184     "reproducibility.set_seed(123)"
185    ]
186   },
187   {
188    "cell_type": "code",
189    "execution_count": null,
190    "id": "66f40c9f-c1c2-4b12-bf14-2ada8c26113d",
191    "metadata": {},
192    "outputs": [],
193    "source": [
194     "params = RNNParams(params)\n",
195     "params.update({'epochs': 200, \n",
196     "               'learning_rate': 0.001,\n",
197     "               'activation': ['relu', 'relu'], # Activation for RNN Layers, Dense layers respectively.\n",
198     "               'recurrent_layers': 1, 'recurrent_units': 30, \n",
199     "               'dense_layers': 1, 'dense_units': 30,\n",
200     "               'early_stopping_patience': 30, # how many epochs of no validation accuracy gain to wait before stopping\n",
201     "               'batch_schedule_type': 'exp', # Hidden state batch reset schedule\n",
202     "               'bmin': 20, # Lower bound of hidden state batch reset, \n",
203     "               'bmax': params_data['hours'], # Upper bound of hidden state batch reset, using max hours\n",
204     "               'batch_size': 60\n",
205     "              })"
206    ]
207   },
208   {
209    "cell_type": "code",
210    "execution_count": null,
211    "id": "82bc407d-9d26-41e3-8b58-ab3f7238e105",
212    "metadata": {},
213    "outputs": [],
214    "source": [
215     "import importlib\n",
216     "import moisture_rnn\n",
217     "importlib.reload(moisture_rnn)\n",
218     "from moisture_rnn import RNNData"
219    ]
220   },
221   {
222    "cell_type": "code",
223    "execution_count": null,
224    "id": "c0c7f5fb-4c33-45f8-9a2e-38c9ab1cd4e3",
225    "metadata": {},
226    "outputs": [],
227    "source": [
228     "rnn_dat_sp = RNNData(\n",
229     "    train, # input dictionary\n",
230     "    scaler=\"standard\",  # data scaling type\n",
231     "    features_list = params['features_list'] # features for predicting outcome\n",
232     ")\n",
233     "\n",
234     "\n",
235     "rnn_dat_sp.train_test_split(   \n",
236     "    time_fracs = [.8, .1, .1], # Percent of total time steps used for train/val/test\n",
237     "    space_fracs = [.8, .1, .1] # Percent of total timeseries used for train/val/test\n",
238     ")\n",
239     "rnn_dat_sp.scale_data()\n",
240     "\n",
241     "rnn_dat_sp.batch_reshape(\n",
242     "    timesteps = params['timesteps'], # Timesteps aka sequence length for RNN input data. \n",
243     "    batch_size = params['batch_size'] # Number of samples of length timesteps for a single round of grad. descent\n",
244     ")\n",
245     "# Update Params specific to spatial training\n",
246     "params.update({\n",
247     "    'loc_batch_reset': rnn_dat_sp.n_seqs # Used to reset hidden state when location changes for a given batch\n",
248     "})"
249    ]
250   },
251   {
252    "cell_type": "code",
253    "execution_count": null,
254    "id": "4bc11474-fed8-47f2-b9cf-dfdda0d3d3b2",
255    "metadata": {},
256    "outputs": [],
257    "source": [
258     "rnn_sp = RNN(params)\n",
259     "m_sp, errs = rnn_sp.run_model(rnn_dat_sp)"
260    ]
261   },
262   {
263    "cell_type": "code",
264    "execution_count": null,
265    "id": "704ad662-d81a-488d-be3d-e90bf775a5b8",
266    "metadata": {},
267    "outputs": [],
268    "source": [
269     "errs.mean()"
270    ]
271   },
272   {
273    "cell_type": "markdown",
274    "id": "62c1b049-304e-4c90-b1d2-b9b96b9a202f",
275    "metadata": {},
276    "source": [
277     "## Save Model"
278    ]
279   },
280   {
281    "cell_type": "code",
282    "execution_count": null,
283    "id": "f333521f-c724-40bf-8c1c-32735aea52cc",
284    "metadata": {},
285    "outputs": [],
286    "source": [
287     "outpath = \"data/outputs/models\"\n",
288     "filename = osp.join(outpath, f\"model_predict_raws_rocky.keras\")\n",
289     "rnn_sp.model_predict.save(filename)"
290    ]
291   },
292   {
293    "cell_type": "markdown",
294    "id": "bc1c601f-23a9-41b0-b921-47f1340f2a47",
295    "metadata": {},
296    "source": [
297     "## Load and Check"
298    ]
299   },
300   {
301    "cell_type": "code",
302    "execution_count": null,
303    "id": "3c27b3c1-6f60-450e-82ea-18eaf012fece",
304    "metadata": {},
305    "outputs": [],
306    "source": [
307     "mod = tf.keras.models.load_model(filename)"
308    ]
309   },
310   {
311    "cell_type": "code",
312    "execution_count": null,
313    "id": "25bf5420-d681-40ec-9eb8-aed784ca4e5a",
314    "metadata": {},
315    "outputs": [],
316    "source": [
317     "from utils import hash_weights\n",
318     "\n",
319     "hash_weights(mod)"
320    ]
321   },
322   {
323    "cell_type": "code",
324    "execution_count": null,
325    "id": "d773b2ab-18de-4b13-a243-b6353c57f192",
326    "metadata": {},
327    "outputs": [],
328    "source": [
329     "type(rnn_dat_sp.X_test)"
330    ]
331   },
332   {
333    "cell_type": "code",
334    "execution_count": null,
335    "id": "253ba437-c3a2-452b-b8e6-078aa17c8408",
336    "metadata": {},
337    "outputs": [],
338    "source": [
339     "X_test = np.stack(rnn_dat_sp.X_test, axis=0)\n",
340     "y_array = np.stack(rnn_dat_sp.y_test, axis=0)"
341    ]
342   },
343   {
344    "cell_type": "code",
345    "execution_count": null,
346    "id": "f4332dd8-57cd-4f5b-a864-dc72f96d72b2",
347    "metadata": {},
348    "outputs": [],
349    "source": [
350     "preds = mod.predict(X_test)\n",
351     "preds.shape"
352    ]
353   },
354   {
355    "cell_type": "code",
356    "execution_count": null,
357    "id": "4e4cd809-6701-4bd7-b4fe-37c5e35d8999",
358    "metadata": {},
359    "outputs": [],
360    "source": [
361     "np.mean(np.sqrt(np.mean(np.square(preds - y_array), axis=(1,2))))"
362    ]
363   },
364   {
365    "cell_type": "code",
366    "execution_count": null,
367    "id": "4f4d80cb-edef-4720-b335-4af5a04992c3",
368    "metadata": {},
369    "outputs": [],
370    "source": []
371   },
372   {
373    "cell_type": "code",
374    "execution_count": null,
375    "id": "e9d7f913-b391-4e14-9b64-46a0a9786f4a",
376    "metadata": {},
377    "outputs": [],
378    "source": []
379   }
380  ],
381  "metadata": {
382   "kernelspec": {
383    "display_name": "Python 3 (ipykernel)",
384    "language": "python",
385    "name": "python3"
386   },
387   "language_info": {
388    "codemirror_mode": {
389     "name": "ipython",
390     "version": 3
391    },
392    "file_extension": ".py",
393    "mimetype": "text/x-python",
394    "name": "python",
395    "nbconvert_exporter": "python",
396    "pygments_lexer": "ipython3",
397    "version": "3.12.5"
398   }
399  },
400  "nbformat": 4,
401  "nbformat_minor": 5