Click here to Skip to main content
15,877,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to use custom window for computing mean and standard deviation using python. The window that I want to use has some values false and True. I just want only to consider the corresponding True value in the sliding window in my computation.
Here is how the sliding window look
[[ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True False False False False  True  True  True]
 [ True  True  True False False False False  True  True  True]
 [ True  True  True False False False False  True  True  True]
 [ True  True  True False False False False  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]]


Sliding window will iterate over all rows and columns of images.
Is there a simple way to compute the mean and std using built-in rolling functions in xarray or pandas ?


It could be helpful this built-in
If a BaseIndexer subclass, the window boundaries based on the defined get_window_bounds method. Additional rolling keyword arguments, namely min_periods, center, and closed will be passed to get_window_bounds.

pandas.DataFrame.rolling — pandas 1.4.2 documentation[^]

What I have tried:

Using loop: it works but too time consuming when you have array 25k*16k
for rows in range(int(S/2),log_intensity.shape[0]-int(S/2)+1):
  for cols in range(int(S/2),log_intensity.shape[1]-int(S/2)+1):
    M= log_intensity[rows-int(S/2):rows+int(S/2),cols-int(S/2):cols+int(S/2)]
    T= log_intensity[rows,cols].values
    Mean= M.where(mask==True).mean().values
    Std= M.where(mask==True).std().values
Posted
Updated 2-Jun-22 21:40pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900