首页 > 解决方案 > scipy stats 中的 FutureWarning

问题描述

我创建了一个函数,其作用是计算每个 bin 的径向平均值和重拨中位数,并针对一个 *.tif 格式的图像对其进行测试。

import numpy as np
from math import fabs
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image
from scipy import stats


def av_radial_profile(data, center):
    y, x = np.indices((data.shape))
    r = np.hypot(x - center[0],y - center[1])
    r = r.astype(np.int)
    b = len(set(r.ravel()))
    av_radialprofile = stats.binned_statistic(r.ravel(), data.ravel(), statistic ='mean', bins = b)[0]

    median_radialprofile = stats.binned_statistic(r.ravel(), data.ravel(), statistic = lambda q: np.nanmedian(q), bins = b)[0]  

    return median_radialprofile, av_radialprofile


img = mpimg.imread('test.tif',0)
center, radi = (509, 546), 55
rad2, rad3 = av_radial_profile(img, center)

图片在这里tif 格式图片 运行此代码后,我面临以下警告消息:

~miniconda3/lib/python3.7/site-packages/scipy/stats/_binned_statistic.py:607: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  result = result[core]

我试图搜索问题的原因,发现原因可能是基于旧的软件包版本。所以在那之后我用 conda 更新了所有的包并再次重新运行我的代码,但是警告信息并没有消失。

可能有人遇到了同样的问题。我将非常感谢有关我的代码的任何帮助和说明。

标签: pythonnumpyscipy

解决方案


在 scipy 1.3.1 中,“违规”代码现在是

# Remove outliers (indices 0 and -1 for each bin-dimension).
core = tuple([slice(None)] + Ndim * [slice(1, -1)])
result = result[core]

scipy/stats/_binned_statistic.py文件中。


推荐阅读