zhongrj
2025-11-25 b89962006164a462404b79a738bee8cbb6d7fe7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import re
from django.test import TestCase
from app.api.formulas import lookup_formula, get_algorithm_list, get_camera_filters_for, algos, get_auto_bands
 
class TestFormulas(TestCase):
    def setUp(self):
        pass
 
    def tearDown(self):
        pass
 
    def test_formulas(self):
        # Original
        self.assertTrue(lookup_formula("_TESTRB", "RGB")[0] == "b1+b3")
        self.assertTrue(lookup_formula("_TESTRB", "RGB")[1] == (0, 1))
 
        # Swap bands
        self.assertTrue(lookup_formula("_TESTRB", "BGR")[0] == "b3+b1")
        self.assertTrue(lookup_formula("_TESTRB", "NRB")[0] == "b2+b3")
 
        # Not enough info
        self.assertRaises(ValueError, lookup_formula, "_TESTRB", "NRG")
 
        # Functions work
        self.assertTrue(lookup_formula("_TESTFUNC", "RGB")[0] == "b1+(sqrt(b3))")
        self.assertTrue(lookup_formula("_TESTFUNC", "RGB")[1] == None)
 
    def test_algo_list(self):
        al = get_algorithm_list()
 
        pattern = re.compile("([A-Z]+?[a-z]*)")
        for i in al:
            # Do not show test algos
            self.assertFalse(i['id'].startswith("_"))
 
            # Filters are less than 3 bands
            for f in i['filters']:
                bands = list(set(re.findall(pattern, f)))
                self.assertTrue(len(bands) <= 3)
 
        self.assertTrue(get_camera_filters_for(algos['VARI']['expr']) == ['RGB'])
 
        # 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)))
                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])