From 33c462570d4611b08eb1518539114c9909b373f1 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Tue, 16 Nov 2021 20:17:46 +0000 Subject: [PATCH] added type hinting for a few functions, added typedef module to hold custom typings Signed-off-by: Arthur Lu --- analysis-master/tra_analysis/Analysis.py | 33 +++++++++++++++++++----- analysis-master/tra_analysis/__init__.py | 4 ++- analysis-master/tra_analysis/typedef.py | 4 +++ 3 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 analysis-master/tra_analysis/typedef.py diff --git a/analysis-master/tra_analysis/Analysis.py b/analysis-master/tra_analysis/Analysis.py index e736ec62..b9f4443b 100644 --- a/analysis-master/tra_analysis/Analysis.py +++ b/analysis-master/tra_analysis/Analysis.py @@ -380,6 +380,7 @@ import numpy as np import scipy import sklearn, sklearn.cluster from tra_analysis.metrics import trueskill as Trueskill +from tra_analysis.typedef import R, List, Dict # import submodules @@ -388,15 +389,27 @@ from .ClassificationMetric import ClassificationMetric class error(ValueError): 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: file_array = np.array(list(csv.reader(csvfile))) csvfile.close() return file_array -# expects 1d array -def basic_stats(data): - +def basic_stats(data: List[R]) -> Dict[str, R]: + """ + 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) _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} -# returns z score with inputs of point, mean and standard deviation of spread -def z_score(point, mean, stdev): +def z_score(point: R, mean: R, stdev: R) -> R: + """ + 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 return score diff --git a/analysis-master/tra_analysis/__init__.py b/analysis-master/tra_analysis/__init__.py index d74c1c9d..2df41075 100644 --- a/analysis-master/tra_analysis/__init__.py +++ b/analysis-master/tra_analysis/__init__.py @@ -73,4 +73,6 @@ from . import RandomForest from .RegressionMetric import RegressionMetric from . import Sort from . import StatisticalTest -from . import SVM \ No newline at end of file +from . import SVM + +from . import typedef \ No newline at end of file diff --git a/analysis-master/tra_analysis/typedef.py b/analysis-master/tra_analysis/typedef.py new file mode 100644 index 00000000..875776a0 --- /dev/null +++ b/analysis-master/tra_analysis/typedef.py @@ -0,0 +1,4 @@ +from typing import TypeVar, List, Dict +List = List +Dict = Dict +R = TypeVar('R', int, float) \ No newline at end of file