@@ -254,38 +254,45 @@ def _rs_large(sinogram, snr, size, drop_ratio=0.1, norm=True):
254254 """
255255 Remove large stripes.
256256 """
257- drop_ratio = max (min (drop_ratio , 0.8 ), 0 ) # = cp.clip(drop_ratio, 0.0, 0.8)
257+ drop_ratio = max (min (drop_ratio , 0.8 ), 0 )
258258 (nrow , ncol ) = sinogram .shape
259259 ndrop = int (0.5 * drop_ratio * nrow )
260260 sinosort = cp .sort (sinogram , axis = 0 )
261261 sinosmooth = median_filter (sinosort , (1 , size ))
262+
262263 list1 = cp .mean (sinosort [ndrop : nrow - ndrop ], axis = 0 )
263264 list2 = cp .mean (sinosmooth [ndrop : nrow - ndrop ], axis = 0 )
264265 listfact = list1 / list2
265- # Locate stripes
266+
266267 listmask = _detect_stripe (listfact , snr )
267268 listmask = binary_dilation (listmask , iterations = 1 ).astype (listmask .dtype )
268269
269- # Normalize
270+ # Normalize only affected columns
270271 if norm :
271- sinogram /= cp . tile ( listfact , ( nrow , 1 ))
272+ sinogram [:, listmask > 0 ] /= listfact [ None , listmask > 0 ]
272273
274+ # Identify affected columns
275+ listxmiss = cp .where (listmask > 0.0 )[0 ]
276+ if listxmiss .size == 0 :
277+ return sinogram # No stripes, return early for efficiency
278+
279+ # Process only affected columns
273280 sino_transposed = sinogram .T
274- ids_sort = cp . argsort ( sino_transposed , axis = 1 )
281+ sino_subset = sino_transposed [ listxmiss ] # Extract affected columns
275282
276- # Apply sorting without explicit matindex
277- sino_sorted = cp .take_along_axis (sino_transposed , ids_sort , axis = 1 )
283+ # Sort only the required subset
284+ ids_sort = cp .argsort (sino_subset , axis = 1 )
285+ sino_sorted = cp .take_along_axis (sino_subset , ids_sort , axis = 1 )
278286
279- # Smoothen sorted sinogram
280- sino_sorted [:, :] = cp .transpose (sinosmooth )
287+ # Apply smoothing
288+ sino_sorted [:, :] = cp .transpose (sinosmooth [:, listxmiss ] )
281289
282290 # Restore original order
283291 ids_restore = cp .argsort (ids_sort , axis = 1 )
284- sino_corrected = cp .take_along_axis (sino_sorted , ids_restore , axis = 1 ). T
292+ sino_corrected = cp .take_along_axis (sino_sorted , ids_restore , axis = 1 )
285293
286- # Apply corrections only to affected columns
287- listxmiss = cp .where (listmask > 0.0 )[0 ]
288- sinogram [:, listxmiss ] = sino_corrected [:, listxmiss ]
294+ # Place back corrected data
295+ sinogram [:, listxmiss ] = sino_corrected .T
289296
290297 return sinogram
291298
0 commit comments