3
3
import scipy as sp
4
4
import torch
5
5
6
+ from thoi .typing import TensorLikeArray
7
+
6
8
7
9
def _get_string_metric (batched_res : np .ndarray , metric :str ):
8
10
'''
@@ -60,16 +62,15 @@ def _to_numpy(X):
60
62
return X .detach ().cpu ().numpy ()
61
63
elif isinstance (X , np .ndarray ):
62
64
return X
63
- else :
64
- raise TypeError (f"Unsupported type: { type (X )} " )
65
+ return np .array (X )
65
66
66
67
def _get_device (use_cpu :bool = False ):
67
68
"""Set the use of GPU if available"""
68
69
using_GPU = torch .cuda .is_available () and not use_cpu
69
70
device = torch .device ('cuda' if using_GPU else 'cpu' )
70
71
return device
71
72
72
- def _normalize_input_data (X : Union [ np . ndarray , torch . Tensor , List [ np . ndarray ], List [ torch . Tensor ]] ,
73
+ def _normalize_input_data (X : TensorLikeArray ,
73
74
covmat_precomputed : bool = False ,
74
75
T : Optional [Union [int , List [int ]]]= None ,
75
76
use_cpu : bool = False ):
@@ -88,30 +89,21 @@ def _normalize_input_data(X: Union[np.ndarray, torch.Tensor, List[np.ndarray], L
88
89
89
90
# Handle different options for X parameter. Accept multivariate data or covariance matrix
90
91
if covmat_precomputed :
91
-
92
- if isinstance (X , (np .ndarray , torch .Tensor )):
93
- assert X .shape [- 2 ] == X .shape [- 1 ], 'Covariance matrix should be square'
94
- assert len (X .shape ) in [2 , 3 ], 'Covariance matrix should have dimensions (N, N) or (D, N, N)'
95
- covmats = torch .as_tensor (X )
96
- covmats = covmats .unsqueeze (0 ) if len (covmats .shape ) == 2 else covmats
97
- else :
98
- assert all ([len (x .shape ) == 2 for x in X ]), 'All covariance matrices should have dimensions (N, N)'
99
- assert all ([x .shape [0 ] == x .shape [1 ] == X [0 ].shape [0 ] for x in X ]), 'All covariance matrices should have same dimensions (N, N)'
100
- covmats = torch .stack ([torch .as_tensor (x ) for x in X ])
92
+ covmats = torch .as_tensor (X )
93
+ covmats = covmats .unsqueeze (0 ) if len (covmats .shape ) == 2 else covmats
94
+ assert X .shape [- 2 ] == X .shape [- 1 ], 'Covariance matrix should be square'
95
+ assert len (X .shape ) == 3 , 'Covariance matrix should have dimensions (N, N) or (D, N, N)'
101
96
else :
102
97
103
- if isinstance ( X , ( np . ndarray , torch . Tensor )) :
98
+ try :
104
99
X = _to_numpy (X )
105
100
assert len (X .shape ) in [2 , 3 ], 'Covariance matrix should have dimensions (T, N) or (D, T, N)'
106
- if len (X .shape ) == 2 :
107
- X = [X ]
108
- else :
109
- X = [X [i ] for i in range (X .shape [0 ])]
110
- else :
101
+ X = [X ] if len (X .shape ) == 2 else [X [i ] for i in range (X .shape [0 ])]
102
+ except :
103
+ X = [_to_numpy (x ) for x in X ]
111
104
assert all ([len (x .shape ) == 2 for x in X ]), 'All multivariate series should have dimensions (T, N) where T my vary and N be constant across all series'
112
105
assert all ([x .shape [1 ] == X [0 ].shape [1 ] for x in X ]), 'All multivariate series should have dimensions (T, N) where T my vary and N be constant across all series'
113
- X = [_to_numpy (x ) for x in X ]
114
-
106
+
115
107
covmats = torch .stack ([torch .from_numpy (gaussian_copula_covmat (x )) for x in X ])
116
108
T = [x .shape [0 ] for x in X ]
117
109
0 commit comments