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
Vectors. - It supports both
DictandDictionary. - 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
Ints.
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
Twhose keys are elements ofitrand whose values count how many times each occurs. Only elements ofitrfor whichfiltreturnstrueare counted.count_maphas been tested forTbeing eitherDictorDictionary.StatsBase.countmapdiffers in that it has an optimization for integers and thatitrof indetermiate length is first collected internally. -
add_counts!(dict::_AbstractDict{<:Any,V}, itr, ncounts=one(V)) where VAdd
ncountscounts todictfor each key initr. Ifncountsis ommited, add one count for each key. -
_AbstractDict{T,V}Either an
AbstractDictor anAbstractDictionary. A union type. Here_indicates not a private identifier, but rather differentiates fromAbstractDict. -
_Dict{T,V}Either a
Dictor aDictionary. A union type -
update!(dict::Union{Dict,Dictionary}, _key, func, default)If
dicthas key_key, replace the value by the result of callingfuncon the value. Otherwise insertdefaultfor_key.This function may work if
dictis some other_AbstractDict.In addition to dictionaries,
update!andadd_counts!also accept (some)AbstractVectortypes -
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 callingfuncon it. Furthermore, the default isdefaultrather than1. -
DictTools.normalize(dict)dictis a dictionary of counts. The output is a dictionary with the same keys, and counts normalized to a probability distribution. -
Specialized method for
Dictionaryfor the ZChop.jl package. -
constructto construct aDictandDictionarywith one API -
map_keys,map_keys!map_keys(dict::Dictionary, keymap_func, combine_func = +)Return a
Dictionarywhose keys are the image ofkeys(dict)underkeymap_funcand whose values are created by accumulating withcombine_functhe values from the preimage of each key in the image ofkeymap_func.For example, suppose
combine_funcis+, andkeymap_funcisiseven, and the only even keys indictare in key-value pairs(2, 9),(4, 9),(6, 9). Then the outputDictionarywill contain the key-value pair(true, 27).map_keysis useful for computing a marginal probability distribution. Ifdictrepresents counts or a probability distribution, andcombine_funcis+andkeymap_funcis many-to-one for some keys, thenmap_keyseffects marginalization of the distribution. -
collect_sparseConvertdictrepresenting a sparse vector to a denseVector. See doc string