<commit_before>"""qicommon.py

Provides common functions / classes for qiview and qislices"""
import argparse
import numpy as np
import scipy.ndimage.interpolation as ndinterp
import matplotlib as mpl
import matplotlib.cm as cm

def find_bbox(img):
    """Finds the bounding box of non-zero voxels"""
    data = img.get_data()

    # Individual axis min/maxes
    xmin, xmax = np.where(np.any(data, axis=(1, 2)))[0][[0, -1]]
    ymin, ymax = np.where(np.any(data, axis=(0, 2)))[0][[0, -1]]
    zmin, zmax = np.where(np.any(data, axis=(0, 1)))[0][[0, -1]]

    # Convedir_rt to physical space
    corners = np.array([[xmin, ymin, zmin, 1.],
                        [xmax, ymax, zmax, 1.]])
    corners = np.dot(img.get_affine(), corners.T)
    # Now do min/maxes again to standardise corners
    corner1 = np.min(corners[0:3, :], axis=1)
    corner2 = np.max(corners[0:3, :], axis=1)
    return corner1, corner2

class Slice:
    """A very simple slice class. Stores physical & voxel space co-ords"""
    def __init__(self, c1, c2, axis, pos, samples, absolute=False):
        axis_map = {'x':0, 'y':1, 'z':2}
        ind_0 = axis_map[axis]
        ind_1 = (ind_0 + 1) % 3
        ind_2 = (ind_0 + 2) % 3
        start = np.copy(c1)
        diag = c2 - c1
        if absolute:
            start[ind_0] = pos
        else:
            start[ind_0] = c1[ind_0]*(1-pos) + c2[ind_0]*pos
        dir_rt = np.zeros((3,))
        dir_up = np.zeros((3,))

        dir_rt[ind_1] = diag[ind_1]
        dir_up[ind_2] = diag[ind_2]
        aspect = np.linalg.norm(dir_up) / np.linalg.norm(dir_rt)
        samples_up = np.round(aspect * samples)
        self._world_space = (start[:, None, None] +<commit_msg>ENH: Added a padding argument to find_bbox<commit_after>"""qicommon.py

Provides common functions / classes for qiview and qislices"""
import argparse
import numpy as np
import scipy.ndimage.interpolation as ndinterp
import matplotlib as mpl
import matplotlib.cm as cm

def find_bbox(img, padding=0):
    """Finds the bounding box of non-zero voxels"""
    data = img.get_data()

    # Individual axis min/maxes
    xmin, xmax = np.where(np.any(data, axis=(1, 2)))[0][[0, -1]]
    ymin, ymax = np.where(np.any(data, axis=(0, 2)))[0][[0, -1]]
    zmin, zmax = np.where(np.any(data, axis=(0, 1)))[0][[0, -1]]

    # Convedir_rt to physical space
    corners = np.array([[xmin, ymin, zmin, 1.],
                        [xmax, ymax, zmax, 1.]])
    corners = np.dot(img.get_affine(), corners.T)
    # Now do min/maxes again to standardise corners
    corner1 = np.min(corners[0:3, :], axis=1) - padding
    corner2 = np.max(corners[0:3, :], axis=1) + padding
    return corner1, corner2

class Slice:
    """A very simple slice class. Stores physical & voxel space co-ords"""
    def __init__(self, c1, c2, axis, pos, samples, absolute=False):
        axis_map = {'x':0, 'y':1, 'z':2}
        ind_0 = axis_map[axis]
        ind_1 = (ind_0 + 1) % 3
        ind_2 = (ind_0 + 2) % 3
        start = np.copy(c1)
        diag = c2 - c1
        if absolute:
            start[ind_0] = pos
        else:
            start[ind_0] = c1[ind_0]*(1-pos) + c2[ind_0]*pos
        dir_rt = np.zeros((3,))
        dir_up = np.zeros((3,))

        dir_rt[ind_1] = diag[ind_1]
        dir_up[ind_2] = diag[ind_2]
        aspect = np.linalg.norm(dir_up) / np.linalg.norm(dir_rt)
        samples_up = np.round(aspect * samples)
        self._world_space = (start[:, None, None] +
{'fraction_non_alphanumeric': 0.15565217391304348, 'fraction_numerical': 0.03333333333333333, 'mean_word_length': 2.636459430979979, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6455644', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1331, 'n_words': 321}