sym_matrix_to_vec(connectivities))
// Check not fitted error
with pytest.raises(ValueError, match="has not been fitted. "):
ConnectivityMeasure().inverse_transform(vectorized_connectivities)
// Check inverse transformation
kinds.remove("tangent")
for kind in kinds:
// without vectorization: input matrices are returned with no change
conn_measure = ConnectivityMeasure(kind=kind)
connectivities = conn_measure.fit_transform(signals)
assert_array_almost_equal(
conn_measure.inverse_transform(connectivities), connectivities)
// with vectorization: input vectors are reshaped into matrices
// if diagonal has not been discarded
conn_measure = ConnectivityMeasure(kind=kind, vectorize=True)
vectorized_connectivities = conn_measure.fit_transform(signals)
assert_array_almost_equal(
conn_measure.inverse_transform(vectorized_connectivities),
connectivities)
// with vectorization if diagonal has been discarded
for kind in ["correlation", "partial correlation"]:
connectivities = ConnectivityMeasure(kind=kind).fit_transform(signals)
conn_measure = ConnectivityMeasure(kind=kind, vectorize=True,
discard_diagonal=True)
vectorized_connectivities = conn_measure.fit_transform(signals)
assert_array_almost_equal(
conn_measure.inverse_transform(vectorized_connectivities),
connectivities)
for kind in ["covariance", "precision"]:
connectivities = ConnectivityMeasure(kind=kind).fit_transform(signals)
conn_measure = ConnectivityMeasure(kind=kind, vectorize=True,
discard_diagonal=True)
vectorized_connectivities = conn_measure.fit_transform(signals)
diagonal = np.array([np.diagonal(conn) / sqrt(2) for conn in
connectivities])
inverse_transformed = conn_measure.inverse_transform(
vectorized_connectivities, diagonal=diagonal)
assert_array_almost_equal(inverse_transformed, connectivities)
with pytest.raises(ValueError,
match="can not reconstruct connectivity matrices"):
conn_measure.inverse_transform(vectorized_connectivities)
// for "tangent" kind, covariance matrices are reconstructed
// without vectorization
tangent_measure = ConnectivityMeasure(kind="tangent")
displacements = tangent_measure.fit_transform(signals)
covariances = ConnectivityMeasure(kind="covariance").fit_transform(
signals)
assert_array_almost_equal(
tangent_measure.inverse_transform(displacements), covariances)
// with vectorization
// when diagonal has not been discarded
tangent_measure = ConnectivityMeasure(kind="tangent", vectorize=True)
vectorized_displacements = tangent_measure.fit_transform(signals)
assert_array_almost_equal(
tangent_measure.inverse_transform(vectorized_displacements),
covariances)
// when diagonal has been discarded
tangent_measure = ConnectivityMeasure(kind="tangent", vectorize=True,
discard_diagonal=True)
vectorized_displacements = tangent_measure.fit_transform(signals)
diagonal = np.array([np.diagonal(matrix) / sqrt(2) for matrix in
displacements])
inverse_transformed = tangent_measure.inverse_transform(
vectorized_displacements, diagonal=diagonal)
assert_array_almost_equal(inverse_transformed, covariances)
with pytest.raises(ValueError,
match="can not reconstruct connectivity matrices"):
tangent_measure.inverse_transform(vectorized_displacements)