-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataModel.cpp
156 lines (134 loc) · 4.19 KB
/
DataModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "DataModel.h"
#include <QDebug>
#include <QMap>
#include <algorithm>
struct DataModelData {
QMap<float, float> rawDataMap;
QList<QPair<float,float>>rawData;
zeroth::BioImage *imageR;
zeroth::BioImage *imageT;
int binsize = 500;
uint32_t imageSize =0;
QList<QPair<QString, float>> displayData;
QVector<float> displayDataVals;
void loadRawData() {
imageSize = imageR->width() * imageR->height();
for (uint32_t i = 0; i < imageSize; i++) {
rawDataMap.insertMulti(imageT->intensityAt(i), imageR->intensityAt(i));
}
QMapIterator<float, float> it(rawDataMap);
while (it.hasNext()) {
it.next();
QPair<float, float> pair(it.key(), it.value());
rawData.append(pair);
}
qDebug() << "Size of raw data" << rawData.size();
createDisplayData();
}
void createDisplayData() {
if(!imageSize) return;
displayData.clear();
// uint32_t totalNumberOfBins = imageSize/binsize;
// for(uint32_t bin = 0; bin < totalNumberOfBins; bin++) {
// QString binName = QString("%1-%2").arg(QString::number(bin)).arg(QString::number(bin*binsize));
// float binValue = 0;
// for(int index = bin*binsize; index < (bin*binsize) +binsize; index++) {
// if(index < rawData.size()) {
// binValue +=rawData.value(index).second;
// }
// }
// QPair<QString, float> dpair(binName, binValue);
// displayData.append(dpair);
// }
for(int index =0; index < rawData.size(); index++) {
int startIndex = index;
float binVal = 0;
int binValCount = 0;
for(int binIndex = index; binIndex < startIndex+binsize; binIndex++) {
if(binIndex < rawData.size()) {
binVal +=rawData.value(binIndex).second;
binValCount++;
} else{
break;
}
}
QString binName = QString("%1-%2").arg(QString::number(startIndex)).arg(QString::number(startIndex+binValCount));
binVal = binVal / binValCount;
QPair<QString, float> dpair(binName, binVal);
displayDataVals.append(binVal);
displayData.append(dpair);
index = (startIndex + binValCount) -1;
}
}
};
DataModel::DataModel(QObject *parent )
:QAbstractTableModel(parent), d(new DataModelData)
{
}
int DataModel::rowCount(const QModelIndex &) const
{
return d->displayData.size();
}
int DataModel::columnCount(const QModelIndex &) const
{
return 3;
}
QVariant DataModel::data(const QModelIndex &index, int role) const
{
if(role == I || (role == Qt::DisplayRole && index.column() == 0)){
return index.row();
}
else if(role == X || (role == Qt::DisplayRole && index.column() == 1)){
return d->displayData[index.row()].first;
} else {
// Y
return d->displayData[index.row()].second;
}
return QVariant();
}
QHash<int, QByteArray> DataModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Qt::DisplayRole] = "Display";
roles[I] = "I";
roles[X] = "X";
roles[Y] = "Y";
return roles;
}
void DataModel::setRawData(zeroth::BioImage *imageR, zeroth::BioImage *imageT)
{
d->imageR = imageR;
d->imageT = imageT;
beginResetModel();
d->loadRawData();
endResetModel();
emit modelUpdated();
}
void DataModel::setBinSize(int val)
{
if(val > d->imageSize)
val = d->imageSize;
d->binsize = val;
beginResetModel();
d->createDisplayData();
endResetModel();
emit modelUpdated();
}
int DataModel::binSize()
{
return d->binsize;
}
int DataModel::totatlNumberOfBins()
{
return d->imageSize / d->binsize;
}
float DataModel::yMin()
{
float m = *std::min_element(d->displayDataVals.constBegin(), d->displayDataVals.constEnd());
return m;
}
float DataModel::yMax()
{
float m = *std::max_element(d->displayDataVals.constBegin(), d->displayDataVals.constEnd());
return m;
}