Skip to content

Commit

Permalink
move some scripting funcs from tgan
Browse files Browse the repository at this point in the history
  • Loading branch information
xukai92 committed Sep 18, 2019
1 parent 134173b commit 0273683
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/MLToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export break_stick_ibp, break_logstick_ibp
include("Data/Data.jl")
Reexport.@reexport using .Data
include("scripting.jl")
export parse_args, flatten_dict, isjupyter, @jupyter, @script, checknumerics, @checknumerics, sweepcmd, sweeprun, CombinedLogger
export parse_args, flatten_dict, dict2namedtuple, merge_namedtuples, map_namedtuple, args_dict2str, isjupyter, @jupyter, @script, checknumerics, @checknumerics, sweepcmd, sweeprun, CombinedLogger
include("plotting.jl")
export make_two_y_axes_plot, plot_grayimg!, plot_actmat!, autoset_lim!, plot_contour!

Expand Down
14 changes: 14 additions & 0 deletions src/scripting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ function flatten_dict(dict::Dict{T,<:Any};
return join(["$k$equal_sym$v" for (k,v) in filter(t -> (t[1] in include) && !(t[1] in exclude), dict)], delimiter)
end

function dict2namedtuple(d)
return NamedTuple{tuple(keys(d)...),typeof(tuple(values(d)...))}(tuple(values(d)...))
end

function merge_namedtuples(op, t1, t2)
return NamedTuple{tuple(keys(t1)...),typeof(tuple(values(t1)...))}(tuple(map(op, zip(values(t1), values(t2)))...))
end

function map_namedtuple(op, t)
return NamedTuple{tuple(keys(t)...),typeof(tuple(values(t)...))}(tuple(map(op, values(t))...))
end

args_dict2str(args_dict) = join([v == "" ? "--$k" : "--$k $v" for (k, v) in args_dict], " ")

isjupyter() = isdefined(Main, :IJulia) && Main.IJulia.inited

"""
Expand Down
29 changes: 29 additions & 0 deletions test/scripting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@ using ArgParse: ArgParseSettings, @add_arg_table
@test flat_args == "a=1"
end

@testset "dict2namedtuple" begin
args_dict = Dict(:a => 1, :b => "two", :c => true, :d => NaN)
args = dict2namedtuple(args_dict)
@test keys(args) |> Set == (:a, :b, :c, :d) |> Set
@test values(args) |> Set == (1, "two", true, NaN) |> Set
@test args.a == 1
@test args.b == "two"
@test args.c == true
@test isnan(args.d)
end

@testset "merge_namedtuples" begin
t1 = (x=1, y=2)
t2 = (x=3, y=4)
t = merge_namedtuples(sum, t1, t2)
@test t == (x=4, y=6)
end

@testset "map_namedtuple" begin
t = (x=1, y=2)
t = map_namedtuple(v -> 2v, t)
@test t == (x=2, y=4)
end

@testset "args_dict2str" begin
args_dict = Dict(:a => 1, :b => "two", :d => NaN, :c => true)
@test args_dict2str(args_dict) == "--a 1 --b two --d NaN --c true"
end

@warn "`jupyter()` is not tested."
@warn "`@jupyter` is not tested."
@warn "`checknumerics()` is not tested."
Expand Down

0 comments on commit 0273683

Please sign in to comment.