Commit 5e5f7a64 authored by cornimueller's avatar cornimueller
Browse files

[ADD #89] First versions of an abstract pager class and a pager class for CNT...

[ADD #89] First versions of an abstract pager class and a pager class for CNT files to load EEG values only on demand.
parent 7bae00e5
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WEEGVALUEMATRIX_H
#define WEEGVALUEMATRIX_H
#include <vector>
/**
* Values of an EEG.
* Saved as vector of channels, and each channel is a vector of samples.
*/
typedef std::vector< std::vector< double > > WEEGValueMatrix;
#endif // WEEGVALUEMATRIX_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "../../common/WIOTools.h"
#include "../exceptions/WDHNoSuchFile.h"
#include "WPagerEEG.h"
WPagerEEG::~WPagerEEG()
{
}
WPagerEEG::WPagerEEG( std::string fileName )
: m_fileName( fileName )
{
if( !wiotools::fileExists( m_fileName ) )
{
throw WDHNoSuchFile( m_fileName + " doesn't exist" );
}
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPAGEREEG_H
#define WPAGEREEG_H
#include <cstddef>
#include <string>
#include <boost/shared_ptr.hpp>
#include "../WEEGValueMatrix.h"
/**
* Abstract class to load an EEG file and keep it open to support paging.
* \ingroup dataHandler
*/
class WPagerEEG
{
public:
/**
* Virtual destructor
*/
virtual ~WPagerEEG();
/**
* Get the values of all channels for a given sample range.
*
* \param segment segment number to read the values from
* \param start start sample of the sample range
* \param length length of the sample range
* \return matrix of values
*/
virtual boost::shared_ptr< WEEGValueMatrix > getValues( std::size_t segment, std::size_t start, std::size_t length ) = 0;
protected:
/**
* Constructor
*
* \param fileName path and filename to the file to load
*/
explicit WPagerEEG( std::string fileName );
std::string m_fileName; //!< name of the loaded file
private:
};
#endif // WPAGEREEG_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <clocale>
#include <cstddef>
#include <cstdio>
#include <sstream>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
extern "C"
{
#include "libeep/cnt/cnt.h"
}
#include "../../common/exceptions/WOutOfBounds.h"
#include "../exceptions/WDHIOFailure.h"
#include "../exceptions/WDHNoSuchFile.h"
#include "../WEEGValueMatrix.h"
#include "WPagerEEGLibeep.h"
WPagerEEGLibeep::WPagerEEGLibeep( std::string fileName )
: WPagerEEG( fileName )
{
// libeep needs the standard C locale to load float values from ASCII
std::setlocale( LC_NUMERIC, "C" );
// initialize
std::FILE* file = std::fopen( m_fileName.c_str(), "rb" );
if( !file )
{
throw WDHNoSuchFile( m_fileName + " could not be opened" );
}
int status;
m_eeg = eep_init_from_file( m_fileName.c_str(), file, &status );
if( status != CNTERR_NONE || !m_eeg )
{
std::ostringstream stream;
stream << m_fileName << " could not be initialized. Libeep status code: " << status;
throw WDHIOFailure( stream.str() );
}
m_nbChannels = eep_get_chanc( m_eeg );
m_nbSamples = eep_get_samplec( m_eeg );
m_scales = std::vector< double >( m_nbChannels );
for( size_t channelID = 0; channelID < m_nbChannels; ++channelID )
{
m_scales[channelID] = eep_get_chan_scale( m_eeg, channelID );
}
}
WPagerEEGLibeep::~WPagerEEGLibeep()
{
// cleanup
int status = eep_fclose( m_eeg );
if( status != CNTERR_NONE )
{
std::ostringstream stream;
stream << m_fileName << " could not be closed. Libeep status code: " << status;
throw WDHIOFailure( stream.str() );
}
}
boost::shared_ptr< WEEGValueMatrix > WPagerEEGLibeep::getValues( std::size_t segment, std::size_t start, std::size_t length )
{
if( segment != 0 )
{
std::ostringstream stream;
stream << m_fileName << " has no segment number " << segment;
throw WOutOfBounds( stream.str() );
}
if( start + length > m_nbSamples )
{
std::ostringstream stream;
stream << "Could not read sample number " << start + length - 1 << " of file " << m_fileName
<< ", it has only " << m_nbSamples << " samples";
throw WOutOfBounds( stream.str() );
}
int status = eep_seek( m_eeg, DATATYPE_EEG, start, 0 );
if( status != CNTERR_NONE )
{
std::ostringstream stream;
stream << "Seek error on " << m_fileName << ". Libeep status code: " << status;
throw WDHIOFailure( stream.str() );
}
sraw_t* buffer = new sraw_t[CNTBUF_ARRAYSIZE( m_eeg, length )];
status = eep_read_sraw( m_eeg, DATATYPE_EEG, buffer, length );
if( status != CNTERR_NONE )
{
std::ostringstream stream;
stream << "Read error on " << m_fileName << ". Libeep status code: " << status;
throw WDHIOFailure( stream.str() );
}
boost::shared_ptr< WEEGValueMatrix > values( new WEEGValueMatrix( m_nbChannels, std::vector< double >( length ) ) );
for( size_t channelID = 0; channelID < m_nbChannels; ++channelID )
{
for( size_t sampleID = 0; sampleID < length; ++sampleID )
{
(*values)[channelID][sampleID] = buffer[sampleID * m_nbChannels + channelID] * m_scales[channelID];
}
}
delete[] buffer;
return values;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPAGEREEGLIBEEP_H
#define WPAGEREEGLIBEEP_H
#include <cstddef>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "../WEEGValueMatrix.h"
#include "WPagerEEG.h"
/**
* Class to load an EEG file and keep it open to support paging.
* Uses the libeep library to read the CNT format.
* \ingroup dataHandler
*/
class WPagerEEGLibeep : public WPagerEEG
{
public:
/**
* Constructor
*
* \param fileName path and filename to a CNT file
*/
explicit WPagerEEGLibeep( std::string fileName );
/**
* Virtual destructor
*/
virtual ~WPagerEEGLibeep();
virtual boost::shared_ptr< WEEGValueMatrix > getValues( std::size_t segment, std::size_t start, std::size_t length );
protected:
private:
eeg_t* m_eeg; //!< handler for the cnt file opened by libeep
std::size_t m_nbChannels; //!< number of channels
std::size_t m_nbSamples; //!< number of samples
std::vector< double > m_scales; //!< scale factors of the channels
};
#endif // WPAGEREEGLIBEEP_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment