From 1492fd6e028df690efab25c5be1d248ce2143ad6 Mon Sep 17 00:00:00 2001
From: Arin Wongprommoon <arin.wongprommoon@ed.ac.uk>
Date: Wed, 11 Jan 2023 17:24:56 +0000
Subject: [PATCH] test(postproc): whether interpolate fills in existing values

WHY IS THIS CHANGE NEEDED?:
- need a simple test to see if gitlab visualises coverage

HOW DOES THE CHANGE SOLVE THE PROBLEM?:
- write a test for interpolate post-process; this test checks that
  interpolated values are as expected by leveraging a dummy signal that
  is a gradient.

REFERENCES:
- issue #46
- merge request !8
---
 tests/postprocessor/test_interpolate.py | 46 +++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
 create mode 100644 tests/postprocessor/test_interpolate.py

diff --git a/tests/postprocessor/test_interpolate.py b/tests/postprocessor/test_interpolate.py
new file mode 100644
index 00000000..c9c993de
--- /dev/null
+++ b/tests/postprocessor/test_interpolate.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+import numpy as np
+import pandas as pd
+from postprocessor.core.processes.interpolate import (
+    interpolate,
+    interpolateParameters,
+)
+
+
+def dummy_signal_array(n_cells, n_tps):
+    """Creates dummy signal array, i.e. increasing gradient"""
+    signal = np.array([np.linspace(1, 2, n_tps) for _ in range(n_cells)])
+    return signal
+
+
+def test_dummy_signal_array():
+    ds = dummy_signal_array(5, 10)
+    # Check dimensions
+    assert ds.shape[0] == 5
+    assert ds.shape[1] == 10
+
+
+def randomly_add_na(input_array, num_of_na):
+    """Randomly replaces a 2d numpy array with NaNs, number of NaNs specified"""
+    input_array.ravel()[
+        np.random.choice(input_array.size, num_of_na, replace=False)
+    ] = np.nan
+    return input_array
+
+
+def test_interpolate():
+    dummy_array = dummy_signal_array(5, 10)
+    # Poke holes so interpolate can fill
+    holey_array = randomly_add_na(dummy_array, 15)
+
+    dummy_signal = pd.DataFrame(dummy_array)
+    holey_signal = pd.DataFrame(holey_array)
+
+    interpolate_runner = interpolate(interpolateParameters.default())
+    interpolated_signal = interpolate_runner.run(holey_signal)
+
+    subtr = interpolated_signal - dummy_signal
+    # Check that interpolated values are the ones that exist in the dummy
+    assert np.nansum(subtr.to_numpy()) == 0
+    # TODO: Check that if there are NaNs remaining after interpolation, they
+    # are at the ends
-- 
GitLab