Developer documentation
Version 3.0.3-105-gd3941f44
connectome.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __connectome_connectome_h__
18#define __connectome_connectome_h__
19
20
21#include <Eigen/Dense>
22
23#include "app.h"
24#include "exception.h"
25#include "header.h"
26#include "mrtrix.h"
27#include "types.h"
28
29
30namespace MR {
31 namespace Connectome {
32
33
34 using node_t = uint32_t;
35
37 using matrix_type = Eigen::Array<value_type, Eigen::Dynamic, Eigen::Dynamic>;
38 using vector_type = Eigen::Array<value_type, Eigen::Dynamic, 1>;
39 using mask_type = Eigen::Array<bool, Eigen::Dynamic, Eigen::Dynamic>;
40
41
42
43 extern const App::OptionGroup MatrixOutputOptions;
44
45
46
47 template <class MatrixType>
48 void check (const MatrixType& in, const node_t num_nodes = 0)
49 {
50 if (in.rows() != in.cols())
51 throw Exception ("Connectome matrix is not square (" + str(in.rows()) + " x " + str(in.cols()) + ")");
52 if (num_nodes && (in.rows() != num_nodes))
53 throw Exception ("Connectome matrix contains " + str(in.rows()) + " nodes; expected " + str(num_nodes));
54 }
55
56
57
58 template <class MatrixType>
59 bool is_directed (MatrixType& in)
60 {
61 if (in.rows() != in.cols())
62 throw Exception ("Connectome matrix is not square (" + str(in.rows()) + " x " + str(in.cols()) + ")");
63
64 for (node_t row = 0; row != in.rows(); ++row) {
65 for (node_t col = row+1; col != in.cols(); ++col) {
66
67 const typename MatrixType::Scalar lower_value = in (col, row);
68 const typename MatrixType::Scalar upper_value = in (row, col);
69
70 if (upper_value && lower_value && (upper_value != lower_value))
71 return true;
72
73 } }
74
75 return false;
76 }
77
78
79
80 template <class MatrixType>
81 void to_symmetric (MatrixType& in)
82 {
83 if (is_directed (in))
84 throw Exception ("Cannot convert a non-symmetric directed matrix to be symmetric");
85
86 for (node_t row = 0; row != in.rows(); ++row) {
87 for (node_t col = row+1; col != in.cols(); ++col) {
88
89 const typename MatrixType::Scalar lower_value = in (col, row);
90 const typename MatrixType::Scalar upper_value = in (row, col);
91
92 if (upper_value && !lower_value)
93 in (row, col) = in (col, row) = upper_value;
94 else if (lower_value && !upper_value)
95 in (row, col) = in (col, row) = lower_value;
96
97 } }
98 }
99
100
101
102 template <class MatrixType>
103 void to_upper (MatrixType& in)
104 {
105 if (is_directed (in))
106 throw Exception ("Cannot convert a non-symmetric directed matrix to upper triangular");
107
108 for (node_t row = 0; row != in.rows(); ++row) {
109 for (node_t col = row+1; col != in.cols(); ++col) {
110
111 const typename MatrixType::Scalar lower_value = in (col, row);
112 const typename MatrixType::Scalar upper_value = in (row, col);
113
114 if (!upper_value && lower_value)
115 in (row, col) = lower_value;
116 in (col, row) = typename MatrixType::Scalar(0);
117
118 } }
119 }
120
121
122
123 void check (const Header&);
124
125
126
127 }
128}
129
130
131#endif
132
uint32_t node_t
Definition: connectome.h:34
bool is_directed(MatrixType &in)
Definition: connectome.h:59
void check(const MatrixType &in, const node_t num_nodes=0)
Definition: connectome.h:48
Eigen::Array< value_type, Eigen::Dynamic, Eigen::Dynamic > matrix_type
Definition: connectome.h:37
void to_symmetric(MatrixType &in)
Definition: connectome.h:81
void to_upper(MatrixType &in)
Definition: connectome.h:103
Eigen::Array< bool, Eigen::Dynamic, Eigen::Dynamic > mask_type
Definition: connectome.h:39
Eigen::Array< value_type, Eigen::Dynamic, 1 > vector_type
Definition: connectome.h:38
default_type value_type
Definition: connectome.h:36
const App::OptionGroup MatrixOutputOptions
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
std::string str(const T &value, int precision=0)
Definition: mrtrix.h:247