Skip to content

Commit 6182395

Browse files
author
Xing Han Lu
authored
Merge pull request #574 from plotly/add-floris-gch
Add Floris demos Former-commit-id: 21e7d05
2 parents 8aa94d5 + a6fe06f commit 6182395

26 files changed

Lines changed: 1474 additions & 0 deletions

apps/dash-floris-flow/.gitignore

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Source: https://github.com/github/gitignore/blob/master/Python.gitignore
2+
.vscode
3+
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
9+
# C extensions
10+
*.so
11+
12+
# Distribution / packaging
13+
.Python
14+
build/
15+
develop-eggs/
16+
dist/
17+
downloads/
18+
eggs/
19+
.eggs/
20+
lib/
21+
lib64/
22+
parts/
23+
sdist/
24+
var/
25+
wheels/
26+
share/python-wheels/
27+
*.egg-info/
28+
.installed.cfg
29+
*.egg
30+
MANIFEST
31+
32+
# PyInstaller
33+
# Usually these files are written by a python script from a template
34+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
35+
*.manifest
36+
*.spec
37+
38+
# Installer logs
39+
pip-log.txt
40+
pip-delete-this-directory.txt
41+
42+
# Unit test / coverage reports
43+
htmlcov/
44+
.tox/
45+
.nox/
46+
.coverage
47+
.coverage.*
48+
.cache
49+
nosetests.xml
50+
coverage.xml
51+
*.cover
52+
*.py,cover
53+
.hypothesis/
54+
.pytest_cache/
55+
cover/
56+
57+
# Translations
58+
*.mo
59+
*.pot
60+
61+
# Django stuff:
62+
*.log
63+
local_settings.py
64+
db.sqlite3
65+
db.sqlite3-journal
66+
67+
# Flask stuff:
68+
instance/
69+
.webassets-cache
70+
71+
# Scrapy stuff:
72+
.scrapy
73+
74+
# Sphinx documentation
75+
docs/_build/
76+
77+
# PyBuilder
78+
.pybuilder/
79+
target/
80+
81+
# Jupyter Notebook
82+
.ipynb_checkpoints
83+
84+
# IPython
85+
profile_default/
86+
ipython_config.py
87+
88+
# pyenv
89+
# For a library or package, you might want to ignore these files since the code is
90+
# intended to run in multiple environments; otherwise, check them in:
91+
# .python-version
92+
93+
# pipenv
94+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
95+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
96+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
97+
# install all needed dependencies.
98+
#Pipfile.lock
99+
100+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
101+
__pypackages__/
102+
103+
# Celery stuff
104+
celerybeat-schedule
105+
celerybeat.pid
106+
107+
# SageMath parsed files
108+
*.sage.py
109+
110+
# Environments
111+
.env
112+
.venv
113+
env/
114+
venv/
115+
ENV/
116+
env.bak/
117+
venv.bak/
118+
119+
# Spyder project settings
120+
.spyderproject
121+
.spyproject
122+
123+
# Rope project settings
124+
.ropeproject
125+
126+
# mkdocs documentation
127+
/site
128+
129+
# mypy
130+
.mypy_cache/
131+
.dmypy.json
132+
dmypy.json
133+
134+
# Pyre type checker
135+
.pyre/
136+
137+
# pytype static type analyzer
138+
.pytype/
139+
140+
# Cython debug symbols
141+
cython_debug/

apps/dash-floris-flow/Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn app:server

apps/dash-floris-flow/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Flow Visualization with FLORIS and VTK
2+
3+
* [Demo](https://dash-gallery.plotly.host/dash-floris-flow)
4+
* [FLORIS Repo](https://github.com/NREL/floris)
5+
* [Dash VTK](https://github.com/plotly/dash-vtk)
6+
7+
![image demo](./demo.jpg)
8+
9+
## Downloading and running a single app
10+
11+
Visit the [releases page](https://github.com/plotly/dash-sample-apps/releases) and download and `unzip` the app you want. Then `cd` into the app directory and install its dependencies in a virtual environment in the following way:
12+
13+
```bash
14+
python -m venv venv
15+
source venv/bin/activate # Windows: \venv\scripts\activate
16+
pip install -r requirements.txt
17+
```
18+
19+
then run the app:
20+
```bash
21+
python app.py
22+
```
23+
24+
## Contact
25+
26+
Interested in building or deploying apps like this? [Reach out](https://plotly.com/contact-us/) or [get a demo](https://plotly.com/get-demo).

apps/dash-floris-flow/app.py

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
# Simpler gist here: https://gist.github.com/xhlulu/773fe238773ea69c8bc2b26560ab67d7
2+
import random
3+
4+
import dash
5+
import dash_bootstrap_components as dbc
6+
import dash_vtk
7+
import dash_core_components as dcc
8+
import dash_html_components as html
9+
from dash.dependencies import Input, Output, State
10+
import plotly.express as px
11+
import floris.tools as wfct
12+
import numpy as np
13+
14+
15+
def Header(name, app):
16+
title = html.H1(name)
17+
logo = html.Img(
18+
src=app.get_asset_url("dash-logo.png"), style={"float": "right", "height": 60}
19+
)
20+
link = html.A(logo, href="https://plotly.com/dash/")
21+
22+
return dbc.Row([dbc.Col(title, md=8), dbc.Col(link, md=4)], align="center")
23+
24+
25+
def CustomSlider(data: np.array, label: str, id: str):
26+
n_unique = np.unique(data).shape[0]
27+
28+
return dbc.FormGroup(
29+
[
30+
dbc.Label(label),
31+
dcc.Slider(min=0, max=n_unique, value=n_unique / 2, step=1, id=id,),
32+
]
33+
)
34+
35+
36+
def build_view_child(
37+
dimensions, spacing, origin, field, enabled, i, j, k, window, level
38+
):
39+
slice_prop = {"colorWindow": window, "colorLevel": level}
40+
child = [
41+
dash_vtk.ShareDataSet(
42+
dash_vtk.ImageData(
43+
dimensions=dimensions,
44+
spacing=spacing,
45+
origin=origin,
46+
children=dash_vtk.PointData(
47+
dash_vtk.DataArray(registration="setScalars", values=field)
48+
),
49+
),
50+
),
51+
]
52+
53+
if "Volume" in enabled:
54+
child.append(
55+
dash_vtk.VolumeRepresentation(
56+
[dash_vtk.VolumeController(), dash_vtk.ShareDataSet()],
57+
)
58+
)
59+
if "i" in enabled:
60+
child.append(
61+
dash_vtk.SliceRepresentation(
62+
iSlice=int(round(i)),
63+
property=slice_prop,
64+
children=dash_vtk.ShareDataSet(),
65+
)
66+
)
67+
68+
if "j" in enabled:
69+
child.append(
70+
dash_vtk.SliceRepresentation(
71+
jSlice=int(round(j)),
72+
property=slice_prop,
73+
children=dash_vtk.ShareDataSet(),
74+
)
75+
)
76+
77+
if "k" in enabled:
78+
child.append(
79+
dash_vtk.SliceRepresentation(
80+
kSlice=int(round(k)),
81+
property=slice_prop,
82+
children=dash_vtk.ShareDataSet(),
83+
)
84+
)
85+
86+
return child
87+
88+
89+
# Initialize the FLORIS interface fi
90+
fi = wfct.floris_interface.FlorisInterface("./data/example_input.json")
91+
fd = fi.get_flow_data()
92+
93+
# compute dimensions, origins and spacing based on flow data
94+
origin = [axis.mean().round().astype(int) for axis in [fd.x, fd.y, fd.z]]
95+
ranges = np.array([axis.ptp().round().astype(int) for axis in [fd.x, fd.y, fd.z]])
96+
dimensions = np.array([np.unique(axis).shape[0] for axis in [fd.x, fd.y, fd.z]])
97+
x, y, z = dimensions
98+
spacing = np.round(ranges / dimensions).astype(int)
99+
100+
# Create the volumes
101+
views = dict(u=fd.u, v=fd.v, w=fd.w)
102+
103+
104+
controls = [
105+
dbc.FormGroup(
106+
[
107+
dbc.Label("Dimension"),
108+
dbc.RadioItems(
109+
options=[{"label": x, "value": x} for x in ["u", "v", "w"]],
110+
value="u",
111+
id="radio-dimension",
112+
inline=True,
113+
),
114+
]
115+
),
116+
dbc.FormGroup(
117+
[
118+
dbc.Label("Enabled"),
119+
dbc.Checklist(
120+
options=[
121+
{"label": x, "value": x.replace("Slice ", "")}
122+
for x in ["Volume", "Slice i", "Slice j", "Slice k"]
123+
],
124+
value=["Volume", "i"],
125+
id="child-enabled",
126+
inline=True,
127+
),
128+
]
129+
),
130+
dbc.FormGroup(
131+
[
132+
dbc.Label("Color Window"),
133+
dcc.Slider(
134+
min=0.01, max=1, value=0.5, step=0.01, id="color-window", tooltip={},
135+
),
136+
]
137+
),
138+
dbc.FormGroup(
139+
[
140+
dbc.Label("Color Level"),
141+
dcc.Slider(
142+
min=0.01, max=1, value=0.5, step=0.01, id="color-level", tooltip={},
143+
),
144+
]
145+
),
146+
CustomSlider(data=fd.x, id="slider-slice-i", label="Slice i"),
147+
CustomSlider(data=fd.y, id="slider-slice-j", label="Slice j"),
148+
CustomSlider(data=fd.z, id="slider-slice-k", label="Slice k"),
149+
]
150+
151+
vtk_view = dash_vtk.View(id="vtk-view")
152+
153+
154+
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
155+
server = app.server # gunicorn needs this for deployment
156+
157+
app.layout = dbc.Container(
158+
fluid=True,
159+
style={"height": "100vh"},
160+
children=[
161+
Header("Flow Visualization with FLORIS and VTK", app),
162+
html.Hr(),
163+
dbc.Row(
164+
[
165+
dbc.Col(
166+
width=4,
167+
children=dbc.Card(
168+
[dbc.CardHeader("Controls"), dbc.CardBody(controls),]
169+
),
170+
),
171+
dbc.Col(
172+
width=8,
173+
children=dbc.Card(
174+
[
175+
dbc.CardHeader("Flow Visualization"),
176+
dbc.CardBody(vtk_view, style={"height": "100%"}),
177+
],
178+
style={"height": "80vh"},
179+
),
180+
),
181+
],
182+
),
183+
],
184+
)
185+
186+
187+
@app.callback(
188+
Output("vtk-view", "children"),
189+
Output("vtk-view", "triggerRender"),
190+
Input("radio-dimension", "value"),
191+
Input("child-enabled", "value"),
192+
Input("color-window", "value"),
193+
Input("color-level", "value"),
194+
Input("slider-slice-i", "value"),
195+
Input("slider-slice-j", "value"),
196+
Input("slider-slice-k", "value"),
197+
)
198+
def update_flow_viz(
199+
selected_dim, enabled, window_coef, level_coef, slice_i, slice_j, slice_k
200+
):
201+
field = views[selected_dim]
202+
window = (field.max() - field.min()) * window_coef
203+
level = (field.max() + field.min()) * level_coef
204+
205+
new_view_child = build_view_child(
206+
dimensions,
207+
spacing,
208+
origin,
209+
window=window,
210+
level=level,
211+
field=field,
212+
enabled=enabled,
213+
i=slice_i,
214+
j=slice_j,
215+
k=slice_k,
216+
)
217+
return new_view_child, random.random()
218+
219+
220+
if __name__ == "__main__":
221+
app.run_server(debug=True)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* Your custom css code goes here */

0 commit comments

Comments
 (0)