Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1243d583
Unverified
提交
1243d583
authored
3月 17, 2026
作者:
Ricardo Vieira
提交者:
GitHub
3月 17, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add xtensor.where as a method and root operation (#1985)
上级
55b00d16
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
51 行增加
和
3 行删除
+51
-3
__init__.py
pytensor/xtensor/__init__.py
+1
-1
type.py
pytensor/xtensor/type.py
+26
-2
test_type.py
tests/xtensor/test_type.py
+24
-0
没有找到文件。
pytensor/xtensor/__init__.py
浏览文件 @
1243d583
...
@@ -2,7 +2,7 @@ import warnings
...
@@ -2,7 +2,7 @@ import warnings
import
pytensor.xtensor.rewriting
import
pytensor.xtensor.rewriting
from
pytensor.xtensor
import
linalg
,
math
,
random
,
signal
from
pytensor.xtensor
import
linalg
,
math
,
random
,
signal
from
pytensor.xtensor.math
import
dot
from
pytensor.xtensor.math
import
dot
,
where
from
pytensor.xtensor.shape
import
broadcast
,
concat
,
full_like
,
ones_like
,
zeros_like
from
pytensor.xtensor.shape
import
broadcast
,
concat
,
full_like
,
ones_like
,
zeros_like
from
pytensor.xtensor.type
import
(
from
pytensor.xtensor.type
import
(
as_xtensor
,
as_xtensor
,
...
...
pytensor/xtensor/type.py
浏览文件 @
1243d583
...
@@ -696,8 +696,6 @@ class XTensorVariable(Variable[_XTensorTypeType, OptionalApplyType]):
...
@@ -696,8 +696,6 @@ class XTensorVariable(Variable[_XTensorTypeType, OptionalApplyType]):
Parameters
Parameters
----------
----------
x : XTensorVariable
The input tensor
dim : str or None or iterable of str, optional
dim : str or None or iterable of str, optional
The name(s) of the dimension(s) to remove. If None, all dimensions of size 1
The name(s) of the dimension(s) to remove. If None, all dimensions of size 1
(known statically) will be removed. Dimensions with unknown static shape will be retained, even if they have size 1 at runtime.
(known statically) will be removed. Dimensions with unknown static shape will be retained, even if they have size 1 at runtime.
...
@@ -758,6 +756,32 @@ class XTensorVariable(Variable[_XTensorTypeType, OptionalApplyType]):
...
@@ -758,6 +756,32 @@ class XTensorVariable(Variable[_XTensorTypeType, OptionalApplyType]):
**
dim_kwargs
,
**
dim_kwargs
,
)
)
# Missing value handling
# https://docs.xarray.dev/en/stable/api/dataarray.html#missing-value-handling
def
where
(
self
,
cond
,
other
=
None
,
drop
:
bool
=
False
):
"""Filter elements from this object according to a condition.
Parameters
----------
cond : Variable
Locations at which to preserve this object's values.
other: Variable, optional
Value to use for locations in this object where cond is False.
By default, these locations are filled with nan (which may cause upcasting).
drop: bool
Ignored by PyTensor
Returns
-------
XTensorVariable
A tensor with additional dimensions inserted at the front.
"""
if
other
is
None
:
other
=
np
.
nan
res
=
px
.
math
.
where
(
cond
,
self
,
other
)
# xarray puts self dims first
return
res
.
transpose
(
*
self
.
dims
,
...
)
# ndarray methods
# ndarray methods
# https://docs.xarray.dev/en/latest/api.html#id7
# https://docs.xarray.dev/en/latest/api.html#id7
def
clip
(
self
,
min
,
max
):
def
clip
(
self
,
min
,
max
):
...
...
tests/xtensor/test_type.py
浏览文件 @
1243d583
...
@@ -23,6 +23,7 @@ from pytensor.xtensor.type import (
...
@@ -23,6 +23,7 @@ from pytensor.xtensor.type import (
xtensor_constant
,
xtensor_constant
,
xtensor_shared
,
xtensor_shared
,
)
)
from
tests.xtensor.util
import
xr_assert_allclose
,
xr_function
def
test_xtensortype
():
def
test_xtensortype
():
...
@@ -231,3 +232,26 @@ def test_isel_missing_dims():
...
@@ -231,3 +232,26 @@ def test_isel_missing_dims():
x
.
isel
(
c
=
0
,
missing_dims
=
"warn"
)
x
.
isel
(
c
=
0
,
missing_dims
=
"warn"
)
x
.
isel
(
c
=
0
,
missing_dims
=
"ignore"
)
.
dims
==
(
"a"
,
"b"
)
x
.
isel
(
c
=
0
,
missing_dims
=
"ignore"
)
.
dims
==
(
"a"
,
"b"
)
def
test_where
():
a
=
xtensor
(
dims
=
(
"a"
,
"b"
))
a_test
=
DataArray
(
np
.
arange
(
6
)
.
reshape
(
2
,
3
),
dims
=
a
.
dims
)
# Implicit other
out
=
a
.
where
(
a
>
1
)
res
=
xr_function
([
a
],
out
)(
a_test
)
expected
=
a_test
.
where
(
a_test
>
1
)
xr_assert_allclose
(
res
,
expected
)
# Explicit other
out
=
a
.
where
(
a
>
1
,
99
)
res
=
xr_function
([
a
],
out
)(
a_test
)
expected
=
a_test
.
where
(
a_test
>
1
,
99
)
xr_assert_allclose
(
res
,
expected
)
# Case that would fail if we didn't transpose
out
=
a
[
0
]
.
where
(
a
>
1
,
-
1
)
res
=
xr_function
([
a
],
out
)(
a_test
)
expected
=
a_test
[
0
]
.
where
(
a_test
>
1
,
-
1
)
xr_assert_allclose
(
res
,
expected
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论