filtering

Remove spurious small islands of noise in an image – Python OpenCV

Remove spurious small islands of noise in an image – Python OpenCV Question: I am trying to get rid of background noise from some of my images. This is the unfiltered image. To filter, I used this code to generate a mask of what should remain in the image: element = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2)) mask = …

Total answers: 2

How to gauss-filter (blur) a floating point numpy array

How to gauss-filter (blur) a floating point numpy array Question: I have got a numpy array a of type float64. How can I blur this data with a Gauss filter? I have tried from PIL import Image, ImageFilter image = Image.fromarray(a) filtered = image.filter(ImageFilter.GaussianBlur(radius=7)) , but this yields ValueError: ‘image has wrong mode’. (It has …

Total answers: 4

How do I filter a pandas DataFrame based on value counts?

How do I filter a pandas DataFrame based on value counts? Question: I’m working in Python with a pandas DataFrame of video games, each with a genre. I’m trying to remove any video game with a genre that appears less than some number of times in the DataFrame, but I have no clue how to …

Total answers: 5

Creating lowpass filter in SciPy – understanding methods and units

Creating lowpass filter in SciPy – understanding methods and units Question: I am trying to filter a noisy heart rate signal with python. Because heart rates should never be above about 220 beats per minute, I want to filter out all noise above 220 bpm. I converted 220/minute into 3.66666666 Hertz and then converted that …

Total answers: 1

filter items in a python dictionary where keys contain a specific string

filter items in a python dictionary where keys contain a specific string Question: I’m a C coder developing something in python. I know how to do the following in C (and hence in C-like logic applied to python), but I’m wondering what the ‘Python’ way of doing it is. I have a dictionary d, and …

Total answers: 6

Logical operators for Boolean indexing in Pandas

Logical operators for Boolean indexing in Pandas Question: I’m working with a Boolean index in Pandas. The question is why the statement: a[(a[‘some_column’]==some_number) & (a[‘some_other_column’]==some_other_number)] works fine whereas a[(a[‘some_column’]==some_number) and (a[‘some_other_column’]==some_other_number)] exits with error? Example: a = pd.DataFrame({‘x’:[1,1],’y’:[10,20]}) In: a[(a[‘x’]==1)&(a[‘y’]==10)] Out: x y 0 1 10 In: a[(a[‘x’]==1) and (a[‘y’]==10)] Out: ValueError: The truth value …

Total answers: 4

Eliminating all data over a given percentile

Eliminating all data over a given percentile Question: I have a pandas DataFrame called data with a column called ms. I want to eliminate all the rows where data.ms is above the 95% percentile. For now, I’m doing this: limit = data.ms.describe(90)[‘95%’] valid_data = data[data[‘ms’] < limit] which works, but I want to generalize that …

Total answers: 3

How do I remove all zero elements from a NumPy array?

How do I remove all zero elements from a NumPy array? Question: I have a rank-1 numpy.array of which I want to make a boxplot. However, I want to exclude all values equal to zero in the array. Currently, I solved this by looping the array and copy the value to a new array if …

Total answers: 7

Filtering os.walk() dirs and files

Filtering os.walk() dirs and files Question: I’m looking for a way to include/exclude files patterns and exclude directories from a os.walk() call. Here’s what I’m doing by now: import fnmatch import os includes = [‘*.doc’, ‘*.odt’] excludes = [‘/home/paulo-freitas/Documents’] def _filter(paths): for path in paths: if os.path.isdir(path) and not path in excludes: yield path for …

Total answers: 8

Pythonic way to get some rows of a matrix

Pythonic way to get some rows of a matrix Question: I was thinking about a code that I wrote a few years ago in Python, at some point it had to get just some elements, by index, of a list of lists. I remember I did something like this: def getRows(m, row_indices): tmp = [] …

Total answers: 3