rdkit.Chem.Draw.mplCanvas

Package rdkit :: Package Chem :: Package Draw :: Module mplCanvas |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Chem.Draw.mplCanvas

     1  #
     2  #  Copyright (C) 2008 Greg Landrum
     3  #
     4  #   @@ All Rights Reserved @@
     5  #  This file is part of the RDKit.
     6  #  The contents are covered by the terms of the BSD license
     7  #  which is included in the file license.txt, found at the root
     8  #  of the RDKit source tree.
     9  #
    10  from matplotlib.lines import Line2D
    11  from matplotlib.patches import Polygon
    12  from matplotlib.pyplot import figure
    13
    14  from rdkit.Chem.Draw.canvasbase import CanvasBase
    15
    16
    17 -class Canvas(CanvasBase):
    18
    19 -  def __init__(self, size, name='', imageType='png'):
    20      self._name = name
    21      self.size = size
    22      dpi = max(size[0], size[1])
    23      figsize = (int(float(size[0]) / dpi), int(float(size[1]) / dpi))
    24      self._figure = figure(figsize=figsize)
    25      self._axes = self._figure.add_axes([0, 0, 2.5, 2.5])
    26      self._axes.set_xticklabels('')
    27      self._axes.set_yticklabels('')
    28      self._dpi = dpi
    29
    30 -  def rescalePt(self, p1):
    31      return [float(p1[0]) / self._dpi, float(self.size[1] - p1[1]) / self._dpi]
    32
    33 -  def addCanvasLine(self, p1, p2, color=(0, 0, 0), color2=None, **kwargs):
    34      canvas = self._axes
    35      p1 = self.rescalePt(p1)
    36      p2 = self.rescalePt(p2)
    37      if color2 and color2 != color:
    38        mp = (p1[0] + p2[0]) / 2., (p1[1] + p2[1]) / 2.
    39        canvas.add_line(Line2D((p1[0], mp[0]), (p1[1], mp[1]), color=color, **kwargs))
    40        canvas.add_line(Line2D((mp[0], p2[0]), (mp[1], p2[1]), color=color2, **kwargs))
    41      else:
    42        canvas.add_line(Line2D((p1[0], p2[0]), (p1[1], p2[1]), color=color, **kwargs))
    43
    44 -  def addCanvasText(self, text, pos, font, color=(0, 0, 0), **kwargs):
    45      import re
    46      pos = self.rescalePt(pos)
    47      canvas = self._axes
    48      text = re.sub(r'<.*?>', '', text)
    49      orientation = kwargs.get('orientation', 'E')
    50      halign = 'center'
    51      valign = 'center'
    52      if orientation == 'E':
    53        halign = 'left'
    54      elif orientation == 'W':
    55        halign = 'right'
    56      elif orientation == 'S':
    57        valign = 'top'
    58      elif orientation == 'N':
    59        valign = 'bottom'
    60
    61      annot = canvas.annotate(text, (pos[0], pos[1]), color=color, verticalalignment=valign,
    62                              horizontalalignment=halign, weight=font.weight, size=font.size * 2.0,
    63                              family=font.face)
    64
    65      try:
    66        bb = annot.get_window_extent(renderer=self._figure.canvas.get_renderer())
    67        w, h = bb.width, bb.height
    68        tw, th = canvas.transData.inverted().transform((w, h))
    69      except AttributeError:
    70        tw, th = 0.1, 0.1  # <- kludge
    71      return (tw, th, 0)
    72
    73 -  def addCanvasPolygon(self, ps, color=(0, 0, 0), **kwargs):
    74      canvas = self._axes
    75      ps = [self.rescalePt(x) for x in ps]
    76      canvas.add_patch(Polygon(ps, linewidth=0, facecolor=color))
    77
    78 -  def addCanvasDashedWedge(self, p1, p2, p3, dash=(2, 2), color=(0, 0, 0), color2=None, **kwargs):
    79      canvas = self._axes
    80      dash = (3, 3)
    81      pts1 = self._getLinePoints(p1, p2, dash)
    82      pts2 = self._getLinePoints(p1, p3, dash)
    83      pts1 = [self.rescalePt(p) for p in pts1]
    84      pts2 = [self.rescalePt(p) for p in pts2]
    85      if len(pts2) < len(pts1):
    86        pts2, pts1 = pts1, pts2
    87      for i in range(len(pts1)):
    88        if color2 and color2 != color:
    89          mp = (pts1[i][0] + pts2[i][0]) / 2., (pts1[i][1] + pts2[i][1]) / 2.
    90          canvas.add_line(Line2D((pts1[i][0], mp[0]), (pts1[i][1], mp[1]), color=color, **kwargs))
    91          canvas.add_line(Line2D((mp[0], pts2[i][0]), (mp[1], pts2[i][1]), color=color2, **kwargs))
    92        else:
    93          canvas.add_line(
    94            Line2D((pts1[i][0], pts2[i][0]), (pts1[i][1], pts2[i][1]), color=color, **kwargs))
    95