3

I'm using python 2.7 and the module called "uncertainties" to analyse data from an experiment. I have two arrays, polycoeffs and cov, that were generated by the numpy function polyfit. I've managed to pull out the leading diagonal from the cov array and I'm trying to match these values with the appropriate coefficients in a list called uncert_coeffs with the uncertainties function "ufloat". Here's the code:

polycoeffs,cov=polyfit(wav,trans,6,cov=True) #wav and trans are themselves, arrays.
print  "Polycoeffs= ",polycoeffs

print "Cov= ",cov

cov_diag=[]
for element in diag(cov):
    cov_diag.append(str(element))
print "The diagonal of the covariant matrix= ",cov_diag
ord_counter=6
uncert_coeffs=[]
cov_index=0

for i in polycoeffs:
    uncert=(cov_diag[cov_index])
    print "uncert: ",uncert
    temp=ufloat("(i+/-uncert)") #error here
    uncert_coeffs.append(temp)
    cov_index+=1

print "The polynomial coefficients with uncertainties, are: ",uncert_coeffs 

This produces the error:

ValueError: Cannot parse (i+/-uncert): was expecting a number like 1.23+/-0.1

So my question is: in these circumstances in which it would be a royal pain to combine the polycoeffs and their uncertainties by hand, how can I make ufloat unpack the variable uncert? Additionally, the values of uncert are mostly scientific notation.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
user2151741
  • 99
  • 1
  • 8

1 Answers1

3

You're passing in the literal string "i+/-uncert" instead of the variable values.

Assuming you're using a recent version of uncertainties, just do:

temp = ufloat(i, uncert)

Alternately, you could do format the numerical values as their string representations:

temp = ufloat('{}+/-{}'.format(i, uncert))

However, there's no reason not to just pass in the values to ufloat directly.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • It would seem that I had version 1.8 from the Debian repos, and I also passed in the arguments wrong as suggested.It works fine in version 2.4. – user2151741 Oct 28 '13 at 22:16
  • Using the uncertainties 2.4 module, this now works in python idle but not when calling the script from a terminal. I get the error UserWarning: either use ufloat(blah,blah) or use ufloat(etc). I am calling it exactly the way you suggested. – user2151741 Oct 28 '13 at 23:23
  • @user2151741 - That's a warning rather than an error. It should still run, it's just letting you know that what you're doing won't be supported in later versions. Based on your code, this is probably because you're converting everything to strings (e.g. `cov_diag.append(str(element))`). Try just passing the actual floats in. – Joe Kington Oct 28 '13 at 23:35
  • I've ammended the line to cov_diag.append(element) but am still getting the error i didn't mention in my last comment: AttributeError: 'numpy.float64' object has no attribute 'strip'. I tried using the uncertainties function wrap ie: poly_wrap=polyfit.wrap() but that also throws an error 'function' object has no attribute 'wrap'. – user2151741 Oct 28 '13 at 23:49
  • You're getting the `AttributeError` because you're trying to use the `strip` method on a numerical value. You're getting confused between strings and numerical types (e.g. floats). Python is a "strongly typed" language. In other words, everything has a type, and you need to explicitly convert between types. Strings have a `strip` method to remove whitespace. Floats don't (it wouldn't make any sense), so you're getting the error when you try to strip a float. – Joe Kington Oct 29 '13 at 00:31
  • Also, just so you know, errors (like an `AttributeError`) stop execuction, while warnings (like the `UserWarning` you mentioned) don't and can potentially be ignored. – Joe Kington Oct 29 '13 at 00:34