| | """ |
| | Runs several baseline compression algorithms and stores results for each FITS file in a csv. |
| | This code is written functionality-only and cleaning it up is a TODO. |
| | """ |
| |
|
| |
|
| | import os |
| | import re |
| | from pathlib import Path |
| | import argparse |
| | import os.path |
| | from astropy.io import fits |
| | import numpy as np |
| | from time import time |
| | import pandas as pd |
| | from tqdm import tqdm |
| |
|
| | from astropy.io.fits import CompImageHDU |
| | from imagecodecs import ( |
| | jpeg2k_encode, |
| | jpeg2k_decode, |
| | jpegls_encode, |
| | jpegls_decode, |
| | jpegxl_encode, |
| | jpegxl_decode, |
| | rcomp_encode, |
| | rcomp_decode, |
| | ) |
| |
|
| | |
| |
|
| | jpegxl_encode_max_effort_preset = lambda x: jpegxl_encode(x, lossless=True, effort=9) |
| | jpegxl_encode_preset = lambda x: jpegxl_encode(x, lossless=True) |
| |
|
| | def find_matching_files(): |
| | """ |
| | Returns list of test set file paths. |
| | """ |
| | df = pd.read_json("./splits/full_test.jsonl", lines=True) |
| | return list(df['image']) |
| |
|
| | def benchmark_imagecodecs_compression_algos(arr, compression_type): |
| |
|
| | encoder, decoder = ALL_CODECS[compression_type] |
| |
|
| | write_start_time = time() |
| | encoded = encoder(arr) |
| | write_time = time() - write_start_time |
| |
|
| | read_start_time = time() |
| | if compression_type == "RICE": |
| | decoded = decoder(encoded, shape=arr.shape, dtype=np.uint16) |
| | else: |
| | decoded = decoder(encoded) |
| | read_time = time() - read_start_time |
| |
|
| | assert np.array_equal(arr, decoded) |
| |
|
| | buflength = len(encoded) |
| |
|
| | return {compression_type + "_BPD": buflength / arr.size, |
| | compression_type + "_WRITE_RUNTIME": write_time, |
| | compression_type + "_READ_RUNTIME": read_time, |
| | |
| | } |
| |
|
| | def main(dim): |
| |
|
| | save_path = f"baseline_results_{dim}.csv" |
| |
|
| | file_paths = find_matching_files() |
| | |
| | df = pd.DataFrame(columns=columns, index=[str(p) for p in file_paths]) |
| | |
| | print(f"Number of files to be tested: {len(file_paths)}") |
| | |
| | ct = 0 |
| |
|
| | for path in tqdm(file_paths): |
| | with fits.open(path) as hdul: |
| | if dim == '2d': |
| | arrs = [hdul[1].data[0][0]] |
| | elif dim == '2d_diffs' and len(hdul[1].data[0]) > 1: |
| | arrs = [hdul[1].data[0][i + 1] - hdul[1].data[0][i] for i in range(len(hdul[1].data[0]) - 1)] |
| | elif dim == '3dt' and len(hdul[1].data[0]) > 2: |
| | arrs = [hdul[1].data[0][0:3]] |
| | else: |
| | continue |
| |
|
| | ct += 1 |
| | if ct % 10 == 0: |
| | print(df.mean()) |
| | df.to_csv(save_path) |
| | for group, arr in enumerate(arrs): |
| | for algo in ALL_CODECS.keys(): |
| | try: |
| | if algo == "JPEG_2K" and dim == '3dt': |
| | test_results = benchmark_imagecodecs_compression_algos(arr.transpose(1, 2, 0), algo) |
| | else: |
| | test_results = benchmark_imagecodecs_compression_algos(arr, algo) |
| |
|
| | for column, value in test_results.items(): |
| | if column in df.columns: |
| | df.at[path + f"_{group}", column] = value |
| |
|
| | except Exception as e: |
| | print(f"Failed at {path} under exception {e}.") |
| |
|
| |
|
| | if __name__ == "__main__": |
| | parser = argparse.ArgumentParser(description="Process some 2D or 3D data.") |
| | parser.add_argument( |
| | "dimension", |
| | choices=['2d', '2d_diffs', '3dt'], |
| | help="Specify whether the data is 2d, 2d_diffs (compressing residuals between second and first exposures), or 3dt (3d time dimension)." |
| | ) |
| | args = parser.parse_args() |
| | dim = args.dimension.lower() |
| | |
| | |
| | |
| | if dim == '2d' or dim == '2d_diffs': |
| | ALL_CODECS = { |
| | "JPEG_XL_MAX_EFFORT": [jpegxl_encode_max_effort_preset, jpegxl_decode], |
| | "JPEG_XL": [jpegxl_encode_preset, jpegxl_decode], |
| | "JPEG_2K": [jpeg2k_encode, jpeg2k_decode], |
| | "JPEG_LS": [jpegls_encode, jpegls_decode], |
| | "RICE": [rcomp_encode, rcomp_decode], |
| | } |
| | else: |
| | ALL_CODECS = { |
| | "JPEG_XL_MAX_EFFORT": [jpegxl_encode_max_effort_preset, jpegxl_decode], |
| | "JPEG_XL": [jpegxl_encode_preset, jpegxl_decode], |
| | "JPEG_2K": [jpeg2k_encode, jpeg2k_decode], |
| | } |
| |
|
| | columns = [] |
| | for algo in ALL_CODECS.keys(): |
| | columns.append(algo + "_BPD") |
| | columns.append(algo + "_WRITE_RUNTIME") |
| | columns.append(algo + "_READ_RUNTIME") |
| | |
| | |
| | main(dim) |