utils and baselines
Browse files- utils/__init__.py +1 -0
- utils/eval_baselines.py +134 -0
utils/__init__.py
CHANGED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from .create_splits import *
|
utils/eval_baselines.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Runs several baseline compression algorithms and stores results for each FITS file in a csv.
|
3 |
+
This code is written functionality-only and cleaning it up is a TODO.
|
4 |
+
"""
|
5 |
+
|
6 |
+
|
7 |
+
import os
|
8 |
+
import re
|
9 |
+
from pathlib import Path
|
10 |
+
import argparse
|
11 |
+
import os.path
|
12 |
+
from astropy.io import fits
|
13 |
+
import numpy as np
|
14 |
+
from time import time
|
15 |
+
import pandas as pd
|
16 |
+
from tqdm import tqdm
|
17 |
+
|
18 |
+
from astropy.io.fits import CompImageHDU
|
19 |
+
from imagecodecs import (
|
20 |
+
jpeg2k_encode,
|
21 |
+
jpeg2k_decode,
|
22 |
+
jpegls_encode,
|
23 |
+
jpegls_decode,
|
24 |
+
jpegxl_encode,
|
25 |
+
jpegxl_decode,
|
26 |
+
rcomp_encode,
|
27 |
+
rcomp_decode,
|
28 |
+
)
|
29 |
+
|
30 |
+
# Functions that require some preset parameters. All others default to lossless.
|
31 |
+
|
32 |
+
jpegxl_encode_max_effort_preset = lambda x: jpegxl_encode(x, lossless=True, effort=9)
|
33 |
+
jpegxl_encode_preset = lambda x: jpegxl_encode(x, lossless=True)
|
34 |
+
|
35 |
+
def find_matching_files():
|
36 |
+
"""
|
37 |
+
Returns list of test set file paths.
|
38 |
+
"""
|
39 |
+
df = pd.read_json("./splits/full_test.jsonl", lines=True)
|
40 |
+
return list(df['image'])
|
41 |
+
|
42 |
+
def benchmark_imagecodecs_compression_algos(arr, compression_type):
|
43 |
+
|
44 |
+
encoder, decoder = ALL_CODECS[compression_type]
|
45 |
+
|
46 |
+
write_start_time = time()
|
47 |
+
encoded = encoder(arr)
|
48 |
+
write_time = time() - write_start_time
|
49 |
+
|
50 |
+
read_start_time = time()
|
51 |
+
if compression_type == "RICE":
|
52 |
+
decoded = decoder(encoded, shape=arr.shape, dtype=np.uint16)
|
53 |
+
else:
|
54 |
+
decoded = decoder(encoded)
|
55 |
+
read_time = time() - read_start_time
|
56 |
+
|
57 |
+
assert np.array_equal(arr, decoded)
|
58 |
+
|
59 |
+
buflength = len(encoded)
|
60 |
+
|
61 |
+
return {compression_type + "_BPD": buflength / arr.size,
|
62 |
+
compression_type + "_WRITE_RUNTIME": write_time,
|
63 |
+
compression_type + "_READ_RUNTIME": read_time,
|
64 |
+
#compression_type + "_TILE_DIVISOR": np.nan,
|
65 |
+
}
|
66 |
+
|
67 |
+
def main(dim):
|
68 |
+
|
69 |
+
save_path = f"baseline_results_{dim}.csv"
|
70 |
+
|
71 |
+
file_paths = find_matching_files()
|
72 |
+
|
73 |
+
df = pd.DataFrame(columns=columns, index=[str(p) for p in file_paths])
|
74 |
+
|
75 |
+
print(f"Number of files to be tested: {len(file_paths)}")
|
76 |
+
|
77 |
+
ct = 0
|
78 |
+
|
79 |
+
for path in tqdm(file_paths):
|
80 |
+
for hdu_idx in [1, 4]:
|
81 |
+
with fits.open(path) as hdul:
|
82 |
+
if dim == '2d':
|
83 |
+
arr = hdul[hdu_idx].data
|
84 |
+
else:
|
85 |
+
raise RuntimeError(f"{dim} not applicable.")
|
86 |
+
|
87 |
+
ct += 1
|
88 |
+
if ct % 10 == 0:
|
89 |
+
print(df.mean())
|
90 |
+
df.to_csv(save_path)
|
91 |
+
|
92 |
+
for algo in ALL_CODECS.keys():
|
93 |
+
try:
|
94 |
+
if algo == "JPEG_2K" and dim != '2d':
|
95 |
+
test_results = benchmark_imagecodecs_compression_algos(arr.transpose(1, 2, 0), algo)
|
96 |
+
else:
|
97 |
+
test_results = benchmark_imagecodecs_compression_algos(arr, algo)
|
98 |
+
|
99 |
+
for column, value in test_results.items():
|
100 |
+
if column in df.columns:
|
101 |
+
df.at[path + f"_hdu{hdu_idx}", column] = value
|
102 |
+
|
103 |
+
except Exception as e:
|
104 |
+
print(f"Failed at {path} under exception {e}.")
|
105 |
+
|
106 |
+
|
107 |
+
if __name__ == "__main__":
|
108 |
+
parser = argparse.ArgumentParser(description="Process some 2D or 3D data.")
|
109 |
+
parser.add_argument(
|
110 |
+
"dimension",
|
111 |
+
choices=['2d'],
|
112 |
+
help="Specify whether the data is 2d, or; not applicable here: 3dt (3d time dimension), or 3dw (3d wavelength dimension)."
|
113 |
+
)
|
114 |
+
args = parser.parse_args()
|
115 |
+
dim = args.dimension.lower()
|
116 |
+
|
117 |
+
# RICE REQUIRES UNIQUE INPUT OF ARR SHAPE AND DTYPE INTO DECODER
|
118 |
+
|
119 |
+
ALL_CODECS = {
|
120 |
+
"JPEG_XL_MAX_EFFORT": [jpegxl_encode_max_effort_preset, jpegxl_decode],
|
121 |
+
"JPEG_XL": [jpegxl_encode_preset, jpegxl_decode],
|
122 |
+
"JPEG_2K": [jpeg2k_encode, jpeg2k_decode],
|
123 |
+
"JPEG_LS": [jpegls_encode, jpegls_decode],
|
124 |
+
"RICE": [rcomp_encode, rcomp_decode],
|
125 |
+
}
|
126 |
+
|
127 |
+
columns = []
|
128 |
+
for algo in ALL_CODECS.keys():
|
129 |
+
columns.append(algo + "_BPD")
|
130 |
+
columns.append(algo + "_WRITE_RUNTIME")
|
131 |
+
columns.append(algo + "_READ_RUNTIME")
|
132 |
+
#columns.append(algo + "_TILE_DIVISOR")
|
133 |
+
|
134 |
+
main(dim)
|