mirror of
https://github.com/titanscouting/tra-analysis.git
synced 2025-09-07 15:27:21 +00:00
Compare commits
35 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
50c064ffa4 | ||
|
1b0a9967c8 | ||
|
2605f7c29f | ||
|
6f5a3edd88 | ||
|
457146b0e4 | ||
|
f7fd8ffcf9 | ||
|
77bc792426 | ||
|
39146cc555 | ||
|
04141bbec8 | ||
|
40e5899972 | ||
|
025c7f9b3c | ||
|
2daa09c040 | ||
|
9776136649 | ||
|
68d27a6302 | ||
|
7fc18b7c35 | ||
|
9b412b51a8 | ||
|
b6ac05a66e | ||
|
435c8a7bc6 | ||
|
a69b18354b | ||
|
7b9e6921d0 | ||
|
fb2800cf9e | ||
|
12cbb21077 | ||
|
46d1a48999 | ||
|
ad0a761d53 | ||
|
43f503a38d | ||
|
d38744438b | ||
|
eb8914aa26 | ||
|
283140094f | ||
|
66ac1c304e | ||
|
0eb9e07711 | ||
|
f56c85b298 | ||
|
6a9a17c5b4 | ||
|
e24c49bedb | ||
|
2daed73aaa | ||
|
8ebdb3b89b |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -17,3 +17,6 @@ data analysis/.ipynb_checkpoints/test-checkpoint.ipynb
|
|||||||
data analysis/arthur_pull.ipynb
|
data analysis/arthur_pull.ipynb
|
||||||
data analysis/keys.txt
|
data analysis/keys.txt
|
||||||
data analysis/check_for_new_matches.ipynb
|
data analysis/check_for_new_matches.ipynb
|
||||||
|
data analysis/test.ipynb
|
||||||
|
data analysis/visualize_pit.ipynb
|
||||||
|
data analysis/config/keys.config
|
@@ -1,6 +1,6 @@
|
|||||||
Metadata-Version: 2.1
|
Metadata-Version: 2.1
|
||||||
Name: analysis
|
Name: analysis
|
||||||
Version: 1.0.0.6
|
Version: 1.0.0.8
|
||||||
Summary: analysis package developed by Titan Scouting for The Red Alliance
|
Summary: analysis package developed by Titan Scouting for The Red Alliance
|
||||||
Home-page: https://github.com/titanscout2022/tr2022-strategy
|
Home-page: https://github.com/titanscout2022/tr2022-strategy
|
||||||
Author: The Titan Scouting Team
|
Author: The Titan Scouting Team
|
||||||
|
@@ -8,4 +8,5 @@ analysis/visualization.py
|
|||||||
analysis.egg-info/PKG-INFO
|
analysis.egg-info/PKG-INFO
|
||||||
analysis.egg-info/SOURCES.txt
|
analysis.egg-info/SOURCES.txt
|
||||||
analysis.egg-info/dependency_links.txt
|
analysis.egg-info/dependency_links.txt
|
||||||
|
analysis.egg-info/requires.txt
|
||||||
analysis.egg-info/top_level.txt
|
analysis.egg-info/top_level.txt
|
6
analysis-master/analysis.egg-info/requires.txt
Normal file
6
analysis-master/analysis.egg-info/requires.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
numba
|
||||||
|
numpy
|
||||||
|
scipy
|
||||||
|
scikit-learn
|
||||||
|
six
|
||||||
|
matplotlib
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -7,10 +7,20 @@
|
|||||||
# current benchmark of optimization: 1.33 times faster
|
# current benchmark of optimization: 1.33 times faster
|
||||||
# setup:
|
# setup:
|
||||||
|
|
||||||
__version__ = "1.1.13.001"
|
__version__ = "1.1.13.006"
|
||||||
|
|
||||||
# changelog should be viewed using print(analysis.__changelog__)
|
# changelog should be viewed using print(analysis.__changelog__)
|
||||||
__changelog__ = """changelog:
|
__changelog__ = """changelog:
|
||||||
|
1.1.13.006:
|
||||||
|
- cleaned up imports
|
||||||
|
1.1.13.005:
|
||||||
|
- cleaned up package
|
||||||
|
1.1.13.004:
|
||||||
|
- small fixes to regression to improve performance
|
||||||
|
1.1.13.003:
|
||||||
|
- filtered nans from regression
|
||||||
|
1.1.13.002:
|
||||||
|
- removed torch requirement, and moved Regression back to regression.py
|
||||||
1.1.13.001:
|
1.1.13.001:
|
||||||
- bug fix with linear regression not returning a proper value
|
- bug fix with linear regression not returning a proper value
|
||||||
- cleaned up regression
|
- cleaned up regression
|
||||||
@@ -239,7 +249,6 @@ __author__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'_init_device',
|
|
||||||
'load_csv',
|
'load_csv',
|
||||||
'basic_stats',
|
'basic_stats',
|
||||||
'z_score',
|
'z_score',
|
||||||
@@ -260,7 +269,6 @@ __all__ = [
|
|||||||
'SVM',
|
'SVM',
|
||||||
'random_forest_classifier',
|
'random_forest_classifier',
|
||||||
'random_forest_regressor',
|
'random_forest_regressor',
|
||||||
'Regression',
|
|
||||||
'Glicko2',
|
'Glicko2',
|
||||||
# all statistics functions left out due to integration in other functions
|
# all statistics functions left out due to integration in other functions
|
||||||
]
|
]
|
||||||
@@ -273,24 +281,15 @@ import csv
|
|||||||
import numba
|
import numba
|
||||||
from numba import jit
|
from numba import jit
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import math
|
|
||||||
import scipy
|
import scipy
|
||||||
from scipy import *
|
from scipy import *
|
||||||
import sklearn
|
import sklearn
|
||||||
from sklearn import *
|
from sklearn import *
|
||||||
import torch
|
from analysis import trueskill as Trueskill
|
||||||
try:
|
|
||||||
from analysis import trueskill as Trueskill
|
|
||||||
except:
|
|
||||||
import trueskill as Trueskill
|
|
||||||
|
|
||||||
class error(ValueError):
|
class error(ValueError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _init_device(): # initiates computation device for ANNs
|
|
||||||
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
|
||||||
return device
|
|
||||||
|
|
||||||
def load_csv(filepath):
|
def load_csv(filepath):
|
||||||
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)))
|
||||||
@@ -349,15 +348,15 @@ def histo_analysis(hist_data):
|
|||||||
|
|
||||||
def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
||||||
|
|
||||||
|
X = np.array(inputs)
|
||||||
|
y = np.array(outputs)
|
||||||
|
|
||||||
regressions = []
|
regressions = []
|
||||||
|
|
||||||
if 'lin' in args: # formula: ax + b
|
if 'lin' in args: # formula: ax + b
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
X = np.array(inputs)
|
|
||||||
y = np.array(outputs)
|
|
||||||
|
|
||||||
def func(x, a, b):
|
def func(x, a, b):
|
||||||
|
|
||||||
return a * x + b
|
return a * x + b
|
||||||
@@ -374,9 +373,6 @@ def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
X = np.array(inputs)
|
|
||||||
y = np.array(outputs)
|
|
||||||
|
|
||||||
def func(x, a, b, c, d):
|
def func(x, a, b, c, d):
|
||||||
|
|
||||||
return a * np.log(b*(x + c)) + d
|
return a * np.log(b*(x + c)) + d
|
||||||
@@ -393,9 +389,6 @@ def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
X = np.array(inputs)
|
|
||||||
y = np.array(outputs)
|
|
||||||
|
|
||||||
def func(x, a, b, c, d):
|
def func(x, a, b, c, d):
|
||||||
|
|
||||||
return a * np.exp(b*(x + c)) + d
|
return a * np.exp(b*(x + c)) + d
|
||||||
@@ -410,8 +403,8 @@ def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
|||||||
|
|
||||||
if 'ply' in args: # formula: a + bx^1 + cx^2 + dx^3 + ...
|
if 'ply' in args: # formula: a + bx^1 + cx^2 + dx^3 + ...
|
||||||
|
|
||||||
inputs = [inputs]
|
inputs = np.array([inputs])
|
||||||
outputs = [outputs]
|
outputs = np.array([outputs])
|
||||||
|
|
||||||
plys = []
|
plys = []
|
||||||
limit = len(outputs[0])
|
limit = len(outputs[0])
|
||||||
@@ -435,9 +428,6 @@ def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
X = np.array(inputs)
|
|
||||||
y = np.array(outputs)
|
|
||||||
|
|
||||||
def func(x, a, b, c, d):
|
def func(x, a, b, c, d):
|
||||||
|
|
||||||
return a * np.tanh(b*(x + c)) + d
|
return a * np.tanh(b*(x + c)) + d
|
||||||
@@ -700,225 +690,6 @@ def random_forest_regressor(data, outputs, test_size, n_estimators="warn", crite
|
|||||||
|
|
||||||
return kernel, RegressionMetrics(predictions, outputs_test)
|
return kernel, RegressionMetrics(predictions, outputs_test)
|
||||||
|
|
||||||
class Regression:
|
|
||||||
|
|
||||||
# Titan Robotics Team 2022: CUDA-based Regressions Module
|
|
||||||
# Written by Arthur Lu & Jacob Levine
|
|
||||||
# Notes:
|
|
||||||
# this module has been automatically inegrated into analysis.py, and should be callable as a class from the package
|
|
||||||
# this module is cuda-optimized and vectorized (except for one small part)
|
|
||||||
# setup:
|
|
||||||
|
|
||||||
__version__ = "1.0.0.003"
|
|
||||||
|
|
||||||
# changelog should be viewed using print(analysis.regression.__changelog__)
|
|
||||||
__changelog__ = """
|
|
||||||
1.0.0.003:
|
|
||||||
- bug fixes
|
|
||||||
1.0.0.002:
|
|
||||||
-Added more parameters to log, exponential, polynomial
|
|
||||||
-Added SigmoidalRegKernelArthur, because Arthur apparently needs
|
|
||||||
to train the scaling and shifting of sigmoids
|
|
||||||
|
|
||||||
1.0.0.001:
|
|
||||||
-initial release, with linear, log, exponential, polynomial, and sigmoid kernels
|
|
||||||
-already vectorized (except for polynomial generation) and CUDA-optimized
|
|
||||||
"""
|
|
||||||
|
|
||||||
__author__ = (
|
|
||||||
"Jacob Levine <jlevine@imsa.edu>",
|
|
||||||
"Arthur Lu <learthurgo@gmail.com>"
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'factorial',
|
|
||||||
'take_all_pwrs',
|
|
||||||
'num_poly_terms',
|
|
||||||
'set_device',
|
|
||||||
'LinearRegKernel',
|
|
||||||
'SigmoidalRegKernel',
|
|
||||||
'LogRegKernel',
|
|
||||||
'PolyRegKernel',
|
|
||||||
'ExpRegKernel',
|
|
||||||
'SigmoidalRegKernelArthur',
|
|
||||||
'SGDTrain',
|
|
||||||
'CustomTrain'
|
|
||||||
]
|
|
||||||
|
|
||||||
global device
|
|
||||||
|
|
||||||
device = "cuda:0" if torch.torch.cuda.is_available() else "cpu"
|
|
||||||
|
|
||||||
#todo: document completely
|
|
||||||
|
|
||||||
def set_device(self, new_device):
|
|
||||||
device=new_device
|
|
||||||
|
|
||||||
class LinearRegKernel():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
bias=None
|
|
||||||
def __init__(self, num_vars):
|
|
||||||
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
|
|
||||||
self.bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.bias]
|
|
||||||
def forward(self,mtx):
|
|
||||||
long_bias=self.bias.repeat([1,mtx.size()[1]])
|
|
||||||
return torch.matmul(self.weights,mtx)+long_bias
|
|
||||||
|
|
||||||
class SigmoidalRegKernel():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
bias=None
|
|
||||||
sigmoid=torch.nn.Sigmoid()
|
|
||||||
def __init__(self, num_vars):
|
|
||||||
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
|
|
||||||
self.bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.bias]
|
|
||||||
def forward(self,mtx):
|
|
||||||
long_bias=self.bias.repeat([1,mtx.size()[1]])
|
|
||||||
return self.sigmoid(torch.matmul(self.weights,mtx)+long_bias)
|
|
||||||
|
|
||||||
class SigmoidalRegKernelArthur():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
in_bias=None
|
|
||||||
scal_mult=None
|
|
||||||
out_bias=None
|
|
||||||
sigmoid=torch.nn.Sigmoid()
|
|
||||||
def __init__(self, num_vars):
|
|
||||||
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
|
|
||||||
self.in_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.scal_mult=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.out_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.in_bias, self.scal_mult, self.out_bias]
|
|
||||||
def forward(self,mtx):
|
|
||||||
long_in_bias=self.in_bias.repeat([1,mtx.size()[1]])
|
|
||||||
long_out_bias=self.out_bias.repeat([1,mtx.size()[1]])
|
|
||||||
return (self.scal_mult*self.sigmoid(torch.matmul(self.weights,mtx)+long_in_bias))+long_out_bias
|
|
||||||
|
|
||||||
class LogRegKernel():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
in_bias=None
|
|
||||||
scal_mult=None
|
|
||||||
out_bias=None
|
|
||||||
def __init__(self, num_vars):
|
|
||||||
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
|
|
||||||
self.in_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.scal_mult=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.out_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.in_bias, self.scal_mult, self.out_bias]
|
|
||||||
def forward(self,mtx):
|
|
||||||
long_in_bias=self.in_bias.repeat([1,mtx.size()[1]])
|
|
||||||
long_out_bias=self.out_bias.repeat([1,mtx.size()[1]])
|
|
||||||
return (self.scal_mult*torch.log(torch.matmul(self.weights,mtx)+long_in_bias))+long_out_bias
|
|
||||||
|
|
||||||
class ExpRegKernel():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
in_bias=None
|
|
||||||
scal_mult=None
|
|
||||||
out_bias=None
|
|
||||||
def __init__(self, num_vars):
|
|
||||||
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
|
|
||||||
self.in_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.scal_mult=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.out_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.in_bias, self.scal_mult, self.out_bias]
|
|
||||||
def forward(self,mtx):
|
|
||||||
long_in_bias=self.in_bias.repeat([1,mtx.size()[1]])
|
|
||||||
long_out_bias=self.out_bias.repeat([1,mtx.size()[1]])
|
|
||||||
return (self.scal_mult*torch.exp(torch.matmul(self.weights,mtx)+long_in_bias))+long_out_bias
|
|
||||||
|
|
||||||
class PolyRegKernel():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
bias=None
|
|
||||||
power=None
|
|
||||||
def __init__(self, num_vars, power):
|
|
||||||
self.power=power
|
|
||||||
num_terms=self.num_poly_terms(num_vars, power)
|
|
||||||
self.weights=torch.rand(num_terms, requires_grad=True, device=device)
|
|
||||||
self.bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.bias]
|
|
||||||
def num_poly_terms(self,num_vars, power):
|
|
||||||
if power == 0:
|
|
||||||
return 0
|
|
||||||
return int(self.factorial(num_vars+power-1) / self.factorial(power) / self.factorial(num_vars-1)) + self.num_poly_terms(num_vars, power-1)
|
|
||||||
def factorial(self,n):
|
|
||||||
if n==0:
|
|
||||||
return 1
|
|
||||||
else:
|
|
||||||
return n*self.factorial(n-1)
|
|
||||||
def take_all_pwrs(self, vec, pwr):
|
|
||||||
#todo: vectorize (kinda)
|
|
||||||
combins=torch.combinations(vec, r=pwr, with_replacement=True)
|
|
||||||
out=torch.ones(combins.size()[0]).to(device).to(torch.float)
|
|
||||||
for i in torch.t(combins).to(device).to(torch.float):
|
|
||||||
out *= i
|
|
||||||
if pwr == 1:
|
|
||||||
return out
|
|
||||||
else:
|
|
||||||
return torch.cat((out,self.take_all_pwrs(vec, pwr-1)))
|
|
||||||
def forward(self,mtx):
|
|
||||||
#TODO: Vectorize the last part
|
|
||||||
cols=[]
|
|
||||||
for i in torch.t(mtx):
|
|
||||||
cols.append(self.take_all_pwrs(i,self.power))
|
|
||||||
new_mtx=torch.t(torch.stack(cols))
|
|
||||||
long_bias=self.bias.repeat([1,mtx.size()[1]])
|
|
||||||
return torch.matmul(self.weights,new_mtx)+long_bias
|
|
||||||
|
|
||||||
def SGDTrain(self, kernel, data, ground, loss=torch.nn.MSELoss(), iterations=1000, learning_rate=.1, return_losses=False):
|
|
||||||
optim=torch.optim.SGD(kernel.parameters, lr=learning_rate)
|
|
||||||
data_cuda=data.to(device)
|
|
||||||
ground_cuda=ground.to(device)
|
|
||||||
if (return_losses):
|
|
||||||
losses=[]
|
|
||||||
for i in range(iterations):
|
|
||||||
with torch.set_grad_enabled(True):
|
|
||||||
optim.zero_grad()
|
|
||||||
pred=kernel.forward(data_cuda)
|
|
||||||
ls=loss(pred,ground_cuda)
|
|
||||||
losses.append(ls.item())
|
|
||||||
ls.backward()
|
|
||||||
optim.step()
|
|
||||||
return [kernel,losses]
|
|
||||||
else:
|
|
||||||
for i in range(iterations):
|
|
||||||
with torch.set_grad_enabled(True):
|
|
||||||
optim.zero_grad()
|
|
||||||
pred=kernel.forward(data_cuda)
|
|
||||||
ls=loss(pred,ground_cuda)
|
|
||||||
ls.backward()
|
|
||||||
optim.step()
|
|
||||||
return kernel
|
|
||||||
|
|
||||||
def CustomTrain(self, kernel, optim, data, ground, loss=torch.nn.MSELoss(), iterations=1000, return_losses=False):
|
|
||||||
data_cuda=data.to(device)
|
|
||||||
ground_cuda=ground.to(device)
|
|
||||||
if (return_losses):
|
|
||||||
losses=[]
|
|
||||||
for i in range(iterations):
|
|
||||||
with torch.set_grad_enabled(True):
|
|
||||||
optim.zero_grad()
|
|
||||||
pred=kernel.forward(data)
|
|
||||||
ls=loss(pred,ground)
|
|
||||||
losses.append(ls.item())
|
|
||||||
ls.backward()
|
|
||||||
optim.step()
|
|
||||||
return [kernel,losses]
|
|
||||||
else:
|
|
||||||
for i in range(iterations):
|
|
||||||
with torch.set_grad_enabled(True):
|
|
||||||
optim.zero_grad()
|
|
||||||
pred=kernel.forward(data_cuda)
|
|
||||||
ls=loss(pred,ground_cuda)
|
|
||||||
ls.backward()
|
|
||||||
optim.step()
|
|
||||||
return kernel
|
|
||||||
|
|
||||||
class Glicko2:
|
class Glicko2:
|
||||||
|
|
||||||
_tau = 0.5
|
_tau = 0.5
|
||||||
|
@@ -1,20 +1,23 @@
|
|||||||
# Titan Robotics Team 2022: CUDA-based Regressions Module
|
# Titan Robotics Team 2022: CUDA-based Regressions Module
|
||||||
# Written by Arthur Lu & Jacob Levine
|
# Written by Arthur Lu & Jacob Levine
|
||||||
# Notes:
|
# Notes:
|
||||||
# this should be imported as a python module using 'import regression'
|
# this module has been automatically inegrated into analysis.py, and should be callable as a class from the package
|
||||||
# this should be included in the local directory or environment variable
|
|
||||||
# this module is cuda-optimized and vectorized (except for one small part)
|
# this module is cuda-optimized and vectorized (except for one small part)
|
||||||
# setup:
|
# setup:
|
||||||
|
|
||||||
__version__ = "1.0.0.002"
|
__version__ = "1.0.0.004"
|
||||||
|
|
||||||
# changelog should be viewed using print(regression.__changelog__)
|
# changelog should be viewed using print(analysis.regression.__changelog__)
|
||||||
__changelog__ = """
|
__changelog__ = """
|
||||||
|
1.0.0.004:
|
||||||
|
- bug fixes
|
||||||
|
- fixed changelog
|
||||||
|
1.0.0.003:
|
||||||
|
- bug fixes
|
||||||
1.0.0.002:
|
1.0.0.002:
|
||||||
-Added more parameters to log, exponential, polynomial
|
-Added more parameters to log, exponential, polynomial
|
||||||
-Added SigmoidalRegKernelArthur, because Arthur apparently needs
|
-Added SigmoidalRegKernelArthur, because Arthur apparently needs
|
||||||
to train the scaling and shifting of sigmoids
|
to train the scaling and shifting of sigmoids
|
||||||
|
|
||||||
1.0.0.001:
|
1.0.0.001:
|
||||||
-initial release, with linear, log, exponential, polynomial, and sigmoid kernels
|
-initial release, with linear, log, exponential, polynomial, and sigmoid kernels
|
||||||
-already vectorized (except for polynomial generation) and CUDA-optimized
|
-already vectorized (except for polynomial generation) and CUDA-optimized
|
||||||
@@ -22,6 +25,7 @@ __changelog__ = """
|
|||||||
|
|
||||||
__author__ = (
|
__author__ = (
|
||||||
"Jacob Levine <jlevine@imsa.edu>",
|
"Jacob Levine <jlevine@imsa.edu>",
|
||||||
|
"Arthur Lu <learthurgo@gmail.com>"
|
||||||
)
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
@@ -39,35 +43,15 @@ __all__ = [
|
|||||||
'CustomTrain'
|
'CustomTrain'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
# imports (just one for now):
|
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
|
global device
|
||||||
|
|
||||||
device = "cuda:0" if torch.torch.cuda.is_available() else "cpu"
|
device = "cuda:0" if torch.torch.cuda.is_available() else "cpu"
|
||||||
|
|
||||||
#todo: document completely
|
#todo: document completely
|
||||||
|
|
||||||
def factorial(n):
|
def set_device(self, new_device):
|
||||||
if n==0:
|
|
||||||
return 1
|
|
||||||
else:
|
|
||||||
return n*factorial(n-1)
|
|
||||||
def num_poly_terms(num_vars, power):
|
|
||||||
if power == 0:
|
|
||||||
return 0
|
|
||||||
return int(factorial(num_vars+power-1) / factorial(power) / factorial(num_vars-1)) + num_poly_terms(num_vars, power-1)
|
|
||||||
|
|
||||||
def take_all_pwrs(vec,pwr):
|
|
||||||
#todo: vectorize (kinda)
|
|
||||||
combins=torch.combinations(vec, r=pwr, with_replacement=True)
|
|
||||||
out=torch.ones(combins.size()[0])
|
|
||||||
for i in torch.t(combins):
|
|
||||||
out *= i
|
|
||||||
return torch.cat(out,take_all_pwrs(vec, pwr-1))
|
|
||||||
|
|
||||||
def set_device(new_device):
|
|
||||||
global device
|
|
||||||
device=new_device
|
device=new_device
|
||||||
|
|
||||||
class LinearRegKernel():
|
class LinearRegKernel():
|
||||||
@@ -154,20 +138,39 @@ class PolyRegKernel():
|
|||||||
power=None
|
power=None
|
||||||
def __init__(self, num_vars, power):
|
def __init__(self, num_vars, power):
|
||||||
self.power=power
|
self.power=power
|
||||||
num_terms=num_poly_terms(num_vars, power)
|
num_terms=self.num_poly_terms(num_vars, power)
|
||||||
self.weights=torch.rand(num_terms, requires_grad=True, device=device)
|
self.weights=torch.rand(num_terms, requires_grad=True, device=device)
|
||||||
self.bias=torch.rand(1, requires_grad=True, device=device)
|
self.bias=torch.rand(1, requires_grad=True, device=device)
|
||||||
self.parameters=[self.weights,self.bias]
|
self.parameters=[self.weights,self.bias]
|
||||||
|
def num_poly_terms(self,num_vars, power):
|
||||||
|
if power == 0:
|
||||||
|
return 0
|
||||||
|
return int(self.factorial(num_vars+power-1) / self.factorial(power) / self.factorial(num_vars-1)) + self.num_poly_terms(num_vars, power-1)
|
||||||
|
def factorial(self,n):
|
||||||
|
if n==0:
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
return n*self.factorial(n-1)
|
||||||
|
def take_all_pwrs(self, vec, pwr):
|
||||||
|
#todo: vectorize (kinda)
|
||||||
|
combins=torch.combinations(vec, r=pwr, with_replacement=True)
|
||||||
|
out=torch.ones(combins.size()[0]).to(device).to(torch.float)
|
||||||
|
for i in torch.t(combins).to(device).to(torch.float):
|
||||||
|
out *= i
|
||||||
|
if pwr == 1:
|
||||||
|
return out
|
||||||
|
else:
|
||||||
|
return torch.cat((out,self.take_all_pwrs(vec, pwr-1)))
|
||||||
def forward(self,mtx):
|
def forward(self,mtx):
|
||||||
#TODO: Vectorize the last part
|
#TODO: Vectorize the last part
|
||||||
cols=[]
|
cols=[]
|
||||||
for i in torch.t(mtx):
|
for i in torch.t(mtx):
|
||||||
cols.append(take_all_pwrs(i,self.power))
|
cols.append(self.take_all_pwrs(i,self.power))
|
||||||
new_mtx=torch.t(torch.stack(cols))
|
new_mtx=torch.t(torch.stack(cols))
|
||||||
long_bias=self.bias.repeat([1,mtx.size()[1]])
|
long_bias=self.bias.repeat([1,mtx.size()[1]])
|
||||||
return torch.matmul(self.weights,new_mtx)+long_bias
|
return torch.matmul(self.weights,new_mtx)+long_bias
|
||||||
|
|
||||||
def SGDTrain(kernel, data, ground, loss=torch.nn.MSELoss(), iterations=1000, learning_rate=.1, return_losses=False):
|
def SGDTrain(self, kernel, data, ground, loss=torch.nn.MSELoss(), iterations=1000, learning_rate=.1, return_losses=False):
|
||||||
optim=torch.optim.SGD(kernel.parameters, lr=learning_rate)
|
optim=torch.optim.SGD(kernel.parameters, lr=learning_rate)
|
||||||
data_cuda=data.to(device)
|
data_cuda=data.to(device)
|
||||||
ground_cuda=ground.to(device)
|
ground_cuda=ground.to(device)
|
||||||
@@ -192,7 +195,7 @@ def SGDTrain(kernel, data, ground, loss=torch.nn.MSELoss(), iterations=1000, lea
|
|||||||
optim.step()
|
optim.step()
|
||||||
return kernel
|
return kernel
|
||||||
|
|
||||||
def CustomTrain(kernel, optim, data, ground, loss=torch.nn.MSELoss(), iterations=1000, return_losses=False):
|
def CustomTrain(self, kernel, optim, data, ground, loss=torch.nn.MSELoss(), iterations=1000, return_losses=False):
|
||||||
data_cuda=data.to(device)
|
data_cuda=data.to(device)
|
||||||
ground_cuda=ground.to(device)
|
ground_cuda=ground.to(device)
|
||||||
if (return_losses):
|
if (return_losses):
|
||||||
|
@@ -7,10 +7,20 @@
|
|||||||
# current benchmark of optimization: 1.33 times faster
|
# current benchmark of optimization: 1.33 times faster
|
||||||
# setup:
|
# setup:
|
||||||
|
|
||||||
__version__ = "1.1.13.001"
|
__version__ = "1.1.13.006"
|
||||||
|
|
||||||
# changelog should be viewed using print(analysis.__changelog__)
|
# changelog should be viewed using print(analysis.__changelog__)
|
||||||
__changelog__ = """changelog:
|
__changelog__ = """changelog:
|
||||||
|
1.1.13.006:
|
||||||
|
- cleaned up imports
|
||||||
|
1.1.13.005:
|
||||||
|
- cleaned up package
|
||||||
|
1.1.13.004:
|
||||||
|
- small fixes to regression to improve performance
|
||||||
|
1.1.13.003:
|
||||||
|
- filtered nans from regression
|
||||||
|
1.1.13.002:
|
||||||
|
- removed torch requirement, and moved Regression back to regression.py
|
||||||
1.1.13.001:
|
1.1.13.001:
|
||||||
- bug fix with linear regression not returning a proper value
|
- bug fix with linear regression not returning a proper value
|
||||||
- cleaned up regression
|
- cleaned up regression
|
||||||
@@ -239,7 +249,6 @@ __author__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'_init_device',
|
|
||||||
'load_csv',
|
'load_csv',
|
||||||
'basic_stats',
|
'basic_stats',
|
||||||
'z_score',
|
'z_score',
|
||||||
@@ -260,7 +269,6 @@ __all__ = [
|
|||||||
'SVM',
|
'SVM',
|
||||||
'random_forest_classifier',
|
'random_forest_classifier',
|
||||||
'random_forest_regressor',
|
'random_forest_regressor',
|
||||||
'Regression',
|
|
||||||
'Glicko2',
|
'Glicko2',
|
||||||
# all statistics functions left out due to integration in other functions
|
# all statistics functions left out due to integration in other functions
|
||||||
]
|
]
|
||||||
@@ -273,24 +281,15 @@ import csv
|
|||||||
import numba
|
import numba
|
||||||
from numba import jit
|
from numba import jit
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import math
|
|
||||||
import scipy
|
import scipy
|
||||||
from scipy import *
|
from scipy import *
|
||||||
import sklearn
|
import sklearn
|
||||||
from sklearn import *
|
from sklearn import *
|
||||||
import torch
|
from analysis import trueskill as Trueskill
|
||||||
try:
|
|
||||||
from analysis import trueskill as Trueskill
|
|
||||||
except:
|
|
||||||
import trueskill as Trueskill
|
|
||||||
|
|
||||||
class error(ValueError):
|
class error(ValueError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _init_device(): # initiates computation device for ANNs
|
|
||||||
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
|
||||||
return device
|
|
||||||
|
|
||||||
def load_csv(filepath):
|
def load_csv(filepath):
|
||||||
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)))
|
||||||
@@ -349,15 +348,15 @@ def histo_analysis(hist_data):
|
|||||||
|
|
||||||
def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
||||||
|
|
||||||
|
X = np.array(inputs)
|
||||||
|
y = np.array(outputs)
|
||||||
|
|
||||||
regressions = []
|
regressions = []
|
||||||
|
|
||||||
if 'lin' in args: # formula: ax + b
|
if 'lin' in args: # formula: ax + b
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
X = np.array(inputs)
|
|
||||||
y = np.array(outputs)
|
|
||||||
|
|
||||||
def func(x, a, b):
|
def func(x, a, b):
|
||||||
|
|
||||||
return a * x + b
|
return a * x + b
|
||||||
@@ -374,9 +373,6 @@ def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
X = np.array(inputs)
|
|
||||||
y = np.array(outputs)
|
|
||||||
|
|
||||||
def func(x, a, b, c, d):
|
def func(x, a, b, c, d):
|
||||||
|
|
||||||
return a * np.log(b*(x + c)) + d
|
return a * np.log(b*(x + c)) + d
|
||||||
@@ -393,9 +389,6 @@ def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
X = np.array(inputs)
|
|
||||||
y = np.array(outputs)
|
|
||||||
|
|
||||||
def func(x, a, b, c, d):
|
def func(x, a, b, c, d):
|
||||||
|
|
||||||
return a * np.exp(b*(x + c)) + d
|
return a * np.exp(b*(x + c)) + d
|
||||||
@@ -410,8 +403,8 @@ def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
|||||||
|
|
||||||
if 'ply' in args: # formula: a + bx^1 + cx^2 + dx^3 + ...
|
if 'ply' in args: # formula: a + bx^1 + cx^2 + dx^3 + ...
|
||||||
|
|
||||||
inputs = [inputs]
|
inputs = np.array([inputs])
|
||||||
outputs = [outputs]
|
outputs = np.array([outputs])
|
||||||
|
|
||||||
plys = []
|
plys = []
|
||||||
limit = len(outputs[0])
|
limit = len(outputs[0])
|
||||||
@@ -435,9 +428,6 @@ def regression(inputs, outputs, args): # inputs, outputs expects N-D array
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
X = np.array(inputs)
|
|
||||||
y = np.array(outputs)
|
|
||||||
|
|
||||||
def func(x, a, b, c, d):
|
def func(x, a, b, c, d):
|
||||||
|
|
||||||
return a * np.tanh(b*(x + c)) + d
|
return a * np.tanh(b*(x + c)) + d
|
||||||
@@ -700,225 +690,6 @@ def random_forest_regressor(data, outputs, test_size, n_estimators="warn", crite
|
|||||||
|
|
||||||
return kernel, RegressionMetrics(predictions, outputs_test)
|
return kernel, RegressionMetrics(predictions, outputs_test)
|
||||||
|
|
||||||
class Regression:
|
|
||||||
|
|
||||||
# Titan Robotics Team 2022: CUDA-based Regressions Module
|
|
||||||
# Written by Arthur Lu & Jacob Levine
|
|
||||||
# Notes:
|
|
||||||
# this module has been automatically inegrated into analysis.py, and should be callable as a class from the package
|
|
||||||
# this module is cuda-optimized and vectorized (except for one small part)
|
|
||||||
# setup:
|
|
||||||
|
|
||||||
__version__ = "1.0.0.003"
|
|
||||||
|
|
||||||
# changelog should be viewed using print(analysis.regression.__changelog__)
|
|
||||||
__changelog__ = """
|
|
||||||
1.0.0.003:
|
|
||||||
- bug fixes
|
|
||||||
1.0.0.002:
|
|
||||||
-Added more parameters to log, exponential, polynomial
|
|
||||||
-Added SigmoidalRegKernelArthur, because Arthur apparently needs
|
|
||||||
to train the scaling and shifting of sigmoids
|
|
||||||
|
|
||||||
1.0.0.001:
|
|
||||||
-initial release, with linear, log, exponential, polynomial, and sigmoid kernels
|
|
||||||
-already vectorized (except for polynomial generation) and CUDA-optimized
|
|
||||||
"""
|
|
||||||
|
|
||||||
__author__ = (
|
|
||||||
"Jacob Levine <jlevine@imsa.edu>",
|
|
||||||
"Arthur Lu <learthurgo@gmail.com>"
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'factorial',
|
|
||||||
'take_all_pwrs',
|
|
||||||
'num_poly_terms',
|
|
||||||
'set_device',
|
|
||||||
'LinearRegKernel',
|
|
||||||
'SigmoidalRegKernel',
|
|
||||||
'LogRegKernel',
|
|
||||||
'PolyRegKernel',
|
|
||||||
'ExpRegKernel',
|
|
||||||
'SigmoidalRegKernelArthur',
|
|
||||||
'SGDTrain',
|
|
||||||
'CustomTrain'
|
|
||||||
]
|
|
||||||
|
|
||||||
global device
|
|
||||||
|
|
||||||
device = "cuda:0" if torch.torch.cuda.is_available() else "cpu"
|
|
||||||
|
|
||||||
#todo: document completely
|
|
||||||
|
|
||||||
def set_device(self, new_device):
|
|
||||||
device=new_device
|
|
||||||
|
|
||||||
class LinearRegKernel():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
bias=None
|
|
||||||
def __init__(self, num_vars):
|
|
||||||
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
|
|
||||||
self.bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.bias]
|
|
||||||
def forward(self,mtx):
|
|
||||||
long_bias=self.bias.repeat([1,mtx.size()[1]])
|
|
||||||
return torch.matmul(self.weights,mtx)+long_bias
|
|
||||||
|
|
||||||
class SigmoidalRegKernel():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
bias=None
|
|
||||||
sigmoid=torch.nn.Sigmoid()
|
|
||||||
def __init__(self, num_vars):
|
|
||||||
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
|
|
||||||
self.bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.bias]
|
|
||||||
def forward(self,mtx):
|
|
||||||
long_bias=self.bias.repeat([1,mtx.size()[1]])
|
|
||||||
return self.sigmoid(torch.matmul(self.weights,mtx)+long_bias)
|
|
||||||
|
|
||||||
class SigmoidalRegKernelArthur():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
in_bias=None
|
|
||||||
scal_mult=None
|
|
||||||
out_bias=None
|
|
||||||
sigmoid=torch.nn.Sigmoid()
|
|
||||||
def __init__(self, num_vars):
|
|
||||||
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
|
|
||||||
self.in_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.scal_mult=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.out_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.in_bias, self.scal_mult, self.out_bias]
|
|
||||||
def forward(self,mtx):
|
|
||||||
long_in_bias=self.in_bias.repeat([1,mtx.size()[1]])
|
|
||||||
long_out_bias=self.out_bias.repeat([1,mtx.size()[1]])
|
|
||||||
return (self.scal_mult*self.sigmoid(torch.matmul(self.weights,mtx)+long_in_bias))+long_out_bias
|
|
||||||
|
|
||||||
class LogRegKernel():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
in_bias=None
|
|
||||||
scal_mult=None
|
|
||||||
out_bias=None
|
|
||||||
def __init__(self, num_vars):
|
|
||||||
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
|
|
||||||
self.in_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.scal_mult=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.out_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.in_bias, self.scal_mult, self.out_bias]
|
|
||||||
def forward(self,mtx):
|
|
||||||
long_in_bias=self.in_bias.repeat([1,mtx.size()[1]])
|
|
||||||
long_out_bias=self.out_bias.repeat([1,mtx.size()[1]])
|
|
||||||
return (self.scal_mult*torch.log(torch.matmul(self.weights,mtx)+long_in_bias))+long_out_bias
|
|
||||||
|
|
||||||
class ExpRegKernel():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
in_bias=None
|
|
||||||
scal_mult=None
|
|
||||||
out_bias=None
|
|
||||||
def __init__(self, num_vars):
|
|
||||||
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
|
|
||||||
self.in_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.scal_mult=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.out_bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.in_bias, self.scal_mult, self.out_bias]
|
|
||||||
def forward(self,mtx):
|
|
||||||
long_in_bias=self.in_bias.repeat([1,mtx.size()[1]])
|
|
||||||
long_out_bias=self.out_bias.repeat([1,mtx.size()[1]])
|
|
||||||
return (self.scal_mult*torch.exp(torch.matmul(self.weights,mtx)+long_in_bias))+long_out_bias
|
|
||||||
|
|
||||||
class PolyRegKernel():
|
|
||||||
parameters= []
|
|
||||||
weights=None
|
|
||||||
bias=None
|
|
||||||
power=None
|
|
||||||
def __init__(self, num_vars, power):
|
|
||||||
self.power=power
|
|
||||||
num_terms=self.num_poly_terms(num_vars, power)
|
|
||||||
self.weights=torch.rand(num_terms, requires_grad=True, device=device)
|
|
||||||
self.bias=torch.rand(1, requires_grad=True, device=device)
|
|
||||||
self.parameters=[self.weights,self.bias]
|
|
||||||
def num_poly_terms(self,num_vars, power):
|
|
||||||
if power == 0:
|
|
||||||
return 0
|
|
||||||
return int(self.factorial(num_vars+power-1) / self.factorial(power) / self.factorial(num_vars-1)) + self.num_poly_terms(num_vars, power-1)
|
|
||||||
def factorial(self,n):
|
|
||||||
if n==0:
|
|
||||||
return 1
|
|
||||||
else:
|
|
||||||
return n*self.factorial(n-1)
|
|
||||||
def take_all_pwrs(self, vec, pwr):
|
|
||||||
#todo: vectorize (kinda)
|
|
||||||
combins=torch.combinations(vec, r=pwr, with_replacement=True)
|
|
||||||
out=torch.ones(combins.size()[0]).to(device).to(torch.float)
|
|
||||||
for i in torch.t(combins).to(device).to(torch.float):
|
|
||||||
out *= i
|
|
||||||
if pwr == 1:
|
|
||||||
return out
|
|
||||||
else:
|
|
||||||
return torch.cat((out,self.take_all_pwrs(vec, pwr-1)))
|
|
||||||
def forward(self,mtx):
|
|
||||||
#TODO: Vectorize the last part
|
|
||||||
cols=[]
|
|
||||||
for i in torch.t(mtx):
|
|
||||||
cols.append(self.take_all_pwrs(i,self.power))
|
|
||||||
new_mtx=torch.t(torch.stack(cols))
|
|
||||||
long_bias=self.bias.repeat([1,mtx.size()[1]])
|
|
||||||
return torch.matmul(self.weights,new_mtx)+long_bias
|
|
||||||
|
|
||||||
def SGDTrain(self, kernel, data, ground, loss=torch.nn.MSELoss(), iterations=1000, learning_rate=.1, return_losses=False):
|
|
||||||
optim=torch.optim.SGD(kernel.parameters, lr=learning_rate)
|
|
||||||
data_cuda=data.to(device)
|
|
||||||
ground_cuda=ground.to(device)
|
|
||||||
if (return_losses):
|
|
||||||
losses=[]
|
|
||||||
for i in range(iterations):
|
|
||||||
with torch.set_grad_enabled(True):
|
|
||||||
optim.zero_grad()
|
|
||||||
pred=kernel.forward(data_cuda)
|
|
||||||
ls=loss(pred,ground_cuda)
|
|
||||||
losses.append(ls.item())
|
|
||||||
ls.backward()
|
|
||||||
optim.step()
|
|
||||||
return [kernel,losses]
|
|
||||||
else:
|
|
||||||
for i in range(iterations):
|
|
||||||
with torch.set_grad_enabled(True):
|
|
||||||
optim.zero_grad()
|
|
||||||
pred=kernel.forward(data_cuda)
|
|
||||||
ls=loss(pred,ground_cuda)
|
|
||||||
ls.backward()
|
|
||||||
optim.step()
|
|
||||||
return kernel
|
|
||||||
|
|
||||||
def CustomTrain(self, kernel, optim, data, ground, loss=torch.nn.MSELoss(), iterations=1000, return_losses=False):
|
|
||||||
data_cuda=data.to(device)
|
|
||||||
ground_cuda=ground.to(device)
|
|
||||||
if (return_losses):
|
|
||||||
losses=[]
|
|
||||||
for i in range(iterations):
|
|
||||||
with torch.set_grad_enabled(True):
|
|
||||||
optim.zero_grad()
|
|
||||||
pred=kernel.forward(data)
|
|
||||||
ls=loss(pred,ground)
|
|
||||||
losses.append(ls.item())
|
|
||||||
ls.backward()
|
|
||||||
optim.step()
|
|
||||||
return [kernel,losses]
|
|
||||||
else:
|
|
||||||
for i in range(iterations):
|
|
||||||
with torch.set_grad_enabled(True):
|
|
||||||
optim.zero_grad()
|
|
||||||
pred=kernel.forward(data_cuda)
|
|
||||||
ls=loss(pred,ground_cuda)
|
|
||||||
ls.backward()
|
|
||||||
optim.step()
|
|
||||||
return kernel
|
|
||||||
|
|
||||||
class Glicko2:
|
class Glicko2:
|
||||||
|
|
||||||
_tau = 0.5
|
_tau = 0.5
|
||||||
|
@@ -1,20 +1,23 @@
|
|||||||
# Titan Robotics Team 2022: CUDA-based Regressions Module
|
# Titan Robotics Team 2022: CUDA-based Regressions Module
|
||||||
# Written by Arthur Lu & Jacob Levine
|
# Written by Arthur Lu & Jacob Levine
|
||||||
# Notes:
|
# Notes:
|
||||||
# this should be imported as a python module using 'import regression'
|
# this module has been automatically inegrated into analysis.py, and should be callable as a class from the package
|
||||||
# this should be included in the local directory or environment variable
|
|
||||||
# this module is cuda-optimized and vectorized (except for one small part)
|
# this module is cuda-optimized and vectorized (except for one small part)
|
||||||
# setup:
|
# setup:
|
||||||
|
|
||||||
__version__ = "1.0.0.002"
|
__version__ = "1.0.0.004"
|
||||||
|
|
||||||
# changelog should be viewed using print(regression.__changelog__)
|
# changelog should be viewed using print(analysis.regression.__changelog__)
|
||||||
__changelog__ = """
|
__changelog__ = """
|
||||||
|
1.0.0.004:
|
||||||
|
- bug fixes
|
||||||
|
- fixed changelog
|
||||||
|
1.0.0.003:
|
||||||
|
- bug fixes
|
||||||
1.0.0.002:
|
1.0.0.002:
|
||||||
-Added more parameters to log, exponential, polynomial
|
-Added more parameters to log, exponential, polynomial
|
||||||
-Added SigmoidalRegKernelArthur, because Arthur apparently needs
|
-Added SigmoidalRegKernelArthur, because Arthur apparently needs
|
||||||
to train the scaling and shifting of sigmoids
|
to train the scaling and shifting of sigmoids
|
||||||
|
|
||||||
1.0.0.001:
|
1.0.0.001:
|
||||||
-initial release, with linear, log, exponential, polynomial, and sigmoid kernels
|
-initial release, with linear, log, exponential, polynomial, and sigmoid kernels
|
||||||
-already vectorized (except for polynomial generation) and CUDA-optimized
|
-already vectorized (except for polynomial generation) and CUDA-optimized
|
||||||
@@ -22,6 +25,7 @@ __changelog__ = """
|
|||||||
|
|
||||||
__author__ = (
|
__author__ = (
|
||||||
"Jacob Levine <jlevine@imsa.edu>",
|
"Jacob Levine <jlevine@imsa.edu>",
|
||||||
|
"Arthur Lu <learthurgo@gmail.com>"
|
||||||
)
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
@@ -39,35 +43,15 @@ __all__ = [
|
|||||||
'CustomTrain'
|
'CustomTrain'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
# imports (just one for now):
|
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
|
global device
|
||||||
|
|
||||||
device = "cuda:0" if torch.torch.cuda.is_available() else "cpu"
|
device = "cuda:0" if torch.torch.cuda.is_available() else "cpu"
|
||||||
|
|
||||||
#todo: document completely
|
#todo: document completely
|
||||||
|
|
||||||
def factorial(n):
|
def set_device(self, new_device):
|
||||||
if n==0:
|
|
||||||
return 1
|
|
||||||
else:
|
|
||||||
return n*factorial(n-1)
|
|
||||||
def num_poly_terms(num_vars, power):
|
|
||||||
if power == 0:
|
|
||||||
return 0
|
|
||||||
return int(factorial(num_vars+power-1) / factorial(power) / factorial(num_vars-1)) + num_poly_terms(num_vars, power-1)
|
|
||||||
|
|
||||||
def take_all_pwrs(vec,pwr):
|
|
||||||
#todo: vectorize (kinda)
|
|
||||||
combins=torch.combinations(vec, r=pwr, with_replacement=True)
|
|
||||||
out=torch.ones(combins.size()[0])
|
|
||||||
for i in torch.t(combins):
|
|
||||||
out *= i
|
|
||||||
return torch.cat(out,take_all_pwrs(vec, pwr-1))
|
|
||||||
|
|
||||||
def set_device(new_device):
|
|
||||||
global device
|
|
||||||
device=new_device
|
device=new_device
|
||||||
|
|
||||||
class LinearRegKernel():
|
class LinearRegKernel():
|
||||||
@@ -154,20 +138,39 @@ class PolyRegKernel():
|
|||||||
power=None
|
power=None
|
||||||
def __init__(self, num_vars, power):
|
def __init__(self, num_vars, power):
|
||||||
self.power=power
|
self.power=power
|
||||||
num_terms=num_poly_terms(num_vars, power)
|
num_terms=self.num_poly_terms(num_vars, power)
|
||||||
self.weights=torch.rand(num_terms, requires_grad=True, device=device)
|
self.weights=torch.rand(num_terms, requires_grad=True, device=device)
|
||||||
self.bias=torch.rand(1, requires_grad=True, device=device)
|
self.bias=torch.rand(1, requires_grad=True, device=device)
|
||||||
self.parameters=[self.weights,self.bias]
|
self.parameters=[self.weights,self.bias]
|
||||||
|
def num_poly_terms(self,num_vars, power):
|
||||||
|
if power == 0:
|
||||||
|
return 0
|
||||||
|
return int(self.factorial(num_vars+power-1) / self.factorial(power) / self.factorial(num_vars-1)) + self.num_poly_terms(num_vars, power-1)
|
||||||
|
def factorial(self,n):
|
||||||
|
if n==0:
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
return n*self.factorial(n-1)
|
||||||
|
def take_all_pwrs(self, vec, pwr):
|
||||||
|
#todo: vectorize (kinda)
|
||||||
|
combins=torch.combinations(vec, r=pwr, with_replacement=True)
|
||||||
|
out=torch.ones(combins.size()[0]).to(device).to(torch.float)
|
||||||
|
for i in torch.t(combins).to(device).to(torch.float):
|
||||||
|
out *= i
|
||||||
|
if pwr == 1:
|
||||||
|
return out
|
||||||
|
else:
|
||||||
|
return torch.cat((out,self.take_all_pwrs(vec, pwr-1)))
|
||||||
def forward(self,mtx):
|
def forward(self,mtx):
|
||||||
#TODO: Vectorize the last part
|
#TODO: Vectorize the last part
|
||||||
cols=[]
|
cols=[]
|
||||||
for i in torch.t(mtx):
|
for i in torch.t(mtx):
|
||||||
cols.append(take_all_pwrs(i,self.power))
|
cols.append(self.take_all_pwrs(i,self.power))
|
||||||
new_mtx=torch.t(torch.stack(cols))
|
new_mtx=torch.t(torch.stack(cols))
|
||||||
long_bias=self.bias.repeat([1,mtx.size()[1]])
|
long_bias=self.bias.repeat([1,mtx.size()[1]])
|
||||||
return torch.matmul(self.weights,new_mtx)+long_bias
|
return torch.matmul(self.weights,new_mtx)+long_bias
|
||||||
|
|
||||||
def SGDTrain(kernel, data, ground, loss=torch.nn.MSELoss(), iterations=1000, learning_rate=.1, return_losses=False):
|
def SGDTrain(self, kernel, data, ground, loss=torch.nn.MSELoss(), iterations=1000, learning_rate=.1, return_losses=False):
|
||||||
optim=torch.optim.SGD(kernel.parameters, lr=learning_rate)
|
optim=torch.optim.SGD(kernel.parameters, lr=learning_rate)
|
||||||
data_cuda=data.to(device)
|
data_cuda=data.to(device)
|
||||||
ground_cuda=ground.to(device)
|
ground_cuda=ground.to(device)
|
||||||
@@ -192,7 +195,7 @@ def SGDTrain(kernel, data, ground, loss=torch.nn.MSELoss(), iterations=1000, lea
|
|||||||
optim.step()
|
optim.step()
|
||||||
return kernel
|
return kernel
|
||||||
|
|
||||||
def CustomTrain(kernel, optim, data, ground, loss=torch.nn.MSELoss(), iterations=1000, return_losses=False):
|
def CustomTrain(self, kernel, optim, data, ground, loss=torch.nn.MSELoss(), iterations=1000, return_losses=False):
|
||||||
data_cuda=data.to(device)
|
data_cuda=data.to(device)
|
||||||
ground_cuda=ground.to(device)
|
ground_cuda=ground.to(device)
|
||||||
if (return_losses):
|
if (return_losses):
|
||||||
|
Binary file not shown.
BIN
analysis-master/dist/analysis-1.0.0.6.tar.gz
vendored
BIN
analysis-master/dist/analysis-1.0.0.6.tar.gz
vendored
Binary file not shown.
BIN
analysis-master/dist/analysis-1.0.0.8-py3-none-any.whl
vendored
Normal file
BIN
analysis-master/dist/analysis-1.0.0.8-py3-none-any.whl
vendored
Normal file
Binary file not shown.
BIN
analysis-master/dist/analysis-1.0.0.8.tar.gz
vendored
Normal file
BIN
analysis-master/dist/analysis-1.0.0.8.tar.gz
vendored
Normal file
Binary file not shown.
@@ -2,7 +2,7 @@ import setuptools
|
|||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="analysis", # Replace with your own username
|
name="analysis", # Replace with your own username
|
||||||
version="1.0.0.006",
|
version="1.0.0.008",
|
||||||
author="The Titan Scouting Team",
|
author="The Titan Scouting Team",
|
||||||
author_email="titanscout2022@gmail.com",
|
author_email="titanscout2022@gmail.com",
|
||||||
description="analysis package developed by Titan Scouting for The Red Alliance",
|
description="analysis package developed by Titan Scouting for The Red Alliance",
|
||||||
@@ -10,6 +10,14 @@ setuptools.setup(
|
|||||||
long_description_content_type="text/markdown",
|
long_description_content_type="text/markdown",
|
||||||
url="https://github.com/titanscout2022/tr2022-strategy",
|
url="https://github.com/titanscout2022/tr2022-strategy",
|
||||||
packages=setuptools.find_packages(),
|
packages=setuptools.find_packages(),
|
||||||
|
install_requires=[
|
||||||
|
"numba",
|
||||||
|
"numpy",
|
||||||
|
"scipy",
|
||||||
|
"scikit-learn",
|
||||||
|
"six",
|
||||||
|
"matplotlib"
|
||||||
|
],
|
||||||
license = "GNU General Public License v3.0",
|
license = "GNU General Public License v3.0",
|
||||||
classifiers=[
|
classifiers=[
|
||||||
"Programming Language :: Python :: 3",
|
"Programming Language :: Python :: 3",
|
||||||
|
Binary file not shown.
@@ -1,6 +0,0 @@
|
|||||||
2020ilch
|
|
||||||
balls-blocked,basic_stats,historical_analysis,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
|
||||||
balls-collected,basic_stats,historical_analysis,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
|
||||||
balls-lower,basic_stats,historical_analysis,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
|
||||||
balls-started,basic_stats,historical_analyss,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
|
||||||
balls-upper,basic_stats,historical_analysis,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
|
|
1
data analysis/config/competition.config
Normal file
1
data analysis/config/competition.config
Normal file
@@ -0,0 +1 @@
|
|||||||
|
2020ilch
|
0
data analysis/config/database.config
Normal file
0
data analysis/config/database.config
Normal file
14
data analysis/config/stats.config
Normal file
14
data analysis/config/stats.config
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
balls-blocked,basic_stats,historical_analysis,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
||||||
|
balls-collected,basic_stats,historical_analysis,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
||||||
|
balls-lower-teleop,basic_stats,historical_analysis,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
||||||
|
balls-lower-auto,basic_stats,historical_analysis,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
||||||
|
balls-started,basic_stats,historical_analyss,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
||||||
|
balls-upper-teleop,basic_stats,historical_analysis,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
||||||
|
balls-upper-auto,basic_stats,historical_analysis,regression_linear,regression_logarithmic,regression_exponential,regression_polynomial,regression_sigmoidal
|
||||||
|
wheel-mechanism
|
||||||
|
low-balls
|
||||||
|
high-balls
|
||||||
|
wheel-success
|
||||||
|
strategic-focus
|
||||||
|
climb-mechanism
|
||||||
|
attitude
|
@@ -8,7 +8,7 @@ def pull_new_tba_matches(apikey, competition, cutoff):
|
|||||||
x=requests.get("https://www.thebluealliance.com/api/v3/event/"+competition+"/matches/simple", headers={"X-TBA-Auth_Key":api_key})
|
x=requests.get("https://www.thebluealliance.com/api/v3/event/"+competition+"/matches/simple", headers={"X-TBA-Auth_Key":api_key})
|
||||||
out = []
|
out = []
|
||||||
for i in x.json():
|
for i in x.json():
|
||||||
if (i["actual_time"]-cutoff >= 0 and i["comp_level"] == "qm"):
|
if (i["actual_time"] != None and i["actual_time"]-cutoff >= 0 and i["comp_level"] == "qm"):
|
||||||
out.append({"match" : i['match_number'], "blue" : list(map(lambda x: int(x[3:]), i['alliances']['blue']['team_keys'])), "red" : list(map(lambda x: int(x[3:]), i['alliances']['red']['team_keys'])), "winner": i["winning_alliance"]})
|
out.append({"match" : i['match_number'], "blue" : list(map(lambda x: int(x[3:]), i['alliances']['blue']['team_keys'])), "red" : list(map(lambda x: int(x[3:]), i['alliances']['red']['team_keys'])), "winner": i["winning_alliance"]})
|
||||||
return out
|
return out
|
||||||
|
|
||||||
@@ -21,6 +21,13 @@ def get_team_match_data(apikey, competition, team_num):
|
|||||||
out[i['match']] = i['data']
|
out[i['match']] = i['data']
|
||||||
return pd.DataFrame(out)
|
return pd.DataFrame(out)
|
||||||
|
|
||||||
|
def get_team_pit_data(apikey, competition, team_num):
|
||||||
|
client = pymongo.MongoClient(apikey)
|
||||||
|
db = client.data_scouting
|
||||||
|
mdata = db.pitdata
|
||||||
|
out = {}
|
||||||
|
return mdata.find_one({"competition" : competition, "team_scouted": team_num})["data"]
|
||||||
|
|
||||||
def get_team_metrics_data(apikey, competition, team_num):
|
def get_team_metrics_data(apikey, competition, team_num):
|
||||||
client = pymongo.MongoClient(apikey)
|
client = pymongo.MongoClient(apikey)
|
||||||
db = client.data_processing
|
db = client.data_processing
|
||||||
@@ -38,7 +45,7 @@ def unkeyify_2l(layered_dict):
|
|||||||
out[i] = list(map(lambda x: x[1], add))
|
out[i] = list(map(lambda x: x[1], add))
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def get_data_formatted(apikey, competition):
|
def get_match_data_formatted(apikey, competition):
|
||||||
client = pymongo.MongoClient(apikey)
|
client = pymongo.MongoClient(apikey)
|
||||||
db = client.data_scouting
|
db = client.data_scouting
|
||||||
mdata = db.teamlist
|
mdata = db.teamlist
|
||||||
@@ -51,6 +58,19 @@ def get_data_formatted(apikey, competition):
|
|||||||
pass
|
pass
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
def get_pit_data_formatted(apikey, competition):
|
||||||
|
client = pymongo.MongoClient(apikey)
|
||||||
|
db = client.data_scouting
|
||||||
|
mdata = db.teamlist
|
||||||
|
x=mdata.find_one({"competition":competition})
|
||||||
|
out = {}
|
||||||
|
for i in x:
|
||||||
|
try:
|
||||||
|
out[int(i)] = get_team_pit_data(apikey, competition, int(i))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
return out
|
||||||
|
|
||||||
def push_team_tests_data(apikey, competition, team_num, data, dbname = "data_processing", colname = "team_tests"):
|
def push_team_tests_data(apikey, competition, team_num, data, dbname = "data_processing", colname = "team_tests"):
|
||||||
client = pymongo.MongoClient(apikey)
|
client = pymongo.MongoClient(apikey)
|
||||||
db = client[dbname]
|
db = client[dbname]
|
||||||
@@ -63,6 +83,12 @@ def push_team_metrics_data(apikey, competition, team_num, data, dbname = "data_p
|
|||||||
mdata = db[colname]
|
mdata = db[colname]
|
||||||
mdata.replace_one({"competition" : competition, "team": team_num}, {"_id": competition+str(team_num)+"am", "competition" : competition, "team" : team_num, "metrics" : data}, True)
|
mdata.replace_one({"competition" : competition, "team": team_num}, {"_id": competition+str(team_num)+"am", "competition" : competition, "team" : team_num, "metrics" : data}, True)
|
||||||
|
|
||||||
|
def push_team_pit_data(apikey, competition, variable, data, dbname = "data_processing", colname = "team_pit"):
|
||||||
|
client = pymongo.MongoClient(apikey)
|
||||||
|
db = client[dbname]
|
||||||
|
mdata = db[colname]
|
||||||
|
mdata.replace_one({"competition" : competition, "variable": variable}, {"competition" : competition, "variable" : variable, "data" : data}, True)
|
||||||
|
|
||||||
def get_analysis_flags(apikey, flag):
|
def get_analysis_flags(apikey, flag):
|
||||||
client = pymongo.MongoClient(apikey)
|
client = pymongo.MongoClient(apikey)
|
||||||
db = client.data_processing
|
db = client.data_processing
|
||||||
|
59
data analysis/get_team_rankings.py
Normal file
59
data analysis/get_team_rankings.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import data as d
|
||||||
|
from analysis import analysis as an
|
||||||
|
import pymongo
|
||||||
|
import operator
|
||||||
|
|
||||||
|
def load_config(file):
|
||||||
|
config_vector = {}
|
||||||
|
file = an.load_csv(file)
|
||||||
|
for line in file[1:]:
|
||||||
|
config_vector[line[0]] = line[1:]
|
||||||
|
|
||||||
|
return (file[0][0], config_vector)
|
||||||
|
|
||||||
|
def get_metrics_processed_formatted(apikey, competition):
|
||||||
|
client = pymongo.MongoClient(apikey)
|
||||||
|
db = client.data_scouting
|
||||||
|
mdata = db.teamlist
|
||||||
|
x=mdata.find_one({"competition":competition})
|
||||||
|
out = {}
|
||||||
|
for i in x:
|
||||||
|
try:
|
||||||
|
out[int(i)] = d.get_team_metrics_data(apikey, competition, int(i))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
return out
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
apikey = an.load_csv("keys.txt")[0][0]
|
||||||
|
tbakey = an.load_csv("keys.txt")[1][0]
|
||||||
|
|
||||||
|
competition, config = load_config("config.csv")
|
||||||
|
|
||||||
|
metrics = get_metrics_processed_formatted(apikey, competition)
|
||||||
|
|
||||||
|
elo = {}
|
||||||
|
gl2 = {}
|
||||||
|
|
||||||
|
for team in metrics:
|
||||||
|
|
||||||
|
elo[team] = metrics[team]["metrics"]["elo"]["score"]
|
||||||
|
gl2[team] = metrics[team]["metrics"]["gl2"]["score"]
|
||||||
|
|
||||||
|
elo = {k: v for k, v in sorted(elo.items(), key=lambda item: item[1])}
|
||||||
|
gl2 = {k: v for k, v in sorted(gl2.items(), key=lambda item: item[1])}
|
||||||
|
|
||||||
|
for team in elo:
|
||||||
|
|
||||||
|
print("teams sorted by elo:")
|
||||||
|
print("" + str(team) + " | " + str(elo[team]))
|
||||||
|
|
||||||
|
print("*"*25)
|
||||||
|
|
||||||
|
for team in gl2:
|
||||||
|
|
||||||
|
print("teams sorted by glicko2:")
|
||||||
|
print("" + str(team) + " | " + str(gl2[team]))
|
||||||
|
|
||||||
|
main()
|
@@ -3,10 +3,24 @@
|
|||||||
# Notes:
|
# Notes:
|
||||||
# setup:
|
# setup:
|
||||||
|
|
||||||
__version__ = "0.0.2.001"
|
__version__ = "0.0.5.000"
|
||||||
|
|
||||||
# changelog should be viewed using print(analysis.__changelog__)
|
# changelog should be viewed using print(analysis.__changelog__)
|
||||||
__changelog__ = """changelog:
|
__changelog__ = """changelog:
|
||||||
|
0.0.5.000:
|
||||||
|
improved user interface
|
||||||
|
0.0.4.002:
|
||||||
|
- removed unessasary code
|
||||||
|
0.0.4.001:
|
||||||
|
- fixed bug where X range for regression was determined before sanitization
|
||||||
|
- better sanitized data
|
||||||
|
0.0.4.000:
|
||||||
|
- fixed spelling issue in __changelog__
|
||||||
|
- addressed nan bug in regression
|
||||||
|
- fixed errors on line 335 with metrics calling incorrect key "glicko2"
|
||||||
|
- fixed errors in metrics computing
|
||||||
|
0.0.3.000:
|
||||||
|
- added analysis to pit data
|
||||||
0.0.2.001:
|
0.0.2.001:
|
||||||
- minor stability patches
|
- minor stability patches
|
||||||
- implemented db syncing for timestamps
|
- implemented db syncing for timestamps
|
||||||
@@ -69,6 +83,10 @@ __all__ = [
|
|||||||
|
|
||||||
from analysis import analysis as an
|
from analysis import analysis as an
|
||||||
import data as d
|
import data as d
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from os import system, name
|
||||||
|
from pathlib import Path
|
||||||
import time
|
import time
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
@@ -77,16 +95,16 @@ def main():
|
|||||||
while(True):
|
while(True):
|
||||||
|
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
print("time: " + str(current_time))
|
print("[OK] time: " + str(current_time))
|
||||||
|
|
||||||
print(" loading config")
|
start = time.time()
|
||||||
competition, config = load_config("config.csv")
|
config = load_config(Path("config/stats.config"))
|
||||||
print(" config loaded")
|
competition = an.load_csv(Path("config/competition.config"))[0][0]
|
||||||
|
print("[OK] configs loaded")
|
||||||
|
|
||||||
print(" loading database keys")
|
apikey = an.load_csv(Path("config/keys.config"))[0][0]
|
||||||
apikey = an.load_csv("keys.txt")[0][0]
|
tbakey = an.load_csv(Path("config/keys.config"))[1][0]
|
||||||
tbakey = an.load_csv("keys.txt")[1][0]
|
print("[OK] loaded keys")
|
||||||
print(" loaded keys")
|
|
||||||
|
|
||||||
previous_time = d.get_analysis_flags(apikey, "latest_update")
|
previous_time = d.get_analysis_flags(apikey, "latest_update")
|
||||||
|
|
||||||
@@ -99,33 +117,55 @@ def main():
|
|||||||
|
|
||||||
previous_time = previous_time["latest_update"]
|
previous_time = previous_time["latest_update"]
|
||||||
|
|
||||||
print(" analysis backtimed to: " + str(previous_time))
|
print("[OK] analysis backtimed to: " + str(previous_time))
|
||||||
|
|
||||||
print(" loading data")
|
print("[OK] loading data")
|
||||||
data = d.get_data_formatted(apikey, competition)
|
start = time.time()
|
||||||
print(" loaded data")
|
data = d.get_match_data_formatted(apikey, competition)
|
||||||
|
pit_data = d.pit = d.get_pit_data_formatted(apikey, competition)
|
||||||
|
print("[OK] loaded data in " + str(time.time() - start) + " seconds")
|
||||||
|
|
||||||
print(" running tests")
|
print("[OK] running tests")
|
||||||
|
start = time.time()
|
||||||
results = simpleloop(data, config)
|
results = simpleloop(data, config)
|
||||||
print(" finished tests")
|
print("[OK] finished tests in " + str(time.time() - start) + " seconds")
|
||||||
|
|
||||||
print(" running metrics")
|
print("[OK] running metrics")
|
||||||
metrics = metricsloop(tbakey, apikey, competition, previous_time)
|
start = time.time()
|
||||||
print(" finished metrics")
|
metricsloop(tbakey, apikey, competition, previous_time)
|
||||||
|
print("[OK] finished metrics in " + str(time.time() - start) + " seconds")
|
||||||
|
|
||||||
|
print("[OK] running pit analysis")
|
||||||
|
start = time.time()
|
||||||
|
pit = pitloop(pit_data, config)
|
||||||
|
print("[OK] finished pit analysis in " + str(time.time() - start) + " seconds")
|
||||||
|
|
||||||
d.set_analysis_flags(apikey, "latest_update", {"latest_update":current_time})
|
d.set_analysis_flags(apikey, "latest_update", {"latest_update":current_time})
|
||||||
|
|
||||||
print(" pushing to database")
|
print("[OK] pushing to database")
|
||||||
push_to_database(apikey, competition, results, metrics)
|
start = time.time()
|
||||||
print(" pushed to database")
|
push_to_database(apikey, competition, results, pit)
|
||||||
|
print("[OK] pushed to database in " + str(time.time() - start) + " seconds")
|
||||||
|
|
||||||
|
clear()
|
||||||
|
|
||||||
|
def clear():
|
||||||
|
|
||||||
|
# for windows
|
||||||
|
if name == 'nt':
|
||||||
|
_ = system('cls')
|
||||||
|
|
||||||
|
# for mac and linux(here, os.name is 'posix')
|
||||||
|
else:
|
||||||
|
_ = system('clear')
|
||||||
|
|
||||||
def load_config(file):
|
def load_config(file):
|
||||||
config_vector = {}
|
config_vector = {}
|
||||||
file = an.load_csv(file)
|
file = an.load_csv(file)
|
||||||
for line in file[1:]:
|
for line in file:
|
||||||
config_vector[line[0]] = line[1:]
|
config_vector[line[0]] = line[1:]
|
||||||
|
|
||||||
return (file[0][0], config_vector)
|
return config_vector
|
||||||
|
|
||||||
def simpleloop(data, tests): # expects 3D array with [Team][Variable][Match]
|
def simpleloop(data, tests): # expects 3D array with [Team][Variable][Match]
|
||||||
|
|
||||||
@@ -147,36 +187,40 @@ def simpleloop(data, tests): # expects 3D array with [Team][Variable][Match]
|
|||||||
|
|
||||||
def simplestats(data, test):
|
def simplestats(data, test):
|
||||||
|
|
||||||
|
data = np.array(data)
|
||||||
|
data = data[np.isfinite(data)]
|
||||||
|
ranges = list(range(len(data)))
|
||||||
|
|
||||||
if(test == "basic_stats"):
|
if(test == "basic_stats"):
|
||||||
return an.basic_stats(data)
|
return an.basic_stats(data)
|
||||||
|
|
||||||
if(test == "historical_analysis"):
|
if(test == "historical_analysis"):
|
||||||
return an.histo_analysis([list(range(len(data))), data])
|
return an.histo_analysis([ranges, data])
|
||||||
|
|
||||||
if(test == "regression_linear"):
|
if(test == "regression_linear"):
|
||||||
return an.regression(list(range(len(data))), data, ['lin'])
|
return an.regression(ranges, data, ['lin'])
|
||||||
|
|
||||||
if(test == "regression_logarithmic"):
|
if(test == "regression_logarithmic"):
|
||||||
return an.regression(list(range(len(data))), data, ['log'])
|
return an.regression(ranges, data, ['log'])
|
||||||
|
|
||||||
if(test == "regression_exponential"):
|
if(test == "regression_exponential"):
|
||||||
return an.regression(list(range(len(data))), data, ['exp'])
|
return an.regression(ranges, data, ['exp'])
|
||||||
|
|
||||||
if(test == "regression_polynomial"):
|
if(test == "regression_polynomial"):
|
||||||
return an.regression(list(range(len(data))), data, ['ply'])
|
return an.regression(ranges, data, ['ply'])
|
||||||
|
|
||||||
if(test == "regression_sigmoidal"):
|
if(test == "regression_sigmoidal"):
|
||||||
return an.regression(list(range(len(data))), data, ['sig'])
|
return an.regression(ranges, data, ['sig'])
|
||||||
|
|
||||||
def push_to_database(apikey, competition, results, metrics):
|
def push_to_database(apikey, competition, results, pit):
|
||||||
|
|
||||||
for team in results:
|
for team in results:
|
||||||
|
|
||||||
d.push_team_tests_data(apikey, competition, team, results[team])
|
d.push_team_tests_data(apikey, competition, team, results[team])
|
||||||
|
|
||||||
for team in metrics:
|
for variable in pit:
|
||||||
|
|
||||||
d.push_team_metrics_data(apikey, competition, team, metrics[team])
|
d.push_team_pit_data(apikey, competition, variable, pit[variable])
|
||||||
|
|
||||||
def metricsloop(tbakey, apikey, competition, timestamp): # listener based metrics update
|
def metricsloop(tbakey, apikey, competition, timestamp): # listener based metrics update
|
||||||
|
|
||||||
@@ -185,8 +229,6 @@ def metricsloop(tbakey, apikey, competition, timestamp): # listener based metric
|
|||||||
|
|
||||||
matches = d.pull_new_tba_matches(tbakey, competition, timestamp)
|
matches = d.pull_new_tba_matches(tbakey, competition, timestamp)
|
||||||
|
|
||||||
return_vector = {}
|
|
||||||
|
|
||||||
red = {}
|
red = {}
|
||||||
blu = {}
|
blu = {}
|
||||||
|
|
||||||
@@ -267,36 +309,13 @@ def metricsloop(tbakey, apikey, competition, timestamp): # listener based metric
|
|||||||
blu[team]["gl2"]["rd"] = blu[team]["gl2"]["rd"] + blu_gl2_delta["rd"]
|
blu[team]["gl2"]["rd"] = blu[team]["gl2"]["rd"] + blu_gl2_delta["rd"]
|
||||||
blu[team]["gl2"]["vol"] = blu[team]["gl2"]["vol"] + blu_gl2_delta["vol"]
|
blu[team]["gl2"]["vol"] = blu[team]["gl2"]["vol"] + blu_gl2_delta["vol"]
|
||||||
|
|
||||||
""" not functional for now
|
temp_vector = {}
|
||||||
red_trueskill = []
|
temp_vector.update(red)
|
||||||
blu_trueskill = []
|
temp_vector.update(blu)
|
||||||
|
|
||||||
red_ts_team_lookup = []
|
for team in temp_vector:
|
||||||
blu_ts_team_lookup = []
|
|
||||||
|
|
||||||
for team in red:
|
d.push_team_metrics_data(apikey, competition, team, temp_vector[team])
|
||||||
|
|
||||||
red_trueskill.append((red[team]["ts"]["mu"], red[team]["ts"]["sigma"]))
|
|
||||||
red_ts_team_lookup.append(team)
|
|
||||||
|
|
||||||
for team in blu:
|
|
||||||
|
|
||||||
blu_trueskill.append((blu[team]["ts"]["mu"], blu[team]["ts"]["sigma"]))
|
|
||||||
blu_ts_team_lookup.append(team)
|
|
||||||
|
|
||||||
print(red_trueskill)
|
|
||||||
print(blu_trueskill)
|
|
||||||
|
|
||||||
results = an.trueskill([red_trueskill, blu_trueskill], [observations["red"], observations["blu"]])
|
|
||||||
|
|
||||||
print(results)
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
return_vector.update(red)
|
|
||||||
return_vector.update(blu)
|
|
||||||
|
|
||||||
return return_vector
|
|
||||||
|
|
||||||
def load_metrics(apikey, competition, match, group_name):
|
def load_metrics(apikey, competition, match, group_name):
|
||||||
|
|
||||||
@@ -312,21 +331,34 @@ def load_metrics(apikey, competition, match, group_name):
|
|||||||
gl2 = {"score": 1500, "rd": 250, "vol": 0.06}
|
gl2 = {"score": 1500, "rd": 250, "vol": 0.06}
|
||||||
ts = {"mu": 25, "sigma": 25/3}
|
ts = {"mu": 25, "sigma": 25/3}
|
||||||
|
|
||||||
d.push_team_metrics_data(apikey, competition, team, {"elo":elo, "gliko2":gl2,"trueskill":ts})
|
#d.push_team_metrics_data(apikey, competition, team, {"elo":elo, "gl2":gl2,"trueskill":ts})
|
||||||
|
|
||||||
group[team] = {"elo": elo, "gl2": gl2, "ts": ts}
|
group[team] = {"elo": elo, "gl2": gl2, "ts": ts}
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
metrics = db_data["metrics"]
|
metrics = db_data["metrics"]
|
||||||
|
|
||||||
elo = metrics["elo"]
|
elo = metrics["elo"]
|
||||||
gl2 = metrics["gliko2"]
|
gl2 = metrics["gl2"]
|
||||||
ts = metrics["trueskill"]
|
ts = metrics["ts"]
|
||||||
|
|
||||||
group[team] = {"elo": elo, "gl2": gl2, "ts": ts}
|
group[team] = {"elo": elo, "gl2": gl2, "ts": ts}
|
||||||
|
|
||||||
return group
|
return group
|
||||||
|
|
||||||
|
def pitloop(pit, tests):
|
||||||
|
|
||||||
|
return_vector = {}
|
||||||
|
for team in pit:
|
||||||
|
for variable in pit[team]:
|
||||||
|
if(variable in tests):
|
||||||
|
if(not variable in return_vector):
|
||||||
|
return_vector[variable] = []
|
||||||
|
return_vector[variable].append(pit[team][variable])
|
||||||
|
|
||||||
|
return return_vector
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
59
data analysis/visualize_pit.py
Normal file
59
data analysis/visualize_pit.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
# To add a new cell, type '# %%'
|
||||||
|
# To add a new markdown cell, type '# %% [markdown]'
|
||||||
|
# %%
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import data as d
|
||||||
|
import pymongo
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
def get_pit_variable_data(apikey, competition):
|
||||||
|
client = pymongo.MongoClient(apikey)
|
||||||
|
db = client.data_processing
|
||||||
|
mdata = db.team_pit
|
||||||
|
out = {}
|
||||||
|
return mdata.find()
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
def get_pit_variable_formatted(apikey, competition):
|
||||||
|
temp = get_pit_variable_data(apikey, competition)
|
||||||
|
out = {}
|
||||||
|
for i in temp:
|
||||||
|
out[i["variable"]] = i["data"]
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
pit = get_pit_variable_formatted("mongodb+srv://api-user-new:titanscout2022@2022-scouting-4vfuu.mongodb.net/test?authSource=admin&replicaSet=2022-scouting-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true", "2020ilch")
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
fig, ax = plt.subplots(1, len(pit), sharey=True, figsize=(80,15))
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
for variable in pit:
|
||||||
|
|
||||||
|
ax[i].hist(pit[variable])
|
||||||
|
ax[i].invert_xaxis()
|
||||||
|
|
||||||
|
ax[i].set_xlabel('')
|
||||||
|
ax[i].set_ylabel('Frequency')
|
||||||
|
ax[i].set_title(variable)
|
||||||
|
|
||||||
|
plt.yticks(np.arange(len(pit[variable])))
|
||||||
|
|
||||||
|
i+=1
|
||||||
|
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user