48 #include <QtConcurrent>
61 using namespace DISPLIB;
62 using namespace FIFFLIB;
63 using namespace RTPROCESSINGLIB;
64 using namespace Eigen;
71 : QAbstractTableModel(parent)
74 , m_bProjActivated(false)
75 , m_bCompActivated(false)
77 , m_qMapAverageColor(QSharedPointer<QMap<QString, QColor> >::create())
78 , m_qMapAverageActivation(QSharedPointer<QMap<QString, bool> >::create())
79 , m_qMapAverageColorOld(QSharedPointer<QMap<QString, QColor> >::create())
80 , m_qMapAverageActivationOld(QSharedPointer<QMap<QString, bool> >::create())
86 EvokedSetModel::~EvokedSetModel()
95 return m_pEvokedSet->info.nchan;
112 if(role != Qt::DisplayRole && role != Qt::BackgroundRole && role != EvokedSetModelRoles::GetAverageData) {
116 if(index.isValid()) {
117 qint32 row = m_qMapIdxRowSelection[index.row()];
120 if(index.column() == 0 && role == Qt::DisplayRole) {
121 return QVariant(m_pEvokedSet->info.ch_names);
126 if(index.column()==1) {
129 QList<DISPLIB::AvrTypeRowVector> lRowDataPerTrigType;
130 DISPLIB::AvrTypeRowVector pairItem;
133 case Qt::DisplayRole: {
138 for(
int i = 0; i < m_matDataFreeze.size(); ++i) {
139 pairItem.first = m_lAvrTypes.at(i);
140 pairItem.second = m_matDataFreeze.at(i).row(row);
141 lRowDataPerTrigType.append(pairItem);
145 for(
int i = 0; i < m_matData.size(); ++i) {
146 pairItem.first = m_lAvrTypes.at(i);
147 pairItem.second = m_matData.at(i).row(row);
148 lRowDataPerTrigType.append(pairItem);
152 v.setValue(lRowDataPerTrigType);
157 case Qt::BackgroundRole: {
174 if(index.column()==2) {
176 QList<DISPLIB::AvrTypeRowVectorPair> lRowDataPerTrigType;
177 DISPLIB::AvrTypeRowVectorPair averagedData;
180 case EvokedSetModelRoles::GetAverageData: {
183 for(
int i = 0; i < m_matDataFreeze.size(); ++i) {
184 averagedData.first = m_lAvrTypes.at(i);
185 averagedData.second.first = m_matDataFreeze.at(i).data();
186 averagedData.second.second = m_matDataFreeze.at(i).cols();
188 lRowDataPerTrigType.append(averagedData);
191 for(
int i = 0; i < m_matData.size(); ++i) {
192 averagedData.first = m_lAvrTypes.at(i);
193 averagedData.second.first = m_matData.at(i).data();
194 averagedData.second.second = m_matData.at(i).cols();
196 lRowDataPerTrigType.append(averagedData);
200 v.setValue(lRowDataPerTrigType);
216 if(role != Qt::DisplayRole && role != Qt::TextAlignmentRole) {
220 if(orientation == Qt::Horizontal) {
226 case Qt::DisplayRole:
227 return QVariant(
"data plot");
228 case Qt::TextAlignmentRole:
229 return QVariant(Qt::AlignLeft);
231 return QVariant(
"data plot");
234 else if(orientation == Qt::Vertical) {
235 QModelIndex chname = createIndex(section,0);
237 case Qt::DisplayRole:
238 return QVariant(
data(chname).toString());
249 m_pEvokedSet = pEvokedSet;
260 void EvokedSetModel::init()
270 QStringList emptyExclude;
272 if(m_pEvokedSet->info.bads.size() > 0) {
273 sel = FiffInfoBase::pick_channels(m_pEvokedSet->info.ch_names, m_pEvokedSet->info.bads, emptyExclude);
278 m_fSps = m_pEvokedSet->info.sfreq;
280 m_matSparseProjMult = SparseMatrix<double>(m_pEvokedSet->info.chs.size(),m_pEvokedSet->info.chs.size());
281 m_matSparseCompMult = SparseMatrix<double>(m_pEvokedSet->info.chs.size(),m_pEvokedSet->info.chs.size());
282 m_matSparseProjCompMult = SparseMatrix<double>(m_pEvokedSet->info.chs.size(),m_pEvokedSet->info.chs.size());
284 m_matSparseProjMult.setIdentity();
285 m_matSparseCompMult.setIdentity();
286 m_matSparseProjCompMult.setIdentity();
288 m_qMapAverageActivation->clear();
289 m_qMapAverageColor->clear();
290 m_qMapAverageActivationOld->clear();
291 m_qMapAverageColorOld->clear();
317 for(
int i = 0; i < m_pEvokedSet->evoked.size(); ++i) {
318 bool doProj = m_bProjActivated && m_pEvokedSet->evoked.at(i).data.cols() > 0 && m_pEvokedSet->evoked.at(i).data.rows() == m_matProj.cols() ? true :
false;
320 bool doComp = m_bCompActivated && m_pEvokedSet->evoked.at(i).data.cols() > 0 && m_pEvokedSet->evoked.at(i).data.rows() == m_matComp.cols() ? true :
false;
325 m_matData.append(m_matSparseProjCompMult * m_pEvokedSet->evoked.at(i).data);
328 m_matData.append(m_matSparseCompMult * m_pEvokedSet->evoked.at(i).data);
333 m_matData.append(m_matSparseProjMult * m_pEvokedSet->evoked.at(i).data);
336 m_matData.append(m_pEvokedSet->evoked.at(i).data);
340 m_pairBaseline = m_pEvokedSet->evoked.at(i).baseline;
342 m_lAvrTypes.append(m_pEvokedSet->evoked.at(i).comment);
346 QStringList slCurrentAvrComments;
347 int iSizeAvrActivation = m_qMapAverageActivation->size();
348 int iSizeAvrColor = m_qMapAverageColor->size();
350 for(
int i = 0; i < m_pEvokedSet->evoked.size(); ++i) {
351 slCurrentAvrComments << m_pEvokedSet->evoked.at(i).comment;
353 if(!m_qMapAverageActivation->contains(m_pEvokedSet->evoked.at(i).comment)) {
354 if(m_qMapAverageActivationOld->contains(m_pEvokedSet->evoked.at(i).comment)) {
355 m_qMapAverageActivation->insert(m_pEvokedSet->evoked.at(i).comment, m_qMapAverageActivationOld->value(m_pEvokedSet->evoked.at(i).comment));
357 m_qMapAverageActivation->insert(m_pEvokedSet->evoked.at(i).comment,
true);
361 if(!m_qMapAverageColor->contains(m_pEvokedSet->evoked.at(i).comment)) {
362 if(m_qMapAverageColorOld->contains(m_pEvokedSet->evoked.at(i).comment)) {
363 m_qMapAverageColor->insert(m_pEvokedSet->evoked.at(i).comment, m_qMapAverageColorOld->value(m_pEvokedSet->evoked.at(i).comment));
365 m_qMapAverageColor->insert(m_pEvokedSet->evoked.at(i).comment, Qt::yellow);
371 QMutableMapIterator<QString, bool> itrActivation(*m_qMapAverageActivation);
372 while(itrActivation.hasNext()) {
373 itrActivation.next();
374 if(!slCurrentAvrComments.contains(itrActivation.key())) {
375 m_qMapAverageActivationOld->insert(itrActivation.key(),itrActivation.value());
376 itrActivation.remove();
380 QMutableMapIterator<QString, QColor> itrColor(*m_qMapAverageColor);
381 while(itrColor.hasNext()) {
383 if(!slCurrentAvrComments.contains(itrColor.key())) {
384 m_qMapAverageColorOld->insert(itrColor.key(),itrColor.value());
390 if(iSizeAvrColor != m_qMapAverageColor->size()) {
394 if(iSizeAvrActivation != m_qMapAverageActivation->size()) {
399 QModelIndex topLeft = this->index(0,1);
400 QModelIndex bottomRight = this->index(m_pEvokedSet->info.nchan-1,1);
401 QVector<int> roles; roles << Qt::DisplayRole;
403 emit dataChanged(topLeft, bottomRight, roles);
410 return m_qMapAverageColor;
417 return m_qMapAverageActivation;
424 m_qMapAverageColor = qMapAverageColor;
431 m_qMapAverageActivation = qMapAverageActivation;
438 if(row < m_qMapIdxRowSelection.size()) {
439 qint32 chRow = m_qMapIdxRowSelection[row];
440 return m_pEvokedSet->info.chs[chRow].kind;
452 if(row < m_qMapIdxRowSelection.size()) {
453 qint32 chRow = m_qMapIdxRowSelection[row];
454 bIsBad = m_pEvokedSet->info.bads.contains(m_pEvokedSet->info.chs[chRow].ch_name);
464 if(row < m_qMapIdxRowSelection.size()) {
465 qint32 chRow = m_qMapIdxRowSelection[row];
466 return m_pEvokedSet->info.chs[chRow].unit;
469 return FIFF_UNIT_NONE;
476 if(row < m_qMapIdxRowSelection.size()) {
477 qint32 chRow = m_qMapIdxRowSelection[row];
478 return m_pEvokedSet->info.chs[chRow].chpos.coil_type;
495 qint32 iNumSamples = 0;
497 if(!m_matData.isEmpty()) {
498 iNumSamples = m_matData.first().cols();
501 return m_bIsInit ? iNumSamples : 0;
508 return data(index(row, column), role);
515 return m_qMapIdxRowSelection;
522 qint32 iNumSamples = 0;
524 if(!m_matData.isEmpty()) {
525 iNumSamples = m_matData.first().cols();
528 return (qint32)(iNumSamples/m_fSps) - 1;
541 if (!m_pEvokedSet->evoked.isEmpty()) {
542 RowVectorXf times = m_pEvokedSet->evoked.first().times;
545 for(
int i = 0; i < times.cols(); i++) {
546 if(times(i) == 0.0f) {
576 qint32 iNumSamples = 0;
578 if(!m_matData.isEmpty()) {
579 iNumSamples = m_matData.first().cols();
582 return floor((iNumSamples/m_fSps)*10);
590 return m_pairBaseline;
597 return m_matData.size();
610 m_qMapIdxRowSelection.clear();
613 for(qint32 i = 0; i < selection.size(); ++i) {
614 if(selection[i] < m_pEvokedSet->info.nchan) {
615 m_qMapIdxRowSelection.insert(count,selection[i]);
635 m_qMapIdxRowSelection.clear();
637 for(qint32 i = 0; i < m_pEvokedSet->info.nchan; ++i) {
638 m_qMapIdxRowSelection.insert(i,i);
653 if(m_pEvokedSet->info.chs.size() > 0) {
654 m_pEvokedSet->info.projs = projs;
655 m_bProjActivated =
false;
656 for(qint32 i = 0; i < projs.size(); ++i) {
657 if(m_pEvokedSet->info.projs[i].active) {
658 m_bProjActivated =
true;
662 m_pEvokedSet->info.make_projector(m_matProj);
667 QStringList emptyExclude;
669 if(m_pEvokedSet->info.bads.size() > 0) {
670 sel = FiffInfoBase::pick_channels(m_pEvokedSet->info.ch_names, m_pEvokedSet->info.bads, emptyExclude);
675 for(qint32 j = 0; j < m_vecBadIdcs.cols(); ++j) {
676 m_matProj.col(m_vecBadIdcs[j]).setZero();
683 qint32 nchan = m_pEvokedSet->info.nchan;
686 typedef Eigen::Triplet<double> T;
687 std::vector<T> tripletList;
688 tripletList.reserve(nchan);
694 tripletList.reserve(m_matProj.rows()*m_matProj.cols());
695 for(i = 0; i < m_matProj.rows(); ++i) {
696 for(
k = 0;
k < m_matProj.cols(); ++
k) {
697 if(m_matProj(i,
k) != 0) {
698 tripletList.push_back(T(i,
k, m_matProj(i,
k)));
703 m_matSparseProjMult = SparseMatrix<double>(m_matProj.rows(),m_matProj.cols());
704 if(tripletList.size() > 0) {
705 m_matSparseProjMult.setFromTriplets(tripletList.begin(), tripletList.end());
709 m_matSparseProjCompMult = m_matSparseProjMult * m_matSparseCompMult;
722 if(m_pEvokedSet->info.chs.size() > 0)
725 m_bCompActivated =
false;
727 m_bCompActivated =
true;
735 m_pEvokedSet->info.make_compensator(0, to, newComp);
740 m_matComp = newComp.
data->data;
745 qint32 nchan = m_pEvokedSet->info.nchan;
748 typedef Eigen::Triplet<double> T;
749 std::vector<T> tripletList;
750 tripletList.reserve(nchan);
753 tripletList.reserve(m_matComp.rows()*m_matComp.cols());
754 for(i = 0; i < m_matComp.rows(); ++i) {
755 for(
k = 0;
k < m_matComp.cols(); ++
k) {
756 if(m_matComp(i,
k) != 0) {
757 tripletList.push_back(T(i,
k, m_matComp(i,
k)));
762 m_matSparseCompMult = SparseMatrix<double>(m_matComp.rows(),m_matComp.cols());
763 if(tripletList.size() > 0) {
764 m_matSparseCompMult.setFromTriplets(tripletList.begin(), tripletList.end());
768 m_matSparseProjCompMult = m_matSparseProjMult * m_matSparseCompMult;
776 m_bIsFreezed = !m_bIsFreezed;
779 m_matDataFreeze = m_matData;
783 QModelIndex topLeft = this->index(0,1);
784 QModelIndex bottomRight = this->index(this->
rowCount(),1);
785 QVector<int> roles; roles << Qt::DisplayRole;
786 emit dataChanged(topLeft, bottomRight, roles);
791 QSharedPointer<FIFFLIB::FiffEvokedSet> EvokedSetModel::getEvokedSet()