Skip to content

Commit d15816f

Browse files
committed
Added correlation matrix and plot embedding example. Minor tweaks at build script and Dockerfile
1 parent 526fdbf commit d15816f

File tree

5 files changed

+821
-7
lines changed

5 files changed

+821
-7
lines changed

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ RUN chgrp -R 0 /srv && \
5858
RUN chmod g+w /etc/passwd
5959
USER 1001
6060

61+
# Install additional user libraries
62+
# RUN pip install ginza
63+
6164
# Expose container port 5000 (MLTK Container Service) and 8888 (Notebook) and 6006 (Tensorboard)
6265
EXPOSE 5000 8888 6006
6366

app/model/correlationmatrix.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
5+
6+
# In[2]:
7+
8+
9+
# this definition exposes all python module imports that should be available in all subsequent commands
10+
import json
11+
import numpy as np
12+
import pandas as pd
13+
import seaborn as sns
14+
import matplotlib.pyplot as plt
15+
# ...
16+
# global constants
17+
MODEL_DIRECTORY = "/srv/app/model/data/"
18+
19+
20+
21+
22+
23+
24+
25+
26+
# In[4]:
27+
28+
29+
# this cell is not executed from MLTK and should only be used for staging data into the notebook environment
30+
def stage(name):
31+
with open("data/"+name+".csv", 'r') as f:
32+
df = pd.read_csv(f)
33+
with open("data/"+name+".json", 'r') as f:
34+
param = json.load(f)
35+
return df, param
36+
37+
38+
39+
40+
41+
42+
43+
44+
# In[6]:
45+
46+
47+
# initialize your model
48+
# available inputs: data and parameters
49+
# returns the model object which will be used as a reference to call fit, apply and summary subsequently
50+
def init(df,param):
51+
model = {}
52+
return model
53+
54+
55+
56+
57+
58+
59+
60+
61+
# In[8]:
62+
63+
64+
# train your model
65+
# returns a fit info json object and may modify the model object
66+
def fit(model,df,param):
67+
# model.fit()
68+
info = {"message": "no fit needed"}
69+
return info
70+
71+
72+
73+
74+
75+
76+
77+
78+
# In[10]:
79+
80+
81+
# apply your model
82+
# returns the calculated results
83+
def plot_to_base64(plot):
84+
import base64
85+
import io
86+
pic_IObytes = io.BytesIO()
87+
if hasattr(plot,'fig'):
88+
plot.fig.savefig(pic_IObytes, format='png')
89+
elif hasattr(plot,'figure'):
90+
plot.figure.savefig(pic_IObytes, format='png')
91+
pic_IObytes.seek(0)
92+
pic_hash = base64.b64encode(pic_IObytes.read())
93+
return pic_hash
94+
95+
96+
def plot_pairplot_as_base64(df,param):
97+
hue=None
98+
if 'options' in param:
99+
if 'target_variable' in param['options']:
100+
hue=str(param['options']['target_variable'][0])
101+
plot = sns.pairplot(df,hue=hue, palette="husl")
102+
return str(plot_to_base64(plot))
103+
104+
105+
def plot_correlationmatrix_as_base64(corr):
106+
# Set up the matplotlib figure
107+
f, ax = plt.subplots(figsize=(15, 15))
108+
# Generate a mask for the upper triangle
109+
mask = np.triu(np.ones_like(corr, dtype=np.bool))
110+
# Generate a custom diverging colormap
111+
cmap = sns.diverging_palette(250, 10, as_cmap=True)
112+
# Draw the heatmap with the mask and correct aspect ratio
113+
#plot = sns.heatmap(corr, mask=mask, cmap="Spectral", vmax=1.0, center=0, square=True, linewidths=.5, cbar_kws={"shrink": .5})
114+
plot = sns.heatmap(corr, cmap="Spectral", vmax=1.0, center=0, square=True, linewidths=.5, cbar_kws={"shrink": .5})
115+
#plot.figure.savefig("plot.png", format='png')
116+
return str(plot_to_base64(plot))
117+
118+
def apply(model,df,param):
119+
# param['options']['model_name']
120+
dfeatures = df[param['feature_variables']]
121+
result = dfeatures.corr() #.reset_index()
122+
if 'plot' in param['options']['params']:
123+
plots = param['options']['params']['plot'].lstrip("\"").rstrip("\"").lower().split(',')
124+
for plot in plots:
125+
if plot=='matrix':
126+
model["plot_matrix"] = plot_correlationmatrix_as_base64(result)
127+
elif plot=='pairplot':
128+
model["plot_pairplot"] = plot_pairplot_as_base64(df,param)
129+
else:
130+
continue
131+
132+
return result
133+
134+
135+
136+
137+
138+
139+
140+
141+
# In[12]:
142+
143+
144+
# save model to name in expected convention "<algo_name>_<model_name>"
145+
def save(model,name):
146+
with open(MODEL_DIRECTORY + name + ".json", 'w') as file:
147+
json.dump(model, file)
148+
return model
149+
150+
151+
152+
153+
154+
155+
156+
157+
# In[14]:
158+
159+
160+
# load model from name in expected convention "<algo_name>_<model_name>"
161+
def load(name):
162+
model = {}
163+
with open(MODEL_DIRECTORY + name + ".json", 'r') as file:
164+
model = json.load(file)
165+
return model
166+
167+
168+
169+
170+
171+
172+
173+
174+
# In[19]:
175+
176+
177+
# return a model summary
178+
def summary(model=None):
179+
returns = {"version": {"numpy": np.__version__, "pandas": pd.__version__} }
180+
return returns
181+
182+
183+
184+
185+

bootstrap.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,3 @@ cp -R -n /dltk/notebooks /srv
1515
export HOME=/dltk
1616

1717
jupyter lab --port=8888 --ip=0.0.0.0 --no-browser & tensorboard --bind_all --logdir /srv/notebooks/logs/ & flask run -h 0.0.0.0
18-
# option for integrated jupyter tensorboard in future
19-
# jupyter lab --port=8888 --ip=0.0.0.0 --no-browser & jupyter tensorboard enable --user & flask run -h 0.0.0.0

build.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ echo ' \ \_\ \ \_\ \_____\ \ \_\\\ \_\ \_\ \ \_____\ \_____\ \_\\\"\_\ \ \_\\
77
echo ' \/_/ \/_/\/_____/ \/_/ \/_/\/_/ \/_____/\/_____/\/_/ \/_/ \/_/ \/_/\/_/\/_/\/_/ \/_/\/_____/\/_/ /_/ '
88
echo "_____________________________________________________________________________________________________________"
99
echo "Splunk> MLTK Container for TensorFlow 2.0, PyTorch and Jupyterlab."
10-
tag="tf-cpu"
11-
base="tensorflow/tensorflow:latest-py3"
10+
tag="golden-image-gpu"
11+
base="nvidia/cuda:10.2-cudnn7-runtime-ubuntu16.04"
1212
dockerfile="Dockerfile"
13-
repo=""
13+
repo="phdrieger/"
1414
if [ -z "$1" ]; then
15-
echo "No build parameters set. Using default tag tf-cpu for building and running the container."
15+
echo "No build parameters set. Using default tag golden-image-gpu for building and running the container."
1616
echo "You can use ./build.sh [golden-image-gpu|tf-cpu|tf-gpu|pytorch|nlp] to build the container for different frameworks."
1717
else
1818
tag="$1"
@@ -38,7 +38,7 @@ case $tag in
3838
dockerfile="Dockerfile.root.3.0"
3939
;;
4040
*)
41-
echo "Invalid container image tag: $tag, expected [golden-image|tf-cpu|tf-gpu|pytorch|pytorch-nlp]"
41+
echo "Invalid container image tag: $tag, expected [golden-image-gpu|tf-cpu|tf-gpu|pytorch|pytorch-nlp]"
4242
break
4343
;;
4444
esac

0 commit comments

Comments
 (0)