Skip to content

Instantly share code, notes, and snippets.

@romer8
Last active May 17, 2021 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romer8/2ea44f0a9536e4cdad8ed7242ca27933 to your computer and use it in GitHub Desktop.
Save romer8/2ea44f0a9536e4cdad8ed7242ca27933 to your computer and use it in GitHub Desktop.
pywaterML.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "pywaterML.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/romer8/2ea44f0a9536e4cdad8ed7242ca27933/pywaterml.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dZ3F0VlUjN8y"
},
"source": [
"\n",
"<h1>\n",
"<img src=\"https://drive.google.com/uc?export=view&id=1ku3-a7IomE0e1VjJkjLSQ2TG-8i_pu7j\"> <b> PYWATERML</b></h1>\n",
"\n",
"\n",
"<br>\n",
"<br>\n",
"\n",
"\n",
"This package tries to be an extension to retrieve data from any WaterOneFlow Web Services (WOF) that supports waterML format. It was developed in the Hydroinformatics Lab at Brigham Young University.\n",
"\n",
"## Contents\n",
"1. [Introduction](#intro)\n",
"2. [Install Pywaterml and Additional Dependencies](#install)\n",
"3. [Pywaterml Methods](#methods)\n",
"> * [Add WOF Web Service](#addService)\n",
" * [GetSites Method](#GetSites)\n",
" * [GetSitesByBoxObject Method](#GetSitesByBoxObject)\n",
" * [GetVariables Method](#GetVariables)\n",
" * [GetSitesByVariable Method](#GetSitesByVariable)\n",
" * [GetSiteInfo Method](#GetSiteInfo)\n",
" * [GetValues Method](#GetValues)\n",
" * [GetInterpolation](#GetInterpolation)\n",
" * [GetMonthlyAverage Method](#GetMonthlyAverage)\n",
" * [GetClustersMonthlyAvg Method](#GetClustersMonthlyAvg)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iCZsmAefqm-D"
},
"source": [
"<a name=\"intro\"></a>\n",
"## Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Nb29VUt4jUdt"
},
"source": [
"The purpose of the package is to retrieve data from any customized datasets using ‘WaterOneFlow’ web services and read any ‘WaterML’ time series data response. The pywaterml python package permits users to execute WaterOneFlow web services methods and receive responses in JSON format as depicted and outlined here: \n",
"\n",
"* The pywaterml package sets up the connection with any WaterOneFlow web service using lightweight SOAP-based web service client for Python from third-party libraries. \n",
"\n",
"* Data from five different WaterOneFlow web services methods can be retrieved on the established connection. \n",
"\n",
"* Responses from the different methods are used for data discovery, download, and visualization in the client component. \n",
"\n",
"\n",
"\n",
"The following WaterOneFlow Web Services can be used to test the pywaterml, and other WOF services as well..\n",
"\n",
"* [Calvin](http://hydroportal.cuahsi.org/CALVIN_HHS/cuahsi_1_1.asmx?WSDL)\n",
"* [Para la naturaleza](http://hydroportal.cuahsi.org/para_la_naturaleza/cuahsi_1_1.asmx?WSDL)\n",
"\n",
"For more WOF services one can use the 97 different services from the CUAHSI HIS central:\n",
"\n",
"* [HIS CENTRAL](https://hiscentral.cuahsi.org/pub_services.aspx)\n",
"\n",
"Additional information can be found in the different links belows:\n",
"\n",
"* [Documentation](https://pywaterml.readthedocs.io/en/latest)\n",
"* [Pypi Package](https://pypi.org/project/pywaterml/)\n",
"* [Conda Package](https://anaconda.org/conda-forge/pywaterml)\n",
"* [Source Code](https://github.com/romer8/pywaterml)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GdsIhCI1kW42"
},
"source": [
"<a name=\"install\"></a>\n",
"## Install Pywaterml and Additional Dependencies"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Hj3hDJpZ4iyk"
},
"source": [
"The following lines of code install the pywaterml from pip and the folium and plotly libraries."
]
},
{
"cell_type": "code",
"metadata": {
"id": "MtWfYP3TgfR_"
},
"source": [
"!pip install pywaterml\n",
"!pip install folium\n",
"!pip install plotly"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "axKiaVU1pjQe"
},
"source": [
"import pywaterml.waterML as pwml\n",
"import folium\n",
"import plotly.graph_objects as go\n",
"import plotly.express as px\n",
"import pandas as pd\n",
"import warnings\n",
"warnings.filterwarnings('ignore')\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "CuVBZCaxscmR"
},
"source": [
"<a name=\"methods\"></a>\n",
"## Pywaterml Methods "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mzil4MKGlBrP"
},
"source": [
"The pywaterml python package permits users to execute WaterOneFlow web services methods and receive responses in JSON format as depicted in the Figure below and outlined here: \n",
"\n",
" 1. The pywaterml package sets up the connection with any WaterOneFlow web service using lightweight SOAP-based web service client for Python from third-party libraries. \n",
"\n",
" 2. Data from five different WaterOneFlow web services methods can be retrieved on the established connection. \n",
"\n",
" 3. Responses from the different methods are used for data discovery, download, and visualization in the client component. \n",
"\n",
"The five different WaterOneFlow web services methods provided by pywaterml are the following: \n",
"\n",
"1. GetSites\n",
"\n",
"2. GetSitesByBoxObject\n",
"\n",
"3. GetVariables\n",
"\n",
"4. GetSiteInfo\n",
"\n",
"5. GetValues \n",
"\n",
"In addition, pywaterml offers customized methods to discover data such as GetSitesByVariable, which retrieves specific sites according to an ontology based variable array. Customized methods are used in the data discovery, download, and visualization functionalities of the WDE. For example, the GetSitesByVariable method is used in the ontology-based data discovery at the server level. Finally, the pywaterml package offers other methods that do not make calls to WaterOneFlow web services , but allows to analyze the data from the WaterOneFlow methods mentioned above. The three main methods that provides the analysis of the regular WaterOneFlow methods are the following:\n",
"\n",
"1. GetInterpolation\n",
"\n",
"2. GetMonthlyAverages\n",
"\n",
"3. GetClusterMonthlyAvg\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0YN3fO0dsmFC"
},
"source": [
"<a name=\"addService\"></a>\n",
"### Add WOF Web Service"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rqGI2HnKslup"
},
"source": [
"Any WOF web service can be added to the wrapper class WaterMLOperations from the pywaterml package. In the link below, please change the <em>hs_url</em>,\n",
"<em>site_index</em>, and <em>variable_index</em> variable below.\n",
"\n",
"```\n",
"hs_url = \"YOUR WOF GOES HERE\"\n",
"site_index = \"THE CHOSEN SITE GOES HERE\"\n",
"variable_index = \"THE CHOSEN VARIABLE GOES HERE\"\n",
"```\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "hfZV_E02lEfs"
},
"source": [
"hs_url = \"http://hydroportal.cuahsi.org/CALVIN_HHS/cuahsi_1_1.asmx?WSDL\"\n",
"site_index = 0\n",
"variable_index = 0\n",
"try:\n",
" water = pwml.WaterMLOperations(url = hs_url)\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "z8skY4N5tq7G"
},
"source": [
"<a name=\"GetSites\"></a>\n",
"\n",
"### GetSites Method"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "chshR9dqt-JU"
},
"source": [
"The GetSites method retrieves meta data for each site in the WOF web service. Data can be retrieved in three different formats: JSON, CSV, and WaterML"
]
},
{
"cell_type": "code",
"metadata": {
"id": "SZNMaxpwrc2D"
},
"source": [
"try:\n",
" sites = water.GetSites()\n",
" df = pd.DataFrame.from_dict(sites)\n",
" print(df)\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "wGdgtYByuvb0"
},
"source": [
"The following code is a helpful code snipped to plot the different stations in a follium map"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ld6QSYA2j_Wk"
},
"source": [
"try:\n",
" m = folium.Map(location=[40.0150, -105.2705], \n",
" tiles = 'Stamen Terrain')\n",
" sitesLocations = []\n",
" for site in sites:\n",
" sitesLocations.append([float(site['latitude']) , float(site['longitude'])])\n",
" folium.Marker(\n",
" location= [float(site['latitude']) , float(site['longitude'])],\n",
" icon=folium.Icon()\n",
" ).add_to(m)\n",
" m.fit_bounds(sitesLocations)\n",
" m\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "HZ4vWfkLwonY"
},
"source": [
"<a name=\"GetSitesByBoxObject\"></a>\n",
"\n",
"### GetSitesByBoxObject Method"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6rORLbDkwvj7"
},
"source": [
"The GetSitesByBoxObject method retrieves meta data for each site from a given lat/lon bounding box in the WOF web service. Data can be retrieved in three different formats: JSON, CSV, and WaterML"
]
},
{
"cell_type": "code",
"metadata": {
"id": "bgdyHVK7m_wY"
},
"source": [
"try:\n",
" Bounds = m.get_bounds()\n",
" BoundsRearranged = [Bounds[0][1],Bounds[0][0],Bounds[1][1],Bounds[1][0]]\n",
" SitesByBoundingBox = water.GetSitesByBoxObject(BoundsRearranged,'epsg:4326')\n",
" df = pd.DataFrame.from_dict(SitesByBoundingBox)\n",
" print(df)\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "0UKt9Ty7xQfP"
},
"source": [
"<a name=\"GetVariables\"></a>\n",
"### GetVariables Method"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Z-Ntb2bWxVR-"
},
"source": [
"The GetVariables method retrieves metadata from the different variables in the WOF web service."
]
},
{
"cell_type": "code",
"metadata": {
"id": "v3DOPKYXsJ7i"
},
"source": [
"try:\n",
" variables = water.GetVariables()\n",
" df = pd.DataFrame.from_dict(variables['variables'])\n",
"\n",
" print(df)\n",
"except Exception as e:\n",
" print(e)\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "gBCT98tRxqrV"
},
"source": [
"<a name=\"GetSitesByVariable\"></a>\n",
"### GetSitesByVariable Method"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WLV1XOUmx72F"
},
"source": [
"The GetSitesByVariable methods retrieves metadata from the sites that contains certain variables in the WOF web service\n",
"\n",
"<em>Please note that the varible used in this example is the first one. If you want to change the variable, you need to change the site_index variable.</em>\n",
"\n",
"```\n",
"variablesTest = [variables['variables'][site_index]['variableCode']]\n",
"```\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "k__x7rdG_sgm"
},
"source": [
"try:\n",
" variablesTest = [variables['variables'][site_index]['variableCode']]\n",
" sitesFiltered = water.GetSitesByVariable(variablesTest,sites)\n",
" df = pd.DataFrame.from_dict(sitesFiltered['sites'])\n",
" print(df)\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "MoeHbfokzByc"
},
"source": [
"<a name=\"GetSiteInfo\"></a>\n",
"### GetSiteInfo Method"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JeVHdm8gzTx1"
},
"source": [
"Retrieves the information of a given site. GetSiteInfo() function is similar to the GetSiteInfo() WaterML function.\n",
"\n",
"<em>Please note that the site used in this example is the first one. If you want to change the site, you need to change the site_index variable.</em>\n",
"\n",
"```\n",
"site_full_code = sites[site_index]['fullSiteCode']\n",
"```"
]
},
{
"cell_type": "code",
"metadata": {
"id": "v4J-ZB9esVlp"
},
"source": [
"try:\n",
" site_full_code = sites[site_index]['fullSiteCode']\n",
" siteInfo = water.GetSiteInfo(site_full_code)\n",
" df = pd.DataFrame.from_dict(siteInfo['siteInfo'])\n",
" print(df)\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "UyYmqDND2pDj"
},
"source": [
"<a name=\"GetValues\"></a>\n",
"### GetValues Method"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "l3I2khI92hFu"
},
"source": [
"Retrieves the values for an specific variable in a site. GetValues() function is similar to the GetValues() WaterML function.\n",
"\n",
"<em>Please note that the variable used in this example is the first one. If you want to change the variable, you need to change the variable_index variable</em>\n",
"\n",
"```\n",
"variable_full_code = siteInfo['siteInfo'][variable_index]['fullVariableCode']\n",
"start_date = siteInfo['siteInfo'][variable_index]['beginDateTime'].split('T')[0]\n",
"end_date = siteInfo['siteInfo'][variable_index]['endDateTime'].split('T')[0]\n",
"```"
]
},
{
"cell_type": "code",
"metadata": {
"id": "I4mlk7O1mn_s"
},
"source": [
"try:\n",
" variable_full_code = siteInfo['siteInfo'][variable_index]['fullVariableCode']\n",
" start_date = siteInfo['siteInfo'][variable_index]['beginDateTime'].split('T')[0]\n",
" end_date = siteInfo['siteInfo'][variable_index]['endDateTime'].split('T')[0]\n",
" variableResponse= water.GetValues(site_full_code, variable_full_code, start_date, end_date)\n",
" df = pd.DataFrame.from_dict(variableResponse['values'])\n",
" print(df)\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "yETx0fg33IoJ"
},
"source": [
"The following is a code snipped for plotting the results of the GetValues function using the Plotly library. If you want to change the variable to the selected one, you need to change the variable variable_index:\n",
"\n",
"\n",
"\n",
"```\n",
"fig.update_layout(title = variableResponse['values'][variable_index]['variableName'],\n",
" xaxis_title =variableResponse['values'][variable_index]['timeUnitAbbreviation'],\n",
" yaxis_title = variableResponse['values'][variable_index]['unitAbbreviation'])\n",
"```\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "diZKW5aB5qQv"
},
"source": [
"##PLOT THE TIME SERIES\n",
"try:\n",
" timeStamps=[]\n",
" valuesTimeSeries = []\n",
" for index in variableResponse['values']:\n",
" timeStamps.append(index['dateTimeUTC'])\n",
" valuesTimeSeries.append(index['dataValue'])\n",
"\n",
" fig = go.Figure(data=go.Scatter(x=timeStamps, y=valuesTimeSeries))\n",
" # Edit the layout\n",
" fig.update_layout(title = variableResponse['values'][variable_index]['variableName'],\n",
" xaxis_title =variableResponse['values'][variable_index]['timeUnitAbbreviation'],\n",
" yaxis_title = variableResponse['values'][variable_index]['unitAbbreviation'])\n",
" fig.show()\n",
"\n",
" df = pd.DataFrame(dict(\n",
" data=valuesTimeSeries\n",
" ))\n",
" fig = px.box(df,y=\"data\", points=\"all\")\n",
" fig.show()\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "7tayhz1L4ShT"
},
"source": [
"<a name=\"GetInterpolation\"></a>\n",
"### GetInterpolation Method"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "V0xGv07qmeAP"
},
"source": [
"Interpolates the data given by the GetValues function in order to fix datasets with missing values. Three ooptions for interpolation are offered: mean, backward, forward. The default is the mean interpolation"
]
},
{
"cell_type": "code",
"metadata": {
"id": "jW9hBijwoqUn"
},
"source": [
"try:\n",
" mean_interpolation = water.GetInterpolation(variableResponse,'mean')\n",
" backward_interpolation = water.GetInterpolation(variableResponse,'backward')\n",
" forward_interpolation = water.GetInterpolation(variableResponse,'forward')\n",
" df_mean = pd.DataFrame.from_dict(mean_interpolation)\n",
" df_back = pd.DataFrame.from_dict(backward_interpolation)\n",
" df_ford = pd.DataFrame.from_dict(forward_interpolation)\n",
"\n",
" print(\"INTERPOLATIONS\")\n",
" # print(df_back)\n",
" print(df_mean)\n",
" # print(df_ford) \n",
" \n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "cXUBcAuomsNP"
},
"source": [
"The following is a code snipped for plotting the results of the GetInterpolation method using the Plotly library."
]
},
{
"cell_type": "code",
"metadata": {
"id": "DOxyWtwyo0sl"
},
"source": [
"try:\n",
" ##PLOT THE TIME SERIES FOR INTERPOLATIONS\n",
" timeStamps=[]\n",
" valuesTimeSeries = []\n",
" timeStampsBack=[]\n",
" valuesTimeSeriesBackward = []\n",
" timeStampsFor=[]\n",
" valuesTimeSeriesForward = []\n",
"\n",
" for index in mean_interpolation:\n",
" timeStamps.append(index[0])\n",
" valuesTimeSeries.append(index[1])\n",
"\n",
" for index in backward_interpolation:\n",
" timeStampsBack.append(index[0])\n",
" valuesTimeSeriesBackward.append(index[1])\n",
"\n",
" for index in forward_interpolation:\n",
" timeStampsFor.append(index[0])\n",
" valuesTimeSeriesForward.append(index[1])\n",
"\n",
" fig = go.Figure(data=go.Scatter(x=timeStamps, y=valuesTimeSeries, mode='lines',\n",
" name='Mean Interpolation'))\n",
" fig.add_trace(go.Scatter(x=timeStampsBack, y=valuesTimeSeriesBackward,\n",
" mode='lines',\n",
" name='Backward Interpolation'))\n",
" fig.add_trace(go.Scatter(x=timeStampsFor, y=valuesTimeSeriesForward,\n",
" mode='lines',\n",
" name='Forward Interpolation'))\n",
"\n",
" # Edit the layout\n",
" fig.update_layout(title = variableResponse['values'][variable_index]['variableName'],\n",
" xaxis_title =variableResponse['values'][variable_index]['timeUnitAbbreviation'],\n",
" yaxis_title = variableResponse['values'][variable_index]['unitAbbreviation'])\n",
" fig.show()\n",
" ##ONLY MEAN INTERPOLATION WHISKER AND BOX PLOT\n",
"\n",
" df = pd.DataFrame(dict(\n",
" data=valuesTimeSeries\n",
" ))\n",
" fig = px.box(df,y=\"data\", points=\"all\")\n",
" fig.show()\n",
"\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "VP69FShLm4wE"
},
"source": [
"<a name=\"GetMonthlyAverage\"></a>\n",
"### GetMonthlyAverage Method"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BkGKWVn9m4ik"
},
"source": [
"Gets the monthly averages for a given variable, or from the response given by the GetValues function for a given site."
]
},
{
"cell_type": "code",
"metadata": {
"id": "3hu3bUo-uGtO"
},
"source": [
"##CALCULATING THE MONTHLY AVERAGES\n",
"try:\n",
" m_avg = water.GetMonthlyAverage(None, site_full_code, variable_full_code, start_date, end_date)\n",
"\n",
" data = {'Months': ['Jan', 'Feb', 'Mar', 'Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],\n",
" 'Monthly Average': m_avg,\n",
" }\n",
" df = pd.DataFrame (data, columns = ['Months','Monthly Average'])\n",
"\n",
" print (df)\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "a6iV1Zl5oE45"
},
"source": [
"<a name=\"GetClustersMonthlyAvg\"></a>\n",
"\n",
"### GetClustersMonthlyAvg Method"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nlBxm9wroHy9"
},
"source": [
"Gets “n” number of clusters using dtw time series interpolation for a given variable."
]
},
{
"cell_type": "code",
"metadata": {
"id": "KVT75Uk1_OfC"
},
"source": [
"## Calculating Clustering\n",
"try:\n",
" Clusters = water.GetClustersMonthlyAvg(sites,siteInfo['siteInfo'][variable_index]['variableCode'])\n",
" print(Clusters)\n",
"except:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "zc_NJkEzoNgi"
},
"source": [
"The following is extra code to create a list of colors and plot the different clusters in a follium map."
]
},
{
"cell_type": "code",
"metadata": {
"id": "GbDmMvBPrRD7"
},
"source": [
"## Function to select the color for the cluster.\n",
"def colorToCluster(cluster):\n",
" colors = [\n",
" 'red',\n",
" 'blue',\n",
" 'green',\n",
" 'pink',\n",
" 'purple',\n",
" 'orange',\n",
" ]\n",
" return colors[cluster]\n",
"try:\n",
" ## Looking in the Map the different clusters\n",
" m = folium.Map(location=[40.0150, -105.2705], \n",
" tiles = 'Stamen Terrain')\n",
" sitesLocations = []\n",
" for site,singlecluster in zip(sitesFiltered['sites'],Clusters):\n",
" sitesLocations.append([float(site['latitude']) , float(site['longitude'])])\n",
" folium.Marker(\n",
" location= [float(site['latitude']) , float(site['longitude'])],\n",
" # popup= sites[\"name\"], # pop-up label for the marker\n",
" icon=folium.Icon(color=colorToCluster(singlecluster[1]))\n",
" ).add_to(m)\n",
" # Display m\n",
" m.fit_bounds(sitesLocations)\n",
" m\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "kFTcLsWcoZXR"
},
"source": [
"The following is extra code to plot the time series for the different clusters in the WOF web service"
]
},
{
"cell_type": "code",
"metadata": {
"id": "inFVIaDnNipE"
},
"source": [
"try:\n",
" m_arr = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\",\"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
" fig = go.Figure(data=go.Scatter(x = m_arr, y = Clusters[0][0], mode='lines',\n",
" name='1'))\n",
" for index in range (1, len(Clusters)):\n",
" if Clusters[index][1] == 0:\n",
" fig.add_trace(go.Scatter(x=m_arr, y=Clusters[index][0],\n",
" mode='lines',\n",
" name=str(index)))\n",
"\n",
"\n",
" # Edit the layout\n",
" fig.update_layout(title = \"Graph for First Cluster\",\n",
" xaxis_title ='Months',\n",
" yaxis_title = variableResponse['values'][variable_index]['unitAbbreviation'])\n",
" fig.show()\n",
" fig = go.Figure(data=go.Scatter(x = m_arr, y = Clusters[0][0], mode='lines',\n",
" name='1'))\n",
" for index in range (1, len(Clusters)):\n",
" if Clusters[index][1] == 1:\n",
" fig.add_trace(go.Scatter(x=m_arr, y=Clusters[index][0],\n",
" mode='lines',\n",
" name=str(index)))\n",
"\n",
"\n",
" # Edit the layout\n",
" fig.update_layout(title = \"Graph for Second Cluster\",\n",
" xaxis_title ='Months',\n",
" yaxis_title =variableResponse['values'][variable_index]['unitAbbreviation'])\n",
" fig.show()\n",
" fig = go.Figure(data=go.Scatter(x = m_arr, y = Clusters[0][0], mode='lines',\n",
" name='1'))\n",
" for index in range (1, len(Clusters)):\n",
" if Clusters[index][1] == 2:\n",
" fig.add_trace(go.Scatter(x=m_arr, y=Clusters[index][0],\n",
" mode='lines',\n",
" name=str(index)))\n",
"\n",
"\n",
" # Edit the layout\n",
" fig.update_layout(title = \"Graph for Third Cluster\",\n",
" xaxis_title ='Months',\n",
" yaxis_title = variableResponse['values'][variable_index]['unitAbbreviation'])\n",
" fig.show()\n",
"\n",
"except Exception as e:\n",
" print(e)"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment