0

I am running into seemingly simple issue, but I am still scratching my head, not sure why it isn't working. If you could please provide your feedback, it is greatly appreciated!

What I am trying to do is that I have txt file that has x and y look like the following (x and y are tab separated):

x        y
1500   1
2000   0.5
2500   2
3000   6

In my code I determine my precursor and products to be within a certain x range. Then I want to determine the fraction of my precursor.

The following is my code

import numpy as np
import os #reading files using os module

myfiles = sorted(os.listdir('input_102417apo'))

my_ratio=[]

for file in myfiles:
    with open('input_102417apo/'+file, 'r') as f:  #determining x, y in my txt files
        data = np.loadtxt(f,delimiter='\t') 
        data_filtered_both = data[data[:,1] != 0.000]
        x_array=(data_filtered_both[:,0])
        y_array=(data_filtered_both[:,1])
        y_norm=(y_array/np.max(y_array))
        x_and_y = []
        row = np.array([list (i) for i in zip(x_array,y_norm)])
        for x, y in row:
            if y>0:       
                x_and_y.append((x,y))



    precursor_x=[]
    precursor_y=[]
    for x,y in (x_and_y):
        if x>2260 and x<2280:
            precursor_x.append(x)
            precursor_y.append(y)

    precursor_y_sum=np.sum(precursor_y)


    product6_x=[]
    product6_y=[]
    for x,y in (x_and_y):
        if x>1685 and x<1722:
            product6_x.append(x)
            product6_y.append(y)

    product6_y_sum=np.sum(product6_y)


    product5_x=[]
    product5_y=[]    
    for x,y in (x_and_y):
        if x>2035 and x<2080:
            product5_x.append(x)
            product5_y.append(y)    

    product5_y_sum=np.sum(product5_y)


    my_ratio.extend((precursor_y_sum)/(precursor_y_sum+monomer6_y_sum+ monomer5_y_sum))


    with open ('output/'+file, 'w') as f:
        f.write('{0:f}\n'.format(my_ratio))

I am batch processing many files that is organized by a order (number) so I want to have one list that shows a fraction of my precursor from all of my files.

That is why I created my_ratio.

But I am running into the following error message:

TypeError: 'numpy.float64' object is not iterable

I am not quite sure what causes it to be not iterable and how I could fix it. Thanks!

1 Answer 1

2

The issue is here: my_ratio.extend((precursor_y_sum)/(precursor_y_sum+monomer6_y_sum+ monomer5_y_sum))

You can't extend with just a variable, it has to be a list. I would use append there unless there is some reason you really need to use extend.

Sign up to request clarification or add additional context in comments.

1 Comment

I just used append function instead. It does what I need to get! Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.