Skip to content

Commit 9f1d5e6

Browse files
committed
more cleaning, first two constraints fixed
1 parent e011bf8 commit 9f1d5e6

File tree

3 files changed

+74
-60
lines changed

3 files changed

+74
-60
lines changed

pysensors/basis/_custom.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66

77
class Custom(InvertibleBasis, MatrixMixin):
88
"""
9-
<<<<<<< HEAD
10-
Use a custom transformation to maps input features to
11-
=======
12-
Generate a custom transformation which maps input features to
13-
>>>>>>> 1ccd23fafed0ca39dac6cde204efa228d133df1c
9+
Use a custom transformation to map input features to
1410
custom modes.
1511
1612
Assumes the data has already been centered (to have mean 0).

pysensors/optimizers/_gqr.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import pysensors as ps
1212
from matplotlib.patches import Circle
13-
13+
from pysensors.utils._norm_calc import returnInstance as normCalcReturnInstance
1414

1515
class GQR(QR):
1616
"""
@@ -91,6 +91,7 @@ def fit(self,basis_matrix=None,**optimizer_kws):
9191
self.all_sensors = None
9292
if 'constraint_option' in optimizer_kws.keys():
9393
self.constraint_option = optimizer_kws['constraint_option']
94+
self._norm_calc_Instance = normCalcReturnInstance(self,self.constraint_option)
9495
else:
9596
self.constraint_option = None
9697
if 'nx' in optimizer_kws.keys():
@@ -127,34 +128,36 @@ def fit(self,basis_matrix=None,**optimizer_kws):
127128

128129
# Norm of each column
129130
dlens = np.sqrt(np.sum(np.abs(r) ** 2, axis=0))
130-
131-
if self.constraint_option == "max_n_const_sensors" :
132-
dlens_updated = ps.utils._norm_calc.norm_calc_max_n_const_sensors(self.constrainedIndices,dlens,p,j, self.nConstrainedSensors,self.all_sensorloc,self.n_sensors)
133-
i_piv = np.argmax(dlens_updated)
134-
dlen = dlens_updated[i_piv]
135-
elif self.constraint_option == "exact_n_const_sensors" :
136-
dlens_updated = ps.utils._norm_calc.norm_calc_exact_n_const_sensors(self.constrainedIndices,dlens,p,j,self.nConstrainedSensors)
137-
i_piv = np.argmax(dlens_updated)
138-
dlen = dlens_updated[i_piv]
139-
elif self.constraint_option == "predetermined_end":
140-
dlens_updated = ps.utils._norm_calc.predetermined_norm_calc(self.constrainedIndices, dlens, p, j, self.nConstrainedSensors, self.n_sensors)
141-
i_piv = np.argmax(dlens_updated)
142-
dlen = dlens_updated[i_piv]
143-
elif self.constraint_option == "radii_constraints":
144-
145-
if j == 0:
146-
i_piv = np.argmax(dlens)
147-
dlen = dlens[i_piv]
148-
dlens_old = dlens
149-
else:
150-
151-
dlens_updated = ps.utils._norm_calc.f_radii_constraint(j,dlens,dlens_old,p,self._nx,self._ny,self._r,self.all_sensorloc, self.n_sensors) #( self.radius,self._nx,self._ny,self.all_sensorloc,dlens,p,j)
152-
i_piv = np.argmax(dlens_updated)
153-
dlen = dlens_updated[i_piv]
154-
dlens_old = dlens_updated
155-
else:
156-
i_piv = np.argmax(dlens)
157-
dlen = dlens[i_piv]
131+
dlens_updated = self._norm_calc_Instance(self.constrainedIndices, dlens, p, j, self.nConstrainedSensors, all_sensors=self.all_sensors, n_sensors=self.n_sensors, nx=self._nx, ny=self._ny, r=self._r)
132+
i_piv = np.argmax(dlens_updated)
133+
dlen = dlens_updated[i_piv]
134+
# if self.constraint_option == "max_n_const_sensors" :
135+
# dlens_updated = ps.utils._norm_calc.norm_calc_max_n_const_sensors(self.constrainedIndices,dlens,p,j, self.nConstrainedSensors,self.all_sensorloc,self.n_sensors)
136+
# i_piv = np.argmax(dlens_updated)
137+
# dlen = dlens_updated[i_piv]
138+
# elif self.constraint_option == "exact_n_const_sensors" :
139+
# dlens_updated = ps.utils._norm_calc.norm_calc_exact_n_const_sensors(self.constrainedIndices,dlens,p,j,self.nConstrainedSensors)
140+
# i_piv = np.argmax(dlens_updated)
141+
# dlen = dlens_updated[i_piv]
142+
# elif self.constraint_option == "predetermined_end":
143+
# dlens_updated = ps.utils._norm_calc.predetermined_norm_calc(self.constrainedIndices, dlens, p, j, self.nConstrainedSensors, self.n_sensors)
144+
# i_piv = np.argmax(dlens_updated)
145+
# dlen = dlens_updated[i_piv]
146+
# elif self.constraint_option == "radii_constraints":
147+
148+
# if j == 0:
149+
# i_piv = np.argmax(dlens)
150+
# dlen = dlens[i_piv]
151+
# dlens_old = dlens
152+
# else:
153+
154+
# dlens_updated = ps.utils._norm_calc.f_radii_constraint(j,dlens,dlens_old,p,self._nx,self._ny,self._r,self.all_sensorloc, self.n_sensors) #( self.radius,self._nx,self._ny,self.all_sensorloc,dlens,p,j)
155+
# i_piv = np.argmax(dlens_updated)
156+
# dlen = dlens_updated[i_piv]
157+
# dlens_old = dlens_updated
158+
# else:
159+
# i_piv = np.argmax(dlens)
160+
# dlen = dlens[i_piv]
158161

159162
# Choose pivot
160163
# i_piv = np.argmax(dlens_updated)

pysensors/utils/_norm_calc.py

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import numpy as np
66

7-
def norm_calc_exact_n_const_sensors(lin_idx, dlens, piv, j, n_const_sensors): ##Will first force sensors into constrained region
8-
#num_sensors should be fixed for each custom constraint (for now)
9-
#num_sensors must be <= size of constraint region
7+
def norm_calc_exact_n_const_sensors(lin_idx, dlens, piv, j, n_const_sensors, **kwargs): ##Will first force sensors into constrained region
8+
# num_sensors should be fixed for each custom constraint (for now)
9+
# num_sensors must be <= size of constraint region
1010
"""
1111
Function for mapping constrained sensor locations with the QR procedure.
1212
@@ -27,18 +27,11 @@ def norm_calc_exact_n_const_sensors(lin_idx, dlens, piv, j, n_const_sensors): ##
2727
-------
2828
dlens : np.darray, shape [Variable based on j] with constraints mapped into it.
2929
"""
30-
if j < n_const_sensors: # force sensors into constraint region
31-
#idx = np.arange(dlens.shape[0])
32-
#dlens[np.delete(idx, lin_idx)] = 0
33-
34-
didx = np.isin(piv[j:],lin_idx,invert=True)
35-
dlens[didx] = 0
36-
else:
37-
didx = np.isin(piv[j:],lin_idx,invert=False)
38-
dlens[didx] = 0
30+
didx = np.isin(piv[j:],lin_idx,invert=j<n_const_sensors)
31+
dlens[didx] = 0
3932
return dlens
4033

41-
def norm_calc_max_n_const_sensors(lin_idx, dlens, piv, j, const_sensors,all_sensors,n_sensors): ##Optimal sensor placement with constraints (will place sensors in the order of QR)
34+
def norm_calc_max_n_const_sensors(lin_idx, dlens, piv, j, n_const_sensors, **kwargs):
4235
"""
4336
Function for mapping constrained sensor locations with the QR procedure (Optimally).
4437
@@ -63,21 +56,29 @@ def norm_calc_max_n_const_sensors(lin_idx, dlens, piv, j, const_sensors,all_sens
6356
-------
6457
dlens : np.darray, shape [Variable based on j] with constraints mapped into it.
6558
"""
59+
if 'all_sensors' in kwargs.keys():
60+
all_sensors = kwargs['all_sensors']
61+
else:
62+
all_sensors = []
63+
if 'n_sensors' in kwargs.keys():
64+
n_sensors = kwargs['n_sensors']
65+
else:
66+
n_sensors = len(all_sensors)
6667
counter = 0
6768
mask = np.isin(all_sensors,lin_idx,invert=False)
6869
const_idx = all_sensors[mask]
69-
updated_lin_idx = const_idx[const_sensors:]
70+
updated_lin_idx = const_idx[n_const_sensors:]
7071
for i in range(n_sensors):
7172
if np.isin(all_sensors[i],lin_idx,invert=False):
7273
counter += 1
73-
if counter < const_sensors:
74+
if counter < n_const_sensors:
7475
dlens = dlens
7576
else:
7677
didx = np.isin(piv[j:],updated_lin_idx,invert=False)
7778
dlens[didx] = 0
7879
return dlens
7980

80-
def predetermined_norm_calc(lin_idx, dlens, piv, j, n_const_sensors, n_sensors):
81+
def predetermined_norm_calc(lin_idx, dlens, piv, j, n_const_sensors, **kwargs):
8182
"""
8283
Function for mapping constrained sensor locations with the QR procedure.
8384
@@ -98,15 +99,13 @@ def predetermined_norm_calc(lin_idx, dlens, piv, j, n_const_sensors, n_sensors):
9899
-------
99100
dlens : np.darray, shape [Variable based on j] with constraints mapped into it.
100101
"""
101-
if (n_sensors - n_const_sensors) <= j <= n_sensors: # force sensors into constraint region
102-
#idx = np.arange(dlens.shape[0])
103-
#dlens[np.delete(idx, lin_idx)] = 0
104-
105-
didx = np.isin(piv[j:],lin_idx,invert=True)
106-
dlens[didx] = 0
102+
if 'n_sensors' in kwargs.keys():
103+
n_sensors = kwargs['n_sensors']
107104
else:
108-
didx = np.isin(piv[j:],lin_idx,invert=False)
109-
dlens[didx] = 0
105+
raise ValueError ('total number of sensors is not given!')
106+
107+
didx = np.isin(piv[j:],lin_idx,invert=(n_sensors - n_const_sensors) <= j <= n_sensors)
108+
dlens[didx] = 0
110109
return dlens
111110

112111
def f_radii_constraint(j,dlens,dlens_old,piv,nx,ny,r, all_sensors, n_sensors):
@@ -122,7 +121,7 @@ def f_radii_constraint(j,dlens,dlens_old,piv,nx,ny,r, all_sensors, n_sensors):
122121
result_list = [x + (j-1) for x in result_list]
123122
result_array = np.array(result_list)
124123
print(result_array)
125-
124+
126125
idx_constrained1 = get_constraind_sensors_indices_radii(j,piv,r, nx,ny, all_sensors)
127126
t = np.concatenate((idx_constrained1,result_array), axis = 0)
128127
didx = np.isin(piv[j:],t,invert= False)
@@ -166,4 +165,20 @@ def get_constraind_sensors_indices_radii(j,piv,r, nx,ny, all_sensors):
166165
else:
167166
idx_constrained = np.ravel_multi_index(constrained_sensors_tuple, (nx,ny))
168167
return idx_constrained
169-
168+
169+
170+
__norm_calc_type = {}
171+
__norm_calc_type['exact_n_const_sensors'] = norm_calc_exact_n_const_sensors
172+
__norm_calc_type['max_n_const_sensors'] = norm_calc_max_n_const_sensors
173+
__norm_calc_type['predetermined_norm_calc'] = predetermined_norm_calc
174+
175+
def returnInstance(cls, name):
176+
"""
177+
Method designed to return class instance:
178+
@ In, cls, class type
179+
@ In, name, string, name of class
180+
@ Out, __crossovers[name], instance of class
181+
"""
182+
if name not in __norm_calc_type:
183+
cls.raiseAnError (IOError, "{} NOT IMPLEMENTED!!!!!".format(name))
184+
return __norm_calc_type[name]

0 commit comments

Comments
 (0)