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()
133 float **data = ALLOC_CMATRIX_14(p_MneNamedMatrix.nrow,p_MneNamedMatrix.ncol);
136 for (j = 0; j < p_MneNamedMatrix.nrow; j++)
137 for (
k = 0;
k < p_MneNamedMatrix.ncol;
k++)
138 data[j][
k] = p_MneNamedMatrix.data[j][
k];
140 this->nrow = res->nrow;
141 this->ncol = res->ncol;
142 this->rowlist = res->rowlist;
143 this->collist = res->collist;
144 this->data = res->data;
151 FREE_CMATRIX_14(data);
158 const QStringList& rowlist,
159 const QStringList& collist,
165 mat->rowlist = rowlist;
166 mat->collist = collist;
175 int *pick_row = NULL;
176 int *pick_col = NULL;
177 QStringList my_pickrowlist;
178 QStringList my_pickcollist;
179 float **pickdata = NULL;
184 if (!pickrowlist.isEmpty() && this->rowlist.isEmpty()) {
185 printf(
"Cannot pick rows: no names for rows in original.");
188 if (!pickcollist.isEmpty() && this->collist.isEmpty()) {
189 printf(
"Cannot pick columns: no names for columns in original.");
192 if (pickrowlist.isEmpty())
193 picknrow = this->nrow;
194 if (pickcollist.isEmpty())
195 pickncol = this->ncol;
196 pick_row = MALLOC_14(picknrow,
int);
197 pick_col = MALLOC_14(pickncol,
int);
201 if (!pickrowlist.isEmpty()) {
202 for (j = 0; j < picknrow; j++) {
203 one = pickrowlist[j];
205 for (
k = 0;
k < this->nrow;
k++) {
206 if (QString::compare(one,this->rowlist[
k]) == 0) {
211 if (pick_row[j] == -1) {
212 printf(
"Row called %s not found in original matrix",one.toUtf8().constData());
215 my_pickrowlist = pickrowlist;
219 for (
k = 0;
k < picknrow;
k++)
221 my_pickrowlist = this->rowlist;
223 if (!pickcollist.isEmpty()) {
224 for (j = 0; j < pickncol; j++) {
225 one = pickcollist[j];
227 for (
k = 0;
k < this->ncol;
k++) {
228 if (QString::compare(one,this->collist[
k]) == 0) {
233 if (pick_col[j] == -1) {
234 printf(
"Column called %s not found in original matrix",one.toUtf8().constData());
237 my_pickcollist = pickcollist;
241 for (
k = 0;
k < pickncol;
k++)
243 my_pickcollist = this->collist;
248 pickdata = ALLOC_CMATRIX_14(picknrow,pickncol);
251 for (j = 0; j < picknrow; j++) {
253 for (
k = 0;
k < pickncol;
k++)
254 pickdata[j][
k] = data[row][pick_col[
k]];
272 QStringList colnames;
273 QStringList rownames;
277 QVector<qint32> dims;
289 if (tmp_node->type == FIFFB_MNE_NAMED_MATRIX) {
290 if(!tmp_node->find_tag(stream, kind, t_pTag))
294 QVector<qint32> dims;
295 t_pTag->getMatrixDimensions(ndim, dims);
298 qCritical(
"mne_read_named_matrix only works with two-dimensional matrices");
302 MatrixXf tmp_data = t_pTag->toFloatMatrix().transpose();
303 data = ALLOC_CMATRIX_14(tmp_data.rows(),tmp_data.cols());
304 fromFloatEigenMatrix_14(tmp_data, data);
307 for (
k = 0;
k < tmp_node->nchild();
k++) {
308 if (tmp_node->children[
k]->type == FIFFB_MNE_NAMED_MATRIX) {
309 if(tmp_node->children[
k]->find_tag(stream, kind, t_pTag)) {
310 t_pTag->getMatrixDimensions(ndim, dims);
312 qCritical(
"mne_read_named_matrix only works with two-dimensional matrices");
316 MatrixXf tmp_data = t_pTag->toFloatMatrix().transpose();
317 data = ALLOC_CMATRIX_14(tmp_data.rows(),tmp_data.cols());
318 fromFloatEigenMatrix_14(tmp_data, data);
320 tmp_node = tmp_node->children[
k];
331 if (!tmp_node->find_tag(stream, FIFF_MNE_NROW, t_pTag))
334 nrow = *t_pTag->toInt();
335 if (nrow != dims[0]) {
336 qCritical(
"Number of rows in the FIFF_MNE_NROW tag and in the matrix data conflict.");
343 if(!tmp_node->find_tag(stream, FIFF_MNE_NCOL, t_pTag))
346 ncol = *t_pTag->toInt();
347 if (ncol != dims[1]) {
348 qCritical(
"Number of columns in the FIFF_MNE_NCOL tag and in the matrix data conflict.");
352 if(!tmp_node->find_tag(stream, FIFF_MNE_ROW_NAMES, t_pTag)) {
353 s = t_pTag->toString();
354 rownames = FiffStream::split_name_list(s);
355 if (rownames.size() != nrow) {
356 qCritical(
"Incorrect number of entries in the row name list");
357 nrow = rownames.size();
361 if(!tmp_node->find_tag(stream, FIFF_MNE_COL_NAMES, t_pTag)) {
362 s = t_pTag->toString();
363 colnames = FiffStream::split_name_list(s);
364 if (colnames.size() != ncol) {
365 qCritical(
"Incorrect number of entries in the column name list");
366 ncol = colnames.size();
373 FREE_CMATRIX_14(data);