-
Notifications
You must be signed in to change notification settings - Fork 2
[WIP] Fix segmentation fault in test_mfi_calculation #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2534,11 +2534,12 @@ def MFI(high: Union[np.ndarray, list], | |||||||||||||||||
| if timeperiod < 2: | ||||||||||||||||||
| raise ValueError("timeperiod must be >= 2") | ||||||||||||||||||
|
|
||||||||||||||||||
| # Convert to numpy arrays | ||||||||||||||||||
| high = np.asarray(high, dtype=np.float64) | ||||||||||||||||||
| low = np.asarray(low, dtype=np.float64) | ||||||||||||||||||
| close = np.asarray(close, dtype=np.float64) | ||||||||||||||||||
| volume = np.asarray(volume, dtype=np.float64) | ||||||||||||||||||
| # Convert to numpy arrays with float64 dtype and ensure contiguous memory layout | ||||||||||||||||||
| # This prevents potential segfaults in numba JIT-compiled functions | ||||||||||||||||||
| high = np.ascontiguousarray(np.asarray(high, dtype=np.float64)) | ||||||||||||||||||
| low = np.ascontiguousarray(np.asarray(low, dtype=np.float64)) | ||||||||||||||||||
| close = np.ascontiguousarray(np.asarray(close, dtype=np.float64)) | ||||||||||||||||||
| volume = np.ascontiguousarray(np.asarray(volume, dtype=np.float64)) | ||||||||||||||||||
|
Comment on lines
+2539
to
+2542
|
||||||||||||||||||
| high = np.ascontiguousarray(np.asarray(high, dtype=np.float64)) | |
| low = np.ascontiguousarray(np.asarray(low, dtype=np.float64)) | |
| close = np.ascontiguousarray(np.asarray(close, dtype=np.float64)) | |
| volume = np.ascontiguousarray(np.asarray(volume, dtype=np.float64)) | |
| high = np.ascontiguousarray(high, dtype=np.float64) | |
| low = np.ascontiguousarray(low, dtype=np.float64) | |
| close = np.ascontiguousarray(close, dtype=np.float64) | |
| volume = np.ascontiguousarray(volume, dtype=np.float64) |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -357,7 +357,14 @@ def test_ad_calculation(self, sample_df): | |||||||||
|
|
||||||||||
| def test_mfi_calculation(self, sample_df): | ||||||||||
| """Test MFI calculation.""" | ||||||||||
| result = sample_df.ta.mfi(timeperiod=14) | ||||||||||
| # Ensure OHLCV data has correct dtypes to prevent numba JIT issues | ||||||||||
| df = sample_df.copy() | ||||||||||
| df['high'] = df['high'].astype(np.float64) | ||||||||||
| df['low'] = df['low'].astype(np.float64) | ||||||||||
| df['close'] = df['close'].astype(np.float64) | ||||||||||
| df['volume'] = df['volume'].astype(np.float64) | ||||||||||
|
Comment on lines
+362
to
+365
|
||||||||||
| df['high'] = df['high'].astype(np.float64) | |
| df['low'] = df['low'].astype(np.float64) | |
| df['close'] = df['close'].astype(np.float64) | |
| df['volume'] = df['volume'].astype(np.float64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
np.ascontiguousarraywrapper is only applied to the MFI function, but not to other similar volume-based indicators likeAD,OBV, andADOSCinsrc/numta/api/volume_indicators.py, which also use numba JIT compilation and accept the same types of inputs.If non-contiguous arrays cause segfaults in MFI's numba functions, the same issue could potentially affect other indicators. Consider either:
This ensures consistency and prevents similar issues from occurring with other indicators.