50 using namespace Eigen;
51 using namespace MNELIB;
52 using namespace FIFFLIB;
54 #define MALLOC_14(x,t) (t *)malloc((x)*sizeof(t))
55 #define REALLOC_14(x,y,t) (t *)((x == NULL) ? malloc((y)*sizeof(t)) : realloc((x),(y)*sizeof(t)))
56 #define ALLOC_CMATRIX_14(x,y) mne_cmatrix_14((x),(y))
58 static void matrix_error_14(
int kind,
int nr,
int nc)
62 printf(
"Failed to allocate memory pointers for a %d x %d matrix\n",nr,nc);
64 printf(
"Failed to allocate memory for a %d x %d matrix\n",nr,nc);
66 printf(
"Allocation error for a %d x %d matrix\n",nr,nc);
67 if (
sizeof(
void *) == 4) {
68 printf(
"This is probably because you seem to be using a computer with 32-bit architecture.\n");
69 printf(
"Please consider moving to a 64-bit platform.");
71 printf(
"Cannot continue. Sorry.\n");
75 float **mne_cmatrix_14(
int nr,
int nc)
82 m = MALLOC_14(nr,
float *);
83 if (!m) matrix_error_14(1,nr,nc);
84 whole = MALLOC_14(nr*nc,
float);
85 if (!whole) matrix_error_14(2,nr,nc);
92 #define FREE_14(x) if ((char *)(x) != NULL) free((char *)(x))
94 #define FREE_CMATRIX_14(m) mne_free_cmatrix_14((m))
96 void mne_free_cmatrix_14 (
float **m)
104 void fromFloatEigenMatrix_14(
const Eigen::MatrixXf& from_mat,
float **& to_mat,
const int m,
const int n)
106 for (
int i = 0; i < m; ++i)
107 for (
int j = 0; j < n; ++j)
108 to_mat[i][j] = from_mat(i,j);
111 void fromFloatEigenMatrix_14(
const Eigen::MatrixXf& from_mat,
float **& to_mat)
113 fromFloatEigenMatrix_14(from_mat, to_mat, from_mat.rows(), from_mat.cols());
120 MneNamedMatrix::MneNamedMatrix()
131 float **data = ALLOC_CMATRIX_14(p_MneNamedMatrix.nrow,p_MneNamedMatrix.ncol);
134 for (j = 0; j < p_MneNamedMatrix.nrow; j++)
135 for (
k = 0;
k < p_MneNamedMatrix.ncol;
k++)
136 data[j][
k] = p_MneNamedMatrix.data[j][
k];
138 this->nrow = res->nrow;
139 this->ncol = res->ncol;
140 this->rowlist = res->rowlist;
141 this->collist = res->collist;
142 this->data = res->data;
149 FREE_CMATRIX_14(data);
156 const QStringList& rowlist,
157 const QStringList& collist,
163 mat->rowlist = rowlist;
164 mat->collist = collist;
173 int *pick_row = NULL;
174 int *pick_col = NULL;
175 QStringList my_pickrowlist;
176 QStringList my_pickcollist;
177 float **pickdata = NULL;
182 if (!pickrowlist.isEmpty() && this->rowlist.isEmpty()) {
183 printf(
"Cannot pick rows: no names for rows in original.");
186 if (!pickcollist.isEmpty() && this->collist.isEmpty()) {
187 printf(
"Cannot pick columns: no names for columns in original.");
190 if (pickrowlist.isEmpty())
191 picknrow = this->nrow;
192 if (pickcollist.isEmpty())
193 pickncol = this->ncol;
194 pick_row = MALLOC_14(picknrow,
int);
195 pick_col = MALLOC_14(pickncol,
int);
199 if (!pickrowlist.isEmpty()) {
200 for (j = 0; j < picknrow; j++) {
201 one = pickrowlist[j];
203 for (
k = 0;
k < this->nrow;
k++) {
204 if (QString::compare(one,this->rowlist[
k]) == 0) {
209 if (pick_row[j] == -1) {
210 printf(
"Row called %s not found in original matrix",one.toUtf8().constData());
213 my_pickrowlist = pickrowlist;
217 for (
k = 0;
k < picknrow;
k++)
219 my_pickrowlist = this->rowlist;
221 if (!pickcollist.isEmpty()) {
222 for (j = 0; j < pickncol; j++) {
223 one = pickcollist[j];
225 for (
k = 0;
k < this->ncol;
k++) {
226 if (QString::compare(one,this->collist[
k]) == 0) {
231 if (pick_col[j] == -1) {
232 printf(
"Column called %s not found in original matrix",one.toUtf8().constData());
235 my_pickcollist = pickcollist;
239 for (
k = 0;
k < pickncol;
k++)
241 my_pickcollist = this->collist;
246 pickdata = ALLOC_CMATRIX_14(picknrow,pickncol);
249 for (j = 0; j < picknrow; j++) {
251 for (
k = 0;
k < pickncol;
k++)
252 pickdata[j][
k] = data[row][pick_col[
k]];
270 QStringList colnames;
271 QStringList rownames;
275 QVector<qint32> dims;
287 if (tmp_node->type == FIFFB_MNE_NAMED_MATRIX) {
288 if(!tmp_node->find_tag(stream, kind, t_pTag))
292 QVector<qint32> dims;
293 t_pTag->getMatrixDimensions(ndim, dims);
296 qCritical(
"mne_read_named_matrix only works with two-dimensional matrices");
300 MatrixXf tmp_data = t_pTag->toFloatMatrix().transpose();
301 data = ALLOC_CMATRIX_14(tmp_data.rows(),tmp_data.cols());
302 fromFloatEigenMatrix_14(tmp_data, data);
305 for (
k = 0;
k < tmp_node->nchild();
k++) {
306 if (tmp_node->children[
k]->type == FIFFB_MNE_NAMED_MATRIX) {
307 if(tmp_node->children[
k]->find_tag(stream, kind, t_pTag)) {
308 t_pTag->getMatrixDimensions(ndim, dims);
310 qCritical(
"mne_read_named_matrix only works with two-dimensional matrices");
314 MatrixXf tmp_data = t_pTag->toFloatMatrix().transpose();
315 data = ALLOC_CMATRIX_14(tmp_data.rows(),tmp_data.cols());
316 fromFloatEigenMatrix_14(tmp_data, data);
318 tmp_node = tmp_node->children[
k];
329 if (!tmp_node->find_tag(stream, FIFF_MNE_NROW, t_pTag))
332 nrow = *t_pTag->toInt();
333 if (nrow != dims[0]) {
334 qCritical(
"Number of rows in the FIFF_MNE_NROW tag and in the matrix data conflict.");
341 if(!tmp_node->find_tag(stream, FIFF_MNE_NCOL, t_pTag))
344 ncol = *t_pTag->toInt();
345 if (ncol != dims[1]) {
346 qCritical(
"Number of columns in the FIFF_MNE_NCOL tag and in the matrix data conflict.");
350 if(!tmp_node->find_tag(stream, FIFF_MNE_ROW_NAMES, t_pTag)) {
351 s = t_pTag->toString();
352 rownames = FiffStream::split_name_list(s);
353 if (rownames.size() != nrow) {
354 qCritical(
"Incorrect number of entries in the row name list");
355 nrow = rownames.size();
359 if(!tmp_node->find_tag(stream, FIFF_MNE_COL_NAMES, t_pTag)) {
360 s = t_pTag->toString();
361 colnames = FiffStream::split_name_list(s);
362 if (colnames.size() != ncol) {
363 qCritical(
"Incorrect number of entries in the column name list");
364 ncol = colnames.size();
371 FREE_CMATRIX_14(data);