46
46
import time
47
47
48
48
import numpy as np
49
+ from scipy .linalg import inv
49
50
from scipy .ndimage import binary_dilation , generate_binary_structure , iterate_structure
50
51
51
52
from pysteps import cascade
@@ -1740,7 +1741,7 @@ def calculate_weights_spn(correlations, cov):
1740
1741
if isinstance (cov , type (None )):
1741
1742
raise ValueError ("cov must contain a covariance matrix" )
1742
1743
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
1744
1745
cov = np .where (cov == 0.0 , 10e-5 , cov )
1745
1746
# Make sure the determinant of the matrix is not zero, otherwise
1746
1747
# subtract 10e-5 from the cross-correlations between the models
@@ -1749,26 +1750,30 @@ def calculate_weights_spn(correlations, cov):
1749
1750
# Ensure the correlation of the model with itself is always 1.0
1750
1751
for i , _ in enumerate (cov ):
1751
1752
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 )
1759
1759
weights = np .nan_to_num (
1760
1760
weights , copy = True , nan = 10e-5 , posinf = 10e-5 , neginf = 10e-5
1761
1761
)
1762
+ weights_dot_correlations = np .dot (weights , correlations )
1762
1763
# If the dot product of the weights with the correlations is
1763
1764
# larger than 1.0, we assign a weight of 0.0 to the noise (to make
1764
1765
# it numerically stable)
1765
- if weights . dot ( correlations ) > 1.0 :
1766
+ if weights_dot_correlations > 1.0 :
1766
1767
noise_weight = np .array ([0 ])
1767
1768
# Calculate the noise weight
1768
1769
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 ()
1770
1775
# 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 )
1772
1777
1773
1778
# Otherwise, the weight equals the correlation on that scale level and
1774
1779
# the noise component weight equals 1 - this weight. This only occurs for
0 commit comments