ufunc 'add' did not contain loop with signature matching type dtype ('S32') ('S32') ('S32')

Question:

I’m trying to run someone’s script for some simulations I’ve made to try plotting some histograms, but when I do I always get the error message mentioned above. I have no idea what’s gone wrong.

Here’s the complete traceback error I get:

File "AVAnalyse.py", line 205, in <module> 
  f.write(line[0] + '  ' + line[1] + '  ' + line[2] + '  ' + line[3]) 
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')

This is the code I am trying to run:

name_out = "histogram_" + donor + "_" + acceptor + ".dat"   
f = open(name_out, 'w')
f.write('distance  d.probability  efficiency  e.probability')
for line in dist_hist:
    f.write(line[0] + '  ' + line[1] + '  ' + line[2] + '  ' + line[3])
f.close()

print "data saved in " + "histogram_" + donor + "_" + acceptor + ".dat"

What am I doing wrong?

Asked By: RAT

||

Answers:

It seems like line[0], line[1], line[2], line[3] are elements of dist_hist. dict_hist is a numpy.ndarray. The elements of dict_hist has a numeric type (like np.float64) (based on calculations from your attached file). You’re trying to add elements of different types: np.float64 and str. If you want to avoid this TypeError, you can change type of line[0], line[1], line[2], line[3] to str.

Your snippet of code should be like this:

name_out = "histogram_"+donor+"_"+acceptor+".dat"   
f = open(name_out,'w')
f.write('distance  d.probability  efficiency  e.probability')
for line in dist_hist:
  f.write(str(line[0])+'  '+str(line[1])+'  '+str(line[2])+'  '+str(line[3]))
f.close()

print "data saved in " +"histogram_"+donor+"_"+acceptor+".dat"

EDIT:

You should replace this snippet of code:

name_out = "histogram_"+donor+"_"+acceptor+".dat"   
f = open(name_out,'w')
f.write('distance  d.probability  efficiency  e.probability')
for line in dist_hist:
  f.write(line[0]+'  '+line[1]+'  '+line[2]+'  '+line[3])
f.close()

to this one:

name_out = "histogram_" + donor + "_" + acceptor + ".dat"   
f = open(name_out,'w')
f.write('distance  d.probability  efficiency  e.probabilityn')
for line in dist_hist:
  f.write(str(line[0]) + '  ' + str(line[1]) + '  ' + str(line[2]) + '  ' + str(line[3]) + 'n')
f.close()

Before that, strings were written to file in one line. Because of that your data variable point to empty array since we start to read from 2nd line (which was empty).

Answered By: Eduard Ilyasov

My problem was solved by @Eduard Ilyasov’s solution. The TLDR is to wrap the part where the error came from in str(thing) (when printing for me).

Answered By: Tian Welgemoed

My best advice when you’re facing an error like that. Typically you have to check the type compatibility of your data. Take few minutes to check it, print it and you should find an incompatibility. Often, that’s mixed of string and numerical values.

Answered By: Claude COULOMBE
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.