Skip to content

Commit b8b3717

Browse files
author
niharika2999
committed
Few changes to gqr and region_optimal which has the radius constraints
1 parent 3484e08 commit b8b3717

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

examples/region_optimal.ipynb

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 358,
5+
"execution_count": 11,
66
"metadata": {
77
"ExecuteTime": {
88
"end_time": "2022-07-10T04:22:04.386599Z",
@@ -26,7 +26,7 @@
2626
},
2727
{
2828
"cell_type": "code",
29-
"execution_count": 359,
29+
"execution_count": 12,
3030
"metadata": {
3131
"ExecuteTime": {
3232
"end_time": "2022-07-10T04:22:07.391526Z",
@@ -66,7 +66,7 @@
6666
},
6767
{
6868
"cell_type": "code",
69-
"execution_count": 360,
69+
"execution_count": 13,
7070
"metadata": {
7171
"ExecuteTime": {
7272
"end_time": "2022-07-10T04:22:09.785781Z",
@@ -92,7 +92,7 @@
9292
},
9393
{
9494
"cell_type": "code",
95-
"execution_count": 361,
95+
"execution_count": 14,
9696
"metadata": {
9797
"ExecuteTime": {
9898
"end_time": "2022-07-10T04:22:10.835009Z",
@@ -117,7 +117,7 @@
117117
},
118118
{
119119
"cell_type": "code",
120-
"execution_count": 362,
120+
"execution_count": 15,
121121
"metadata": {
122122
"ExecuteTime": {
123123
"end_time": "2022-07-10T04:22:11.651751Z",
@@ -173,7 +173,7 @@
173173
},
174174
{
175175
"cell_type": "code",
176-
"execution_count": 364,
176+
"execution_count": 16,
177177
"metadata": {},
178178
"outputs": [],
179179
"source": [
@@ -204,7 +204,7 @@
204204
},
205205
{
206206
"cell_type": "code",
207-
"execution_count": 365,
207+
"execution_count": 17,
208208
"metadata": {},
209209
"outputs": [],
210210
"source": [
@@ -221,7 +221,7 @@
221221
},
222222
{
223223
"cell_type": "code",
224-
"execution_count": 366,
224+
"execution_count": 18,
225225
"metadata": {
226226
"ExecuteTime": {
227227
"end_time": "2022-07-10T04:22:22.713344Z",
@@ -304,10 +304,14 @@
304304
"\n",
305305
" for j in range(k):\n",
306306
" r = R[j:, j:]\n",
307-
" # Norm of each column\n",
307+
" # Norm of each column \n",
308308
" dlens = np.sqrt(np.sum(np.abs(r) ** 2, axis=0))\n",
309-
" dlens_updated = f_region_distance_constraints(self.radius,self._nx,self._ny,self.all_sensorloc,dlens,p,j,self.nSensors) #Handling constrained region sensor placement problem\n",
310-
" #print(dlens_updated)\n",
309+
" if j != 0:\n",
310+
" dlens_old = dlens_updated\n",
311+
" else:\n",
312+
" dlens_old = dlens\n",
313+
" dlens_updated = f_region_distance_constraints(self.radius,self._nx,self._ny,self.all_sensorloc,dlens_old,p,j,self.nSensors) #Handling constrained region sensor placement problem\n",
314+
" print(dlens_updated)\n",
311315
" # Choose pivot\n",
312316
" i_piv = np.argmax(dlens_updated)\n",
313317
" \n",
@@ -398,14 +402,36 @@
398402
},
399403
{
400404
"cell_type": "code",
401-
"execution_count": 368,
405+
"execution_count": 19,
402406
"metadata": {
403407
"ExecuteTime": {
404408
"end_time": "2022-07-10T04:22:27.911973Z",
405409
"start_time": "2022-07-10T04:22:26.180620Z"
406410
}
407411
},
408-
"outputs": [],
412+
"outputs": [
413+
{
414+
"name": "stdout",
415+
"output_type": "stream",
416+
"text": [
417+
"[3.6017013 3.6861243 3.727096 ... 3.9694474 3.9748106 3.9125938]\n"
418+
]
419+
},
420+
{
421+
"ename": "IndexError",
422+
"evalue": "boolean index did not match indexed array along dimension 0; dimension is 4096 but corresponding boolean dimension is 4095",
423+
"output_type": "error",
424+
"traceback": [
425+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
426+
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
427+
"\u001b[1;32m/Users/karnn/projects/pysensors/examples/region_optimal.ipynb Cell 11'\u001b[0m in \u001b[0;36m<cell line: 4>\u001b[0;34m()\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000010?line=1'>2</a>\u001b[0m optimizer1 \u001b[39m=\u001b[39m AQR(n_sensors,all_sensors,r,nx,ny)\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000010?line=2'>3</a>\u001b[0m model1 \u001b[39m=\u001b[39m ps\u001b[39m.\u001b[39mSSPOR(optimizer \u001b[39m=\u001b[39m optimizer1, n_sensors \u001b[39m=\u001b[39m n_sensors)\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000010?line=3'>4</a>\u001b[0m model1\u001b[39m.\u001b[39;49mfit(X)\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000010?line=4'>5</a>\u001b[0m all_sensors1 \u001b[39m=\u001b[39m model1\u001b[39m.\u001b[39mget_all_sensors()\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000010?line=6'>7</a>\u001b[0m top_sensors \u001b[39m=\u001b[39m model1\u001b[39m.\u001b[39mget_selected_sensors()\n",
428+
"File \u001b[0;32m~/projects/pysensors/pysensors/reconstruction/_sspor.py:156\u001b[0m, in \u001b[0;36mSSPOR.fit\u001b[0;34m(self, x, quiet, prefit_basis, seed, **optimizer_kws)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_validate_n_sensors()\n\u001b[1;32m 155\u001b[0m \u001b[39m# Find sparse sensor locations\u001b[39;00m\n\u001b[0;32m--> 156\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mranked_sensors_ \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49moptimizer\u001b[39m.\u001b[39;49mfit(\n\u001b[1;32m 157\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mbasis_matrix_, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49moptimizer_kws\n\u001b[1;32m 158\u001b[0m )\u001b[39m.\u001b[39mget_sensors()\n\u001b[1;32m 160\u001b[0m \u001b[39m# Randomly shuffle sensors after self.basis.n_basis_modes\u001b[39;00m\n\u001b[1;32m 161\u001b[0m rng \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39mrandom\u001b[39m.\u001b[39mdefault_rng(seed)\n",
429+
"\u001b[1;32m/Users/karnn/projects/pysensors/examples/region_optimal.ipynb Cell 9'\u001b[0m in \u001b[0;36mAQR.fit\u001b[0;34m(self, basis_matrix)\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000008?line=78'>79</a>\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000008?line=79'>80</a>\u001b[0m dlens_old \u001b[39m=\u001b[39m dlens\n\u001b[0;32m---> <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000008?line=80'>81</a>\u001b[0m dlens_updated \u001b[39m=\u001b[39m f_region_distance_constraints(\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mradius,\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_nx,\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_ny,\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mall_sensorloc,dlens_old,p,j,\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mnSensors) \u001b[39m#Handling constrained region sensor placement problem\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000008?line=81'>82</a>\u001b[0m \u001b[39mprint\u001b[39m(dlens_updated)\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000008?line=82'>83</a>\u001b[0m \u001b[39m# Choose pivot\u001b[39;00m\n",
430+
"\u001b[1;32m/Users/karnn/projects/pysensors/examples/region_optimal.ipynb Cell 8'\u001b[0m in \u001b[0;36mf_region_distance_constraints\u001b[0;34m(r, nx, ny, all_sensors, dlens, piv, j, n_sensors)\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000007?line=5'>6</a>\u001b[0m idx_constrained \u001b[39m=\u001b[39m distance_constraints_indices(r,nx,ny,piv,n_sensors,j)\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000007?line=6'>7</a>\u001b[0m didx \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39misin(piv[j:],idx_constrained,invert\u001b[39m=\u001b[39m\u001b[39mFalse\u001b[39;00m)\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000007?line=7'>8</a>\u001b[0m dlens[didx] \u001b[39m=\u001b[39m \u001b[39m0\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karnn/projects/pysensors/examples/region_optimal.ipynb#ch0000007?line=8'>9</a>\u001b[0m \u001b[39mreturn\u001b[39;00m dlens\n",
431+
"\u001b[0;31mIndexError\u001b[0m: boolean index did not match indexed array along dimension 0; dimension is 4096 but corresponding boolean dimension is 4095"
432+
]
433+
}
434+
],
409435
"source": [
410436
"r = 7\n",
411437
"optimizer1 = AQR(n_sensors,all_sensors,r,nx,ny)\n",

pysensors/optimizers/_gqr.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,23 @@ def plot_gallery(title, images, n_col=n_col, n_row=n_row, cmap=plt.cm.gray):
241241
# plot_gallery("First few centered faces", X[:n_components])
242242

243243
#Find all sensor locations using built in QR optimizer
244-
max_const_sensors = 230
245-
n_const_sensors = 2
246-
n_sensors = 200
244+
#max_const_sensors = 230
245+
n_const_sensors = 3
246+
n_sensors = 10
247+
n_modes = 11
248+
basis = ps.basis.SVD(n_basis_modes=n_modes)
247249
optimizer = ps.optimizers.QR()
248-
model = ps.SSPOR(optimizer=optimizer, n_sensors=n_sensors)
250+
model = ps.SSPOR(basis = basis, optimizer=optimizer, n_sensors=n_sensors)
249251
model.fit(X)
250252

251253
all_sensors = model.get_all_sensors()
252254

253255
##Constrained sensor location on the grid:
254-
xmin = 20
255-
xmax = 40
256-
ymin = 25
257-
ymax = 45
256+
xmin = 0
257+
xmax = 64
258+
ymin = 10
259+
ymax = 30
258260
sensors_constrained = ps.utils._constraints.get_constraind_sensors_indices(xmin,xmax,ymin,ymax,nx,ny,all_sensors) #Constrained column indices
259-
260261
# didx = np.isin(all_sensors,sensors_constrained,invert=False)
261262
# const_index = np.nonzero(didx)
262263
# j =
@@ -277,7 +278,7 @@ def plot_gallery(title, images, n_col=n_col, n_row=n_row, cmap=plt.cm.gray):
277278

278279
## Fit the dataset with the optimizer GQR
279280
optimizer1 = GQR(sensors_constrained,n_sensors,n_const_sensors,all_sensors, constraint_option = "exact_n_const_sensors")
280-
model1 = ps.SSPOR(optimizer = optimizer1, n_sensors = n_sensors)
281+
model1 = ps.SSPOR(basis = basis, optimizer = optimizer1, n_sensors = n_sensors)
281282
model1.fit(X)
282283
all_sensors1 = model1.get_all_sensors()
283284

0 commit comments

Comments
 (0)