I'm trying to use butter and lfilter from scipy.signal in a compiled program. After some debugging I found that when the program reached this line:
b, a = butter(order, normal_cutoff, btype='low', analog=False)
The executable was closing. My program runs fine when it's in the IDE. I've put some sample code below that will re-create the problem.
from scipy.signal import butter, lfilter
from tkinter import *
master = Tk()
def test_program():
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def butter_lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
corrected_values_800 = [-0.76947910399999997, -1.007095104, -1.0556311039999999, -0.93497010399999991, -0.65326510399999993, -0.29980610400000002, -0.031438104000000008, 0.072425896000000003, -0.0016751040000000106, -0.29881810400000003, -0.67858310399999999, -0.91570410400000002, -0.95263110399999995, -0.81319810399999992, -0.52321910400000005, -0.15321010400000001, 0.112192896, 0.211981896, 0.14232689599999998, -0.13752510400000001, -0.50358210400000003, -0.73107010399999994, -0.78059410399999996, -0.66363910399999992, -0.39737110400000003, -0.037366104000000011, 0.23124789599999995, 0.33338289599999998, 0.25001989599999996, -0.026251104000000004, -0.414908104, -0.65042410399999995, -0.68377010399999993, -0.55224110400000004, -0.27597010399999999, 0.083540895999999989, 0.33511189599999996, 0.42440289599999997, 0.34733889599999995, 0.050812895999999996, -0.32413510400000001, -0.54754810399999998, -0.57965810399999995, -0.46640810400000005, -0.19260710400000003, 0.14948989600000001, 0.398591896, 0.49319289599999999, 0.40649589599999997, 0.12034389599999999, -0.26930110400000001, -0.52210710400000004, -0.57237210399999994, -0.46122110400000005, -0.17618110400000001, 0.17369689599999999, 0.42156289599999996, 0.50455489600000003, 0.42267389599999999, 0.12417289599999999, -0.26152010400000003, -0.49790110400000004, -0.55767510399999998, -0.435533104, -0.169512104, 0.17616689600000002, 0.42464989599999997, 0.51307689600000006, 0.42143889599999995, 0.12985389599999997, -0.25509810399999999, -0.50457010400000002, -0.56595010400000001, -0.49098510400000001, -0.20532710399999998, 0.15418289600000001, 0.40414889599999998, 0.50517289600000004, 0.42674989599999996, 0.12725989599999998, -0.26139710399999999, -0.50753410399999999, -0.547178104, -0.43738510400000002, -0.160497104, 0.18481189599999998, 0.42440289599999997, 0.53048989600000007, 0.45799589599999996, 0.16727489600000001, -0.20458610399999999, -0.44442510400000002, -0.490120104, -0.38292210400000004, -0.10924410400000001, 0.24742689599999995, 0.48763589599999996, 0.59149989600000008, 0.50974189600000008, 0.21494589599999997, -0.16988310400000001, -0.41058610400000001, -0.43874410400000002, -0.301041104, -0.0029101040000000106, 0.35042589599999996, 0.60347889600000004, 0.69042389599999998, 0.61138389599999998, 0.31522889599999998, -0.056756104000000009, -0.27214110400000002, -0.30548710400000001, -0.20063410399999998, 0.08564089599999998, 0.45614289599999996, 0.72129889600000008, 0.82256989600000008, 0.74711089600000002, 0.46503489599999998, 0.07724189599999999, -0.15740910400000002, -0.20199310400000003, -0.082444104000000004, 0.20691789599999999, 0.58174289600000006, 0.83664889600000003, 0.94421789600000006, 0.850604896, 0.56939289599999998, 0.19530889599999995, -0.045147104000000007, -0.090348104000000012, 0.025371895999999991, 0.285834896, 0.63089689599999998, 0.87518089600000004, 0.98126789600000008, 0.90210389600000007, 0.61372989600000005, 0.220996896, -0.032056104000000009, -0.087014104000000009, 0.01561589599999999, 0.28175989599999995, 0.64089989600000008, 0.88604889600000003, 0.98904889600000012, 0.90531489600000004, 0.59680989600000001, 0.20432489599999998, -0.030203104000000008, -0.081333104000000003, 0.025248895999999993, 0.28151289599999996, 0.63176089600000007, 0.858878896, 0.94360089599999997, 0.85739689600000002, 0.55234989600000006, 0.15245389599999998, -0.098993103999999998, -0.16963610400000001, -0.086396104000000001, 0.15899989599999997, 0.50060289600000007, 0.729820896, 0.799598896, 0.69598189600000004, 0.36882789599999999, -0.040454104000000005, -0.29918810400000001, -0.35241710400000004, -0.25806210400000001, -0.0068621040000000078, 0.337087896, 0.54642189600000002, 0.61039589599999999, 0.50146789600000008, 0.18975189599999998, -0.21780110400000002, -0.470607104, -0.54372010400000004, -0.46369110400000002, -0.209897104, 0.12886589599999998, 0.35894789599999999, 0.44811489599999993, 0.337952896, 0.017097895999999987, -0.38872610400000002, -0.63498710399999991, -0.69266210399999995, -0.58583310399999999, -0.32030710400000001, 0.012774895999999994, 0.24088089599999996, 0.32535589599999998, 0.24063389599999996, -0.063795104000000005, -0.44454910400000003, -0.69377310399999992, -0.74885510399999999, -0.65684610399999999, -0.40082910400000005, -0.05972010400000001, 0.18011889599999997, 0.26039389599999996, 0.181229896, -0.12035910400000001, -0.52025510399999997, -0.77528410399999992, -0.82172010399999995, -0.71674410399999999, -0.44479610400000003, -0.096276104000000001, 0.14158589599999999, 0.22766589599999998, 0.14207989599999998, -0.14728210400000002, -0.52074910399999996, -0.73773910399999998, -0.78034710399999996, -0.66104510399999994, -0.385268104, -0.012913104000000009, 0.24643889599999996, 0.35499589599999998, 0.28175989599999995, 0.0036358959999999996, -0.37884610400000002, -0.61288010399999993, -0.65289410399999992, -0.53544510400000001, -0.249911104, 0.12701289599999999, 0.39044089599999998, 0.48096689600000003, 0.40291389599999999, 0.11293389599999999, -0.26460810400000001, -0.49024410400000001, -0.52371310400000004, -0.42182410400000003, -0.13406710399999999, 0.20951189599999998, 0.46651689599999996, 0.57816189600000001, 0.49158789600000002, 0.19703789599999999]
# Filter requirements.
order = 6
fs = 1600 # sample rate, Hz
cutoff = 300 # desired cutoff frequency of the filter, Hz
# Get the filter coefficients so we can check its frequency response.
b, a = butter_lowpass(cutoff, fs, order)
# Filter the data, and plot both the original and filtered signals.
y = butter_lowpass_filter(corrected_values_800, cutoff, fs, order)
print(y)
master.minsize(width=450, height=600)
master.wm_title("Functional Tester")
start_button = Button(master, text='START', command=test_program )
start_button.place(x=230, y=180)
master.mainloop()
And here's the setup file (there's some extra things in there for the actual program):
import sys
import os
from cx_Freeze import setup, Executable
import cx_Freeze
import tkinter
import os.path
import scipy
base = None
if sys.platform == 'win32':
base = "Win32GUI"
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
#os.environ['TCL_LIBRARY'] = r'C:\Users\matthew\Downloads\WinPython-64bit-3.5.3.0Qt5\python-3.5.3.amd64\tcl\tcl8.6'
#os.environ['TK_LIBRARY'] = r'C:\Users\matthew\Downloads\WinPython-64bit-3.5.3.0Qt5\python-3.5.3.amd64\tcl\tk8.6'
executables = [cx_Freeze.Executable("stacked_question.py", base=base)]
addtional_mods = ['numpy.core._methods', 'numpy.lib.format']
packages = ["idna", "numpy",]
options = {
'build_exe': {
'include_files':[
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
os.path.dirname(scipy.__file__),
],
'includes': addtional_mods,
'packages':packages,
},
}
cx_Freeze.setup(
name = "Test",
options = options,
version = "0.01",
description = 'Tester',
executables = executables
)
Thanks for any help!
So the solution as it turns out is due to DLL files. I was having an issue with a different program while trying to plot and show graphs in a cx freeze application and eventually found that I needed to copy more DLL files, which is also the solution here.
anaconda_bin = r'C:\ProgramData\Anaconda3\Library\bin'
for file in os.listdir(anaconda_bin):
if 'mkl_' in file:
shutil.copy2("%s\%s" % (anaconda_bin, file),r'C:\Users\matthew\PycharmProjects\untitled5\build\exe.win32-3.6\%s' % file)