Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Former-commit-id: c501f0e
  • Loading branch information
xhlulu committed Jul 3, 2020
1 parent c548c56 commit cdef45b
Show file tree
Hide file tree
Showing 7 changed files with 436 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .deployignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# these apps are ignored for deployment
dash-summarize
dash-translate
dash-translate
dash-cuml-umap
2 changes: 2 additions & 0 deletions apps/dash-cuml-umap/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*__pycache__*
*.vscode*
1 change: 1 addition & 0 deletions apps/dash-cuml-umap/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn app:server --workers 1
31 changes: 31 additions & 0 deletions apps/dash-cuml-umap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Dash cuML UMAP

A demo of RAPIDS.ai's cuML UMAP functionality.

## Instructions

To get started, first clone this repo:
```
git clone https://github.com/plotly/dash-sample-apps.git
cd dash-sample-apps/apps/dash-cuml-umap
```

Create a conda env and install the requirements:
```
conda create --name dash-cuml-umap --file requirements.txt
```

You can now run the app:
```
python app.py
```

and visit http://127.0.0.1:8050/.


## Note about exporting

Since RAPIDS.ai only support a conda environment, `pip freeze` the requirements would not work. Instead, do:
```
conda list -e > requirements.txt
```
114 changes: 114 additions & 0 deletions apps/dash-cuml-umap/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import time

import cudf
import cuml
import cupy as cp
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
import plotly.express as px


# Load CSV into a cudf
path = 'creditcard.csv'
gdf = cudf.read_csv(path)
gdf.Time = gdf.Time / 3600
gdf.loc[gdf.Amount > 500, 'Amount'] = 500

# Define app
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server


controls = dbc.Row(
[
dbc.Col(
dbc.FormGroup(
[
dbc.Label("Time since start (h)"),
dcc.RangeSlider(
id="slider-hours",
min=0, max=50, step=1,
value=[20, 30],
marks={i: str(i) for i in range(0, 51, 10)}
),
]
),
md=6
),

dbc.Col(
dbc.FormGroup(
[
dbc.Label("Transaction Amount ($)"),
dcc.RangeSlider(
id="slider-amount",
min=0, max=500, step=5,
value=[200, 300],
marks={i: str(i) for i in range(0, 501, 100)}
),
]
),
md=6
)
],
)


# Define Layout
app.layout = dbc.Container(
fluid=True,
children=[
html.H1("Dash cuML UMAP Demo"),
html.Hr(),
dbc.Card(controls, body=True),
dcc.Graph(id="graph-umap", style={'height': '70vh', 'max-height': '90vw'}),
html.Div(id='output-info'),
],
style={'max-width': '960px', 'margin': 'auto'}
)


@app.callback(
[Output('graph-umap', 'figure'),
Output('output-info', 'children')],
[Input('slider-amount', 'value'),
Input('slider-hours', 'value'),]
)
def update_graph(amt, hrs):
t0 = time.time()
# First, filter based on the slider values
time_mask = (gdf.Time >= hrs[0]) & (gdf.Time <= hrs[1])
amount_mask = (gdf.Amount >= amt[0]) & (gdf.Amount <= amt[1])
filt_df = gdf.loc[time_mask & amount_mask]

# Then, select the features and train a UMAP model with cuML
features = filt_df.loc[:, "V1": "V28"].values
reducer = cuml.UMAP()
embedding = reducer.fit_transform(features)

# Convert the embedding back to numpy
embedding = cp.asnumpy(embedding)
amount = cp.asnumpy(filt_df.Amount.values.round(2))

# Create a plotly.express scatter plot
fig = px.scatter(
x=embedding[:, 0],
y=embedding[:, 1],
color=amount,
labels={'color': 'Amount ($)'},
title='UMAP projection of credit card transactions'
)

t1 = time.time()
out_msg = f"Projected {embedding.shape[0]} transactions in {t1-t0:.2f}s."
alert = dbc.Alert(out_msg, color="success", dismissable=True)

return fig, alert



if __name__ == "__main__":
app.run_server(debug=True)
Loading

0 comments on commit cdef45b

Please sign in to comment.