Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP [Maint] Refactoring Hpi Fitting #884

Merged
merged 102 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
9d8f4fa
Enh: Drop coils
RDoerfel Aug 28, 2021
ccc2e95
Enh: working coil dropping example
RDoerfel Oct 14, 2021
7cb0e0e
Enh: only use good coils
RDoerfel Oct 14, 2021
f6855a2
Enh: only take errors/gof > -1 into account
RDoerfel Oct 15, 2021
cb37d00
Enh: prepare refactoring by introducing new functions (only header so…
RDoerfel Oct 21, 2021
0faf69d
enh: check if data was passed
RDoerfel Oct 22, 2021
b837e9a
Enh: add drop option
RDoerfel Oct 22, 2021
8e4bee9
Enh: implement compute function and add get functions for member vari…
RDoerfel Oct 22, 2021
2ee664d
Enh,Fix: implement test for updateModel() and remove hardcoded featur…
RDoerfel Oct 23, 2021
2389cec
Maint: merge updateSensors and createSensorSet
RDoerfel Oct 23, 2021
a74a18a
Enh: test constructor and update sensors
RDoerfel Oct 23, 2021
f8181f4
Enh: Add default constructor
RDoerfel Oct 23, 2021
818d6bb
Enh,fix: make updateChannels public and fix problem with cont. append…
RDoerfel Oct 23, 2021
4641d95
Enh: test update channels and adapt constructor test accordingly
RDoerfel Oct 23, 2021
52e9498
Enh: working tests for computeAmplitudes; disable reordering and svd …
RDoerfel Oct 23, 2021
e5bd6c6
enh: firt draft compute headloc
RDoerfel Oct 27, 2021
6704d15
Enh: add init function to HPI
RDoerfel Oct 27, 2021
5f3433c
Enh,Fix: Implement computeCoilLoc and fix appending m_vecInnerInd
RDoerfel Oct 31, 2021
80ea907
Enh: implement test for testComputeCoilLoc
RDoerfel Oct 31, 2021
60e24c5
Maint: rename function
RDoerfel Oct 31, 2021
647e6d8
Enh: implement computeHeadPosition
RDoerfel Oct 31, 2021
c02a5f4
tmp: temporary test and implementation
RDoerfel Oct 31, 2021
05de425
Enh: implement computeHead Pos Function
RDoerfel Nov 2, 2021
2e97b43
Enh: implement test for findOrder
RDoerfel Nov 2, 2021
e455c5c
Enh: add unit and integration test for hpi fit
RDoerfel Nov 2, 2021
3733676
Fix: remove separation into unit and integration test until decission…
RDoerfel Nov 2, 2021
16dbd06
Fix: fix findOrder
RDoerfel Nov 3, 2021
3ad68e6
Enh: add performance/integration test for hpi fit
RDoerfel Nov 22, 2021
fcdb4be
Enh: add coil ordering using permutation
RDoerfel Nov 22, 2021
13b03f3
Enh: add sensor set class
RDoerfel Nov 22, 2021
bd7be49
Doc: document functions
RDoerfel Nov 22, 2021
2dc1842
Maint: add sensorset
RDoerfel Dec 2, 2021
3203d65
Enh: add test for sensorset
RDoerfel Dec 2, 2021
5e76030
Fix: remove test_hpiFit from .pro file (no idea how it got there)
RDoerfel Dec 2, 2021
b552895
Maint: workin test
RDoerfel Dec 2, 2021
00befcd
Enh: add test for ampty channel list
RDoerfel Dec 2, 2021
a17eb8d
Enh: move sensors entirely to its own class
RDoerfel Dec 2, 2021
98e24bb
Maint: move reading of coil definitions into update function so that …
RDoerfel Dec 2, 2021
db6578c
Maint: restructure
RDoerfel Dec 2, 2021
740be4f
Maint: make it easier to follow
RDoerfel Dec 2, 2021
24e33a9
Enh: add data handler for hpi fit with unit test
RDoerfel Dec 2, 2021
60f454c
Maint,ENH: remove files and add getData functio to updater
RDoerfel Dec 7, 2021
826df2c
Maint: improve test and hpidataupdater
RDoerfel Dec 7, 2021
0a4a304
Enh: implement hpifitupdater
RDoerfel Dec 7, 2021
1d52d7a
Enh: implement first draft of signalmodel
RDoerfel Dec 7, 2021
8af575c
Enh: working test for singal model
RDoerfel Dec 9, 2021
aba5462
Enh: test and implement .fit function
RDoerfel Dec 9, 2021
e4d8bfe
Maint: don't pass data reference to only check cols
RDoerfel Dec 17, 2021
bad80ec
Enh: improve data handling in sensorset
RDoerfel Dec 17, 2021
4326c58
Doc: fix typo
RDoerfel Dec 17, 2021
d22b327
Enh: return reference to data rather then copying. onership stays at …
RDoerfel Dec 17, 2021
daa29ab
Enh: use reference of data
RDoerfel Dec 17, 2021
b95646d
Maint: remove init function
RDoerfel Dec 17, 2021
c66f763
Maint: only return projected data
RDoerfel Dec 17, 2021
7d64cdd
Maint: update tests to combine projectors and data
RDoerfel Dec 17, 2021
2a45bd9
Maint: reorder computeAmplitudes function
RDoerfel Dec 17, 2021
07b846f
Enh: declare modelParams at same time as data
RDoerfel Dec 17, 2021
0979211
Maint: update test to use new signal model
RDoerfel Dec 17, 2021
8c9f00b
Enh: move signal model to hpifit constructor and add as member
RDoerfel Dec 17, 2021
8ae2b12
Maint: use bBasic
RDoerfel Dec 17, 2021
33d6de1
Maint: remove access to member variable in signal;odel
RDoerfel Dec 19, 2021
4bd4df9
Maint: clean up test_hpiFit
RDoerfel Dec 19, 2021
4def4bc
Maint: remove inline access to members of HpiFit
RDoerfel Dec 19, 2021
b503858
Enh: make ModelParameter part of input to computeAmplitudes
RDoerfel Dec 19, 2021
48c7df1
Enh: check if modelparameters contain important frequencies
RDoerfel Dec 25, 2021
3bffa2a
Maint: implement fit function to be used instead of 3 splitted functions
RDoerfel Jan 8, 2022
3848e9f
Maint: Use struct for results and input to reduce number of inputs
RDoerfel Jan 8, 2022
e7a228b
Maint: remove old hpi functions and enforce usage of new functions
RDoerfel Jan 8, 2022
df1f28a
Maint: move sensors and channels to constructor
RDoerfel Jan 14, 2022
a0144f8
Maint: move hpiFitUpdater out of private member functions of HpiFit
RDoerfel Jan 14, 2022
91feb53
Enh: add ez to SensorSet -> no channel list needed any more
RDoerfel Jan 14, 2022
8cf4085
Maint: use SensorSet as struct created by SensorSetCreator
RDoerfel Jan 14, 2022
261c602
Maint: add sensor set to included
RDoerfel Jan 14, 2022
0086205
Enh: implement setModelParameters function
RDoerfel Jan 16, 2022
46c4c3e
Enh: move hpiDataUpdater out of HpiFit
RDoerfel Jan 18, 2022
e7c64a0
Enh: unify fitting and find order
RDoerfel Jan 21, 2022
09069dd
Maint: make compute functions private
RDoerfel Jan 21, 2022
15e2047
Maint: Make compute functions private and remove test of private func…
RDoerfel Jan 25, 2022
16ac28c
Maint: some renaming of variables and removing of redundant/old comments
RDoerfel Jan 27, 2022
d5089b4
Maint: remove dropCoil functions for now
RDoerfel Jan 27, 2022
7c969bb
Maint: fix documentation
RDoerfel Feb 5, 2022
c7a3f4d
Maint: rename ModelParameters to HpiModelParameters
RDoerfel Feb 5, 2022
90707ee
Enh,Maint: make HpiModelParameters a seperate class
RDoerfel Feb 5, 2022
a9edd55
Enh,Maint: make SensorSet a class and update everything accordingly
RDoerfel Feb 6, 2022
bbf8f07
Maint: add QVector to includes
RDoerfel Feb 7, 2022
6c7903d
Maint: adress request from PR, mainly add right version
RDoerfel Feb 9, 2022
a2c32cd
Maint: inititalize member in header instead of default constructor in…
RDoerfel Feb 9, 2022
d022201
Maint: make matAmplitudes return value
RDoerfel Feb 9, 2022
73af54f
Maint: return coils and compute gof seperately
RDoerfel Feb 9, 2022
89d0d25
Maint: refactor computeHeadPosition into smaller functions
RDoerfel Feb 9, 2022
c643d0e
Maint: Add constructor to CoilParam and make various sensorsets const…
RDoerfel Feb 9, 2022
fc49c24
Maint: fix definition of inline function
RDoerfel Feb 9, 2022
23fc430
Maint: rename function
RDoerfel Feb 9, 2022
a82c6f1
Maint: avoid copying of return values
RDoerfel Feb 9, 2022
162b27c
Maint: clean up
RDoerfel Feb 10, 2022
39b82f6
Fix: add QVector to includes
RDoerfel Feb 10, 2022
8829a6d
Maint: fix typos and email
RDoerfel Feb 12, 2022
4f7d96c
Maint: improve copying and rename variables
RDoerfel Feb 12, 2022
5649eba
Enh: add and test dimension check in hpiFit.fit()
RDoerfel Feb 12, 2022
977191d
Maint: make local variables const where possible
RDoerfel Feb 12, 2022
ae5f4c3
Enh: check if SensorSet has changed and test functionality
RDoerfel Feb 12, 2022
ecb94a2
Maint: restructure/re-organize HpiFit.fit()
RDoerfel Feb 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions applications/mne_scan/plugins/hpi/hpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <scMeas/realtimemultisamplearray.h>
#include <scMeas/realtimehpiresult.h>
#include <inverse/hpiFit/hpifit.h>
#include <inverse/hpiFit/hpidataupdater.h>

#include <fiff/fiff_info.h>
#include <fiff/c/fiff_digitizer_data.h>
Expand Down Expand Up @@ -547,14 +548,21 @@ void Hpi::run()
}

// init hpi fit
HpiModelParameters hpiModelParameters(m_vCoilFreqs,
m_pFiffInfo->sfreq,
m_pFiffInfo->linefreq,
false);
HpiFitResult fitResult;
fitResult.hpiFreqs = m_vCoilFreqs;
fitResult.errorDistances = QVector<double>(m_vCoilFreqs.size());
fitResult.devHeadTrans = m_pFiffInfo->dev_head_t;
fitResult.devHeadTrans.from = 1;
fitResult.devHeadTrans.to = 4;

FiffCoordTrans transDevHeadRef = m_pFiffInfo->dev_head_t;

HPIFit HPI = HPIFit(m_pFiffInfo, true);
HpiDataUpdater hpiDataUpdater = HpiDataUpdater(m_pFiffInfo);
HPIFit HPI = HPIFit(hpiDataUpdater.getSensors());

double dErrorMax = 0.0;
double dMeanErrorDist = 0.0;
Expand All @@ -571,6 +579,7 @@ void Hpi::run()
m_mutex.unlock();

MatrixXd matDataMerged(m_pFiffInfo->chs.size(), fittingWindowSize);
bool bOrder = false;

while(!isInterruptionRequested()) {
m_mutex.lock();
Expand All @@ -591,41 +600,40 @@ void Hpi::run()
m_mutex.lock();
if(m_bDoSingleHpi) {
m_bDoSingleHpi = false;
fitResult.devHeadTrans.clear();
fitResult = HpiFitResult();
}
fitResult.sFilePathDigitzers = m_sFilePathDigitzers;
m_mutex.unlock();

matDataMerged.block(0, iDataIndexCounter, matData.rows(), matDataMerged.cols()-iDataIndexCounter) =
matData.block(0, 0, matData.rows(), matDataMerged.cols()-iDataIndexCounter);

// Perform HPI fit
m_mutex.lock();
if(m_bDoFreqOrder) {
// find correct frequencie order if requested
HPI.findOrder(matDataMerged,
m_matCompProjectors,
fitResult.devHeadTrans,
m_vCoilFreqs,
fitResult.errorDistances,
fitResult.GoF,
fitResult.fittedCoils,
m_pFiffInfo);
m_bDoFreqOrder = false;
}
hpiModelParameters = HpiModelParameters(m_vCoilFreqs,
m_pFiffInfo->sfreq,
m_pFiffInfo->linefreq,
false);
hpiDataUpdater.checkForUpdate(m_pFiffInfo);
HPI.checkForUpdate(hpiDataUpdater.getSensors());

hpiDataUpdater.prepareDataAndProjectors(matDataMerged,m_matCompProjectors);
bOrder = m_bDoFreqOrder;
m_bDoFreqOrder = false;
m_mutex.unlock();

// Perform actual fitting
m_mutex.lock();
HPI.fitHPI(matDataMerged,
m_matCompProjectors,
fitResult.devHeadTrans,
m_vCoilFreqs,
fitResult.errorDistances,
fitResult.GoF,
fitResult.fittedCoils,
m_pFiffInfo);
m_mutex.unlock();
HPI.fit(hpiDataUpdater.getProjectedData(),
hpiDataUpdater.getProjectors(),
hpiModelParameters,
hpiDataUpdater.getHpiDigitizer(),
bOrder,
fitResult);

if(bOrder) {
m_mutex.lock();
m_vCoilFreqs = fitResult.hpiFreqs;
m_mutex.unlock();
}

//Check if the error meets distance requirement
if(fitResult.errorDistances.size() > 0) {
Expand Down
2 changes: 1 addition & 1 deletion examples/ex_hpiFit/ex_hpiFit.pro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#==============================================================================================================
#
# @file ex_hpiFit.pro
# @author Ruben Dörfel <[email protected]>;
# @author Ruben Dörfel <[email protected]>;
# @since 0.1.0
# @date January, 2020
#
Expand Down
67 changes: 37 additions & 30 deletions examples/ex_hpiFit/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//=============================================================================================================
/**
* @file main.cpp
* @author Ruben Dörfel <[email protected]>;
* @author Ruben Dörfel <[email protected]>;
* @since 0.1.0
* @date January, 2020
*
Expand Down Expand Up @@ -44,6 +44,7 @@
#include <fiff/fiff_dig_point_set.h>

#include <inverse/hpiFit/hpifit.h>
#include <inverse/hpiFit/hpidataupdater.h>

#include <utils/ioutils.h>
#include <utils/generics/applicationlogger.h>
Expand Down Expand Up @@ -105,8 +106,7 @@ int main(int argc, char *argv[])
QCommandLineOption inStep("step", "The step size in ms.", "in","10");
QCommandLineOption inFreqs("freqs", "The frequencies used.", "in","154,158,161,166");
QCommandLineOption inSave("save", "Store the fitting results [0,1].", "in","0");
QCommandLineOption inVerbose("verbose", "Print to command line [0,1].", "in","0");
QCommandLineOption inDebug("debug", "Save debug info during HPI fit [0,1].", "in","0");
QCommandLineOption inVerbose("verbose", "Print to command line [0,1].", "in","1");
QCommandLineOption inFast("fast", "Do fast fits [0,1].", "in","0");
QCommandLineOption outName("fileOut", "The output file name for movement data.", "out","position.txt");

Expand All @@ -116,7 +116,6 @@ int main(int argc, char *argv[])
parser.addOption(inFreqs);
parser.addOption(inSave);
parser.addOption(inVerbose);
parser.addOption(inDebug);
parser.addOption(inFast);
parser.addOption(outName);

Expand All @@ -138,7 +137,6 @@ int main(int argc, char *argv[])
QFile t_fileIn(parser.value(inFile));
bool bSave = parser.value(inSave).toInt();
bool bVerbose = parser.value(inVerbose).toInt();
bool bDoDebug = parser.value(inDebug).toInt();
bool bFast = parser.value(inFast).toInt();
QString sNameOut(parser.value(outName));

Expand Down Expand Up @@ -175,7 +173,7 @@ int main(int argc, char *argv[])
vecFreqs[i] = lFreqs[i].toInt();
}

QVector<double> vecError;
QVector<double> vecError(lFreqs.size());
VectorXd vecGoF;
FiffDigPointSet fittedPointSet;

Expand All @@ -202,7 +200,11 @@ int main(int argc, char *argv[])
// if debugging files are necessary set bDoDebug = true;
QString sHPIResourceDir = QCoreApplication::applicationDirPath() + "/HPIFittingDebug";

HPIFit HPI = HPIFit(pFiffInfo,bFast);
HpiDataUpdater hpiDataUpdater = HpiDataUpdater(pFiffInfo);
HPIFit HPI = HPIFit(hpiDataUpdater.getSensors());

MatrixXd matAmplitudes;
MatrixXd matCoilLoc(4,3);

// ordering of frequencies
from = first + vecTime(0);
Expand All @@ -214,14 +216,21 @@ int main(int argc, char *argv[])

// order frequencies
timer.start();
HPI.findOrder(matData,
matProjectors,
pFiffInfo->dev_head_t,
vecFreqs,
vecError,
vecGoF,
fittedPointSet,
pFiffInfo);
hpiDataUpdater.prepareDataAndProjectors(matData,matProjectors);
const auto& matProjectedData = hpiDataUpdater.getProjectedData();
const auto& matPreparedProjectors = hpiDataUpdater.getProjectors();
const auto& matCoilsHead = hpiDataUpdater.getHpiDigitizer();
HpiModelParameters hpiModelParameters(vecFreqs,
pFiffInfo->sfreq,
pFiffInfo->linefreq,
bFast);

HpiFitResult hpiFitResult;
HPI.fit(matProjectedData,matPreparedProjectors,hpiModelParameters,matCoilsHead,true,hpiFitResult);
hpiModelParameters = HpiModelParameters(hpiFitResult.hpiFreqs,
pFiffInfo->sfreq,
pFiffInfo->linefreq,
bFast);

float fTimer = 0.0;

Expand All @@ -240,30 +249,28 @@ int main(int argc, char *argv[])
}

timer.start();
HPI.fitHPI(matData,
matProjectors,
pFiffInfo->dev_head_t,
vecFreqs,
vecError,
vecGoF,
fittedPointSet,
pFiffInfo,
bDoDebug,
sHPIResourceDir);
hpiDataUpdater.prepareDataAndProjectors(matData,matProjectors);
const auto& matProjectedData = hpiDataUpdater.getProjectedData();
const auto& matPreparedProjectors = hpiDataUpdater.getProjectors();
HPI.fit(matProjectedData,
matPreparedProjectors,
hpiModelParameters,
matCoilsHead,
hpiFitResult);
fTimer = timer.elapsed();

HPIFit::storeHeadPosition(vecTime(i), pFiffInfo->dev_head_t.trans, matPosition, vecGoF, vecError);
HPIFit::storeHeadPosition(vecTime(i), hpiFitResult.devHeadTrans.trans, matPosition, hpiFitResult.GoF, hpiFitResult.errorDistances);
matPosition(i,9) = fTimer;
// if big head displacement occures, update debHeadTrans
if(MNEMath::compareTransformation(transDevHead.trans, pFiffInfo->dev_head_t.trans, fThreshRot, fThreshTrans)) {
transDevHead = pFiffInfo->dev_head_t;
if(MNEMath::compareTransformation(transDevHead.trans, hpiFitResult.devHeadTrans.trans, fThreshRot, fThreshTrans)) {
transDevHead = hpiFitResult.devHeadTrans;
qInfo() << "dev_head_t has been updated.";
}
if(bVerbose) {
qInfo() << "Iteration" << i << "Of" << vecTime.size()
<< " Duration " << fTimer << "ms"
<< " Error" << vecError[0]*1000 << vecError[1]*1000 << vecError[2]*1000 << vecError[3]*1000
<< " GoF" << vecGoF[0] << vecGoF[1] << vecGoF[2] << vecGoF[3];
<< " Error" << hpiFitResult.errorDistances[0]*1000 << hpiFitResult.errorDistances[1]*1000 << hpiFitResult.errorDistances[2]*1000 << hpiFitResult.errorDistances[3]*1000
<< " GoF" << hpiFitResult.GoF[0] << hpiFitResult.GoF[1] << hpiFitResult.GoF[2] << hpiFitResult.GoF[3];
}
}
qInfo() << "Iterations:" << vecTime.size()
Expand Down
Loading