-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
551 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.