Skip to content

Commit

Permalink
Fixed broken selection of random depiction settings and removed tempo…
Browse files Browse the repository at this point in the history
…rary file creation when creating Indigo depictions
  • Loading branch information
OBrink committed Sep 27, 2021
1 parent 89afb68 commit 7932158
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions RanDepict/RanDepict.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def get_random_indigo_rendering_settings(
using Indigo. It returns an Indigo object with the settings."""
# Define random shape for depiction (within boundaries); image is resized later)
indigo = Indigo()
renderer = IndigoRenderer(indigo)
# Get slightly distorted shape
y, x = self.random_image_size(shape)
indigo.setOption("render-image-width", x)
Expand Down Expand Up @@ -178,7 +179,7 @@ def get_random_indigo_rendering_settings(
# Collapse superatoms (default: expand)
if self.random_choice([True, False]):
indigo.setOption("render-superatom-mode", "collapse")
return indigo
return indigo, renderer

def depict_and_resize_indigo(
self, smiles: str, shape: Tuple[int, int] = (299, 299)
Expand All @@ -187,26 +188,19 @@ def depict_and_resize_indigo(
using Indigo with random rendering/depiction settings and returns an RGB image (np.array)
with the given image shape."""
# Instantiate Indigo with random settings and IndigoRenderer
try:
indigo = self.get_random_indigo_rendering_settings()
except IndigoException:
# This happens the first time something is depicted, I don't know why.
indigo = Indigo()
renderer = IndigoRenderer(indigo)
#try:
indigo, renderer = self.get_random_indigo_rendering_settings()

# Load molecule
molecule = indigo.loadMolecule(smiles)
# Do not kekulize in 20% of cases
if self.random_choice([True, True, False, False, False, False]):
molecule.aromatize()
molecule.layout()
# Create structure depiction, save in temporary file and load as Pillow image
# TODO: Do this without saving file (renderToBuffer format does not seem to work)
if not os.path.exists("./temp/"):
os.mkdir("./temp/")
tmp_filename = self.get_nonexisting_image_name(path="./temp/", format="png")
renderer.renderToFile(molecule, os.path.join("./temp/", tmp_filename))
depiction = sk_io.imread(os.path.join("./temp/", tmp_filename))
os.remove(os.path.join("./temp/", tmp_filename))
# Write to buffer
temp = renderer.renderToBuffer(molecule)
temp = io.BytesIO(temp)
depiction = sk_io.imread(temp)
depiction = resize(depiction, (shape[0], shape[1], 4))
depiction = rgba2rgb(depiction)
depiction = img_as_ubyte(depiction)
Expand Down

0 comments on commit 7932158

Please sign in to comment.