Skip to content
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

Fix out of bounds segfault #272

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/e2e_testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
NEPTUNE_API_TOKEN: ${{ secrets.NEPTUNE_E2E_TOKEN }}
NEPTUNE_PROJECT_NAME: "MannLabs/alphaDIA-e2e-tests"
NUMBA_BOUNDSCHECK: 1
NUMBA_DEVELOPER_MODE: 1
NUMBA_FULL_TRACEBACKS: 1
steps:
- uses: actions/checkout@v4
- name: Conda info
Expand Down
79 changes: 75 additions & 4 deletions alphadia/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,19 @@ def center_envelope(x):
) * 0.5


@nb.njit
def center_envelope_1d(x):
@nb.njit(inline="always")
def _odd_center_envelope(x: np.ndarray):
"""
Applies an interference correction envelope to a collection of odd-length 1D arrays.
Numba function which operates in place.

Parameters
----------
x: np.ndarray
Array of shape (a, b) where a is the number of arrays and b is the length of each array.
It is mandatory that dimension b is odd.

"""
center_index = x.shape[1] // 2

for a0 in range(x.shape[0]):
Expand All @@ -362,16 +373,76 @@ def center_envelope_1d(x):

for i in range(1, center_index + 1):
x[a0, center_index - i] = min(left_intensity, x[a0, center_index - i])

left_intensity = (
x[a0, center_index - i] + x[a0, center_index - i - 1]
x[a0, center_index - i] + x[a0, center_index - i + 1]
) * 0.5

x[a0, center_index + i] = min(right_intensity, x[a0, center_index + i])
right_intensity = (
x[a0, center_index + i] + x[a0, center_index + i + 1]
x[a0, center_index + i] + x[a0, center_index + i - 1]
) * 0.5


@nb.njit(inline="always")
def _even_center_envelope(x: np.ndarray):
"""
Applies an interference correction envelope to a collection of even-length 1D arrays.
Numba function which operates in place.

Parameters
----------
x: np.ndarray
Array of shape (a, b) where a is the number of arrays and b is the length of each array.
It is mandatory that dimension b is even.

"""
center_index_right = x.shape[1] // 2
center_index_left = center_index_right - 1

for a0 in range(x.shape[0]):
GeorgWa marked this conversation as resolved.
Show resolved Hide resolved
left_intensity = x[a0, center_index_left]
right_intensity = x[a0, center_index_right]

for i in range(1, center_index_left + 1):
x[a0, center_index_left - i] = min(
left_intensity, x[a0, center_index_left - i]
)

left_intensity = (
x[a0, center_index_left - i] + x[a0, center_index_left - i + 1]
) * 0.5

x[a0, center_index_right + i] = min(
right_intensity, x[a0, center_index_right + i]
)
right_intensity = (
x[a0, center_index_right + i] + x[a0, center_index_right + i - 1]
) * 0.5


@nb.njit
def center_envelope_1d(x: np.ndarray):
"""
Applies an interference correction envelope to a collection of 1D arrays.
Numba function which operates in place.

Parameters
----------
x: np.ndarray
Array of shape (a, b) where a is the number of arrays and b is the length of each array.
It is mandatory that dimension b is odd.

"""

is_even = x.shape[1] % 2 == 0

if is_even:
_even_center_envelope(x)
else:
_odd_center_envelope(x)


@nb.njit
def weighted_mean_a1(array, weight_mask):
"""
Expand Down
11 changes: 6 additions & 5 deletions alphadia/plexscoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1889,11 +1889,12 @@ def __call__(
n_candidates = score_group_container.get_candidate_count()
psm_proto_df = OuptutPsmDF(n_candidates, self.config.top_k_fragments)

# if debug mode, only iterate over 10 elution groups
iterator_len = (
min(10, len(score_group_container)) if debug else len(score_group_container)
)
thread_count = 1 if debug else thread_count
iterator_len = len(score_group_container)

if debug:
GeorgWa marked this conversation as resolved.
Show resolved Hide resolved
logger.info("Debug mode enabled. Processing only the first 10 score groups")
thread_count = 1
iterator_len = min(10, iterator_len)

alphatims.utils.set_threads(thread_count)
_executor(
Expand Down
5 changes: 4 additions & 1 deletion tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ def pytest_configure(config):
]
pytest.test_data[raw_folder] = raw_files

# important to supress matplotlib output
# set numba environment variables
os.environ["NUMBA_BOUNDSCHECK"] = "1"
os.environ["NUMBA_DEVELOPER_MODE"] = "1"
os.environ["NUMBA_FULL_TRACEBACKS"] = "1"


def random_tempfolder():
Expand Down
79 changes: 79 additions & 0 deletions tests/unit_tests/test_features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import numpy as np
import pytest

from alphadia.features import center_envelope_1d


@pytest.mark.parametrize(
"input_array, expected_output",
[
(
np.array([[1, 1, 1, 1, 1, 1, 1]], dtype=np.float64),
np.array(
[
[1, 1, 1, 1, 1, 1, 1],
],
dtype=np.float64,
),
),
(
np.array([[100, 10, 1, 1, 1, 10, 100]], dtype=np.float64),
np.array([[1, 1, 1, 1, 1, 1, 1]], dtype=np.float64),
),
(
np.array([[100, 0, 0, 1, 0, 0, 100]], dtype=np.float64),
np.array([[0, 0, 0, 1, 0, 0, 0]], dtype=np.float64),
),
(
np.array([[1, 1, 1, 1, 1, 1, 1, 1]], dtype=np.float64),
np.array(
[
[1, 1, 1, 1, 1, 1, 1, 1],
],
dtype=np.float64,
),
),
(
np.array([[100, 10, 1, 1, 1, 1, 10, 100]], dtype=np.float64),
np.array([[1, 1, 1, 1, 1, 1, 1, 1]], dtype=np.float64),
),
(
np.array([[100, 0, 0, 1, 1, 0, 0, 100]], dtype=np.float64),
np.array([[0, 0, 0, 1, 1, 0, 0, 0]], dtype=np.float64),
),
],
)
def test_center_envelope_1d_simple(input_array, expected_output):
# given

# when
center_envelope_1d(input_array)

# then
np.testing.assert_array_almost_equal(input_array, expected_output)


def test_center_envelope_1d_multiple_rows():
# given
shape = (10, 11)

input_array = np.random.rand(*shape)
output_array = input_array.copy()
# when
center_envelope_1d(input_array)

# then
assert output_array.shape == input_array.shape
assert np.all(input_array[:, 0] <= output_array[:, 0])
assert np.all(input_array[:, -1] <= output_array[:, -1])


def test_center_envelope_1d_empty_array():
# given
x = np.array([[]], dtype=np.float64)

# when
center_envelope_1d(x)

# then
assert x.shape == (1, 0)
Loading