I have a list of SMILES in a csv file (about 900 of them) that I would like to convert to individual images (200 x 200). I know I can run this:
from rdkit import Chem
from rdkit.Chem import Draw
import matplotlib.pyplot as plt
%matplotlib inline
smiles = 'C1CC[13CH2]CC1C1CCCCC1'
mol = Chem.MolFromSmiles(smiles)
Draw.MolToMPL(mol, size=(200, 200))
and get one image out at a time but all my attempts to put it into a for loop (using a list or reading in a csv) has failed.
My for loop looks like:
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem import Descriptors
from rdkit.Chem import AllChem
import PIL
from PIL import Image
smiles_list = ['ClCC(=O)N1CCc2ccc(cc12)N(=O)=O',
'ClCC(=O)N1CCc2ccc(cc12)N(=O)=O', 'ClCC(=O)Nc1nc(cs1)-c1ccco1']
mol_list = []
for smiles in smiles_list:
mol = Chem.MolFromSmiles(smiles)
mol_list.append(mol)
Draw.MolToMPL(mol_list)
Which has been giving me error:
AttributeError Traceback (most recent call last)
<ipython-input-3-83978cee41c7> in <module>
15 mol_list.append(mol)
16
---> 17 Draw.MolToMPL(mol_list)
~\Anaconda3\envs\my-rdkit-env\lib\site-packages\rdkit\Chem\Draw\__init__.py in MolToMPL(mol, size, kekulize, wedgeBonds, imageType, fitImage, options, **kwargs)
346 if kekulize:
347 from rdkit import Chem
--> 348 mol = Chem.Mol(mol.ToBinary())
349 Chem.Kekulize(mol)
350
AttributeError: 'list' object has no attribute 'ToBinary'
Does anyone know of any rdkit tricks?
Thank you Oliver Scott and Quat for the comments. Can get exactly what I need now! For anyone who is interested:
smiles_list = ['ClCC(=O)N1CCc2ccc(cc12)N(=O)=O', 'ClCC(=O)N1CCc2ccc(cc12)N(=O)=O',
'ClCC(=O)Nc1nc(cs1)-c1ccco1']
mol_list = []
for smiles in smiles_list:
mol = Chem.MolFromSmiles(smiles)
mol_list.append(mol)
Draw.MolToMPL(mol, size=(200, 200))
Or simply replace smiles_list with a csv read-in:
smiles_list = pd.read_csv('test.csv')
mol_list = []
for smiles in smiles_list:
for smiles in smiles_list['Smiles']: # assuming your column is called Smiles
mol = Chem.MolFromSmiles(smiles)
mol_list.append(mol)
Draw.MolToMPL(mol, size=(200, 200))
Related
I'm using this code but it dosen't work. could youn tell me what is the problem?
import glob , cv2
import numpy as np
def read_img(img_list , img):
n=cv2.imread(img)
img_list.append(n)
return img_list
path = glob.glob("02291G0AR/*.bmp")
list_ = []
cv_image = [read_img(list_,img) for img in path]
for img in cv_image:
cv2.imshow('image',img)
and the error is:
cv2.imshow('image',img)
TypeError: mat is not a numpy array, neither a scalar
I think you'll get on better with something like this:
#!/usr/bin/env python3
import glob , cv2
import numpy as np
# Load an image by name and return as Numpy array
def read_img(name):
img=cv2.imread(name)
return img
# Generate list of all image names
names = glob.glob("*.bmp")
# Load all images into list
images = [read_img(name) for name in names]
# Display all images in list
for img in images:
cv2.imshow('image',img)
cv2.waitKey()
I am trying to develop a python app for a RaspberryPi on a PC. In order to test one aspect, I have written an object class to emulate an OLED display device (hardware) that the PC cannot use (no I2C). The Wdisp class is simply loaded in place of the hardware driver when using the PC.
I have another class that wraps up the few OLED functions I need ( write text , draw progressbar etc. )
# OLED display object
import os
Win = False
if os.name == 'nt':
Win=True
from WindowsOLED import Wdisp
else:
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
class OLEDDisplay(object):
def __init__(self):
# Raspberry Pi pin configuration:
RST = 24
if Win:
self.disp = Wdisp()
else:
self.disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Initialize library.
self.disp.begin()
# Create blank image for drawing.
self.width = 128 #self.disp.width
self.height = 64 #self.disp.height
self._image = Image.new('1', (self.width, self.height))
self.draw = ImageDraw.Draw(self._image)
# Load default font.
self.font = ImageFont.load_default()
self.clear()
def __del__(self):
self.clear()
def __repr__(self):
return 'OLED display object for SSD1306 I2C'
def clear(self):
# Clear display
self.disp.clear()
self.disp.display()
def write(self, text):
self.draw.text((0, 2), text, font=self.font, fill=255)
self._show()
def progressbar(self, percent):
#draw a progress bar on top half of screen
shape_width = self.width * (percent/100)
padding =2
top = padding
bottom = (self.height/2) - padding
x = padding
self.draw.rectangle((x, top, self.width-padding, bottom), outline=255, fill=0)
self.draw.rectangle((x, top, x+shape_width, bottom), outline=255, fill=255)
self._show()
def loadimage(self, imagefile):
#open, convert and display an image
self._image = Image.open(imagefile).resize((self.disp.width,
self.disp.height),
Image.ANTIALIAS).convert('1')
self._show()
def _show(self):
# Display image.
self.disp.image(self._image)
self.disp.display()
If I test this class at the command line
Example:
>>> from OLEDDisplay import*
>>> OD = OLEDDisplay()
>>> OD.progressbar(50)
Its works fine - the OLEDDisplay object draws on a PIL image object, passes the image to the Wdisp object and the Wdisp object converts the image to a BitMapImage and displays in on a Tkinter Label ( self.L ).
#Windows subsitute OLED display class
#displays a label image for testing
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog, messagebox
from tkinter.constants import *
class Wdisp(object):
i=None
width = 128
height =64
_image = None
def __init__(self):
self.master = Tk()
self._image = Image.new('1', (self.width, self.height))
self.i = ImageTk.PhotoImage(self._image)
self.L= Label(self.master, text="label place holder", image=self.i)
self.L.pack(expand=YES, fill=BOTH)
def begin(self):
pass
def image(self, newimage):
self._image = newimage
def display(self):
#display the image
self.i = ImageTk.BitmapImage(self._image)
self.L.config(image=self.i)
However, if I use this in my app or test it in a small app:
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog, messagebox
from tkinter.constants import *
from OLEDDisplay import*
def updateOLED():
text = E.get
print(text)
OD.write(text)
master = Tk()
OD = OLEDDisplay()
E = Entry(master)
B = Button(master, text="update OLED", command = updateOLED)
E.pack()
B.pack()
It fails with a tkinter.TclError: image "pyimage4" doesn't exist:
Traceback (most recent call last):
File "D:\My Documents\PI Projects\Pyprojects\PTPIC\OLEDDisplay test.py", line 19, in <module>
OD = OLEDDisplay()
File "D:\My Documents\PI Projects\Pyprojects\PTPIC\OLEDDisplay.py", line 42, in __init__
self.clear()
File "D:\My Documents\PI Projects\Pyprojects\PTPIC\OLEDDisplay.py", line 52, in clear
self.disp.clear()
File "D:\My Documents\PI Projects\Pyprojects\PTPIC\WindowsOLED.py", line 45, in clear
self.display()
File "D:\My Documents\PI Projects\Pyprojects\PTPIC\WindowsOLED.py", line 36, in display
self.L.config(image=self.i)
File "C:\Python34\lib\tkinter\__init__.py", line 1324, in configure
return self._configure('configure', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 1315, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage4" doesn't exist
I have no idea why : perhaps variable scope? perhaps something weird with PIL , so any suggestions welcome.
Cheers Bill
I have seen many examples of how to show images in Tkinter using an image URL but none of those examples are working for me i.e.
import urllib
from Tkinter import *
import io
from PIL import Image, ImageTk
app = Tk()
app.geometry("1000x800")
im = None #<-- im is global
def search():
global im #<-- declar im as global, so that you can write to it
# not needed if you only want to read from global variable.
tx1get = tx1.get()
Label(app, text="You Entered: \"" + tx1get + "\"").grid(row=1, column=0)
fd = urllib.urlopen("http://ia.media-imdb.com/images/M/MV5BMTc2MTU4ODI5MF5BMl5BanBnXkFtZTcwODI2MzAyOA##._V1_SY317_CR7,0,214,317_AL_.jpg")
imgFile = io.BytesIO(fd.read())
im = ImageTk.PhotoImage(Image.open(imgFile))
image = Label(app, image = im, bg = "blue")
image.grid(row=2, column=0)
tx1=StringVar()
tf = Entry(app, textvariable=tx1, width="100")
b1 = Button(app, text="Search", command=search, width="10")
tf.grid(row=0, column=0)
b1.grid(row=0, column=1)
app.mainloop()
When I run this I get the error "No module name PIL" And in this one:
from io import BytesIO
import urllib
import urllib.request
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
url = "http://imgs.xkcd.com/comics/python.png"
with urllib.request.urlopen(url) as u:
raw_data = u.read()
im = Image.open(BytesIO(raw_data))
image = ImageTk.PhotoImage(im)
label = tk.Label(image=image)
label.pack()
root.mainloop()
I get "No module name request" Nearly all examples make use of the PIL module among others, but I cannot get them to work because Python 2.7 doesn't recognize many of them. I need to display an image as a part of an assessment and while we can import things such as Tkinter, the file needs to run without having to add modules from outside the standard Python library.
It is worth noting as well that I can't even import "tkinter". It will say there is no module named "tkinter" because it needs to start with a capital "T".
So my questions are:
Does PIL need me to install additional software/libraries
Does the importation of "tkinter" without the capital "T" not work because I am using Python 2.7?
Using Python 2.7 how do I display an image in a Tkinter window from a URL
This works, using python 2.7 on windows:
from io import BytesIO
import Tkinter as tk
import urllib # not urllib.request
from PIL import Image, ImageTk
root = tk.Tk()
url = "http://imgs.xkcd.com/comics/python.png"
u = urllib.urlopen(url)
raw_data = u.read()
u.close()
im = Image.open(BytesIO(raw_data))
image = ImageTk.PhotoImage(im)
label = tk.Label(image=image)
label.pack()
root.mainloop()
To answer your questions:
You need to install PIL (it's not standard on Python 2.7).
Yes, you need to import Tkinter in Python 2.7; tkinter is for Python 3.x
You can use the code above (provided you install PIL).
Also,
In Python 2.7 you need urllib and not urllib.request
Seems you can't use with....open(x) as fnamein urllib so you need to open and close the file explicitly.
import matplotlib
from tkinter import *
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
import tkinter as tk
from tkinter import ttk
import numpy as np
import matplotlib.animation as animation
from matplotlib import style
from matplotlib import pyplot as plt
from scipy.misc import *
import webbrowser
import subprocess
import csv
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from os import startfile
LARGE_FONT = ("Times", 11, "bold italic")
NORM_FONT = ("Helvetica", 9)
SMALL_FONT = ("Helvetica",7)
HELP_FONT=("Times", 9 , "bold")
class PAL3_guide(tk.Tk):
def __init__(self,*args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage,Tut01):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame=self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self,parent, controller):
tk.Frame.__init__(self,parent, background="white")
button_PAL = ttk.Button(self, text="Setup Guide", command=lambda:controller.show_frame(Tut01))
button_PAL.pack()
class Tut01(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
var=BooleanVar()
label = tk.Label(self, text="Guide", font=LARGE_FONT)
label.pack(pady=10, padx=10)
label = tk.Label(self, text="Check the methods to accomplish", font=NORM_FONT)
label.pack(pady=10, padx=10)
c1 = Checkbutton(self, state=ACTIVE).pack()
c1Label = tk.Label(self, text="digestion1",font=SMALL_FONT).pack()
c2 = Checkbutton(self, state=ACTIVE).pack()
c2Label = tk.Label(self, text="digestion2",font=SMALL_FONT).pack()
c3 = Checkbutton(self, text="enrichment",state=ACTIVE, variable=var, command=lambda:onClick()).pack()
def onClick():
var.get()
if var == True:
print("hi")
else:
print("hiww")
app = PAL3_guide()
app.geometry("1280x920")
app.mainloop()
Please check last code lines around the c3 checkbox.
I dont get why my boolean variable var is not getting updated by clicking the checkbox on the 2nd page. By clicking the checkbox (c3) the GUI should act differently. However as mentioned above it is not getting updated.. and stays False. I feel it keeps the value from start and does not get changed. Thanks for help ..
EDIT:
Above the if statement missed the var.get() and I could make it work in smaller setup.. however, in the real tool I am calling a plt.figure() in the beginning since it included and anmiate function, If I erase this call, everything works like it should, might be that this figure calling forces the checkbutton to have the first onvalue ?
I would change var, to be self.var and set the off and on values explicitly:
offvalue=
The value corresponding to a non-checked button. The default is 0. (offValue/Value)
onvalue=
The value corresponding to a checked button. The default is 1. (onValue/Value)
c3 = Checkbutton(self, text="enrichment",state=ACTIVE,
variable=self.var, command=onClick,
onvalue=True, offvalue=False)
c3.pack()
command= does not take a function call, just a function.
You would have to write either
command=lambda: print(self.var.get())
OR:
def printVar():
print(self.var.get())
THEN:
..., command=printVar, ...
NOTICE the abscence of the paranthesis!
You are supposed to be settting command to a function.
When you use parenthesis, you CALL the function, you DO NOT want that here.
I'm working on a GUI that basically hold multiple widgets that each contain a figure as well as a few buttons/whatever. One of the figures is supposed to be interactive, calling a function whenever the user clicks on any part of the plot. Yet I can't get the function to fire using mpl_connect, even after playing with focus and whatnot. I'm somewhat new to PySide/Qt, so I don't exactly understand why my code is behaving like this (I've been searching for days for a solution, but haven't found anything about it).
I used Qt Designer to create the layout for the GUI. I'm using Spyder from Anaconda 2.2.0 (32-bit), Python 2.7, and PySide to develop the GUI. If it's any help, I come from more of a MATLAB background where I developed a full version of the GUI I'm trying to make in Python.
Below is the relevant code (scroll down a bit to see where the problem is):
from PySide import QtCore, QtGui
from PySide.QtCore import *
from PySide.QtGui import *
import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt
from PySide.QtGui import QPalette, QCursor
import matplotlib.colors as colors
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1316, 765)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.widget = QtGui.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(75, 40, 375, 490))
self.widget.setObjectName("widget")
color = self.centralwidget.palette().color(QPalette.Window)
self.leftPlot = MatplotlibWidget(None,'','','',False,color)
self.setupPlot(self.widget,self.leftPlot)
self.leftPlot.figure.tight_layout()
self.leftImage = self.leftPlot.axes.imshow(self.defaultSlide, cmap = mymap)
Snippet of interest:
self.leftPlot.figure.canvas.setFocusPolicy(QtCore.Qt.StrongFocus)
self.leftPlot.figure.canvas.setFocus()
cid = self.leftPlot.figure.canvas.mpl_connect('button_release_event', self.getCoordinates) # doesn't get called
plt.show()
def getCoordinates(self, event):
print 'dasdsadadsa'
print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(event.button, event.x, event.y, event.xdata, event.ydata)
The rest:
class MatplotlibWidget(FigureCanvas):
def __init__(self, parent=None,xlabel='x',ylabel='y',title='Title',showTicks=False,color=None):
super(MatplotlibWidget, self).__init__(Figure())
self.setParent(parent)
if color != None:
self.figure = Figure(facecolor=(color.red()/256.0,color.green()/256.0,color.blue()/256.0),frameon=0)
else:
self.figure = Figure(frameon=0)
self.canvas = FigureCanvas(self.figure)
self.axes = self.figure.add_subplot(111)
self.axes.set_xlabel(xlabel)
self.axes.set_ylabel(ylabel)
self.axes.set_title(title)
self.axes.get_xaxis().set_visible(showTicks)
self.axes.get_yaxis().set_visible(showTicks)
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
plt.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mySW = ControlMainWindow()
mySW.show()
sys.exit(app.exec_())
I'm aware the code is messy, but any input is greatly appreciated.
Update (2015-09-04) : I've updated the MWE I provided as part of my original answer to use instead the approach that is suggested in the matplotlib documentation to embed a mpl figure in an application. This approach does not use the pyplot interface (as in my original answer) and use the Object Oriented API of mpl instead. Also, since all the mpl artists (figure, axes, etc.) know each other, there is no need to explicitly create new class variables. This allows a structure of code that is, IMO, easier to read and to maintain.
The problem comes from the fact that you are not connecting correctly your event to self.leftPlot (FigureCanvasQTAgg), but to self.leftPlot.figure.canvas (FigureCanvasQTAgg.figure.FigureCanvasQTAgg) instead. You are creating a canvas within a canvas in the MatplotlibWidget class (which is already a subclass of FigureCanvasQTAgg). You only need to create one mpl canvas, pass a figure to it, and then connect the event to it directly.
I've put together a MWE to demonstrate how this can be done using the Object Oriented API of Matplotlib as suggested in the documentation:
from PySide import QtGui
import numpy as np
import sys
import matplotlib as mpl
mpl.use('Qt4Agg')
mpl.rcParams['backend.qt4']='PySide'
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
self.setupUi()
def setupUi(self):
figure = mpl.figure.Figure(figsize=(5, 5))
leftPlot = MatplotlibWidget(figure)
self.setCentralWidget(leftPlot)
class MatplotlibWidget(FigureCanvasQTAgg):
def __init__(self, fig):
super(MatplotlibWidget, self).__init__(fig)
#-- set up an axe artist --
ax = fig.add_axes([0.1, 0.1, 0.85, 0.85])
ax.plot(np.arange(15), np.arange(15))
self.draw()
#---- setup event ----
self.mpl_connect('button_press_event', self.onclick)
def onclick(self, event):
x, y = event.x, event.y
print(x, y)
if x != None and y != None:
ax = self.figure.axes[0]
ax.plot(event.xdata, event.ydata, 'ro')
self.draw()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mySW = ControlMainWindow()
mySW.show()
sys.exit(app.exec_())
The code above results in: