https://numpy.org/doc/stable/reference/generated/numpy.ma.mean.html
https://www.geeksforgeeks.org/numpy-maskedarray-mean-function-python/
https://www.geeksforgeeks.org/change-data-type-of-given-numpy-array/
import numpy as np
def _compute_stats(vol, mask):
oring_dtype = mask.dtype
vol = vol.astype('float64')
# print(oring_dtype)
if len(vol) == 0:
return np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan
vol_mask = np.ma.masked_array(vol, mask=mask==0)
# print(vol_mask)
# median = np.median(vol)
# mean = np.mean(vol)
# sd = np.std(vol)
# mn = np.min(vol)
# mx = np.max(vol)
# percentile_99_5 = np.percentile(vol, 99.5)
# percentile_00_5 = np.percentile(vol, 00.5)
# print(median, mean, sd, mn, mx, percentile_99_5, percentile_00_5)
median = np.ma.median(vol_mask)
mean = np.ma.mean(vol_mask)
sd = np.ma.std(vol_mask)
mn = np.ma.min(vol_mask)
mx = np.ma.max(vol_mask)
mdata = vol_mask.filled(np.nan)
percentile_99_5 = np.nanpercentile(mdata, 99.5)
percentile_00_5 = np.nanpercentile(mdata, 00.5)
# print(median, mean, sd, mn, mx, percentile_99_5, percentile_00_5)
return median, mean, sd, mn, mx, percentile_99_5, percentile_00_5
in_arr = np.array([[1, 2], [3, -1], [5, -3]])
mask = np.array([[1, 0], [1, 0], [0, 0]])
median, mean, sd, mn, mx, percentile_99_5, percentile_00_5 = _compute_stats(in_arr, mask)
print(median, mean, sd, mn, mx, percentile_99_5, percentile_00_5)