skills/cv/black-slice-replacement/SKILL.md
Detect all-black DICOM/MRI slices (mean==0) and replace them by randomly sampling a non-black slice from the same series
npx skillsauth add wenmin-wu/ds-skills cv-black-slice-replacementInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Medical imaging volumes sometimes contain entirely black slices due to scanner artifacts, corrupt files, or padding. Feeding black slices into a model wastes capacity and can degrade training. Detecting slices with mean == 0 and replacing them with a randomly sampled non-black slice from the same series maintains the expected input shape while providing meaningful pixel data.
import random
import numpy as np
def load_slice_with_fallback(path, all_paths, read_fn, max_retries=100):
"""Load a DICOM slice; if all-black, replace with random non-black slice."""
image = read_fn(path)
retries = 0
while image.mean() == 0 and retries < max_retries:
image = read_fn(random.choice(all_paths))
retries += 1
return image
def load_volume_safe(file_paths, read_fn):
"""Load volume with black-slice replacement."""
slices = []
for p in file_paths:
slices.append(load_slice_with_fallback(p, file_paths, read_fn))
return np.stack(slices)
image.mean() == 0 (all-black)mean == 0 catches fully black; use mean < epsilon for near-blackdata-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF