Skip to content

Commit 5c7e71a

Browse files
committed
[bug] fix #6, correctly parse big-endian nii file, bump 0.8.2
1 parent de5b0a0 commit 5c7e71a

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- Copyright: (C) Qianqian Fang (2019-2025) <q.fang at neu.edu>
66
- License: Apache License, Version 2.0
7-
- Version: 0.8.1
7+
- Version: 0.8.2
88
- URL: https://github.com/NeuroJSON/pyjdata
99
- Acknowledgement: This project is supported by US National Institute of Health (NIH)
1010
grant [U24-NS124027](https://reporter.nih.gov/project-details/10308329)

jdata/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121

122122
from .neurojson import neuroj, neurojgui
123123

124-
__version__ = "0.8.1"
124+
__version__ = "0.8.2"
125125
__all__ = [
126126
"loadjson",
127127
"savejson",

jdata/jnifti.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ def nii2jnii(filename, format="jnii", *varargin, **kwargs):
8484
dataendian = sys.byteorder
8585

8686
if nii["hdr"]["sizeof_hdr"] not in [348, 540]:
87-
nii["hdr"]["sizeof_hdr"] = struct.unpack(
88-
">I", struct.pack("<I", nii["hdr"]["sizeof_hdr"])
89-
)[0]
87+
value = nii["hdr"]["sizeof_hdr"]
88+
if not isinstance(value, np.ndarray):
89+
value = np.array(value)
90+
nii["hdr"]["sizeof_hdr"] = value.byteswap()
9091

9192
if nii["hdr"]["sizeof_hdr"] == 540: # NIFTI-2 format
9293
niftiheader = niiformat("nifti2")
@@ -104,14 +105,15 @@ def nii2jnii(filename, format="jnii", *varargin, **kwargs):
104105
if nii["hdr"]["dim"][0] > 7:
105106
names = list(nii["hdr"].keys())
106107
for name in names:
107-
nii["hdr"][name] = struct.unpack(
108-
">" + nii["hdr"][name][1:],
109-
struct.pack("<" + nii["hdr"][name][1:], nii["hdr"][name]),
110-
)[0]
108+
value = nii["hdr"][name]
109+
if not isinstance(value, np.ndarray):
110+
value = np.array(value)
111+
112+
# Swap bytes and change dtype
113+
nii["hdr"][name] = value.byteswap()
114+
111115
if nii["hdr"]["sizeof_hdr"] > 540:
112-
nii["hdr"]["sizeof_hdr"] = struct.unpack(
113-
">I", struct.pack("<I", nii["hdr"]["sizeof_hdr"])
114-
)[0]
116+
nii["hdr"]["sizeof_hdr"] = nii["hdr"]["sizeof_hdr"].byteswap()
115117

116118
type2byte = np.array(
117119
[

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
setup(
77
name="jdata",
88
packages=["jdata"],
9-
version="0.8.1",
9+
version="0.8.2",
1010
license="Apache license 2.0",
1111
description="JSON/binary JSON formats for exchanging Python and Numpy data",
1212
long_description=readme,

0 commit comments

Comments
 (0)