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