Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkConnectomicsNetworkCSVWriter.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 <mitkStringProperty.h>
22 #include <boost/algorithm/string.hpp>
23 
25  : AbstractFileWriter(mitk::ConnectomicsNetwork::GetStaticNameOfClass(), CustomMimeType( mitk::DiffusionIOMimeTypes::CONNECTOMICS_LIST_MIMETYPE() ), "Write only CSV Connectomics Networks" )
26 {
28 }
29 
31  : AbstractFileWriter(other)
32 {
33 }
34 
35 
37 {}
38 
40 {
41  return new ConnectomicsNetworkCSVWriter(*this);
42 }
43 
45 {
46  MITK_INFO << "Writing connectomics network";
47  InputType::ConstPointer input = dynamic_cast<const InputType*>(this->GetInput());
48  if (input.IsNull() )
49  {
50  MITK_ERROR <<"Sorry, input to ConnectomicsNetworkMatrixWriter is NULL!";
51  return;
52  }
53 
54  this->ValidateOutputLocation();
55 
56  std::ostream* out;
57  std::ofstream outStream;
58 
59  if( this->GetOutputStream() )
60  {
61  out = this->GetOutputStream();
62  }
63  else
64  {
65  outStream.open( this->GetOutputLocation().c_str() );
66  out = &outStream;
67  }
68 
69  if ( !out->good() )
70  {
71  mitkThrow() << "Could not open stream.";
72  }
73 
74  try
75  {
76  const std::string& locale = "C";
77  const std::string& currLocale = setlocale( LC_ALL, NULL );
78  setlocale(LC_ALL, locale.c_str());
79 
80  std::locale previousLocale(out->getloc());
81  std::locale I("C");
82  out->imbue(I);
83 
84  // construct header
85  std::stringstream header;
86 
87  mitk::StringProperty::Pointer addHeaderInfoProperty = dynamic_cast<mitk::StringProperty*>(input->GetProperty(connectomicsDataAdditionalHeaderInformation.c_str()).GetPointer());
88  if(addHeaderInfoProperty.IsNotNull())
89  {
90  std::string additionalHeaderInfo = addHeaderInfoProperty->GetValue();
91  // if the additional header info contains newlines we need to add #
92  // in front of the new lines
93  std::vector<std::string> strings;
94  boost::split(strings, additionalHeaderInfo, boost::is_any_of("\n"));
95  for( unsigned int index(0); index < strings.size(); ++index)
96  {
97  header << "#" << strings[index] << "\n";
98  }
99  }
100 
101  // construct body
102  std::stringstream body;
103 
104  std::vector< InputType::VertexDescriptorType > nodes = input->GetVectorOfAllVertexDescriptors();
105  for(unsigned int i(0); i < nodes.size(); ++i)
106  {
107  for(unsigned int j(0); j < i; ++j)
108  {
109  double weight(0);
110  if( input->EdgeExists(nodes[i], nodes[j]) )
111  {
112  weight = (input->GetEdge(nodes[i], nodes[j])).edge_weight;
113  }
114  body << (input->GetNode(nodes[i])).label << " "
115  << (input->GetNode(nodes[j])).label << " "
116  << weight << "\n";
117  }
118  }
119 
120  (*out)<< header.str() << body.str();
121 
122  setlocale(LC_ALL, currLocale.c_str());
123  MITK_INFO << "Connectomics network connection list written";
124  }
125  catch(...)
126  {
127  mitkThrow() << "Error while writing to stream.";
128  }
129 }
130 
132 {
133  if (AbstractFileWriter::GetConfidenceLevel() == Unsupported)
134  {
135  return Unsupported;
136  }
137 
138  // exporting is supported but will result in loss of data
139  // which prevents re-import
140  return PartiallySupported;
141 }
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.
mitk::IFileIO::ConfidenceLevel GetConfidenceLevel() const
The confidence level of the reader or writer implementation.
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
virtual void Write()
Write the base data to the specified location or output stream.
#define mitkThrow()
static const char * GetStaticNameOfClass()
virtual mitk::ConnectomicsNetworkCSVWriter * 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)