Developer documentation
Version 3.0.3-105-gd3941f44
import.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#ifndef __math_stats_import_h__
17#define __math_stats_import_h__
18
19#include <fstream>
20#include <memory>
21#include <string>
22#include <vector>
23
24#include "progressbar.h"
25
26#include "file/path.h"
27
28#include "math/stats/typedefs.h"
29
30
31namespace MR
32{
33 namespace Math
34 {
35 namespace Stats
36 {
37
38
39
52 public:
53 SubjectDataImportBase (const std::string& path) :
54 path (path) { }
56
61 virtual void operator() (matrix_type::RowXpr column) const = 0;
62
67 virtual default_type operator[] (const size_t index) const = 0;
68
69 const std::string& name() const { return path; }
70
71 virtual size_t size() const = 0;
72
73 protected:
74 const std::string path;
75
76 };
78
79
80
81 // During the initial import, the above class can simply be fed one subject at a time
82 // according to per-file path
83 // However for use in GLMTTestVariable, a class is needed that stores a list of text files,
84 // where each text file contains a list of file names (one for each subject), and
85 // for each subject a mechanism of data access is spawned & remains open throughout
86 // processing.
89 public:
91
92 // Needs to be its own function rather than the constructor
93 // so that the correct template type can be invoked explicitly
94 template <class SubjectDataImport>
95 void initialise (const std::string& listpath, const std::string& explicit_from_directory = "");
96
101 vector_type operator() (const size_t index) const;
102
103 size_t size() const { return files.size(); }
104
105 std::shared_ptr<SubjectDataImportBase> operator[] (const size_t i) const
106 {
107 assert (i < files.size());
108 return files[i];
109 }
110
111 bool allFinite() const;
112
113 protected:
115 };
116
117
118
119 template <class SubjectDataImport>
120 void CohortDataImport::initialise (const std::string& listpath, const std::string& explicit_from_directory)
121 {
122 // Read the provided text file one at a time
123 // For each file, create an instance of SubjectDataImport
124 // (which must derive from SubjectDataImportBase)
125
126 // - If a directory path is explicitly provided, first try to find
127 // all input files relative to that directory
128 // - If that fails, try to find all files relative to
129 // directory in which subject list text file is stored
130 // - If that fails, try to find all files relative to
131 // current working directory
132 // Only once a directory is selected that contains all inputs listed in the
133 // text file is an attempt made to load all of those files
135 {
136 std::ifstream ifs (listpath.c_str());
137 if (!ifs)
138 throw Exception ("Unable to open subject file list \"" + listpath + "\"");
139 std::string line;
140 while (getline (ifs, line)) {
141 size_t p = line.find_last_not_of(" \t");
142 if (p != std::string::npos)
143 line.erase (p+1);
144 if (line.size())
145 lines.push_back (line);
146 }
147 }
148
149 vector<std::string> directories { Path::dirname (listpath) };
150 if (directories[0].empty())
151 directories[0] = ".";
152 else if (directories[0] != ".")
153 directories.push_back (".");
154 if (explicit_from_directory.size())
155 directories.insert (directories.begin(), explicit_from_directory);
156
157 Exception e_nosuccess ("Unable to load all input data from file \"" + listpath + "\"");
158 std::string load_from_dir;
159 for (const auto& directory : directories) {
160 try {
161 for (const auto& line : lines) {
162 const std::string full_path = Path::join (directory, line);
163 if (!Path::is_file (full_path))
164 throw Exception ("File \"" + full_path + "\" not found");
165 }
166 load_from_dir = directory;
167 break;
168 } catch (Exception& e) {
169 e_nosuccess.push_back ("If loading relative to directory \"" + directory + "\": ");
170 e_nosuccess.push_back (e);
171 }
172 }
173
174 if (load_from_dir.empty())
175 throw e_nosuccess;
176
177 ProgressBar progress ("Importing data from files listed in \""
178 + Path::basename (listpath)
179 + "\" as found relative to directory \""
180 + load_from_dir + "\"");
181
182 for (const auto& line : lines) {
183 try {
184 std::shared_ptr<SubjectDataImport> subject (new SubjectDataImport (Path::join (load_from_dir, line)));
185 files.emplace_back (subject);
186 } catch (Exception& e) {
187 throw Exception (e, "Input data not successfully loaded: \"" + line + "\"");
188 }
189 ++progress;
190 }
191 }
192
193
194
195
196 }
197 }
198}
199
200
201#endif
void push_back(const std::string &s)
Definition: exception.h:102
vector_type operator()(const size_t index) const
vector< std::shared_ptr< SubjectDataImportBase > > files
Definition: import.h:114
void initialise(const std::string &listpath, const std::string &explicit_from_directory="")
Definition: import.h:120
std::shared_ptr< SubjectDataImportBase > operator[](const size_t i) const
Definition: import.h:105
const std::string & name() const
Definition: import.h:69
virtual default_type operator[](const size_t index) const =0
SubjectDataImportBase(const std::string &path)
Definition: import.h:53
virtual size_t size() const =0
virtual void operator()(matrix_type::RowXpr column) const =0
implements a progress meter to provide feedback to the user
Definition: progressbar.h:58
constexpr double e
Definition: math.h:39
#define NOMEMALIGN
Definition: memory.h:22
Eigen::Array< value_type, Eigen::Dynamic, 1 > vector_type
Definition: typedefs.h:35
std::string join(const std::string &first, const std::string &second)
Definition: path.h:74
bool is_file(const std::string &path)
Definition: path.h:120
std::string basename(const std::string &name)
Definition: path.h:60
std::string dirname(const std::string &name)
Definition: path.h:67
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
std::istream & getline(std::istream &stream, std::string &string)
read a line from the stream
Definition: mrtrix.h:47
size_t index