# Magic
%matplotlib inline
# Reload modules whenever they change
%load_ext autoreload
%autoreload 2
# Make clusterking package available even without installation
import sys
sys.path = ["../../"] + sys.path
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import os.path
from clusterking.plots import ClusterPlot
import numpy as np
# [ For testing purposes we make sure that we always have a file
# that exists to fall back to ]
paths = [
"output/cluster/long_g10_n30_d05_data.csv",
"output/cluster/tutorial_basics_data.csv"
]
path = None
for p in paths:
if os.path.exists(p):
path = p
break
df = pd.read_csv(path)
cp = ClusterPlot(df)
cp.debug = True
class BetterClusterPlot(ClusterPlot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def voxel(self):
self._setup_all(cols, clusters)
for isubplot in range(self._nsubplots):
for cluster in self._clusters:
df_cluster = self.df[self.df[self.cluster_column] == cluster]
for col in self._dofs:
df_cluster = df_cluster[df_cluster[col] ==
self._df_dofs.iloc[isubplot][col]]
self._axli[isubplot].scatter(
*[df_cluster[col] for col in self._axis_columns],
color=self.colors[cluster-1 % len(self.colors)],
marker=self.markers[cluster-1 % len(self.markers)],
label=cluster
)
if 'inline' not in matplotlib.get_backend():
return self._fig
bcp = BetterClusterPlot(df)
cp.scatter(["l", "sl", 't'])
x, y, z = np.indices((2, 2, 2))
x
x, y, z = np.indices((8, 8, 8))
# prepare some coordinates
x, y, z = np.indices((8, 8, 8))
# draw cuboids in the top left and bottom right corners, and a link between them
cube1 = (x < 3) & (y < 3) & (z < 3)
cube2 = (x >= 5) & (y >= 5) & (z >= 5)
link = abs(x - y) + abs(y - z) + abs(z - x) <= 2
# combine the objects into a single boolean array
voxels = cube1 | cube2 | link
# set the colors of each object
colors = np.empty(voxels.shape, dtype=object)
colors[link] = 'red'
colors[cube1] = 'blue'
colors[cube2] = 'green'
# and plot everything
fig = plt.figure()
ax = fig.gca(projection='3d')
_ = ax.voxels(voxels, facecolors=colors, edgecolor='k')
mask = df['cluster'] == 4
mask2 = df['cluster'] == 2
x_values = df['l'].unique()
y_values = df['sl'].unique()
z_values = df['t'].unique()
yes = mask.values.reshape(len(x_values), len(y_values), len(z_values))
yes2 = mask2.values.reshape(len(x_values), len(y_values), len(z_values))
colors = np.empty(yes.shape, dtype=object)
colors[yes] = "#FF000050"
colors[yes2] = "#00FF0050"
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.view_init(30, 20)
_ = ax.voxels(yes|yes2, facecolors=colors, edgecolor='')