Skip to content

Commit

Permalink
Add option to supress conjugation on creation from numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
wtclarke committed Jan 12, 2023
1 parent 13e15f8 commit 2f08d8e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ __pycache__/
*.egg-info/
.vscode
tmp_notes
build

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
This document contains the nifti_mrs_tools release history in reverse chronological order.

0.1.2 (Thursday 12th January 2023)
----------------------------------
- Add option to prevent conjugation with numpy data. For spec2nii compatibility.

0.1.1 (Wednesday 11th January 2023)
-----------------------------------
- Make compliant with NIfTI-MRS V0.6
Expand Down
24 changes: 20 additions & 4 deletions src/nifti_mrs/create_nmrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ def _default_affine():
return default


def gen_nifti_mrs(data, dwelltime, spec_freq, nucleus='1H', affine=None, dim_tags=[None, None, None], nifti_version=2):
def gen_nifti_mrs(
data,
dwelltime,
spec_freq,
nucleus='1H',
affine=None,
dim_tags=[None, None, None],
nifti_version=2,
no_conj=False):
"""Generate NIfTI-MRS from data and required metadata
:param data: Complex-typed numpy array of at least 4 dimensions (max 7)
Expand All @@ -54,6 +62,8 @@ def gen_nifti_mrs(data, dwelltime, spec_freq, nucleus='1H', affine=None, dim_tag
:type dim_tags: list, optional
:param nifti_version: Version of NIfTI header format, defaults to 2
:type nifti_version: int, optional
:param no_conj: If true stops conjugation of data on creation, defaults to False
:type no_conj: bool, optional
:return: NIfTI-MRS object
:rtype: nifti_mrs.nifti_mrs.NIFTI_MRS
"""
Expand All @@ -74,10 +84,11 @@ def gen_nifti_mrs(data, dwelltime, spec_freq, nucleus='1H', affine=None, dim_tag
dwelltime,
hdr_ext,
affine=affine,
nifti_version=nifti_version)
nifti_version=nifti_version,
no_conj=no_conj)


def gen_nifti_mrs_hdr_ext(data, dwelltime, hdr_ext, affine=None, nifti_version=2):
def gen_nifti_mrs_hdr_ext(data, dwelltime, hdr_ext, affine=None, nifti_version=2, no_conj=False):
"""Generate NIfTI-MRS from data and header extension object
:param data: Complex-typed numpy array of at least 4 dimensions (max 7)
Expand All @@ -92,6 +103,8 @@ def gen_nifti_mrs_hdr_ext(data, dwelltime, hdr_ext, affine=None, nifti_version=2
:type dim_tags: list, optional
:param nifti_version: Version of NIfTI header format, defaults to 2
:type nifti_version: int, optional
:param no_conj: If true stops conjugation of data on creation, defaults to False
:type no_conj: bool, optional
:return: NIfTI-MRS object
:rtype: nifti_mrs.nifti_mrs.NIFTI_MRS
"""
Expand Down Expand Up @@ -133,4 +146,7 @@ def gen_nifti_mrs_hdr_ext(data, dwelltime, hdr_ext, affine=None, nifti_version=2
extension = nib.nifti1.Nifti1Extension(44, json_s.encode('UTF-8'))
header.extensions.append(extension)

return NIFTI_MRS(data, header=header)
if no_conj:
return NIFTI_MRS(data.conj(), header=header)
else:
return NIFTI_MRS(data, header=header)
8 changes: 8 additions & 0 deletions tests/test_create_nmrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ def test_gen_new_nifti_mrs_conj(tmp_path):
nucleus='1H')

assert np.allclose(nmrs[:], obj_in[:])

nmrs = gen_nifti_mrs(obj_in[:],
obj_in.dwelltime,
obj_in.spectrometer_frequency[0],
nucleus='1H',
no_conj=True)

assert np.allclose(nmrs[:], obj_in[:].conj())

0 comments on commit 2f08d8e

Please sign in to comment.