MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
fiff_io.cpp
Go to the documentation of this file.
1//=============================================================================================================
37//=============================================================================================================
38// INCLUDES
39//=============================================================================================================
40
41#include "fiff_io.h"
42#include "fiff.h"
43#include "fiff_evoked_set.h"
44#include "fiff_stream.h"
45
46#include <rtprocessing/filter.h>
48
49//=============================================================================================================
50// USED NAMESPACES
51//=============================================================================================================
52
53using namespace FIFFLIB;
54using namespace Eigen;
55using namespace RTPROCESSINGLIB;
56
57//=============================================================================================================
58// DEFINE MEMBER METHODS
59//=============================================================================================================
60
64
65//=============================================================================================================
66
70
71//=============================================================================================================
72
73FiffIO::FiffIO(QIODevice& pIODevice)
74{
75 // execute read method
76 FiffIO::read(pIODevice);
77}
78
79//=============================================================================================================
80
81FiffIO::FiffIO(QList<QIODevice*>& p_qlistIODevices)
82{
83 QList<QIODevice*>::iterator i;
84 for(i = p_qlistIODevices.begin(); i != p_qlistIODevices.end(); ++i) {
85 FiffIO::read((**i));
86 }
87}
88
89//=============================================================================================================
90
91bool FiffIO::setup_read(QIODevice& pIODevice,
92 FiffInfo& info,
93 FiffDirNode::SPtr& dirTree)
94{
95 //Open the file
96 FiffStream::SPtr p_pStream(new FiffStream(&pIODevice));
97 QString t_sFileName = p_pStream->streamName();
98
99 printf("Opening fiff data %s...\n",t_sFileName.toUtf8().constData());
100
101 if(!p_pStream->open())
102 return false;
103
104 //Read the measurement info
105 if(!p_pStream->read_meas_info(p_pStream->dirtree(), info, dirTree))
106 return false;
107
108 return true;
109}
110
111//=============================================================================================================
112
113bool FiffIO::read(QIODevice& pIODevice)
114{
115 //Read dirTree from fiff data (raw,evoked,fwds,cov)
116 FiffInfo t_fiffInfo;
117 FiffDirNode::SPtr t_dirTree;
118 bool hasRaw = false;
119 bool hasEvoked = false; // hasFwds=false;
120
121 FiffIO::setup_read(pIODevice, t_fiffInfo, t_dirTree);
122 pIODevice.close(); //file can be closed, since IODevice is already read
123
124 if(!t_dirTree) {
125 qWarning() << "[FiffIO::read] Dir tree could not be read";
126 return false;
127 }
128
129 //Search dirTree for specific data types
130 if(t_dirTree->has_kind(FIFFB_EVOKED)) {
131 hasEvoked = true;
132 }
133
134 if(t_dirTree->has_kind(FIFFB_RAW_DATA) ||
135 t_dirTree->has_kind(FIFFB_PROCESSED_DATA) ||
136 t_dirTree->has_kind(FIFFB_CONTINUOUS_DATA) ||
137 t_dirTree->has_kind(FIFFB_SMSH_RAW_DATA)) {
138 hasRaw = true;
139 }
140
141 // if(t_Tree.has_kind(FIFFB_MNE_FORWARD_SOLUTION))
142 // hasFwds = true;
143
144 //Read all sort of types
145 //raw data
146 if(hasRaw) {
147 QSharedPointer<FiffRawData> p_fiffRawData(new FiffRawData(pIODevice));
148 pIODevice.close();
149
150 //append to corresponding member qlist
151 m_qlistRaw.append(p_fiffRawData);
152
153 printf( "Finished reading raw data!\n");
154 }
155
156 //evoked data + projections
157 if(hasEvoked) {
158 FiffEvokedSet p_fiffEvokedSet(pIODevice);
159 pIODevice.close();
160
161 //append to corresponding member qlist
162 for(qint32 i=0; i < p_fiffEvokedSet.evoked.size(); ++i) {
163 m_qlistEvoked.append(QSharedPointer<FiffEvoked>(&p_fiffEvokedSet.evoked[i]));
164 }
165 }
166
167// //forward solutions
168// if(hasFwds) {
169// MNEForwardSolution p_forwardSolution(pIODevice);
170
171// //append to corresponding member qlist
172// m_qlistFwd.append(QSharedPointer<MNEForwardSolution>(&p_forwardSolution));
173// }
174
175 //print summary
176 //std::cout << *this << std::endl;
177
178 return true;
179}
180
181//=============================================================================================================
182
183bool FiffIO::write(QIODevice& pIODevice,
184 const fiff_int_t type,
185 const fiff_int_t idx) const {
186 switch(type) {
187 case FIFFB_RAW_DATA: {
188 FiffIO::write_raw(pIODevice,idx);
189 qDebug() << "Finished writing single raw data with index" << idx << ".";
190 }
191 case FIFFB_EVOKED:
192 //ToDo: write evoked set to file
193 ;
194 }
195
196 return true;
197}
198
199//=============================================================================================================
200
201bool FiffIO::write(QFile& p_QFile,
202 const fiff_int_t type,
203 const fiff_int_t idx) const {
204 qDebug("------------------------ Writing fiff data ------------------------");
205
206 switch(type) {
207 case FIFFB_RAW_DATA: {
208 QString t_nameoftype = "raw";
209
210 if(idx == -1) {
211 for(qint32 i=0; i < m_qlistRaw.size(); ++i) {
212 QString t_fname;
213 //insert
214 qint32 p = p_QFile.fileName().indexOf(".fif");
215 t_fname = p_QFile.fileName().insert(p,QString("_"+t_nameoftype+"-"+QString::number(i)));
216
217 //assign new file name
218 qDebug("\nWriting set with index %i to file %s ...",i,t_fname.toUtf8().constData());
219 QFile t_file(t_fname);
220
221 FiffIO::write_raw(t_file,i);
222 }
223 }
224 else {
225 FiffIO::write_raw(p_QFile,idx);
226 }
227 qDebug("\nFinished Writing %lli raw data sets!\n",m_qlistRaw.size());
228 }
229 case FIFFB_EVOKED:
230
231 //ToDo: write evoked set to file
232 ;
233 }
234
235 return true;
236}
237
238//=============================================================================================================
239
240bool FiffIO::write_raw(QIODevice &pIODevice,
241 const fiff_int_t idx) const
242{
243 RowVectorXd cals;
244 SparseMatrix<double> mult;
245 RowVectorXi sel;
246 FiffStream::SPtr outfid = FiffStream::start_writing_raw(pIODevice, this->m_qlistRaw[idx]->info, cals);
247
248 //Setup reading parameters
249 fiff_int_t from = m_qlistRaw[idx]->first_samp;
250 fiff_int_t to = m_qlistRaw[idx]->last_samp;
251 float quantum_sec = 30.0f;//read and write in 30 sec junks
252 fiff_int_t quantum = ceil(quantum_sec*m_qlistRaw[idx]->info.sfreq);
253
254 // Uncomment to read the whole file at once. Warning Matrix may be none-initialisable because its huge
255 //quantum = to - from + 1;
256
257 // Read and write all the data
258 bool first_buffer = true;
259
260 fiff_int_t first, last;
261 MatrixXd data;
262 MatrixXd times;
263
264 for(first = from; first < to; first+=quantum) {
265 last = first+quantum-1;
266 if (last > to)
267 last = to;
268
269 if (!m_qlistRaw[idx]->read_raw_segment(data, times, mult, first, last, sel)) {
270 qDebug("error during read_raw_segment\n");
271 return false;
272 }
273
274 qDebug("Writing...");
275 if (first_buffer) {
276 if (first > 0)
277 outfid->write_int(FIFF_FIRST_SAMPLE,&first);
278 first_buffer = false;
279 }
280 outfid->write_raw_buffer(data, cals);
281 qDebug("[done]\n");
282 }
283
284 outfid->finish_writing_raw();
285
286 return true;
287}
The FilterKernel class represents a filter object that generates the FIR filter coefficients using Pa...
Filter declarations.
FIFF class declaration, which provides static wrapper functions to stay consistent with mne matlab to...
FiffStream class declaration.
FiffEvokedSet class declaration.
#define FIFF_FIRST_SAMPLE
Definition fiff_file.h:461
#define FIFFB_SMSH_RAW_DATA
Definition fiff_file.h:382
Definition of a generic Fiff IO interface.
QSharedPointer< FiffDirNode > SPtr
QList< FiffEvoked > evoked
FIFF measurement file information.
Definition fiff_info.h:85
bool write(QIODevice &pIODevice, const fiff_int_t type, const fiff_int_t idx) const
Definition fiff_io.cpp:183
bool read(QIODevice &pIODevice)
Definition fiff_io.cpp:113
bool write_raw(QIODevice &pIODevice, const fiff_int_t idx) const
Definition fiff_io.cpp:240
static bool setup_read(QIODevice &pIODevice, FiffInfo &info, FiffDirNode::SPtr &dirTree)
Definition fiff_io.cpp:91
FIFF raw measurement data.
FIFF File I/O routines.
static FiffStream::SPtr start_writing_raw(QIODevice &p_IODevice, const FiffInfo &info, Eigen::RowVectorXd &cals, Eigen::MatrixXi sel=defaultMatrixXi, bool bResetRange=true)
QSharedPointer< FiffStream > SPtr