-
Notifications
You must be signed in to change notification settings - Fork 88
/
perturbations.py
66 lines (51 loc) · 2.16 KB
/
perturbations.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
from __future__ import print_function
from builtins import zip
from builtins import range
import itertools
import numpy as np
def possibilities(umin, umax, Amount):
"""This function is to calculate all possible perturbations of a maximum and
minimum of a vector of parameters"""
def box_ready(umin, umax, Amount):
"""Create a suitable matrix for the mesh function"""
return list(zip(umin, umax, [Amount]*len(umin)))
def coords(box):
return [entry[:2] for entry in box]
def internalmesh(box):
"""Generate internal points linearly spaced inside of a box"""
return itertools.product(*[np.linspace(*b) for b in box])
def surfmesh_slow(box):
"""Generate points on the edges of a box by generating an internal
grid and then selecting the points on the outside"""
return (point for point in internalmesh(box)
if any(p in minmax for p, minmax in zip(point, coords(box))))
def surfmesh(box):
"""Generate points on the edges of a box"""
Ndims = len(box)
dimindex = list(range(Ndims))
# have at least one constrained dimension
for Nunconstrained in range(0, Ndims):
# select all ways of generating this
for dims in itertools.combinations(dimindex, Nunconstrained):
pointbase = coords(box)
for i in dims:
pointbase[i] = np.linspace(*box[i])[1:-1]
for coord in itertools.product(*pointbase):
yield coord
box = box_ready(umin, umax, Amount)
return np.array(list(surfmesh(box)))
if __name__ == '__main__':
#for example a matrix of the minimum and maximum values of a certain set of parameters
umin = [0, 0, 10]
umax = [1, 1, 20]
# Create a matrix of all possibilities of the umin and umax vectors
# The first entry of the matrix correspondse to the first entry in
# The minimum and maximum matrices
possible = possibilities(umin, umax, 3)
print(possible)
print(possible.shape[0])
umin = np.ones(20)
umax = 2*np.random.random(20)
print(umax)
print(umin)
print(possibilities(umin, umax, 1))