Re: [Tutor] Fitting data to error function

2015-03-15 Thread Colin Ross
Hi Danny,

Thanks for the help! As you mentioned, using scipy.special.erfc was a much
better idea. Below is a copy of my program and the stack trace, showing a
new error. It seems that the first auto correlation works,  however the
second fails.

###

# Autocorrelation program

###


import numpy as np

from numpy import ma, logical_or

import pylab

from pylab import *

import matplotlib.pyplot as plt

from matplotlib.ticker import AutoMinorLocator

import math

from math import exp

import scipy

from scipy.integrate import quad,fixed_quad

import sys

from scipy.optimize import curve_fit

from scipy.fftpack import fft, ifft, fftfreq


##

#fitting function (gaussian)

##


def fit(x,a,b,c):

return a*np.exp(-(x-b)**2/c)

#(1.0/(np.sqrt(2*np.pi)*sigma))*np.exp(-(x-mu)**2 / (2*sigma**2))


##

# Load data from .txt file

##


data1 = np.loadtxt('TL_pulseC.txt',skiprows = 2 ,usecols = (0,1))


data2 = np.loadtxt('cos_pulseC.txt',skiprows = 2 ,usecols = (0,1))


#print \n  Stage Position (um) Amplitude (V)

#print data




# Create data arrays




pos = np.array(data1[:,0])   # micrometers


pos_m = pos*1.0*10**(-6) # meters


print pos_m


amp = np.array(data1[:,1])   # V


amp_auto = np.array(data2[:,1])




#

# Finding the index of the maximum amplitude where the pulse delay is zero.
Then calculating the

# mean to shift the curve accordingly.

#


peak_id = np.where(amp == np.max(amp))[0][0]

mean = pos_m[peak_id]

print mean


dif = pos_m - mean


t_d =(2.*dif)/(2.99*10**8)*10**(12.) # ps


print t_d


t = np.arange(0.,0.5,1/float(2*len(t_d)))# picoseconds (ps)




# Define constants




c = 2.99*10**8  # m/s

alpha = np.pi   # rad

gamma = 200.0*10**(-15.)

lamb_o = 1160.00*10**(-9.)  # m

omega_o = ((2.*np.pi*c)/lamb_o)*1.*10**(-12.)   # ps^-1

delta = np.pi/2.# rad

FWHM = 32.53*10**(-6)   # m

t_p = ((FWHM)/c)*10**(12.)  # ps


print t_p


E_norm = 1.   # normalized




# Define functions






# Electric field




def E_o(x):

return E_norm*(cosh(1.76*x/t_p))**(-1.)


# Real part of electric field


def E_rin(x):

return (1./2.)*E_o(x)*cos(omega_o*x)


# Imaginary part of electric field


def E_iin(x):

return (1./2.)*E_o(x)*sin(omega_o*x)


# Total electric field


def E_(x):

return (1./2.)*E_o(x)*np.exp(-1j*omega_o*x)




# Autocorrelation




'''

def integrand(t,t_d):

return abs(E_rin(t))**2.*abs(E_rin(t - t_d))**2.

'''


def integrand(x,y):

return abs(E_(x))**2.*abs(E_(x - y))**2.



#integrand = abs(E_(t))**2.*abs(E_(t - t_d))**2.


def G(y):

return quad(integrand, -np.infty, np.infty, args=(y))



G_plot = []


for tau in t_d:

integral,error = G(tau)

G_plot.append(integral)


#fit data

params = curve_fit(fit,pos[174-100:174+100],amp[174-100:174+100],p0=[0.003,
8550,350]) #function, xdata, ydata, initial guess (from plot)

#parameters

[a,b,d] = params[0]

#error

[da,db,dd] = np.sqrt(np.diag(params[1]))


#

# Shaped electric field

#


alpha = np.pi

delta = np.pi/2.

gamma = 200.*10**(-15)   # sec


dt = (t[1]-t[0])*(1*10**(-12))


def phi(x):

return alpha*np.cos(gamma*(x - omega_o) - delta)


def M(x):

return np.exp(1j*phi(x))


def E_out(x):

E_in_w = fft(E_(x))

omega = fftfreq(len(x),dt)*2*np.pi

E_out_w = E_in_w*M(omega)

return ifft(E_out_w)


#

# Second autocorrelation

#


def integrand1(x,y):

return abs(E_out(x))**2.*abs(E_out(x - y))**2.


def G1(y):

return quad(integrand1, -np.infty, np.infty, args=(y))


G_plot_1 = []


for tau in t_d:

integral,error = G1(tau)

G_plot_1.append(integral)


#

# Plotting data

#



# Defining x and y minorlocator



xminorLocator = AutoMinorLocator()

yminorLocator = AutoMinorLocator()



# Setting minor ticks


ax = plt.subplot(111)

ax.xaxis.set_minor_locator(xminorLocator)

ax.yaxis.set_minor_locator(yminorLocator)

sample_pos = np.linspace(min(pos),max(pos),1000)


[Tutor] print method in Python2.7 problem

2015-03-15 Thread Doug Basberg
Stat = {'Vbatt': 51.24, 'Ichrg': 6.75}

print '   td style=text-align: center;SOLAR PANEL VOLTAGE/td'
print('DSB HI; %s') % (str(Stat['Vbatt']))
print('   td style=width: 50%; text-align: center;%s/td') % 
(str(Stat['Vbatt']))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print method in Python2.7 problem

2015-03-15 Thread Mark Lawrence

On 15/03/2015 17:40, Mark Lawrence wrote:

On 15/03/2015 16:46, Doug Basberg wrote:

Nothing because it was in a completely unnecessary attachment (some
people won't even receive it), so here's the code.

Stat = {'Vbatt': 51.24, 'Ichrg': 6.75}

print '   td style=text-align: center;SOLAR PANEL VOLTAGE/td'
print('DSB HI; %s') % (str(Stat['Vbatt']))
print('   td style=width: 50%; text-align: center;%s/td') %
(str(Stat['Vbatt']))

He also didn't bother specifying the actual problem,but like others here
we just love guessing games.  So I'll go for:-

TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

Print in 2.7 is a statement so the first one is fine.  The second (and
third) you're trying to apply the % operator to the print statememt
which by definition is None, hence the error.  You also don't need the
call to str so put it all together it simply becomes.

print 'DSB HI; %s' % Stat['Vbatt']

I'll leave you to come back with the next problem as I haven't worked
out what the solution is yet :)



Having read Steven D'Aprano's reply I have now, I was mixing Python 2 
and 3, whoops, sorry about that!!!


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print method in Python2.7 problem

2015-03-15 Thread Alan Gauld

To be fair to Doug his mails aren't cp,ing through to the list fully.
I susp3ect because he is using an Outlook message format with lots of 
fancy formatting and a background all of which are being sent as 
attachments...


Here is his text (with a lot of extraneous whitespace removed):

-

For some reason my e-mails to this forum seem to have a problem,
I may sign up again to see if something is wrong with my account.
Mean while can the moderator cut and paste so members can see my post? 
Thank you.  Do you know what my account problem is?  Thank you, Doug 
Basberg dbasb...@comcast.net


I am attempting to write web pages in Python and inserting
numbers from a dictionary read in this program and populated
in another using a classes methods for read and write.
In this process, I found that in Python 2.7 on my Raspberry Pi,
I have a problem with a print statement.  I have attached a
very small test program exhibiting the same problem.  The
error message tells me that the semicolon is a problem.
It is in a single quoted string, so it should not be interpreted ??
I know that %xxx is interpreted inside strings of print
statements.  Are there other special characters (like %) that
I am not aware of?

I can sure use some help with this.  Thanks, Doug

File attached and shown as text below:

Stat = {'Vbatt': 51.24, 'Ichrg': 6.75}
print '   td style=text-align: center;SOLAR PANEL VOLTAGE/td'
print('DSB HI; %s') % (str(Stat['Vbatt']))
print('   td style=width: 50%; text-align: center;%s/td') %
(str(Stat['Vbatt']))

Running the program:

$ python dsbtest02.py
   td style=text-align: center;SOLAR PANEL VOLTAGE/td
DSB HI; 51.24
Traceback (most recent call last):
  File dsbtest02.py, line 5, in module
print('   td style=width: 50%; text-align: center;%s/td') %
(str(Stat['Vbatt']))
ValueError: unsupported format character ';' (0x3b) at index 24

-

The solution (to the messaging issue) is if Doug uses plain text
for posts to the list in future.

The solution for the code issue is, as Steven says,
to use %% to escape the percent sign.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Ipython Queries

2015-03-15 Thread Santosh Kumar
Thank you All. I forgot to update you guys , i use ubuntu 14.10 .
I believe/was under assumption that we cannot used ipython both for python
2.x and python 3.x.
So ipython is for python 2.x and ipython3 is for python 3.x

Thanks,
santosh

On Sun, Mar 15, 2015 at 3:47 AM, Oscar Benjamin oscar.j.benja...@gmail.com
wrote:

 On 14 March 2015 at 09:53, Steven D'Aprano st...@pearwood.info wrote:
  On Fri, Mar 13, 2015 at 10:39:50PM +0530, Santosh Kumar wrote:
  Hi All,
 
  I have installed both python2 and python3 in my system . When i used
  ipython it by default goes to python2 base. How/what is the easy way to
  swith between python2 and python3 ?
 
  I don't think there is an easy way on Windows, but I could be wrong. You
  could try asking on a dedicated iPython mailing list. If you get an
  answer, please come back and tell us here so we can learn too.

 When you install ipython (or any Python package) you install it for a
 particular Python version. You haven't said how you installed either
 of the Python versions or ipython or which version of Python you
 installed ipython for.

 Assuming that you have installed ipython for Python 3 then you may be
 able to run ipython for Python 3 by typing one of the following in a
 terminal:

 $ ipython3

 $ python3 -m IPython

 $ py -3 -m IPython

 (Note carefully that the I and P at the start of IPython in the last
 two examples are capitalised - the first example is all lower case).

 You may find that ipython is in the programs menu on Windows. I'm
 not really sure how that stuff works on newer versions of Windows
 though.

 Without knowing more about your setup it's hard to be specific about
 what you should do. What OS are you using (e.g. Windows 8?)? How did
 you install Python 2 and Python 3 and which versions (e.g. 2.7 and
 3.4?). How did you install ipython? Which version of Python did you
 install it for? Do you know what folder the Python installations are
 in (e.g. C:\Python34)?


 Oscar
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor




-- 
D. Santosh Kumar
RHCE | SCSA
+91-9703206361


Every task has a unpleasant side .. But you must focus on the end result
you are producing.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print method in Python2.7 problem

2015-03-15 Thread Mark Lawrence

On 15/03/2015 16:46, Doug Basberg wrote:

Nothing because it was in a completely unnecessary attachment (some 
people won't even receive it), so here's the code.


Stat = {'Vbatt': 51.24, 'Ichrg': 6.75}

print '   td style=text-align: center;SOLAR PANEL VOLTAGE/td'
print('DSB HI; %s') % (str(Stat['Vbatt']))
print('   td style=width: 50%; text-align: center;%s/td') % 
(str(Stat['Vbatt']))


He also didn't bother specifying the actual problem,but like others here 
we just love guessing games.  So I'll go for:-


TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

Print in 2.7 is a statement so the first one is fine.  The second (and 
third) you're trying to apply the % operator to the print statememt 
which by definition is None, hence the error.  You also don't need the 
call to str so put it all together it simply becomes.


print 'DSB HI; %s' % Stat['Vbatt']

I'll leave you to come back with the next problem as I haven't worked 
out what the solution is yet :)


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print method in Python2.7 problem

2015-03-15 Thread Steven D'Aprano
On Sun, Mar 15, 2015 at 12:46:04PM -0400, Doug Basberg wrote:
 Stat = {'Vbatt': 51.24, 'Ichrg': 6.75}
 print '   td style=text-align: center;SOLAR PANEL VOLTAGE/td'
 print('DSB HI; %s') % (str(Stat['Vbatt']))
 print('   td style=width: 50%; text-align: center;%s/td') % 
 (str(Stat['Vbatt']))

Yes? What about it? Do you have a question or do you expect us to read 
your mind?

If the second, this is your lucky day, because in fact I am a powerful 
psychic, and by reading your mind I can see that you are getting a 
ValueError exception from the last line:

ValueError: unsupported format character ';' (0x3b) at index 24


By using another of my superpowers, called reading the error message, 
I can work out the nature of the problem: you have a format command %; 
which isn't supported.


When using string interpolation with the % operator, it looks for % 
formatting characters in the string. %s you already know. %d needs an 
integer value, %f a float. And %; isn't supported at all -- it is an 
error.

What you need to do is escape the percent sign, so it will be turned 
into a percent sign in the output, not treated as a format command in 
the input:

print('   td style=width: 50%%; text-align: center;%s/td') % 
(str(Stat['Vbatt']))


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fitting data to error function

2015-03-15 Thread Danny Yoo
 Thanks for the help! As you mentioned, using scipy.special.erfc was a much
 better idea. Below is a copy of my program and the stack trace, showing a
 new error. It seems that the first auto correlation works,  however the
 second fails.


At this point, the program is large enough that we need to apply
alternative techniques besides reading the entire program and trying
to keep it in our head at once.  One approach that is often successful
is to check the _user functions_ in isolation, because they should be
separable and independently testable.

You've written a few functions in your program.  In particular:

##
def fit(x,a,b,c): ...
def E_o(x): ...
def E_rin(x): ...
def E_iin(x): ...
def E_(x): ...
def integrand(t,t_d): ...
def integrand(x,y): ...## why is this being defined twice?!
def G(y): ...
def phi(x): ...
def M(x): ...
def E_out(x): ...
def integrand1(x,y): ...
def G1(y): ...
##

A function-based perspective will ask the following questions:

1.  What is the documented purpose of each of these functions?

2.  What are the expected inputs and output types for each of
these functions?

3.  Which of these functions have unit tests to spot-check their quality?

I can say for certain, without looking at any significant code, that
something is strange with regards to integrand.  Your program has
defined this twice, and that's almost certainly not right.

The strategy here is to pinpoint where the problem is: we can't
possibly say that the whole program is broken, so we try to scope the
problem down to a manageable size.

You mention that the first auto-correlation is producing good results.
What functions are involved in the computation of the first
auto-correlation?


Another technique to try to scope is to look at the stack trace and
the involved functions.  The stack trace says that the following
functions are involved at the point of the program breakage:


  G1
  quad
  integrand1
  E_out
  E_

In particular, note the tail end of the stack trace:



   7 def E_out(x):
  8 E_in_w = fft(E_(x))


It is on the call to:

fft(E_(x))

that things break with the IndexError.  Note that fft() is provided by
the scipy library.  For the purposes of debugging, let us assume that
all external third-party libraries are bug-free.

What does fft expect to receive as an argument?  We can read the following:


http://docs.scipy.org/doc/scipy/reference/generated/scipy.fftpack.fft.html#scipy.fftpack.fft

Since fft is erroring out: there's only one possibility: E_(x) is not
providing a value that's appropriate to fft().

Why would it do that?  Two possibilities:

1.  E_ is buggy and needs investigation.

2.  E_ is fine, but the inputted value of x is one that E_x is not
defined to handle.


And so we start the investigation by considering those two
possibilities.  Start with #1.

Do you know if E_ is fine?  What is it supposed to do?  What is the
shape of its input?  What is the shape of its output?  Is its output
something that fft() is designed to handle?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fitting data to error function

2015-03-15 Thread Danny Yoo
 What does fft expect to receive as an argument?  We can read the following:

 
 http://docs.scipy.org/doc/scipy/reference/generated/scipy.fftpack.fft.html#scipy.fftpack.fft

 Since fft is erroring out: there's only one possibility: E_(x) is not
 providing a value that's appropriate to fft().

 Why would it do that?  Two possibilities:

 1.  E_ is buggy and needs investigation.

 2.  E_ is fine, but the inputted value of x is one that E_x is not
 defined to handle.


 And so we start the investigation by considering those two
 possibilities.  Start with #1.

 Do you know if E_ is fine?  What is it supposed to do?  What is the
 shape of its input?  What is the shape of its output?  Is its output
 something that fft() is designed to handle?

Just to add: this is not to say that E_() is buggy.  We just have to
start investigation somewhere, and it seemed as good a place as any,
since I don't know if _any_ of the functions are behaving.  :P

This is very much the reason why programmers like to do unit testing:
we want to know what functions at least do something that we know is
useful.  We know all too well that whole programs break, and we want
to have some confidence on what components of our program are likely
ok.

If #2 is the actual issue, then the question becomes: why is the
program producing an invalid input 'x'?  And that's where we need to
start reasoning backwards, to discover how that value was constructed.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor