Developer documentation
Version 3.0.3-105-gd3941f44
metric.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 __dwi_tractography_connectome_metric_h__
18#define __dwi_tractography_connectome_metric_h__
19
20
21#include "image.h"
22#include "types.h"
23#include "algo/loop.h"
24#include "interp/linear.h"
25
26#include "connectome/connectome.h"
27
30
31
32
33
34namespace MR {
35namespace DWI {
36namespace Tractography {
37namespace Connectome {
38
39
40
41// Provide a common interface for calculating the contribution from a
42// particular streamline to a particular edge of the connectome
43class Metric { MEMALIGN(Metric)
44
45 public:
46 Metric() :
47 scale_by_length (false),
48 scale_by_invlength (false),
49 scale_by_invnodevol (false),
50 scale_by_file (false) { }
51
52 double operator() (const Streamline<>& tck, const NodePair& nodes) const
53 {
54 if (scale_by_invnodevol) {
55 assert (nodes.first < node_volumes.size());
56 assert (nodes.second < node_volumes.size());
57 const double sum_volumes = (node_volumes[nodes.first] + node_volumes[nodes.second]);
58 if (!sum_volumes) return 0.0;
59 return (*this)(tck) * 2.0 / sum_volumes;
60 }
61 return (*this)(tck);
62 }
63
64 double operator() (const Streamline<>& tck, const vector<node_t>& nodes) const
65 {
66 if (scale_by_invnodevol) {
67 double sum_volumes = 0.0;
68 for (vector<node_t>::const_iterator n = nodes.begin(); n != nodes.end(); ++n) {
69 assert (*n < node_volumes.size());
70 sum_volumes += node_volumes[*n];
71 }
72 if (!sum_volumes) return 0.0;
73 return (*this)(tck) * nodes.size() / sum_volumes;
74 }
75 return (*this)(tck);
76 }
77
78 double operator() (const Streamline<>& tck) const
79 {
80 double result = 1.0;
81 if (scale_by_length)
82 result *= Tractography::length (tck);
83 else if (scale_by_invlength)
84 result = (tck.size() > 1 ? (result / Tractography::length (tck)) : 0.0);
85 if (scale_by_file) {
86 if (tck.get_index() >= size_t(file_values.size()))
87 throw Exception ("File " + file_path + " does not contain enough entries for this tractogram");
88 result *= file_values[tck.get_index()];
89 }
90 return result;
91 }
92
93
94 void set_scale_length (const bool i = true) {
95 if (i) assert (!scale_by_invlength);
96 scale_by_length = i;
97 }
98 void set_scale_invlength (const bool i = true) {
99 if (i) assert (!scale_by_length);
100 scale_by_invlength = i;
101 }
102 void set_scale_invnodevol (Image<node_t>& nodes, const bool i = true) {
103 scale_by_invnodevol = i;
104 if (!i) {
105 node_volumes.resize (0);
106 return;
107 }
108 for (auto l = Loop() (nodes); l; ++l) {
109 const node_t index = nodes.value();
110 if (index >= node_volumes.size())
111 node_volumes.conservativeResizeLike (Eigen::VectorXd::Zero (index + 1));
112 node_volumes[index]++;
113 }
114 }
115 void set_scale_file (const std::string& path, const bool i = true) {
116 scale_by_file = i;
117 if (!i) {
118 file_path.clear();
119 file_values.resize (0);
120 return;
121 }
122 file_path = Path::basename (path);
123 file_values = MR::load_vector (path);
124 }
125
126
127 private:
128 bool scale_by_length, scale_by_invlength, scale_by_invnodevol, scale_by_file;
129 Eigen::VectorXd node_volumes;
130 std::string file_path;
131 Eigen::VectorXd file_values;
132
133};
134
135
136
137
138
139
140}
141}
142}
143}
144
145
146#endif
147
FORCE_INLINE LoopAlongAxes Loop()
Definition: loop.h:419
std::pair< node_t, node_t > NodePair
Definition: connectome.h:41
MR::Connectome::node_t node_t
Definition: connectome.h:40
PointType::Scalar length(const vector< PointType > &tck)
Definition: streamline.h:134
std::string basename(const std::string &name)
Definition: path.h:60
Definition: base.h:24
Eigen::Matrix< ValueType, Eigen::Dynamic, 1 > load_vector(const std::string &filename)
read the vector data from filename
Definition: math.h:327
size_t index