In the module _denoise.py of skimage I found the following piece of code:
def estimate_sigma(image, average_sigmas=False, multichannel=False):
# some more code here
sigmas = [estimate_sigma(image[..., c], multichannel=False)...
return _sigma_est_dwt(detail_coeffs, distribution='Gaussian')
Inside estimate_sigma there's an estimate_sigma? How and why does that work? The imports are
import scipy.stats
import numpy as np
from math import ceil
from .. import img_as_float
from ..restoration._denoise_cy import _denoise_bilateral, _denoise_tv_bregman
from .._shared.utils import skimage_deprecation, warn
import pywt
import skimage.color as color
import numbers
which doesn't seem to sneak in any new functions.
Note that the recursive call of estimate_sigma is inside an if-clause:
if multichannel:
sigmas = [estimate_sigma(image[..., c], multichannel=False)...
...
return _sigma_est_dwt(detail_coeffs, distribution='Gaussian')
Case A) If we call estimate_sigma with multichannel=False, the function won't get inside the if-clause, thus won't invoke itself and will return reaching the end of its body.
Case B) If we call estimate_sigma with multichannel=True the condition will succeed, so estimate_sigma will call itself. As seen from the piece of source above, when estimate_sigma calls itself, it passes multichannel as False. It means that during the recursive invocation "case A" will happen. This time the program will not enter the above if block and recursion will end, ending execution of the function and returning.
Basically the idea is: if we've got multiple channels, let's divide them into separate ones and perform sigma estimation on each channel
Related
I have the following code, but it shows error:
IntegrationWarning: The maximum number of subdivisions (50) has been achieved.
If increasing the limit yields no improvement it is advised to analyze
the integrand in order to determine the difficulties. If the position of a
local difficulty can be determined (singularity, discontinuity) one will
probably gain from splitting up the interval and calling the integrator
on the subranges. Perhaps a special-purpose integrator should be used.
potC=sc.integrate.quad(lambda r: Psi(r,n2)*(-1/r)Psi(r,n1)(r**2),0,np.inf)
How to fix it?
import scipy as sc
import numpy as np
def Psi(r,n):
return 2*np.exp(-r/n)*np.sqrt(n)*sc.special.hyp1f1(1-n, 2, 2*r/n)/n**2
def p(n1,n2):
potC=sc.integrate.quad(lambda r: Psi(r,n2)*(-1/r)*Psi(r,n1)*(r**2),0,np.inf)
pot=potC[0]
return pot
print(p(15,15))
The error literally says what your problem is. Your function is not "well behaved" in some regions.
For example with n = 15 and r = 50 your special.hyp1f1(-14, 2, 2*50/15) result in NaN. I am not familiar with this function, so I do not know if this is the expected behaviour, but this is what happens.
You can try to isolate these points and exclude them from the integration (if you know lower and upper bounds of the function's value (if it is defined) you can also update the expected error of your integration) and just integrate in the well behaved regions.
If it is a bug in scipy, then please report it to them.
Ps.: Tested with scipy 1.8.0
Ps2.: With some reading I found, that you can get the values correctly, if you do your calculations with complex number, so the following code gives you a value:
import scipy as sc
from scipy import integrate
from scipy import special
import numpy as np
def Psi(r,n):
r = np.array(r,dtype=complex)
return 2*np.exp(-r/n)*np.sqrt(n)*special.hyp1f1(1-n, 2, 2*r/n)/n**2
def p(n1,n2):
potC=integrate.quad(lambda r: Psi(r,n2)*(-1/r)*Psi(r,n1)*(r**2),0,np.inf)
pot=potC[0]
return pot
print(p(15,15))
I just spent 20 min monkey-patching this, figured I’d share
My code calls a function dataloader.dataloader from a module dataloader which calls the function scipy.misc.imresize
I tried using numpy.array(Image.fromarray(arr).resize()) as suggested in the scipy doc, but I ran into this issue, and since the accepted answer was to use scipy.misc.imresize, so that was not very helpful.
By what should I replace imresize?
import PIL
import numpy as np
def imresize(arr, size, interp="nearest" , mode="L"):
if len(arr.shape) == 3:
return np.stack([imresize(arr[:,:,channel], size, interp, mode) for channel in range(arr.shape[-1])], axis=-1)
resample_ = {'nearest': PIL.Image.NEAREST, 'lanczos': PIL.Image.LANCZOS, 'bilinear': PIL.Image.BILINEAR, 'bicubic': PIL.Image.BICUBIC, 'cubic': PIL.Image.BICUBIC}[interp]
return np.array(PIL.Image.fromarray(arr,mode=mode).resize(size, resample=resample_))
dataloader.scipy.misc.imresize=imresize
explanation:
this functions supports all the arguments of the original imresize (translating the call to a call to PIL as suggested in the scipy documentation)
The first part is in case the image has several channels, we simply process them one at a time, and use that.
I am trying to optimize a function in python, using optimize.fmin from scipy. The function should optimize a vector of parameters, given initial conditions and arguments. However, I keep receiving the following error when I try to run the optimization, while running the function itself works:
IndexError: too many indices for array, line 1, in parametrization
In brief, my code is like:
import numpy as np # import numpy library
import pandas as pd # import pandas library
from scipy import optimize # import optimize from scipy library
from KF_GATSM import KF_GATSM # import script with Kalman filter
yields=pd.read_excel('data.xlsx',index_col=None,header=None) # Import observed yields
Omega0=pd.read_excel('parameters.xlsx') # Import initial parameters
# Function to optimize
def GATSM(Omega,yields,N):
# recover parameters
Omega=np.matrix(Omega)
muQ,muP=parametrization(N,Omega) # run parametrization
Y=muQ+muP # or any other function
return Y
# Parametrization of the function
def parametrization(nstate,N,Omega):
muQ=np.matrix([[Omega[0,0],0,0]]).T # intercept risk-neutral world
muP=np.matrix([[Omega[1,0],Omega[2,0],Omega[3,0]]]).T # intercept physical world
return muQ,muP
# Run optimization
def MLE(data,Omega0):
# extract number of observations and yields maturities
N=np.shape(yields)[1]
# local optimization
omega_opt=optimize.fmin(GATSM,np.array(Omega0)[:,0],args=(yields,N))
return Y
I solved the issue. It seems that I cannot select the element of an array as follows in Scipy (although it works in Numpy):
Omega[0,0]
Omega[0]
The trick is to use:
Omega.item(0)
I have three files, one being the main file that needs to be run, and the other two contain utility functions, as follows. All the files are in the same directory and I am running it on PyCharm.
# delta_plots.py - This is the main file
...
from delta_plots_utility_1 import *
from delta_plots_utility_2 import *
...
def print_parameter_header(params, flag):
batch_size, epochs, lr = params[0], params[1], params[2]
print("{} - Batch size: {}, Epochs: {}, Learning rate: {}".
format(flag.upper(), batch_size, epochs, lr))
...
if __name__ == '__main__':
# call the utility functions based on a condition
if (condition1):
utility_function_1()
elif (condition2):
utility_function_2()
# delta_plots_utility_1.py - Utility file 1
# this import statement is to import the print_parameter_header() function
# from the main file
from plot_delta_mp import *
def utility_function_1():
# this function makes a call to the print_parameter_header() function
...
print_parameter_header(params, flag)
...
# delta_plots_utility_2.py - Utility file 2
from plot_delta_mp import *
def utility_function_2():
# this function also makes a call to the print_parameter_header() function
...
print_parameter_header(params, flag)
...
The problem is when in the main file, if condition1 is true, then I am forced to put the import statement for utility file 1 before the import statement for utility file 2, and vice versa.
Otherwise, I get the following error:
NameError: name 'print_parameter_header' is not defined
I also tried importing the files as modules and then accessing the function as module.print_parameter_header(), but that does not help either.
I had the following questions regarding this:
From what I understand, the order of the import statements is not important. So why is this happening? Why does changing the order resolve the error?
Could this be because of the loop-like importing? Since I am importing the main file in the utility functions too.
If yes, then is it okay to define print_parameter_header() in the utility files? Although it would be redundant, is that a good practice?
It seems that all of your issues come from that initial misunderstanding: "From what I understand, the order of the import statements is not important."
In python, an import statement
can happen anywhere in the code (not necessarily at the beginning), so if you enter into circular dependency issues it might be a good idea to import the latest possible if you have no other design choice
creates symbols in the code. So from xxx import a will create variable a locally, just like writing a = 0. It is exactly the same.
So maybe a good solution for you would be to stop using from <xxx> import * or import <xxx>, which both import all symbols from the other module, but to import selected symbols in precisely controlled places. Such as from <xxx> import a, b and later in your code from <xxx> import c.
Sorry for not taking the time to adapt the above answer to your precise code example, but hopefully you'll get the idea.
I have a script which generates a series of time dependent plots. I'd like to "stitch" these together to make a movie.
I would preferably like to use matplotlib.animation. I have looked at examples from matplotlib documentation but I can't understand how it works.
My script currently makes 20 plots at successive time values and saves these as 00001.png up to 000020.png:
from scipy.integrate import odeint
from numpy import *
from math import cos
import pylab
omega=1.4
delta=0.1
F=0.35
def f(initial,t):
x,v=initial
xdot=v
vdot=x-x**3-delta*v-F*cos(omega*t)
return array([xdot,vdot])
T=2*pi/omega
nperiods = 100
totalsteps= 1000
small=int((totalsteps)/nperiods)
ntransients= 10
initial=[-1,0]
kArray= linspace(0,1,20)
for g in range (0,20):
k=kArray[g]
x,v=initial
xpc=[]
vpc=[]
if k==0:
x,v=x,v
else:
for i in range(1,nperiods)
x,v=odeint(f,[x,v],linspace(0,k*T,small))[-1] )
for i in range (1,nperiods):
x,v=odeint(f,[x,v],linspace(k*T,T+k*T,small))[-1]
xpc.append(x)
vpc.append(v)
xpc=xpc[ntransients:]
vpc=vpc[ntransients:]
pylab.figure(17.8,10)
pylab.scatter(xpc,vpc,color='red',s=0.2)
pylab.ylim([-1.5,1.5])
pylab.xlim([-2,2])
pylab.savefig('0000{0}.png'.format(g), dpi=200)
I'd appreciate any help. Thank you.
I think matplotlib.animation.FuncAnimation is what you're looking for. Basically, it repeatedly calls a defined function, passing in (optional) arguments as needed. This is exactly what you're already doing in your for g in range(0,20): code. You can also define an init function to get things set up. Check out the base class matplotlib.animation.Animation for more info on formats, saving, the MovieWriter class, etc.