Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkConnectomicsNetworkMatrixWriter.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
21 #include <boost/algorithm/string.hpp>
22 
24  : AbstractFileWriter(mitk::ConnectomicsNetwork::GetStaticNameOfClass(), CustomMimeType( mitk::DiffusionIOMimeTypes::CONNECTOMICS_MATRIX_MIMETYPE() ), "Write only Matrix Connectomics Networks" )
25 {
27 }
28 
30  : AbstractFileWriter(other)
31 {
32 }
33 
34 
36 {}
37 
39 {
40  return new ConnectomicsNetworkMatrixWriter(*this);
41 }
42 
44 {
45  MITK_INFO << "Writing connectomics network";
46  InputType::ConstPointer input = dynamic_cast<const InputType*>(this->GetInput());
47  if (input.IsNull() )
48  {
49  MITK_ERROR <<"Sorry, input to ConnectomicsNetworkMatrixWriter is NULL!";
50  return;
51  }
52 
53  this->ValidateOutputLocation();
54 
55  std::ostream* out;
56  std::ofstream outStream;
57 
58  if( this->GetOutputStream() )
59  {
60  out = this->GetOutputStream();
61  }
62  else
63  {
64  outStream.open( this->GetOutputLocation().c_str() );
65  out = &outStream;
66  }
67 
68  if ( !out->good() )
69  {
70  mitkThrow() << "Could not open stream.";
71  }
72 
73  try
74  {
75  const std::string& locale = "C";
76  const std::string& currLocale = setlocale( LC_ALL, NULL );
77  setlocale(LC_ALL, locale.c_str());
78 
79  std::locale previousLocale(out->getloc());
80  std::locale I("C");
81  out->imbue(I);
82 
83  // construct header
84  std::stringstream header;
85 
86  mitk::StringProperty::Pointer addHeaderInfoProperty = dynamic_cast<mitk::StringProperty*>(input->GetProperty(connectomicsDataAdditionalHeaderInformation.c_str()).GetPointer());
87  if(addHeaderInfoProperty.IsNotNull())
88  {
89  std::string additionalHeaderInfo = addHeaderInfoProperty->GetValue();
90  // if the additional header info contains newlines we need to add #
91  // in front of the new lines
92  std::vector<std::string> strings;
93  boost::split(strings, additionalHeaderInfo, boost::is_any_of("\n"));
94  for( unsigned int index(0); index < strings.size(); ++index)
95  {
96  header << "#" << strings[index] << "\n";
97  }
98  }
99 
100  // add the order of labels to the header
101  std::vector< InputType::VertexDescriptorType > nodes = input->GetVectorOfAllVertexDescriptors();
102  header << "#";
103  for(unsigned int index(0); index < nodes.size(); ++index)
104  {
105  header << " " << (input->GetNode(nodes[index])).label;
106  }
107 
108  // construct body
109  std::stringstream body;
110 
111  for(unsigned int i(0); i < nodes.size(); ++i)
112  {
113  for(unsigned int j(0); j < nodes.size(); ++j)
114  {
115  if( input->EdgeExists(nodes[i], nodes[j]))
116  {
117  body << (input->GetEdge(nodes[i], nodes[j])).edge_weight;
118  }
119  else
120  {
121  body << 0;
122  }
123  if(j < nodes.size() - 1)
124  {
125  body << " ";
126  }
127  }
128  body << "\n";
129  }
130 
131  (*out)<< header.str() << "\n" << body.str();
132  setlocale(LC_ALL, currLocale.c_str());
133  MITK_INFO << "Connectomics network connection list written";
134  }
135  catch(...)
136  {
137  mitkThrow() << "Error while writing to stream.";
138  }
139 }
140 
142 {
143  if (AbstractFileWriter::GetConfidenceLevel() == Unsupported)
144  {
145  return Unsupported;
146  }
147 
148  // exporting is supported but will result in loss of data
149  // which prevents re-import
150  return PartiallySupported;
151 }
This file defines the data properties for connectomics networks in MITK.
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
virtual ConfidenceLevel GetConfidenceLevel() const override
The confidence level of the reader or writer implementation.
virtual void Write()
Write the base data to the specified location or output stream.
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
#define mitkThrow()
static const char * GetStaticNameOfClass()
virtual mitk::ConnectomicsNetworkMatrixWriter * Clone() const
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
const std::string connectomicsDataAdditionalHeaderInformation
Additional header information.
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:49
Property for strings.
Base class for writing mitk::BaseData objects to files or streams.
Connectomics Network Class.
static std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
mitk::IFileIO::ConfidenceLevel GetConfidenceLevel() const
The confidence level of the reader or writer implementation.