85 unsigned int tsize = tag->size();
89 if (tag->data() ==
nullptr) {
90 qCritical(
"fiff_get_matrix_dims: no data available!");
94 qCritical(
"fiff_get_matrix_dims: tag does not contain a matrix!");
98 qCritical(
"fiff_get_matrix_dims: too small matrix data!");
106 qCritical(
"fiff_get_matrix_dims: unreasonable # of dimensions!");
111 qCritical(
"fiff_get_matrix_dims: too small matrix data!");
114 std::vector<int> res(ndim + 1);
117 for (
int k = 0; k < ndim; k++)
124 qCritical(
"fiff_get_matrix_sparse_dims: too small matrix data!");
127 std::vector<int> res(ndim + 2);
130 for (
int k = 0; k < ndim; k++)
132 res[ndim+1] = dims[-1];
136 qCritical(
"fiff_get_matrix_dims: unknown matrix coding.");
155, m_eigen(std::move(mat))
171 int cod,correct_size;
177 qWarning(
"[FiffSparseMatrix::fiff_get_float_sparse_matrix] wrong data type!");
186 qWarning(
"[FiffSparseMatrix::fiff_get_float_sparse_matrix] wrong # of dimensions!");
202 qWarning(
"[FiffSparseMatrix::fiff_get_float_sparse_matrix] Incomprehensible sparse matrix coding");
205 if (tag->size() != correct_size) {
206 qWarning(
"[FiffSparseMatrix::fiff_get_float_sparse_matrix] wrong data size!");
212 const float* src_data =
reinterpret_cast<const float*
>(tag->data());
213 const int* src_inds =
reinterpret_cast<const int*
>(src_data + nz);
214 const int* src_ptrs = src_inds + nz;
216 using T = Eigen::Triplet<float>;
217 std::vector<T> triplets;
218 triplets.reserve(nz);
221 for (
int row = 0; row < m; ++row) {
222 for (
int j = src_ptrs[row]; j < src_ptrs[row + 1]; ++j) {
223 triplets.push_back(T(row, src_inds[j], src_data[j]));
227 for (
int col = 0; col < n; ++col) {
228 for (
int j = src_ptrs[col]; j < src_ptrs[col + 1]; ++j) {
229 triplets.push_back(T(src_inds[j], col, src_data[j]));
234 Eigen::SparseMatrix<float> eigenMat(m, n);
235 eigenMat.setFromTriplets(triplets.begin(), triplets.end());
236 eigenMat.makeCompressed();
238 auto res = std::make_unique<FiffSparseMatrix>(std::move(eigenMat), cod);
248 for (j = 0, totalNz = 0; j < nrow; j++)
249 totalNz = totalNz + nnz[j];
252 qWarning(
"[FiffSparseMatrix::create_sparse_rcs] No nonzero elements specified.");
256 using T = Eigen::Triplet<float>;
257 std::vector<T> triplets;
258 triplets.reserve(totalNz);
260 for (j = 0; j < nrow; j++) {
261 for (k = 0; k < nnz[j]; k++) {
262 int col = colindex[j][k];
263 if (col < 0 || col >= ncol) {
264 qWarning(
"[FiffSparseMatrix::create_sparse_rcs] Column index out of range");
267 float val = vals ? vals[j][k] : 0.0f;
268 triplets.push_back(T(j, col, val));
272 Eigen::SparseMatrix<float> eigenMat(nrow, ncol);
273 eigenMat.setFromTriplets(triplets.begin(), triplets.end());
274 eigenMat.makeCompressed();
276 return std::make_unique<FiffSparseMatrix>(std::move(eigenMat),
FIFFTS_MC_RCS);
289 if (nRows != nCols) {
290 qWarning(
"[FiffSparseMatrix::mne_add_upper_triangle_rcs] input must be square");
295 Eigen::SparseMatrix<float> full = m_eigen + Eigen::SparseMatrix<float>(m_eigen.transpose());
298 for (
int k = 0; k < full.outerSize(); ++k) {
299 for (Eigen::SparseMatrix<float>::InnerIterator it(full, k); it; ++it) {
300 if (it.row() == it.col()) {
302 it.valueRef() = 0.5f * it.value();
307 full.makeCompressed();
308 return std::make_unique<FiffSparseMatrix>(std::move(full),
FIFFTS_MC_RCS);
316 if (mat.nonZeros() == 0)
320 result.m_eigen = mat.cast<
float>();
321 result.m_eigen.makeCompressed();
330 if (mat.nonZeros() == 0)
334 result.m_eigen = mat;
335 result.m_eigen.makeCompressed();
346 if (nRows != nCols) {
347 qWarning(
"[FiffSparseMatrix::pickLowerTriangleRcs] input must be square");
351 using T = Eigen::Triplet<float>;
352 std::vector<T> triplets;
353 triplets.reserve(m_eigen.nonZeros());
355 for (
int k = 0; k < m_eigen.outerSize(); ++k) {
356 for (Eigen::SparseMatrix<float>::InnerIterator it(m_eigen, k); it; ++it) {
357 if (it.row() >= it.col()) {
358 triplets.push_back(T(it.row(), it.col(), it.value()));
363 Eigen::SparseMatrix<float> lower(nRows, nCols);
364 lower.setFromTriplets(triplets.begin(), triplets.end());
365 lower.makeCompressed();
367 return std::make_unique<FiffSparseMatrix>(std::move(lower),
FIFFTS_MC_RCS);
Old fiff_type declarations - replace them.
fiff_int_t fiff_type_base(fiff_int_t type)
std::vector< int > fiff_get_matrix_dims(const FiffTag::UPtr &tag)
fiff_int_t fiff_type_matrix_coding(fiff_int_t type)
fiff_int_t fiff_type_fundamental(fiff_int_t type)
Header file describing the numerical values used in fif files.
#define FIFFC_MATRIX_MAX_DIM
FiffSparseMatrix class declaration.
FIFF file I/O and data structures (raw, epochs, evoked, covariance, forward).
unsigned char fiff_byte_t
static FiffSparseMatrix fromEigenSparse(const Eigen::SparseMatrix< double > &mat)
static std::vector< int > fiff_get_matrix_sparse_dims(const FIFFLIB::FiffTag::UPtr &tag)
FiffSparseMatrix::UPtr pickLowerTriangleRcs() const
static FiffSparseMatrix::UPtr create_sparse_rcs(int nrow, int ncol, int *nnz, int **colindex, float **vals)
static FiffSparseMatrix::UPtr fiff_get_float_sparse_matrix(const FIFFLIB::FiffTag::UPtr &tag)
std::unique_ptr< FiffSparseMatrix > UPtr
FiffSparseMatrix::UPtr mne_add_upper_triangle_rcs()
FIFFLIB::fiff_int_t coding
std::unique_ptr< FiffTag > UPtr