Square - 1 year ago 72

Python Question

I am having a hard time with pythons uncertainties package. I have to evaluate experimental data with python, which I've been doing for a while but never encountered the following problem:

`>>>from uncertainties import ufloat`

>>>from uncertainties import unumpy as unp

>>>u = ufloat(5, 1)

>>>l = unp.log(u)

>>>print(l)

1.61+/-0.2

Everything seems to be all right, right? But here comes the strange part:

`>>>print(type(l))`

<type 'numpy.ndarray'>

Which is a

`>>>print(l.n)`

AttributeError: 'numpy.ndarray' object has no attribute 'n'

Right now in my work I desperately need the nominal values and standard devivations separately for linear regressions. What's really strange about this and makes me think it actually is a bug is the fact, that printing the variable actually works like intended but python "thinks" its type is an array when its actually supposed to be an ufloat.

Any ideas or tips for an easy workaround? Do you think it is a bug or did I miss anything and it is actually my mistake?

To prevent anybody from asking why I am doing such an easy calculation: That's just an example of course. In my actual work I have many many much more complex values stored in arrays.

Edit1: https://pythonhosted.org/uncertainties/user_guide.html

Edit2:

Ok here is the code I am actually having problems with, the above was just supposed to illustrate the problem.

`d, t, n = loadtxt('mess_blei_gamma.txt', unpack=True)`

fh = open('table_blei.txt', 'w')

nn = []

ln = []

for i in range(0, len(d)):

nn.append(norm(n[i], t[i], n0))

ln.append(unp.log(nn[i]))

fh.write(tex(str(d[i])+" & "+str(t[i])+" & "+str(n[i])+" & "+str(nn[i])+" & "+str(ln[i]))) #works how it's supposed to, the table is perfectly fine

fh.close()

print(unp.nominal_values(nn)) #works fine

print(unp.nominal_values(ln)) #error

Answer Source

First of all, many unumpy objects are basically numpy arrays:

```
>>>arr = unp.uarray([1, 2], [0.01, 0.002])
>>>arr
[1.0+/-0.01 2.0+/-0.002]
>>>type(arr)
<type 'numpy.ndarray'>
```

so you should not be surprised.

By the way, ufloat is a function and not a type:

```
>>>x = ufloat(0.20, 0.01) # x = 0.20+/-0.01
>>>print type(x)
<class 'uncertainties.Variable'>
>>>type(ufloat)
<type 'function'>
```

Secondly, in order to get the nominal values you should use:

```
unumpy.nominal_values(l)
```

EDIT: After you edited your original message, I think I understand your problem. You can use unumpy.log outside the for loop like this:

```
>>>nn = [ ufloat(1, 2), ufloat(53, 4)]
>>>ln = unp.log(nn)
>>>ln
[0.0+/-2.0 3.970291913552122+/-0.07547169811320754]
>>>type(ln)
<type 'numpy.ndarray'>
>>>(unp.nominal_values(ln)) #now it works fine
[ 0. 3.97029191]
```

I also agree that this behavior is a bit weird.

A code that runs well and achieves your goal:

```
d, t, n = loadtxt('mess_blei_gamma.txt', unpack=True)
fh = open('table_blei.txt', 'w')
nn = (norm(n[i], t[i], n0) for i range(0, len(d)))
ln = unp.log(nn)
for i in range(0, len(d)):
fh.write(tex(str(d[i])+" & "+str(t[i])+" & "+str(n[i])+" & "+str(nn[i])+" & "+str(ln[i]))) #works how it's supposed to, the table is perfectly fine
fh.close()
print(unp.nominal_values(nn))
print(unp.nominal_values(ln))
```