@@ -170,11 +170,15 @@ def update_weights(components, data_input, method=None):
170170 stretched_components = np .zeros ((signal_length , number_of_components ))
171171 for i , component in enumerate (components ):
172172 stretched_components [:, i ] = component .apply_stretch (signal )[0 ]
173- if method == ' align' :
173+ if method == " align" :
174174 weights = lsqnonneg (stretched_components , data_input [:, signal ])
175175 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+ )
178182 weight_matrix [:, signal ] = weights
179183 return weight_matrix
180184
@@ -236,13 +240,16 @@ def initialize_arrays(number_of_components, number_of_moments, signal_length):
236240 """
237241 component_matrix_guess = np .random .rand (signal_length , number_of_components )
238242 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+ )
241247 return component_matrix_guess , weight_matrix_guess , stretching_matrix_guess
242248
243249
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+ ):
246253 """Defines the objective function of the algorithm and returns its value.
247254
248255 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
284291 residual_matrix = np .asarray (residual_matrix )
285292 stretching_factor_matrix = np .asarray (stretching_factor_matrix )
286293 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+ )
289299
290300
291301def get_stretched_component (stretching_factor , component , signal_length ):
@@ -325,11 +335,23 @@ def stretched_component_func(stretching_factor):
325335 stretched_component_gra = derivative_func (stretching_factor )
326336 stretched_component_hess = second_derivative_func (stretching_factor )
327337
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+ ):
333355 """Update the weight factors matrix.
334356
335357 Parameters
@@ -376,21 +398,25 @@ def update_weights_matrix(component_amount, signal_length, stretching_factor_mat
376398 for i in range (moment_amount ):
377399 stretched_components = np .zeros ((signal_length , component_amount ))
378400 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" :
382405 weight = lsqnonneg (stretched_components [0 :signal_length , :], data_input [0 :signal_length , i ])
383406 else :
384407 weight = get_weights (
385408 stretched_components [0 :signal_length , :].T @ stretched_components [0 :signal_length , :],
386409 - 1 * stretched_components [0 :signal_length , :].T @ data_input [0 :signal_length , i ],
387- 0 , 1 )
410+ 0 ,
411+ 1 ,
412+ )
388413 weights_matrix [:, i ] = weight
389414 return weights_matrix
390415
391416
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+ ):
394420 """Obtains the residual matrix between the experimental data and calculated data
395421
396422 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
442468 for m in range (moment_amount ):
443469 residual = residual_matrx [:, m ]
444470 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+ )
448476 residual_matrx [:, m ] = residual
449477 return residual_matrx
450478
0 commit comments