4646import time
4747
4848import numpy as np
49+ from scipy .linalg import inv
4950from scipy .ndimage import binary_dilation , generate_binary_structure , iterate_structure
5051
5152from pysteps import cascade
@@ -1740,7 +1741,7 @@ def calculate_weights_spn(correlations, cov):
17401741 if isinstance (cov , type (None )):
17411742 raise ValueError ("cov must contain a covariance matrix" )
17421743 else :
1743- # Make a numpy matrix out of cov and get the inverse
1744+ # Make a numpy array out of cov and get the inverse
17441745 cov = np .where (cov == 0.0 , 10e-5 , cov )
17451746 # Make sure the determinant of the matrix is not zero, otherwise
17461747 # subtract 10e-5 from the cross-correlations between the models
@@ -1749,26 +1750,30 @@ def calculate_weights_spn(correlations, cov):
17491750 # Ensure the correlation of the model with itself is always 1.0
17501751 for i , _ in enumerate (cov ):
17511752 cov [i ][i ] = 1.0
1752- # Make a numpy matrix out of the array
1753- cov_matrix = np .asmatrix (cov )
1754- # Get the inverse of the matrix
1755- cov_matrix_inv = cov_matrix .getI ()
1756- # The component weights are the dot product between cov_matrix_inv
1757- # and cor_vec
1758- weights = cov_matrix_inv .dot (correlations )
1753+ # Use a numpy array instead of a matrix
1754+ cov_matrix = np .array (cov )
1755+ # Get the inverse of the matrix using scipy's inv function
1756+ cov_matrix_inv = inv (cov_matrix )
1757+ # The component weights are the dot product between cov_matrix_inv and cor_vec
1758+ weights = np .dot (cov_matrix_inv , correlations )
17591759 weights = np .nan_to_num (
17601760 weights , copy = True , nan = 10e-5 , posinf = 10e-5 , neginf = 10e-5
17611761 )
1762+ weights_dot_correlations = np .dot (weights , correlations )
17621763 # If the dot product of the weights with the correlations is
17631764 # larger than 1.0, we assign a weight of 0.0 to the noise (to make
17641765 # it numerically stable)
1765- if weights . dot ( correlations ) > 1.0 :
1766+ if weights_dot_correlations > 1.0 :
17661767 noise_weight = np .array ([0 ])
17671768 # Calculate the noise weight
17681769 else :
1769- noise_weight = np .asarray (np .sqrt (1.0 - weights .dot (correlations )))[0 ]
1770+ noise_weight = np .sqrt (1.0 - weights_dot_correlations )
1771+ # Convert weights to a 1D array
1772+ weights = np .array (weights ).flatten ()
1773+ # Ensure noise_weight is a 1D array before concatenation
1774+ noise_weight = np .array (noise_weight ).flatten ()
17701775 # Finally, add the noise_weights to the weights variable.
1771- weights = np .concatenate ((np . array ( weights )[ 0 ] , noise_weight ), axis = 0 )
1776+ weights = np .concatenate ((weights , noise_weight ), axis = 0 )
17721777
17731778 # Otherwise, the weight equals the correlation on that scale level and
17741779 # the noise component weight equals 1 - this weight. This only occurs for
0 commit comments