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

add array_interpolate_linear process #44

Merged
merged 4 commits into from
Mar 9, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function array_interpolate_linear(arguments) {
const { data } = arguments;

if (data === null || data === undefined) {
throw new Error(
"Mandatory argument `data` is either null or not defined."
);
}

if (!Array.isArray(data)) {
throw new Error("Argument `data` is not an array.");
}

const linear_interpolation = (v0, v1, t) => (1 - t) * v0 + t * v1;
let newData = [...data];
let start = newData[0];
let end = newData[0];
let nullIndices = [];

for (let i = 0; i < newData.length; i++) {
if (newData[i] === null) {
if (i === 0 || i === newData.length - 1) {
continue;
}

nullIndices.push(i);
continue;
}

if (typeof newData[i] !== "number") {
throw new Error("Element in `data` is not of correct type.");
}

end = newData[i];

if (start !== null && end !== null) {
for (let j = 0; j < nullIndices.length; j++) {
const t = (j + 1) / (nullIndices.length + 1);

newData[nullIndices[j]] = linear_interpolation(start, end, t);
}
}

start = newData[i];
nullIndices = [];
}

return newData;
}
70 changes: 70 additions & 0 deletions tests/unit_tests/test_array_interpolate_linear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import json

import pytest

from tests.utils import load_process_code, run_process


@pytest.fixture
def array_interpolate_linear_process_code():
return load_process_code("array_interpolate_linear")


@pytest.mark.parametrize(
"example_input,expected_output",
[
({"data": [1, 2, 3]}, [1, 2, 3]),
({"data": [1, None, None, 4]}, [1, 2, 3, 4]),
({"data": [None, 1, None, 6, None, -8, None]}, [None, 1, 3.5, 6, -1, -8, None]),
({"data": [None, None, None]}, [None, None, None]),
({"data": [1, None, None]}, [1, None, None]),
({"data": [None, None, 1]}, [None, None, 1]),
({"data": [None, 1, None]}, [None, 1, None]),
({"data": []}, []),
],
)
def test_array_interpolate_linear(
array_interpolate_linear_process_code, example_input, expected_output
):
output = run_process(
array_interpolate_linear_process_code, "array_interpolate_linear", example_input
)
output = json.loads(output)
assert output == expected_output


@pytest.mark.parametrize(
"example_input,raises_exception,error_message",
[
({"data": [1, None, 3]}, False, None),
(
{"data": None},
True,
"Mandatory argument `data` is either null or not defined.",
),
({}, True, "Mandatory argument `data` is either null or not defined."),
({"data": "[1]"}, True, "Argument `data` is not an array."),
({"data": [1, 2, "3"]}, True, "Element in `data` is not of correct type."),
],
)
def test_array_interpolate_linear_exceptions(
array_interpolate_linear_process_code,
example_input,
raises_exception,
error_message,
):
if raises_exception:
with pytest.raises(Exception) as exc:
run_process(
array_interpolate_linear_process_code,
"array_interpolate_linear",
example_input,
)
assert error_message in str(exc.value)

else:
run_process(
array_interpolate_linear_process_code,
"array_interpolate_linear",
example_input,
)