Skip to content

Commit dc015bb

Browse files
authored
Merge pull request #56 from thewtex/compress-encode-path
2 parents a38b626 + 3c33020 commit dc015bb

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

server/scripts/compress_encode.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def zip_zchunkstore(zip_file, url=None):
2828
2929
JSON-serializable reference description.
3030
"""
31-
rfs = {}
31+
zchunkstore = {}
3232
with zipfile.ZipFile(zip_file) as zf:
3333
if zf.compression != 0:
3434
raise RuntimeError("Compressed zip's are not supported.")
@@ -39,7 +39,6 @@ def zip_zchunkstore(zip_file, url=None):
3939
if url is not None:
4040
data_url = url
4141

42-
zchunkstore = {}
4342
for info in zf.infolist():
4443
name_bytes = len(info.filename.encode("utf-8"))
4544
offset = info.header_offset + 30 + name_bytes
@@ -50,7 +49,7 @@ def zip_zchunkstore(zip_file, url=None):
5049
else:
5150
zchunkstore[info.filename] = [data_url, offset, size]
5251

53-
return zchunkstore
52+
return zchunkstore
5453

5554

5655
def compress_encode(input_filepath,
@@ -60,10 +59,11 @@ def compress_encode(input_filepath,
6059
cname='zstd',
6160
clevel=5,
6261
shuffle=True,
63-
zip_chunk_store=True):
62+
zip_chunk_store=True,
63+
zip_store_url=None):
6464
image = itk.imread(input_filepath)
6565
image_da = itk.xarray_from_image(image)
66-
dataset_name = str(Path(input_filepath))
66+
dataset_name = Path(input_filepath).stem
6767
image_ds = image_da.to_dataset(name=dataset_name)
6868

6969
store_name = output_directory
@@ -88,7 +88,7 @@ def compress_encode(input_filepath,
8888
reduced = image
8989
while not np.all(np.array(itk.size(reduced)) < 64):
9090
scale = len(pyramid)
91-
shrink_factors = [2]*3
91+
shrink_factors = [2]*image.GetImageDimension()
9292
for i, s in enumerate(itk.size(reduced)):
9393
if s < 4:
9494
shrink_factors[i] = 1
@@ -120,7 +120,10 @@ def compress_encode(input_filepath,
120120
zip_store_path = str(Path(output_directory)) + '.zip'
121121
with zarr.storage.ZipStore(zip_store_path, mode='w', compression=0) as zip_store:
122122
zarr.copy_store(store, zip_store)
123-
zchunkstore = zip_zchunkstore(zip_store_path)
123+
store_url = None
124+
if zip_store_url:
125+
store_url = zip_store_url
126+
zchunkstore = zip_zchunkstore(zip_store_path, store_url)
124127
with open(zip_store_path + '.zchunkstore', 'w') as fp:
125128
json.dump(zchunkstore, fp)
126129

@@ -136,6 +139,7 @@ def compress_encode(input_filepath,
136139
parser.add_argument('--clevel', default=5, type=int, help='Compression level.')
137140
parser.add_argument('--no-multi-scale', action='store_true', help='Do not generate a multi-scale pyramid.')
138141
parser.add_argument('--no-zip-chunk-store', action='store_true', help='Do not generate a zip file and corresponding chunk store.')
142+
parser.add_argument('--zip-store-url', help='URL where the generated zip file will be hosted.')
139143

140144
args = parser.parse_args()
141145

@@ -146,4 +150,5 @@ def compress_encode(input_filepath,
146150
cname=args.cname,
147151
clevel=args.clevel,
148152
shuffle=not args.no_shuffle,
149-
zip_chunk_store=not args.no_zip_chunk_store)
153+
zip_chunk_store=not args.no_zip_chunk_store,
154+
zip_store_url=args.zip_store_url)

0 commit comments

Comments
 (0)