"Assertion '__builtin_expect(__n < this->size(), true)' failed" error in Visual Studio Code Python

Viewed 1032

I know there is already a lot on Assertion Failure. But none was useful for me. Hear me out.

This is the code:

import numpy as np, pandas as pd
from outliertree import OutlierTree

### random data frame with an obvious outlier
nrows = 100
np.random.seed(1)
df = pd.DataFrame({
    "numeric_col1" : np.r_[np.random.normal(size = nrows - 1), np.array([float(1e6)])],
    "numeric_col2" : np.random.gamma(1, 1, size = nrows),
    "categ_col"    : np.random.choice(['categA', 'categB', 'categC'], size = nrows)
    })

### test data frame with another obvious outlier
df_test = pd.DataFrame({
    "numeric_col1" : np.random.normal(size = nrows),
    "numeric_col2" : np.r_[np.array([float(-1e6)]), np.random.gamma(1, 1, size = nrows - 1)],
    "categ_col"    : np.random.choice(['categA', 'categB', 'categC'], size = nrows)
    })

### fit model
outliers_model = OutlierTree()
outliers_df = outliers_model.fit(df, outliers_print = 10, return_outliers = True) # gives error

### find outliers in new data
new_outliers = outliers_model.predict(df_test)

### print outliers in readable format
outliers_model.print_outliers(new_outliers)

This is the error:

/usr/include/c++/10/bits/stl_vector.h:1045: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = char; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::reference = char&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]: Assertion '__builtin_expect(__n < this->size(), true)' failed. Aborted (core dumped)

The error happens at line:

outliers_df = outliers_model.fit(df, outliers_print = 10, return_outliers = True)

Python version:

Python 3.9.2 (default, Feb 20 2021, 00:00:00) [GCC 10.2.1 20201125 (Red Hat 10.2.1-9)] on linux

OS:

Fedora 5.10.12-200.fc33.x86_64

IDE:

Visual Studio Code

Code works fine in Google Colab. So why is it happening only in the IDE? And if it's a setting or environment issue then what can I do? This is my first time working on Visual Studio Code.

Thank you

2 Answers

Author of the library here. There was a bug in the code which would only trigger when the setup has defined a macro _GLIBCXX_ASSERTIONS and would otherwise not show up. It should be fixed now in the latest version (1.7.0) - please try again (pip install -U outliertree) and comment in the github issue tracker if the problem persists.

I don't know enough about OutlierTree to know how to fix the error. However, the error you see is in the function std::vector::operator[], which is std::vector's access by index function. The assertion error simply means that you tried to access an item at an index that is greater than the vector's length.

As to why you see this error only in VS Code, I assume that it's because assertions are usually checked only in debug mode. Google Collab is probably compiled in release mode without the assertions - that doesn't mean that you don't access out-of-bounds items, it just doesn't catch the error.

I suggest you use gdb or another debugger to understand which vector you try to access out-of-bound, then attaching a stacktrace to your question. Maybe file a bug report with OutlierTree - this seems something they could handle better.

Related