Issue when data contains empty spectra (edited)

I ran into an error with RobustSpecReg:

Error using lsqncommon
Objective function is returning undefined values at initial point. lsqnonlin cannot continue.

Turns out this is due to one of my data files (Philips MEGA-PRESS in nii.gz format) containing some spectra that are all zeros. I wonder whether it is possible to set Osprey to exclude empty spectra to avoid this issue?

Edit: actually, this is not specific to RbustSpecReg. Another datafile with zeroed spectra gave this error instead:

Index exceeds the number of array elements. Index must not exceed 0.

Error in osp_XReferencing (line 89)
HWHM=abs(tempx(gtHalfMax(1)) - tempx(gtHalfMax(end)))/2 * 0.8;

I found that in both cases I could work around this by manually editing the nifti-file to remove empty spectra pairwise. The reason we have this at all is probably because scanning was interrupted due to tolerability issues (other software like Gannet never cared about this).

Hi @JonasP,

What’s the shape of the data? And which elements contain the zeros? This might be a failing of spec2nii to know about the intricacies of the Philips MEGA-PRESS sequence.

Hi Jonas,

I’m glad to hear you were able to resolve your issue!

I don’t believe Osprey has an option/flag to ignore empty spectra, but—In case you aren’t already aware—there are some FID-A-derived functions built in to Osprey to edit your data: io_loadspec_niimrs & io_writeniimrs for reading and writing, respectively, and you can use op_takeaverages for including only the non-zero elements.

I’d also be keen to know the exact shape of the data, and if it’s not a spec2nii-specifc issue, would you be willing to share your partial data file? I’m happy to dig into this a little, as the partial-acquisition concern has also cropped up in another project.


The data is stored as a 5d matrix: [1 1 1 1024 320].

The last few spectra are all zeros. This occurs in 3 out of 135 data sets (most likely scanning was interrupted). To be specific, I can get an index of zero spectra in matlab with


Zeroed spectra are present in the SDAT as well.

Thank you. I did use the nii_tool(‘load’)/(‘save’) functions to edit the nifti files as a workaround.

Curiously, while the SDAT also contains the empty spectra, it does not trigger any error. However, Osprey mixes on and off spectra when using the SDAT version of the same data.

I may be able to share data privately if needed. nifti, sdat or both?

Yes, I’m unsure why there should be different treatment. Would you mind sending me both, please? I’d like to understand why this is the case. My email is Thanks again for raising this, @JonasP.

Let me know if it’s anything that spec2nii is getting wrong.

1 Like