Is there a solid method for wavelet analysis in Python? - python

all. So, I have some time series data that I'd like to process with a wavelet transform to represent thusly. I am relatively new to the concept of wavelets. I noticed scipy.signal has a few objects, but it seems thin. Is there a library or something out there that will aid in this? Any documentation or tutorials you know of will be greatly appreciated.

Have you tried PyWavelets?
import pywt
x = [3, 7, 1, 1, -2, 5, 4, 6]
# Discrete Wavelet Transform
cA, cD = pywt.dwt(x, 'db2')
x2 = pywt.idwt(cA, cD, 'db2')
There are a few examples in their documentation.
The GitHub repository has more updated information to check out as well.

Related

Kalman Filtering_1-dimensional_Python

I try to use Kalman filtering for my one dimensional data. So, assume that I have the following dataset:
Variable
250.1
248.5
262.3
265.3
270.2
I do know that there is a noise in my data and hence, I want to clean this data by using Kalman filtering. Which way can produce the most efficient result for me?
I run the following code:
from pykalman import KalmanFilter
import numpy as np
kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]],
observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
measurements = np.asarray([(250.1),(248.5),(262.3),(265.3), (270.2)])
kf = kf.em(measurements, n_iter=5)
(filtered_state_means, filtered_state_covariances)=kf.filter(measurements)
(smoothed_state_means, smoothed_state_covariances)=kf.smooth(measurements)
As you can see, I try to use pykalman, however I cannot install this module. I try to use easy_install pykalman direction, and the error is invalid syntax. Another problem is, I have a huge data set, so I have more than one hundred thousand rows in my variable column. So, I cannot write all observations one by one.
To install pykalman I used:
pip install pykalman --user
The --user flag installs to my home directory, avoiding having to use sudo to install. I was told that scipy was missing, so I pip installed that as well. On the project github page there is a list of dependent libraries, so you may be asked to install any one of those.
You are using single values for each of your readings. Most examples have more than this, for instance position and velocity for each reading. To get something to plot with the transition and observation matrices you supplied, I added a second bogus reading of '1' to each of your measurements. The following Jupyter notebook script will produce a plot, but the output is poor as the matrices values need to be adjusted for your data set.
%matplotlib inline
from pykalman import KalmanFilter
import numpy as np
kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]],
observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
# measurements = np.asarray([(250.1),(248.5),(262.3),(265.3), (270.2)])
measurements = np.array([[250.1,1],[248.5,1],[262.3,1],[265.3,1], [270.2,1]])
kf = kf.em(measurements, n_iter=5)
filtered_state_estimates = kf.filter(measurements)[0]
(smoothed_state_estimates, smoothed_state_covariances)=kf.smooth(measurements)
# draw estimates
pl.figure()
lines_true = pl.plot(measurements, color='b')
lines_filt = pl.plot(filtered_state_estimates, color='r')
lines_smooth = pl.plot(smoothed_state_estimates, color='g')
pl.legend((lines_true[0], lines_filt[0], lines_smooth[0]),
('true', 'filt', 'smooth'),
loc='lower right'
)
pl.show()
For the data set that you propose, a fast and simpler way to produce a filtered output would be to use a one minus alpha filter. Have a look at this link for more details on this type of filter:
http://stats.stackexchange.com/questions/44650/a-simpler-way-to-calculate-exponentially-weighted-moving-average

Python version of h=area() in Matlab

I asked a related question yesterday and fortunately got my answer from jlarsch quickly. But now I am stuck with the next part, which starts with the h=area() line. I'd like to know the python version of the area() function, via which I will be able to set the colors. Could someone shed me some light again? Thanks much in advance.
...
Subplot (2,1,1);
H = plot (rand(100,5));
C = get (H, 'Color')
H = area (myX, myY);
H(1).FaceColor = C(1);
H(2).FaceColor = C(2);
Grid on;
...
The pretty much exact equivalent of MATLAB's Area plot is matplotlib's stackplot. Here is the first MATLAB example from the above link reproduced using matplotlib:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(4)
y = [[1, 3, 1, 2],
[5, 2, 5, 6],
[3, 7, 3, 1]]
plt.stackplot(x, y)
plt.show()
And here is the result:
You might be looking for pygame.draw.polygon(), which can fill a polygon defined by an arbitrary array of points.
You probably want plt.fill().
A huge amount of graph types at Matplotlib Gallery

How to generate image by using python and given data?

I have one data file which is like this:
1, 23%
2, 33%
3, 12%
I want to use python to generate one histogram to represent the percentage. I followed these command:
from PIL import Image
img = Image.new('RGB', (width, height))
img.putdata(my_data)
img.show()
However I got the error when I put the data: SystemError: new style getargs format but argument is not a tuple. Do I have to change my data file? and How?
A histogram is usually made in matplotlib by having a set of data points and then assigning them into bins. An example would be this:
import matplotlib.pyplot as plt
data = [1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7]
plt.hist(data, 7)
plt.show()
You already know what percentage of your data fits into each category (although, I might point out your percentages don't add to 100...). A way to represent this is to to make a list where each data value is represented a number of times equal to its percentage like below.
data = [1]*23 + [2]*33 + [3]*12
plt.hist(data, 3)
plt.show()
The second argument to hist() is the number of bins displayed, so this is likely the number you want to make it look pretty.
Documentation for hist() is found here:
http://matplotlib.org/api/pyplot_api.html
Are you graphing only? PIL is an image processing module - if you want histograms and other graphs you should consider matplotlib.
I found an example of a histogram here.

How to export VisPy result as png image

Is it possible to save images made with VisPy? Maybe using vispy.io.imsave or vispy.write_png?
Also, it is possible to plot matplotlib figures in vispy using vispy.mpl_plot but is it possible to use a vispy image in matplotlib?
In any case, I would need to generate an image object with VisPy but I did not find any example of that.
Here is a minimal example. Use canvas.render to create an image, then export it with io.write_png:
import vispy.plot as vp
import vispy.io as io
# Create a canvas showing plot data
canvas = vp.plot([1, 6, 2, 4, 3, 8, 5, 7, 6, 3])
# Use render to generate an image object
img=canvas.render()
# Use write_png to export your wonderful plot as png !
io.write_png("wonderful.png",img)
Here is an updated version jvtrudel's of answer (working with vispy 0.5.0-dev):
The official demo https://github.com/vispy/vispy/blob/master/examples/basics/plotting/export.py does something very similar, and a stripped down version adjusted to export a png could look like this:
import vispy.plot as vp
import vispy.io as io
fig = vp.Fig(show=False)
fig[0, 0].plot([1, 6, 2, 4, 3, 8, 5, 7, 6, 3])
image = fig.render()
io.write_png("wonderful.png",image)

Python: how to make a histogram with given bins and binned data [duplicate]

This question already has answers here:
Plotting a histogram from pre-counted data in Matplotlib
(6 answers)
Closed 8 years ago.
I would like to make a histogram with binned data with Python, but I didn't figure out how to make one. I didn't find any documentation (or I Googled wrong), but I've tried this:
import pylab as plb
a = [1, 2, 3, 4, 5, 6] # my histogram bins
b = [1, 4, 6, 1, 3, 7] # my data
plb.hist(b, bins = a)
plb.show()
and related alternatives, and of course it doesn't work.
The example you give runs just fine on my PC, granted it is not looking very nice. A good place to search for recipes common graphs is the gallery of Matplotlib: http://matplotlib.org/gallery.html. If you want to get more information about what arguments the functions use, you can take a look at the documentation (http://matplotlib.org/api/pyplot_api.html?highlight=hist#matplotlib.pyplot.hist)
If you take a look at that gallery and search for histogram, you will find this example which fits your needs:
http://matplotlib.org/examples/statistics/histogram_demo_histtypes.html

Categories

Resources