Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9bee61d5
提交
9bee61d5
authored
2月 08, 2026
作者:
ricardoV94
提交者:
Ricardo Vieira
2月 10, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix empty xtensor indexing
上级
18449731
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
47 行增加
和
5 行删除
+47
-5
indexing.py
pytensor/xtensor/indexing.py
+14
-2
type.py
pytensor/xtensor/type.py
+2
-3
test_indexing.py
tests/xtensor/test_indexing.py
+31
-0
没有找到文件。
pytensor/xtensor/indexing.py
浏览文件 @
9bee61d5
...
@@ -153,8 +153,18 @@ class Index(XOp):
...
@@ -153,8 +153,18 @@ class Index(XOp):
if
len
(
idxs
)
>
x_ndim
:
if
len
(
idxs
)
>
x_ndim
:
raise
IndexError
(
"Too many indices"
)
raise
IndexError
(
"Too many indices"
)
# Remove useless trailing slice(None) indices
# starting at -1 ensures we handle the case of no useful indices correctly with idxs[:0]
last_useful_index
=
-
1
for
i
,
idx
in
enumerate
(
idxs
):
if
isinstance
(
idx
,
slice
)
and
idx
==
slice
(
None
):
continue
last_useful_index
=
i
# Convert (useful) indices to symbolic variables
idxs
=
[
idxs
=
[
as_idx_variable
(
idx
,
dim
)
for
idx
,
dim
in
zip
(
idxs
,
x_dims
,
strict
=
False
)
as_idx_variable
(
idx
,
dim
)
for
idx
,
dim
in
zip
(
idxs
[:
last_useful_index
+
1
],
x_dims
,
strict
=
False
)
]
]
for
i
,
idx
in
enumerate
(
idxs
):
for
i
,
idx
in
enumerate
(
idxs
):
...
@@ -174,7 +184,9 @@ class Index(XOp):
...
@@ -174,7 +184,9 @@ class Index(XOp):
idx_dim_shape
=
idx
.
type
.
shape
[
idx_dims
.
index
(
idx_dim
)]
idx_dim_shape
=
idx
.
type
.
shape
[
idx_dims
.
index
(
idx_dim
)]
combine_dim_info
(
idx_dim
,
idx_dim_shape
)
combine_dim_info
(
idx_dim
,
idx_dim_shape
)
for
dim_i
,
shape_i
in
zip
(
x_dims
[
i
+
1
:],
x_shape
[
i
+
1
:]):
for
dim_i
,
shape_i
in
zip
(
x_dims
[
last_useful_index
+
1
:],
x_shape
[
last_useful_index
+
1
:]
):
# Add back any unindexed dimensions
# Add back any unindexed dimensions
if
dim_i
not
in
out_dims
:
if
dim_i
not
in
out_dims
:
# If the dimension was not indexed, we keep it as is
# If the dimension was not indexed, we keep it as is
...
...
pytensor/xtensor/type.py
浏览文件 @
9bee61d5
...
@@ -488,9 +488,8 @@ class XTensorVariable(Variable[_XTensorTypeType, OptionalApplyType]):
...
@@ -488,9 +488,8 @@ class XTensorVariable(Variable[_XTensorTypeType, OptionalApplyType]):
)
)
indexers
=
indexers_kwargs
indexers
=
indexers_kwargs
if
not
indexers
:
elif
indexers
is
None
:
# No-op
indexers
=
{}
return
self
if
missing_dims
not
in
{
"raise"
,
"warn"
,
"ignore"
}:
if
missing_dims
not
in
{
"raise"
,
"warn"
,
"ignore"
}:
raise
ValueError
(
raise
ValueError
(
...
...
tests/xtensor/test_indexing.py
浏览文件 @
9bee61d5
...
@@ -12,6 +12,7 @@ from xarray import DataArray
...
@@ -12,6 +12,7 @@ from xarray import DataArray
from
pytensor.tensor
import
tensor
from
pytensor.tensor
import
tensor
from
pytensor.xtensor
import
xtensor
from
pytensor.xtensor
import
xtensor
from
tests.unittest_tools
import
assert_equal_computations
from
tests.xtensor.util
import
(
from
tests.xtensor.util
import
(
xr_arange_like
,
xr_arange_like
,
xr_assert_allclose
,
xr_assert_allclose
,
...
@@ -511,3 +512,33 @@ def test_diff(dim, n):
...
@@ -511,3 +512,33 @@ def test_diff(dim, n):
else
:
else
:
expected_res
=
x_test
.
diff
(
dim
,
n
=
n
)
expected_res
=
x_test
.
diff
(
dim
,
n
=
n
)
xr_assert_allclose
(
res
,
expected_res
)
xr_assert_allclose
(
res
,
expected_res
)
def
test_empty_index
():
x
=
xtensor
(
"x"
,
shape
=
(
5
,
5
),
dims
=
(
"a"
,
"b"
))
out1
=
x
[()]
out2
=
x
[
...
]
out3
=
x
.
isel
({})
out4
=
x
.
isel
({
"c"
:
0
},
missing_dims
=
"ignore"
)
assert_equal_computations
([
out1
],
[
out2
])
assert_equal_computations
([
out1
],
[
out3
])
assert_equal_computations
([
out1
],
[
out4
])
fn
=
xr_function
([
x
],
out1
)
x_test
=
xr_random_like
(
x
)
xr_assert_allclose
(
fn
(
x_test
),
x_test
)
def
test_empty_update_index
():
x
=
xtensor
(
"x"
,
shape
=
(
5
,
5
),
dims
=
(
"a"
,
"b"
))
out1
=
x
[()]
.
inc
(
1
)
out2
=
x
[
...
]
.
inc
(
1
)
out3
=
x
.
isel
({})
.
inc
(
1
)
out4
=
x
.
isel
({
"c"
:
0
},
missing_dims
=
"ignore"
)
.
inc
(
1
)
assert_equal_computations
([
out1
],
[
out2
])
assert_equal_computations
([
out1
],
[
out3
])
assert_equal_computations
([
out1
],
[
out4
])
fn
=
xr_function
([
x
],
out1
)
x_test
=
xr_random_like
(
x
)
xr_assert_allclose
(
fn
(
x_test
),
x_test
+
1
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论