0

I am trying to write a coincidence analysis code comparing two text files in python.The code I have written is as follows:

datafile1 = np.loadtxt('some_file_1.txt')
datafile2 = np.loadtxt('some_file_2.txt')
SNR1 = datafile1[:,0]
peak1 = datafile1[:,1]
m11 = datafile1[:,2]
m12 = datafile1[:,3]
SNR2 = datafile2[:,0]
peak2 = datafile2[:,1]
m21 = datafile2[:,2]
m22 = datafile2[:,3]

f = open('out_file.txt', 'w')
F = datafile[:,1].astype(int)         
for i in F:         
    for j in range(i-40,i+41):
        if (m11[j] == m21[i] and m12[j] == m22[i]):
          f.write(SNR1[j] + " " + peak1[j] + " " + m11[j])
          f.write("\n")
f.close()

The code should take a value from "peak2", then compare it to all the values of "peak1". If the value of "peak1" is equal to or within a difference of +/- 40 from "peak2" and if the value of "m11" is equal to "m21" and if "m12" is equal to "m22", it should write the values of "peak1", "m11" and "m12" in a text file. The error I get is that "index 477358 is out of bounds for axis 0 with size 10000 ". I realize the error is regarding the indices, but I cannot figure out how to correct it in a way that works.

BhashT
  • 1
  • 1
  • What does "compare" mean to you? What exactly is the code supposed to do? Please show a short example of what might appear in the files, *exactly* what should happen as a result, *exactly* what happens instead, and explain how that is different or wrong. – Karl Knechtel Mar 20 '21 at 09:31
  • Edited the original post. – BhashT Mar 20 '21 at 09:49
  • Does this answer your question? [Python - Compare 2 files and output differences](https://stackoverflow.com/questions/28213525/python-compare-2-files-and-output-differences) –  Mar 20 '21 at 09:57
  • Haha this is exactly opposite of what I'm asking. In my case, the code should pick the other three data points in the code(from the link you sent). – BhashT Mar 20 '21 at 10:56

1 Answers1

0

if i get you well, this is how you compare two files in Python.

with open('some_file_1.txt', 'r') as file1:
    with open('some_file_2.txt', 'r') as file2:
        same = set(file1).intersection(file2)

same.discard('\n')
with open('some_output_file.txt', 'w') as file_out:
    for line in same:
        file_out.write(line) 

Let me know if you are answered

crispengari
  • 7,901
  • 7
  • 45
  • 53
  • Yes, this is how it usually works, but since I also require to list values which have the same values for other columns AND are valid within a tolerance of +/-40, I don't know how to change this code to include that. – BhashT Mar 20 '21 at 10:52