Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
uriele committed Jul 25, 2024
1 parent 56f0f4c commit 3d57470
Show file tree
Hide file tree
Showing 8 changed files with 551 additions and 6 deletions.
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,49 @@

[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://uriele.github.io/UnitfulData.jl/stable/)
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://uriele.github.io/UnitfulData.jl/dev/)
[![Build Status](https://github.com/uriele/UnitfulData.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/uriele/UnitfulData.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Coverage](https://codecov.io/gh/uriele/UnitfulData.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/uriele/UnitfulData.jl)
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
[![PkgEval](https://JuliaCI.github.io/NanosoldierReports/pkgeval_badges/U/UnitfulData.svg)](https://JuliaCI.github.io/NanosoldierReports/pkgeval_badges/U/UnitfulData.html)
[![Build Status](https://github.com/uriele/UnitfulData.jl/actions/workflows/CI.yml/badge.svg?branch=master)](https://github.com/uriele/UnitfulData.jl/actions/workflows/CI.yml?query=branch%3Amaster)

UnitfulData.jl is an extension for [Unitful.jl](https://github.com/PainterQubits/Unitful.jl) that includes units of Information commonly encountered in Information Theory and Data Storage.

The extension also includes the exported macro `@unit_custom_prefix` that allows the generation of prefixed units for a general unit using a custom base and exponents and the constant `prefixed_data` containing the most common units in metric (base 10) and IEC (binary) format.

# Defined Units and Dimensions

## Data
A dimension representing Information either as Storage, Memory, or Entropy.

- `bit` : the most basic unit of information in computing repesented an a binary digit

- `trit`: a ternary unit that encode the information with three non-negative numbers

- `Byte`: number of bits used to encode a character of text in the computer.

- `sh`: the shannon is the unit of information content associated with and event occuring with probability 1/2.

- `Hast`: the hartley is a unit of information and entropy on base 10

- `nat`: the natural unit of information is a unit of information and entropy based on the natural logarithms

## DataRate
A derived dimension that represate the unit of Information passing through a channel per unit of time.

- `bps`: the change of rate of information in bits/seconds

- `Bps`: the change of rate of information in Bytes/seconds

# Exported Macros and Functions

## From `Unitful`

- `@u_str`
- `@unit`
- `dimension`
- `uconvert`
- `ustrip`
- `upreferred`

## From `UnitfulData`

- `@unit_custom_prefix`
- `@data_allocated`
- `data_summery`
29 changes: 28 additions & 1 deletion src/UnitfulData.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
module UnitfulData
import Unitful
using Unitful: @u_str, @unit, @dimension, @derived_dimension,𝐓,s,uconvert,Unitlike
using Unitful: dimension,basefactor,unit,Unit,@refunit,@unit_symbols,abbr,Unit,basefactors_expr, FreeUnits
using Base: @__doc__, gc_bytes, summarysize
import Base.Experimental
export @u_str, @unit, @unit_custom_prefix, prefixed_data,abbr,Unitlike
export Unit,@unit_symbols,s,uconvert, absoluteunit, dimension, uconvert, ustrip, upreferred
export Data, DataRate
export bit, Byte, bps, Bps, sh, Hart, nat,dibit,tribit,nybl,trit
export kbit, Mbit, Gbit, Tbit, Pbit, Ebit, Zbit, Ybit, Kibit, Mibit, Gibit, Tibit, Pibit, Eibit, Zibit, Yibit
export KByte, MByte, GByte, TByte, PByte, EByte, ZByte, YByte, KiByte, MiByte, GiByte, TiByte, PiByte, EiByte, ZiByte, YiByte
export kbps, Mbps, Gbps, Tbps, Pbps, Ebps, Zbps, Ybps, KiBps, MiBps, GiBps, TiBps, PiBps, EiBps, ZiBps, YiBps
export KBps, MBps, GBps, TBps, PBps, EBps, ZBps, YBps, KIBps, MIBps, GIBps, TIBps, PIBps, EIBps, ZIBps, YIBps
export @data_allocated, data_summary
# Constants
const _log2_10 = log2(10);
const _log2_3 = log2(3);
const _log2_e = log2(ℯ);
include("./utils.jl")
include("./macro.jl")
include("./units.jl")

# Write your package code here.
const localunits = copy(Unitful.basefactors)
const localpromotion = copy(Unitful.promotion)
function __init__()
merge!(Unitful.basefactors, localunits)
merge!(Unitful.promotion, localpromotion) # only if you've used @dimension
Unitful.register(UnitfulData)
end

end
114 changes: 114 additions & 0 deletions src/macro.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""
const prefixed_data
A dictionary that contains the most common prefixes for unit of Information in metric and IEC format.
The keys of the dictionary are tuples of the form `(n, b)`, where `n` is the exponent and `b` is the base.
The values of the dictionary are strings representing the corresponding prefixes.
"""
const prefixed_data = Dict(
(3,10) => "k",
(6,10) => "M",
(9,10) => "G",
(12,10) => "T",
(15,10) => "P",
(18,10) => "E",
(21,10) => "Z",
(24,10) => "Y",
(10,2) => "Ki",
(20,2) => "Mi",
(30,2) => "Gi",
(40,2) => "Ti",
(50,2) => "Pi",
(60,2) => "Ei",
(70,2) => "Zi",
(80,2) => "Yi"
)

"""
@unit_custom_prefix(symb,abbr,name, _prefix,autodocs=false)
A macro to create prefixed units. The macro creates a unit with the specified symbol, abbreviation, and name.
The unit is prefixed by the values in `_prefix`. If `autodocs` is true, the macro will generate a docstring for the unit.
```julia-repl
julia> @unit utest "utest" uTests 1bit false
utest
julia> @unit_custom_prefix utest "utest" uTest _prefix=Dict((2,10)=>"hecto")
See also: [`@unit`](@ref), [`@unit_symbols`](@ref)
"""
macro unit_custom_prefix(symb,abbr,name, _prefix,autodocs=false)
expr = Expr(:block)
basefactor = (1.0, 1)
n=Meta.quot(Symbol(name))
user_dimension = :($Unitful.dimension($symb))

for ((k,kbase), value) in eval(_prefix)
symb_i= Symbol(string(value, symb))
name_i= Meta.quot(Symbol(value, name))

abbr_i = string(value, abbr)
scale_i = eval(:($float($kbase)^($k)))
scalefactor_i = :($scale_i * $symb)



@debug symb_i name_i
u = :($Unit{$name_i, $user_dimension}($k,1//1))
@debug u
if k == 0
ea = quote
Base.@__doc__ $symb_i
end
else
push!(expr.args,quote
$Unitful.@unit $symb_i $abbr_i $name_i $scalefactor_i false false
end)
docstring1 = """
$__module__.$symb_i
A prefixed unit, equal to $kbase^$k $symb.
Dimension: """
docstring2 = "\n\nSee also: [`$__module__.$symb`](@ref)."
ea = quote
if $autodocs
@doc $docstring1*string($user_dimension)*$docstring2 $symb_i
end
end
end
push!(expr.args, ea)
end

esc(expr)
end


"""
@data_allocated(ex,unit=Byte)
A macro to evaluate an expression, discarding the resulting value, instead returning the
total size of the allocated memory in a unit of `UnitfulData.Byte`. If `unit` is specified,
the result is converted to that unit. By default, the result is in Bytes.
See also [`@allocations`](@ref), [`data_summary`](@ref)
```julia-repl
julia> @data_allocated rand(10^6) MByte
8.000080 MBytes
```
"""
macro data_allocated(ex,unit=Byte)
quote
Experimental.@force_compile
local b0 = Ref{Int64}(0)
local b1 = Ref{Int64}(0)
gc_bytes(b0)
$(esc(ex))
gc_bytes(b1)
local memory_object = (b1[] - b0[])*Byte
eval(uconvert($unit,memory_object))
end
end
189 changes: 189 additions & 0 deletions src/units.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@

"""
$(@__MODULE__).Data
A dimension representing information.
"""
@dimension Data "Data" Information true


"""
$(@__MODULE__).bit
The bit, the SI base unit of information.
Dimension: $(@__MODULE__).Data
See also:
- [`$(@__MODULE__).Data`](@ref)
- [`$(@__MODULE__).sh`](@ref)
- [`$(@__MODULE__).Hart`](@ref)
- [`$(@__MODULE__).nat`](@ref)
- [`$(@__MODULE__).Byte`](@ref)
"""
@refunit bit "bit" Bits Data false


"""
$(@__MODULE__).Byte
The Byte, a SI unit of information, defined as 2^3 bits.
Dimension: $(@__MODULE__).Data
See also:
- [`$(@__MODULE__).Data`](@ref)
- [`$(@__MODULE__).bit`](@ref)
"""
@unit Byte "Byte" Bytes 8*bit false

@derived_dimension DataRate Data*𝐓^-1 true

"""
$(@__MODULE__).sh
The shannon, the SI base unit of information entropy.
The maximum entropy od n `bits` in n `shannon`.
Dimension: $(@__MODULE__).Data
See also:
- [`$(@__MODULE__).Data`](@ref)
- [`$(@__MODULE__).bit`](@ref)
- [`$(@__MODULE__).Hart`](@ref)
- [`$(@__MODULE__).nat`](@ref)
"""
@unit sh "sh" Shannon 1*bit true true



"""
$(@__MODULE__).sh
The hartley, a base 10 logaritmic unit of information entropy
defined as log2(10) sh.
Dimension: $(@__MODULE__).Data
See also:
- [`$(@__MODULE__).Data`](@ref)
- [`$(@__MODULE__).bit`](@ref)
- [`$(@__MODULE__).Hart`](@ref)
- [`$(@__MODULE__).nat`](@ref)
"""
@unit Hart "Hart" Hartley log2(10)*sh true true

"""
$(@__MODULE__).sh
The natural unit of information, a natural logaritmic unit of information entropy
defined as 1/log(2) sh.
Dimension: $(@__MODULE__).Data
See also:
- [`$(@__MODULE__).Data`](@ref)
- [`$(@__MODULE__).bit`](@ref)
- [`$(@__MODULE__).Hart`](@ref)
- [`$(@__MODULE__).shannon`](@ref)
"""
@unit nat "nat" Nat (1/log(2))*sh true true



"""
$(@__MODULE__).bps
The byte, a unit of data transfer rate, defined as 1 bit / s.
Dimension: $(@__MODULE__).DataRate
See also:
- [`$(@__MODULE__).DataRate`](@ref)
- [`$(@__MODULE__).Bps`](@ref)
"""
@unit bps "bps" Bits_Per_Seconds 1*bit/s false


"""
$(@__MODULE__).Bps
The byte per second, a unit of data transfer rate, defined as 1 byte / s.
Dimension: $(@__MODULE__).DataRate
See also:
- [`$(@__MODULE__).DataRate`](@ref)
- [`$(@__MODULE__).bps`](@ref)
"""
@unit Bps "Bps" Bytes_Per_Seconds 1*Byte/s false

#To Do: Add Quantum Information Units, need an @expscale and @expunit macro similar to @logscale and @logunit

@unit_custom_prefix bit "bit" Bits prefixed_data true
@unit_custom_prefix Byte "Byte" Bytes prefixed_data true
@unit_custom_prefix bps "bps" Bits_Per_Seconds prefixed_data true
@unit_custom_prefix Bps "Bps" Bytes_Per_Seconds prefixed_data true


# Arcaic and Unusual Units
"""
$(@__MODULE__).trit
The trit, a unit of information, defined as log2(3) bits.
Dimension: $(@__MODULE__).Data
See also:
- [`$(@__MODULE__).Data`](@ref)
- [`$(@__MODULE__).dit`](@ref)
- [`$(@__MODULE__).nybl`](@ref)
"""
@unit trit "trit" Trits _log2_3*bit true true
"""
$(@__MODULE__).tribit
The tribit, a unit of information, defined as 3 bits.
Dimension: $(@__MODULE__).Data
See also:
- [`$(@__MODULE__).Data`](@ref)
- [`$(@__MODULE__).dibit`](@ref)
- [`$(@__MODULE__).nybl`](@ref)
"""
@unit tribit "tribit" Tribits 3*bit true true

"""
$(@__MODULE__).dibit
The dibit, a unit of information, defined as 2*bits.
Dimension: $(@__MODULE__).Data
See also:
- [`$(@__MODULE__).Data`](@ref)
- [`$(@__MODULE__).tribit`](@ref)
- [`$(@__MODULE__).nybl`](@ref)
"""
@unit dibit "dibit" Dibits 2*bit true true



"""
$(@__MODULE__).nybl
The nibble, a unit of information, defined as 1/2 Bytes or 4 bits.
Dimension: $(@__MODULE__).Data
See also:
- [`$(@__MODULE__).Data`](@ref)
- [`$(@__MODULE__).dibit`](@ref)
- [`$(@__MODULE__).tribit`](@ref)
"""
@unit nybl "nybl" Nibbles 4*bit true true
Loading

0 comments on commit 3d57470

Please sign in to comment.