Skip to content
Snippets Groups Projects
Commit 027a8d67 authored by Hamish Graham's avatar Hamish Graham
Browse files

updated pandas_with_bokeh

parent 6112215b
Branches
No related tags found
No related merge requests found
Pipeline #2969958 failed
import pandas_skeleton as ps
import tree_maker as tm
from pandas import DataFrame
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Slider, WheelZoomTool, BoxZoomTool, Button
from bokeh.plotting import figure
from bokeh.themes import Theme
from bokeh.io import show, output_notebook
output_notebook()
#root = tm.tree_from_json(filename='/home/HPC/sterbini/DA_study_example/study_000/tree.json')
def bkapp(doc):
global my_df
global source
try:
ps.create_tree(root)
except:
raise Exception('Sorry, I need a root of a tree!')
x_values, y_values, path = ps.create_xypath(root)
my_colors = ps.create_color(root)
angles = ps.create_tree_cartesian(root)
my_df = ps.create_df(root, path, x_values, y_values, my_colors)
del my_df['handle']
source = ColumnDataSource(data=my_df)
plot = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here")
plot.circle('x', 'y', source=source, alpha=0.6, color = 'color')
plot.add_tools(BoxZoomTool())
def callback(attr, index_list, _):
global my_df
global my_df_selected
source.data = ColumnDataSource.from_df(my_df)
my_df_selected = my_df.loc[index_list]
def dummyfunction():
global my_df
my_df = ps.update(my_df, 'completed', 'green')
source.selected.on_change('indices', callback)
button = Button(label="Update", button_type="success")
button.on_click(dummyfunction)
doc.add_root(column(plot, button))
show(bkapp, notebook_url='http://localhost:8102', port=8202) # notebook_url="http://localhost:8888"
\ No newline at end of file
import tree_maker as tm
import numpy as np
import math
import random
from anytree import AnyNode, RenderTree
import numpy as np
import json
import pandas as pd
def create_tree(node):
"""
Creates a tree with the shape of a 'flower'. The nodes that have attributes; x, y, ragius, short_path, angle, min_angle, max_angle and color.
"""
if node.is_leaf:
pass
else:
if node.is_root:
node.x = 0
node.y = 0
node.radius = 0
#node.min_x = -2
#node.max_x = 2
node.short_path = '/'.join(node.path.split('/')[-3:])
node.angle = 2 * math.pi
node.min_angle = 0
node.max_angle = 2 * math.pi
node.color = 'black'
for my_child, my_angle in zip(node.children, np.linspace(node.min_angle, node.max_angle, len(node.children))):
#my_child.min_x = xx - (node.max_x - node.min_x)/len(node.children)/2
#my_child.max_x = xx + (node.max_x - node.min_x)/len(node.children)/2
my_child.color = "black"
my_child.short_path = '/'.join(my_child.path.split('/')[-3:])
my_child.angle = my_angle
my_child.min_angle = my_angle - (node.max_angle - node.min_angle)/len(node.children)/2
my_child.max_angle = my_angle + (node.max_angle - node.min_angle)/len(node.children)/2
my_child.radius = node.radius + 1
my_child.x = my_child.radius * math.cos(my_angle)
my_child.y = my_child.radius * math.sin(my_angle)
create_tree(my_child)
def create_xypath(node):
"""
Creates a list for x_values and y_values for the nodes of a tree.
"""
x_values = [node.x]
y_values = [node.y]
path = [node.short_path]
for descendant in node.descendants:
x_values.append(descendant.x)
y_values.append(descendant.y)
path.append(descendant.short_path)
return x_values, y_values, path
def create_color(node):
"""
Adds colors of nodes to an array.
"""
my_colors = [node.color]
for descendant in node.descendants:
my_colors.append(descendant.color)
return my_colors
def create_tree_cartesian(node):
"""
Adds the angle attribute to an array.
"""
angles = [node.angle]
for descendant in node.descendants:
angles.append(node.angle)
return angles
def get_status(handle):
"""
Returns last key, the status of the job, from 'log_file'. Incase of an empty log_file, 'None' is returned.
"""
keys = list(tm.from_json(handle.log_file))
if len(keys) > 0:
return keys[-1]
else:
return None
def create_df(node, path, x_values, y_values, my_colors):
"""
Creating a dataframe and its attributes. Here its attributes are; node, path, x_values, y_values and my_colors.
"""
my_df = pd.DataFrame([node]+list(node.descendants), columns=['handle']).copy()
my_df['name'] = my_df['handle'].apply(lambda x:x.name)
my_df['path'] = path
my_df['x'] = x_values # to check the order
my_df['y'] = y_values # to check the order
my_df['status'] = my_df['handle'].apply(get_status)
my_df['color'] = my_colors
return my_df
def update(my_df, last_key, my_color):
"""
Filtering through the dataframe using the 'last_key'. The color that is connected to this 'last_key', can also be chosen.
"""
new_df1 = my_df[(my_df.status == last_key)].copy()
new_df1['color'] = my_color
new_df2 = my_df[(my_df.status != last_key)].copy()
my_df = pd.concat([new_df1, new_df2]).sort_index()
return my_df
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment