2019-12-20 20:02:34 +00:00
|
|
|
import re
|
2019-11-19 17:51:17 +00:00
|
|
|
from django.test import TestCase
|
2023-10-04 17:04:39 +00:00
|
|
|
from app.api.formulas import lookup_formula, get_algorithm_list, get_camera_filters_for, algos, get_auto_bands
|
2019-11-19 17:51:17 +00:00
|
|
|
|
|
|
|
class TestFormulas(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_formulas(self):
|
|
|
|
# Original
|
2019-11-26 21:29:11 +00:00
|
|
|
self.assertTrue(lookup_formula("_TESTRB", "RGB")[0] == "b1+b3")
|
|
|
|
self.assertTrue(lookup_formula("_TESTRB", "RGB")[1] == (0, 1))
|
2019-11-19 17:51:17 +00:00
|
|
|
|
|
|
|
# Swap bands
|
2019-11-26 21:29:11 +00:00
|
|
|
self.assertTrue(lookup_formula("_TESTRB", "BGR")[0] == "b3+b1")
|
|
|
|
self.assertTrue(lookup_formula("_TESTRB", "NRB")[0] == "b2+b3")
|
2019-11-19 17:51:17 +00:00
|
|
|
|
2019-11-19 18:35:59 +00:00
|
|
|
# Not enough info
|
|
|
|
self.assertRaises(ValueError, lookup_formula, "_TESTRB", "NRG")
|
|
|
|
|
|
|
|
# Functions work
|
2019-11-26 21:29:11 +00:00
|
|
|
self.assertTrue(lookup_formula("_TESTFUNC", "RGB")[0] == "b1+(sqrt(b3))")
|
|
|
|
self.assertTrue(lookup_formula("_TESTFUNC", "RGB")[1] == None)
|
2019-11-19 18:35:59 +00:00
|
|
|
|
2019-11-19 21:27:42 +00:00
|
|
|
def test_algo_list(self):
|
2019-12-20 20:02:34 +00:00
|
|
|
al = get_algorithm_list()
|
2019-11-19 21:27:42 +00:00
|
|
|
|
2019-12-20 20:02:34 +00:00
|
|
|
pattern = re.compile("([A-Z]+?[a-z]*)")
|
|
|
|
for i in al:
|
|
|
|
# Do not show test algos
|
2019-11-26 21:29:11 +00:00
|
|
|
self.assertFalse(i['id'].startswith("_"))
|
2019-11-19 21:27:42 +00:00
|
|
|
|
2019-12-20 20:02:34 +00:00
|
|
|
# Filters are less than 3 bands
|
|
|
|
for f in i['filters']:
|
|
|
|
bands = list(set(re.findall(pattern, f)))
|
|
|
|
self.assertTrue(len(bands) <= 3)
|
|
|
|
|
2023-10-03 19:13:56 +00:00
|
|
|
self.assertTrue(get_camera_filters_for(algos['VARI']['expr']) == ['RGB'])
|
2019-12-20 20:02:34 +00:00
|
|
|
|
|
|
|
# Request algorithms with more band filters
|
|
|
|
al = get_algorithm_list(max_bands=5)
|
|
|
|
|
|
|
|
pattern = re.compile("([A-Z]+?[a-z]*)")
|
|
|
|
for i in al:
|
|
|
|
# Filters are less than 5 bands
|
|
|
|
for f in i['filters']:
|
|
|
|
bands = list(set(re.findall(pattern, f)))
|
2023-10-04 17:04:39 +00:00
|
|
|
self.assertTrue(len(bands) <= 5)
|
|
|
|
|
|
|
|
def test_auto_bands(self):
|
|
|
|
obands = [{'name': 'red', 'description': 'red'},
|
|
|
|
{'name': 'green', 'description': 'green'},
|
|
|
|
{'name': 'blue', 'description': 'blue'},
|
|
|
|
{'name': 'gray', 'description': 'nir'},
|
|
|
|
{'name': 'alpha', 'description': None}]
|
|
|
|
|
|
|
|
self.assertEqual(get_auto_bands(obands, "NDVI")[0], "RGBN")
|
|
|
|
self.assertTrue(get_auto_bands(obands, "NDVI")[1])
|
|
|
|
|
|
|
|
self.assertEqual(get_auto_bands(obands, "Celsius")[0], "L")
|
|
|
|
self.assertFalse(get_auto_bands(obands, "Celsius")[1])
|
|
|
|
|
|
|
|
self.assertEqual(get_auto_bands(obands, "VARI")[0], "RGBN")
|
|
|
|
self.assertTrue(get_auto_bands(obands, "VARI")[0])
|
|
|
|
|
|
|
|
obands = [{'name': 'red', 'description': None},
|
|
|
|
{'name': 'green', 'description': None},
|
|
|
|
{'name': 'blue', 'description': None},
|
|
|
|
{'name': 'gray', 'description': None},
|
|
|
|
{'name': 'alpha', 'description': None}]
|
|
|
|
|
|
|
|
self.assertEqual(get_auto_bands(obands, "NDVI")[0], "RGN")
|
|
|
|
self.assertFalse(get_auto_bands(obands, "NDVI")[1])
|
|
|
|
|
|
|
|
self.assertEqual(get_auto_bands(obands, "VARI")[0], "RGB")
|
|
|
|
self.assertFalse(get_auto_bands(obands, "VARI")[1])
|