s

Downloading and running the project



Downloading and running the project

  • How to run the project notebook, importing Python libraries and retrieving the dataset
  • How to run the web application
  • Instructions for using Docker container.

How to download the project repository

Go to the URL for the repository at https://github.com/angela1C/Mlasproject and click the green Code button. Follow instructions to clone to your local machine.

How to run the code

Python 3 was used to develop this project and is needed to run the code in the notebook. Python 3 can be downloaded from the official Python website at https://www.python.org/downloads/. It can also be downloaded using the Anaconda Python distribution at https://www.anaconda.com/distribution/.

The Jupyter Notebook wind_project.ipynb can be viewed directly in this GitHub repository in a browser without Python 3 being installed. On occasion the Jupyter Notebook may not render correctly in which case the URL https://github.com/angela1C/MLASProject/blob/main/wind_project.ipynb can be copied and pasted in to the Jupyter nbviewer at https://nbviewer.jupyter.org or by clicking this link. This will render a static version of the notebook in the browser. If the repository is downloaded it can be run locally by navigating to the folder and entering the command jupyter lab or jupyter notebook on the command line. This will open Jupyter in the browser. The wind_project.ipynb notebook can be opened then within the Jupyter session. Once opened you can can select Restart and Run All from the Kernel menu.

The following Python packages were used for this project:

These packages can be installed if necessary on the command line using pip install <package-name>. Most of them come with the Anaconda distribution of Python 3.


Importing Python libraries in the Jupyter notebook

# first importing the following libraries
import numpy as np
# pandas dataframes
import pandas as pd  
# plotting library
import matplotlib.pyplot as plt 
# another plotting library
import seaborn as sns
# splitting dataset into training and testing sets
import sklearn
print(sklearn.__version__)
from sklearn.model_selection import train_test_split
# for polynomial regression 
from sklearn.preprocessing import PolynomialFeatures
# for linear regression 
from sklearn.linear_model import LinearRegression
from sklearn import linear_model
# for evaluation metrics
from sklearn.metrics import mean_squared_error, r2_score
# neural networks
import tensorflow.keras as kr
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
# save a model in scikit-learn by using Python’s built-in persistence model pickle:
import pickle
# In the specific case of scikit-learn, it may be better to use joblib’s replacement of pickle
import joblib
from joblib import dump, load
print (joblib.__version__)
0.23.2
0.16.0

Importing the dataset into the Jupyter notebook

The dataset is available from Github.[1]

# save link to data and reference the link 
csv_url = 'https://raw.githubusercontent.com/ianmcloughlin/2020A-machstat-project/master/dataset/powerproduction.csv'
# read the csv data into a pandas dataframe
df = pd.read_csv(csv_url)
df
# write the dataframe to csv
df.to_csv('df.csv')
# make a copy of the dataframe
dfx = df.copy()

Running the web application application.py

The web application for this project is included in this repository. The server program is a Flask application called application.py. Instructions on how to use it are included in the README. A docker file is included which can be used to create a docker container image containing everything required to run the application. However to do so you need to have Docker installed. An alternate way of running the web service on a local machine is by running the flask app on your local host. Instructions are provided in the README.

The Docker file included in the project repository contains the instructions to build a docker image. Here is the text of the docker file.

FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

ENV FLASK_APP=application.py

CMD flask run --host=0.0.0.0

The following instructions can be used to get the web server up and running:

  • Clone the repository to your local machine

  • Check if docker is installed on your machine using docker --version.
    If not you will need to install docker. Note that this may take some time. Go to https://www.docker.com

  • docker build -t windpower-app . This will build a docker image in your repository in the current folder (.) The -t flag assigns a tag name to the application, otherwise the container id is used.

  • docker run -d -p 5000:5000 windpower-app Once this is running you can access the app on your browser at the local host.

  • docker image ls will list the images in the container

  • docker container ls will list the container ids (if you need to kill the container)

  • docker kill <container-id> to kill the container

  • docker rm <container-id> to remove a container.

Note that Tensorflow may cause some problems when using docker. If you do not have docker installed you can alternatively run the Flask application program on your local machine using the local host. First create a virtual environment and install the packages required into the virtual environment using the requirements.txt file. Then navigate to the local host on the browser to run the application where you can interact with the html page. The webpage index.html is saved in the static folder.

To run Flask application on Linux / Mac

  • python -m venv venv to create a virtual environment.
  • source venv/bin/activate to activate the virtual environment
  • pip install -r requirements.txt to install the required packages into the virtual environment.
  • export FLASK_APP=application to set the name of the server program as an environmental variable
  • export FLASK_ENV=development optionally to set the development environment for debugging mode.
  • flask run to run the server program application.py
  • This will start the application on http://127.0.0.1:5000/. Copy the link into your browser to access the web service program.
  • deactivate to leave the virtual environment and go back to using the system wide environment.

To run Flask application on Windows

Replace:

  • source venv/bin/activate with .\venv\Scripts\activate.bat
  • export FLASK_APP=application with set FLASK_APP=application

Using the Web service application:

  • Enter a wind speed value between 0 and 25 representing wind speeds in metres per second.

  • Click the button.

  • The page will respond by outputting the predictions for wind turbine power output based on the machine learning models trained on the dataset.

  • The predictions give a range for the minimum and maximum power values that can be expected for the wind speed value entered.

  • Note that for wind speeds above 24.4 metres per second, the turbines are switched for safety reasons.


Additional files

  • The project repository on Github also includes a .dockerignore file as well as the Dockerfile.
  • The requirements.txt file contains the Python packages required to run the project in a virtual environment.
  • The models folder contains the machine learning models developed in the Jupyter notebook and used in the web application.

Running screenshot

Tech used:
  • JavaScript
  • CSS
  • HTML