diff --git a/.gitignore b/.gitignore index 6355091..08d9a17 100644 --- a/.gitignore +++ b/.gitignore @@ -137,3 +137,6 @@ docs/notebooks/ # ignore the generated sphinx conf.py file # we generate it during the doc build using jupyter-book from _conf.py docs/conf.py + +# ignore any example/notebook output +notebooks/Implementations/output diff --git a/src/GOSTurban/LEI.py b/src/GOSTurban/LEI.py index fe54e7c..aa84b39 100755 --- a/src/GOSTurban/LEI.py +++ b/src/GOSTurban/LEI.py @@ -217,6 +217,33 @@ def calculate_LEI(inputGHSL, old_list, new_list, buffer_dist=300, transform=""): return allVals +def calculate_LEI_class(val, leap_val, exp_val): + """ + Calculate the LEI class based on the input value + + Parameters + ---------- + val : float + LEI value + leap_val : float + LEI value below which areas are considered to be leapfrog + exp_val : float + LEI value above which areas are considered to be infill + + Returns + ------- + string + LEI class + + """ + if val <= leap_val: + return "Leapfrog" + elif val < exp_val: + return "Expansion" + else: + return "Infill" + + def summarize_LEI(in_file, leap_val=0.05, exp_val=0.9): """ Summarize the LEI results produced by self.calculate_LEI @@ -244,32 +271,6 @@ def summarize_LEI(in_file, leap_val=0.05, exp_val=0.9): if "area" not in res.columns: res["area"] = res["geometry"].apply(lambda x: x.area) - def calculate_LEI_class(val, leap_val, exp_val): - """ - Calculate the LEI class based on the input value - - Parameters - ---------- - val : float - LEI value - leap_val : float - LEI value below which areas are considered to be leapfrog - exp_val : float - LEI value above which areas are considered to be infill - - Returns - ------- - string - LEI class - - """ - if val <= leap_val: - return "Leapfrog" - elif val < exp_val: - return "Expansion" - else: - return "Infill" - res["class"] = res["LEI"].apply(lambda x: calculate_LEI_class(x, leap_val, exp_val)) xx = res.groupby("class") diff --git a/tests/test_LEI.py b/tests/test_LEI.py index 278d341..5d752a5 100644 --- a/tests/test_LEI.py +++ b/tests/test_LEI.py @@ -4,6 +4,28 @@ import pandas as pd from shapely.geometry import Polygon from unittest.mock import MagicMock +import numpy as np + + +class TestCalculateLEI: + """Tests for the calculate_LEI() function.""" + + # make some fake data to test with + raster = np.zeros((10, 10)) + raster[:5, :5] = 4 + raster[5:, 5:] = 3 + + def test_calculate_lei(self): + # run the function + result = LEI.calculate_LEI( + self.raster, + old_list=[4], + new_list=[3], + buffer_dist=1, + transform=(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0), + ) + # assert things about the result + assert isinstance(result, list) class TestSummarizeLEI: @@ -55,3 +77,19 @@ def test_mp_lei(self): # assert things about the result assert isinstance(result, pd.Series) assert result.name == "area" + + +class TestCalculateLEIClass: + """Tests for the calculate_LEI_class() function.""" + + def test_calculate_lei_class_01(self): + val = LEI.calculate_LEI_class(0.1, 1.0, 2.0) + assert val == "Leapfrog" + + def test_calculate_lei_class_02(self): + val = LEI.calculate_LEI_class(1.5, 1.0, 2.0) + assert val == "Expansion" + + def test_calculate_lei_class_03(self): + val = LEI.calculate_LEI_class(2.5, 1.0, 2.0) + assert val == "Infill"