Spaces:
Sleeping
Sleeping
File size: 10,879 Bytes
6b83428 4727dd8 6b83428 4727dd8 6b83428 4727dd8 6b83428 4727dd8 6b83428 4727dd8 6b83428 6a04b7a 6b83428 6a04b7a 6b83428 |
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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import datetime
import logging
import os
import traceback
from typing import Dict, List
# import dash_html_components as html
# import flask
import numpy as np
import plotly.express as px
from dash import Dash, Input, Output, State # , dcc # ,html
from dash.exceptions import PreventUpdate
from flask import Flask, render_template
from libs.dashapp import layout, open_image, perform_analysis
from libs.utils import setup_logging
from libs.utils import verbose as vprint
setup_logging()
log = logging.getLogger(__name__)
CONFIG = {}
V = 1
V_IGNORE = [] # Debug, Warning, Error
MODEL_PATH = ""
FARM_NAME = ""
# ===============================================================================
# The Main App
# ===============================================================================
app = Flask(__name__)
@app.route("/")
def home():
return render_template(
"index.html"
) # "Welcome to Nowcasting App.<br><br>The dashapp is still under development..."
@app.route("/register")
def register():
return "Welcome to Nowcasting signup page.<br><br>The dashapp is still under development..."
@app.route("/addjob")
def addjob():
return "Welcome to Nowcasting signup page.<br><br>The dashapp is still under development..."
@app.route("/dashboard")
def dashboard():
return "Welcome to Nowcasting dashboard page.<br><br>This page will contain dashboard for the prediction pipelines, and allow you to manage and add new predictions.<br>The dashapp is still under development..."
# dashapp
# ===============================================================================
# Soil Moisture Comparison Tool App Layout
# ===============================================================================
# external JavaScript files
external_scripts = [
"https://www.google-analytics.com/analytics.js",
{"src": "https://cdn.polyfill.io/v2/polyfill.min.js"},
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.core.js",
"integrity": "sha256-Qqd/EfdABZUcAxjOkMi8eGEivtdTkh3b65xCZL4qAQA=",
"crossorigin": "anonymous",
},
]
# external CSS stylesheets
external_stylesheets = [
"https://codepen.io/chriddyp/pen/bWLwgP.css",
{
"href": "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css",
"rel": "stylesheet",
"integrity": "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO",
"crossorigin": "anonymous",
},
]
dashapp = Dash(
__name__,
server=app,
routes_pathname_prefix="/dashapp/",
external_scripts=external_scripts,
external_stylesheets=external_stylesheets,
title="Soil Moisture Comparison Tool",
update_title="Loading the tool...",
)
# farm_name = "Arawa"
# layer = "SM2"
time_delta = datetime.timedelta(days=20)
FAIL_IMAGE = dashapp.get_asset_url("icons/fail.png")
SUCCESS_IMAGE = dashapp.get_asset_url("icons/success.png")
WAIT_IMAGE = dashapp.get_asset_url("icons/wait.png")
current_working_directory = os.getcwd()
dashapp.index_template = os.path.join(
current_working_directory, "templates", "dashapp.html"
)
dashapp.layout = layout(WAIT_IMAGE)
def find_model_path(real_path):
for root, dirs, _ in os.walk(real_path):
# print(root,dirs,"\n\n")
if "soilwatermodel" in dirs:
real_path = os.path.join(root, "soilwatermodel")
print("real_path is", real_path)
return real_path
return real_path
# ====================================================================================================
# Callbacks
# ====================================================================================================
@dashapp.callback(
[
Output("farm-name-session", "data"),
Output("farm-image", "src"),
],
[Input("farm-name", "value"), State("farm-name-session", "data")],
)
def update_session(farm_name, session):
global MODEL_PATH
global FARM_NAME
session = farm_name
if farm_name is None or farm_name == "":
session = ""
image = WAIT_IMAGE
else:
print(f"Getting some data about farm: {farm_name}")
# if the path does not exist, do not update the session
real_path = INPUT.format(farm_name)
print(f"Checking: {real_path}")
try:
path = find_model_path(real_path)
real_path = path
except Exception:
print("Exception raised while searching for the root")
MODEL_PATH = path
if os.path.exists(real_path):
session = farm_name
FARM_NAME = farm_name
image = SUCCESS_IMAGE
else:
session = ""
image = FAIL_IMAGE
print(f"\n\nSession updated to {session}")
print(f"Image updated to {image}\n\n")
return session, image
@dashapp.callback(
Output("farm-name", "value"),
Input("farm-name-session", "modified_timestamp"),
State("farm-name-session", "data"),
)
def display_name_from_session(timestamp, name):
print(f"Updating the farm name from the session: {name}")
if timestamp is not None:
return name
else:
return ""
@dashapp.callback(
Output("visualisation-select", "options"),
# Input("farm-name", "value"),
Input("layer-dropdown", "value"),
Input("window-select", "start_date"),
Input("window-select", "end_date"),
Input("historic-dropdown", "value"),
Input("w-aggregation-dropdown", "value"),
Input("h-aggregation-dropdown", "value"),
Input("generate-button", "n_clicks"),
State("farm-name-session", "data"),
)
def get_analysis(
layer, window_start, window_end, historic_years, w_agg, h_agg, n_clicks, farm_name
) -> List[Dict[str, str]]:
"""Get the analysis files and return them as a list of dicts.
Parameters
----------
layer : str
layer to use for the analysis
window_start : str
start date of the window
window_end : str
end date of the window
historic_years : int
number of years to use for the historic data
w_agg : str
aggregation method for the window data
h_agg : str
aggregation method for the historic data
n_clicks : int
number of times the generate button has been clicked
Returns
-------
files : list
list of dicts of analysis files
"""
global MODEL_PATH
global FARM_NAME
print("\nAnalysis callback triggered")
if n_clicks == 0 or n_clicks is None:
raise PreventUpdate
# window_start = datetime.datetime.strptime(window_start, '%Y-%m-%d')
# window_end = datetime.datetime.strptime(window_end, '%Y-%m-%d')
print(f"\nPath: {MODEL_PATH}\n")
files = perform_analysis(
input=MODEL_PATH,
window_start=window_start,
window_end=window_end,
historic_years=historic_years,
layer=layer,
agg_window=w_agg,
agg_history=h_agg,
comparison="diff",
output=None,
match_raster=None,
farm_name=FARM_NAME,
)
print(MODEL_PATH)
print(
f"n_clicks: {n_clicks}\n"
+ f"window_start: {window_start}\n"
+ f"window_end: {window_end}\n"
+ f"historic_years: {historic_years}\n"
+ f"layer: {layer}\n"
+ f"agg_window: {w_agg}\n"
+ f"agg_history: {h_agg}\n"
+ "comparison: 'diff'\n"
+ f"output: {None}\n"
+ f"match_raster: {None}\n"
)
print(files)
files = {
i: [
" ".join(files[i].split("/")[-1].split(".")[0].split("-")).capitalize(),
files[i],
]
for i in files
}
print(files)
options = [{"label": files[i][0], "value": files[i][1]} for i in files]
return options
@dashapp.callback(
Output("graph", "figure"),
Input("visualisation-select", "value"),
Input("platter-dropdown", "value"),
Input("generate-button", "n_clicks"),
)
def change_colorscale(file, palette, n_clicks):
"""Display the selected visualisation and change the colorscale of the
visualisation.
Parameters
----------
file : str
path to the visualisation file
palette : str
name of the colorscale to use
Returns
-------
fig : plotly.graph_objects.Figure
plotly figure object
"""
if n_clicks == 0 or n_clicks is None or file is None:
raise PreventUpdate
band1, lons_a, lats_a = open_image(file)
# Get the second dimension of the lons
lats = lats_a[:, 0]
lons = lons_a[0, :]
if "quantile" in file:
value_type = "Percentile"
else:
value_type = "SM"
print(lons.shape, lons)
print(lats.shape, lats)
print(band1.shape, band1)
print(file)
fig = px.imshow(band1, x=lons, y=lats, color_continuous_scale=palette)
fig.update(
data=[
{
"customdata": np.stack((band1, lats_a, lons_a), axis=-1),
"hovertemplate": f"<b>{value_type}"
+ "</b>: %{customdata[0]}<br>"
+ "<b>Lat</b>: %{customdata[1]}<br>"
+ "<b>Lon</b>: %{customdata[2]}<br>"
+ "<extra></extra>",
}
]
)
print("Render successful")
return fig
# ==============================================================================
# Main
# ==============================================================================
if __name__ == "__main__":
# Load Configs
parser = argparse.ArgumentParser(
description="Download rainfall data from Google Earth Engine for a range of dates.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-i",
"--input",
help="Absolute or relative path to the netcdf data directory for each farm. Should be in this format: '/path/to/farm/{}/soilwatermodel'",
default=os.path.join(os.path.expanduser("~"), "Data/results_default/{}"),
)
parser.add_argument(
"-d",
"--debug",
help="Debug mode as True or False. Default is True.",
default=True,
)
parser.add_argument(
"-s",
"--spaces",
help="Whether running on huggingface spaces or not. Default is False.",
action=argparse.BooleanOptionalAction,
)
args = parser.parse_args()
INPUT = args.input
try:
# dashapp.run_server(debug=args.debug)
if args.spaces:
app.run(debug=args.debug, host="0.0.0.0", port=7860)
else:
app.run(debug=args.debug)
except Exception as e:
vprint(
0,
V,
V_IGNORE,
Error="Failed to execute the main function:",
ErrorMessage=e,
)
traceback.print_exc()
raise e
|