<jupyter_start><jupyter_text>Load a simple 4D example of preprocessed data<jupyter_code>nifti_mrs_file = 'svs_preprocessed.nii.gz';<jupyter_output><empty_output><jupyter_text>Load using the nibabel interface<jupyter_code>img = nib.load(nifti_mrs_file)<jupyter_output><empty_output><jupyter_text>Examine dataComplex data type must be specified.<jupyter_code>data = img.get_fdata(dtype=np.complex64)
print(f'Data shape = {data.shape}')<jupyter_output>Data shape = (1, 1, 1, 4096)<jupyter_text>Data is stored in the time-domain. Plot the absolute and real component.<jupyter_code>fig,axes = plt.subplots(1,2,figsize=(15,4))
axes[0].plot(np.abs(data[0,0,0,:]),color='k')
axes[1].plot(np.real(data[0,0,0,:]),color='r')
plt.savefig('plots/data.png')<jupyter_output><empty_output><jupyter_text>Examine NIfTI header<jupyter_code>header = img.header
print(f'intent_name: {header.get_intent()[2]}')
print(f'Position information: \n {header.get_best_affine()}\n')

dwelltime = header['pixdim'][4]
print(f'Dwelltime: {dwelltime} s\n')

print(header)<jupyter_output>intent_name: mrs_v0_2
Position information: 
 [[ 20.          -0.          -0.         -32.90067836]
 [ -0.         -20.          -0.          10.6633755 ]
 [  0.           0.         -20.          21.35589036]
 [  0.           0.           0.           1.        ]]

Dwelltime: 8.33e-05 s

<class 'nibabel.nifti2.Nifti2Header'> object, endian='<'
sizeof_hdr      : 540
magic           : b'n+2'
eol_check       : [13 10 26 10]
datatype        : complex64
bitpix          : 64
dim             : [   4    1    1    1 4096    1    1    1]
intent_p1       : 0.0
intent_p2       : 0.0
intent_p3       : 0.0
pixdim          : [1.00e+00 2.00e+01 2.00e+01 2.00e+01 8.33e-05 1.00e+00 1.00e+00 1.00e+00]
vox_offset      : 0
scl_slope       : nan
scl_inter       : nan
cal_max         : 0.0
cal_min         : 0.0
slice_duration  : 0.0
toffset         : 0.0
slice_start     : 0
slice_end       : 0
descrip         : b''
aux_file        : b''
qform_code      : aligned
sform_code      : aligned
quatern_b       : [...]<jupyter_text>Examine NIfTI MRS header extension<jupyter_code>hdr_ext_codes = img.header.extensions.get_codes()
mrs_hdr_ext = json.loads(img.header.extensions[hdr_ext_codes.index(44)].get_content())
mrs_hdr_ext<jupyter_output><empty_output><jupyter_text>Plot spectrum on frequency and ppm axis<jupyter_code>spectrometer_frequency = mrs_hdr_ext['SpectrometerFrequency'][0]
bandwidth = 1/dwelltime
frequency_axis = np.linspace(bandwidth/2, -bandwidth/2, data.shape[3])
ppm_axis = 4.65+frequency_axis/spectrometer_frequency

spectrum = np.fft.fftshift(np.fft.fft(data[0,0,0,:]))

fig,(ax1,ax2) = plt.subplots(2,1,figsize=(15,7))
ax1.plot(frequency_axis,np.real(spectrum))
ax2.plot(ppm_axis,np.real(spectrum))

ax1.invert_xaxis()
ax2.invert_xaxis()

ax1.set_xlabel('$|\omega|-|\omega_{ref}|$ (Hz)')
ax2.set_xlabel('$\delta$ (ppm)')

plt.savefig('plots/spectra.png')<jupyter_output><empty_output><jupyter_text>Load more complex SVS example<jupyter_code>nifti_mrs_file_2 = 'svs_suppressed.nii.gz';
img2 = nib.load(nifti_mrs_file_2)

print(f'Data shape: {img2.get_fdata(dtype=np.complex64).shape}')<jupyter_output>Data shape: (1, 1, 1, 4096, 32, 64)<jupyter_text>Use header extension to find out what the higher dimensions are used for<jupyter_code>hdr_ext_codes = img2.header.extensions.get_codes()
mrs_hdr_ext2 = json.loads(img2.header.extensions[hdr_ext_codes.index(44)].get_content())
mrs_hdr_ext2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14158529580700746, 'fraction_numerical': 0.0617461229178633, 'mean_word_length': 3.9615384615384617, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '214997', 'license': 'MIT', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1405, 'n_words': 219, 'path': 'interactive_example.ipynb', 'repo_name': 'wtclarke/nifti_mrs_python_example', 'size': '3482'}