lama/qc/qc_images.py
def _overlay_labels(first_stage_reg_dir: Path,
                    inverted_labeldir: Path,
                    out_dir_labels: Path,
                    mask: Path=None):
    """
    Overlay the first registrated image (rigid) with the corresponding inverted labels
    It depends on the registered volumes and inverted label maps being named identically
    """
    if mask:
        mask = sitk.GetArrayFromImage(sitk.ReadImage(str(mask)))
        rp = regionprops(mask)
        # Get the largest label. Likley only one from the mask
        mask_props = list(reversed(sorted(rp, key=lambda x: x.area)))[0]
        bbox = mask_props['bbox']

    for vol_path in common.get_file_paths(first_stage_reg_dir, ignore_folders=[RESOLUTION_IMGS_DIR, IMG_PYRAMID_DIR]):

        vol_reader = common.LoadImage(vol_path)

        if not vol_reader:
            logging.error(f'cannnot create qc image from {vol_path}')
            return

        label_path = inverted_labeldir / vol_path.stem / vol_path.name

        if label_path.is_file():
            label_reader = common.LoadImage(label_path)

            if not label_reader:
                logging.error(f'cannot create qc image from label file {label_path}')
                return

            cast_img = sitk.Cast(sitk.RescaleIntensity(vol_reader.img), sitk.sitkUInt8)
            arr = sitk.GetArrayFromImage(cast_img)
            base = splitext(basename(label_reader.img_path))[0]
            l_arr = label_reader.array

            def sag(idx_):
                slice_sag = np.flipud(arr[:, :, idx_])
                l_slice_sag = np.flipud(l_arr[:, :, idx_])
                sag_dir = out_dir_labels / 'sagittal'
                sag_dir.mkdir(exist_ok=True)
                out_path_sag = sag_dir / f'{base}_{idx_}.png'
                _blend_8bit(slice_sag, l_slice_sag, out_path_sag)
            if mask is None: # get a few slices from middle
                sag_indxs = np.linspace(0, arr.shape[2], 8, dtype=np.int)[2:-2]
            else:
                sag_start = bbox[2]
                sag_end = bbox[5]
                sag_indxs = np.linspace(sag_start, sag_end, 6, dtype=np.int)[1:-1] # Take the 4 inner slices
            for idx in sag_indxs:
                sag(idx)

            def ax(idx_):
                slice_ax = arr[idx_, :, :]
                l_slice_ax = l_arr[idx_, :, :]
                ax_dir = out_dir_labels / 'axial'
                ax_dir.mkdir(exist_ok=True)
                out_path_ax = ax_dir / f'{base}_{idx_}.png'
                _blend_8bit(slice_ax, l_slice_ax, out_path_ax)
            if mask is None: # get a few slices from middle
                ax_indxs = np.linspace(0, arr.shape[0], 8, dtype=np.int)[2:-2]
            else:
                ax_start = bbox[0]
                ax_end = bbox[3]
                ax_indxs = np.linspace(ax_start, ax_end, 6, dtype=np.int)[1:-1]
            for idx in ax_indxs:
                ax(idx)

            def cor(idx_):
                slice_cor = np.flipud(arr[:, idx_, :])
                l_slice_cor = np.flipud(l_arr[:, idx_, :])
                cor_dir = out_dir_labels / 'coronal'
                cor_dir.mkdir(exist_ok=True)
                out_path_cor = cor_dir / f'{base}_{idx_}.png'
                _blend_8bit(slice_cor, l_slice_cor, out_path_cor)
            if mask is None: # get a few slices from middle
                cor_indxs = np.linspace(0, arr.shape[1], 8, dtype=np.int)[2:-2]
            else:
                cor_start = bbox[1]
                cor_end = bbox[4]
                cor_indxs = np.linspace(cor_start, cor_end, 6, dtype=np.int)[1:-1]
            for idx in cor_indxs:
                cor(idx)

        else:
            logging.info('No inverted label found. Skipping creation of inverted label-image overlay')

    Overlay the first registrated image (rigid) with the corresponding inverted labels
    It depends on the registered volumes and inverted label maps being named identically
    
Overlay the first registrated image (rigid) with the corresponding inverted labels
It depends on the registered volumes and inverted label maps being named identically
Overlay the first registrated image ( rigid ) with the corresponding inverted labels It depends on the registered volumes and inverted label maps being named identically
def _overlay_labels(first_stage_reg_dir: Path,
                    inverted_labeldir: Path,
                    out_dir_labels: Path,
                    mask: Path=None):
    if mask:
        mask = sitk.GetArrayFromImage(sitk.ReadImage(str(mask)))
        rp = regionprops(mask)
        mask_props = list(reversed(sorted(rp, key=lambda x: x.area)))[0]
        bbox = mask_props['bbox']
    for vol_path in common.get_file_paths(first_stage_reg_dir, ignore_folders=[RESOLUTION_IMGS_DIR, IMG_PYRAMID_DIR]):
        vol_reader = common.LoadImage(vol_path)
        if not vol_reader:
            logging.error(f'cannnot create qc image from {vol_path}')
            return
        label_path = inverted_labeldir / vol_path.stem / vol_path.name
        if label_path.is_file():
            label_reader = common.LoadImage(label_path)
            if not label_reader:
                logging.error(f'cannot create qc image from label file {label_path}')
                return
            cast_img = sitk.Cast(sitk.RescaleIntensity(vol_reader.img), sitk.sitkUInt8)
            arr = sitk.GetArrayFromImage(cast_img)
            base = splitext(basename(label_reader.img_path))[0]
            l_arr = label_reader.array
            def sag(idx_):
                slice_sag = np.flipud(arr[:, :, idx_])
                l_slice_sag = np.flipud(l_arr[:, :, idx_])
                sag_dir = out_dir_labels / 'sagittal'
                sag_dir.mkdir(exist_ok=True)
                out_path_sag = sag_dir / f'{base}_{idx_}.png'
                _blend_8bit(slice_sag, l_slice_sag, out_path_sag)
            if mask is None: 
                sag_indxs = np.linspace(0, arr.shape[2], 8, dtype=np.int)[2:-2]
            else:
                sag_start = bbox[2]
                sag_end = bbox[5]
                sag_indxs = np.linspace(sag_start, sag_end, 6, dtype=np.int)[1:-1] 
            for idx in sag_indxs:
                sag(idx)
            def ax(idx_):
                slice_ax = arr[idx_, :, :]
                l_slice_ax = l_arr[idx_, :, :]
                ax_dir = out_dir_labels / 'axial'
                ax_dir.mkdir(exist_ok=True)
                out_path_ax = ax_dir / f'{base}_{idx_}.png'
                _blend_8bit(slice_ax, l_slice_ax, out_path_ax)
            if mask is None: 
                ax_indxs = np.linspace(0, arr.shape[0], 8, dtype=np.int)[2:-2]
            else:
                ax_start = bbox[0]
                ax_end = bbox[3]
                ax_indxs = np.linspace(ax_start, ax_end, 6, dtype=np.int)[1:-1]
            for idx in ax_indxs:
                ax(idx)
            def cor(idx_):
                slice_cor = np.flipud(arr[:, idx_, :])
                l_slice_cor = np.flipud(l_arr[:, idx_, :])
                cor_dir = out_dir_labels / 'coronal'
                cor_dir.mkdir(exist_ok=True)
                out_path_cor = cor_dir / f'{base}_{idx_}.png'
                _blend_8bit(slice_cor, l_slice_cor, out_path_cor)
            if mask is None: 
                cor_indxs = np.linspace(0, arr.shape[1], 8, dtype=np.int)[2:-2]
            else:
                cor_start = bbox[1]
                cor_end = bbox[4]
                cor_indxs = np.linspace(cor_start, cor_end, 6, dtype=np.int)[1:-1]
            for idx in cor_indxs:
                cor(idx)
        else:
            logging.info('No inverted label found. Skipping creation of inverted label-image overlay')
def _overlay_labels ( first_stage_reg_dir : Path , inverted_labeldir : Path , out_dir_labels : Path , mask : Path = None ) : if mask : mask = sitk . GetArrayFromImage ( sitk . ReadImage ( str ( mask ) ) ) rp = regionprops ( mask ) mask_props = list ( reversed ( sorted ( rp , key = lambda x : x . area ) ) ) [ 0 ] bbox = mask_props [ 'bbox' ] for vol_path in common . get_file_paths ( first_stage_reg_dir , ignore_folders = [ RESOLUTION_IMGS_DIR , IMG_PYRAMID_DIR ] ) : vol_reader = common . LoadImage ( vol_path ) if not vol_reader : logging . error ( f'cannnot create qc image from {vol_path}' ) return label_path = inverted_labeldir / vol_path . stem / vol_path . name if label_path . is_file ( ) : label_reader = common . LoadImage ( label_path ) if not label_reader : logging . error ( f'cannot create qc image from label file {label_path}' ) return cast_img = sitk . Cast ( sitk . RescaleIntensity ( vol_reader . img ) , sitk . sitkUInt8 ) arr = sitk . GetArrayFromImage ( cast_img ) base = splitext ( basename ( label_reader . img_path ) ) [ 0 ] l_arr = label_reader . array def sag ( idx_ ) : slice_sag = np . flipud ( arr [ : , : , idx_ ] ) l_slice_sag = np . flipud ( l_arr [ : , : , idx_ ] ) sag_dir = out_dir_labels / 'sagittal' sag_dir . mkdir ( exist_ok = True ) out_path_sag = sag_dir / f'{base}_{idx_}.png' _blend_8bit ( slice_sag , l_slice_sag , out_path_sag ) if mask is None : sag_indxs = np . linspace ( 0 , arr . shape [ 2 ] , 8 , dtype = np . int ) [ 2 : - 2 ] else : sag_start = bbox [ 2 ] sag_end = bbox [ 5 ] sag_indxs = np . linspace ( sag_start , sag_end , 6 , dtype = np . int ) [ 1 : - 1 ] for idx in sag_indxs : sag ( idx ) def ax ( idx_ ) : slice_ax = arr [ idx_ , : , : ] l_slice_ax = l_arr [ idx_ , : , : ] ax_dir = out_dir_labels / 'axial' ax_dir . mkdir ( exist_ok = True ) out_path_ax = ax_dir / f'{base}_{idx_}.png' _blend_8bit ( slice_ax , l_slice_ax , out_path_ax ) if mask is None : ax_indxs = np . linspace ( 0 , arr . shape [ 0 ] , 8 , dtype = np . int ) [ 2 : - 2 ] else : ax_start = bbox [ 0 ] ax_end = bbox [ 3 ] ax_indxs = np . linspace ( ax_start , ax_end , 6 , dtype = np . int ) [ 1 : - 1 ] for idx in ax_indxs : ax ( idx ) def cor ( idx_ ) : slice_cor = np . flipud ( arr [ : , idx_ , : ] ) l_slice_cor = np . flipud ( l_arr [ : , idx_ , : ] ) cor_dir = out_dir_labels / 'coronal' cor_dir . mkdir ( exist_ok = True ) out_path_cor = cor_dir / f'{base}_{idx_}.png' _blend_8bit ( slice_cor , l_slice_cor , out_path_cor ) if mask is None : cor_indxs = np . linspace ( 0 , arr . shape [ 1 ] , 8 , dtype = np . int ) [ 2 : - 2 ] else : cor_start = bbox [ 1 ] cor_end = bbox [ 4 ] cor_indxs = np . linspace ( cor_start , cor_end , 6 , dtype = np . int ) [ 1 : - 1 ] for idx in cor_indxs : cor ( idx ) else : logging . info ( 'No inverted label found. Skipping creation of inverted label-image overlay' )
Overlay the first registrated image (rigid) with the corresponding inverted labels
It depends on the registered volumes and inverted label maps being named identically
Overlay the first registrated image ( rigid ) with the corresponding inverted labels It depends on the registered volumes and inverted label maps being named identically
"""
    Overlay the first registrated image (rigid) with the corresponding inverted labels
    It depends on the registered volumes and inverted label maps being named identically
    """ # Get the largest label. Likley only one from the mask # get a few slices from middle # Take the 4 inner slices # get a few slices from middle # get a few slices from middle
first_stage_reg_dir
Path
inverted_labeldir
Path
out_dir_labels
Path
mask
Path