0

I am trying to create a new array, which will consist of (data value <=20000) from the first column, along with all other corresponding columns.

Mathematically I am doing the following: I am reading data from a text file. I am finding distance to all the points from the last point. Then I will take only those rows with distances less than 20000 and M1 less than 11.5. The data looks as follows:

# ID M1 M2 M3 M4 R4 M5 R5 x y z
10217 11.467 11.502 13.428 13.599  432.17 13.266  281.06 34972.8 42985.9 14906
7991 11.529 11.559 13.438 13.520  435.23 13.224  272.23 8538.05 33219.8 43375.1
2100 11.526 11.573 13.478 13.490  448.97 13.356  301.27 9371.75 13734.1 43398.6
9467 11.557 11.621 13.481 13.537  449.99 13.367  303.67 33200.3 36008.9 12735.8

my code looks the following:

import numpy as np
import matplotlib.pyplot as plt


halo = 'nntest.txt'
ID, m,r,x,y,z= np.loadtxt(halo, usecols=(0,6,7,8,9,10), unpack =True)



# selet the last point
m_mass = m[-1:]
ID_2 = ID[-1:]
r_2 = r[-1:]
x_2 = x[-1:]
y_2 = y[-1:]
z_2 = z[-1:]

#######################################
#Find distance to all points from our targeted point
nearest_neighbors = []

def neighbors(ID_2, cx,cy,cz, ID, m, r, x, y, z):

    dist = np.sqrt((cx-x)**2 + (cy-y)**2 + (cz-z)**2)

    return dist, ID, m, r, x, y, z

for i in range(len(ID_2)):
    hist = neighbors(ID_2[i], m_2[i], r_2[i], x_2[i], y_2[i], z_2[i], ID, m , r, x, y, z)



    #print all the IDs and all other data which are less than 20000 and M less than 11.5  of that targeted value
    print ID[hist[0]<20000] and m[hist[1]<11.5]

But I am having problem with setting the 2 conditions. It returns me this error:

File "overlaping_halos_finder.py", line 53, in <module>
    combined = zip(ID[hist[0]<r_2[i] and m[hist[1]>1.e12]])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

if instead of

print ID[hist[0]<20000] and m[hist[1]<11.5]

I do only:

print ID[hist[0]<20000]

I have following sample output:

# ID M R X Y Z
6737.0 909913272632.0 103.06 1988.35 15894.6 40649.0
6617.0 997700063823.0 106.28 1523.55 15433.2 40363.2
6723.0 11 109.91 1993.05 15687.5 40557.2

But I want to get rid of those first two outputs and only want to print the 3 rd row where M column value <11.5

hope it clarifies a little

Your suggestion to fix this issue will be very helpful

9
  • 1
    Can you describe exactly what you are trying to do? setting the 2 conditions doesn't explain what you are trying to achieve. Ideally, you can show us an example of 2 small arrays and desired output. Commented Jul 30, 2018 at 22:21
  • I am trying to print only those rows for which calculated distance (from the neighbors function) will be less than 20000. At the same time those also have to meet the condition that the value of m column is less than 11.5 Commented Jul 30, 2018 at 22:24
  • So, to clarify, do you want a tuple of 2 arrays as your output, filtered by Boolean arrays hist[0]<20000 and hist[1]<11.5 respectively? As far as I can see ID and m are 2 separate arrays. Commented Jul 30, 2018 at 22:31
  • Or maybe you want ID[(hist[0]<20000) & (hist[1]<11.5)] ? Commented Jul 30, 2018 at 22:33
  • 1
    I think that solves the problem. Thanks very much for your time Commented Jul 30, 2018 at 22:41

2 Answers 2

1

You do not need logical and. To print a tuple of arrays, simply print a comma-separated sequence of arrays:

print ID[hist[0]<20000], m[hist[1]<11.5]

ID will be indexed by the Boolean array hist[0]<20000, while m will be indexed by the Boolean array hist[1]<11.5.

If you need to assign to variables, you can use sequence unpacking:

res1, res2 = ID[hist[0]<20000], m[hist[1]<11.5]
Sign up to request clarification or add additional context in comments.

Comments

0

I didn't understand your code, but from the title I advise you to define a function that given one point, returns true if that point should be filtered. Then you can do:

def filter_point(point):
    # Conditions over a single point
    # You can read from global variables here, or create a partial function
    # to access external data

    return condition1(point) and condition2(point)

result = filter(filter_point, list_of_points)

Comments

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.