The main feature of this package is count_map
. For example count_map(itr)
returns a Dictionary
counting
the number of times each item in itr
occurs.
It differs from the one in StatsBase
in a few ways.
- It allows adding counts from any iterable rather than only
Vector
s. - It supports both
Dict
andDictionary
. - It is built on an abstraction that allows you update items via a function call.
- It does not use radix sort to optimize the case that the data are
Int
s.
Some tools to support Dict
and Dictionary
(and their abstract supertypes.)
In general, it is often not worth the trouble to support both Dict
and Dictionary
with a single interface.
But, sometimes it is, and these tools can be useful.
We defined _AbstractDict{T, V} = Union{AbstractDict{T,V}, AbstractDictionary{T,V}}
.
To make the interfaces more compatible one could define the pirate method Base.Dict(inds, vals) = Dict(zip(inds, vals))
.
But instead DictTools
defines construct(::Type{T<:_AbstractDict}, inds, vals)
which provides a common interface for
construction. The latter is less convenient, but is not piracy.
docstrings may be more up to date than what appears below.
-
count_map([::Type{T}=Dictionary], itr, filt = x -> true)
Return a dictionary of type
T
whose keys are elements ofitr
and whose values count how many times each occurs. Only elements ofitr
for whichfilt
returnstrue
are counted.count_map
has been tested forT
being eitherDict
orDictionary
.StatsBase.countmap
differs in that it has an optimization for integers and thatitr
of indetermiate length is first collected internally. -
add_counts!(dict::_AbstractDict{<:Any,V}, itr, ncounts=one(V)) where V
Add
ncounts
counts todict
for each key initr
. Ifncounts
is ommited, add one count for each key. -
_AbstractDict{T,V}
Either an
AbstractDict
or anAbstractDictionary
. A union type. Here_
indicates not a private identifier, but rather differentiates fromAbstractDict
. -
_Dict{T,V}
Either a
Dict
or aDictionary
. A union type -
update!(dict::Union{Dict,Dictionary}, _key, func, default)
If
dict
has key_key
, replace the value by the result of callingfunc
on the value. Otherwise insertdefault
for_key
.This function may work if
dict
is some other_AbstractDict
.In addition to dictionaries,
update!
andadd_counts!
also accept (some)AbstractVector
types -
update_map(::Type{T}=Dictionary, _keys, func, default)
Like
count_map
, but instead of incrementing an existing value by one, it is replaced by the result of callingfunc
on it. Furthermore, the default isdefault
rather than1
. -
DictTools.normalize(dict)
dict
is a dictionary of counts. The output is a dictionary with the same keys, and counts normalized to a probability distribution. -
Specialized method for
Dictionary
for the ZChop.jl package. -
construct
to construct aDict
andDictionary
with one API -
map_keys
,map_keys!
map_keys(dict::Dictionary, keymap_func, combine_func = +)Return a
Dictionary
whose keys are the image ofkeys(dict)
underkeymap_func
and whose values are created by accumulating withcombine_func
the values from the preimage of each key in the image ofkeymap_func
.For example, suppose
combine_func
is+
, andkeymap_func
isiseven
, and the only even keys indict
are in key-value pairs(2, 9)
,(4, 9)
,(6, 9)
. Then the outputDictionary
will contain the key-value pair(true, 27)
.map_keys
is useful for computing a marginal probability distribution. Ifdict
represents counts or a probability distribution, andcombine_func
is+
andkeymap_func
is many-to-one for some keys, thenmap_keys
effects marginalization of the distribution. -
collect_sparse
Convertdict
representing a sparse vector to a denseVector
. See doc string