File: import.h

package info (click to toggle)
mrtrix3 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,712 kB
  • sloc: cpp: 129,776; python: 9,494; sh: 593; makefile: 234; xml: 47
file content (201 lines) | stat: -rw-r--r-- 6,879 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Covered Software is provided under this License on an "as is"
 * basis, without warranty of any kind, either expressed, implied, or
 * statutory, including, without limitation, warranties that the
 * Covered Software is free of defects, merchantable, fit for a
 * particular purpose or non-infringing.
 * See the Mozilla Public License v. 2.0 for more details.
 *
 * For more details, see http://www.mrtrix.org/.
 */
#ifndef __math_stats_import_h__
#define __math_stats_import_h__

#include <fstream>
#include <memory>
#include <string>
#include <vector>

#include "progressbar.h"

#include "file/path.h"

#include "math/stats/typedefs.h"


namespace MR
{
  namespace Math
  {
    namespace Stats
    {



      /** \addtogroup Statistics
      @{ */
      /*! A base class defining the interface for importing subject data
       * This class defines the interface for how subject data is imported
       * into a GLM measurement matrix. Exactly how the subject data is
       * 'vectorised' will depend on the particular type of data being
       * tested; nevertheless, the data for each subject should be stored
       * in a single column within the measurement matrix (or in some
       * cases, within the design matrix).
       */
      class SubjectDataImportBase
      { NOMEMALIGN
        public:
          SubjectDataImportBase (const std::string& path) :
              path (path) { }
          virtual ~SubjectDataImportBase() { }

          /*!
          * @param row the row of a matrix into which the data from this
          * particular file should be loaded
          */
          virtual void operator() (matrix_type::RowXpr column) const = 0;

          /*!
           * @param index extract the data from this file corresponding to a particular
           * row in the measurements vector
           */
          virtual default_type operator[] (const size_t index) const = 0;

          const std::string& name() const { return path; }

          virtual size_t size() const = 0;

        protected:
          const std::string path;

      };
      //! @}



      // During the initial import, the above class can simply be fed one subject at a time
      //   according to per-file path
      // However for use in GLMTTestVariable, a class is needed that stores a list of text files,
      //   where each text file contains a list of file names (one for each subject), and
      //   for each subject a mechanism of data access is spawned & remains open throughout
      //   processing.
      class CohortDataImport
      { NOMEMALIGN
        public:
          CohortDataImport() { }

          // Needs to be its own function rather than the constructor
          //   so that the correct template type can be invoked explicitly
          template <class SubjectDataImport>
          void initialise (const std::string& listpath, const std::string& explicit_from_directory = "");

          /*!
           * @param index for a particular element being tested (data will be acquired for
           * all subjects for that element)
           */
          vector_type operator() (const size_t index) const;

          size_t size() const { return files.size(); }

          std::shared_ptr<SubjectDataImportBase> operator[] (const size_t i) const
          {
            assert (i < files.size());
            return files[i];
          }

          bool allFinite() const;

        protected:
          vector<std::shared_ptr<SubjectDataImportBase>> files;
      };



      template <class SubjectDataImport>
      void CohortDataImport::initialise (const std::string& listpath, const std::string& explicit_from_directory)
      {
        // Read the provided text file one at a time
        // For each file, create an instance of SubjectDataImport
        //   (which must derive from SubjectDataImportBase)

        // - If a directory path is explicitly provided, first try to find
        //   all input files relative to that directory
        // - If that fails, try to find all files relative to
        //   directory in which subject list text file is stored
        // - If that fails, try to find all files relative to
        //   current working directory
        // Only once a directory is selected that contains all inputs listed in the
        //   text file is an attempt made to load all of those files
        vector<std::string> lines;
        {
          std::ifstream ifs (listpath.c_str());
          if (!ifs)
            throw Exception ("Unable to open subject file list \"" + listpath + "\"");
          std::string line;
          while (getline (ifs, line)) {
            size_t p = line.find_last_not_of(" \t");
            if (p != std::string::npos)
              line.erase (p+1);
            if (line.size())
              lines.push_back (line);
          }
        }

        vector<std::string> directories { Path::dirname (listpath) };
        if (directories[0].empty())
          directories[0] = ".";
        else if (directories[0] != ".")
          directories.push_back (".");
        if (explicit_from_directory.size())
          directories.insert (directories.begin(), explicit_from_directory);

        Exception e_nosuccess ("Unable to load all input data from file \"" + listpath + "\"");
        std::string load_from_dir;
        for (const auto& directory : directories) {
          try {
            for (const auto& line : lines) {
              const std::string full_path = Path::join (directory, line);
              if (!Path::is_file (full_path))
                throw Exception ("File \"" + full_path + "\" not found");
            }
            load_from_dir = directory;
            break;
          } catch (Exception& e) {
            e_nosuccess.push_back ("If loading relative to directory \"" + directory + "\": ");
            e_nosuccess.push_back (e);
          }
        }

        if (load_from_dir.empty())
          throw e_nosuccess;

        ProgressBar progress ("Importing data from files listed in \""
                              + Path::basename (listpath)
                              + "\" as found relative to directory \""
                              + load_from_dir + "\"");

        for (const auto& line : lines) {
          try {
            std::shared_ptr<SubjectDataImport> subject (new SubjectDataImport (Path::join (load_from_dir, line)));
            files.emplace_back (subject);
          } catch (Exception& e) {
            throw Exception (e, "Input data not successfully loaded: \"" + line + "\"");
          }
          ++progress;
        }
      }




    }
  }
}


#endif