Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
show_root_toc_entry: false
show_source: false

::: unpackqa.pack
selection:
members:
- pack_from_array
- pack_from_dict
docstring_style: numpy
rendering:
show_root_heading: false
show_root_toc_entry: false
show_source: false

::: unpackqa.product_loader
selection:
members:
Expand Down
59 changes: 59 additions & 0 deletions test/test_core_unpacking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import numpy as np
import pytest

from unpackqa.tools.unpackbits import unpackbits, int16_to_bits, packbits


def test_compare_unpack_methods():
"""
Core unpacking function should have the exact results as a slower
string based method
"""
arr = np.arange(2046)

method1 = np.array([int16_to_bits(int(x)) for x in arr], dtype=np.uint8)
method2 = unpackbits(arr, num_bits=16)

# bit axis at the end to match method1
method2 = np.moveaxis(method2, source = 0, destination = -1)

assert (method1 == method2).all()

@pytest.mark.parametrize('num_bits', [8,16,32])
def test_unpackbits_shape_retention(num_bits):
"""
Core unpacking function should take an arbitrary shape and return the
same with 1 new axis of length num_bits at axis position 0.
"""
high_range = (2**num_bits)-1
arr1 = np.random.randint(low=0, high=high_range, size=64)
arr2 = np.random.randint(low=0, high=high_range, size=64**2).reshape((64,64))
arr3 = np.random.randint(low=0, high=high_range, size=64**3).reshape((64,64,64))
arr4 = np.random.randint(low=0, high=high_range, size=64*21*42).reshape((64,21,42))

test_result = [
unpackbits(arr1, num_bits=num_bits).shape == (num_bits,64),
unpackbits(arr2, num_bits=num_bits).shape == (num_bits,64,64),
unpackbits(arr3, num_bits=num_bits).shape == (num_bits,64,64,64),
unpackbits(arr4, num_bits=num_bits).shape == (num_bits,64,21,42),
]

assert all(test_result)

@pytest.mark.parametrize('num_bits', [8,16,32])
def test_unpack_to_pack(num_bits):
"""packbits should reverse unpackbits"""
high_range = (2**num_bits)-1
arr1 = np.random.randint(low=0, high=high_range, size=64)
arr2 = np.random.randint(low=0, high=high_range, size=64**2).reshape((64,64))
arr3 = np.random.randint(low=0, high=high_range, size=64**3).reshape((64,64,64))
arr4 = np.random.randint(low=0, high=high_range, size=64*21*42).reshape((64,21,42))

test_results = [
(arr1 == packbits(unpackbits(arr1, num_bits=num_bits), num_bits=num_bits)).all(),
(arr2 == packbits(unpackbits(arr2, num_bits=num_bits), num_bits=num_bits)).all(),
(arr3 == packbits(unpackbits(arr3, num_bits=num_bits), num_bits=num_bits)).all(),
(arr4 == packbits(unpackbits(arr4, num_bits=num_bits), num_bits=num_bits)).all(),
]

assert all(test_results)
115 changes: 0 additions & 115 deletions test/test_output.py

This file was deleted.

68 changes: 68 additions & 0 deletions test/test_packing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pytest
import numpy as np

from unpackqa import (unpack_to_array,
unpack_to_dict,
pack_from_array,
pack_from_dict,
)



custom_spec1 = {'flag_info':{
'flag1_description':[0],
'flag2_description':[1],
'flag3_description':[2],
'flag4_description':[3],
'flag5_description':[4],
'flag6_description':[5,6,7],

},
'max_value' : 255,
'num_bits' : 8}

custom_spec2 = {'flag_info':{
'flag1_description':[0],
'flag2_description':[1],
'flag3_description':[2],
'flag4_description':[3],
'flag5_description':[4,5],
'flag6_description':[6],
'flag7_description':[7,8,9],
'flag8_description':[10,11],
'flag10_description':[12],
'flag11_description':[13],
'flag12_description':[14],
'flag13_description':[15],
},
'max_value' : 65535,
'num_bits' : 16}

product_test_cases = [custom_spec1, custom_spec2]

@pytest.mark.parametrize('product_spec', product_test_cases)
def test_array_packing(product_spec):
high_range = (2**product_spec['num_bits'])-1
qa_array = np.random.randint(low=0, high=high_range, size=32**3).reshape((32,32,32))

flag_array = unpack_to_array(qa_array, product=product_spec, flags='all')
packed_array = pack_from_array(flag_array, product=product_spec, flags='all')

assert (qa_array == packed_array).all()

@pytest.mark.parametrize('product_spec', product_test_cases)
def test_dict_packing(product_spec):
high_range = (2**product_spec['num_bits'])-1
qa_array = np.random.randint(low=0, high=high_range, size=32**3).reshape((32,32,32))

flag_array = unpack_to_dict(qa_array, product=product_spec, flags='all')
packed_array = pack_from_dict(flag_array, product=product_spec, flags='all')

assert (qa_array == packed_array).all()







21 changes: 17 additions & 4 deletions test/test_qa_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
import numpy as np

from unpackqa import (unpack_to_array,
unpack_to_dict,
list_products,
list_qa_flags
)
unpack_to_dict,
pack_from_array,
pack_from_dict,
list_products,
list_qa_flags
)

from unpackqa.product_loader import all_products

# Given a list of actual QA values, ensure the flags are as expected.
# These values and resulting flags are
Expand Down Expand Up @@ -69,3 +73,12 @@ def test_confirm_wrong_flag_values(product, test_qa_value, expected_output):
first_key = list(output.keys())[0]
output[first_key] += 1
assert output != expected_output

@pytest.mark.parametrize('product, test_qa_value, expected_output', qa_test_cases)
def test_confirm_repacked_qa_values(product, test_qa_value, expected_output):
"""unpack -> pack should have the same result"""
unpacked_array = unpack_to_array(test_qa_value, product=product, flags='all')
# pack_to_ functions do not currently take a preconfigured product.
product_spec = all_products[product]
repacked_qa = pack_from_array(unpacked_array, product=product_spec, flags='all')
assert (test_qa_value == repacked_qa).all()
Loading