added type hinting for a few functions,

added typedef module to hold custom typings

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2021-11-16 20:17:46 +00:00
parent 5d5d6c4c5e
commit 33c462570d
3 changed files with 34 additions and 7 deletions

View File

@ -380,6 +380,7 @@ import numpy as np
import scipy import scipy
import sklearn, sklearn.cluster import sklearn, sklearn.cluster
from tra_analysis.metrics import trueskill as Trueskill from tra_analysis.metrics import trueskill as Trueskill
from tra_analysis.typedef import R, List, Dict
# import submodules # import submodules
@ -388,15 +389,27 @@ from .ClassificationMetric import ClassificationMetric
class error(ValueError): class error(ValueError):
pass pass
def load_csv(filepath): def load_csv(filepath: str) -> np.ndarray:
"""
Loads csv file into 2D numpy array. Does not check csv file validity.
parameters:
filepath: String path to the csv file
return:
2D numpy array of values stored in csv file
"""
with open(filepath, newline='') as csvfile: with open(filepath, newline='') as csvfile:
file_array = np.array(list(csv.reader(csvfile))) file_array = np.array(list(csv.reader(csvfile)))
csvfile.close() csvfile.close()
return file_array return file_array
# expects 1d array def basic_stats(data: List[R]) -> Dict[str, R]:
def basic_stats(data): """
Calculates mean, median, standard deviation, variance, minimum, maximum of a simple set of elements
parameters:
data: List representing set of unordered elements
return:
Dictionary with (mean, median, standard-deviation, variance, minimum, maximum) as keys and corresponding values
"""
data_t = np.array(data).astype(float) data_t = np.array(data).astype(float)
_mean = mean(data_t) _mean = mean(data_t)
@ -408,8 +421,16 @@ def basic_stats(data):
return {"mean": _mean, "median": _median, "standard-deviation": _stdev, "variance": _variance, "minimum": _min, "maximum": _max} return {"mean": _mean, "median": _median, "standard-deviation": _stdev, "variance": _variance, "minimum": _min, "maximum": _max}
# returns z score with inputs of point, mean and standard deviation of spread def z_score(point: R, mean: R, stdev: R) -> R:
def z_score(point, mean, stdev): """
Calculates z score of a specific point given mean and standard deviation of data
parameters:
point: Real value corresponding to a single point of data
mean: Real value corresponding to the mean of the dataset
stdev: Real value corresponding to the standard deviation of the dataset
return:
Real value that is the point's z score
"""
score = (point - mean) / stdev score = (point - mean) / stdev
return score return score

View File

@ -74,3 +74,5 @@ from .RegressionMetric import RegressionMetric
from . import Sort from . import Sort
from . import StatisticalTest from . import StatisticalTest
from . import SVM from . import SVM
from . import typedef

View File

@ -0,0 +1,4 @@
from typing import TypeVar, List, Dict
List = List
Dict = Dict
R = TypeVar('R', int, float)