Can't get scipy.io.wavfile.read() to work - python-2.7

I am trying to read a .wav file into an array so that I can then plot the array and do a FFT. I got the file open with the wave module and now I am struggling. I was advised to use scipy.io.wavfile.read(filename, mmap=False) but am not having any luck. This function should do exactly what I want it to do but it isn't. I am running Python 2.7 and maybe that is it. Please help me figure out how to make this work. The code I have written is below.
import scipy
import wave
harp=wave.open('/Users/williamweiss2/Desktop/Test 2/harp.wav','r')
frames_harp=harp.getnframes()
harp_rate,harp_data=scipy.io.wavfile.read(harp,mmap=False)
This is the error I am getting when I try to run the program.
---> harp_rate,harp_data=scipy.io.wavfile.read(harp,mmap=False)
AttributeError: 'module' object has no attribute 'io'
Any help would be greatly appreciated. Thanks in advance.

You have confused SciPy's WAV module with Python's. Remove import wave, use import scipy.io.wavfile, and call scipy.io.wavfile.read.
Example:
>>> import scipy.io.wavfile
>>> FSample, samples = scipy.io.wavfile.read('myfile.wav')
SciPy's module does the job of converting from a byte string to numbers for you, unlike Python's module. See the linked docs for more details.

Related

Import a .mdb file into Python

I want to import a .mdb file into Python (I'm using Jupyter). My file name is PIF.mdb and i want a table called ValuesXYZ. Do anyone know what code works for this? I teste a lot of codes but they doesn't worked.
So grateful,
Henrique.
Unfortunately, you cannot directly read the table.
.mdb is database file and not tables. You need to connect to it first, via pyodbc for example.
This SO thread explains it really well.

How to correctly import modules from a file?

My question has 3 parts:
Given a file .sig with a signature, how to import it correctly into a file .sml specifying a structure implementation?
Given a file .sml specifying a structure implementation, how to correctly import it into other .sml files?
Where can I find more information on structuring SML projects? Simple websearch didn't help.
I was told that open SomeStructure is not a safe way. I am new to ML-family idioms, hence the question. Thanks.
OK, I finally found this example, which is about module management in the SML project:
How to import from another file in SML, with a path relative to the importer?

Import sklearn2pmml generated .pmml back into ScikitLearn or Python

Apologies if this may have been answered somewhere but I've been looking for about an hour and can't find a good answer.
I have a simple Logistic Regression model trained in Scikit-Learn that I'm exporting to a .pmml file.
from sklearn2pmml import PMMLPipeline, sklearn2pmml
my_pipeline = PMMLPipeline(
( classifier", LogisticRegression() )
)
my_pipeline.fit(blah blah)
sklearn2pmml(my_pipeline, "filename.pmml")
etc....
So what I'm wondering is if/how I can import this file back into Python (2.7 preferably) or Scikit-Learn to use as I would in Java/Scala. Something along the lines of
"import (filename.pmml) as pm
pm.predict(data)
Thanks for any help!
Scikit-learn does not offer support for importing PMML files, so what you're trying to achieve cannot be done I'm afraid.
The concept of using libraries such as sklearn2pmml is really to extend the functionality that sklearn does not have when it comes to supporting the model export to a PMML format.
Typically, those who use sklearn2pmml are really looking to re-use the PMML models in other platforms (e.g. IBM's SPSS, Apache Spark ML, Weka or any other consumer as listed in the Data Mining Group's website).
If you're looking to save a model created with scikit-learn and re-use it afterwards with scikit-learn as well then you should explore its native persistence model mechanism named Pickle, which uses a binary data format.
You can read more about how to save/load models in Pickle format (together with its known issues) here.
I created a simple solution to generate sklearn kmeans models from pmml files which i exported from knime analytics platform. You can check it out pmml2sklearn
You could use PyPMML to make predictions on a new dataset using PMML in Python, for example:
from pypmml import Model
model = Model.fromFile('the/pmml/file/path')
result = model.predict(data)
The data could be dict, json, Series or DataFrame of Pandas.
I believe you can Import/Export a pmml file with python. After you load back your model you can predict again with out any problem. However output file formats can differ, like 1d array, or nxn panda tables etc.
from sklearn2pmml import make_pmml_pipeline, sklearn2pmml
from pypmml import Model
#Extract as pmml
yourModelPipeline = make_pmml_pipeline(yourModelObjectGoesHere)
sklearn2pmml(yourModelPipeline, "yourModel.pmml")
#Load from pmml
yourModelLoaded = Model.fromFile('yourModel.pmml')
prediction = yourModelLoaded.predict(yourPredictionDataSet)
Lastly reproducing result make take long time, don't let it discourage you :). I would like to share developers comment about the issue: https://github.com/autodeployai/pypmml/issues/53

what is the double dot operator(..) in the python?

Before installing the package, I read the code and I got a question.
There is double dot in the code when the code calls a module, the module is tools.
What is the double dot in python?
The code wrote from ..tools import *.
You can find it here, https://github.com/synergetics/spectrum/blob/master/src/conventional/cum2x.py
The code is computing the bispectrum.
And do you know the other code analysing a wave interaction for python such as HOSA in MATLAB?
In python, The . is just accessing an attribute. The attribute could be a class, an instance, a method/function, etc and The .. is just specify an relative address in Linux environment. It means go to ../tools folder and import everything inside it.

Embedding IPython-shell in C/C++-program

I have a C++-program that allows me to run Python-scripts that C++ passes data to. If the Python-script fails the C++-program calls PyRun_InteractiveLoop to allow investigating the problem on an interactive shell. This works alright but I thought "Would be cool if IPython is installed I could use it in that case".
Now my problem is: all I find when looking for "ipython embedding" is instructions how to embed IPython into Python-programs (amongst others http://ipython.org/ipython-doc/dev/interactive/reference.html#embedding-ipython). I tried to reproduce these on the embedded regular python-shell I have but most of them fail in some way (usually because of a missing sys.argv .... that one I can solve).
Any suggestions how to do this? My first plan would have been to first import IPython via the C-API (that one I got covered). If that fails use the "regular" shell. Otherwise call IPython.embed() (or similar) via PyRun_InteractiveOneFlags
Have you considered using python debugger
>>> import pdb
>>> import yourmodule
>>> pdb.run('yourmodule.test()')