Skip to content

OmniTools.ForCollections

Exported

OmniTools.ForCollections.dict_to_namedtuple Function
julia
dict_to_namedtuple(d::AbstractDict)

Convert a nested dictionary to a NamedTuple.

Arguments

  • d::AbstractDict: The input dictionary to convert

Returns

  • A NamedTuple with the same structure as the input dictionary

Examples

julia
julia> using OmniTools

julia> dict_to_namedtuple(Dict(:a => 1, :b => 2))
(a = 1, b = 2)
Code
julia
function dict_to_namedtuple(dict::AbstractDict)
    for k  keys(dict)
        if dict[k] isa Array{Any,1}
            dict[k] = [v for v  dict[k]]
        elseif dict[k] isa DataStructures.OrderedDict
            dict[k] = dict_to_namedtuple(dict[k])
        end
    end
    dict_tuple = NamedTuple{Tuple(Symbol.(keys(dict)))}(values(dict))
    return dict_tuple
end
OmniTools.ForCollections.drop_empty_namedtuple_fields Function
julia
drop_empty_namedtuple_fields(tpl::NamedTuple)

Remove all empty fields from a NamedTuple.

Arguments

  • tpl: The input NamedTuple

Returns

  • A new NamedTuple with empty fields removed

Examples

julia
julia> using OmniTools

julia> drop_empty_namedtuple_fields((a=(;), b=(x=1,)))
(b = (x = 1,),)
Code
julia
function drop_empty_namedtuple_fields(nt::NamedTuple)
    indx = findall(x -> x != NamedTuple(), values(nt))
    nkeys, nvals = tuple(collect(keys(nt))[indx]...), values(nt)[indx]
    return NamedTuple{nkeys}(nvals)
end
OmniTools.ForCollections.drop_namedtuple_fields Function
julia
drop_namedtuple_fields(namedtuple::NamedTuple, names::Tuple{Vararg{Symbol}})

Remove specified fields from a NamedTuple.

Arguments

  • namedtuple: The input NamedTuple

  • names: A tuple of field names to remove

Returns

  • A new NamedTuple with the specified fields removed

Examples

julia
julia> using OmniTools

julia> drop_namedtuple_fields((a=1, b=2, c=3), (:b,))
(a = 1, c = 3)
Code
julia
function drop_namedtuple_fields(nt::NamedTuple, names::Tuple{Vararg{Symbol}})
    keepnames = Base.diff_names(Base._nt_names(nt), names)
    return NamedTuple{keepnames}(nt)
end
OmniTools.ForCollections.duplicates Function
julia
duplicates(x::AbstractArray{T}) where T

Finds and returns a vector of duplicate elements in the input array.

Arguments:

  • x: The input array.

Returns:

A vector of duplicate elements.

Examples

julia
julia> using OmniTools

julia> duplicates([1, 2, 2, 3, 3, 3])
2-element Vector{Int64}:
 2
 3
Code
julia
function duplicates(items::AbstractArray{T}) where {T}
    xs = sort(items)
    duplicatedvector = T[]
    for i  eachindex(xs)[2:end]
        if (
            isequal(xs[i], xs[i-1]) &&
            (length(duplicatedvector) == 0 || !isequal(duplicatedvector[end], xs[i]))
        )
            push!(duplicatedvector, xs[i])
        end
    end
    return duplicatedvector
end
OmniTools.ForCollections.foldl_tuple_unrolled Function
julia
foldl_tuple_unrolled(f, x::Tuple{Vararg{Any, N}}; init)

Generate an unrolled expression to run a function for each element of a tuple to avoid complexity of for loops for compiler.

Arguments

  • f: The function to apply

  • x: The tuple to iterate through

  • init: Initial value for the fold operation

Returns

  • The result of applying the function to each element

Examples

julia
julia> using OmniTools

julia> foldl_tuple_unrolled(+, (1, 2, 3); init=0)
6
OmniTools.ForCollections.list_to_table Function
julia
list_to_table(_list)

Converts a list or tuple into a table using TypedTables.

Arguments:

  • _list: The input list or tuple.

Returns:

A table representation of the input list.

Examples

julia
julia> using OmniTools

julia> tbl = list_to_table((:a, :b));

julia> propertynames(tbl)
(:name,)
Code
julia
function list_to_table(list)
    table = Table((; name=[list...]))
    return table
end
OmniTools.ForCollections.merge_namedtuple Function

Merges algorithm options by combining default options with user-provided options.

This function takes two option dictionaries and combines them, with user options taking precedence over default options.

Arguments

  • def_o: Default options object (NamedTuple/Struct/Dictionary) containing baseline algorithm parameters

  • u_o: User options object containing user-specified overrides

Returns

  • A merged object containing the combined algorithm options

Examples

julia
julia> using OmniTools

julia> merge_namedtuple((a=1, b=2), (b=99,))
(a = 1, b = 99)
Code
julia
function merge_namedtuple_prefer_nonempty(base_nt::NamedTuple, priority_nt::NamedTuple)
    combined_nt = (;)
    base_fields = propertynames(base_nt)
    var_fields = propertynames(priority_nt)
    all_fields = Tuple(unique([base_fields..., var_fields...]))
    for var_field  all_fields
        field_value = nothing
        if hasproperty(base_nt, var_field)
            field_value = getfield(base_nt, var_field)
        else
            field_value = getfield(priority_nt, var_field)
        end
        if hasproperty(priority_nt, var_field)
            var_prop = getfield(priority_nt, var_field)
            if !isnothing(var_prop) && length(var_prop) > 0
                field_value = getfield(priority_nt, var_field)
            end
        end
        combined_nt = set_namedtuple_field(combined_nt,
            (var_field, field_value))
    end
    return combined_nt
end
OmniTools.ForCollections.merge_namedtuple_prefer_nonempty Function
julia
merge_namedtuple_prefer_nonempty(base_nt::NamedTuple, priority_nt::NamedTuple)

Combine property values from base and priority NamedTuples.

Arguments

  • base_nt: The base NamedTuple

  • priority_nt: The priority NamedTuple whose values take precedence

Returns

  • A new NamedTuple combining values from both inputs

Examples

julia
julia> using OmniTools

julia> merge_namedtuple_prefer_nonempty((a=[1], b=[2]), (b=[99],))
(a = [1], b = [99])
Code
julia
function merge_namedtuple_prefer_nonempty(base_nt::NamedTuple, priority_nt::NamedTuple)
    combined_nt = (;)
    base_fields = propertynames(base_nt)
    var_fields = propertynames(priority_nt)
    all_fields = Tuple(unique([base_fields..., var_fields...]))
    for var_field  all_fields
        field_value = nothing
        if hasproperty(base_nt, var_field)
            field_value = getfield(base_nt, var_field)
        else
            field_value = getfield(priority_nt, var_field)
        end
        if hasproperty(priority_nt, var_field)
            var_prop = getfield(priority_nt, var_field)
            if !isnothing(var_prop) && length(var_prop) > 0
                field_value = getfield(priority_nt, var_field)
            end
        end
        combined_nt = set_namedtuple_field(combined_nt,
            (var_field, field_value))
    end
    return combined_nt
end
OmniTools.ForCollections.namedtuple_from_names_values Function
julia
namedtuple_from_names_values(input_data, input_names)

Create a NamedTuple from input data and names.

Arguments

  • input_data: Vector of data values

  • input_names: Vector of names for the fields

Returns

  • A NamedTuple with the specified names and values

Examples

julia
julia> using OmniTools

julia> namedtuple_from_names_values([1, 2], [:a, :b])
(a = 1, b = 2)
Code
julia
function namedtuple_from_names_values(values, names)
    return (; Pair.(names, values)...)
end
OmniTools.ForCollections.set_namedtuple_field Function
julia
set_namedtuple_field(tpl, vals)

Set a field in a NamedTuple.

Arguments

  • tpl: The input NamedTuple

  • vals: Tuple containing field name and value

Returns

  • A new NamedTuple with the updated field

Examples

julia
julia> using OmniTools

julia> set_namedtuple_field((a=1,), (:b, 2))
(a = 1, b = 2)
OmniTools.ForCollections.set_namedtuple_subfield Function
julia
set_namedtuple_subfield(tpl, fieldname, vals)

Set a subfield of a NamedTuple.

Arguments

  • tpl: The input NamedTuple

  • fieldname: The name of the field to set

  • vals: Tuple containing subfield name and value

Returns

  • A new NamedTuple with the updated subfield

Examples

julia
julia> using OmniTools

julia> set_namedtuple_subfield((a=(;),), :a, (:x, 1))
(a = (x = 1,),)
Code
julia
function set_namedtuple_subfield(nt::NamedTuple, fieldname::Symbol, vals::Tuple{Symbol,Any})
    if !hasproperty(nt, fieldname)
        nt = set_namedtuple_field(nt, (fieldname, (;)))
    end
    return (; nt..., fieldname => (; getfield(nt, fieldname)..., first(vals) => last(vals)))
end
OmniTools.ForCollections.table_to_namedtuple Function
julia
table_to_namedtuple(tbl; replace_missing_values=false)

Convert a table to a NamedTuple.

Arguments

  • tbl: The input table

  • replace_missing_values: Whether to replace missing values with empty strings

Returns

  • A NamedTuple representation of the table

Examples

julia
julia> using OmniTools

julia> tbl = list_to_table((:a, :b));

julia> table_to_namedtuple(tbl)
(name = [:a, :b],)
Code
julia
function table_to_namedtuple(tbl; replace_missing_values=false)
    a_nt = (;)
    for a_p in propertynames(tbl)
        t_p = getproperty(tbl, a_p)
        values_to_replace = t_p
        if replace_missing_values
            values_to_replace = [ismissing(t_p[i]) ? "" : t_p[i] for i in eachindex(t_p)]
        end
        values_to_replace = [values_to_replace...]
        a_nt = set_namedtuple_field(a_nt, (a_p, values_to_replace))
    end
    return a_nt
end
OmniTools.ForCollections.tc_print Function
julia
tc_print(d; _color=true, _type=true, _value=true, t_op=true)

Print a formatted representation of a data structure with type annotations and colors.

Arguments

  • d: The object to print

  • _color: Whether to use colors (default: true)

  • _type: Whether to show types (default: false)

  • _value: Whether to show values (default: true)

  • _tspace: Starting tab space

  • space_pad: Additional space padding

Returns

  • Nothing (prints to console)

Examples

julia
julia> using OmniTools

julia> redirect_stdout(devnull) do
           tc_print((a=1, b=(c=2,)); _color=false)
       end === nothing
true
Code
julia
function tc_print(data; _color=true, _type=false, _value=true, _tspace="", space_pad="")
    colors_types = _collectTypeColors(data; _color=_color)
    # aio = AnnotatedIOBuffer()
    lc = nothing
    ttf = _tspace * space_pad
    for k  sort(collect(keys(data)))
        if data[k] isa NamedTuple
            tp = " = (;"
            if length(data[k]) > 0
                printstyled(Crayon(; foreground=colors_types[typeof(data[k])]), "$(k)$(tp)\n")
            else
                printstyled(Crayon(; foreground=colors_types[typeof(data[k])]), "$(k)$(tp)")
            end
            tc_print(data[k]; _color=_color, _type=_type, _value=_value, _tspace=ttf, space_pad="  ")
        else
            if _type == true
                tp = "::$(typeof(data[k]))"
                if tp == "::NT"
                    tp = "::Tuple"
                end
            else
                tp = ""
            end
            if typeof(data[k]) <: Float32
                to_print = "$(ttf) $(k) = $(data[k])f0$(tp),\n"
                if !_value
                    to_print = "$(ttf) $(k)$(tp),\n"
                end
                print(Crayon(; foreground=colors_types[typeof(data[k])]),
                    to_print)
            elseif typeof(data[k]) <: AbstractVector
                to_print = "$(ttf) $(k) = $(data[k])$(tp),\n"
                if !_value
                    to_print = "$(ttf) $(k)$(tp),\n"
                end
                print(Crayon(; foreground=colors_types[typeof(data[k])]), to_print)
            elseif typeof(data[k]) <: Matrix
                print(Crayon(; foreground=colors_types[typeof(data[k])]), "$(ttf) $(k) = [\n")
                tt_row = repeat(ttf[1], length(ttf) + 1)
                for row  eachrow(data[k])
                    row_str = nothing
                    if eltype(row) == Float32
                        row_str = join(row, "f0 ") * "f0"
                    else
                        row_str = join(row, " ")
                    end
                    print(Crayon(; foreground=colors_types[typeof(data[k])]),
                        "$(tt_row) $(row_str);\n")
                end
                print(Crayon(; foreground=colors_types[typeof(data[k])]), "$(tt_row) ]$(tp),\n")
            else
                to_print = "$(ttf) $(k) = $(data[k])$(tp),"
                if !_value
                    to_print = "$(ttf) $(k)$(tp),"
                end
                print(Crayon(; foreground=colors_types[typeof(data[k])]),
                    to_print)
            end
            lc = colors_types[typeof(data[k])]
        end
        # end
        if _type == true
            _tspace = _tspace * " "
            print(Crayon(; foreground=lc), " $(ttf))::NamedTuple,\n")
        else
            if data[k] isa NamedTuple
                print(Crayon(; foreground=lc), "$(ttf)),\n")
            end
        end
    end
end

Internal

OmniTools.ForCollections._collectTypeColors Method
julia
_collectTypeColors(d; _color = true)

Collect colors for all types from nested namedtuples.

Arguments

  • d: The input data structure

  • _color: Whether to use colors (default: true)

Returns

  • A dictionary mapping types to color codes
OmniTools.ForCollections._collectTypes! Method
julia
_collectTypes!(d, all_types)

Collect all types from nested namedtuples.

Arguments

  • d: The input data structure

  • all_types: Array to store collected types

Returns

  • Array of unique types found in the data structure
OmniTools.ForCollections._setFieldValue Function
julia
_setFieldValue(o, p, v)

Set a field in an options object.

Arguments

  • o: The options object (NamedTuple or mutable struct)

  • p: The field name to update

  • v: The new value to assign

Variants:

  1. For NamedTuple options:
  • Updates the field in an immutable NamedTuple by creating a new NamedTuple with the updated value.

  • Uses the @set macro for immutability handling.

  1. For mutable struct options (e.g., BayesOpt):
  • Directly updates the field in the mutable struct using Base.setproperty!.

Returns:

  • The updated options object with the specified field modified.

Notes:

  • This function is used internally by merge_namedtuple to handle field updates in both mutable and immutable options objects.

  • Ensures compatibility with different types of optimization algorithm configurations.

Examples:

  1. Updating a NamedTuple:
julia
options = (max_iters = 100, tol = 1e-6)
updated_options = _setFieldValue(options, :tol, 1e-8)
  1. Updating a mutable struct:
julia
mutable struct BayesOptConfig
    max_iters::Int
    tol::Float64
end
config = BayesOptConfig(100, 1e-6)
updated_config = _setFieldValue(config, :tol, 1e-8)

OmniTools.jl - Foundational utilities for Julia development