scipy

Peak-finding algorithm for Python/SciPy

I can write something myself by finding zero-crossings of the first derivative or something, but it seems like a common-enough function to be included in standard libraries. Anyone know of one? My particular application is a 2D array, but usually it would be used for finding peaks in FFTs, etc. Specifically, in these kinds of problems...

Principal component analysis in Python

I'd like to use principal component analysis (PCA) for dimensionality reduction. Does numpy or scipy already have it, or do I have to roll my own using numpy.linalg.eigh? I don't just want to use singular value decomposition (SVD) because my input data are quite high-dimensional (~460 dimensions), so I think SVD will be slower than com...

How to normalize a NumPy array in Python?

After doing some processing on an audio or image array, it needs to be normalized within a range before it can be written back to a file. This can be done like so: # Normalize audio channels to between -1.0 and +1.0 audio[:,0] = audio[:,0]/abs(audio[:,0]).max() audio[:,1] = audio[:,1]/abs(audio[:,1]).max() # Normalize image to between...

How to compute laplacian of a field?

I'm trying to compute the laplacian of a 2d field A using scipy.ndimage.convolve. stencil = numpy.array([[0, 1, 0],[1, -4, 1], [0, 1, 0]]) scipy.ndimage.convolve(A, stencil, mode='wrap') This doesn't seem to give me the right answer though. Any ideas where I'm going wrong, or are there better ways of computing the laplacian in numpy? ...

Strategies for debugging numerical stability issues?

Dear SO, I'm trying to write an implementation of Wilson's spectral density factorization algorithm [1] for Python. The algorithm iteratively factorizes a [QxQ] matrix function into its square root (it's sort of an extension of the Newton-Raphson square-root finder for spectral density matrices). The problem is that my implementation o...

Why don't these two math functions return the same result?

I'm trying to use fancy indexing instead of looping to speed up a function in Numpy. To the best of my knowledge, I've implemented the fancy indexing version correctly. The problem is that the two functions (loop and fancy-indexed) do not return the same result. I'm not sure why. It's worth pointing out that the functions do return the s...

How much of NumPy and SciPy is in C?

Python newbie here. Are parts of NumPy and/or SciPy programmed in C/C++? And how does the overhead of calling C from Python compare to the overhead of calling C from Java and/or C#? I'm just wondering if Python is a better option than Java or C# for scientific apps. If I look at the shootouts, Python loses by a huge margin. But I gue...

How to disable screen update in matplotlib

I have a loop that is adding a line to a plot on each iteration. Right now this is horribly slow as it seems to redraw the the whole graph each time. Is it possible to disable screen updates for a graph while it is being set up then re-enable them afterwards. Here's the code: for rr,dd in zip(angles,dists): if dd == inf: ...

When to choose R vs. SciPy?

What are some advantages and disadvantages to doing statistical analyses in SciPy vs. R? They seem to have been designed with opposite philosophies (plain old library vs. DSL). What are some rules of thumb about which is the right tool for the job? ...

"painting" one array onto another using python / numpy

I'm writing a library to process gaze tracking in Python, and I'm rather new to the whole numpy / scipy world. Essentially, I'm looking to take an array of (x,y) values in time and "paint" some shape onto a canvas at those coordinates. For example, the shape might be a blurred circle. The operation I have in mind is more or less identi...

2 dimensional interpolation problem

I have DATA on x and y axes and the output is on z for example y = 10 x = [1,2,3,4,5,6] z = [2.3,3.4,5.6,7.8,9.6,11.2] y = 20 x = [1,2,3,4,5,6] z = [4.3,5.4,7.6,9.8,11.6,13.2] y = 30 x = [1,2,3,4,5,6] z = [6.3,7.4,8.6,10.8,13.6,15.2] how can i find the value of z when y = 15 x = 3.5 I was trying to use scipy but i am very new ...

How to access fields in a struct imported from a .mat file using loadmat in Python?

Following this question which asks (and answers) how to read .mat files that were created in Matlab using Scipy, I want to know how to access the fields in the imported structs. I have a file in Matlab from which I can import a struct: >> load bla % imports a struct called G >> G G = Inp: [40x40x2016 uint8] Tgt: [8...

Interpolate Question

import re from decimal import * import numpy from scipy.signal import cspline1d, cspline1d_eval import scipy.interpolate import scipy import math import numpy from scipy import interpolate Y1 =[0.48960000000000004, 0.52736099999999997, 0.56413900000000006, 0.60200199999999993, 0.64071400000000001, 0.67668...

scipy linregress function erroneous standard error return?

Hello, I have a weird situation with scipy.stats.linregress seems to be returning an incorrect standard error: >>> from scipy import stats >>> x = [5.05, 6.75, 3.21, 2.66] >>> y = [1.65, 26.5, -5.93, 7.96] >>> gradient, intercept, r_value, p_value, std_err = stats.linregress(x,y) >>> gradient 5.3935773611970186 >>> intercept -16.28112...

Python library needed

I am trying to run a python script which has the following statements: import random as RD import pylab as PL import scipy as SP import networkx as NX Where can I download these packages? I have installed these packages and I get the following error when I run my code I am getting the following error when I run the code Traceback (...

Is the order of a Python dictionary guaranteed over iterations?

I'm currently implementing a complex microbial food-web in Python using SciPy.integrate.ode. I need the ability to easily add species and reactions to the system, so I have to code up something quite general. My scheme looks something like this: class Reaction(object): def __init__(self): #stuff common to all reactions d...

Why scipy.io.wavfile.read does not return a tuple?

I am trying to read a *.wav file using scipy. I do the following: import scipy x = scipy.io.wavfile.read('/usr/share/sounds/purple/receive.wav') As a result of this code I get: Traceback (most recent call last): File "test3.py", line 2, in <module> x = scipy.io.wavfile.read('/usr/share/sounds/purple/receive.wav') AttributeError...

ImportError: cannot import name NumpyTest

I am trying to read a *.wav file using scipy. I do it in the following way: import scipy.io x = scipy.io.wavfile.read('/usr/share/sounds/purple/receive.wav') As a result I get the following error message: Traceback (most recent call last): File "test3.py", line 1, in <module> import scipy.io File "/usr/lib/python2.5/site-pack...

What is the easiest way to read wav-files using Python [summary]?

I want to use Python to access a wav-file and write its content in a form which allows me to analyze it (let's say arrays). I heard that "audiolab" is a suitable tool for that (it transforms numpy arrays into wav and vica versa). I have installed the "audiolab" but I had a problem with the version of numpy (I could not "from numpy.test...

Seasonal adjustment in Python and Scipy

Hello I am looking to seasonally adjust monthly data, using Python. As you can see from these series: www.emconfidential.com, there is a high seasonal component to the data. I would like to adjust for this so that I can better guage if the series trend is rising or falling. Anybody know how to do this easily using scipy or other Pytho...