Exemplo n.º 1
0
def _toReactionPNG(rxn):
    rc = copy.deepcopy(rxn)
    img = Draw.ReactionToImage(rc,
                               subImgSize=(int(molSize[0] / 3), molSize[1]))
    bio = BytesIO()
    img.save(bio, format='PNG')
    return bio.getvalue()
Exemplo n.º 2
0
def _toPNG(mol):
  if hasattr(mol, '__sssAtoms'):
    highlightAtoms = mol.__sssAtoms
  else:
    highlightAtoms = []
  try:
    mol.GetAtomWithIdx(0).GetExplicitValence()
  except RuntimeError:
    mol.UpdatePropertyCache(False)

  if not hasattr(rdMolDraw2D, 'MolDraw2DCairo'):
    mc = copy.deepcopy(mol)
    try:
      img = Draw.MolToImage(mc, size=molSize, kekulize=kekulizeStructures,
                            highlightAtoms=highlightAtoms)
    except ValueError:  # <- can happen on a kekulization failure
      mc = copy.deepcopy(mol)
      img = Draw.MolToImage(mc, size=molSize, kekulize=False, highlightAtoms=highlightAtoms)
    bio = BytesIO()
    img.save(bio, format='PNG')
    return bio.getvalue()
  else:
    nmol = rdMolDraw2D.PrepareMolForDrawing(mol, kekulize=kekulizeStructures)
    d2d = rdMolDraw2D.MolDraw2DCairo(molSize[0], molSize[1])
    d2d.DrawMolecule(nmol, highlightAtoms=highlightAtoms)
    d2d.FinishDrawing()
    return d2d.GetDrawingText()
Exemplo n.º 3
0
def _toPNG(mol):
  if hasattr(mol, '__sssAtoms'):
    highlightAtoms = mol.__sssAtoms
  else:
    highlightAtoms = []
  try:
    mol.GetAtomWithIdx(0).GetExplicitValence()
  except RuntimeError:
    mol.UpdatePropertyCache(False)

  if not hasattr(rdMolDraw2D, 'MolDraw2DCairo'):
    mc = copy.deepcopy(mol)
    try:
      img = Draw.MolToImage(mc, size=molSize, kekulize=kekulizeStructures,
                            highlightAtoms=highlightAtoms)
    except ValueError:  # <- can happen on a kekulization failure
      mc = copy.deepcopy(mol)
      img = Draw.MolToImage(mc, size=molSize, kekulize=False, highlightAtoms=highlightAtoms)
    bio = BytesIO()
    img.save(bio, format='PNG')
    return bio.getvalue()
  else:
    nmol = rdMolDraw2D.PrepareMolForDrawing(mol, kekulize=kekulizeStructures)
    d2d = rdMolDraw2D.MolDraw2DCairo(molSize[0], molSize[1])
    d2d.DrawMolecule(nmol, highlightAtoms=highlightAtoms)
    d2d.FinishDrawing()
    return d2d.GetDrawingText()
Exemplo n.º 4
0
def _toReactionPNG(rxn):
  rc = copy.deepcopy(rxn)
  img = Draw.ReactionToImage(rc, subImgSize=(int(molSize[0] / 3), molSize[1]),
                             highlightByReactant=highlightByReactant)
  bio = BytesIO()
  img.save(bio, format='PNG')
  return bio.getvalue()
Exemplo n.º 5
0
def _toPNG(mol):
    if hasattr(mol, '__sssAtoms'):
        highlightAtoms = mol.__sssAtoms
    else:
        highlightAtoms = []
    try:
        mol.GetAtomWithIdx(0).GetExplicitValence()
    except RuntimeError:
        mol.UpdatePropertyCache(False)

    mc = copy.deepcopy(mol)
    try:
        img = Draw.MolToImage(mc,
                              size=molSize,
                              kekulize=kekulizeStructures,
                              highlightAtoms=highlightAtoms)
    except ValueError:  # <- can happen on a kekulization failure
        mc = copy.deepcopy(mol)
        img = Draw.MolToImage(mc,
                              size=molSize,
                              kekulize=False,
                              highlightAtoms=highlightAtoms)

    bio = BytesIO()
    img.save(bio, format='PNG')
    return bio.getvalue()
Exemplo n.º 6
0
def getStreamIO(sdfString):
    """ Return a StringIO/BytesIO for the string """
    if PY3:
        sio = BytesIO() if sdfString is None else BytesIO(
            sdfString.encode('utf-8'))
    else:  # pragma: nocover
        sio = StringIO() if sdfString is None else StringIO(sdfString)
    return sio
Exemplo n.º 7
0
def SaveXlsxFromFrame(frame, outFile, molCol='ROMol', size=(300, 300)):
    """
    Saves pandas DataFrame as a xlsx file with embedded images.
    It maps numpy data types to excel cell types:
    int, float -> number
    datetime -> datetime
    object -> string (limited to 32k character - xlsx limitations)

    Cells with compound images are a bit larger than images due to excel.
    Column width weirdness explained (from xlsxwriter docs):
    The width corresponds to the column width value that is specified in Excel.
    It is approximately equal to the length of a string in the default font of Calibri 11.
    Unfortunately, there is no way to specify "AutoFit" for a column in the Excel file format.
    This feature is only available at runtime from within Excel.
    """

    import xlsxwriter  # don't want to make this a RDKit dependency

    cols = list(frame.columns)
    cols.remove(molCol)
    dataTypes = dict(frame.dtypes)

    workbook = xlsxwriter.Workbook(outFile)  # New workbook
    worksheet = workbook.add_worksheet()  # New work sheet
    worksheet.set_column('A:A', size[0] / 6.)  # column width

    # Write first row with column names
    c2 = 1
    for x in cols:
        worksheet.write_string(0, c2, x)
        c2 += 1

    c = 1
    for _, row in frame.iterrows():
        image_data = BytesIO()
        img = Draw.MolToImage(row[molCol], size=size)
        img.save(image_data, format='PNG')

        worksheet.set_row(c, height=size[1])  # looks like height is not in px?
        worksheet.insert_image(c, 0, "f", {'image_data': image_data})

        c2 = 1
        for x in cols:
            if str(dataTypes[x]) == "object":
                worksheet.write_string(
                    c, c2,
                    str(row[x])[:32000])  # string length is limited in xlsx
            elif ('float' in str(dataTypes[x])) or ('int' in str(
                    dataTypes[x])):
                if (row[x] != np.nan) or (row[x] != np.inf):
                    worksheet.write_number(c, c2, row[x])
            elif 'datetime' in str(dataTypes[x]):
                worksheet.write_datetime(c, c2, row[x])
            c2 += 1
        c += 1

    workbook.close()
    image_data.close()
Exemplo n.º 8
0
def _get_image(x):
  """displayhook function for PIL Images, rendered as PNG"""
  bio = BytesIO()
  x.save(bio, format='PNG')
  s = b64encode(bio.getvalue()).decode('ascii')
  pd.set_option('display.max_columns', len(s) + 1000)
  pd.set_option('display.max_rows', len(s) + 1000)
  if len(s) + 100 > pd.get_option("display.max_colwidth"):
    pd.set_option("display.max_colwidth", len(s) + 1000)
  return s
Exemplo n.º 9
0
def _get_image(x):
  """displayhook function for PIL Images, rendered as PNG"""
  bio = BytesIO()
  x.save(bio, format='PNG')
  s = b64encode(bio.getvalue()).decode('ascii')
  pd.set_option('display.max_columns', len(s) + 1000)
  pd.set_option('display.max_rows', len(s) + 1000)
  if len(s) + 100 > pd.get_option("display.max_colwidth"):
    pd.set_option("display.max_colwidth", len(s) + 1000)
  return s
Exemplo n.º 10
0
def SaveXlsxFromFrame(frame, outFile, molCol='ROMol', size=(300, 300)):
  """
    Saves pandas DataFrame as a xlsx file with embedded images.
    It maps numpy data types to excel cell types:
    int, float -> number
    datetime -> datetime
    object -> string (limited to 32k character - xlsx limitations)

    Cells with compound images are a bit larger than images due to excel.
    Column width weirdness explained (from xlsxwriter docs):
    The width corresponds to the column width value that is specified in Excel.
    It is approximately equal to the length of a string in the default font of Calibri 11.
    Unfortunately, there is no way to specify "AutoFit" for a column in the Excel file format.
    This feature is only available at runtime from within Excel.
    """

  import xlsxwriter  # don't want to make this a RDKit dependency

  cols = list(frame.columns)
  cols.remove(molCol)
  dataTypes = dict(frame.dtypes)

  workbook = xlsxwriter.Workbook(outFile)  # New workbook
  worksheet = workbook.add_worksheet()  # New work sheet
  worksheet.set_column('A:A', size[0] / 6.)  # column width

  # Write first row with column names
  c2 = 1
  for x in cols:
    worksheet.write_string(0, c2, x)
    c2 += 1

  c = 1
  for _, row in frame.iterrows():
    image_data = BytesIO()
    img = Draw.MolToImage(row[molCol], size=size)
    img.save(image_data, format='PNG')

    worksheet.set_row(c, height=size[1])  # looks like height is not in px?
    worksheet.insert_image(c, 0, "f", {'image_data': image_data})

    c2 = 1
    for x in cols:
      if str(dataTypes[x]) == "object":
        worksheet.write_string(c, c2, str(row[x])[:32000])  # string length is limited in xlsx
      elif ('float' in str(dataTypes[x])) or ('int' in str(dataTypes[x])):
        if (row[x] != np.nan) or (row[x] != np.inf):
          worksheet.write_number(c, c2, row[x])
      elif 'datetime' in str(dataTypes[x]):
        worksheet.write_datetime(c, c2, row[x])
      c2 += 1
    c += 1

  workbook.close()
  image_data.close()
Exemplo n.º 11
0
def _get_image(x):
    """displayhook function for PIL Images, rendered as PNG"""
    import pandas as pd

    bio = BytesIO()
    x.save(bio, format="PNG")
    s = b64encode(bio.getvalue()).decode("ascii")
    pd.set_option("display.max_columns", len(s) + 1000)
    pd.set_option("display.max_rows", len(s) + 1000)
    if len(s) + 100 > pd.get_option("display.max_colwidth"):
        pd.set_option("display.max_colwidth", len(s) + 1000)
    return s
Exemplo n.º 12
0
  def testSaveState(self):
    fName = os.path.join(RDConfig.RDCodeDir, 'ML/Descriptors/test_data', 'molcalc.dsc')
    with open(fName, 'r') as inTF:
      buf = inTF.read().replace('\r\n', '\n').encode('utf-8')
      inTF.close()
    inF = BytesIO(buf)
    calc = cPickle.load(inF)
    self.assertEqual(calc.GetDescriptorNames(), tuple(self.descs))
    self.assertEqual(calc.GetDescriptorVersions(), tuple(self.vers))
    self._testVals(calc, self.testD)

    f = StringIO()
    with redirect_stdout(f):
      calc.ShowDescriptors()
    s = f.getvalue()
    for name in calc.GetDescriptorNames():
      self.assertIn(name, s)

    self.assertIn('Wildman-Crippen LogP value', calc.GetDescriptorSummaries())
    self.assertIn('N/A', calc.GetDescriptorSummaries())

    funcs = calc.GetDescriptorFuncs()
    self.assertEqual(len(funcs), len(self.descs))
    for f in funcs:
      self.assertTrue(callable(f))
Exemplo n.º 13
0
 def _loadPackage(self):
     with open(os.path.join(self.dataDir, 'Jan9_build3_pkg.pkl'),
               'r') as pkgTF:
         buf = pkgTF.read().replace('\r\n', '\n').encode('utf-8')
         pkgTF.close()
     io = BytesIO(buf)
     pkg = cPickle.load(io)
     return pkg
Exemplo n.º 14
0
def _DrawBits(fn, *args, **kwargs):
  if 'useSVG' not in kwargs:
    kwargs['useSVG'] = ipython_useSVG
  res = fn(*args, **kwargs)
  if kwargs['useSVG']:
    return SVG(res)
  else:
    sio = BytesIO(res)
    return Image.open(sio)
Exemplo n.º 15
0
def _toPNG(mol):
    if hasattr(mol,'__sssAtoms'):
        highlightAtoms=mol.__sssAtoms
    else:
        highlightAtoms=[]
    try:
        mol.GetAtomWithIdx(0).GetExplicitValence()
    except RuntimeError:
        mol.UpdatePropertyCache(False)

    mc = copy.deepcopy(mol)
    try:
        img = Draw.MolToImage(mc,size=molSize,kekulize=kekulizeStructures,
                            highlightAtoms=highlightAtoms)
    except ValueError:  # <- can happen on a kekulization failure
        mc = copy.deepcopy(mol)
        img = Draw.MolToImage(mc,size=molSize,kekulize=False,
                            highlightAtoms=highlightAtoms)
    bio = BytesIO()
    img.save(bio,format='PNG')
    return bio.getvalue()
Exemplo n.º 16
0
 def testBuild(self):
     # """ tests building and screening a packager """
     with open(os.path.join(self.dataDir, 'Jan9_build3_calc.dsc'),
               'r') as calcTF:
         buf = calcTF.read().replace('\r\n', '\n').encode('utf-8')
         calcTF.close()
     calc = cPickle.load(BytesIO(buf))
     with open(os.path.join(self.dataDir, 'Jan9_build3_model.pkl'),
               'rb') as modelF:
         model = cPickle.load(modelF)
     pkg = Packager.ModelPackage(descCalc=calc, model=model)
     self._verify(pkg, self.testD)
Exemplo n.º 17
0
    def testResults(self):
        # " test the results of CrossValidation "
        RDRandom.seed(self.randomSeed)
        examples, attrs, nPossibleVals = randomtest.GenRandomExamples(
            nExamples=200, seed=self.randomArraySeed)
        tree, frac = CrossValidate.CrossValidationDriver(examples,
                                                         attrs,
                                                         nPossibleVals,
                                                         silent=1)
        self.assertGreater(frac, 0)

        with open(self.origTreeName, 'r') as inTFile:
            buf = inTFile.read().replace('\r\n', '\n').encode('utf-8')
            inTFile.close()
        inFile = BytesIO(buf)
        oTree = cPickle.load(inFile)

        assert oTree == tree, 'Random CrossValidation test failed'
Exemplo n.º 18
0
def display_pil_image(img):
  """displayhook function for PIL Images, rendered as PNG"""
  bio = BytesIO()
  img.save(bio, format='PNG')
  return bio.getvalue()
Exemplo n.º 19
0
def display_pil_image(img):
    """displayhook function for PIL Images, rendered as PNG"""
    bio = BytesIO()
    img.save(bio,format='PNG')
    return bio.getvalue()