selxCommandLine.cxx 5.89 KB
Newer Older
Floris Berendsen's avatar
Floris Berendsen committed
1
2
/*=========================================================================
 *
3
 *  Copyright Leiden University Medical Center, Erasmus University Medical
Floris Berendsen's avatar
Floris Berendsen committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *  Center and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

20
#include "selxSuperElastixFilter.h"
21
22
#include "selxBlueprint.h"
#include "selxConfigurationReader.h"
23
#include "selxAnyFileReader.h"
24
#include "selxAnyFileWriter.h"
25

26
#include "selxDefaultComponents.h"
27

28
29
#include <boost/algorithm/string.hpp>

30
#include <boost/filesystem.hpp>
31
namespace fs = boost::filesystem;
32
33
34
35
36
37
38

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <iostream>
#include <algorithm>
#include <iterator>
39
#include <string>
FBerendsen's avatar
FBerendsen committed
40
#include <stdexcept>
41

42
43
using namespace std;
using namespace selx;
44

45
46
47
template< class T >
ostream &
operator<<( ostream & os, const vector< T > & v )
48
{
49
  copy( v.begin(), v.end(), ostream_iterator< T >( os, " " ) );
50
51
52
  return os;
}

53

54
55
int
main( int ac, char * av[] )
56
{
57
58
  try
  {
59
60
    typedef vector< string > split_vector_type;

61
    SuperElastixFilter< DefaultComponents >::Pointer superElastixFilter = SuperElastixFilter< DefaultComponents >::New();
62

63
64
65
    fs::path         configurationPath;
    vector< string > inputPairs;
    vector< string > outputPairs;
66

67
    // Store the reader so that they will not be destroyed before the pipeline is executed.
68
    vector< AnyFileReader::Pointer > fileReaders;
69
    // Store the writers for the update call
70
    //vector<ImageWriter2DType::Pointer> fileWriters;
71
    vector< AnyFileWriter::Pointer > fileWriters;
72

73
    po::options_description desc( "Allowed options" );
74
    desc.add_options()
75
76
77
78
79
80
      ( "help", "produce help message" )
      ( "conf", po::value< fs::path >( &configurationPath )->required(), "Configuration file" )
      ( "in", po::value< vector< string >>( &inputPairs )->multitoken(), "Input data: images, labels, meshes, etc. Usage <name>=<path>" )
      ( "out", po::value< vector< string >>( &outputPairs )->multitoken(), "Output data: images, labels, meshes, etc. Usage <name>=<path>" )
      ( "graphout", po::value< fs::path >(), "Output Graphviz dot file" )
    ;
81
82

    po::variables_map vm;
83
84
    po::store( po::parse_command_line( ac, av, desc ), vm );
    po::notify( vm );
85

86
87
    if( vm.count( "help" ) )
    {
88
89
90
91
      cout << desc << "\n";
      return 0;
    }

92
    Blueprint::Pointer blueprint;
93
    if( configurationPath.extension() == ".xml" )
94
95
    {
      // TODO: open file here and pass a stream to the ConfigurationReader
96
      blueprint = ConfigurationReader::FromXML( configurationPath.string() );
97
    }
98
    else if( configurationPath.extension() == ".json" )
99
100
    {
      // TODO: open file here and pass a stream to the ConfigurationReader
101
      blueprint = ConfigurationReader::FromJson( configurationPath.string() );
102
    }
103
104
105
    else
    {
      throw invalid_argument( "Configuration file requires extension .xml or .json" );
106
107
    }

108
    if( vm.count( "graphout" ) )
109
    {
110
      blueprint->WriteBlueprint( vm[ "graphout" ].as< fs::path >().string() );
111
112
    }

113
    superElastixFilter->SetBlueprint( blueprint );
114

115
116
    if( vm.count( "in" ) )
    {
117
      cout << "Number of input data: " << inputPairs.size() << "\n";
118
      int index = 0;
119
      for( const auto & inputPair : inputPairs )
120
      {
121
        split_vector_type nameAndPath;
122
123
124
        boost::split( nameAndPath, inputPair, boost::is_any_of( "=" ) );  // NameAndPath == { "name","path" }
        const string & name = nameAndPath[ 0 ];
        const string & path = nameAndPath[ 1 ];
125
126

        cout << " " << index << " " << name << " : " << path << "\n";
127
        ++index;
128

129
        // since we do not know which reader type we should instantiate for input "name",
130
131
        // we ask SuperElastix for a reader that matches the type of the source component "name"

132
133
134
135
        AnyFileReader::Pointer reader = superElastixFilter->GetInputFileReader( name );
        reader->SetFileName( path );
        superElastixFilter->SetInput( name, reader->GetOutput() );
        fileReaders.push_back( reader );
136
137
      }
    }
138

139
140
    if( vm.count( "out" ) )
    {
141
      cout << "Number of output data: " << outputPairs.size() << "\n";
142
      int index = 0;
143
      for( const auto & outputPair : outputPairs )
144
      {
145
        split_vector_type nameAndPath;
146
147
148
        boost::split( nameAndPath, outputPair, boost::is_any_of( "=" ) );  // NameAndPath == { "name","path" }
        const string & name = nameAndPath[ 0 ];
        const string & path = nameAndPath[ 1 ];
149
150

        cout << " " << index << " " << name << " : " << path << "\n";
151
        ++index;
152

153
        // since we do not know which writer type we should instantiate for output "name",
154
        // we ask SuperElastix for a writer that matches the type of the sink component "name"
155
156
        AnyFileWriter::Pointer writer = superElastixFilter->GetOutputFileWriter( name );

157
        //ImageWriter2DType::Pointer writer = ImageWriter2DType::New();
158
        writer->SetFileName( path );
159
        //writer->SetInput(superElastixFilter->GetOutput<Image2DType>(name));
160
161
        writer->SetInput( superElastixFilter->GetOutput( name ) );
        fileWriters.push_back( writer );
162
163
      }
    }
164

165
    /* Execute SuperElastix by updating the writers */
166

167
    for( auto & writer : fileWriters )
168
169
170
    {
      writer->Update();
    }
171
  }
172
173
  catch( exception & e )
  {
174
175
176
    cerr << "error: " << e.what() << "\n";
    return 1;
  }
177
178
  catch( ... )
  {
179
180
181
182
    cerr << "Exception of unknown type!\n";
  }

  return 0;
183
}