Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask 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?

Show us the shape and dtype for line or dist_hist. I suspect line[0] is an array with 'S32' dtype, not just a string element of such an array. – hpaulj Jan 25, 2017 at 19:53 @hpaulj dist_hist = zeroes((bins,4)) and is in the script 5 more times, but I'm not sure what it means here are the other times it's mentioned other than the line in the origional question dist_hist[index_ef,3] += hist_add dist_hist[index,1] += hist_add dist_hist[:,0] = arange(mini,maxi,(maxi-mini)/(float(bins)-0.5)) dist_hist[:,2] = arange(mine,maxe,(maxe-mine)/(float(bins)-0.5)) – RAT Jan 25, 2017 at 20:29 the print(line) before the script stated above is print "average distance between dyes is ", average_distance/(len(data[::step])*len(datd[::step])) while the next line with print is in the question. I'm sorry if I'm misunderstanding and I appreciate you trying to help! – RAT Jan 25, 2017 at 20:56

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.probability\n')
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).

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Sep 28, 2021 at 6:33

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.