@@ -12,7 +12,7 @@ class GraphRPCA:
1212
1313 References
1414 ----------
15- Shahid, Nauman, et al. "Fast robust PCA on graphs."
15+ Shahid, Nauman, et al. "Fast robust PCA on graphs."
1616 IEEE Journal of Selected Topics in Signal Processing 10.4 (2016): 740-756.
1717
1818 Parameters
@@ -28,7 +28,7 @@ class GraphRPCA:
2828 gamma1 : int
2929 regularizing parameter for the graph G1, constructed from the columns of D
3030 gamma2 : int
31- regularizing parameter for the graph G1, constructed from the rows of D
31+ regularizing parameter for the graph G1, constructed from the rows of D
3232 G1 : Optional[np.ndarray]
3333 graph G1, constructed from the columns of D
3434 G2 : Optional[np.ndarray]
@@ -58,7 +58,7 @@ def __init__(
5858 nbg2 : Optional [int ] = 10 ,
5959 maxIter : Optional [int ] = int (1e4 ),
6060 tol : Optional [float ] = 1e-6 ,
61- cv : Optional [int ] = 5 ,
61+ cv : Optional [int ] = 5 ,
6262 verbose : Optional [bool ] = False ,
6363 ) -> None :
6464
@@ -80,16 +80,16 @@ def __init__(
8080 self .maxIter = maxIter
8181 self .tol = tol
8282 self .verbose = verbose
83-
83+
8484 self ._prepare_data ()
8585
8686 def _prepare_data (self ) -> None :
8787 """Prepare data fot RPCA computation:
88- Transform signal to matrix if needed
89- Get the omega matrix
90- Impute the nan values if needed
88+ Transform signal to matrix if needed
89+ Get the omega matrix
90+ Impute the nan values if needed
9191 """
92-
92+
9393 self .ret = 0
9494 if (self .D is None ) and (self .period is None ):
9595 self .period = utils .get_period (self .signal )
@@ -98,7 +98,7 @@ def _prepare_data(self) -> None:
9898
9999 self .initial_D = self .D .copy ()
100100 self .initial_D_proj = utils .impute_nans (self .initial_D , method = "median" )
101-
101+
102102 self .omega = 1 - (self .D != self .D )
103103 if np .isnan (np .sum (self .D )):
104104 self .proj_D = utils .impute_nans (self .D , method = "median" )
@@ -113,15 +113,15 @@ def compute_graph_rpca(self) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
113113 Tuple[np.ndarray, np.ndarray, np.ndarray]
114114 observations, low-rank and sparse matrices
115115 """
116-
116+
117117 self .omega = 1 - (self .D != self .D )
118118 if np .isnan (np .sum (self .D )):
119119 self .proj_D = utils .impute_nans (self .D , method = "median" )
120120 else :
121121 self .proj_D = self .D
122122 if self .rank is None :
123123 self .rank = utils .approx_rank (self .proj_D )
124-
124+
125125 if self .G1 is None :
126126 self .G1 = utils .construct_graph ((self .D ).T , n_neighbors = self .nbg1 )
127127 if self .G2 is None :
@@ -148,10 +148,12 @@ def compute_graph_rpca(self) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
148148 grad_g = 2 * (self .gamma1 * Y @ laplacian1 + self .gamma2 * laplacian2 @ Y )
149149
150150 X = utils .proximal_operator (Y_past - lam * grad_g , self .D , lam )
151- t = (1 + (1 + 4 * t_past ** 2 ) ** 0.5 ) / 2
151+ t = (1 + (1 + 4 * t_past ** 2 ) ** 0.5 ) / 2
152152 Y = X + (t_past - 1 ) / t * (X - X_past )
153153
154- errors .append (np .linalg .norm (Y - Y_past , "fro" ) / np .linalg .norm (Y_past , "fro" ))
154+ errors .append (
155+ np .linalg .norm (Y - Y_past , "fro" ) / np .linalg .norm (Y_past , "fro" )
156+ )
155157 if errors [- 1 ] < self .tol :
156158 if self .verbose :
157159 print (
@@ -177,12 +179,12 @@ class GraphRPCAHyperparams(GraphRPCA):
177179 GraphRPCA : Type[GraphRPCA]
178180 [description]
179181 """
180-
182+
181183 def add_hyperparams (
182184 self ,
183185 hyperparams_gamma1 : Optional [List [float ]] = [],
184186 hyperparams_gamma2 : Optional [List [float ]] = [],
185- cv : Optional [int ] = 5 ,
187+ cv : Optional [int ] = 5 ,
186188 ) -> None :
187189 """Define the search space associated to each hyperparameter
188190
@@ -224,7 +226,7 @@ def objective(self, args):
224226 float
225227 criterion to minimise
226228 """
227-
229+
228230 self .gamma1 = args [0 ]
229231 self .gamma2 = args [1 ]
230232
@@ -244,8 +246,7 @@ def objective(self, args):
244246
245247 error = (
246248 np .linalg .norm (
247- self .initial_D [indices_x , indices_y ]
248- - W [indices_x , indices_y ],
249+ self .initial_D [indices_x , indices_y ] - W [indices_x , indices_y ],
249250 1 ,
250251 )
251252 / nb_missing
@@ -255,20 +256,22 @@ def objective(self, args):
255256
256257 if len (errors ) == 0 :
257258 print ("Warning: not converged - return default 10^10" )
258- return 10 ** 10
259+ return 10 ** 10
259260
260261 return np .mean (errors )
261-
262- def compute_graph_rpca_hyperparams (self ) -> Tuple [np .ndarray , np .ndarray , np .ndarray ]:
262+
263+ def compute_graph_rpca_hyperparams (
264+ self ,
265+ ) -> Tuple [np .ndarray , np .ndarray , np .ndarray ]:
263266 """Decompose a matrix into a low rank part and a sparse part
264- Hyperparams are set by Bayesian optimisation and cross-validation
267+ Hyperparams are set by Bayesian optimisation and cross-validation
265268
266269 Returns
267270 -------
268271 Tuple[np.ndarray, np.ndarray]
269272 the low rank matrix and the sparse matrix
270273 """
271-
274+
272275 res = skopt .gp_minimize (
273276 self .objective ,
274277 self .search_space ,
@@ -285,4 +288,4 @@ def compute_graph_rpca_hyperparams(self) -> Tuple[np.ndarray, np.ndarray, np.nda
285288 self .gamma2 = res .x [1 ]
286289 D , X , A = self .compute_graph_rpca ()
287290
288- return D , X , A
291+ return D , X , A
0 commit comments