Is there anyway to call PubChem API In python? - python-3.x

I have been using PubChem API to convert Chemical smiles to the structure but still have an error.
Here is my google colab I try with PIL image plus TKinter
https://colab.research.google.com/drive/1TE9WxXwaWKSLQzKRQoNlWFqztVSoIxB7
My desired output should be in structure format like this
https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/O=C(N1C=CN=C1)N2C=CN=C2/PNG?record_type=2d&image_size=large

Download and display in a Jupyter Notebook
from urllib.request import urlretrieve
from IPython.display import Image
smiles = 'NC1=NC(C)=C(C2=CC=C(S(=O)(C)=O)C(F)=C2)S1'
urlretrieve('https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/'+smiles+'/PNG', 'smi_pic.png')
p = Image(filename='smi_pic.png')
p
Output

Related

Import Image from Google Drive to Google Colab

I have mounted my Google drive to my colab notebook:
from google.colab import drive
drive.mount("/content/gdrive")
I can import csv files through it, in my case:
df = pd.read_csv("/content/gdrive/MyDrive/colab/heart-disease.csv")
But when I try to import images in markdown/text in colab
nothing happens:
![](/content/gdrive/MyDrive/colab/6-step-ml-framework.png)
Here's my directory on Google drive:
You can using OpenCV in colab by import cv2 as cv, read image by img = cv.imread('/content/gdrive/MyDrive/colab/6-step-ml-framework.png'), convert image to numpy array using img = np.float32(img)
#Mr. For Example
you should not use np.float(img), because OpenCV's method imread has converted it to np.array already

Attempting to convert an image to grayscale, or better, binary

My basic plan here is to create an image recognition software that tracks the size of different bubbles. I basically have a compilation of pictures that constitute a video. I have it working as of right now using PIMS to import the files I need and place them into an array (rawframes). I can print my picture.
import numpy as np
import pandas as pd
import pims
from pims import pipeline
import trackpy as tp
import os
import matplotlib as mpl
import matplotlib.pyplot as plt
#pipeline
def binary(frame):
return frame[:, :, 1]
id_example = 1
rawframes = pims.ImageSequence(os.path.join('BubbleSize/90FoamQuality/DryFoams', 'T20190411_002_ (*).jpg'), process_func=binary)
plt.imshow(rawframes[id_example])
What I am trying to do here is convert the images from regular into black and white. I have not used many of the things I imported yet I know, this is a very preliminary step.
However, below is a before and after image comparison. Can someone help me out or walk me through these steps here? I get lost when it comes to filtering the images through python.
edit --> when I change my pipeline function to the below, I get the same green image
edit2 --> printing frame.shape and frame.dtype in binary pipeline respectively

Folium map issue in PyCharm

I am using Pycharm and the folium maps are not being displayed. My code is as follows:
import folium
from folium import plugins
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.core.display import display, HTML
m = folium.Map([52.5, 2], zoom_start=5.5)
display(m)
But I am able to save it as html and it is correctly displayed in html.
m.save('aee.html')
You can't open the map directly in pycharm.
Yet, as you stated you can make an HTML File of it.
Here's a little function to auto-open it in browser:
def auto_open(path):
html_page = f'{path}'
f_map.save(html_page)
# open in browser.
new = 2
webbrowser.open(html_page, new=new)
I'm using fuliom like this in a Django app, it's quite useful as render tool as well.
Folium also provides ability to save the map to images, check it out.

Trying to pass a base_64 encoded image to google authentication

So i am working on a small project(a restful service so json format which is not mentioned in the code) in which the code accepts base_64 image data and decodes it to from an image ,i'm able to convert it back to image but i am not able to use google vision(googel ocr) on the image to extract the text . The only part that isn't working is the following block of code:
from flask import Flask,request,jsonify
import os,io,re,glob,base64
from google.cloud import vision
from google.cloud.vision import types
from PIL import Image
app = Flask(__name__)
os.environ['GOOGLE_APPLICATION_CREDENTIALS']=r'date_scanner.json'
#app.route('/jason_example',methods=['POST'])
def jason_example():
req_data=request.get_json()
base_64_image_content=req_data['imgcnt']
#the issue starts from here
image = base64.b64decode(base_64_image_content)
image=Image.open(io.BytesIO(image))
image=vision.types.Image(content=content)
response=client.text_detection(image=image)
texts=response.text_annotations`
enter code here
No need to use Image.open which I think is a PIL method anyway. You should be able to decode this straight to a byte string with base64.decodebytes, as outlined in this answer,
The code should look like:
# the issue starts from here
image_bytes = base64.decodebytes(base_64_image_content)
image = vision.types.Image(content=image_bytes)
response=client.text_detection(image=image)
texts=response.text_annotations

folium map not showing datbricks python

I am working on Databricks and have a folium map:
import geopandas as gpd
import matplotlib as plt
import os
import folium
from IPython.display import display
map_osm = folium.Map(location=[45.5236, -122.6750])
map_osm
I get the following:
<folium.folium.Map at 0x7f9978eec748>
I tried Folium map not displaying to no avail.
Any suggestions
Try this
import folium
import webbrowser
map_osm = folium.Map(location=[45.5236, -122.6750])
map_osm.save('map.html')
webbrowser.open('map.html')
The output of the function is a HTML file and Python IDLE fails to render the html document unless explicitly called. You can also try using the same code on Jupyter notebook which runs on a browser and can render html map at ease.
Turning the map into HTML then displaying worked for me in Databricks using Python 3.5
world_map = folium.Map()
html_map = world_map._repr_html_()
displayHTML(html_map)
The original answer came from Databricks forums by ShumZZ: https://forums.databricks.com/questions/444/how-to-create-maps-in-databricks.html

Resources