<commit_before>#!/usr/bin/env python
"""qi

Quick Image Module"""

import numpy as np
import scipy.ndimage.interpolation as ndinterp
import argparse
import matplotlib as mpl
import matplotlib.cm as cm
from .slice import Slice, axis_map, axis_indices

def sample_point(img, point, order=1):
    scale = np.mat(img.get_affine()[0:3, 0:3]).I
    offset = np.dot(-scale, img.get_affine()[0:3, 3]).T
    s_point = np.dot(scale, point).T + offset[:]
    return ndinterp.map_coordinates(img.get_data().squeeze(), s_point, order=order)

def img_bbox(img):
    img_shape = img.get_data().shape
    corners = np.array([[0, 0, 0, 1.],
                        [img_shape[0], img_shape[1], img_shape[2], 1.]])
    corners = np.dot(img.get_affine(), corners.T)
    corner1 = np.min(corners[0:3, :], axis=1)
    corner2 = np.max(corners[0:3, :], axis=1)
    return corner1, corner2

def mask_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<commit_msg>WIP: Adding a 3-plane function<commit_after>#!/usr/bin/env python
"""qi

Quick Image Module"""

import numpy as np
import scipy.ndimage.interpolation as ndinterp
import argparse
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from .slice import Slice, axis_map, axis_indices

def sample_point(img, point, order=1):
    scale = np.mat(img.get_affine()[0:3, 0:3]).I
    offset = np.dot(-scale, img.get_affine()[0:3, 3]).T
    s_point = np.dot(scale, point).T + offset[:]
    return ndinterp.map_coordinates(img.get_data().squeeze(), s_point, order=order)

def img_bbox(img):
    img_shape = img.get_data().shape
    corners = np.array([[0, 0, 0, 1.],
                        [img_shape[0], img_shape[1], img_shape[2], 1.]])
    corners = np.dot(img.get_affine(), corners.T)
    corner1 = np.min(corners[0:3, :], axis=1)
    corner2 = np.max(corners[0:3, :], axis=1)
    return corner1, corner2

def center_of_mass(img):
    idx0 = np.argmax(np.sum(img.get_data(), axis=(1,2)))
    idx1 = np.argmax(np.sum(img.get_data(), axis=(0,2)))
    idx2 = np.argmax(np.sum(img.get_data(), axis=(0,1)))
    phys = np.dot(img.affine, np.array([idx0, idx1, idx2,1]).T)
    return phys

def cross_sections(img, cmap='gray'):
    bbox = img_bbox(img)
    center = center_of_mass(img)
    a = ('x', 'y', 'z')
    fig, axes = plt.subplots(1, 3, figsize=(18, 6))
    for i in range(3):
        sl = Slice(bbox[0], bbox[1], a[i], center[i], 256,
                   absolute=True, orient='clin')
        sl_img = sl.sample(img, order=0)
        print(np.min(sl_img), np.max(sl_img))
        im = axes[i].imshow(sl_img, origin='lower', extent=sl.extent, cmap=cmap, vmin = 0.1)
        axes[i].axis('off')
        if i == 2:
            fig.colorbar(im)
    return (fig, axes)

def mask_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
{'fraction_non_alphanumeric': 0.17983651226158037, 'fraction_numerical': 0.032945256378498884, 'mean_word_length': 3.241596638655462, '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': '4960371', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1591, 'n_words': 350}