rithwiks commited on
Commit
352e0d3
1 Parent(s): a6a92a9
Files changed (2) hide show
  1. utils/__init__.py +1 -0
  2. utils/eval_baselines.py +144 -0
utils/__init__.py CHANGED
@@ -0,0 +1 @@
 
 
1
+ from .create_splits import *
utils/eval_baselines.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ with fits.open(path) as hdul:
81
+ if dim == '2d':
82
+ arr = hdul[1].data[0][0]
83
+ elif dim == '2d_diffs' and len(hdul[1].data[0]) > 1:
84
+ arr = hdul[1].data[0][1] - hdul[1].data[0][0]
85
+ elif dim == '3dt' and len(hdul[1].data[0]) > 2:
86
+ arr = hdul[1].data[0][0:3]
87
+ else:
88
+ continue
89
+
90
+ ct += 1
91
+ if ct % 10 == 0:
92
+ print(df.mean())
93
+ df.to_csv(save_path)
94
+
95
+ for algo in ALL_CODECS.keys():
96
+ try:
97
+ if algo == "JPEG_2K" and dim == '3dt':
98
+ test_results = benchmark_imagecodecs_compression_algos(arr.transpose(1, 2, 0), algo)
99
+ else:
100
+ test_results = benchmark_imagecodecs_compression_algos(arr, algo)
101
+
102
+ for column, value in test_results.items():
103
+ if column in df.columns:
104
+ df.at[path, column] = value
105
+
106
+ except Exception as e:
107
+ print(f"Failed at {path} under exception {e}.")
108
+
109
+
110
+ if __name__ == "__main__":
111
+ parser = argparse.ArgumentParser(description="Process some 2D or 3D data.")
112
+ parser.add_argument(
113
+ "dimension",
114
+ choices=['2d', '2d_diffs', '3dt'],
115
+ help="Specify whether the data is 2d, 2d_diffs (compressing residuals between second and first exposures), or 3dt (3d time dimension)."
116
+ )
117
+ args = parser.parse_args()
118
+ dim = args.dimension.lower()
119
+
120
+ # RICE REQUIRES UNIQUE INPUT OF ARR SHAPE AND DTYPE INTO DECODER
121
+
122
+ if dim == '2d' or dim == '2d_diffs':
123
+ ALL_CODECS = {
124
+ "JPEG_XL_MAX_EFFORT": [jpegxl_encode_max_effort_preset, jpegxl_decode],
125
+ "JPEG_XL": [jpegxl_encode_preset, jpegxl_decode],
126
+ "JPEG_2K": [jpeg2k_encode, jpeg2k_decode],
127
+ "JPEG_LS": [jpegls_encode, jpegls_decode],
128
+ "RICE": [rcomp_encode, rcomp_decode],
129
+ }
130
+ else:
131
+ ALL_CODECS = {
132
+ "JPEG_XL_MAX_EFFORT": [jpegxl_encode_max_effort_preset, jpegxl_decode],
133
+ "JPEG_XL": [jpegxl_encode_preset, jpegxl_decode],
134
+ "JPEG_2K": [jpeg2k_encode, jpeg2k_decode],
135
+ }
136
+
137
+ columns = []
138
+ for algo in ALL_CODECS.keys():
139
+ columns.append(algo + "_BPD")
140
+ columns.append(algo + "_WRITE_RUNTIME")
141
+ columns.append(algo + "_READ_RUNTIME")
142
+ #columns.append(algo + "_TILE_DIVISOR")
143
+
144
+ main(dim)