-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedCalcFunctions.py
49 lines (43 loc) · 1.91 KB
/
sharedCalcFunctions.py
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
import math
import numpy as np
# Basic Functions used by both WoE and FR.
def getLandslideTotalCount(lsArray: np.ndarray, noData: int, nols: int) -> int:
"""Returns the total amount of landslidepixels.
Gets called by init
Expects everything besides nols and noData to be a landslide pixel.
"""
return ((lsArray != noData) & (lsArray != nols)).sum()
def getTotalCount(rasterArray, noData: int) -> int:
"""Returns the total amount of pixel in raster excluding noData.
Gets called by init.
"""
return np.count_nonzero(rasterArray != noData)
def getClassValues(rasterArray: np.ndarray, noData) -> np.ndarray:
"""Returns a numpy array with unique values in rasterArray, discarding noData"""
classValues = np.unique(rasterArray)
# we dont care about noData
if noData in classValues:
classValues = classValues[classValues != noData]
return classValues
def getClassArrayList(rasterArray, classValues: list, noData=-9999) -> list:
"""
Returns a list with n arrays for n unique values in rasterArray.
The return arrays will have 1 "trueValue" where the class is present, 0 "falseValue" elsewhere and
-9999 for noData.
"""
classArrayList = []
for classValue in classValues:
classArray = rasterArray.copy()
conditions = [classArray == classValue, classArray == noData]
choices = [1, -9999]
classArray = np.select(conditions, choices, 0)
classArrayList.append(classArray)
return classArrayList
def getClassCount(classArray, trueValue=1) -> int:
"""Returns the amount of pixels in a classArray that are inside the class.
Gets called by init.
"""
return np.count_nonzero(classArray == trueValue)
def getLandslideClassCount(lsArray, classArray, trueValue=1) -> int:
"""Returns the amount of pixels with landslide inside the class."""
return (np.logical_and(lsArray == 1, classArray == trueValue)).sum()