Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elbeejay committed Apr 4, 2024
1 parent 1b095bb commit 0c61c4c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 27 additions & 26 deletions src/GOSTurban/LEI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
38 changes: 38 additions & 0 deletions tests/test_LEI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"

0 comments on commit 0c61c4c

Please sign in to comment.