<commit_before>"""ROI masks."""

import re
import numpy as np

import dwi.util

class Mask(object):
    """Mask for one slice in 3D image."""
    def __init__(self, slice, array):
        if slice < 1:
            raise Exception('Invalid slice')
        self.slice = slice # Slice number, one-based indexing
        self.array = array.astype(bool) # 2D mask of one slice.

    def __repr__(self):
        return repr((self.slice, self.array.shape))

    def __str__(self):
        return repr(self)

    def get_subwindow(self, coordinates, onebased=True):
        """Get a view of a specific subwindow."""
        if onebased:
            coordinates = [i-1 for i in coordinates] # One-based indexing.
        z0, z1, y0, y1, x0, x1 = coordinates
        slice = self.slice - z0
        array = self.array[y0:y1,x0:x1]
        return Mask(slice, array)

    def get_masked(self, array):
        """Get masked region as a flat array."""
        if array.ndim == self.array.ndim:
            return array[self.array]
        else:
            return array[self.slice-1, self.array]

    def write(self, filename):
        """Write mask as an ASCII file."""
        with open(filename, 'w') as f:
            f.write('slice: %s\n' % self.slice)
            f.write(mask_to_text(self.array.astype(int)))

def mask_to_text(mask):
    return '\n'.join(map(line_to_text, mask))

def line_to_text(line):
    return ''.join(map(str, line))

def load_ascii(filename):
    """Read a ROI mask file."""
    slice = 1
    arrays = []
    with open(filename, 'rU') as f:
        p = re.compile(r'(\w+)\s*:\s*(.*)')
        for line in f:
            line = line.strip()
            if not line:
                continue
            m = p.match(line)
            if m:
                if m.group(1) == 'slice':
                    slice = int(m.group(2))
            elif line[0] == '0' or line[0] == '1':
                a = np.array(list(line), dtype=int)
                arrays.append(a)
    if arrays:
        return Mask(slice, np.array(arrays))
    else:
        raise Exception('No mask found in %s' % filename)

class Mask3D(object):
    """Image mask stored as a 3D array."""
    def __init__(self, a):
        if a.ndim != 3:
            raise 'Invalid mask dimensionality: %s' % a.shape
        self.array = a.astype(bool)

    def __repr__(self):
        return repr(self.array.shape)

    def __str__(self):
        return repr(self)

    def n_selected(self):
        """Return number of selected voxels."""
        return np.count_nonzero(mask)

    def get_subwindow(self, coordinates, onebased=True):
        """Get a view of a specific subwindow."""
        if onebased:
            coordinates = [i-1 for i in coordinates]
        z0, z1, y0, y1, x0, x1 = coordinates
        array = self.array[z0:z1, y0:y1, x0:x1]
        return Mask3D(array)

    def get_masked(self, a):
        """Return masked voxels."""
        return a[self.array]

    def apply_mask(self, a, value=0):
        """Cover masked voxels by zero or other value."""
        copy = a.copy()
        copy[-self.array,...] = value
        return copy

    def where(self):
        """Return indices of masked voxels."""
        return np.argwhere(self.array)

    def crop(self, subwindow, onebased=False):
        """Return a copied subwindow."""
        a = dwi.util.crop_image(self.array, subwindow, onebased).copy()
        return Mask3D(a)
<commit_msg>Added function to get slices with selected voxels<commit_after>"""ROI masks."""

import re
import numpy as np

import dwi.util

class Mask(object):
    """Mask for one slice in 3D image."""
    def __init__(self, slice, array):
        if slice < 1:
            raise Exception('Invalid slice')
        self.slice = slice # Slice number, one-based indexing
        self.array = array.astype(bool) # 2D mask of one slice.

    def __repr__(self):
        return repr((self.slice, self.array.shape))

    def __str__(self):
        return repr(self)

    def get_subwindow(self, coordinates, onebased=True):
        """Get a view of a specific subwindow."""
        if onebased:
            coordinates = [i-1 for i in coordinates] # One-based indexing.
        z0, z1, y0, y1, x0, x1 = coordinates
        slice = self.slice - z0
        array = self.array[y0:y1,x0:x1]
        return Mask(slice, array)

    def get_masked(self, array):
        """Get masked region as a flat array."""
        if array.ndim == self.array.ndim:
            return array[self.array]
        else:
            return array[self.slice-1, self.array]

    def write(self, filename):
        """Write mask as an ASCII file."""
        with open(filename, 'w') as f:
            f.write('slice: %s\n' % self.slice)
            f.write(mask_to_text(self.array.astype(int)))

def mask_to_text(mask):
    return '\n'.join(map(line_to_text, mask))

def line_to_text(line):
    return ''.join(map(str, line))

def load_ascii(filename):
    """Read a ROI mask file."""
    slice = 1
    arrays = []
    with open(filename, 'rU') as f:
        p = re.compile(r'(\w+)\s*:\s*(.*)')
        for line in f:
            line = line.strip()
            if not line:
                continue
            m = p.match(line)
            if m:
                if m.group(1) == 'slice':
                    slice = int(m.group(2))
            elif line[0] == '0' or line[0] == '1':
                a = np.array(list(line), dtype=int)
                arrays.append(a)
    if arrays:
        return Mask(slice, np.array(arrays))
    else:
        raise Exception('No mask found in %s' % filename)

class Mask3D(object):
    """Image mask stored as a 3D array."""
    def __init__(self, a):
        if a.ndim != 3:
            raise 'Invalid mask dimensionality: %s' % a.shape
        self.array = a.astype(bool)

    def __repr__(self):
        return repr(self.array.shape)

    def __str__(self):
        return repr(self)

    def n_selected(self):
        """Return number of selected voxels."""
        return np.count_nonzero(mask)

    def selected_slices(self):
        """Return slice indices that have voxels selected."""
        return np.unique(self.array.nonzero()[0])

    def get_subwindow(self, coordinates, onebased=True):
        """Get a view of a specific subwindow."""
        if onebased:
            coordinates = [i-1 for i in coordinates]
        z0, z1, y0, y1, x0, x1 = coordinates
        array = self.array[z0:z1, y0:y1, x0:x1]
        return Mask3D(array)

    def get_masked(self, a):
        """Return masked voxels."""
        return a[self.array]

    def apply_mask(self, a, value=0):
        """Cover masked voxels by zero or other value."""
        copy = a.copy()
        copy[-self.array,...] = value
        return copy

    def where(self):
        """Return indices of masked voxels."""
        return np.argwhere(self.array)

    def crop(self, subwindow, onebased=False):
        """Return a copied subwindow."""
        a = dwi.util.crop_image(self.array, subwindow, onebased).copy()
        return Mask3D(a)

{'fraction_non_alphanumeric': 0.1325284090909091, 'fraction_numerical': 0.012073863636363636, 'mean_word_length': 2.07736013986014, 'pattern_counts': {'":': 0, '<': 5, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2424446', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2195, 'n_words': 646}