Skip to content

Commit

Permalink
Enable Compression for NIFTI Conversion Script (#182)
Browse files Browse the repository at this point in the history
* Update convert_nifti.py

* fix formatting

* fixed compression
  • Loading branch information
hotzenklotz authored Feb 7, 2020
1 parent d2c4bab commit 91fee9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "/usr/local/Caskroom/miniconda/base/bin/python"
}
27 changes: 25 additions & 2 deletions wkcuber/convert_nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,18 @@ def to_target_datatype(data: np.ndarray, target_dtype) -> np.ndarray:
return (data / factor).astype(np.dtype(target_dtype))


def convert_nifti(source_nifti_path, target_path, layer_name, dtype, scale, mag=1):
def convert_nifti(
source_nifti_path, target_path, layer_name, dtype, scale, mag=1, file_len=256
):
target_wkw_info = WkwDatasetInfo(
str(target_path.resolve()), layer_name, mag, wkw.Header(np.dtype(dtype))
str(target_path.resolve()),
layer_name,
mag,
wkw.Header(
np.dtype(dtype),
block_type=wkw.Header.BLOCK_TYPE_LZ4HC,
file_len=file_len // 32,
),
)
ensure_wkw(target_wkw_info)

Expand Down Expand Up @@ -106,6 +115,20 @@ def convert_nifti(source_nifti_path, target_path, layer_name, dtype, scale, mag=
scale = tuple(map(float, source_nifti.header["pixdim"][:3]))

cube_data = to_target_datatype(cube_data, dtype)

# Writing wkw compressed requires files of shape (file_len, file_len, file_len)
# Pad data accordingly
padding_offset = file_len - np.array(cube_data.shape[1:4]) % file_len
cube_data = np.pad(
cube_data,
(
(0, 0),
(0, int(padding_offset[0])),
(0, int(padding_offset[1])),
(0, int(padding_offset[2])),
),
)

target_wkw.write(offset, cube_data)

logging.debug(
Expand Down

0 comments on commit 91fee9f

Please sign in to comment.