civet.minc

Defines types and operations related to MINC files.

  1"""
  2Defines types and operations related to MINC files.
  3"""
  4from dataclasses import dataclass
  5from os import PathLike
  6from typing import Literal, TypeVar, Generic, Optional
  7
  8from civet.bases import DataFile
  9from civet.extraction.kernels import ngh_count_kernel
 10from civet.xfm import TransformProgram, TransformableMixin
 11
 12_M = TypeVar('_M', bound='GenericMinc')
 13_V = TypeVar('_V', bound='GenericMinc')
 14
 15
 16@dataclass(frozen=True)
 17class GenericMinc(TransformableMixin[_M], DataFile[_M], Generic[_M]):
 18
 19    preferred_suffix = '.mnc'
 20    transform_program: TransformProgram = 'transform_volume'
 21
 22    def mincresample(self, like_volume: _V) -> _V:
 23        def command(output):
 24            return (
 25                'mincresample', '-clobber', '-quiet',
 26                '-like', like_volume, self, output
 27            )
 28        return like_volume.create_command(command)
 29
 30    def reshape255(self) -> 'GenericMask':
 31        def command(output):
 32            return (
 33                'mincreshape', '-quiet', '-clobber', '-unsigned', '-byte',
 34                '-image_range', '0', '255', '-valid_range', '0', '255',
 35                self, output
 36            )
 37        return GenericMask(self).create_command(command)
 38
 39    def resamplef64(self, *extra_flags: str) -> 'GenericFloatMinc':
 40        def command(output):
 41            return 'mincresample', '-quiet', '-double', *extra_flags, self, output
 42        return GenericFloatMinc(self).create_command(command)
 43
 44
 45class MincVolume(GenericMinc['MincVolume']):
 46    """
 47    A `MincVolume` represents a volume (`.mnc`).
 48    """
 49    pass
 50
 51
 52_MA = TypeVar('_MA', bound='GenericMask')
 53
 54
 55class GenericMask(GenericMinc[_MA], Generic[_MA]):
 56
 57    def dilate_volume(self, dilation_value: int, neighbors: Literal[6, 26], n_dilations: int) -> _MA:
 58        def command(output):
 59            return (
 60                'dilate_volume', self, output,
 61                str(dilation_value), str(neighbors), str(n_dilations)
 62            )
 63        return self.create_command(command)
 64
 65    def minccalc_u8(self, expression: str, *other_volumes: 'GenericMask') -> _MA:
 66        def command(output):
 67            return (
 68                'minccalc', '-clobber', '-quiet',
 69                '-unsigned', '-byte',
 70                '-expression', expression,
 71                self, *other_volumes, output
 72            )
 73        return self.create_command(command)
 74
 75    def mincdefrag(self, label: int, stencil: Literal[6, 19, 27], max_connect: Optional[int] = None) -> _MA:
 76        def command(output):
 77            cmd = ['mincdefrag', self, output, str(label), str(stencil)]
 78            if max_connect is not None:
 79                cmd.append(str(max_connect))
 80            return cmd
 81        return self.create_command(command)
 82
 83    def reshape_bbox(self) -> _MA:
 84        def command(output):
 85            return 'mincreshape_bbox_helper', self, output
 86        return self.create_command(command)
 87
 88    def mincmorph_convolve_u8(self, kernel: str | PathLike = ngh_count_kernel) -> _MA:
 89        def command(output):
 90            return (
 91                'mincmorph',
 92                '-unsigned', '-byte',
 93                '-convolve',
 94                '-kernel', kernel,
 95                self, output
 96            )
 97        return self.create_command(command)
 98
 99
100class Mask(GenericMask['Mask']):
101    """
102    A `Mask` represents a volume (`.mnc`) with discrete intensities (segmentation volume or brain mask).
103    """
104    pass
105
106
107_F = TypeVar('_F', bound='GenericFloatMinc')
108
109
110class GenericFloatMinc(GenericMinc[_F], Generic[_F]):
111    def mincblur(self, fwhm: float) -> _F:
112        # result is not a binary mask, it has float values in [0, 1],
113        # maybe define a unique type?
114        def command(output):
115            return 'mincblur_correct_name.sh', '-quiet', '-fwhm', str(fwhm), self, output
116        return self.create_command(command)
117
118
119class FloatMinc(GenericFloatMinc['FloatMinc']):
120    pass
@dataclass(frozen=True)
class GenericMinc(civet.xfm.TransformableMixin[~_M], civet.bases.DataFile[~_M], typing.Generic[~_M]):
17@dataclass(frozen=True)
18class GenericMinc(TransformableMixin[_M], DataFile[_M], Generic[_M]):
19
20    preferred_suffix = '.mnc'
21    transform_program: TransformProgram = 'transform_volume'
22
23    def mincresample(self, like_volume: _V) -> _V:
24        def command(output):
25            return (
26                'mincresample', '-clobber', '-quiet',
27                '-like', like_volume, self, output
28            )
29        return like_volume.create_command(command)
30
31    def reshape255(self) -> 'GenericMask':
32        def command(output):
33            return (
34                'mincreshape', '-quiet', '-clobber', '-unsigned', '-byte',
35                '-image_range', '0', '255', '-valid_range', '0', '255',
36                self, output
37            )
38        return GenericMask(self).create_command(command)
39
40    def resamplef64(self, *extra_flags: str) -> 'GenericFloatMinc':
41        def command(output):
42            return 'mincresample', '-quiet', '-double', *extra_flags, self, output
43        return GenericFloatMinc(self).create_command(command)
GenericMinc( input: str | os.PathLike | civet.abstract_data.AbstractDataCommand, transform_program: Literal['transform_objects', 'transform_volume'] = 'transform_volume')
preferred_suffix = '.mnc'

Preferred output path suffix for this command.

transform_program: Literal['transform_objects', 'transform_volume'] = 'transform_volume'

Specifies which program is used to transform objects of this type.

def mincresample(self, like_volume: ~_V) -> ~_V:
23    def mincresample(self, like_volume: _V) -> _V:
24        def command(output):
25            return (
26                'mincresample', '-clobber', '-quiet',
27                '-like', like_volume, self, output
28            )
29        return like_volume.create_command(command)
def reshape255(self) -> civet.minc.GenericMask:
31    def reshape255(self) -> 'GenericMask':
32        def command(output):
33            return (
34                'mincreshape', '-quiet', '-clobber', '-unsigned', '-byte',
35                '-image_range', '0', '255', '-valid_range', '0', '255',
36                self, output
37            )
38        return GenericMask(self).create_command(command)
def resamplef64(self, *extra_flags: str) -> civet.minc.GenericFloatMinc:
40    def resamplef64(self, *extra_flags: str) -> 'GenericFloatMinc':
41        def command(output):
42            return 'mincresample', '-quiet', '-double', *extra_flags, self, output
43        return GenericFloatMinc(self).create_command(command)
class MincVolume(civet.minc.GenericMinc[ForwardRef('MincVolume')]):
46class MincVolume(GenericMinc['MincVolume']):
47    """
48    A `MincVolume` represents a volume (`.mnc`).
49    """
50    pass

A MincVolume represents a volume (.mnc).

class GenericMask(civet.minc.GenericMinc[~_MA], typing.Generic[~_MA]):
56class GenericMask(GenericMinc[_MA], Generic[_MA]):
57
58    def dilate_volume(self, dilation_value: int, neighbors: Literal[6, 26], n_dilations: int) -> _MA:
59        def command(output):
60            return (
61                'dilate_volume', self, output,
62                str(dilation_value), str(neighbors), str(n_dilations)
63            )
64        return self.create_command(command)
65
66    def minccalc_u8(self, expression: str, *other_volumes: 'GenericMask') -> _MA:
67        def command(output):
68            return (
69                'minccalc', '-clobber', '-quiet',
70                '-unsigned', '-byte',
71                '-expression', expression,
72                self, *other_volumes, output
73            )
74        return self.create_command(command)
75
76    def mincdefrag(self, label: int, stencil: Literal[6, 19, 27], max_connect: Optional[int] = None) -> _MA:
77        def command(output):
78            cmd = ['mincdefrag', self, output, str(label), str(stencil)]
79            if max_connect is not None:
80                cmd.append(str(max_connect))
81            return cmd
82        return self.create_command(command)
83
84    def reshape_bbox(self) -> _MA:
85        def command(output):
86            return 'mincreshape_bbox_helper', self, output
87        return self.create_command(command)
88
89    def mincmorph_convolve_u8(self, kernel: str | PathLike = ngh_count_kernel) -> _MA:
90        def command(output):
91            return (
92                'mincmorph',
93                '-unsigned', '-byte',
94                '-convolve',
95                '-kernel', kernel,
96                self, output
97            )
98        return self.create_command(command)

GenericMinc(input: str | os.PathLike | civet.abstract_data.AbstractDataCommand, transform_program: Literal['transform_objects', 'transform_volume'] = 'transform_volume')

def dilate_volume( self, dilation_value: int, neighbors: Literal[6, 26], n_dilations: int) -> ~_MA:
58    def dilate_volume(self, dilation_value: int, neighbors: Literal[6, 26], n_dilations: int) -> _MA:
59        def command(output):
60            return (
61                'dilate_volume', self, output,
62                str(dilation_value), str(neighbors), str(n_dilations)
63            )
64        return self.create_command(command)
def minccalc_u8(self, expression: str, *other_volumes: civet.minc.GenericMask) -> ~_MA:
66    def minccalc_u8(self, expression: str, *other_volumes: 'GenericMask') -> _MA:
67        def command(output):
68            return (
69                'minccalc', '-clobber', '-quiet',
70                '-unsigned', '-byte',
71                '-expression', expression,
72                self, *other_volumes, output
73            )
74        return self.create_command(command)
def mincdefrag( self, label: int, stencil: Literal[6, 19, 27], max_connect: Optional[int] = None) -> ~_MA:
76    def mincdefrag(self, label: int, stencil: Literal[6, 19, 27], max_connect: Optional[int] = None) -> _MA:
77        def command(output):
78            cmd = ['mincdefrag', self, output, str(label), str(stencil)]
79            if max_connect is not None:
80                cmd.append(str(max_connect))
81            return cmd
82        return self.create_command(command)
def reshape_bbox(self) -> ~_MA:
84    def reshape_bbox(self) -> _MA:
85        def command(output):
86            return 'mincreshape_bbox_helper', self, output
87        return self.create_command(command)
def mincmorph_convolve_u8( self, kernel: str | os.PathLike = PosixPath('/home/runner/work/pycivet/pycivet/civet/extraction/kernels/data/ngh_count.kernel')) -> ~_MA:
89    def mincmorph_convolve_u8(self, kernel: str | PathLike = ngh_count_kernel) -> _MA:
90        def command(output):
91            return (
92                'mincmorph',
93                '-unsigned', '-byte',
94                '-convolve',
95                '-kernel', kernel,
96                self, output
97            )
98        return self.create_command(command)
101class Mask(GenericMask['Mask']):
102    """
103    A `Mask` represents a volume (`.mnc`) with discrete intensities (segmentation volume or brain mask).
104    """
105    pass

A Mask represents a volume (.mnc) with discrete intensities (segmentation volume or brain mask).

class GenericFloatMinc(civet.minc.GenericMinc[~_F], typing.Generic[~_F]):
111class GenericFloatMinc(GenericMinc[_F], Generic[_F]):
112    def mincblur(self, fwhm: float) -> _F:
113        # result is not a binary mask, it has float values in [0, 1],
114        # maybe define a unique type?
115        def command(output):
116            return 'mincblur_correct_name.sh', '-quiet', '-fwhm', str(fwhm), self, output
117        return self.create_command(command)

GenericMinc(input: str | os.PathLike | civet.abstract_data.AbstractDataCommand, transform_program: Literal['transform_objects', 'transform_volume'] = 'transform_volume')

def mincblur(self, fwhm: float) -> ~_F:
112    def mincblur(self, fwhm: float) -> _F:
113        # result is not a binary mask, it has float values in [0, 1],
114        # maybe define a unique type?
115        def command(output):
116            return 'mincblur_correct_name.sh', '-quiet', '-fwhm', str(fwhm), self, output
117        return self.create_command(command)
120class FloatMinc(GenericFloatMinc['FloatMinc']):
121    pass

GenericMinc(input: str | os.PathLike | civet.abstract_data.AbstractDataCommand, transform_program: Literal['transform_objects', 'transform_volume'] = 'transform_volume')