Skip to content

Commit 0b13f19

Browse files
committed
Update ver 0.4.1
1 parent 760aeb1 commit 0b13f19

File tree

9 files changed

+655
-245
lines changed

9 files changed

+655
-245
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ If you want to use developmental version, it can be installed using the followin
1616
`pip install git+https://github.com/ponnhide/patchworklib.git`
1717

1818
## News
19+
#### 04182022: Version 0.4.1 is released.
20+
- `load_seaborngrid` can accepts a `seaborn.clustermap` plot. For details, see example code on [Google colab](https://colab.research.google.com/drive/1wQQyBHLNXJ5Ks6ev88IjXhGfT98SGJuM?usp=sharing)
21+
- Some bugs were fixed.
22+
1923
#### 03272022: Version 0.4.0 is released.
2024
- Add docstring for each method and class
2125
- Add some new methods of `patchworklib.Bricks` class to set common label, title, spine and colorbar for `Brick` objets in the `Bricks` object.

example/clutermap.ipynb

Lines changed: 187 additions & 0 deletions
Large diffs are not rendered by default.

example/seaborn_ggplot.ipynb

Lines changed: 12 additions & 20 deletions
Large diffs are not rendered by default.

example/seaborn_grid.ipynb

Lines changed: 23 additions & 15 deletions
Large diffs are not rendered by default.

example/stack.ipynb

Lines changed: 34 additions & 26 deletions
Large diffs are not rendered by default.

example/sup_elements.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
{
128128
"data": {
129129
"text/plain": [
130-
"<matplotlib.spines.Spine at 0x12231eac0>"
130+
"<matplotlib.spines.Spine at 0x148651310>"
131131
]
132132
},
133133
"execution_count": 5,
@@ -195,7 +195,7 @@
195195
],
196196
"metadata": {
197197
"kernelspec": {
198-
"display_name": "Python 3",
198+
"display_name": "Python 3 (ipykernel)",
199199
"language": "python",
200200
"name": "python3"
201201
},
@@ -209,7 +209,7 @@
209209
"name": "python",
210210
"nbconvert_exporter": "python",
211211
"pygments_lexer": "ipython3",
212-
"version": "3.9.6"
212+
"version": "3.9.9"
213213
}
214214
},
215215
"nbformat": 4,

example/tutorial.ipynb

Lines changed: 152 additions & 152 deletions
Large diffs are not rendered by default.

patchworklib/modified_grid.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import pandas as pd
44
import matplotlib as mpl
55
import matplotlib.pyplot as plt
6+
from matplotlib import gridspec
67
from itertools import product
78

89
import seaborn
910
from inspect import signature
1011
from seaborn.axisgrid import FacetGrid, JointGrid, PairGrid, Grid
12+
import seaborn.matrix as sm
13+
from seaborn.matrix import ClusterGrid
1114
from seaborn._core import VectorPlotter, variable_type, categorical_order
1215
from seaborn import utils
1316
from seaborn.utils import _check_argument, adjust_legend_subtitles, _draw_figure
@@ -461,3 +464,114 @@ def __setattr_for_clustergrid__(self, key, value):
461464
else:
462465
super.__setattr__(self, key, value)
463466

467+
def __init_for_clustergrid__(self, data, pivot_kws=None, z_score=None, standard_scale=None,
468+
figsize=None, row_colors=None, col_colors=None, mask=None,
469+
dendrogram_ratio=None, colors_ratio=None, cbar_pos=None):
470+
"""Grid object for organizing clustered heatmap input on to axes"""
471+
try:
472+
import scipy
473+
except:
474+
raise RuntimeError("ClusterGrid requires scipy to be available")
475+
476+
if isinstance(data, pd.DataFrame):
477+
self.data = data
478+
else:
479+
self.data = pd.DataFrame(data)
480+
481+
self.data2d = self.format_data(self.data, pivot_kws, z_score,
482+
standard_scale)
483+
484+
self.mask = sm._matrix_mask(self.data2d, mask)
485+
486+
self._figure = Grid._figure #Modified by Hideto
487+
self._figsize = figsize
488+
self._figure.set_size_inches(figsize)
489+
#self._figure = plt.figure(figsize=figsize)
490+
491+
self.row_colors, self.row_color_labels = \
492+
self._preprocess_colors(data, row_colors, axis=0)
493+
self.col_colors, self.col_color_labels = \
494+
self._preprocess_colors(data, col_colors, axis=1)
495+
496+
try:
497+
row_dendrogram_ratio, col_dendrogram_ratio = dendrogram_ratio
498+
except TypeError:
499+
row_dendrogram_ratio = col_dendrogram_ratio = dendrogram_ratio
500+
501+
try:
502+
row_colors_ratio, col_colors_ratio = colors_ratio
503+
except TypeError:
504+
row_colors_ratio = col_colors_ratio = colors_ratio
505+
506+
width_ratios = self.dim_ratios(self.row_colors,
507+
row_dendrogram_ratio,
508+
row_colors_ratio)
509+
height_ratios = self.dim_ratios(self.col_colors,
510+
col_dendrogram_ratio,
511+
col_colors_ratio)
512+
513+
nrows = 2 if self.col_colors is None else 3
514+
ncols = 2 if self.row_colors is None else 3
515+
516+
self.gs = gridspec.GridSpec(nrows, ncols,
517+
width_ratios=width_ratios,
518+
height_ratios=height_ratios, wspace=0, hspace=0)
519+
520+
self.ax_row_dendrogram = self._figure.add_subplot(self.gs[-1, 0])
521+
self.ax_col_dendrogram = self._figure.add_subplot(self.gs[0, -1])
522+
self.ax_row_dendrogram.set_axis_off()
523+
self.ax_col_dendrogram.set_axis_off()
524+
525+
self.ax_row_colors = None
526+
self.ax_col_colors = None
527+
528+
if self.row_colors is not None:
529+
self.ax_row_colors = self._figure.add_subplot(
530+
self.gs[-1, 1])
531+
if self.col_colors is not None:
532+
self.ax_col_colors = self._figure.add_subplot(
533+
self.gs[1, -1])
534+
535+
self.ax_heatmap = self._figure.add_subplot(self.gs[-1, -1])
536+
if cbar_pos is None:
537+
self.ax_cbar = self.cax = None
538+
else:
539+
# Initialize the colorbar axes in the gridspec so that tight_layout
540+
# works. We will move it where it belongs later. This is a hack.
541+
self.ax_cbar = self._figure.add_subplot(self.gs[0, 0])
542+
self.cax = self.ax_cbar # Backwards compatibility
543+
self.cbar_pos = cbar_pos
544+
545+
self.dendrogram_row = None
546+
self.dendrogram_col = None
547+
548+
def __plot_for_clustergrid__(self, metric, method, colorbar_kws, row_cluster, col_cluster, row_linkage, col_linkage, tree_kws, **kws):
549+
# heatmap square=True sets the aspect ratio on the axes, but that is
550+
# not compatible with the multi-axes layout of clustergrid
551+
if kws.get("square", False):
552+
msg = "``square=True`` ignored in clustermap"
553+
warnings.warn(msg)
554+
kws.pop("square")
555+
556+
colorbar_kws = {} if colorbar_kws is None else colorbar_kws
557+
558+
self.plot_dendrograms(row_cluster, col_cluster, metric, method,
559+
row_linkage=row_linkage, col_linkage=col_linkage,
560+
tree_kws=tree_kws)
561+
562+
try:
563+
xind = self.dendrogram_col.reordered_ind
564+
except AttributeError:
565+
xind = np.arange(self.data2d.shape[1])
566+
567+
try:
568+
yind = self.dendrogram_row.reordered_ind
569+
except AttributeError:
570+
yind = np.arange(self.data2d.shape[0])
571+
572+
self.plot_colors(xind, yind, **kws)
573+
self.plot_matrix(colorbar_kws, xind, yind, **kws)
574+
self._figure.set_size_inches((1,1))
575+
#mpl.rcParams["figure.subplot.hspace"] = hspace
576+
#mpl.rcParams["figure.subplot.wspace"] = wspace
577+
return self

0 commit comments

Comments
 (0)