1+ function result = check_health()
2+
3+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4+ % Validate MHKiT Python integration and environment health
5+ % Performs comprehensive checks of Python environment, module imports,
6+ % and MHKiT functionality to ensure everything is working correctly
7+ %
8+ % Parameters
9+ % ------------
10+ % None
11+ %
12+ % Returns
13+ % ---------
14+ % result: logical
15+ % true if all health checks pass, false if any fail
16+ %
17+ % Example
18+ % -------
19+ % if mhkit.check_health()
20+ % disp('MHKiT is ready to use!');
21+ % else
22+ % disp('MHKiT has issues - check the output above');
23+ % end
24+ %
25+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26+
27+ logger = mhkit .utils.get_logger();
28+ result = true ; % Start optimistic
29+
30+ try
31+ logger .info(' === MHKiT Health Check ===' );
32+
33+ % Check 1: Python Environment Configuration
34+ logger .info(' === Python Environment Validation ===' );
35+ try
36+ pe = pyenv();
37+ logger .info(' Python Version: %s ' , pe .Version);
38+ logger .info(' Python Executable: %s ' , pe .Executable);
39+ logger .info(' Python Library: %s ' , pe .Library);
40+ logger .info(' Python Home: %s ' , pe .Home);
41+ logger .info(' Execution Mode: %s ' , pe .ExecutionMode);
42+ logger .info(' Status: %s ' , pe .Status);
43+
44+ % Validate Python is properly configured
45+ if strcmp(pe .Status, ' NotLoaded' )
46+ logger .error(' ✗ Python is not loaded' );
47+ result = false ;
48+ elseif strcmp(pe .Status, ' Loaded' )
49+ logger .info(' ✓ Python environment is loaded' );
50+ else
51+ logger .warning(' ⚠ Python status is: %s ' , pe .Status);
52+ end
53+
54+ catch ME
55+ logger .error(' ✗ Failed to get Python environment info: %s ' , ME .message);
56+ result = false ;
57+ end
58+
59+ % Check 2: Python Module Import Tests
60+ logger .info(' === Testing Python Module Import ===' );
61+
62+ % Test mhkit module import
63+ try
64+ py .importlib.import_module(' mhkit' );
65+ logger .info(' ✓ mhkit module imported successfully' );
66+ catch ME
67+ logger .error(' ✗ Failed to import mhkit: %s ' , ME .message);
68+ logger .error(' Try: pip install mhkit==0.9' );
69+ result = false ;
70+ end
71+
72+ % Test mhkit_python_utils module import
73+ try
74+ py .importlib.import_module(' mhkit_python_utils' );
75+ logger .info(' ✓ mhkit_python_utils module imported successfully' );
76+ catch ME
77+ logger .error(' ✗ Failed to import mhkit_python_utils: %s ' , ME .message);
78+ logger .error(' This module should be installed by mhkit.install()' );
79+ result = false ;
80+ end
81+
82+ % Check 3: MHKiT Functionality Test
83+ logger .info(' === Testing MHKiT Functionality ===' );
84+ try
85+ test_result = py .mhkit.river.performance.circular(30 );
86+ logger .info(' ✓ MHKiT function test successful: %s ' , char(test_result ));
87+ catch ME
88+ logger .error(' ✗ Failed to execute MHKiT function: %s ' , ME .message);
89+ logger .error(' This indicates a problem with the MHKiT Python installation' );
90+ result = false ;
91+ end
92+
93+ % Check 4: Additional System Information
94+ logger .info(' === System Information ===' );
95+ try
96+ % Check MATLAB version
97+ logger .info(' MATLAB Version: %s ' , version(' -release' ));
98+
99+ % Check platform
100+ if ispc
101+ platform = ' Windows' ;
102+ elseif ismac
103+ platform = ' macOS' ;
104+ elseif isunix
105+ platform = ' Linux' ;
106+ else
107+ platform = ' Unknown' ;
108+ end
109+ logger .info(' Platform: %s ' , platform );
110+
111+ % Check PATH for Python
112+ current_path = getenv(' PATH' );
113+ if contains(current_path , ' python' ) || contains(current_path , ' conda' )
114+ logger .info(' ✓ Python/Conda found in system PATH' );
115+ else
116+ logger .warning(' ⚠ Python/Conda not detected in system PATH' );
117+ end
118+
119+ catch ME
120+ logger .warning(' ⚠ Could not gather all system information: %s ' , ME .message);
121+ end
122+
123+ % Final Result
124+ if result
125+ logger .info(' === ✓ All Health Checks Passed ===' );
126+ logger .info(' MHKiT is ready to use!' );
127+ else
128+ logger .error(' === ✗ Health Check Failed ===' );
129+ logger .error(' MHKiT has configuration issues that need to be resolved.' );
130+ logger .error(' ' );
131+ logger .error(' Troubleshooting steps:' );
132+ logger .error(' 1. Try restarting MATLAB' );
133+ logger .error(' 2. Run mhkit.install() again' );
134+ logger .error(' 3. Check that pyenv() points to the correct Python environment' );
135+ logger .error(' 4. Verify conda/Python are in your system PATH' );
136+ end
137+
138+ catch ME
139+ logger .error(' === ✗ Health Check Error ===' );
140+ logger .error(' Health check encountered an unexpected error: %s ' , ME .message);
141+ result = false ;
142+ end
143+ end
0 commit comments