File: testMolDraw2DQt.py

package info (click to toggle)
rdkit 202209.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 203,880 kB
  • sloc: cpp: 334,239; python: 80,247; ansic: 24,579; java: 7,667; sql: 2,123; yacc: 1,884; javascript: 1,358; lex: 1,260; makefile: 576; xml: 229; fortran: 183; cs: 181; sh: 101
file content (93 lines) | stat: -rw-r--r-- 2,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import sys
import unittest

from rdkit import Chem
from rdkit import RDConfig
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
from rdkit.Chem import rdDepictor
from rdkit.Chem.Draw import rdMolDraw2DQt

try:
  if rdMolDraw2DQt.rdkitQtVersion.startswith('6'):
    from PyQt6.QtGui import *
    Format_RGB32 = QImage.Format.Format_RGB32
  else:
    from PyQt5.Qt import *
    Format_RGB32 = QImage.Format_RGB32
except ImportError:
  # If we can't find Qt, there's nothing we can do
  sip = None
else:
  try:
    # Prefer the PyQt-bundled sip
    if rdMolDraw2DQt.rdkitQtVersion.startswith('6'):
      from PyQt6 import sip
    else:
      from PyQt5 import sip
  except ImportError:
    # No bundled sip, try the standalone package
    try:
      import sip
    except ImportError:
      # No sip at all
      sip = None


@unittest.skipIf(sip is None, "skipping tests because pyqt is not installed")
class TestCase(unittest.TestCase):

  def setUp(self):
    pass

  def testSIPBasics(self):
    m = Chem.MolFromSmiles('c1ccccc1O')
    Draw.PrepareMolForDrawing(m)
    qimg = QImage(250, 200, Format_RGB32)
    with QPainter(qimg) as qptr:
      p = sip.unwrapinstance(qptr)
      d2d = rdMolDraw2DQt.MolDraw2DFromQPainter_(250, 200, p)
      d2d.DrawMolecule(m)
    qimg.save("testImageFromPyQt-1.png")

  def testBasics(self):
    m = Chem.MolFromSmiles('c1ccccc1O')
    Draw.PrepareMolForDrawing(m)
    qimg = QImage(250, 200, Format_RGB32)
    with QPainter(qimg) as qptr:
      d2d = Draw.MolDraw2DFromQPainter(qptr)
      d2d.DrawMolecule(m)
    qimg.save("testImageFromPyQt-2.png")

  def testMemory1(self):

    def testfunc():
      m = Chem.MolFromSmiles('c1ccccc1O')
      Draw.PrepareMolForDrawing(m)
      qimg = QImage(250, 200, Format_RGB32)
      with QPainter(qimg) as qptr:
        p = sip.unwrapinstance(qptr)
        d2d = rdMolDraw2DQt.MolDraw2DFromQPainter_(250, 200, p, -1, -1)
      raise ValueError("expected")

    with self.assertRaises(ValueError):
      testfunc()

  def testMemory2(self):

    def testfunc():
      m = Chem.MolFromSmiles('c1ccccc1O')
      Draw.PrepareMolForDrawing(m)
      qimg = QImage(250, 200, Format_RGB32)
      with QPainter(qimg) as qptr:
        d2d = Draw.MolDraw2DFromQPainter(qptr)
      raise ValueError("expected")

    with self.assertRaises(ValueError):
      testfunc()


if __name__ == "__main__":
  if sip is not None:
    app = QGuiApplication(sys.argv)
  unittest.main()