pack: Pack/Unpack values as low-fidelity representation into a...

Description Usage Arguments Value Packing Specification Overview Packing Specification - Integer Packing Specification - Double Packing Specification - Logical Packing Specification - Choice Packing Specification - Scaled Packing Specification - Colour Packing Specification - Custom Examples

View source: R/pack.R

Description

Pack/Unpack values as low-fidelity representation into a single 32-bit integer. These functions require a packing spec (pack_spec) which defines how values are converted to/from their low-fidelity representations.

Usage

1
2
3
pack(values, pack_spec)

unpack(int32, pack_spec)

Arguments

values

named vector or list of values

pack_spec

list describing the low-fidelity representation of each value

int32

standard R integer with the packed bits

Value

pack() takes a named list of values and returns an integer. unpack() takes an integer and returns a named list of values.

Packing Specification Overview

The packing specification (pack_spec) is a named list detailing how values should be converted to their low-fidelity representations.

The name of the values in the pack_spec correspond to the names in the values argument to pack().

The following are valid types for packing:

Packing Specification - Integer

Integers are packed by truncating leading bits that aren't needed e.g. the number 8 only needs 4 bis to represent it, and the other leading 28 bits can be ignored.

Packing an integer in this way is lossless - the reconstructed value using unpack() will be identical to the original value.

specification

Packing Specification - Double

Doubles are packed by truncating the mantissa and re-encoding the exponent. This will almost definitely lead to loss of precision, and any reconstructed value will not be identical to the original.

Packing Specification - Logical

Logical values only require a single bit, but more bits can be specified if desired.

Packing Specification - Choice

A choice is very similar to a factor, but the labels are only stored in the specification, and the index is 0-based (instead of 1-based)

Packing Specification - Scaled

Specifying the stored of a scaled value is sometimes easier than trying to work out how to corectly store a double precision floating point.

Packing Specification - Colour

Packing a colour is achieved by truncating the bits for each of the R, G and B channels separately.

Packing Specification - Custom

This packing specification allows you to specify a function to convert a value into a low-fidelity representation, and a matching function to reconstruct the original value.

See vignette('packing-specification', package = 'lofi') for more examples on using a custom packing specification.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Specify how to convert values to low-fidelity representation
pack_spec <- list(
  x      = list(type = 'double', float_name = 'bfloat16'),
  valid  = list(type = 'logical'),
  stars  = list(type = 'integer', mult = 10, nbits = 6),
  alpha  = list(type = 'scaled', max = 1, nbits = 5),
  grade  = list(type = 'choice', options = c('A', 'B', 'C', 'D'))
)

# Assemble the values
values <- list(
  x      = 1.234e21,
  valid  = TRUE,
  stars  = 4.5,     # Star rating 0-5. 1 decimal place.
  alpha  = 0.8,     # alpha. range [0, 1]
  grade  = 'B'
)

# pack them into a single integer
packed_values <- pack(values, pack_spec)

# Reconstruct the initial values from the packed values
unpack(packed_values, pack_spec)

#> $x
#> [1] 1.226708e+21
#>
#> $valid
#> [1] TRUE
#>
#> $stars
#> [1] 4.5
#>
#> $alpha
#> [1] 0.8064516
#>
#> $grade
#> [1] "B"

coolbutuseless/lofi documentation built on Nov. 4, 2019, 9:13 a.m.