Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference Implementation: Julia #3

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions julia/NIfTIZarr.jl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Files generated by invoking Julia with --code-coverage
*.jl.cov
*.jl.*.cov

# Files generated by invoking Julia with --track-allocation
*.jl.mem

# System-specific files and directories generated by the BinaryProvider and BinDeps packages
# They contain absolute paths specific to the host computer, and so should not be committed
deps/deps.jl
deps/build.log
deps/downloads/
deps/usr/
deps/src/

# Build artifacts for creating documentation generated by the Documenter package
docs/build/
docs/site/

# File generated by Pkg, the package manager, based on a corresponding Project.toml
# It records a fixed state of all packages used by the project. As such, it should not be
# committed for packages, but should be committed for applications that require a static
# environment.
Manifest.toml
22 changes: 22 additions & 0 deletions julia/NIfTIZarr.jl/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name = "NIfTIZarr"
uuid = "37a98a6a-6d49-420d-b818-2675248c7c06"
authors = ["Yael Balbastre <[email protected]>"]
version = "0.1.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Zarr = "0a941bbe-ad1d-11e8-39d9-ab76183a1d99"
NIfTI = "a3a9e032-41b5-5fc4-967a-a6b7a19844d3"
EnumX = "4e289a0a-7415-4d19-859d-a7e5c4648b56"
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
GZip = "92fee26a-97fe-5a0c-ad85-20a5f3185b63"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat] # to check
Zarr = "0.7" # storage API change (added path)
NIfTI = "0.6"
EnumX = "1.0"
Images = ">=0.8"
julia = "1.6"
21 changes: 21 additions & 0 deletions julia/NIfTIZarr.jl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Example implementation of the nifti-zarr specification in JULIA

Convert a nifti file to a nifti-zarr storage.
```julia
import NIfTIZarr: nii2zarr
nii2zarr("path/to/nifti.nii.gz", "s3://path/to/bucket")
```

Convert a nifti-zarr storage to a nifti file.
The pyramid level can be selected with `level=L`, where 0 is the
base/finest level.
```julia
import NIfTIZarr: zarr2nii
zarr2nii("s3://path/to/bucket", "path/to/nifti.nii.gz"; level=0)
```

Load a nifti-zarr into a `NIVolume` object.
```julia
import NIfTIZarr: zarr2nii
nivol = zarr2nii("s3://path/to/bucket"; level=0)
```
246 changes: 246 additions & 0 deletions julia/NIfTIZarr.jl/notebooks/conversion.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/Dropbox (Partners HealthCare)/code/balbasty/nifti-zarr/Project.toml`\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/Dropbox (Partners HealthCare)/code/balbasty/nifti-zarr/Manifest.toml`\n"
]
}
],
"source": [
"import Pkg\n",
"Pkg.develop(path=\"..\")\n",
"\n",
"import NIfTIZarr: nii2zarr, zarr2nii\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"/Users/yb947/Dropbox/data/niizarr/sub-control01_T1w_level1_julia.nii.gz\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"PATH_NII = \"$(homedir())/Dropbox/data/niizarr/sub-control01_T1w.nii.gz\"\n",
"PATH_ZARR = \"$(homedir())/Dropbox/data/niizarr/sub-control01_T1w_julia.nii.zarr\"\n",
"PATH_NII0 = \"$(homedir())/Dropbox/data/niizarr/sub-control01_T1w_level0_julia.nii.gz\"\n",
"PATH_NII1 = \"$(homedir())/Dropbox/data/niizarr/sub-control01_T1w_level1_julia.nii.gz\"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"Base.Filesystem.rm(PATH_ZARR; recursive=true, force=true)\n",
"nii2zarr(PATH_NII, PATH_ZARR)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Warning: In the future, fill values will not be interpreted as missing values by default. \n",
"│ Please set the keyword argument `fill_as_missing` to a boolean accordingly. Setting to `true`\n",
"│ for now, but in the future `false` will be the default.\n",
"└ @ Zarr /Users/yb947/.julia/packages/Zarr/tmr2s/src/metadata.jl:182\n",
"┌ Warning: In the future, fill values will not be interpreted as missing values by default. \n",
"│ Please set the keyword argument `fill_as_missing` to a boolean accordingly. Setting to `true`\n",
"│ for now, but in the future `false` will be the default.\n",
"└ @ Zarr /Users/yb947/.julia/packages/Zarr/tmr2s/src/metadata.jl:182\n"
]
},
{
"data": {
"text/plain": [
"88×128×128 NIfTI.NIVolume{Float64, 3, Array{Float64, 3}}:\n",
"[:, :, 1] =\n",
" 1.357 1.59278 1.15461 1.40221 … 3.2627 4.23032 3.92529 2.51416\n",
" 1.68678 1.76973 1.63274 1.56868 3.49257 3.68682 3.93434 2.07428\n",
" 1.45721 1.63145 1.58165 1.4172 4.13475 4.79732 4.6226 2.42931\n",
" 1.69925 1.46471 1.43601 1.36439 4.57429 4.97035 4.39584 2.29437\n",
" 1.40136 1.27899 1.33121 1.36919 4.2912 4.70872 4.2165 2.45473\n",
" 1.28602 1.24542 1.21639 1.54462 … 3.9789 4.11125 4.73422 3.51948\n",
" 1.22865 1.36626 1.30932 1.46237 3.72477 4.11917 4.29881 3.00019\n",
" 1.08642 1.22378 1.30596 1.29892 4.06885 4.24407 4.29809 2.98617\n",
" 1.15746 1.05802 1.19913 1.18579 4.89318 4.60041 4.39017 2.49266\n",
" 1.59305 1.64865 2.07987 1.827 4.77972 4.62573 4.90107 2.39773\n",
" ⋮ ⋱ ⋮ \n",
" 1.8988 2.04685 1.68634 1.83424 2.39405 2.95284 2.76905 1.80408\n",
" 2.95865 2.87626 1.94597 1.84225 … 2.37085 2.87516 2.81667 2.07517\n",
" 3.69512 2.89118 1.91726 1.83967 2.42435 2.53215 3.40241 3.85031\n",
" 3.40958 3.10382 1.93668 1.66595 2.33986 2.83406 2.71042 1.46891\n",
" 1.6467 1.64079 1.44207 1.55783 2.45361 2.70208 2.77448 1.56535\n",
" 1.62883 1.71731 1.59183 1.68947 2.39216 2.74502 2.60914 1.24669\n",
" 1.8562 1.77936 1.19125 1.33149 … 2.29203 2.4514 1.9159 1.12135\n",
" 1.24088 1.35093 1.56063 1.77214 2.57145 2.76735 2.16129 1.20665\n",
" 1.21197 1.10896 1.44275 1.78821 2.17404 2.0966 2.23398 1.05109\n",
"\n",
"[:, :, 2] =\n",
" 1.43438 1.71098 1.50112 1.55965 … 3.14031 3.04088 3.16777 1.83256\n",
" 1.764 1.62288 1.75647 1.62165 3.40485 3.50771 4.23924 2.27938\n",
" 1.36998 1.61714 1.80452 1.52606 3.95623 4.25975 4.5158 2.67975\n",
" 1.55195 1.571 1.65805 1.64152 3.83503 4.53851 4.09135 2.6831\n",
" 1.65399 1.46628 1.48882 1.58637 4.23867 4.62805 4.14902 2.53774\n",
" 1.70546 1.39855 1.34914 1.33323 … 3.75185 4.06125 4.38279 2.97135\n",
" 1.83519 1.68586 1.43978 1.37402 3.55075 3.47083 3.8411 2.69463\n",
" 1.23271 1.51352 1.54674 1.65991 3.78235 3.31272 3.72457 2.38275\n",
" 1.46823 1.55733 1.83671 1.78892 4.10724 3.49368 3.30602 2.01975\n",
" 1.86673 1.48315 1.89374 1.95869 4.12345 3.98642 3.8535 2.27323\n",
" ⋮ ⋱ ⋮ \n",
" 2.04854 1.93758 1.73773 1.83671 2.63226 3.29729 3.21092 1.64701\n",
" 2.80843 2.4302 1.525 1.85575 … 2.44869 2.86777 3.10036 2.15439\n",
" 3.4653 2.97965 1.75715 1.96621 2.25209 2.4738 3.37928 3.90141\n",
" 4.10582 3.82363 1.90032 1.61527 2.11971 2.63485 2.58776 1.839\n",
" 1.54638 1.67765 1.56448 1.4784 2.13384 2.26822 2.43016 1.42046\n",
" 1.49186 1.6004 1.77617 1.86036 2.15884 2.24682 2.20466 1.30258\n",
" 1.93203 1.72646 1.73006 1.72763 … 2.19006 2.58145 2.07294 1.53433\n",
" 1.75215 1.71188 1.76014 1.68292 2.10476 2.61619 2.31862 1.53551\n",
" 1.41662 1.50966 1.65199 1.72882 2.17255 2.29342 2.56416 1.2272\n",
"\n",
"[:, :, 3] =\n",
" 1.45447 1.39946 1.71497 1.72248 … 3.23837 2.86939 2.97971 1.75254\n",
" 1.588 1.47563 1.56445 1.59111 3.42411 3.29554 3.78897 2.2778\n",
" 1.52613 1.6936 1.62732 1.45715 3.06856 3.47821 3.60062 1.93519\n",
" 1.68487 1.38851 1.50072 1.54873 2.98044 3.74508 3.63863 2.15971\n",
" 1.4906 1.3778 1.59961 1.54661 3.50072 4.12115 4.39875 3.27381\n",
" 1.62444 1.37224 1.69886 1.52737 … 3.38672 3.892 4.23806 2.81283\n",
" 1.35691 1.48736 1.52595 1.67267 3.7818 3.81279 4.61339 2.49706\n",
" 1.34012 1.57926 1.56791 1.97012 4.15824 4.08119 4.00561 2.75955\n",
" 1.46162 1.83515 2.00171 1.90024 4.44659 3.68892 3.34229 2.52708\n",
" 1.77913 2.18913 2.15865 2.09737 4.02312 3.98434 3.88309 2.59335\n",
" ⋮ ⋱ ⋮ \n",
" 1.9207 1.68214 1.43685 1.63338 2.86951 2.80342 2.56119 1.38803\n",
" 1.81444 1.8355 1.26882 1.2588 … 2.55888 2.91637 2.87506 1.41316\n",
" 1.99421 1.90442 1.20229 1.69347 2.1619 2.42659 2.65033 1.95861\n",
" 2.31642 2.29034 1.43663 1.79278 2.22865 2.39957 2.24065 1.49708\n",
" 1.42834 1.54763 1.69824 1.6077 2.28484 2.31776 2.37689 1.32171\n",
" 1.14791 1.43901 1.72212 1.66098 2.69995 2.80654 2.71949 1.47134\n",
" 1.46346 1.7129 1.89597 1.86367 … 2.7248 2.79492 2.59643 1.50133\n",
" 1.85584 1.85465 1.80461 1.54776 2.65738 2.53776 2.35061 1.4255\n",
" 2.1019 1.92472 1.94874 1.9252 2.27987 2.62855 2.31835 1.31463\n",
"\n",
";;; … \n",
"\n",
"[:, :, 126] =\n",
" 0.948734 0.883659 1.00251 … 1.30374 1.06609 1.20856 0.7441\n",
" 0.839563 1.15727 1.01221 1.36967 1.25564 1.47619 0.538766\n",
" 0.959702 1.03702 1.05715 1.51956 1.58813 1.60058 0.646945\n",
" 0.984698 1.0498 0.968773 1.57405 1.57038 1.30374 0.71597\n",
" 1.18625 1.17561 0.960204 1.29479 1.51104 1.49695 0.814069\n",
" 1.38572 1.05657 0.965485 … 1.27518 1.34662 1.51112 0.847014\n",
" 1.03731 1.08803 1.08334 1.52516 1.46626 1.35028 0.794648\n",
" 1.13177 1.1748 1.13171 1.48109 1.52687 1.44238 0.75571\n",
" 1.09348 1.06361 1.03788 1.4975 1.40539 1.24627 0.698204\n",
" 1.02364 1.06139 1.04602 1.4445 1.35153 1.13683 0.714981\n",
" ⋮ ⋱ ⋮ \n",
" 1.16879 1.14239 1.29535 1.23177 1.00725 1.2816 0.733979\n",
" 1.22908 1.05818 1.1717 … 1.30682 1.05041 1.0543 0.602555\n",
" 1.15152 0.916998 1.10414 1.27151 1.09899 1.14741 0.615706\n",
" 1.23529 0.963552 1.2224 1.23997 1.33232 1.58954 0.743184\n",
" 0.783101 0.865499 1.0314 1.09339 1.20747 1.3508 0.707541\n",
" 0.70045 0.905443 1.23835 1.13594 1.13522 1.2922 0.805321\n",
" 1.12574 1.36662 1.35096 … 1.15807 1.41222 1.23882 0.747729\n",
" 1.22653 1.09489 1.11344 1.298 1.32458 1.35833 0.703806\n",
" 1.2532 1.08641 1.01581 1.16314 1.51612 1.42116 0.863436\n",
"\n",
"[:, :, 127] =\n",
" 0.961934 1.01625 1.39392 1.33686 … 1.14224 1.34796 0.633942\n",
" 0.955435 1.12264 1.05252 1.31089 1.61212 1.72255 0.755992\n",
" 1.06604 0.990939 0.982047 1.50945 1.78166 1.92994 0.888357\n",
" 1.01792 1.11335 0.967425 1.23839 1.60884 1.4728 0.914809\n",
" 1.20744 1.34394 1.03329 1.08042 1.49726 1.5207 1.01235\n",
" 1.41233 1.07854 1.13411 1.28756 … 1.34038 1.50595 0.87348\n",
" 1.0639 0.942872 0.89819 1.03062 1.55421 1.32355 0.74044\n",
" 0.913582 1.0451 1.0208 0.976221 1.39133 1.3779 0.878592\n",
" 1.02216 1.09259 1.04501 0.996944 1.3139 1.28093 0.86989\n",
" 1.03489 1.05262 1.10897 1.01842 1.3184 1.09564 0.746787\n",
" ⋮ ⋱ ⋮ \n",
" 1.33144 1.10893 1.28263 1.4355 1.10776 1.27297 0.62796\n",
" 1.31673 1.0354 1.28069 1.20425 … 1.03677 1.17098 0.687918\n",
" 1.2214 1.2452 1.35566 0.993172 1.16286 1.24002 0.838187\n",
" 1.19516 1.06936 1.18436 1.08582 1.40989 1.34841 0.83577\n",
" 0.918455 1.18751 1.16187 1.10403 1.22639 1.34132 0.642707\n",
" 0.913276 1.23535 1.29452 1.07763 0.989565 1.10117 0.598383\n",
" 1.18587 1.29838 1.30831 1.16317 … 1.27463 1.27353 0.777536\n",
" 1.21928 0.961139 0.986386 1.06376 1.35767 1.17591 0.690446\n",
" 1.267 1.20985 1.30839 1.10933 1.41515 1.34566 0.983026\n",
"\n",
"[:, :, 128] =\n",
" 0.535765 0.631842 0.822765 0.706593 … 0.674699 0.849007 0.470105\n",
" 0.481257 0.502498 0.599324 0.690372 0.823602 0.987125 0.565625\n",
" 0.530189 0.5556 0.614905 0.849758 0.966873 1.12195 0.454767\n",
" 0.583437 0.566761 0.434739 0.668544 0.797212 0.872818 0.503655\n",
" 0.586878 0.576774 0.416648 0.585352 0.737466 0.702692 0.502115\n",
" 0.534955 0.425603 0.497979 0.579622 … 0.702771 0.693032 0.412116\n",
" 0.638919 0.531254 0.478862 0.45936 0.75821 0.672218 0.394128\n",
" 0.587414 0.504078 0.619495 0.67532 0.623851 0.696656 0.46504\n",
" 0.484093 0.55266 0.583579 0.669872 0.630375 0.604059 0.3987\n",
" 0.492043 0.627576 0.826248 0.669414 0.661946 0.558626 0.394894\n",
" ⋮ ⋱ ⋮ \n",
" 0.660428 0.722206 0.60854 0.632177 0.713089 0.713276 0.337343\n",
" 0.646061 0.589514 0.686182 0.624146 … 0.718902 0.58754 0.312289\n",
" 0.724258 0.922162 0.797074 0.420749 0.716054 0.631018 0.352095\n",
" 0.694618 0.600853 0.521169 0.455658 0.730584 0.646882 0.370528\n",
" 0.598762 0.7442 0.697167 0.520344 0.825367 0.819818 0.32897\n",
" 0.45521 0.61518 0.587057 0.568058 0.580297 0.760701 0.456735\n",
" 0.509076 0.574354 0.5325 0.602875 … 0.783706 0.885157 0.480425\n",
" 0.563556 0.508574 0.539496 0.656335 0.804593 0.762366 0.472935\n",
" 0.456855 0.685933 0.893084 0.730634 0.692565 0.681531 0.536124"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"zarr2nii(PATH_ZARR, PATH_NII0; level=0)\n",
"zarr2nii(PATH_ZARR, PATH_NII1; level=1)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.8.3",
"language": "julia",
"name": "julia-1.8"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
10 changes: 10 additions & 0 deletions julia/NIfTIZarr.jl/src/NIfTIZarr.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module NIfTIZarr

include("header.jl")
include("affines.jl")
include("nii2zarr.jl")
include("zarr2nii.jl")

export nii2json, nii2zarr

end # module
Loading