[BUG] Potential wrong edge_mask calculation and out-of-range indexing issues with QuickShear defacing
@uturkbey がすでに取り組んでいます。
2026年1月28日 から。
評価
この issue はまだ評価されていません。
説明
Description of the potential edge_mask calculation issue
@sezginer and I might have found an issue in brainles_preprocessing/defacing/quickshear/nipy_quickshear.py where the edge_mask computation can become incorrect when the brain area in the brain masks touch the array boundary.
In particular, the current implementation of edge_mask uses np.roll to compare shifted versions of the mask:
When the brain mask touches an array edge, np.roll introduces wrap-around: voxels rolled off from one side, appear on the opposite side. This creates an artificial edge on the opposite boundary. As a result, edgemask contains spurious edges and can propagate incorrect geometry into subsequent steps.
An example case is visualized in the top row of the first figure below. In RPS orientation, posterior part of the brain mask touches the posterior array boundary. During edge_mask calculation, 'fake' edge voxels appear on the anterior side even though the real boundary contact is posterior. And these 'fake' edges propagate through convex_hull, and slope calculation steps. Second figure zooms in to 'fake' edge area.
Description of the potential out-of-range indexing during defaced_mask calculation issue
Additionally (related, but not necessarily dependent), a separate error can occur during the calculation of the defacing mask, if the computed defacing plane intersects the array edge posterior to the brain (e.g., as in the top row right plot of the first figure above).
Based on the ys calculation with mask_RPS.shape[2] and the for loop iterating over the positive ys indices, x values might go beyond defaced_mask_RPS.shape[1] range and cause index out-of-range errors for such special cases.
https://github.com/BrainLesion/preprocessing/blob/6878ce198dce5454ddef9ae10953f4906b8ba1de/brainles_preprocessing/defacing/quickshear/nipy_quickshear.py#L162-L166
In our case, the incorrect edgemask triggered this later crash, but we believe the out-of-range indexing can happen independently depending on geometry of brain masks and plane placement (although we acknowledge this should be a very rare case with regular brain segmentations).
To Reproduce
Steps to reproduce the behavior:
- Download this brain segmentation mask: native__t1-der-cor-nc_brain_mask.nii.gz
- Install 'BrainLes-Preprocessing' as suggested
- Run defacing with the example code below:
from brainles_preprocessing.defacing import QuickshearDefacer defacer = QuickshearDefacer( buffer=10.0, force_atlas_registration=False, # We don't need atlas registration ) defacer.deface( input_image_path=Path(your_path_to_native__t1-der-cor-nc_brain_mask), mask_image_path=Path(your_path_to_output_defacing_mask) ) - You should see an error message similar to this:
Traceback (most recent call last): File "/home/user/deface_test.py", line 112, in <module> defacer.deface( File "/home/user/preprocessing/brainles_preprocessing/defacing/quickshear/quickshear.py", line 68, in deface mask = run_quickshear(bet_img=bet_img, buffer=self.buffer) File "/home/user/preprocessing/brainles_preprocessing/defacing/quickshear/nipy_quickshear.py", line 166, in run_quickshear defaced_mask_RPS[:, x, :y] = 0 IndexError: index 180 is out of bounds for axis 1 with size 180 - As intermediate values such as
edgemaskare implicit, you would need to extract those arrays manually.
Expected behavior
Second row of the first figure above shows our expected behavior. To get these results:
- We replace the
np.rollimplementation inedge_maskcalculation with convolution as below:kernel = np.array([ [0, -1, 0], [-1, 4, -1], [0, -1, 0] ]) edgemask = ndimage.convolve(brain, kernel, mode='constant', cval=0)
operating system and version
Debian GNU/Linux 11 (bullseye)
Python environment and version?
miniforge3 environment with Python 3.10.19
version of brainles_preprocessing
brainles_preprocessing==0.6.8
numpy==2.2.6
- 主要言語
- C
- スター
- 39
- フォーク
- 11
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトの環境構築ファイルはまだ確認していません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
BrainLesion/preprocessing のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
BrainLesion/preprocessing#169 · コメント 7 件 ·
-
[BUG] ANTsRegistrator resamples MRI modalities with nearest-neighbor interpolation by default対応中かも @LMZimmer が 9 日前に担当しました。 オープンbug
BrainLesion/preprocessing#192 · コメント 4 件 · 担当者 1 名 ·
-
[BUG] HTTP error responses from Zenodo bypass the "use local copy" fallback対応中かも @ErikGro が 19 日前に担当しました。 オープンbug
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
BrainLesion/preprocessing#190 ·
-
[BUG] cannot import ANTSRegistrator再び着手できるかも @neuronflow が 44 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンbug
BrainLesion/preprocessing#188 · リアクション 1 件 · 担当者 2 名 ·
-
enhancement
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
BrainLesion/preprocessing#185 ·
BrainLesion/preprocessing の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
johnsonjh/emu2-cpm86#68 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Zenmap CrashオープンZenmap
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
BasedHardware/omi#19306 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
fastfetch-cli/fastfetch#2619 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100