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

Add support for lossy mode #13

Merged
merged 18 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
12 changes: 7 additions & 5 deletions .github/workflows/cibuildwheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ on:

env:
CIBW_SKIP: '*-win32 *-manylinux*_i686 *-musllinux_*'
# This throws an Illegal instruction (core dumped) error in Ubuntu
# CIBW_TEST_REQUIRES: blosc2
CIBW_TEST_REQUIRES: blosc2==2.2.9
CIBW_TEST_REQUIRES: blosc2 pillow
CIBW_TEST_SKIP: "*macosx*arm64*"
CIBW_TEST_COMMAND: BTUNE_TRACE=1 python {project}/examples/btune_config.py
CIBW_TEST_COMMAND: 'BTUNE_TRACE=1 python {project}/examples/btune_config.py;
BTUNE_TRADEOFF="(0.3, 0.1, 0.6)" BTUNE_PERF_MODE=DECOMP BTUNE_TRACE=1 python {project}/examples/lossy.py {project}/examples/lossy_example.tif'
CIBW_BUILD_VERBOSITY: 1

jobs:
Expand Down Expand Up @@ -77,7 +76,10 @@ jobs:
CIBW_BEFORE_BUILD: bash prebuild.sh
CIBW_TEST_COMMAND: >
set BTUNE_TRACE=1 &&
python {project}/examples/btune_config.py
python {project}/examples/btune_config.py &&
set BTUNE_TRADEOFF="(0.3, 0.1, 0.6)" &&
set BTUNE_PERF_MODE=DECOMP &&
python {project}/examples/lossy.py {project}/examples/lossy_example.tif

- name: Build wheels (Linux / Mac OSX)
if: runner.os != 'Windows'
Expand Down
261 changes: 169 additions & 92 deletions README.md

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions blosc2_btune/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pathlib import Path
from enum import Enum
import ctypes
import numpy as np

VERSION = "1.1.2"

Expand Down Expand Up @@ -84,12 +85,23 @@ def set_params_defaults(**kwargs):
params.update(kwargs)
args = params.values()
args = list(args)
args[2] = ctypes.c_float(args[2])
args[5] = args[5].encode('utf-8')
# Insert the number of tradeoff values
if isinstance(args[2], (float, int)):
args.insert(3, 1)
args[2] = np.array(args[2], dtype=np.float32)
else:
args.insert(3, 3)
args[2] = np.array(args[2], dtype=np.float32)
# args[2] = ctypes.c_float(args[2])
args[6] = args[6].encode('utf-8')
# Get value of enums
args[1] = args[1].value
args[-1] = args[-1].value

lib.set_params_defaults.argtypes = [ctypes.c_uint] * 2 + [np.ctypeslib.ndpointer(dtype=np.float32)] + \
[ctypes.c_int, ctypes.c_bool, ctypes.c_int, ctypes.c_char_p] + \
[ctypes.c_uint] * 4

lib.set_params_defaults(*args)


Expand Down
9 changes: 8 additions & 1 deletion examples/btune_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ static int compress(const char* in_fname, const char* out_fname) {
// btune
btune_config btune_config = BTUNE_CONFIG_DEFAULTS;
//btune_config.perf_mode = BTUNE_PERF_DECOMP;
btune_config.tradeoff = .5;
btune_config.tradeoff[0] = .5;
btune_config.tradeoff_nelems = 1;
/* For lossy mode it would be
btune_config.tradeoff[0] = .5;
btune_config.tradeoff[1] = .2;
btune_config.tradeoff[2] = .3;
btune_config.tradeoff_nelems = 3;
*/
btune_config.behaviour.nhards_before_stop = 10;
btune_config.behaviour.repeat_mode = BTUNE_REPEAT_ALL;
btune_config.use_inference = 2;
Expand Down
52 changes: 52 additions & 0 deletions examples/lossy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
##############################################################################
# blosc2_grok: Grok (JPEG2000 codec) plugin for Blosc2
#
# Copyright (c) 2023 The Blosc Development Team <[email protected]>
# https://blosc.org
# License: GNU Affero General Public License v3.0 (see LICENSE.txt)
##############################################################################

import blosc2
import argparse
import numpy as np
from PIL import Image
import os
import blosc2_btune



if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Compress the given input image using Blosc2 and grok',
)
add_argument = parser.add_argument
add_argument('inputfile')
add_argument('-o', '--outputfile', type=str, help='File name from decompressed image')
args = parser.parse_args()
im = Image.open(args.inputfile)

# Set Btune parameters
base_dir = os.path.dirname(__file__)
kwargs = {
"tradeoff": (0.5, 0.3, 0.2), # (cratio tradeoff, speed tradeoff, quality tradeoff)
"perf_mode": blosc2_btune.PerformanceMode.COMP,
"models_dir": f"{base_dir}/models/"}
blosc2_btune.set_params_defaults(**kwargs)

# Convert the image to a numpy array
np_array = np.asarray(im)

cparams = {'tuner': blosc2.Tuner.BTUNE}
nchunks = 10

# Create a ndarray made out of the same image `nchunks` times
chunks = blocks = [1] + list(np_array.shape)
bl_array = blosc2.uninit(
shape = [nchunks] + list(np_array.shape),
chunks=chunks,
blocks=blocks,
mode="w",
cparams=cparams,
)
for i in range(nchunks):
bl_array[i,...] = np_array
Binary file added examples/lossy_example.tif
Binary file not shown.
8 changes: 4 additions & 4 deletions src/btune-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@
typedef struct {
int compcode;
// The compressor code
uint8_t compcode_meta;
// The compressor meta
uint8_t filter;
// The precompression filter
uint8_t filter_meta;
// The filter meta
int32_t splitmode;
// Whether the blocks should be split or not
int clevel;
// The compression level
int32_t blocksize;
// The block size
int32_t shufflesize;
// The shuffle size
int nthreads_comp;
// The number of threads used for compressing
int nthreads_decomp;
Expand All @@ -43,8 +45,6 @@ typedef struct {
// Control parameter for clevel
bool increasing_block;
// Control parameter for blocksize
bool increasing_shuffle;
// Control parameter for shufflesize
bool increasing_nthreads;
// Control parameter for nthreads
double score;
Expand Down
Loading
Loading