I am trying to use the package pynfft in python 2.7 to do the non-uniform fast Fourier transform (nfft). I have learnt python for only two months, so I have some difficulties.
This is my code:
import numpy as np
from pynfft.nfft import NFFT
#loading data, 104 lines
t_diff, x_diff = np.loadtxt('data/analysis/amplitudes.dat', unpack = True)
N = [13,8]
M = 52
#fourier coefficients
f_hat = np.fft.fft(x_diff)/(2*M)
#instantiation
plan = NFFT(N,M)
#precomputation
x = t_diff
plan.x = x
plan.precompute()
# vector of non uniform samples
f = x_diff[0:M]
#execution
plan.f = f
plan.f_hat = f_hat
f = plan.trafo()
I am basically following the instructions I found in the pynfft tutorial (http://pythonhosted.org/pyNFFT/tutorial.html).
I need the nfft because the time intervals in which my data are taken are not constant (I mean, the first measure is taken at t, the second after dt, the third after dt+dt' with dt' different from dt and so on).
The pynfft package wants the vector of the fourier coefficients ("f_hat") before execution, so I calculated it using numpy.fft, but I am not sure this procedure is correct. Is there another way to do it (maybe with the nfft)?
I would like also to calculate the frquencies; I know that with numpy.fft there is a command: is ther anything like that also for pynfft? I did not find anything in the tutorial.
Thank you for any advice you can give me.
Here is a working example, taken from here:
First we define the function we want to reconstruct, which is the sum of four harmonics:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12345)
%pylab inline --no-import-all
# function we want to reconstruct
k=[1,5,10,30] # modulating coefficients
def myf(x,k):
return sum(np.sin(x*k0*(2*np.pi)) for k0 in k)
x=np.linspace(-0.5,0.5,1000) # 'continuous' time/spatial domain; -0.5<x<+0.5
y=myf(x,k) # 'true' underlying trigonometric function
fig=plt.figure(1,(20,5))
ax =fig.add_subplot(111)
ax.plot(x,y,'red')
ax.plot(x,y,'r.')
# we should sample at a rate of >2*~max(k)
M=256 # number of nodes
N=128 # number of Fourier coefficients
nodes =np.random.rand(M)-0.5 # non-uniform oversampling
values=myf(nodes,k) # nodes&values will be used below to reconstruct
# original function using the Solver
ax.plot(nodes,values,'bo')
ax.set_xlim(-0.5,+0.5)
The we initialize and run the Solver:
from pynfft import NFFT, Solver
f = np.empty(M, dtype=np.complex128)
f_hat = np.empty([N,N], dtype=np.complex128)
this_nfft = NFFT(N=[N,N], M=M)
this_nfft.x = np.array([[node_i,0.] for node_i in nodes])
this_nfft.precompute()
this_nfft.f = f
ret2=this_nfft.adjoint()
print this_nfft.M # number of nodes, complex typed
print this_nfft.N # number of Fourier coefficients, complex typed
#print this_nfft.x # nodes in [-0.5, 0.5), float typed
this_solver = Solver(this_nfft)
this_solver.y = values # '''right hand side, samples.'''
#this_solver.f_hat_iter = f_hat # assign arbitrary initial solution guess, default is 0
this_solver.before_loop() # initialize solver internals
while not np.all(this_solver.r_iter < 1e-2):
this_solver.loop_one_step()
Finally, we display the frequencies:
import matplotlib.pyplot as plt
fig=plt.figure(1,(20,5))
ax =fig.add_subplot(111)
foo=[ np.abs( this_solver.f_hat_iter[i][0])**2 for i in range(len(this_solver.f_hat_iter) ) ]
ax.plot(np.abs(np.arange(-N/2,+N/2,1)),foo)
cheers
Related
I have a 4-4 differential equations system in a function (subsystem4) and I solved it with odeint funtion. I managed to plot the results of the system. My problem is that I want to plot and some other equations (e.g. x,y,vcxdot...) which are included in the same function (subsystem4) but I get NameError: name 'vcxdot' is not defined. Also, I want to use some of these equations (not only the results of the equation's system) as inputs in a following differential equations system and plot all the equations in the same period of time (t). I have done this using Matlab-Simulink but it was much easier because of Simulink blocks. How can I have access to and plot all the equations of a function (subsystem4) and use them as input in a following system? I am new in python and I use Python 2.7.12. Thank you in advance!
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def subsystem4(u,t):
added_mass_x = 0.03 # kg
added_mass_y = 0.04
mb = 0.3 # kg
m1 = mb-added_mass_x
m2 = mb-added_mass_y
l1 = 0.07 # m
l2 = 0.05 # m
J = 0.00050797 # kgm^2
Sa = 0.0110 # m^2
Cd = 2.44
Cl = 3.41
Kd = 0.000655 # kgm^2
r = 1000 # kg/m^3
f = 2 # Hz
c1 = 0.5*r*Sa*Cd
c2 = 0.5*r*Sa*Cl
c3 = 0.5*mb*(l1**2)
c4 = Kd/J
c5 = (1/(2*J))*(l1**2)*mb*l2
c6 = (1/(3*J))*(l1**3)*mb
vcx = u[0]
vcy = u[1]
psi = u[2]
wz = u[3]
x = 3 + 0.3*np.cos(t)
y = 0.5 + 0.3*np.sin(t)
xdot = -0.3*np.sin(t)
ydot = 0.3*np.cos(t)
xdotdot = -0.3*np.cos(t)
ydotdot = -0.3*np.sin(t)
vcx = xdot*np.cos(psi)-ydot*np.sin(psi)
vcy = ydot*np.cos(psi)+xdot*np.sin(psi)
psidot = wz
vcxdot = xdotdot*np.cos(psi)-xdot*np.sin(psi)*psidot-ydotdot*np.sin(psi)-ydot*np.cos(psi)*psidot
vcydot = ydotdot*np.cos(psi)-ydot*np.sin(psi)*psidot+xdotdot*np.sin(psi)+xdot*np.cos(psi)*psidot
g1 = -(m1/c3)*vcxdot+(m2/c3)*vcy*wz-(c1/c3)*vcx*np.sqrt((vcx**2)+(vcy**2))+(c2/c3)*vcy*np.sqrt((vcx**2)+(vcy**2))*np.arctan2(vcy,vcx)
g2 = (m2/c3)*vcydot+(m1/c3)*vcx*wz+(c1/c3)*vcy*np.sqrt((vcx**2)+(vcy**2))+(c2/c3)*vcx*np.sqrt((vcx**2)+(vcy**2))*np.arctan2(vcy,vcx)
A = 12*np.sin(2*np.pi*f*t+np.pi)
if A>=0.1:
wzdot = ((m1-m2)/J)*vcx*vcy-c4*wz**2*np.sign(wz)-c5*g2-c6*np.sqrt((g1**2)+(g2**2))
elif A<-0.1:
wzdot = ((m1-m2)/J)*vcx*vcy-c4*wz**2*np.sign(wz)-c5*g2+c6*np.sqrt((g1**2)+(g2**2))
else:
wzdot = ((m1-m2)/J)*vcx*vcy-c4*wz**2*np.sign(wz)-c5*g2
return [vcxdot,vcydot,psidot,wzdot]
u0 = [0,0,0,0]
t = np.linspace(0,15,1000)
u = odeint(subsystem4,u0,t)
vcx = u[:,0]
vcy = u[:,1]
psi = u[:,2]
wz = u[:,3]
plt.figure(1)
plt.subplot(211)
plt.plot(t,vcx,'r-',linewidth=2,label='vcx')
plt.plot(t,vcy,'b--',linewidth=2,label='vcy')
plt.plot(t,psi,'g:',linewidth=2,label='psi')
plt.plot(t,wz,'c',linewidth=2,label='wz')
plt.xlabel('time')
plt.legend()
plt.show()
To the immediate question of plotting the derivatives, you can get the velocities by directly calling the ODE function again on the solution,
u = odeint(subsystem4,u0,t)
udot = subsystem4(u.T,t)
and get the separate velocity arrays via
vcxdot,vcydot,psidot,wzdot = udot
In this case the function involves branching, which is not very friendly to vectorized calls of it. There are ways to vectorize branching, but the easiest work-around is to loop manually through the solution points, which is slower than a working vectorized implementation. This will again procude a list of tuples like odeint, so the result has to be transposed as a tuple of lists for "easy" assignment to the single array variables.
udot = [ subsystem4(uk, tk) for uk, tk in zip(u,t) ];
vcxdot,vcydot,psidot,wzdot = np.asarray(udot).T
This may appear to double somewhat the computation, but not really, as the solution points are usually interpolated from the internal step points of the solver. The evaluation of the ODE function during integration will usually happen at points that are different from the solution points.
For the other variables, extract the computation of position and velocities into functions to have the constant and composition in one place only:
def xy_pos(t): return 3 + 0.3*np.cos(t), 0.5 + 0.3*np.sin(t)
def xy_vel(t): return -0.3*np.sin(t), 0.3*np.cos(t)
def xy_acc(t): return -0.3*np.cos(t), -0.3*np.sin(t)
or similar that you can then use both inside the ODE function and in preparing the plots.
What Simulink most likely does is to collect all the equations of all the blocks and form this into one big ODE system which is then solved for the whole state at once. You will need to implement something similar. One big state vector, and each subsystem knows its slice of the state resp. derivatives vector to get its specific state variables from and write the derivatives to. The computation of the derivatives can then use values communicated among the subsystems.
What you are trying to do, solving the subsystems separately, works only for resp. will likely result in a order 1 integration method. All higher order methods need to be able to simultaneously shift the state in some direction computed from previous stages of the method, and evaluate the whole system there.
I have generated an ascii file (dat file of size (1407675×3) covariance matrix of size (2055×2055) from gromacs and want to calculate the entropy using the schlitter equation, S=Kb/2*ln det|1 + (Kb*e*e*matrix/hbar*hbar|
But I am getting strange values, could anyone already have a code please share or someone can modify (or check)the code below is much appreciated.
import sys, os
import numpy as np # Import numpy for dot products etc
import numpy.testing as npt # Import numpy.testing for comparing numbers
import pmx # Import pmx classes
import math # Import maths classes (floor)
from numpy.linalg import det
sigma = numpy.loadtxt('covar_fit.dat')
hbar = 1.054571628e-34 #Joules seconds
T = 310.0 # Kelvin
Kb = 1.3806504e-23 #Joules/Kelvin
Euler = 2.71828
matrix = np.resize(sigma,(2055,2055))
k = np.identity(2055)
mat = ((Kb*T*Euler*Euler)*matrix)/(hbar*hbar)
m = np.array(mat)+ np.array(k)
sign,logdet = np.linalg.slogdet(m)
(sign, logdet)
det = logdet
entropy = Kb*0.5*det
print (entropy)
THere is a bug in your code - e in equation is not an exponent (2.71828), but electron charge.
e2/(hbar*c) ~ 1/137 (aka fine-structure constant)
I have a simple Python code which reads two matrices in from a file, and each matrix element contains two undefined variables, A and B. After being read in, A and B are assigned values and the string is evaluated using eval() (I am aware of the security issues with this, but this code will only ever be used by me). A numpy array is formed for each of the matrices and the general eigenvalue problem is solved using both matrices. The code is as follows:
from __future__ import division
from scipy.linalg import eigh
import numpy as np
Mat_size = 4 ## Define matrix size
## Input the two matrices from each side of the generalised eigenvalue problem
HHfile = "Matrix_4x4HH.mat" ## left
SSfile = "Matrix_4x4SS.mat" ## right
## Read matrix files
f1 = open(HHfile, 'r')
HH_data = f1.read()
f2 = open(SSfile, 'r')
SS_data = f2.read()
# Define dictionary of variables
trans = {'A':2.1,
'B':3,
}
HHmat = eval(HH_data, trans)
SSmat = eval(SS_data, trans)
## Convert to numpy array
HHmat2 = np.array(HHmat)
SSmat2 = np.array(SSmat)
## Form correct matrix dimensions
shape = ( Mat_size, Mat_size )
HH = HHmat2.reshape( shape )
SS = SSmat2.reshape( shape )
ev, ew = eigh(HH,SS)
## solve eigenvalue problem
eigenValues,eigenVectors = eigh(HH,SS)
print("Eigenvalue:",eigenValues[0])
This works and outputs an answer. However I wish to vary the parameters A and B to minimise the value that it gives me. (Something like e.g.):
def func_min(x):
A, B = x
ev, ew=eigh(HH,SS)
return ev[0]
x0 = np.array([2.1, 3]) # Preliminary guess
minimize(func_min, x0)
I am having an issue with the fact that A and B have to be defined in order for the eval call to work; thus disallowing the optimisation routine as A and B cannot be varied since they have specified values. I have tried a few different methods of reading in the files (pandas, csv, np.genfromtxt etc...) but always seem to be left with the same issue. Is there a better way, or a modification to the current code to implement this?
Here are the inputs from the files. I was unsure of how to include them, but I can use a file upload service if that is better.
Input from Matrix_4x4HH.mat
-.7500000000000000/B**3*A**5/(A+B)**6-4.500000000000000/B**2*A**4/(A+B)**6-12./B*A**3/(A+B)**6-19.50000000000000*A**2/(A+B)**6-118.5000000000000*B*A/(A+B)**6-19.50000000000000*B**2/(A+B)**6-12.*B**3/A/(A+B)**6-4.500000000000000*B**4/A**2/(A+B)**6-.7500000000000000*B**5/A**3/(A+B)**6+2./B**3*A**4/(A+B)**6+13./B**2*A**3/(A+B)**6+36./B*A**2/(A+B)**6+165.*A/(A+B)**6+165.*B/(A+B)**6+36.*B**2/A/(A+B)**6+13.*B**3/A**2/(A+B)**6+2.*B**4/A**3/(A+B)**6,-.3750000000000000/B**3*A**5/(A+B)**6-2.125000000000000/B**2*A**4/(A+B)**6-4.750000000000000/B*A**3/(A+B)**6-23.87500000000000*A**2/(A+B)**6-1.750000000000000*B*A/(A+B)**6-23.87500000000000*B**2/(A+B)**6-4.750000000000000*B**3/A/(A+B)**6-2.125000000000000*B**4/A**2/(A+B)**6-.3750000000000000*B**5/A**3/(A+B)**6-1.500000000000000/B**2*A**3/(A+B)**6-5.500000000000000/B*A**2/(A+B)**6-25.*A/(A+B)**6-25.*B/(A+B)**6-5.500000000000000*B**2/A/(A+B)**6-1.500000000000000*B**3/A**2/(A+B)**6,1.500000000000000/B**3*A**6/(A+B)**7+10.12500000000000/B**2*A**5/(A+B)**7+29.12500000000000/B*A**4/(A+B)**7+45.25000000000000*A**3/(A+B)**7-135.1250000000000*B*A**2/(A+B)**7+298.7500000000000*B**2*A/(A+B)**7+14.87500000000000*B**3/(A+B)**7-5.750000000000000*B**4/A/(A+B)**7-2.375000000000000*B**5/A**2/(A+B)**7-.3750000000000000*B**6/A**3/(A+B)**7-4./B**3*A**5/(A+B)**7-27./B**2*A**4/(A+B)**7-76.50000000000000/B*A**3/(A+B)**7-9.500000000000000*A**2/(A+B)**7-305.*B*A/(A+B)**7-362.*B**2/(A+B)**7-14.50000000000000*B**3/A/(A+B)**7-1.500000000000000*B**4/A**2/(A+B)**7,-.3750000000000000/B**3*A**6/(A+B)**7-2.375000000000000/B**2*A**5/(A+B)**7-5.750000000000000/B*A**4/(A+B)**7+14.87500000000000*A**3/(A+B)**7+298.7500000000000*B*A**2/(A+B)**7-135.1250000000000*B**2*A/(A+B)**7+45.25000000000000*B**3/(A+B)**7+29.12500000000000*B**4/A/(A+B)**7+10.12500000000000*B**5/A**2/(A+B)**7+1.500000000000000*B**6/A**3/(A+B)**7-1.500000000000000/B**2*A**4/(A+B)**7-14.50000000000000/B*A**3/(A+B)**7-362.*A**2/(A+B)**7-305.*B*A/(A+B)**7-9.500000000000000*B**2/(A+B)**7-76.50000000000000*B**3/A/(A+B)**7-27.*B**4/A**2/(A+B)**7-4.*B**5/A**3/(A+B)**7,-.3750000000000000/B**3*A**5/(A+B)**6-2.125000000000000/B**2*A**4/(A+B)**6-4.750000000000000/B*A**3/(A+B)**6-23.87500000000000*A**2/(A+B)**6-1.750000000000000*B*A/(A+B)**6-23.87500000000000*B**2/(A+B)**6-4.750000000000000*B**3/A/(A+B)**6-2.125000000000000*B**4/A**2/(A+B)**6-.3750000000000000*B**5/A**3/(A+B)**6-1.500000000000000/B**2*A**3/(A+B)**6-5.500000000000000/B*A**2/(A+B)**6-25.*A/(A+B)**6-25.*B/(A+B)**6-5.500000000000000*B**2/A/(A+B)**6-1.500000000000000*B**3/A**2/(A+B)**6,-1.500000000000000/B**3*A**5/(A+B)**6-10.50000000000000/B**2*A**4/(A+B)**6-35./B*A**3/(A+B)**6-101.5000000000000*A**2/(A+B)**6-343.*B*A/(A+B)**6-101.5000000000000*B**2/(A+B)**6-35.*B**3/A/(A+B)**6-10.50000000000000*B**4/A**2/(A+B)**6-1.500000000000000*B**5/A**3/(A+B)**6+2./B**3*A**4/(A+B)**6+16./B**2*A**3/(A+B)**6+45./B*A**2/(A+B)**6+201.*A/(A+B)**6+201.*B/(A+B)**6+45.*B**2/A/(A+B)**6+16.*B**3/A**2/(A+B)**6+2.*B**4/A**3/(A+B)**6,.7500000000000000/B**3*A**6/(A+B)**7+5.625000000000000/B**2*A**5/(A+B)**7+18.87500000000000/B*A**4/(A+B)**7+19.62500000000000*A**3/(A+B)**7+170.3750000000000*B*A**2/(A+B)**7+73.87500000000000*B**2*A/(A+B)**7+57.62500000000000*B**3/(A+B)**7+4.875000000000000*B**4/A/(A+B)**7+.3750000000000000*B**5/A**2/(A+B)**7+1.500000000000000/B**2*A**4/(A+B)**7+7.500000000000000/B*A**3/(A+B)**7-1.*A**2/(A+B)**7+39.*B*A/(A+B)**7+47.50000000000000*B**2/(A+B)**7+1.500000000000000*B**3/A/(A+B)**7,.3750000000000000/B**2*A**5/(A+B)**7+4.875000000000000/B*A**4/(A+B)**7+57.62500000000000*A**3/(A+B)**7+73.87500000000000*B*A**2/(A+B)**7+170.3750000000000*B**2*A/(A+B)**7+19.62500000000000*B**3/(A+B)**7+18.87500000000000*B**4/A/(A+B)**7+5.625000000000000*B**5/A**2/(A+B)**7+.7500000000000000*B**6/A**3/(A+B)**7+1.500000000000000/B*A**3/(A+B)**7+47.50000000000000*A**2/(A+B)**7+39.*B*A/(A+B)**7-1.*B**2/(A+B)**7+7.500000000000000*B**3/A/(A+B)**7+1.500000000000000*B**4/A**2/(A+B)**7,1.500000000000000/B**3*A**6/(A+B)**7+10.12500000000000/B**2*A**5/(A+B)**7+29.12500000000000/B*A**4/(A+B)**7+45.25000000000000*A**3/(A+B)**7-135.1250000000000*B*A**2/(A+B)**7+298.7500000000000*B**2*A/(A+B)**7+14.87500000000000*B**3/(A+B)**7-5.750000000000000*B**4/A/(A+B)**7-2.375000000000000*B**5/A**2/(A+B)**7-.3750000000000000*B**6/A**3/(A+B)**7-4./B**3*A**5/(A+B)**7-27./B**2*A**4/(A+B)**7-76.50000000000000/B*A**3/(A+B)**7-9.500000000000000*A**2/(A+B)**7-305.*B*A/(A+B)**7-362.*B**2/(A+B)**7-14.50000000000000*B**3/A/(A+B)**7-1.500000000000000*B**4/A**2/(A+B)**7,.7500000000000000/B**3*A**6/(A+B)**7+5.625000000000000/B**2*A**5/(A+B)**7+18.87500000000000/B*A**4/(A+B)**7+19.62500000000000*A**3/(A+B)**7+170.3750000000000*B*A**2/(A+B)**7+73.87500000000000*B**2*A/(A+B)**7+57.62500000000000*B**3/(A+B)**7+4.875000000000000*B**4/A/(A+B)**7+.3750000000000000*B**5/A**2/(A+B)**7+1.500000000000000/B**2*A**4/(A+B)**7+7.500000000000000/B*A**3/(A+B)**7-1.*A**2/(A+B)**7+39.*B*A/(A+B)**7+47.50000000000000*B**2/(A+B)**7+1.500000000000000*B**3/A/(A+B)**7,-5.250000000000000/B**3/(A+B)**8*A**7-40.50000000000000/B**2/(A+B)**8*A**6-138.7500000000000/B/(A+B)**8*A**5-280.7500000000000/(A+B)**8*A**4-629.7500000000000*B/(A+B)**8*A**3+770.2500000000000*B**2/(A+B)**8*A**2-836.7500000000000*B**3/(A+B)**8*A-180.2500000000000*B**4/(A+B)**8-52.*B**5/(A+B)**8/A-12.75000000000000*B**6/(A+B)**8/A**2-1.500000000000000*B**7/(A+B)**8/A**3+14./B**3/(A+B)**8*A**6+107./B**2/(A+B)**8*A**5+355./B/(A+B)**8*A**4+778./(A+B)**8*A**3+284.*B/(A+B)**8*A**2+597.*B**2/(A+B)**8*A+911.*B**3/(A+B)**8+100.*B**4/(A+B)**8/A+20.*B**5/(A+B)**8/A**2+2.*B**6/(A+B)**8/A**3,.7500000000000000/B**3/(A+B)**8*A**7+5.625000000000000/B**2/(A+B)**8*A**6+18.25000000000000/B/(A+B)**8*A**5+52.50000000000000/(A+B)**8*A**4+397.*B/(A+B)**8*A**3-2356.250000000000*B**2/(A+B)**8*A**2+397.*B**3/(A+B)**8*A+52.50000000000000*B**4/(A+B)**8+18.25000000000000*B**5/(A+B)**8/A+5.625000000000000*B**6/(A+B)**8/A**2+.7500000000000000*B**7/(A+B)**8/A**3+1.500000000000000/B**2/(A+B)**8*A**5+10.50000000000000/B/(A+B)**8*A**4-276.5000000000000/(A+B)**8*A**3+1848.500000000000*B/(A+B)**8*A**2+1848.500000000000*B**2/(A+B)**8*A-276.5000000000000*B**3/(A+B)**8+10.50000000000000*B**4/(A+B)**8/A+1.500000000000000*B**5/(A+B)**8/A**2,-.3750000000000000/B**3*A**6/(A+B)**7-2.375000000000000/B**2*A**5/(A+B)**7-5.750000000000000/B*A**4/(A+B)**7+14.87500000000000*A**3/(A+B)**7+298.7500000000000*B*A**2/(A+B)**7-135.1250000000000*B**2*A/(A+B)**7+45.25000000000000*B**3/(A+B)**7+29.12500000000000*B**4/A/(A+B)**7+10.12500000000000*B**5/A**2/(A+B)**7+1.500000000000000*B**6/A**3/(A+B)**7-1.500000000000000/B**2*A**4/(A+B)**7-14.50000000000000/B*A**3/(A+B)**7-362.*A**2/(A+B)**7-305.*B*A/(A+B)**7-9.500000000000000*B**2/(A+B)**7-76.50000000000000*B**3/A/(A+B)**7-27.*B**4/A**2/(A+B)**7-4.*B**5/A**3/(A+B)**7,.3750000000000000/B**2*A**5/(A+B)**7+4.875000000000000/B*A**4/(A+B)**7+57.62500000000000*A**3/(A+B)**7+73.87500000000000*B*A**2/(A+B)**7+170.3750000000000*B**2*A/(A+B)**7+19.62500000000000*B**3/(A+B)**7+18.87500000000000*B**4/A/(A+B)**7+5.625000000000000*B**5/A**2/(A+B)**7+.7500000000000000*B**6/A**3/(A+B)**7+1.500000000000000/B*A**3/(A+B)**7+47.50000000000000*A**2/(A+B)**7+39.*B*A/(A+B)**7-1.*B**2/(A+B)**7+7.500000000000000*B**3/A/(A+B)**7+1.500000000000000*B**4/A**2/(A+B)**7,.7500000000000000/B**3/(A+B)**8*A**7+5.625000000000000/B**2/(A+B)**8*A**6+18.25000000000000/B/(A+B)**8*A**5+52.50000000000000/(A+B)**8*A**4+397.*B/(A+B)**8*A**3-2356.250000000000*B**2/(A+B)**8*A**2+397.*B**3/(A+B)**8*A+52.50000000000000*B**4/(A+B)**8+18.25000000000000*B**5/(A+B)**8/A+5.625000000000000*B**6/(A+B)**8/A**2+.7500000000000000*B**7/(A+B)**8/A**3+1.500000000000000/B**2/(A+B)**8*A**5+10.50000000000000/B/(A+B)**8*A**4-276.5000000000000/(A+B)**8*A**3+1848.500000000000*B/(A+B)**8*A**2+1848.500000000000*B**2/(A+B)**8*A-276.5000000000000*B**3/(A+B)**8+10.50000000000000*B**4/(A+B)**8/A+1.500000000000000*B**5/(A+B)**8/A**2,-1.500000000000000/B**3/(A+B)**8*A**7-12.75000000000000/B**2/(A+B)**8*A**6-52./B/(A+B)**8*A**5-180.2500000000000/(A+B)**8*A**4-836.7500000000000*B/(A+B)**8*A**3+770.2500000000000*B**2/(A+B)**8*A**2-629.7500000000000*B**3/(A+B)**8*A-280.7500000000000*B**4/(A+B)**8-138.7500000000000*B**5/(A+B)**8/A-40.50000000000000*B**6/(A+B)**8/A**2-5.250000000000000*B**7/(A+B)**8/A**3+2./B**3/(A+B)**8*A**6+20./B**2/(A+B)**8*A**5+100./B/(A+B)**8*A**4+911./(A+B)**8*A**3+597.*B/(A+B)**8*A**2+284.*B**2/(A+B)**8*A+778.*B**3/(A+B)**8+355.*B**4/(A+B)**8/A+107.*B**5/(A+B)**8/A**2+14.*B**6/(A+B)**8/A**3
Input from Matrix_4x4SS.mat
1./B**3*A**3/(A+B)**6+6./B**2*A**2/(A+B)**6+15./B*A/(A+B)**6+84./(A+B)**6+15.*B/A/(A+B)**6+6.*B**2/A**2/(A+B)**6+1.*B**3/A**3/(A+B)**6,-.5000000000000000/B**3*A**3/(A+B)**6-3.500000000000000/B**2*A**2/(A+B)**6-9.500000000000000/B*A/(A+B)**6-53./(A+B)**6-9.500000000000000*B/A/(A+B)**6-3.500000000000000*B**2/A**2/(A+B)**6-.5000000000000000*B**3/A**3/(A+B)**6,-2./B**3*A**4/(A+B)**7-12.50000000000000/B**2*A**3/(A+B)**7-32.50000000000000/B*A**2/(A+B)**7+18.50000000000000*A/(A+B)**7-253.*B/(A+B)**7-17.50000000000000*B**2/A/(A+B)**7-4.500000000000000*B**3/A**2/(A+B)**7-.5000000000000000*B**4/A**3/(A+B)**7,-.5000000000000000/B**3*A**4/(A+B)**7-4.500000000000000/B**2*A**3/(A+B)**7-17.50000000000000/B*A**2/(A+B)**7-253.*A/(A+B)**7+18.50000000000000*B/(A+B)**7-32.50000000000000*B**2/A/(A+B)**7-12.50000000000000*B**3/A**2/(A+B)**7-2.*B**4/A**3/(A+B)**7,-.5000000000000000/B**3*A**3/(A+B)**6-3.500000000000000/B**2*A**2/(A+B)**6-9.500000000000000/B*A/(A+B)**6-53./(A+B)**6-9.500000000000000*B/A/(A+B)**6-3.500000000000000*B**2/A**2/(A+B)**6-.5000000000000000*B**3/A**3/(A+B)**6,2./B**3*A**3/(A+B)**6+14./B**2*A**2/(A+B)**6+38./B*A/(A+B)**6+212./(A+B)**6+38.*B/A/(A+B)**6+14.*B**2/A**2/(A+B)**6+2.*B**3/A**3/(A+B)**6,1./B**3*A**4/(A+B)**7+6.500000000000000/B**2*A**3/(A+B)**7+16.50000000000000/B*A**2/(A+B)**7-19.*A/(A+B)**7+118.*B/(A+B)**7+4.500000000000000*B**2/A/(A+B)**7+.5000000000000000*B**3/A**2/(A+B)**7,.5000000000000000/B**2*A**3/(A+B)**7+4.500000000000000/B*A**2/(A+B)**7+118.*A/(A+B)**7-19.*B/(A+B)**7+16.50000000000000*B**2/A/(A+B)**7+6.500000000000000*B**3/A**2/(A+B)**7+1.*B**4/A**3/(A+B)**7,-2./B**3*A**4/(A+B)**7-12.50000000000000/B**2*A**3/(A+B)**7-32.50000000000000/B*A**2/(A+B)**7+18.50000000000000*A/(A+B)**7-253.*B/(A+B)**7-17.50000000000000*B**2/A/(A+B)**7-4.500000000000000*B**3/A**2/(A+B)**7-.5000000000000000*B**4/A**3/(A+B)**7,1./B**3*A**4/(A+B)**7+6.500000000000000/B**2*A**3/(A+B)**7+16.50000000000000/B*A**2/(A+B)**7-19.*A/(A+B)**7+118.*B/(A+B)**7+4.500000000000000*B**2/A/(A+B)**7+.5000000000000000*B**3/A**2/(A+B)**7,7./B**3/(A+B)**8*A**5+50./B**2/(A+B)**8*A**4+154./B/(A+B)**8*A**3+331./(A+B)**8*A**2-147.*B/(A+B)**8*A+848.*B**2/(A+B)**8+80.*B**3/(A+B)**8/A+19.*B**4/(A+B)**8/A**2+2.*B**5/(A+B)**8/A**3,1./B**3/(A+B)**8*A**5+8.500000000000000/B**2/(A+B)**8*A**4+31./B/(A+B)**8*A**3-152.5000000000000/(A+B)**8*A**2+1568.*B/(A+B)**8*A-152.5000000000000*B**2/(A+B)**8+31.*B**3/(A+B)**8/A+8.500000000000000*B**4/(A+B)**8/A**2+1.*B**5/(A+B)**8/A**3,-.5000000000000000/B**3*A**4/(A+B)**7-4.500000000000000/B**2*A**3/(A+B)**7-17.50000000000000/B*A**2/(A+B)**7-253.*A/(A+B)**7+18.50000000000000*B/(A+B)**7-32.50000000000000*B**2/A/(A+B)**7-12.50000000000000*B**3/A**2/(A+B)**7-2.*B**4/A**3/(A+B)**7,.5000000000000000/B**2*A**3/(A+B)**7+4.500000000000000/B*A**2/(A+B)**7+118.*A/(A+B)**7-19.*B/(A+B)**7+16.50000000000000*B**2/A/(A+B)**7+6.500000000000000*B**3/A**2/(A+B)**7+1.*B**4/A**3/(A+B)**7,1./B**3/(A+B)**8*A**5+8.500000000000000/B**2/(A+B)**8*A**4+31./B/(A+B)**8*A**3-152.5000000000000/(A+B)**8*A**2+1568.*B/(A+B)**8*A-152.5000000000000*B**2/(A+B)**8+31.*B**3/(A+B)**8/A+8.500000000000000*B**4/(A+B)**8/A**2+1.*B**5/(A+B)**8/A**3,2./B**3/(A+B)**8*A**5+19./B**2/(A+B)**8*A**4+80./B/(A+B)**8*A**3+848./(A+B)**8*A**2-147.*B/(A+B)**8*A+331.*B**2/(A+B)**8+154.*B**3/(A+B)**8/A+50.*B**4/(A+B)**8/A**2+7.*B**5/(A+B)**8/A**3
Edit
I guess the important part of this question is how to read in an array of equations from a file into a numpy array and be able to assign values to the variables without the need for eval. As an example of what can usually be done using numpy:
import numpy as np
A = 1
B = 1
arr = np.array([[A+2*B,2],[2,B+6*(B+A)]])
print arr
This gives:
[[ 3 2]
[ 2 13]]
The numpy array contained algebraic terms and when printed the specified values of A and B were inserted. Is this possible with equations read from a file into a numpy array? I have looked through the numpy documentation and is the issue to do with the type of data being read in? i.e. I cannot specify the dtype to be int or float as each array element contains ints, floats and text. I feel there is a very simple aspect of Python I am missing that can do this.
I have an image of land cover and I segmented it using K-means clustering. Now I want to calculate the accuracy of my segmentation algorithm. I read somewhere that dice co-efficient is the substantive evaluation measure. But I am not sure how to calculate it.
I use Python 2.7
Are there any other effective evaluation methods? Please give a summary or a link to a source. Thank You!
Edits:
I used the following code for measuring the dice similarity for my original and the segmented image but it seems to take hours to calculate:
for i in xrange(0,7672320):
for j in xrange(0,3):
dice = np.sum([seg==gt])*2.0/(np.sum(seg)+np.sum(gt)) #seg is the segmented image and gt is the original image. Both are of same size
Please refer to Dice similarity coefficient at wiki
A sample code segment here for your reference. Please note that you need to replace k with your desired cluster since you are using k-means.
import numpy as np
k=1
# segmentation
seg = np.zeros((100,100), dtype='int')
seg[30:70, 30:70] = k
# ground truth
gt = np.zeros((100,100), dtype='int')
gt[30:70, 40:80] = k
dice = np.sum(seg[gt==k])*2.0 / (np.sum(seg) + np.sum(gt))
print 'Dice similarity score is {}'.format(dice)
If you are working with opencv you could use the following function:
import cv2
import numpy as np
#load images
y_pred = cv2.imread('predictions/image_001.png')
y_true = cv2.imread('ground_truth/image_001.png')
# Dice similarity function
def dice(pred, true, k = 1):
intersection = np.sum(pred[true==k]) * 2.0
dice = intersection / (np.sum(pred) + np.sum(true))
return dice
dice_score = dice(y_pred, y_true, k = 255) #255 in my case, can be 1
print ("Dice Similarity: {}".format(dice_score))
In case you want to evaluate with this metric within a deep learning model using tensorflow you can use the following:
def dice_coef(y_true, y_pred):
y_true_f = tf.reshape(tf.dtypes.cast(y_true, tf.float32), [-1])
y_pred_f = tf.reshape(tf.dtypes.cast(y_pred, tf.float32), [-1])
intersection = tf.reduce_sum(y_true_f * y_pred_f)
return (2. * intersection + 1.) / (tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f) + 1.)
This is an important clarification if what you're using has more than 2 classes (aka, a mask with 1 and 0).
If you are using multiple classes, make sure to specify that the prediction and ground truth also equal the value which you want. Otherwise you can end up getting DSC values greater than 1.
This is the extra ==k at the end of each [] statement:
import numpy as np
k=1
# segmentation
seg = np.zeros((100,100), dtype='int')
seg[30:70, 30:70] = k
# ground truth
gt = np.zeros((100,100), dtype='int')
gt[30:70, 40:80] = k
dice = np.sum(seg[gt==k]==k)*2.0 / (np.sum(seg[seg==k]==k) + np.sum(gt[gt==k]==k))
print 'Dice similarity score is {}'.format(dice)
I am using matplotlib.pyplot to create histograms. I'm not actually interested in the plots of these histograms, but interested in the frequencies and bins (I know I can write my own code to do this, but would prefer to use this package).
I know I can do the following,
import numpy as np
import matplotlib.pyplot as plt
x1 = np.random.normal(1.5,1.0)
x2 = np.random.normal(0,1.0)
freq, bins, patches = plt.hist([x1,x1],50,histtype='step')
to create a histogram. All I need is freq[0], freq[1], and bins[0]. The problem occurs when I try and use,
freq, bins, patches = plt.hist([x1,x1],50,histtype='step')
in a function. For example,
def func(x, y, Nbins):
freq, bins, patches = plt.hist([x,y],Nbins,histtype='step') # create histogram
bincenters = 0.5*(bins[1:] + bins[:-1]) # center bins
xf= [float(i) for i in freq[0]] # convert integers to float
xf = [float(i) for i in freq[1]]
p = [ (bincenters[j], (1.0 / (xf[j] + yf[j] )) for j in range(Nbins) if (xf[j] + yf[j]) != 0]
Xt = [j for i,j in p] # separate pairs formed in p
Yt = [i for i,j in p]
Y = np.array(Yt) # convert to arrays for later fitting
X = np.array(Xt)
return X, Y # return arrays X and Y
When I call func(x1,x2,Nbins) and plot or print X and Y, I do not get my expected curve/values. I suspect it something to do with plt.hist, since there is a partial histogram in my plot.
I don't know if I'm understanding your question very well, but here, you have an example of a very simple home-made histogram (in 1D or 2D), each one inside a function, and properly called:
import numpy as np
import matplotlib.pyplot as plt
def func2d(x, y, nbins):
histo, xedges, yedges = np.histogram2d(x,y,nbins)
plt.plot(x,y,'wo',alpha=0.3)
plt.imshow(histo.T,
extent=[xedges.min(),xedges.max(),yedges.min(),yedges.max()],
origin='lower',
interpolation='nearest',
cmap=plt.cm.hot)
plt.show()
def func1d(x, nbins):
histo, bin_edges = np.histogram(x,nbins)
bin_center = 0.5*(bin_edges[1:] + bin_edges[:-1])
plt.step(bin_center,histo,where='mid')
plt.show()
x = np.random.normal(1.5,1.0, (1000,1000))
func1d(x[0],40)
func2d(x[0],x[1],40)
Of course, you may check if the centering of the data is right, but I think that the example shows some useful things about this topic.
My recommendation: Try to avoid any loop in your code! They kill the performance. If you look, In my example there aren't loops. The best practice in numerical problems with python is avoiding loops! Numpy has a lot of C-implemented functions that do all the hard looping work.
You can use np.histogram2d (for 2D histogram) or np.histogram (for 1D histogram):
hst = np.histogram(A, bins)
hst2d = np.histogram2d(X,Y,bins)
Output form will be the same as plt.hist and plt.hist2d, the only difference is there is no plot.
No.
But you can bypass the pyplot:
import matplotlib.pyplot
fig = matplotlib.figure.Figure()
ax = matplotlib.axes.Axes(fig, (0,0,0,0))
numeric_results = ax.hist(data)
del ax, fig
It won't impact active axes and figures, so it would be ok to use it even in the middle of plotting something else.
This is because any usage of plt.draw_something() will put the plot in current axis - which is a global variable.
If you would like to simply compute the histogram (that is, count the number of points in a given bin) and not display it, the np.histogram() function is available