Khalil Al Hooti
Khalil Al Hooti

Reputation: 4516

How to select data within a range

Suppose I have some circular data [min >= 0, max < 360],

data = np.array([355.9,  2.47 ,  30.52, 5.33, 40.22, 340.22])

The circular mean of these data is 9

I want to select the data around the mean by +- 15 degrees. Therefore the selected data will be new_data = [355.9, 2.47 , 5.33]. The selected data fall in the range between [low=9-15, high=9+15] = [354, 24]

What I have tried is

def remove_outliers(angles, mean, extend):

    high = (mean + extend) - 360 if (mean + extend)>360 else (mean + extend)
    low = (mean - extend) + 360 if (mean - extend)<0 else (mean - extend)

    angles = angles[angles>=low]
    angles = angles[angles<=high]

    return angles

high = 24, and low = 354 but the next part is wrong

angles = angles[angles>=low] # this is wrong
angles = angles[angles<=high] # this is wrong

remove_outliers(data, 9, 15) # gives empty array

The function will work if the mean is above 15 or below 345 for extend = 15.

Upvotes: 0

Views: 239

Answers (1)

Tony Pellerin
Tony Pellerin

Reputation: 241

According to what you want, when low > high, the conditions should be angles>=low OR angles<=high, not angles>=low AND angles<=high. You can also use the np.mod function to get the high and low values.

I would write:

import numpy as np

def remove_outliers(angles, mean, extend):
    high = np.mod(mean+extend,360)
    low = np.mod(mean-extend,360)
    if high < low:
        return angles[(angles<=high) | (angles>=low)]
    else:
        return angles[(angles<=high) & (angles>=low)]


data = np.array([355.9,  2.47 ,  30.52, 5.33, 40.22, 340.22])


correct_angles = remove_outliers(data, 9, 15)

Upvotes: 1

Related Questions