OpenWalnut  1.5.0dev
WEEGSourceCalculator.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <cstddef>
26 #include <limits>
27 #include <memory>
28 #include <vector>
29 
30 
31 #include "WEEGEvent.h"
32 #include "WEEGSourceCalculator.h"
33 #include "core/common/WAssert.h"
34 #include "core/common/math/linearAlgebra/WPosition.h"
35 #include "core/dataHandler/WEEG2.h"
36 #include "core/dataHandler/WEEGChannelInfo.h"
37 #include "core/dataHandler/exceptions/WDHException.h"
38 
39 
40 WEEGSourceCalculator::WEEGSourceCalculator( const std::shared_ptr< const WEEG2 > eeg )
41  : m_eeg( eeg ),
42  m_numPositions( 0 )
43 {
44  // it is possible to calculate some time position independend data
45  // structures here (like the lead matrix)
46 
47  m_hasPosition.reserve( m_eeg->getNumberOfChannels() );
48  for( std::size_t channelID = 0; channelID < m_eeg->getNumberOfChannels(); ++channelID )
49  {
50  try
51  {
52  m_eeg->getChannelInfo( channelID )->getPosition();
53  m_hasPosition.push_back( true );
55  }
56  catch( const WDHException& )
57  {
58  m_hasPosition.push_back( false );
59  }
60  }
61 }
62 
63 WPosition WEEGSourceCalculator::calculate( const std::shared_ptr< const WEEGEvent > event ) const
64 {
65  const std::vector< double >& values = event->getValues();
66  WAssert( values.size() == m_hasPosition.size(), "Event and loaded EEG dataset have to have the same number of channels" );
67 
68  double sum = 0.0;
69  double min = std::numeric_limits< double >::infinity();
70  for( std::size_t channelID = 0; channelID < values.size(); ++channelID )
71  {
72  if( m_hasPosition[channelID] )
73  {
74  sum += values[channelID];
75  if( values[channelID] < min )
76  {
77  min = values[channelID];
78  }
79  }
80  }
81 
82  sum -= m_numPositions * min;
83 
84  WPosition source;
85  for( std::size_t channelID = 0; channelID < values.size(); ++channelID )
86  {
87  if( m_hasPosition[channelID] )
88  {
89  source += ( values[channelID] - min ) / sum * m_eeg->getChannelInfo( channelID )->getPosition();
90  }
91  }
92 
93  return source;
94 }
General purpose exception and therefore base class for all DataHandler related exceptions.
Definition: WDHException.h:40
const std::shared_ptr< const WEEG2 > m_eeg
pointer to the EEG dataset
std::vector< bool > m_hasPosition
vector which saves for each channel whether there exists a position in the EEG dataset or not
std::size_t m_numPositions
how many channels with positions exist in the EEG dataset
WEEGSourceCalculator(const std::shared_ptr< const WEEG2 > eeg)
Constructor.
WPosition calculate(const std::shared_ptr< const WEEGEvent > event) const
Does the calculation.
This only is a 3d double vector.