-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeights.py
88 lines (76 loc) · 2.9 KB
/
Weights.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import math
import numpy as np
import json
from Exceptions import *
settingsJson = None
with open("settings.json") as settings:
settingsJson = json.load(settings)["blend_settings"]
# This function will return an list of value, like below:
# [0,1,2,3,...,n] -> [a,...,b]
def scaleRange(n,a,b):
return [(x*(b-a)/(n-1))+a for x in range(0,n)]
def equal(n):
return [1/n]*n
def gauss(n, c, bound):
r = scaleRange(n, bound[0], bound[1])
val = [math.exp(-((x)**2)/(2*(c**2))) for x in r]
val = val/np.sum(val)
return val
def gauss_sym(n, c, bound):
r = scaleRange(n, -np.amax(np.abs(bound)), np.amax(np.abs(bound)))
val = [math.exp(-((x)**2)/(2*(c**2))) for x in r]
val = val/np.sum(val)
return val
def pyramid(n, reverse):
val = []
if reverse:
val = [x for x in range(n,0,-1)]
else:
val = [x for x in range(1,n+1)]
val = val/np.sum(val)
return val
def pyramid_sym(n):
r = range(0,n)
val = [((n-1)/2-abs(x-((n-1)/2))+1) for x in r]
val = val/np.sum(val)
return val
def funcEval(func,nums):
try:
return eval(f"[({func}) for x in nums]")
except NameError as e:
raise InvalidCustomWeighting
def custom(n, func="", bound=(0,1)):
r = scaleRange(n, bound[0], bound[1])
val = funcEval(func, r)
if np.amin(val) < 0: val -= np.amin(val)
val = val/np.sum(val)
return val
# This function will stretch the given array (weights) to a specific length (n)
# Example : n = 10, weights = [1,2]
# Result : val = [1,1,1,1,1,2,2,2,2,2], then normalize it to [0.0667, 0.0667, 0.0667, 0.0667, 0.0667, 0.1333, 0.1333, 0.1333, 0.1333, 0.1333]
def divide(n,weights):
r = scaleRange(n,0,len(weights)-0.1)
val = []
idx = [math.floor(x) for x in r]
for x in range(0,n):
index = int(idx[x])
val.append(weights[index])
if np.amin(val) < 0: val -= np.amin(val)
val = (val/np.sum(val))
return val
def weight(mode,count):
if count == 1:
return [1.0] # If only one frame is weighted, it's weight is always going to be 1.
else:
try:
return {
"EQUAL" : equal(count),
"GAUSSIAN" : gauss(count, c=settingsJson['gaussian']['standard_deviation'], bound=settingsJson['gaussian']['bound']),
"GAUSSIAN_SYM" : gauss_sym(count, c=settingsJson['gaussian']['standard_deviation'], bound=settingsJson['gaussian']['bound']),
"PYRAMID" : pyramid(count, reverse=settingsJson['pyramid']['reverse']),
"PYRAMID_SYM" : pyramid_sym(count),
"CUSTOM_FUNCTION": custom(count, func=settingsJson['custom_function']['function'], bound=settingsJson['custom_function']['bound']),
"CUSTOM_WEIGHT" : divide(count, weights=settingsJson['custom_weight']['weight'])
}[mode.upper()]
except KeyError:
raise InvalidBlendMode