@@ -170,11 +170,15 @@ def update_weights(components, data_input, method=None):
170
170
stretched_components = np .zeros ((signal_length , number_of_components ))
171
171
for i , component in enumerate (components ):
172
172
stretched_components [:, i ] = component .apply_stretch (signal )[0 ]
173
- if method == ' align' :
173
+ if method == " align" :
174
174
weights = lsqnonneg (stretched_components , data_input [:, signal ])
175
175
else :
176
- weights = get_weights (stretched_components .T @ stretched_components ,
177
- - stretched_components .T @ data_input [:, signal ], 0 , 1 )
176
+ weights = get_weights (
177
+ stretched_components .T @ stretched_components ,
178
+ - stretched_components .T @ data_input [:, signal ],
179
+ 0 ,
180
+ 1 ,
181
+ )
178
182
weight_matrix [:, signal ] = weights
179
183
return weight_matrix
180
184
@@ -236,13 +240,16 @@ def initialize_arrays(number_of_components, number_of_moments, signal_length):
236
240
"""
237
241
component_matrix_guess = np .random .rand (signal_length , number_of_components )
238
242
weight_matrix_guess = np .random .rand (number_of_components , number_of_moments )
239
- stretching_matrix_guess = np .ones (number_of_components , number_of_moments ) + np .random .randn (number_of_components ,
240
- number_of_moments ) * 1e-3
243
+ stretching_matrix_guess = (
244
+ np .ones (number_of_components , number_of_moments )
245
+ + np .random .randn (number_of_components , number_of_moments ) * 1e-3
246
+ )
241
247
return component_matrix_guess , weight_matrix_guess , stretching_matrix_guess
242
248
243
249
244
- def objective_function (residual_matrix , stretching_factor_matrix , smoothness , smoothness_term , component_matrix ,
245
- sparsity ):
250
+ def objective_function (
251
+ residual_matrix , stretching_factor_matrix , smoothness , smoothness_term , component_matrix , sparsity
252
+ ):
246
253
"""Defines the objective function of the algorithm and returns its value.
247
254
248
255
Calculates the value of '(||residual_matrix||_F) ** 2 + smoothness * (||smoothness_term *
@@ -284,8 +291,11 @@ def objective_function(residual_matrix, stretching_factor_matrix, smoothness, sm
284
291
residual_matrix = np .asarray (residual_matrix )
285
292
stretching_factor_matrix = np .asarray (stretching_factor_matrix )
286
293
component_matrix = np .asarray (component_matrix )
287
- return .5 * np .linalg .norm (residual_matrix , 'fro' ) ** 2 + .5 * smoothness * np .linalg .norm (
288
- smoothness_term @ stretching_factor_matrix .T , 'fro' ) ** 2 + sparsity * np .sum (np .sqrt (component_matrix ))
294
+ return (
295
+ 0.5 * np .linalg .norm (residual_matrix , "fro" ) ** 2
296
+ + 0.5 * smoothness * np .linalg .norm (smoothness_term @ stretching_factor_matrix .T , "fro" ) ** 2
297
+ + sparsity * np .sum (np .sqrt (component_matrix ))
298
+ )
289
299
290
300
291
301
def get_stretched_component (stretching_factor , component , signal_length ):
@@ -325,11 +335,23 @@ def stretched_component_func(stretching_factor):
325
335
stretched_component_gra = derivative_func (stretching_factor )
326
336
stretched_component_hess = second_derivative_func (stretching_factor )
327
337
328
- return np .asarray (stretched_component ), np .asarray (stretched_component_gra ), np .asarray (stretched_component_hess )
329
-
330
-
331
- def update_weights_matrix (component_amount , signal_length , stretching_factor_matrix , component_matrix , data_input ,
332
- moment_amount , weights_matrix , method ):
338
+ return (
339
+ np .asarray (stretched_component ),
340
+ np .asarray (stretched_component_gra ),
341
+ np .asarray (stretched_component_hess ),
342
+ )
343
+
344
+
345
+ def update_weights_matrix (
346
+ component_amount ,
347
+ signal_length ,
348
+ stretching_factor_matrix ,
349
+ component_matrix ,
350
+ data_input ,
351
+ moment_amount ,
352
+ weights_matrix ,
353
+ method ,
354
+ ):
333
355
"""Update the weight factors matrix.
334
356
335
357
Parameters
@@ -376,21 +398,25 @@ def update_weights_matrix(component_amount, signal_length, stretching_factor_mat
376
398
for i in range (moment_amount ):
377
399
stretched_components = np .zeros ((signal_length , component_amount ))
378
400
for n in range (component_amount ):
379
- stretched_components [:, n ] = get_stretched_component (stretching_factor_matrix [n , i ], component_matrix [:, n ],
380
- signal_length )[0 ]
381
- if method == 'align' :
401
+ stretched_components [:, n ] = get_stretched_component (
402
+ stretching_factor_matrix [n , i ], component_matrix [:, n ], signal_length
403
+ )[0 ]
404
+ if method == "align" :
382
405
weight = lsqnonneg (stretched_components [0 :signal_length , :], data_input [0 :signal_length , i ])
383
406
else :
384
407
weight = get_weights (
385
408
stretched_components [0 :signal_length , :].T @ stretched_components [0 :signal_length , :],
386
409
- 1 * stretched_components [0 :signal_length , :].T @ data_input [0 :signal_length , i ],
387
- 0 , 1 )
410
+ 0 ,
411
+ 1 ,
412
+ )
388
413
weights_matrix [:, i ] = weight
389
414
return weights_matrix
390
415
391
416
392
- def get_residual_matrix (component_matrix , weights_matrix , stretching_matrix , data_input , moment_amount ,
393
- component_amount , signal_length ):
417
+ def get_residual_matrix (
418
+ component_matrix , weights_matrix , stretching_matrix , data_input , moment_amount , component_amount , signal_length
419
+ ):
394
420
"""Obtains the residual matrix between the experimental data and calculated data
395
421
396
422
Calculates the difference between the experimental data and the reconstructed experimental data created from the
@@ -442,9 +468,11 @@ def get_residual_matrix(component_matrix, weights_matrix, stretching_matrix, dat
442
468
for m in range (moment_amount ):
443
469
residual = residual_matrx [:, m ]
444
470
for k in range (component_amount ):
445
- residual = residual + weights_matrix [k , m ] * get_stretched_component (stretching_matrix [k , m ],
446
- component_matrix [:, k ], signal_length )[
447
- 0 ]
471
+ residual = (
472
+ residual
473
+ + weights_matrix [k , m ]
474
+ * get_stretched_component (stretching_matrix [k , m ], component_matrix [:, k ], signal_length )[0 ]
475
+ )
448
476
residual_matrx [:, m ] = residual
449
477
return residual_matrx
450
478
0 commit comments