How can i write the unit test for tempfile? - python-unittest

import tempfile
def tmpdir(destdir=None):
return tempfile.mkdtemp(suffix='.' + str(os.getpid()), dir=destdir)

Related

not able to load the image and pass it to preprocessing for the model prediction

I am trying to upload the image from the local system within the same directory. Post uploading, when I am passing through open cv split and merge for b,g, and r colors, i get the error ValueError: not enough values to unpack (expected 3, got 0)
Error :
this is the error that is showing is there any possibility to debug in the streamlit where I can track changes at different lines of code? (As in the image path,) when executed in a google collab as individual ipynb files run properly and I get by required classification
ValueError: not enough values to unpack (expected 3, got 0)
Traceback:
File "C:\Users\ADARSH\anaconda3\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 564, in _run_script
exec(code, module.__dict__)
File "C:\Users\ADARSH\streamlit\deploy_test.py", line 76, in <module>
main()
File "C:\Users\ADARSH\streamlit\deploy_test.py", line 68, in main
mask = imageToTensor('image')
File "C:\Users\ADARSH\streamlit\deploy_test.py", line 44, in imageToTensor
b,g,r = cv2.split(bgr_img)
My entire streamlit app code
from pathlib import Path
import cv2
import numpy as np
import pandas as pd
import os
import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import random
from sklearn.utils import shuffle
from tqdm import tqdm_notebook
import streamlit as st
from PIL import Image as impo
from fastai import *
from fastai.vision import *
from torchvision.models import *
class MyImageItemList(ImageList):
def open(self, fn:PathOrStr)->Image:
img = readCroppedImage(fn.replace('/./','').replace('//','/'))
# This ndarray image has to be converted to tensor before passing on as fastai Image, we can use pil2tensor
return vision.Image(px=pil2tensor(img, np.float32))
def read_image(name):
image = st.file_uploader("Upload an "+ name, type=["png", "jpg", "jpeg",'tif'])
if image is not None:
im = impo.open(image)
im.filename = image.name
return image
def imageToTensor(image):
sz = 68
bgr_img = cv2.imread(image)
b,g,r = cv2.split(bgr_img)
rgb_img = cv2.merge([r,g,b])
# crop to center to the correct size and convert from 0-255 range to 0-1 range
H,W,C = rgb_img.shape
rgb_img = rgb_img[(H-sz)//2:(sz +(H-sz)//2),(H-sz)//2:(sz +(H-sz)//2),:] / 256
return vision.Image(px=pil2tensor(rgb_img, np.float32))
def learn_infernce():
return load_learner('./')
def get_prediction(image):
if st.button('Classify'):
pred, pred_idx, probs = learn_inference.predict(image)
classes = ['negative', 'tumor']
st.write(f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}')
else:
st.write(f'Click the button to classify')
def main():
st.set_page_config(page_title='Cancer detection', page_icon=None, layout='centered', initial_sidebar_state='auto')
image = read_image('image')
mask = imageToTensor('image')
if mask is not None:
get_prediction('mask')
if __name__ == "__main__":
main()
In your main function you are passing 'str' instead of variables, and also I think your read_image is not well structured.
What you should do is to first save the uploaded file in a directory and fetch the file from that directory and pass it to imageToTensor() as a parameter. That's one work around which will give you a total control over the file. Otherwise you will get other error messages after the first error is fixed.
You can automate some few lines of code in a separate python file to delete the uploaded file from the directory with a given duration.
Note: Keep an eye on the imports because I skipped them to keep the code short
class MyImageItemList(ImageList):
def open(self, fn:PathOrStr)->Image:
img = readCroppedImage(fn.replace('/./','').replace('//','/'))
# This ndarray image has to be converted to tensor before passing on as fastai Image, we can use pil2tensor
return vision.Image(px=pil2tensor(img, np.float32))
# Refactured read_image()
def get_uploaded_image():
upload = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg",'tif'])
if upload is not None:
st.write(upload.name)
# Create a directory and save the image file before proceeding.
file_path = os.path.join("data/uploadedImages/", upload.name)
with open(file_path, "wb") as user_file:
user_file.write(upload.getbuffer())
return file_path # fixed indentation
def imageToTensor(image):
sz = 68
bgr_img = cv2.imread(image)
b,g,r = cv2.split(bgr_img)
rgb_img = cv2.merge([r,g,b])
# crop to center to the correct size and convert from 0-255 range to 0-1 range
H,W,C = rgb_img.shape
rgb_img = rgb_img[(H-sz)//2:(sz +(H-sz)//2),(H-sz)//2:(sz +(H-sz)//2),:] / 256
return vision.Image(px=pil2tensor(rgb_img, np.float32))
def learn_infernce():
return load_learner('./')
def get_prediction(image):
if st.button('Classify'):
pred, pred_idx, probs = learn_inference.predict(image)
classes = ['negative', 'tumor']
st.write(f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}')
else:
st.write(f'Click the button to classify')
def main():
st.set_page_config(page_title='Cancer detection', page_icon=None, layout='centered', initial_sidebar_state='auto')
# Holds the saved file path
user_image = get_uploaded_image()
if user_image is not None:
# Pass the path to imageToTensor() as a parameter.
mask = imageToTensor(user_image)
get_prediction(mask)
if __name__ == "__main__":
main()
output:

error(kivy garden graph) what to write in bulldozer specs( requirements & garden requirements)

This is my python program
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.garden.graph import MeshLinePlot
from kivy.clock import Clock
from kivy.config import Config
Config.set('graphics', 'resizable', True)
import kivy
kivy.require('1.9.0')
from kivy.uix.label import Label
from kivy.uix.spinner import Spinner
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from functools import partial
from kivy.core.audio import SoundLoader
from kivy.properties import BooleanProperty, StringProperty
from threading import Thread
import urllib.request
import requests
import threading
import json
global e
e=0
global e_exit
e_exit=0
import time
def get_pulse_level():
global levels
global e_exit
e_exit=0
levels=[]
global e
i=0
start_time = time.time()
global pulse
pulse=0
def low_beep():
sound = SoundLoader.load('low.mp3')
if sound:
sound.play()
def normal_beep():
sound1 = SoundLoader.load('normal.mp3')
if sound1:
sound1.play()
def high_beep():
sound2 = SoundLoader.load('high.mp3')
if sound2:
sound2.play()
while True:
if((time.time() - start_time) >= 60):
levels=[]
print("1 minutes done levels contain: ",levels,"\n")
start_time= time.time();
URL='https://api.thingspeak.com/channels/1370563/fields/1.json?api_key=HX7TNEUYXRKQEVRO'
HEADER='&results=1'
NEW_URL=URL+HEADER
get_data=requests.get(NEW_URL).json()
feild_1=get_data['feeds']
for x in feild_1:
pulse=int(x['field1'])
levels.append(int(int(x['field1'])))
print(int(int(x['field1'])))
if((int(int(x['field1']))<60) and (e==1)):
low_beep()
print("check")
elif((int(int(x['field1']))<120) and (e==1)):
normal_beep()
print("check1")
elif(e==1):
high_beep()
print("check2")
if(e_exit==1):
print("thead exit")
levels=[]
break
class Logic(BoxLayout):
pulse_label_value = StringProperty()
global pulse
def __init__(self, **kwargs):
super(Logic, self).__init__(**kwargs)
self.plot = MeshLinePlot(color=[1, 0, 0, 1])
Clock.schedule_interval(lambda dt: self.update_time(), 1)
def update_time(self):
self.pulse_label_value = "Heart Beat: "+str(pulse)
def start(self):
global e
e=1
global e_exit
e_exit=0
self.ids.graph.add_plot(self.plot)
Clock.schedule_interval(self.get_value,1)
def stop(self):
global e
e=0
Clock.unschedule(self.get_value)
def get_value(self, dt):
self.plot.points = [(i, j) for i, j in enumerate(levels)]
def home(self):
global e
e=0
global e_exit
e_exit=1
Clock.unschedule(self.get_value)
self.clear_widgets()
App().get_running_app().stop()
import main
main.home()
class RealTimePulse(App):
def build(self):
return Builder.load_file("look.kv")
def run():
levels = [] # store levels of microphone
get_level_thread = Thread(target = get_pulse_level)
get_level_thread.daemon = True
get_level_thread.start()
RealTimePulse().run()
And this is what I have written in buildozer.spec file
requirements =
python3,kivy,requests,kivy.garden.graph,kivy_garden.graph
garden_requirements = # if I write here "graph" i am getting
error
Error getting after writing graph in garden requirements Screenshot
Andriod Studio logcat debug details screenshot

How to ignore the whole output of an instruction using python doctest?

Ellispis does not seem to work to ignore the whole output line.
I'd like to ignore everything that is outputed by foo:
def foo():
"""
>>> foo() # doctest: +ELLIPSIS
...
"""
print("IGNORE ME")
if __name__ == '__main__':
import doctest
doctest.testmod()
Running with python3 gives:
Failed example:
foo() # doctest: +ELLIPSIS
Expected nothing
Got:
IGNORE ME
**********************************************************************
1 items had failures:
1 of 1 in __main__.foo
***Test Failed*** 1 failures.
Note that ignoring only part of the output works. Adding a character before ... (here "-"):
def foo():
"""
>>> foo() # doctest: +ELLIPSIS
-...
"""
print("-IGNORE ME")
if __name__ == '__main__':
import doctest
doctest.testmod()

External code incompatibility with metadata?

If I use the same setup with metadata for an external code component and explicit component it seems to fail with the externalcode comp. The code below has both of the component. As is there is no error, if i comment out the explicit component part I get an error :
comp = GFWrapper(DVLO=np.array(DVLIST))
TypeError: init() got an unexpected keyword argument 'DVLO'
import numpy as np
import json as js
import re,sys,subprocess,os
from openmdao.api import Problem, Group, IndepVarComp
from openmdao.api import ScipyOptimizeDriver
from openmdao.api import ExternalCode
class GFWrapper(ExternalCode):
def initialize(self):
self.metadata.declare('DVLO', types=np.ndarray)
def setup(self):
DVLO = self.metadata['DVLO']
for dv in DVLO:
self.add_input(dv)
self.add_output('OBJECTIVE')
self.input_file = 'GFWrapper_input.dat'
self.output_file = 'GFWrapper_output.dat'
self.options['external_input_files'] = [self.input_file,]
self.options['external_output_files'] = [self.output_file,]
self.options['command'] = [
'python', 'run.py', self.input_file, self.output_file
]
#self.declare_partials(of='*', wrt='*', method='fd')
for dv in DVLO:
self.declare_partials(of='OBJECTIVE', wrt=dv, method='fd')
def compute(self, inputs, outputs):
DVLO = self.metadata['DVLO']
# generate the input file for the external code
outF = open(self.input_file, "w")
for dv in DVLO:
outF.write(inputs[dv])
print(dv,inputs[dv])
outF.write("\n")
outF.close()
# the parent compute function actually runs the external code
super(GFWrapper, self).compute(inputs, outputs)
# parse the output file from the external code
file_contents=np.loadtxt(self.output_file)
outputs['OBJECTIVE'] = file_contents[0]
from openmdao.api import ExplicitComponent
#
class GFWrapper(ExplicitComponent):
def initialize(self):
self.metadata.declare('DVLO', types=np.ndarray)
def setup(self):
DVLO = self.metadata['DVLO']
for dv in DVLO:
self.add_input(dv)
self.add_output('OBJECTIVE')
#self.declare_partials(of='*', wrt='*', method='fd')
for dv in DVLO:
self.declare_partials(of='OBJECTIVE', wrt=dv, method='fd')
def compute(self, inputs, outputs):
DVLO = self.metadata['DVLO']
powetemp = 0
for dv in DVLO:
powetemp += inputs[dv]
outputs['OBJECTIVE'] = powetemp
#
DVLIST=['DV1','DV2']
DVMIN =[2,11]
DVMAX =[3,12]
InitDVVal=[3,5]
nr_of_desvar=len(DVLIST)
top = Problem()
top.model = model = Group()
" Introduce independent variables later will be design variables (optimization parameters) "
inputs_comp = IndepVarComp()
for i in range(nr_of_desvar):
inputs_comp.add_output(DVLIST[i],InitDVVal[i])
" Add components/subsystems to the Group-model- "
model.add_subsystem('inputs_comp', inputs_comp)
comp = GFWrapper(DVLO=np.array(DVLIST))
model.add_subsystem('GFWrapper', comp)
This is a bug in OpenMDAO version 2.2. The error occurs because we didn't include **kwargs in the init statement for ExternalCode. We intend to fix this for V2.3, but in the meantime, you can work around for your GFWrapper component by adding an init statement like this:
class GFWrapper(ExternalCode):
def __init__(self, **kwargs):
super(GFWrapper, self).__init__()
self.metadata.update(kwargs)

Microsoft Azure Machine Learning and Cognitive Services API

Is it possible to call Cognitive Services API in Azure ML studio when build model?” any document our sample experiment can be reference?
Thanks in advance.
Here is the sample code you can try:
import urllib2
import urllib
import sys
import base64
import json
import numpy as np
import pandas as pd
# The entry point function can contain up to two input arguments:
# Param<dataframe1>: a pandas.DataFrame
# Param<dataframe2>: a pandas.DataFrame
def azureml_main(dataframe1 = None, dataframe2 = None):
# Execution logic goes here
#print('Input pandas.DataFrame #1:\r\n\r\n{0}'.format(dataframe1))
# Account key is for Ted Way
account_key = str(dataframe2['Col1'][0])
#account_key = 'api_key'
#base_url = 'https://api.datamarket.azure.com/data.ashx/amla/text-analytics/v1'
#base_url = str(dataframe2['Col2'][0])
base_url = 'https://westus.api.cognitive.microsoft.com/'
headers = {'Content-Type':'application/json', 'Ocp-Apim-Subscription-Key':account_key}
#input_text = sys.argv[2]
sentiment_scores = []
num_examples = len(dataframe1.index)
input_texts = '{"documents":['
#for each record
for i in range(0,num_examples):
input_text = str(dataframe1['Text'][i])
input_text = input_text.replace("\"", "'")
#params = { 'Text': input_text}
input_texts = input_texts + '{"id":"' + str(i) + '","text":"'+ input_text + '"},'
input_texts = input_texts + ']}'
print input_texts
# Detect sentiment.
batch_sentiment_url = base_url + 'text/analytics/v2.0/sentiment'
req = urllib2.Request(batch_sentiment_url, input_texts, headers)
response = urllib2.urlopen(req)
result = response.read()
obj = json.loads(result)
for sentiment_analysis in obj['documents']:
sentiment_scores.append( str(sentiment_analysis['score']))
#print('Sentiment score: ' + str(obj['Score']))
sentiment_scores = pd.Series(np.array(sentiment_scores))
df1 = pd.DataFrame({'SentimentScore':sentiment_scores})
# Don't return the original text'
#frames = [dataframe1, df1]
#dataframe1 = pd.concat(frames, axis=1)
# Return value must be of a sequence of pandas.DataFrame
return df1
It is possible to execute Python snippets inside Azure ML. From there, you may call the Microsoft Cognitive Services API using a Python interface (take a look at the example for the Face API from Python).

Resources