`DenseFieldTransform.map()` wraps or raises for on-grid points outside the field

オープン 初心者向け
#306 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
numpy, python

調査の方向性

nitransforms/nonlinear.py の 185-191 行付近にある fast path から始めます。ここでは on-grid の点がボクセルインデックスに丸められ、境界チェックなしでフィールドのインデックス指定に使われます。次に、0.01 mm off-grid で使われる補間パスと比較します。issue の再現スニペットを実行して、インデックス -1 がラップし、インデックス n が IndexError を発生させることを確認します。両方のアサーションが通り、グリッド外の on-grid の点が off-grid パスと同じ結果を返したら完了です。

索引モデルが issue の本文から書いたものです。

説明

When every point falls on a voxel center, map() takes a fast path:

https://github.com/nipy/nitransforms/blob/06537caee1e5908e7faada275187a0568982b549/nitransforms/nonlinear.py#L185-L191

That rounds points to voxel indices and indexes the field with them, without checking bounds. A point one voxel before the grid gets index -1 and wraps to the last voxel. A point one voxel past the grid gets index n and raises IndexError. Shifting 0.01 mm off the grid switches to the interpolation path, with the correct result.

Reproduction

import nibabel as nb
import numpy as np
from nitransforms.nonlinear import DenseFieldTransform

# 21^3 grid of 4 mm voxels spanning -40..40 mm; +100 mm x-shift in the last x slab only
deltas = np.zeros((21, 21, 21, 3), dtype="float32")
deltas[-1, ..., 0] = 100.0
affine = nb.affines.from_matvec(np.eye(3) * 4, [-40] * 3)
xfm = DenseFieldTransform(nb.Nifti1Image(deltas, affine))

# 0.01 mm off-grid, one voxel before the grid start: no displacement
assert np.allclose(xfm.map([[-44.01, 0, 0]]), [[-44.01, 0, 0]])
# On-grid, one voxel before the grid start
assert np.allclose(xfm.map([[-44.0, 0, 0]]), [[-44.0, 0, 0]])

The first assertion passes, second fails:

>>> xfm.map([[-44.01, 0, 0]])
array([[-44.01,   0.  ,   0.  ]])
>>> xfm.map([[-44.0, 0, 0]])
array([[140.,   0.,   0.]])
主要言語
Python
スター
36
フォーク
18
平均マージ
1時間 24分
マージ済み PR(30日)
1

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

nipy/nitransforms のほかの issue

nipy/nitransforms の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。