| 1 |  |  | from __future__ import annotations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from importlib import import_module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from importlib.util import find_spec | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from shutil import which | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from types import ModuleType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | def _check_module(*, module: str, extra: str, package: str | None = None) -> None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     if find_spec(module) is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         name = module if package is None else package | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         message = ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |             f'The `{name}` package is required for this.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |             f' Install TorchIO with the `{extra}` extra:' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |             f' `pip install torchio[{extra}]`.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         raise ImportError(message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | def _check_and_import(module: str, extra: str, **kwargs) -> ModuleType: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     _check_module(module=module, extra=extra, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     return import_module(module) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | def get_pandas() -> ModuleType: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     return _check_and_import(module='pandas', extra='csv') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | def get_colorcet() -> ModuleType: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     return _check_and_import(module='colorcet', extra='plot') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | def get_ffmpeg() -> ModuleType: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     ffmpeg = _check_and_import(module='ffmpeg', extra='video', package='ffmpeg-python') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     _check_executable('ffmpeg') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     return ffmpeg | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | def get_sklearn() -> ModuleType: | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     return _check_and_import(module='sklearn', extra='sklearn', package='scikit-learn') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | def _check_executable(executable: str) -> None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     if which(executable) is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         message = ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             f'The `{executable}` executable is required for this. Install it from your' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ' package manager or download it from the official website.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         ) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |         raise FileNotFoundError(message) | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |  |