"""
Molecule images.
"""

__author__ = "Steven Kearnes"
__copyright__ = "Copyright 2014, Stanford University"
__license__ = "BSD 3-clause"

import os
import subprocess

from rdkit import Chem

from pande_gas.features import Featurizer
from pande_gas.utils import image_utils


class MolImage(Featurizer):
    """
    Molecule images.

    Parameters
    ----------
    size : int, optional (default 32)
        Size (in any dimension) of generated images.
    flatten : bool, optional (default False)
        Whether to flatten the pixel array. If False, the features for each
        molecule will be a 3D array.
    """
    name = 'image'

    def __init__(self, size=32, flatten=False):
        self.size = size
        if not flatten:
            self.topo_view = True
        self.flatten = flatten

    def _featurize(self, mol):
        """
        Generate a 2D depiction of a molecule.

        Parameters
        ----------
        mol : RDKit Mol
            Molecule.
        """
        smiles = Chem.MolToSmiles(mol, isomericSmiles=True, canonical=True)
        image = self.image_from_smiles(smiles)
        pixels = image_utils.get_pixels(image)
        if self.flatten:
            pixels = pixels.ravel()
        return pixels

    def image_from_smiles(self, smiles):
        """
        Generate a PNG image from a SMILES string using OpenBabel.

        Note that we call OpenBabel using Popen to avoid entanglements with
        the GNU GPL.

        See http://stackoverflow.com/questions/13332268 for details on the
        echo function.

        Parameters
        ----------
        smiles : str
            SMILES string.
        """
        devnull = open(os.devnull, 'w')
        echo = lambda string: subprocess.Popen(['echo', string],
                                               stdout=subprocess.PIPE).stdout
        png_args = ['obabel', '-ican', '-opng', '-xd', '-xC',
                    '-xp {}'.format(self.size)]
        png = subprocess.check_output(png_args, stdin=echo(smiles),
                                      stderr=devnull)
        im = image_utils.load(png)
        return im

@@ -1402,21 +1402,25 @@
 l using 
-Popen
+subprocss
  to avoi
@@ -1438,13 +1438,8 @@
 ents
- with
 %0A   
@@ -1443,16 +1443,21 @@
         
+with 
 the GNU 
@@ -1466,107 +1466,8 @@
 L.%0A%0A
-        See http://stackoverflow.com/questions/13332268 for details on the%0A        echo function.%0A%0A
     
@@ -1604,151 +1604,8 @@
 w')%0A
-        echo = lambda string: subprocess.Popen(%5B'echo', string%5D,%0A                                               stdout=subprocess.PIPE).stdout%0A
     
@@ -1719,18 +1719,16 @@
        p
-ng
  = subpr
@@ -1737,20 +1737,13 @@
 ess.
-check_output
+Popen
 (png
@@ -1759,74 +1759,78 @@
 din=
-echo(smiles),%0A                                      stderr=devnull
+subprocess.PIPE, stderr=devnull)%0A        png, _ = p.communicate(smiles
 )%0A  

Make sure subprocess waits