用于在样品位置不均匀的情况下计算3D梯度的功能 [英] Function to compute 3D gradient with unevenly spaced sample locations

查看:75
本文介绍了用于在样品位置不均匀的情况下计算3D梯度的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实验观察结果:

I have experimental observations in a volume:

import numpy as np

# observations are not uniformly spaced 
x = np.random.normal(0, 1, 10)
y = np.random.normal(5, 2, 10)
z = np.random.normal(10, 3, 10)
xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')

# fake temperatures at those coords
tt = xx*2 + yy*2 + zz*2

# sample distances
dx = np.diff(x)
dy = np.diff(y)
dz = np.diff(z)

grad = np.gradient(tt, [dx, dy, dz])  # returns error

这给了我错误:

ValueError: operands could not be broadcast together with shapes (10,10,10) (3,9) (10,10,10).

根据@ jay-kominek在以下评论中:

according to @jay-kominek in the comments below:

np.gradient不适用于您,它根本无法处理采样不均的数据.

np.gradient won't work for you, it simply doesn't handle unevenly sampled data.

我已经更新了问题.有什么功能可以进行计算吗?

I've updated the question. Is there any function which can can do my computation?

推荐答案

注意两点:首先,标量是单个值,而不是数组.其次,函数的签名为numpy.gradient(f, *varargs, **kwargs).注意varargs前的*.这意味着如果varargs是列表,则传递*varargs.或者,您可以只提供varargs的元素作为单独的参数.

Two things to note: First, scalars are single values, not arrays. Second, the signature of the function is numpy.gradient(f, *varargs, **kwargs). Note the * before varargs. That means if varargs is a list, you pass *varargs. Or you can just provide the elements of varargs as separate arguments.

因此,np.gradient希望沿每个维度的距离为单个值,例如:

So, np.gradient wants a single value for the distance along each dimension, like:

np.gradient(tt, np.diff(x)[0], np.diff(y)[0], np.diff(z)[0])

或:

distances = [np.diff(x)[0], np.diff(y)[0], np.diff(z)[0]]
np.gradient(tt, *distances)

这篇关于用于在样品位置不均匀的情况下计算3D梯度的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆