predictor module

This module contains functions for the recurrent neural network that predicts future water levels

predictor.build_model(lookback)

Function that builds the recurrent neural network, which has 1 lstm layer and 2 dense layers.

Parameters:lookback (int) – The look back value, which determines the input shape.
Returns:Untrained model.
Return type:Keras model
predictor.data_prep(data, lookback, exclude=0)

Function that prepares the dataset by constructing x,y pairs. Each y is determined on the previous <lookback> data points (x).

Parameters:
  • data (array) – The water level data.
  • lookback (int) – The look back value, i.e. every y is determined how many x.
  • exclude (int, optional) – The number of latest data points to ignore (default 0).
Returns:

x. array: y.

Return type:

array

predictor.fetch_levels(station_name, dt, return_date=False)

Function that returns measurements and dates of a specified station since a specified number of days ago.

Parameters:
  • station_name (str) – The name of the station.
  • dt (int) – The number of days.
  • return_date (bool, optional) – Whether to return the dates (default False)
Returns:

  • If return_date False
    • array - Water levels.
  • If return_date True
    • list - List of dates (datetime object).
    • array - Water levels.

predictor.predict(station_name, dataset_size=1000, lookback=2000, iteration=100, display=300, use_pretrained=True, batch_size=256, epoch=20)

Function that predict a specified number of future water levels of a specific station.

If the model for that station is not cached, it will be trained according to the parameters specified.

The returned data includes actual data over the specified interval, demonstration data the model produced based on actual data points prior to the displayed actual data, and the predicted date using all the available actual data.

Parameters:
  • station_name (str) – The name of the station.
  • dataset_size (int, optional) – The number of days in the dataset (default: 1000).
  • lookback (int, optional) – Look back value (default: 2000).
  • iteration (int, optional) – Number of future water levels to be predicted (effectively the number of times data in passed to the nn) (default: 100).
  • display (int, optional) – Number of real data points to be returned (default: 300).
  • use_pretrained (bool, optional) – Whether to used pretrained model if possible (default: True).
  • batch_size (int, optional) – (default: 256).
  • epoch (int, optional) – (default: 20).
Returns:

  • 2-tuple (list, list)

    List of datetime objects of actual and demo data, list of datatime objects of future predicted data.

  • 3-tuple (list, list, list)

    Lists of water levels of actual data, demo data, predicted data.

Return type:

tuple

predictor.train_all(stations, dataset_size=1000, lookback=2000, batch_size=256, epoch=20)

Function that trains models for all station supplied.

Parameters:
  • stations (list) – List of MonitoringStation objects.
  • dataset_size (int, optional) – The number of days in the dataset (default: 1000).
  • lookback (int, optional) – Look back value (default: 2000).
  • batch_size (int, optional) – (default: 256).
  • epoch (int, optional) – (default: 20).
predictor.train_model(model, x, y, batch_size, epoch, save_file='./cache/predictor_model.hdf5', show_loss=False)

Function that trains and saves the Keras model.

Parameters:
  • model (Keras model) – The built model.
  • x (list) –
  • y (list) –
  • batch_size (int) – Batch size.
  • epoch (int) – Number of epochs.
  • save_file (str, optional) – Path to save the trained model file (default: ‘./cache/predictor_model.hdf5’)
  • show_loss (bool, optional) – Whether to display the loss-epoch graph after training.
Returns:

The trained model.

Return type:

Keras model