text:
precompile(Tuple{typeof(Base.Broadcast.broadcasted), Type, Base.UnitRange{Int64}, Function})
precompile(Tuple{Type{Base.RefValue{T} where T}, typeof(DLMReader.NUM_NA!)})
precompile(Tuple{Type{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Axes, F, Args} where Args<:Tuple where F where Axes}, Type{Base.Pair{A, B} where B where A}, Tuple{Base.UnitRange{Int64}, Base.RefValue{typeof(DLMReader.NUM_NA!)}}})
precompile(Tuple{typeof(Base.Broadcast.materialize), Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, Type{Base.Pair{A, B} where B where A}, Tuple{Base.UnitRange{Int64}, Base.RefValue{typeof(DLMReader.NUM_NA!)}}}})
precompile(Tuple{Type{Base.Dict{K, V} where V where K}, Array{Base.Pair{Int64, typeof(DLMReader.NUM_NA!)}, 1}})
precompile(Tuple{typeof(Base.setindex!), Base.Dict{Int64, typeof(DLMReader.NUM_NA!)}, Function, Int64})
precompile(Tuple{Type{NamedTuple{(:quotechar, :informat), T} where T<:Tuple}, Tuple{Char, Base.Dict{Int64, typeof(DLMReader.NUM_NA!)}}})
precompile(Tuple{Base.Cartesian.var"#@ncall", LineNumberNode, Module, Int64, Any, Vararg{Any, N} where N})
precompile(Tuple{typeof(Base.tuple_type_tail), Type})
precompile(Tuple{typeof(Base.argtail), Type, Type, Vararg{Type, N} where N})
precompile(Tuple{typeof(Base.argtail), Type})
precompile(Tuple{DLMReader.var"#filereader##kw", NamedTuple{(:quotechar, :informat), Tuple{Char, Base.Dict{Int64, typeof(DLMReader.NUM_NA!)}}}, typeof(DLMReader.filereader), String})
precompile(Tuple{typeof(DLMReader.allocatecol_for_res), Type, Int64})
precompile(Tuple{typeof(Missings.missings), Type{String}, Int64})
precompile(Tuple{typeof(Base._array_for), Type{Array{Union{Base.Missing, String}, 1}}, Base.UnitRange{Int64}, Base.HasShape{1}})
precompile(Tuple{Type{NamedTuple{(:delimiter, :linebreak, :buffsize, :fixed, :dlmstr, :lsize, :informat, :quotechar, :escapechar, :eolwarn), T} where T<:Tuple}, Tuple{Char, Char, Int64, Base.UnitRange{Int64}, Nothing, Int64, Base.Dict{Int64, typeof(DLMReader.NUM_NA!)}, UInt8, UInt8, Bool}})
precompile(Tuple{DLMReader.var"#readfile_chunk!##kw", NamedTuple{(:delimiter, :linebreak, :buffsize, :fixed, :dlmstr, :lsize, :informat, :quotechar, :escapechar, :eolwarn), Tuple{Char, Char, Int64, Base.UnitRange{Int64}, Nothing, Int64, Base.Dict{Int64, typeof(DLMReader.NUM_NA!)}, UInt8, UInt8, Bool}}, typeof(DLMReader.readfile_chunk!), Array{Array{Union{Base.Missing, String}, 1}, 1}, Int64, Int64, Array{Any, 1}, String, Array{DataType, 1}, Int64, Int64, Int64})
precompile(Tuple{typeof(Base.getindex), Array{Array{Union{Base.Missing, String}, 1}, 1}, Int64})
precompile(Tuple{typeof(Base.Broadcast.broadcasted), Function, Base.Missing, Array{Union{Base.Missing, String}, 1}})
precompile(Tuple{Type{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Axes, F, Args} where Args<:Tuple where F where Axes}, typeof(Base.isequal), Tuple{Base.RefValue{Base.Missing}, Array{Union{Base.Missing, String}, 1}}})
precompile(Tuple{typeof(Base.Broadcast.materialize), Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(Base.isequal), Tuple{Base.RefValue{Base.Missing}, Array{Union{Base.Missing, String}, 1}}}})
precompile(Tuple{typeof(Base.all), Base.BitArray{1}})
precompile(Tuple{typeof(DLMReader.r_type_guess), Array{Union{Base.Missing, String}, 1}, Function})
precompile(Tuple{typeof(DLMReader.tryparse_with_missing), Type, String, typeof(Base.identity)})
precompile(Tuple{typeof(Base.tryparse), Type{Int64}, DLMReader.LineBuffer})
precompile(Tuple{typeof(Base.tryparse), Type{Float64}, DLMReader.LineBuffer})
precompile(Tuple{Type{NamedTuple{(:delimiter, :linebreak, :header, :threads, :guessingrows, :fixed, :buffsize, :dtformat, :dlmstr, :lsize, :informat, :escapechar, :quotechar, :warn, :eolwarn), T} where T<:Tuple}, Tuple{Char, Char, Bool, Bool, Int64, Base.UnitRange{Int64}, Int64, Dates.DateFormat{Symbol("yyyy-mm-dd"), Tuple{Dates.DatePart{Char(0x79)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x6d)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x64)}}}, Nothing, Int64, Base.Dict{Int64, typeof(DLMReader.NUM_NA!)}, UInt8, UInt8, Int64, Bool}})
precompile(Tuple{DLMReader.var"#distribute_file##kw", NamedTuple{(:delimiter, :linebreak, :header, :threads, :guessingrows, :fixed, :buffsize, :dtformat, :dlmstr, :lsize, :informat, :escapechar, :quotechar, :warn, :eolwarn), Tuple{Char, Char, Bool, Bool, Int64, Base.UnitRange{Int64}, Int64, Dates.DateFormat{Symbol("yyyy-mm-dd"), Tuple{Dates.DatePart{Char(0x79)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x6d)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x64)}}}, Nothing, Int64, Base.Dict{Int64, typeof(DLMReader.NUM_NA!)}, UInt8, UInt8, Int64, Bool}}, typeof(DLMReader.distribute_file), String, Array{DataType, 1}})
precompile(Tuple{typeof(DLMReader._generate_colname_based), String, UInt8, Int64, Int64, Int64, Array{DataType, 1}, Char, Char, Int64, Base.UnitRange{Int64}, Nothing, UInt8, UInt8})
precompile(Tuple{typeof(Base.deepcopy), Array{Array{UInt8, 1}, 1}})
precompile(Tuple{typeof(Base.similar), Array{Array{UInt8, 1}, 1}})
precompile(Tuple{typeof(Base._array_for), Type{Array{Array{UInt8, 1}, 1}}, Base.UnitRange{Int64}, Base.HasShape{1}})
precompile(Tuple{typeof(Base.Threads.threading_run), Function})
    precompile(Tuple{DLMReader.var"#133#threadsfor_fun#12"{String, Array{Int64, 1}, Array{Int64, 1}, Array{Int64, 1}, UInt8, Base.UnitRange{Int64}}})
precompile(Tuple{typeof(Missings.missings), Type{Int64}, Int64})
    precompile(Tuple{DLMReader.var"#148#threadsfor_fun#13"{Char, Char, Int64, Int64, Nothing, Base.Dict{Int64, typeof(DLMReader.NUM_NA!)}, UInt8, UInt8, Int64, Bool, String, Array{DataType, 1}, Array{Int64, 1}, Array{Int64, 1}, Array{Any, 1}, Array{Int64, 1}, Array{Int64, 1}, Array{Int64, 1}, Base.UnitRange{Int64}}})
precompile(Tuple{typeof(Base.getindex), Array{Array{Array{UInt8, 1}, 1}, 1}, Int64})
precompile(Tuple{Type{NamedTuple{(:delimiter, :linebreak, :lsize, :buffsize, :fixed, :df, :dlmstr, :informat, :escapechar, :quotechar, :warn, :eolwarn), T} where T<:Tuple}, Tuple{Char, Char, Int64, Int64, Base.UnitRange{Int64}, Array{Dates.DateFormat{Symbol("yyyy-mm-dd"), Tuple{Dates.DatePart{Char(0x79)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x6d)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x64)}}}, 1}, Nothing, Base.Dict{Int64, typeof(DLMReader.NUM_NA!)}, UInt8, UInt8, Int64, Bool}})
    precompile(Tuple{Dates.var"##s831#36", Any, Any, Any, Any, Any, Any, Any, Any})
precompile(Tuple{typeof(Dates.character_codes), Type{Dates.DateFormat{Symbol("yyyy-mm-dd"), Tuple{Dates.DatePart{Char(0x79)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x6d)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x64)}}}}})
precompile(Tuple{typeof(Base.first), Core.SimpleVector})
precompile(Tuple{typeof(Base.push!), Array{Char, 1}, Char})
precompile(Tuple{typeof(Dates.genvar), DataType})
precompile(Tuple{typeof(Base._array_for), Type{Symbol}, Tuple{DataType, DataType, DataType}, Base.HasLength})
precompile(Tuple{Type{Base.LinearIndices{N, R} where R<:Tuple{Vararg{Base.AbstractUnitRange{Int64}, N}} where N}, Array{Symbol, 1}})
precompile(Tuple{typeof(Base.setindex!), Array{Symbol, 1}, Symbol, Int64})
    precompile(Tuple{Type{Base.Generator{I, F} where F where I}, Dates.var"#37#38", Tuple{DataType, DataType, DataType}})
    precompile(Tuple{Type{Tuple}, Base.Generator{Tuple{DataType, DataType, DataType}, Dates.var"#37#38"}})
precompile(Tuple{typeof(Base._array_for), Type{Int64}, Tuple{DataType, DataType, DataType}, Base.HasLength})
    precompile(Tuple{typeof(Base.collect_to_with_first!), Array{Int64, 1}, Int64, Base.Generator{Tuple{DataType, DataType, DataType}, Dates.var"#37#38"}, Int64})
precompile(Tuple{typeof(Base.Iterators.zip), Array{Symbol, 1}, Vararg{Any, N} where N})
precompile(Tuple{Type{Base.Iterators.Zip{Is} where Is<:Tuple}, Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64}}})
precompile(Tuple{typeof(Base._array_for), Type{Expr}, Base.Iterators.Zip{Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64}}}, Base.HasLength})
precompile(Tuple{Type{Base.LinearIndices{N, R} where R<:Tuple{Vararg{Base.AbstractUnitRange{Int64}, N}} where N}, Array{Expr, 1}})
precompile(Tuple{typeof(Base.Iterators._zip_iterate_all), Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64}}, Tuple{Tuple{}, Tuple{}}})
precompile(Tuple{typeof(Base.setindex!), Array{Expr, 1}, Expr, Int64})
precompile(Tuple{typeof(Base.map), typeof(tuple), Tuple{Int64, Int64}})
precompile(Tuple{typeof(Base.Iterators._zip_iterate_all), Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64}}, Tuple{Tuple{Int64}, Tuple{Int64}}})
    precompile(Tuple{Dates.var"##s832#33", Any, Any, Any, Any, Any, Any})
precompile(Tuple{typeof(Dates._directives), Type{Dates.DateFormat{Symbol("yyyy-mm-dd"), Tuple{Dates.DatePart{Char(0x79)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x6d)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x64)}}}}})
precompile(Tuple{typeof(Base._array_for), Type{Int64}, Array{Type, 1}, Base.HasShape{1}})
    precompile(Tuple{typeof(Base.collect_to_with_first!), Array{Int64, 1}, Int64, Base.Generator{Array{Type, 1}, Dates.var"#34#35"}, Int64})
precompile(Tuple{typeof(Base.length), Core.SimpleVector})
precompile(Tuple{Base.var"#@goto", LineNumberNode, Module, Symbol})
precompile(Tuple{Base.var"#@label", LineNumberNode, Module, Symbol})
precompile(Tuple{typeof(Base._array_for), Type{Symbol}, Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType, DataType}, Base.HasLength})
    precompile(Tuple{Type{Base.Generator{I, F} where F where I}, Dates.var"#37#38", Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType, DataType}})
    precompile(Tuple{Type{Tuple}, Base.Generator{Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType, DataType}, Dates.var"#37#38"}})
precompile(Tuple{typeof(Base._array_for), Type{Int64}, Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType, DataType}, Base.HasLength})
    precompile(Tuple{typeof(Base.collect_to_with_first!), Array{Int64, 1}, Int64, Base.Generator{Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType, DataType}, Dates.var"#37#38"}, Int64})
precompile(Tuple{typeof(Base.setindex_widen_up_to), Array{Int64, 1}, Dates.AMPM, Int64})
    precompile(Tuple{typeof(Base.collect_to!), Array{Any, 1}, Base.Generator{Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType, DataType}, Dates.var"#37#38"}, Int64, Int64})
precompile(Tuple{Type{Base.Iterators.Zip{Is} where Is<:Tuple}, Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64, Int64, Int64, Int64, Int64, Dates.AMPM}}})
precompile(Tuple{typeof(Base._array_for), Type{Expr}, Base.Iterators.Zip{Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64, Int64, Int64, Int64, Int64, Dates.AMPM}}}, Base.HasLength})
precompile(Tuple{typeof(Base.Iterators._zip_iterate_all), Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64, Int64, Int64, Int64, Int64, Dates.AMPM}}, Tuple{Tuple{}, Tuple{}}})
precompile(Tuple{typeof(Base.Iterators._zip_iterate_all), Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64, Int64, Int64, Int64, Int64, Dates.AMPM}}, Tuple{Tuple{Int64}, Tuple{Int64}}})
precompile(Tuple{typeof(Base._array_for), Type{Symbol}, Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType}, Base.HasLength})
    precompile(Tuple{Type{Base.Generator{I, F} where F where I}, Dates.var"#37#38", Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType}})
    precompile(Tuple{Type{Tuple}, Base.Generator{Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType}, Dates.var"#37#38"}})
precompile(Tuple{typeof(Base._array_for), Type{Int64}, Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType}, Base.HasLength})
    precompile(Tuple{typeof(Base.collect_to_with_first!), Array{Int64, 1}, Int64, Base.Generator{Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType}, Dates.var"#37#38"}, Int64})
    precompile(Tuple{typeof(Base.collect_to!), Array{Any, 1}, Base.Generator{Tuple{DataType, DataType, DataType, DataType, DataType, DataType, DataType}, Dates.var"#37#38"}, Int64, Int64})
precompile(Tuple{Type{Base.Iterators.Zip{Is} where Is<:Tuple}, Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64, Int64, Int64, Int64, Dates.AMPM}}})
precompile(Tuple{typeof(Base._array_for), Type{Expr}, Base.Iterators.Zip{Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64, Int64, Int64, Int64, Dates.AMPM}}}, Base.HasLength})
precompile(Tuple{typeof(Base.Iterators._zip_iterate_all), Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64, Int64, Int64, Int64, Dates.AMPM}}, Tuple{Tuple{}, Tuple{}}})
precompile(Tuple{typeof(Base.Iterators._zip_iterate_all), Tuple{Array{Symbol, 1}, Tuple{Int64, Int64, Int64, Int64, Int64, Int64, Dates.AMPM}}, Tuple{Tuple{Int64}, Tuple{Int64}}})
precompile(Tuple{DLMReader.var"#readfile_chunk!##kw", NamedTuple{(:delimiter, :linebreak, :lsize, :buffsize, :fixed, :df, :dlmstr, :informat, :escapechar, :quotechar, :warn, :eolwarn), Tuple{Char, Char, Int64, Int64, Base.UnitRange{Int64}, Array{Dates.DateFormat{Symbol("yyyy-mm-dd"), Tuple{Dates.DatePart{Char(0x79)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x6d)}, Dates.Delim{Char, 1}, Dates.DatePart{Char(0x64)}}}, 1}, Nothing, Base.Dict{Int64, typeof(DLMReader.NUM_NA!)}, UInt8, UInt8, Int64, Bool}}, typeof(DLMReader.readfile_chunk!), Array{Any, 1}, Int64, Int64, Array{Array{UInt8, 1}, 1}, String, Array{DataType, 1}, Int64, Int64, Int64})
precompile(Tuple{typeof(Base.setindex!), Array{AbstractArray{T, 1} where T, 1}, Array{Union{Base.Missing, Int64}, 1}, Int64})
precompile(Tuple{typeof(Base.setindex!), Array{AbstractArray{T, 1} where T, 1}, Array{Union{Base.Missing, String}, 1}, Int64})
precompile(Tuple{typeof(Base.length), Array{Union{Base.Missing, Int64}, 1}})
precompile(Tuple{typeof(Base.length), Array{Union{Base.Missing, String}, 1}})
# precompile(Tuple{typeof(InMemoryDatasets._preprocess_column), Array{Union{Base.Missing, Int64}, 1}, Int64, Bool})
# precompile(Tuple{typeof(InMemoryDatasets._preprocess_column), Array{Union{Base.Missing, String}, 1}, Int64, Bool})
# precompile(Tuple{typeof(Base.firstindex), Array{Union{Base.Missing, Int64}, 1}})
# precompile(Tuple{typeof(Base.firstindex), Array{Union{Base.Missing, String}, 1}})
# precompile(Tuple{Type{Ref{Any}}, InMemoryDatasets.Dataset})
#     precompile(Tuple{Core.Compiler.var"#256#258"{DataType, Tuple{String}}, Int64})
#     precompile(Tuple{typeof(Base.show), Base.IOContext{Base.TTY}, Base.Multimedia.MIME{Symbol("text/plain")}, InMemoryDatasets.Dataset})
#     precompile(Tuple{Type{InMemoryDatasets.DatasetColumn{T, E} where E where T<:InMemoryDatasets.AbstractDataset}, Int64, InMemoryDatasets.Dataset, Array{Union{Base.Missing, Int64}, 1}})
#     precompile(Tuple{Type{InMemoryDatasets.DatasetColumn{T, E} where E where T<:InMemoryDatasets.AbstractDataset}, Int64, InMemoryDatasets.Dataset, Array{Union{Base.Missing, String}, 1}})
#     precompile(Tuple{Base.var"#4#5"{InMemoryDatasets.var"#660#662"{Base.Dict{Any, String}}}, Tuple{Union, Int64}})
#     precompile(Tuple{InMemoryDatasets.var"#660#662"{Base.Dict{Any, String}}, Type, Int64})
#     precompile(Tuple{typeof(Base.get!), InMemoryDatasets.var"#661#663"{Union, Int64}, Base.Dict{Any, String}, Type{Union{Base.Missing, Int64}}})
#     precompile(Tuple{typeof(Base.show), Base.GenericIOBuffer{Array{UInt8, 1}}, Type})
#     precompile(Tuple{typeof(Base.show), Base.IOContext{Base.GenericIOBuffer{Array{UInt8, 1}}}, Type})
#     precompile(Tuple{typeof(Base.get!), InMemoryDatasets.var"#661#663"{Union, Int64}, Base.Dict{Any, String}, Type{Union{Base.Missing, String}}})
#     precompile(Tuple{Type{NamedTuple{(:alignment, :alignment_anchor_fallback, :alignment_anchor_regex, :body_hlines, :compact_printing, :crop, :crop_num_lines_at_beginning, :ellipsis_line_skip, :formatters, :header, :header_alignment, :hlines, :highlighters, :maximum_columns_width, :newline_at_end, :nosubheader, :row_name_alignment, :row_name_crayon, :row_name_column_title, :row_names, :row_number_alignment, :row_number_column_title, :show_row_number, :title, :vlines), T} where T<:Tuple}, Tuple{Array{Symbol, 1}, Symbol, Base.Dict{Int64, Array{Base.Regex, 1}}, Array{Int64, 1}, Bool, Symbol, Int64, Int64, Tuple{}, Tuple{Array{String, 1}, Array{String, 1}, Array{String, 1}}, Symbol, Array{Symbol, 1}, Tuple{InMemoryDatasets.PrettyTables.Highlighter}, Array{Int64, 1}, Bool, Bool, Symbol, InMemoryDatasets.PrettyTables.Crayons.Crayon, String, Nothing, Symbol, String, Bool, String, Array{Int64, 1}}})
#     precompile(Tuple{typeof(Base._compute_eltype), Any})
#     precompile(Tuple{typeof(Base.iterate), Base.IdSet{Any}})
#     precompile(Tuple{typeof(Base.convert), Type{Any}, Type})
#     precompile(Tuple{typeof(Base.iterate), Base.IdSet{Any}, Int64})
#     precompile(Tuple{typeof(Base.afoldl), Base.var"#42#43", Type, Type, Type, Type, Type, Type, Type, Type, Type, Type, Type, Type})
#     precompile(Tuple{typeof(Base.afoldl), Base.var"#42#43", Type, Type, Type, Type, Type, Type, Type, Type, Type, Type, Type})
#     precompile(Tuple{Base.var"##s79#169", Any, Any, Any, Any, Any})
#     precompile(Tuple{typeof(Base.merge_types), Tuple{Vararg{Symbol, N} where N}, Type{var"#s79"} where var"#s79"<:(NamedTuple{names, T} where T<:Tuple where names), Type{var"#s78"} where var"#s78"<:(NamedTuple{names, T} where T<:Tuple where names)})
#     precompile(Tuple{typeof(Base.afoldl), Base.var"#42#43", Type, Type, Type, Type, Type, Type, Type, Type})
#     precompile(Tuple{InMemoryDatasets.PrettyTables.var"#pretty_table##kw", NamedTuple{(:alignment, :alignment_anchor_fallback, :alignment_anchor_regex, :body_hlines, :compact_printing, :crop, :crop_num_lines_at_beginning, :ellipsis_line_skip, :formatters, :header, :header_alignment, :hlines, :highlighters, :maximum_columns_width, :newline_at_end, :nosubheader, :row_name_alignment, :row_name_crayon, :row_name_column_title, :row_names, :row_number_alignment, :row_number_column_title, :show_row_number, :title, :vlines), Tuple{Array{Symbol, 1}, Symbol, Base.Dict{Int64, Array{Base.Regex, 1}}, Array{Int64, 1}, Bool, Symbol, Int64, Int64, Tuple{}, Tuple{Array{String, 1}, Array{String, 1}, Array{String, 1}}, Symbol, Array{Symbol, 1}, Tuple{InMemoryDatasets.PrettyTables.Highlighter}, Array{Int64, 1}, Bool, Bool, Symbol, InMemoryDatasets.PrettyTables.Crayons.Crayon, String, Nothing, Symbol, String, Bool, String, Array{Int64, 1}}}, typeof(InMemoryDatasets.PrettyTables.pretty_table), Base.IOContext{Base.TTY}, InMemoryDatasets.Dataset})
#     precompile(Tuple{InMemoryDatasets.PrettyTables.var"##_pretty_table#73", Tuple{Array{String, 1}, Array{String, 1}, Array{String, 1}}, Base.Iterators.Pairs{Symbol, Any, Tuple{Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol}, NamedTuple{(:alignment, :alignment_anchor_fallback, :alignment_anchor_regex, :body_hlines, :compact_printing, :crop, :crop_num_lines_at_beginning, :ellipsis_line_skip, :formatters, :header_alignment, :hlines, :highlighters, :maximum_columns_width, :newline_at_end, :nosubheader, :row_name_alignment, :row_name_crayon, :row_name_column_title, :row_names, :row_number_alignment, :row_number_column_title, :show_row_number, :title, :vlines), Tuple{Array{Symbol, 1}, Symbol, Base.Dict{Int64, Array{Base.Regex, 1}}, Array{Int64, 1}, Bool, Symbol, Int64, Int64, Tuple{}, Symbol, Array{Symbol, 1}, Tuple{InMemoryDatasets.PrettyTables.Highlighter}, Array{Int64, 1}, Bool, Bool, Symbol, InMemoryDatasets.PrettyTables.Crayons.Crayon, String, Nothing, Symbol, String, Bool, String, Array{Int64, 1}}}}, typeof(InMemoryDatasets.PrettyTables._pretty_table), IO, InMemoryDatasets.Dataset})
#     precompile(Tuple{typeof(Base.length), InMemoryDatasets.DatasetColumn{InMemoryDatasets.Dataset, Array{Union{Base.Missing, Int64}, 1}}})
#     precompile(Tuple{typeof(Base.get), Base.IOContext{Base.TTY}, Symbol, Bool})
#     precompile(Tuple{typeof(InMemoryDatasets.PrettyTables._compute_cell_alignment_override), InMemoryDatasets.PrettyTables.ColumnTable, Array{Int64, 1}, Array{Int64, 1}, Int64, Int64, Int64, Base.RefValue{Any}})
#     precompile(Tuple{typeof(InMemoryDatasets.PrettyTables._fill_matrix_data!), Array{String, 2}, Array{Array{String, 1}, 2}, Array{Int64, 1}, Array{Int64, 1}, Array{Int64, 1}, Int64, Any, Any, Ref{Any}, InMemoryDatasets.PrettyTables.Display, Bool, Bool, Array{Int64, 1}, Bool, Bool, Bool, Bool, Bool, Base.Val{:print}, Symbol})
#     precompile(Tuple{typeof(Base.isassigned), Array{String, 1}, Int64})
#     precompile(Tuple{InMemoryDatasets.PrettyTables.var"#_parse_cell_text##kw", NamedTuple{(:autowrap, :cell_first_line_only, :column_width, :compact_printing, :has_color, :limit_printing, :linebreaks, :renderer), Tuple{Bool, Bool, Int64, Bool, Bool, Bool, Bool, Base.Val{:print}}}, typeof(InMemoryDatasets.PrettyTables._parse_cell_text), String})
#     precompile(Tuple{Base.var"##sprint#385", Nothing, Int64, typeof(Base.sprint), Function, String, Vararg{String, N} where N})
#     precompile(Tuple{typeof(Base.convert), Type{Array{String, 1}}, Array{String, 1}})
#     precompile(Tuple{typeof(Base.isassigned), InMemoryDatasets.PrettyTables.ColumnTable, Int64, Int64})
#     precompile(Tuple{typeof(InMemoryDatasets.Tables.getcolumn), InMemoryDatasets.DatasetColumns{InMemoryDatasets.Dataset}, Symbol})
#     precompile(Tuple{typeof(Base.isassigned), InMemoryDatasets.DatasetColumn{InMemoryDatasets.Dataset, Array{Union{Base.Missing, Int64}, 1}}, Int64})
#     precompile(Tuple{typeof(Base.getindex), InMemoryDatasets.PrettyTables.ColumnTable, Int64, Int64})
#     precompile(Tuple{typeof(Base.getindex), InMemoryDatasets.DatasetColumn{InMemoryDatasets.Dataset, Array{Union{Base.Missing, Int64}, 1}}, Int64})
#     precompile(Tuple{InMemoryDatasets.PrettyTables.var"#_parse_cell_text##kw", NamedTuple{(:autowrap, :cell_data_type, :cell_first_line_only, :column_width, :compact_printing, :has_color, :limit_printing, :linebreaks, :renderer), Tuple{Bool, DataType, Bool, Int64, Bool, Bool, Bool, Bool, Base.Val{:print}}}, typeof(InMemoryDatasets.PrettyTables._parse_cell_text), Int64})
#     precompile(Tuple{Type{Base.IOContext{IO_t} where IO_t<:IO}, Base.TTY, Base.Pair{Symbol, Bool}, Base.Pair{Symbol, Bool}})
#     precompile(Tuple{Type{NamedTuple{(:context,), T} where T<:Tuple}, Tuple{Base.IOContext{Base.TTY}}})
#     precompile(Tuple{Base.var"#sprint##kw", NamedTuple{(:context,), Tuple{Base.IOContext{Base.TTY}}}, typeof(Base.sprint), Function, Int64})
#     precompile(Tuple{typeof(Base.isassigned), InMemoryDatasets.DatasetColumn{InMemoryDatasets.Dataset, Array{Union{Base.Missing, String}, 1}}, Int64})
#     precompile(Tuple{typeof(Base.getindex), InMemoryDatasets.DatasetColumn{InMemoryDatasets.Dataset, Array{Union{Base.Missing, String}, 1}}, Int64})
#     precompile(Tuple{InMemoryDatasets.PrettyTables.var"#_parse_cell_text##kw", NamedTuple{(:autowrap, :cell_data_type, :cell_first_line_only, :column_width, :compact_printing, :has_color, :limit_printing, :linebreaks, :renderer), Tuple{Bool, DataType, Bool, Int64, Bool, Bool, Bool, Bool, Base.Val{:print}}}, typeof(InMemoryDatasets.PrettyTables._parse_cell_text), String})
#     precompile(Tuple{typeof(Base.ht_keyindex), Base.Dict{Int64, Array{Base.Regex, 1}}, Int64})
#     precompile(Tuple{typeof(Base.copyto!), Array{Int64, 1}, Base.KeySet{Int64, Base.Dict{Int64, Array{Base.Regex, 1}}}})
#     precompile(Tuple{typeof(Base._replace!), Base.var"#new#295"{Tuple{Base.Pair{Symbol, Int64}, Base.Pair{Symbol, Int64}, Base.Pair{Symbol, Int64}}}, Array{Any, 1}, Array{Symbol, 1}, Int64})
#     precompile(Tuple{typeof(Base._typed_vcat!), Array{Any, 1}, Tuple{Array{Any, 1}, Array{Int64, 1}}})
#     precompile(Tuple{typeof(Base.in), Int64, Base.Set{Any}})
#     precompile(Tuple{typeof(Base.push!), Base.Set{Any}, Int64})
#     precompile(Tuple{Type{Array{Int64, 1}}, Array{Any, 1}})
#     precompile(Tuple{typeof(Base._replace!), Base.var"#new#295"{Tuple{Base.Pair{Symbol, Int64}, Base.Pair{Symbol, Int64}}}, Array{Int64, 1}, Array{Int64, 1}, Int64})
#     precompile(Tuple{typeof(Base.print), Base.IOContext{Base.GenericIOBuffer{Array{UInt8, 1}}}, InMemoryDatasets.PrettyTables.Crayons.Crayon})
#     precompile(Tuple{typeof(InMemoryDatasets.PrettyTables._print_table_header!), Base.IOContext{Base.GenericIOBuffer{Array{UInt8, 1}}}, InMemoryDatasets.PrettyTables.Display, Any, Array{String, 2}, Array{Int64, 1}, Array{Int64, 1}, Int64, Int64, Array{Int64, 1}, Array{Int64, 1}, Array{Symbol, 1}, Array{Symbol, 1}, Ref{Any}, Bool, Bool, InMemoryDatasets.PrettyTables.TextFormat, InMemoryDatasets.PrettyTables.Crayons.Crayon, Array{InMemoryDatasets.PrettyTables.Crayons.Crayon, 1}, Array{InMemoryDatasets.PrettyTables.Crayons.Crayon, 1}, InMemoryDatasets.PrettyTables.Crayons.Crayon, InMemoryDatasets.PrettyTables.Crayons.Crayon})
#     precompile(Tuple{typeof(Base.print), Base.GenericIOBuffer{Array{UInt8, 1}}, InMemoryDatasets.PrettyTables.Crayons.Crayon, String, Vararg{Any, N} where N})
#     precompile(Tuple{typeof(Base.print), Base.GenericIOBuffer{Array{UInt8, 1}}, InMemoryDatasets.PrettyTables.Crayons.Crayon})
#     precompile(Tuple{typeof(Base._collect), Base.UnitRange{Int64}, Base.RegexMatchIterator, Base.HasEltype, Base.SizeUnknown})
#     precompile(Tuple{typeof(Base.getproperty), InMemoryDatasets.PrettyTables.Highlighter, Symbol})
#     precompile(Tuple{typeof(InMemoryDatasets._pretty_tables_highlighter_func), InMemoryDatasets.Dataset, Int64, Int64})
#     precompile(Tuple{typeof(Base.getindex), Array{Union{Base.Missing, Int64}, 1}, Int64})
#     precompile(Tuple{typeof(Base.getindex), Array{Union{Base.Missing, String}, 1}, Int64})
#     precompile(Tuple{typeof(InMemoryDatasets.PrettyTables._flush_buffer!), Base.IOContext{Base.TTY}, Base.GenericIOBuffer{Array{UInt8, 1}}, Bool, Bool, Int64})


text:
using InteractiveUtils
versioninfo()

using JuliaTDD
JuliaTDD.greet()

println("The test works.")
println("Let's do something useful.")
@time 1 + 1

include("sum.jl")

ext: jl

max_stars_repo_name: dev-juliacn/JuliaTDD

max_issues_repo_name: dev-juliacn/JuliaTDD

max_forks_repo_name: dev-juliacn/JuliaTDD


text:
using Test
using ShelfSpaceAllocation

@test true


text:
mutable struct ExpectedScore
    var::Symbol
    val::Matrix{Float64}
    min::Vector{Float64}
    max::Vector{Float64}
    pts::Vector{Float64}
    ExpectedScore(var, val, min, max, pts) = new(var, val, min, max, pts)
    ExpectedScore() = new(Symbol(""), zeros(Float64, 0, 0), Float64[], Float64[], Float64[])
end


text:
function resolve_import(x, state::State)
    u = typof(x) === Using
    i = 2
    n = length(x.args)

    root = par = getsymbolserver(state.server)
    bindings = []
    while i <= length(x.args)
        arg = x.args[i]
        if isidentifier(arg) || typof(arg) === CSTParser.MacroName
            if refof(x.args[i]) !== nothing
                par = refof(x.args[i])
            else
                par = _get_field(par, arg, state)
            end
        elseif typof(arg) === PUNCTUATION && kindof(arg) == CSTParser.Tokens.COMMA
            # end of chain, make available
            if i > 2
                _mark_import_arg(x.args[i - 1], par, state, u)
            end
            par = root
        elseif typof(arg) === OPERATOR && kindof(arg) == CSTParser.Tokens.COLON
            root = par
            if par != nothing && i > 2 && isidentifier(x.args[i-1]) && refof(x.args[i-1]) === nothing
                setref!(x.args[i-1], par)
            end
        elseif typof(arg) === PUNCTUATION && kindof(arg) == CSTParser.Tokens.DOT
            #dot between identifiers
            if par != nothing && i > 2 && isidentifier(x.args[i-1]) && refof(x.args[i-1]) === nothing
                setref!(x.args[i-1], par)
            end
        elseif typof(arg) === OPERATOR && kindof(arg) == CSTParser.Tokens.DOT
            #dot prexceding identifiser
            if par == root == getsymbolserver(state.server)
                par = state.scope
            elseif par isa Scope && parentof(par) !== nothing
                par = parentof(par)
            else
                return
            end
        else
            return
        end
        if i == n
            _mark_import_arg(x.args[i], par, state, u)
        end
        i += 1
    end

end

function _mark_import_arg(arg, par, state, u)
    if par != nothing && (typof(arg) === IDENTIFIER || typof(arg) === MacroName)
        if par isa Binding #mark reference to binding
            push!(par.refs, arg)
        end
        if bindingof(arg) === nothing
            arg.binding = Binding(CSTParser.str_value(arg), par, _typeof(par), [], nothing)
        end
        if u && par isa SymbolServer.ModuleStore
            if state.scope.modules isa Dict
                state.scope.modules[valof(arg)] = par
            else
                state.scope.modules = Dict(valof(arg) => par)
            end
        elseif u && par isa Binding && par.val isa SymbolServer.ModuleStore
            if state.scope.modules isa Dict
                state.scope.modules[valof(arg)] = par.val
            else
                state.scope.modules = Dict(valof(arg) => par.val)
            end
        elseif u && par isa Binding && par.val isa EXPR && typof(par.val) === CSTParser.ModuleH
            if state.scope.modules isa Dict
                state.scope.modules[valof(arg)] = scopeof(par.val)
            else
                state.scope.modules = Dict(valof(arg) => scopeof(par.val))
            end
        elseif u && par isa Binding && par.val isa Binding && par.val.val isa EXPR && typof(par.val.val) === CSTParser.ModuleH
            if state.scope.modules isa Dict
                state.scope.modules[valof(arg)] = scopeof(par.val.val)
            else
                state.scope.modules = Dict(valof(arg) => scopeof(par.val.val))
            end

        end
    end
end

function _get_field(par, arg, state)
    if par isa Dict{String, SymbolServer.ModuleStore} #package store
        if haskey(par, CSTParser.str_value(arg))
            return par[CSTParser.str_value(arg)]
        end
    elseif par isa Scope
        if haskey(par.names, valof(arg))
            return par.names[valof(arg)]
        end
    elseif par isa Binding
        if par.val isa EXPR && typof(par.val) === ModuleH
            if scopeof(par.val) isa Scope && haskey(scopeof(par.val).names, valof(arg))
                return scopeof(par.val).names[valof(arg)]
            end
        elseif par.val isa SymbolServer.ModuleStore
            if haskey(par.val.vals, CSTParser.str_value(arg))
                par = par.val.vals[CSTParser.str_value(arg)]
                if par isa SymbolServer.PackageRef # reference to dependency
                    return SymbolServer._lookup(par, getsymbolserver(state.server))
                end
                return par
            end
        end
    elseif par isa SymbolServer.ModuleStore # imported module
        if haskey(par.vals, CSTParser.str_value(arg))
            par = par.vals[CSTParser.str_value(arg)]
            if par isa SymbolServer.PackageRef # reference to dependency
                return SymbolServer._lookup(par, getsymbolserver(state.server))
            end
            return par
        end
    end
    return
end

_typeof(x) = nothing


text:
import UncertainData

# Performs the causality test for a single ensemble member
function apply_test(source, target, test::CausalityTest, intp_and_bin::UncertainData.InterpolateAndBin)
    s_vals = resample(source, intp_and_bin)
    t_vals = resample(target, intp_and_bin)

    causality(s_vals, t_vals, test)
end

"""
    causality(source::AbstractUncertainIndexValueDataset,
        target::AbstractUncertainIndexValueDataset,
        test::InterpolatedBinTest{CT, IB}) where {CT, IB}

Apply a causality test of type `CT` to `test.n` independent
realisations of `source` and `target`. Each realisation is
generated by first drawing realisations of the datasets,
then using the provided `InterpolateAndBin` resampling scheme to
interpolate and bin the draw.

See also [`UncertainData.InterpolateAndBin`](@ref).

## Examples

```
using CausalityTools, UncertainData

# Some test data
N = 100
sys = ar1_unidir(c_xy = 0.2)
X, Y = example_uncertain_indexvalue_datasets(sys, N, (1, 2),
    d_xval = Uniform(0.001, 0.2), d_yval = Uniform(0.001, 0.2));

# Define a causality test
k, l, m = 1, 1, 1 # embedding parameters
n_subdivisions = floor(Int, N^(1/(k + l + m + 1)))
state_space_binning = RectangularBinning(n_subdivisions)

te_test = VisitationFrequencyTest(k = k, l = l, m = m,
            binning = state_space_binning,
            ηs = ηs, b = 2) # use base-2 logarithms
pa_test = PredictiveAsymmetryTest(predictive_test = te_test)

# Define interpolation grid over the range of available index values
tmin = max(minimum(mean.(X.indices)), minimum(mean.(Y.indices)))
tmax = max(maximum(mean.(X.indices)), maximum(mean.(Y.indices)))
intp_grid = tmin:0.01:tmax

# Define binning grid
left_bin_edges = tmin:1.5:tmax

# Define the InterpolateAndBin instance
intp_bin = InterpolateAndBin(mean, left_bin_edges, Linear(), intp_grid, Flat(OnGrid()))

# Define interpolate-and-bin test
ib_test = InterpolateBinTest(pa_test, intp_bin, 2)
```
"""
function causality(source::AbstractUncertainIndexValueDataset,
    target::AbstractUncertainIndexValueDataset,
    test::InterpolateBinTest{CT, IB}) where {CT, IB}

    resamp = test.interpolate_bin_resampling

    [apply_test(source, target, test.test, resamp) for i = 1:test.n]
end


text:
__precompile__()

module ExtremelyRandomizedTrees

using FunctionalDataUtils, ProgressMeter

export ExtraTrees, predict, predict!

immutable ExtremelyRandomizedTree{T}
    indmatrix::Array{Int,2}
    thresholds::Array{T}
    leafs
    regression
end

z(a...) = zeros(Float32,a...)
immutable Buffers
    px::Array{Float32}
    py::Array{Float32}
    pxy::Array{Float32,2}
    hc::Array{Float32}
    hs::Array{Float32}
    Is::Array{Float32,2}
    Buffers(n) = new(z(n), z(n), z(n, 2), z(n), z(2), z(n, 2))
end
c(a) = fill!(a, 0f0)
clear!(a::Buffers) = (c(a.px); c(a.py); c(a.pxy); c(a.hc); c(a.hs); c(a.Is))

type ExtraTrees{T<:AbstractFloat}
	trees::Array{ExtremelyRandomizedTree{T}}
end

function ExtraTrees{T<:AbstractFloat}(data::Matrix{T}, labels; ntrees = 32, showprogress = true, pids = localworkers(), kargs...)
    if @unix ? (intersect(pids, localworkers()) == pids) : false
        workerpool = @p localworkers | unstack
        data = share(data)
        labels = share(labels)
        remotedata   = [data for i in 1:ntrees]
        remotelabels = [labels for i in 1:ntrees]
     else
        workerpool = @p workers | unstack
        remotedata   = @p map workerpool RemoteRef | map put! data
        remotelabels = @p map workerpool RemoteRef | map put! labels
    end

    ids = @p partsoflen (1:ntrees) len(workerpool)
    r = Any[]
    progress = showprogress && Progress(len(ids), 1, "Training ExtraTrees ... ", 50)
    for idset = ids
        refs = [@spawnat workerpool[i] ExtremelyRandomizedTree(fetch(remotedata[i]), fetch(remotelabels[i]); kargs...) for i in 1:length(idset)]
        push!(r, [fetch(ref) for ref in refs])
        showprogress && ProgressMeter.next!(progress)
    end
    @p flatten r | ExtraTrees{T}
end

function ExtraTrees{T<:Integer}(data::Matrix{T}, labels; kargs...)
	ExtraTrees(asfloat32(data), labels; kargs...)
end

function ExtremelyRandomizedTree{T1<:Number,T2<:Number}(data::AbstractArray{T1,2}, labels::AbstractArray{T2,2}; kargs...)

	assert(!isempty(data))
	assert(!isempty(labels))
	if size(data,2) != size(labels,2)
        error("ExtremelyRandomizedTree: size(data) was $(size(data)), size(labels) was $(size(labels))")
    end
	assert(!any(isnan(data)))

	ExtremelyRandomizedTree(buildSingleTree(data, labels; kargs...)...)
end

function swap(a, i::Int, j::Int)
    temp = a[i]
    a[i] = a[j]
    a[j] = temp
end

function halfsort!{T}(indices::Array{Int}, data::AbstractArray{T}, featureind::Int, threshold::T)
    assert(length(indices)>1)
    leftind = 1
    rightind = length(indices)
    while leftind <= rightind
        if data[featureind, indices[leftind]] < threshold
            leftind += 1
        else
            swap(indices, leftind, rightind)
            rightind -= 1
        end
    end
    FD.view(indices, 1:leftind-1), FD.view(indices, rightind+1:length(indices))
end

function splits!{T}(rsplits::AbstractArray{T}, mins::AbstractArray{T}, maxs::AbstractArray{T}, data::AbstractArray{T,2}, selectedfeatures::AbstractArray{Int}, indices::AbstractArray{Int})
    for m = selectedfeatures
        mins[m] = data[m, indices[1]]
        maxs[m] = data[m, indices[1]]
    end
    for n = indices, m = selectedfeatures
        mins[m] = min(mins[m], data[m, n])
        maxs[m] = max(maxs[m], data[m, n])
    end
    for m = selectedfeatures
        rsplits[m] = mins[m] + 100*eps(T) + rand(eltype(rsplits))*(maxs[m]-mins[m]-200*eps(T))
    end
end

function buildSingleTree(data, labels;
        classificationNMin::Int = 2,
        regressionNMin::Int = 5,
        regression::Bool = false,
        nmin::Int = regression ? regressionNMin : classificationNMin,
        nclasses::Int = asint(maximum(labels)),
        k::Int = round(Int,sqrt(size(data,1))),
	)

    if !regression && minimum(labels)<1
        error("labels need to be >= 1")
    end
    if !regression
        labels = asint(labels)
    end

    nLeafs = 0
    nNodes = 0
    initalSize = ceil(Int,length(labels)/100)
    leafsize = regression ? size(labels,1) : nclasses
    leafs = zeros(Float32, leafsize, initalSize)
    indmatrix = ones(Int, 4, initalSize)
    thresholds = ones(eltype(data), initalSize)
    indices = collect(1:len(data))
    mins = zeros(eltype(data), sizem(data))
    maxs = zeros(eltype(data), sizem(data))
    splits = zeros(eltype(data), sizem(data))
    buffers = Buffers(nclasses)

    function buildTree(indices)
        if len(indices) < nmin
			makeLeaf = true
        else
            makeLeaf = true
            for i in indices
                if labels[i] != labels[1]
                    makeLeaf = false
                    break
                end
            end
        end

        if !makeLeaf
			nonConstantFeatures = falses(sizem(data))
            for featureInd = 1:sizem(data)
                firstDatum = data[featureInd,indices[1]]
                for i = 2:len(indices)
                    if abs(data[featureInd,indices[i]] - firstDatum) > 200*eps(typeof(firstDatum))
                        nonConstantFeatures[featureInd] = true
                        break
                    end
                end
            end
            if !any(nonConstantFeatures)
				makeLeaf = true
			end
        end

        nNodes = nNodes + 1
        if nNodes > len(indmatrix)
            indmatrix = hcat(indmatrix, indmatrix)
            thresholds = vcat(thresholds, thresholds)
        end
        nodeind = nNodes

        if makeLeaf
            if nLeafs == size(leafs,2)
                leafs = hcat(leafs, leafs)
            end
            nLeafs = nLeafs + 1

			if regression
				leafs[:,nLeafs] = @p part labels indices | mean_
			else
				# return leaf label with class frequencies
				classhist = zeros(Float32, nclasses)
                s = zeroel(classhist)
				for i in indices
					classhist[labels[i]] += 1f0
                    s += 1f0
				end
                for i in 1:nclasses
                    leafs[i,nLeafs] = classhist[i]/s
                end
                # @show classhist s indices leafs
			end

            indmatrix[2, nodeind] = nodeind
            indmatrix[3, nodeind] = nodeind
            indmatrix[4, nodeind] = nLeafs
            thresholds[nodeind] = NaN
        else
            # 1) select random K from nonConstantFeatures

            nonConstantFeatures = find(nonConstantFeatures)
            randIndices = randperm(length(nonConstantFeatures))
            selectedFeatures = nonConstantFeatures[
                randIndices[1:min(k,length(nonConstantFeatures))]]
            assert(length(selectedFeatures)>0)
            # @show nonConstantFeatures selectedFeatures

            # 2) for each K:
            # 	randomSplit(data)
            # 		min and max von feature
            # 		return random split in [min,max]

            # mins = minimum(data[selectedFeatures,indices],2)
            # maxs = maximum(data[selectedFeatures,indices],2)
            # splits = rand(eltype(data), length(selectedFeatures),1).*(maxs-mins)+mins
            splits!(splits, mins, maxs, data, selectedFeatures, indices)

            # 3) over all K: computeScore, keep best split s*

            if length(indices) == 2
                bestSplitInd = @p randsample (1:length(selectedFeatures)) | fst
            else
                if length(selectedFeatures) == 1
                    bestSplitInd = 1
                else
                    bestSplitInd = rand(1:length(selectedFeatures))
                    scores = zeros(length(selectedFeatures))
                    for splitInd = 1:length(selectedFeatures)
                        scores[splitInd] = computeScore(regression, data[selectedFeatures[splitInd],indices], labels[:,indices],
                            splits[selectedFeatures[splitInd]], nclasses, buffers)
                    end
                    temp = find(scores .== maximum(scores))
                    bestSplitInd = randsample(temp,1)[1]
                end
            end

            # 4) split data according to split s*, d1, d2
            featureind = selectedFeatures[bestSplitInd]
            threshold = splits[featureind]

            leftindices, rightindices = halfsort!(indices, data, featureind, threshold)
            # @show scores bestSplitInd splits leftindices rightindices

            indmatrix[1, nodeind] = featureind
            indmatrix[2, nodeind] = buildTree(leftindices)
            indmatrix[3, nodeind] = buildTree(rightindices)
            indmatrix[4, nodeind] = 0
            thresholds[nodeind] = threshold
        end
		nodeind
    end
	buildTree(indices)
	indmatrix = indmatrix[:, 1:nNodes]
	thresholds = thresholds[1:nNodes]
	leafs = leafs[:,1:nLeafs]
	(indmatrix, thresholds, leafs, regression)
end

function accumvotes!{T}(votesview::Matrix, leafind::Int, votesfor::Array{Int}, leafs::Array{T,2})
    # @show leafind
    for i in 1:length(votesfor)
        votesview[i] += leafs[votesfor[i], leafind]
    end
end

predict{T}(a::ExtraTrees{T}, data; kargs...) = predict(a, convert(Array{T,2}, data); kargs...)
function predict{T<:AbstractFloat}(a::ExtraTrees{T}, data::Array{T,2}; kargs...)
    predict!([], a, data; kargs... )
end

function predictitem{T}(dataview::Matrix{T}, indmatrix::Matrix{Int}, thresholds::Vector{T})
    nodeind = 0
    nextnodeind = 1

    while nextnodeind != nodeind
        nodeind = nextnodeind
        featureind = indmatrix[1,nodeind]::Int
        threshold = thresholds[nodeind]::T
        if dataview[featureind]::T < threshold
            nextnodeind = indmatrix[2,nodeind]::Int
        else
            nextnodeind = indmatrix[3,nodeind]::Int
        end
    end
    nodeind
end

function predicttree(extratree, data, dataview, votes, votesview, votesfor)
    indmatrix = extratree.indmatrix
    thresholds = extratree.thresholds
    for dataind = 1:len(data)
        view!(data, dataind, dataview)
        view!(votes, dataind, votesview)
        nodeind = predictitem(dataview, indmatrix, thresholds)
        # @show nodeind
        accumvotes!(votesview, indmatrix[4,nodeind], votesfor, extratree.leafs)
    end
end

function predict!{T<:Number}(votes::Array, a::ExtraTrees{T}, data::Array{T,2}; votesfor = [], returnvotes = false)
    assert(!isempty(data))
    assert(!isempty(a.trees))
    assert(eltype(data) == eltype(a.trees[1].thresholds))

    onlyvotes = true
    if isempty(votesfor)
        onlyvotes = false
        votesfor = collect(1:size(a.trees[1].leafs,1))
    end
    if isa(votesfor, Number)
        votesfor = [votesfor]
    end

    if isempty(votes)
        votes = zeros(eltype(a.trees[1].leafs), length(votesfor), len(data))
    else
        assert(size(votes)==(length(votesfor),len(data)))
    end

    dataview = FD.view(data)
    votesview = FD.view(votes)
    fill!(votes, zero(eltype(votes)))

    for extratree = a.trees
        predicttree(extratree, data, dataview, votes, votesview, votesfor)
    end

    factor = 1/length(a.trees)
    for i = 1:length(votes) votes[i] *= factor end

    if a.trees[1].regression
        return votes
    else
        if onlyvotes
            return votes
        else
            result = @p map votes indmax | row
            if returnvotes
                return result, votes
            else
                return result
            end
        end
    end
end

function volume!(a)
    if size(a,2)<2
        return 0.
    end
    mean_ = mean(a,2)
    for n = 1:size(a,2), m = 1:size(a,1)
        a[m,n] -= mean_[m]
    end
    c = cov(a')
    v = eigvals(c)
    prod(sqrt(max(0.001,v)))::Float64
end

function computeScore{T}(regression::Bool, featureData, labels::Matrix{T}, split, nclasses::Int, b)
	if regression
		ind = vec(featureData .< split)
		n = length(ind)
		nleft = sum(ind)
		nright = n-nleft

		if size(labels,1) == 1
 			v = var(labels)
			vleft = nleft > 1 ? var(labels[find(ind)]) : 0.
			vright = nright > 1 ? var(labels[find(!ind)]) : 0.
		else
			v = volume!(labels)
 			vleft = nleft > 1 ? volume!(labels[:,ind]) : 0.
			vright = nright > 1 ? volume!(labels[:,!ind]) : 0.
		end
		score = v > 0. ? (v - nleft/n*vleft - nright/n*vright)/v : 0.
	else
		x = labels
		y = [x < split ? 1 : 2 for x in featureData]

        clear!(b)
		for i = 1:length(x)
			b.pxy[x[i],y[i]] += 1
			b.px[x[i]] += 1
			b.py[y[i]] += 1
		end

        for i = 1:nclasses
            b.px[i] ./= length(x)
            b.py[i] ./= length(x)
            b.pxy[i,1] ./= length(x)
            b.pxy[i,2] ./= length(x)
        end

		for i = 1:nclasses
			b.hc[i] = b.px[i] == zeroel(b.px) ? zeroel(b.px) : b.px[i]*log(b.px[i])
		end
		for i = 1:2
			b.hs[i] = b.py[i] == zeroel(b.py) ? zeroel(b.py) : b.py[i]*log(b.py[i])
		end
		Hc = - sum(b.hc)
		Hs = - sum(b.hs)

		for xi = 1:nclasses
			for yi = 1:2
				b.Is[xi,yi] = b.pxy[xi,yi]*log( b.pxy[xi,yi]/(b.px[xi]*b.py[yi]))
			end
		end
        I = zeroel(b.Is)
        for i = eachindex(b.Is)
            !isnan(b.Is[i]) ? I += b.Is[i] : nothing
        end

		score = 2*I/(Hc + Hs)
	end
    assert(!isnan(score))
    score
end

end # module


text:
module Output
import Fermi
using Formatting
#using Fermi.Options

#printstyle = Fermi.Options.printstyle
if !isdefined(Fermi.Output,:printstyle)
    if isinteractive()
        printstyle = ["stdout"]
    else
        printstyle = ["file"]
    end
end

export output
export @output

"""
    set_print(pstyle)

Used to set the print style for @output and output calls.
Options are "none","file","stdout","both"
"""
function set_print(pstyle)
    if pstyle in ["none","file","stdout","both"]
        Fermi.Output.printstyle[1] = pstyle
        #revise(Fermi.Output)
    else
        return false
    end
end

function output(str,x...)
    if Fermi.Output.printstyle[1] == "stdout"
        f = format(str,x...)
        print(f)
    elseif Fermi.Output.printstyle[1] == "file"
        f = format(str,x...)
        open("output.dat","a") do file
            write(file,f)
            flush(file)
        end
    elseif Fermi.Output.printstyle[1] == "both"
        f = format(str,x...)
        print(f)
        open("output.dat","a") do file
            write(file,f)
            flush(file)
        end
    elseif Fermi.Output.printstyle[1] == "none"
    end
end

macro output(str,x...)
    return quote
        if Fermi.Output.printstyle[1] == "stdout"
                local f = format($str, $([esc(i) for i in x]...))
                print(f)
        elseif Fermi.Output.printstyle[1] == "file"
                local f = format($str, $([esc(i) for i in x]...))
                open("output.dat","a") do file
                    write(file,f)
                    flush(file)
                end
        elseif Fermi.Output.printstyle[1] == "both"
            if length(x) >= 1
                local f = format($str, $([esc(i) for i in x]...))
                open("output.dat","a") do file
                    write(file,f)
                    flush(file)
                end
                print(f)
            else
                local f = format($str)
                open("output.dat","a") do file
                    write(file,f)
                    flush(file)
                end
            end
        elseif Fermi.Output.printstyle[1] == "none"
        end
    end
end
end #module


text:
using SequentialLOB
using StylizedFacts
using Plots

# Configuration Arguments
num_paths = 1
M = 400
T = 2299
p₀ = 238.75
L = 200
Δx = L / M

# Free-Parameters
λ = 1.0
D = 1.0
Δt = (Δx^2) / (2.0 * D)
σ = 1.5
μ = 1.0
nu = 0.5
α_slob = 100.0
α_lob = 0.0
println(Δx)
println(Δt)

lob_model = SLOB(num_paths,
T, p₀, M, L, D, σ, nu, α_lob, SourceTerm(λ, μ))

lob_densities, raw_price_paths, sample_price_paths, P⁺s, P⁻s, Ps = lob_model(true, 7136)

anim = @animate for i = 1:100:15000
    l = @layout [a ; b]
    time_step = round(i * lob_model.Δt, digits=0)
    time_step_ind = floor(Int, time_step)
    plt1 = plot(1:time_step_ind, sample_price_paths[1:time_step_ind,1],
        legend=false, ylab="Price", xlab="Time") ;
    plt2 = plot(lob_model.x, lob_densities[:,i,1], legend=false, title="t=$time_step",
    xlab="Price", ylab="LOB Density") ;
    plot!(lob_model.x, x -> 0) ;
    plot(plt1, plt2, layout=l)
end
gif(anim, "$(dirname(@__FILE__))/LOB.gif", fps=8)

max_stars_repo_name: GantZA/CALOBMTMD

max_issues_repo_name: GantZA/CALOBMTMD

max_forks_repo_name: GantZA/CALOBMTMD


text:
# Full model sampling, follows prescription from jax-ns paper
using BenchmarkTools
using CSV
using NestedSamplers
using PythonCall
using Statistics
using StatsBase

rows = []
dims = [2, 4, 8, 16, 32]
for D in dims

    model, true_lnZ = Models.CorrelatedGaussian(D)
    splr = Nested(D, 50D; proposal=Proposals.Slice(), bounds=Bounds.Ellipsoid)
    # run once to extract values from state, also precompile
    ch, state = sample(model, splr; dlogz=0.01, chain_type=Array)
    lnZ = state.logz
    lnZstd = state.logzerr

    tt = @belapsed sample($model, $splr; dlogz=0.01, chain_type=Array)

    dlnZ = abs(true_lnZ - lnZ)

    row = (; library="NestedSamplers.jl", D, t=median(tt), lnZ, lnZstd, dlnZ)
    @info "$row"
    push!(rows, row)
end

path = joinpath(@__DIR__, "sampling_results.csv")
CSV.write(path, rows)
@info "output saved to $path"


text:
abstract type AbstractTarget{T} <: AbstractParameter{T} end

target_loss() = error("undefined.")


text:
@testset "neighbour prices" begin
    println("pending")

    # ind_pref = []
    # for i in 1:20
    #     push!(ind_pref, sparse(collect(1:10), collect(1:10), rand(0:100, 10)))
    # end

    # budget = rand(150:200, 20)
    # capacity = fill(3, 20)
    # price = rand(30:70, 10)
    # max_spots = rand(5:7, 10)

    # dem = demand(price, ind_pref, budget, capacity)

    # # Test functions
    # #grad neighbour
    # grad_descent(price, clear_error)
    # # should return 5 element array (10 element vectors)

    # # indiv neighbour
    # #decrease_demand(k, price, dem, clear_error, ind_pref, budget, capacity)
    # k = 2
    # new_price = indiv_adjust(k, price, demand, clear_error, ind_pref, budget, capacity)
    # dem2 = demand(new_price, ind_pref, budget, capacity)
    # clear_error2 = dem2["total_demand"] - max_spots
    # #should find either new_price k = 0 or new clear error drop by 1

    # neighb_fun(price, demand, clear_error, ind_pref, budget, capacity)

end

max_stars_repo_name: ScPo-CompEcon/CourseMatch

max_issues_repo_name: ScPo-CompEcon/CourseMatch

max_forks_repo_name: ScPo-CompEcon/CourseMatch


text:
#######################################################################################################################################################################################################
#
# Style Guide
# 1. Types and modules start with upper case letters for each word, such as LeafBiophysics
# 2. Functions are lower case words connected using _, such as leaf_biophysics
# 3. Constants are defined using all upper cases, such as LEAF_BIOPHYSICS
# 4. Variables are defined using all lower cases, such as leaf_bio_para
# 5. Temporary variables are defined to start with _, such as _leaf
# 6. Maximum length of each line is 200 letters (including space)
# 7. There should be 2 lines of  empty lines between different components, such as two functions and methods
# 8. Bug fixes or new changes should be documented in the comments above the struct, function, or method, such as this Style Guide above Emerald.jl
# 9. Function parameter list that spans multiple lines need to be spaced with 12 spaces (3 tabs)
#
#######################################################################################################################################################################################################
module Emerald

using LazyArtifacts

using DocStringExtensions: TYPEDEF, TYPEDFIELDS
using MAT: matread
using PkgUtility: T_25
using Statistics: mean
using UnPack: @unpack
using WaterPhysics: saturation_vapor_pressure

# export public types
export BrooksCorey, Leaf, SoilAir, VanGenuchten, WaveLengthSet

# include utility files
include("utils/radiation.jl" )
include("utils/statistics.jl")

# include type files
include("types/environment.jl")
include("types/wavelength.jl" )
include("types/leaf.jl"       )

# include sub-modules
include("Aeris.jl" )
include("Folium.jl")
include("Soli.jl"  )

end # module


text:
#!/usr/bin/env julia
# This file is a part of Julia. License is MIT: https://julialang.org/license

pushfirst!(LOAD_PATH, joinpath(@__DIR__, "..", "ext"))

include("loadmeta.jl")
include("utils.jl")
include("gitmeta.jl")
include("genstdlib.jl")
include("generate.jl")

max_stars_repo_name: greimel/julia

max_issues_repo_name: greimel/julia

max_forks_repo_name: greimel/julia


text:
# This file is a part of JuliaFEM.
# License is MIT: see https://github.com/JuliaFEM/MortarContact2DAD.jl/blob/master/LICENSE

mutable struct Mortar2DAD <: BoundaryProblem
    master_elements :: Vector{Element}
end

function Mortar2DAD()
    return Mortar2DAD([])
end

function FEMBase.add_elements!(::Problem{Mortar2DAD}, ::Any)
    error("use `add_slave_elements!` and `add_master_elements!` to add ",
          "elements to the Mortar2D problem.")
end

function FEMBase.add_slave_elements!(problem::Problem{Mortar2DAD}, elements)
    for element in elements
        push!(problem.elements, element)
    end
end

function FEMBase.add_master_elements!(problem::Problem{Mortar2DAD}, elements)
    for element in elements
        push!(problem.properties.master_elements, element)
    end
end

function FEMBase.get_slave_elements(problem::Problem{Mortar2DAD})
    return problem.elements
end

function FEMBase.get_master_elements(problem::Problem{Mortar2DAD})
    return problem.properties.master_elements
end

function get_slave_dofs(problem::Problem{Mortar2DAD})
    dofs = Int64[]
    for element in get_slave_elements(problem)
        append!(dofs, get_gdofs(problem, element))
    end
    return sort(unique(dofs))
end

function get_master_dofs(problem::Problem{Mortar2DAD})
    dofs = Int64[]
    for element in get_master_elements(problem)
        append!(dofs, get_gdofs(problem, element))
    end
    return sort(unique(dofs))
end

function project_from_master_to_slave_ad(slave_element::Element{E}, x1_, n1_, x2, time;
                    tol=1.0e-9, max_iterations=5) where {E<:MortarElements2D}

    x1(xi1) = interpolate(vec(get_basis(slave_element, [xi1], time)), x1_)
    dx1(xi1) = interpolate(vec(get_dbasis(slave_element, [xi1], time)), x1_)
    n1(xi1) = interpolate(vec(get_basis(slave_element, [xi1], time)), n1_)
    dn1(xi1) = interpolate(vec(get_dbasis(slave_element, [xi1], time)), n1_)
    cross2(a, b) = cross([a; 0], [b; 0])[3]
    R(xi1) = cross2(x1(xi1)-x2, n1(xi1))
    dR(xi1) = cross2(dx1(xi1), n1(xi1)) + cross2(x1(xi1)-x2, dn1(xi1))

    xi1 = 0.0
    dxi1 = 0.0
    for i=1:max_iterations
        dxi1 = -R(xi1)/dR(xi1)
        xi1 += dxi1
        if norm(dxi1) < tol
            return xi1
        end
    end

    @error("Projection from master to slave failed with the following data:",
          x1, n1, x2, xi1, dxi1, -R(xi1), dR(xi1))
    error("find projection from master to slave: did not converge")

end

function project_from_slave_to_master_ad(
    master_element::Element{E}, x1, n1, x2_, time;
    tol=1.0e-10, max_iterations=20) where E<:MortarElements2D

    x2(xi2) = interpolate(vec(get_basis(master_element, [xi2], time)), x2_)
    dx2(xi2) = interpolate(vec(get_dbasis(master_element, [xi2], time)), x2_)
    cross2(a, b) = cross([a; 0], [b; 0])[3]
    R(xi2) = cross2(x2(xi2)-x1, n1)
    dR(xi2) = cross2(dx2(xi2), n1)

    xi2 = 0.0
    dxi2 = 0.0
    for i=1:max_iterations
        dxi2 = -R(xi2) / dR(xi2)
        xi2 += dxi2
        if norm(dxi2) < tol
            return xi2
        end
    end

    error("find projection from slave to master: did not converge, last val: $xi2 and $dxi2")

end

""" 2d mesh tie using ForwardDiff.

Construct .. + fc*la and C(d,la)=0

"""
function FEMBase.assemble_elements!(problem::Problem{Mortar2DAD}, assembly::Assembly,
                                    elements::Vector{Element{Seg2}}, time::Float64)

    props = problem.properties
    field_dim = get_unknown_field_dimension(problem)
    field_name = get_parent_field_name(problem)
    slave_elements = get_slave_elements(problem)

    function calculate_interface(x::Vector)

        ndofs = round(Int, length(x)/2)
        nnodes = round(Int, ndofs/field_dim)
        u = reshape(x[1:ndofs], field_dim, nnodes)
        la = reshape(x[ndofs+1:end], field_dim, nnodes)
        fc = zero(u)
        gap = zero(u)
        C = zero(la)

        S = Set{Int64}()
        # 1. update nodal normals for slave elements
        tangents = zero(u)
        for element in slave_elements
            conn = get_connectivity(element)
            push!(S, conn...)
            X1 = element("geometry", time)
            u1 = ((u[:,i] for i in conn)...,)
            x1 = map(+, X1, u1)
            dN = get_dbasis(element, [0.0], time)
            tangent = sum([kron(dN[:,i], x1[i]') for i=1:length(x1)])
            for nid in conn
                tangents[:,nid] += tangent[:]
            end
        end

        Q = [0.0 -1.0; 1.0 0.0]
        normals = zero(u)
        for j in S
            tangents[:,j] /= norm(tangents[:,j])
            normals[:,j] = Q*tangents[:,j]
        end

        #update!(slave_elements, "normal", time => Dict(j => normals[:,j] for j in S))
        #update!(slave_elements, "tangent", time => Dict(j => tangents[:,j] for j in S))

        # 2. loop all slave elements
        for slave_element in slave_elements

            nsl = length(slave_element)
            slave_element_nodes = get_connectivity(slave_element)
            X1 = slave_element("geometry", time)
            u1 = ((u[:,i] for i in slave_element_nodes)...,)
            x1 = map(+, X1, u1)
            la1 = ((la[:,i] for i in slave_element_nodes)...,)
            n1 = ((normals[:,i] for i in slave_element_nodes)...,)

            # 3. loop all master elements
            for master_element in get_master_elements(problem)

                nm = length(master_element)
                master_element_nodes = get_connectivity(master_element)
                X2 = master_element("geometry", time)
                u2 = ((u[:,i] for i in master_element_nodes)...,)
                x2 = map(+, X2, u2)

                # 3.1 calculate segmentation
                xi1a = project_from_master_to_slave_ad(slave_element, x1, n1, x2[1], time)
                xi1b = project_from_master_to_slave_ad(slave_element, x1, n1, x2[2], time)
#               xi1a = project_from_master_to_slave(slave_element, X2[1], time)
#               xi1b = project_from_master_to_slave(slave_element, X2[2], time)
                xi1 = clamp.([xi1a; xi1b], -1.0, 1.0)
                l = 1/2*abs(xi1[2]-xi1[1])
                isapprox(l, 0.0) && continue # no contribution in this master element

                De = zeros(nsl, nsl)
                Me = zeros(nsl, nsl)

                # 3.3. loop integration points of one integration segment and calculate
                # local mortar matrices
                for ip in get_integration_points(slave_element, 3)
                    detJ = slave_element(ip, time, Val{:detJ})
                    w = ip.weight*detJ*l
                    #dN = get_dbasis(slave_element, ip, time)
                    #j = sum([kron(dN[:,i], x1[i]') for i=1:length(x1)])
                    #w = ip.weight*norm(j)*l

                    xi = ip.coords[1]
                    xi_s = dot([1/2*(1-xi); 1/2*(1+xi)], xi1)
                    N1 = vec(get_basis(slave_element, xi_s, time))
                    Phi = N1
                    # project gauss point from slave element to master element in direction n_s
                    x_s = interpolate(N1, x1) # coordinate in gauss point
                    n_s = interpolate(N1, n1) # normal direction in gauss point
                    #xi_m = project_from_slave_to_master(master_element, X_s, n_s, time)
                    xi_m = project_from_slave_to_master_ad(master_element, x_s, n_s, x2, time)
                    N2 = vec(get_basis(master_element, xi_m, time))
                    x_m = interpolate(N2, x2)

                    la_s = interpolate(Phi, la1)
                    gn = dot(n_s, x_s-x_m)

                    u_s = interpolate(N1, u1)
                    u_m = interpolate(N2, u2)
                    X_s = interpolate(N1, X1)
                    X_m = interpolate(N2, X2)

                    fc[:,slave_element_nodes] += w*la_s*N1'
                    fc[:,master_element_nodes] -= w*la_s*N2'
                    #gap[1,slave_element_nodes] += w*gn*Phi'
                    gap[:,slave_element_nodes] += w*(u_s-u_m)*Phi'
                    # if props.adjust
                    #     G = w*(X_s-X_m)*Phi'
                    #     gap[:,slave_element_nodes] += G
                    # end
                end

            end # master elements done

        end # slave elements done, contact virtual work ready

        C = gap

        return vec([fc C])

    end

    # x doesn't mean deformed configuration here
    x = [problem.assembly.u; problem.assembly.la]
    ndofs = round(Int, length(x)/2)
    A = ForwardDiff.jacobian(calculate_interface, x)
    b = -calculate_interface(x)

    A = sparse(A)
    b = sparse(b)
    SparseArrays.droptol!(A, 1.0e-12)
    SparseArrays.droptol!(b, 1.0e-12)

    K = A[1:ndofs,1:ndofs]
    C1 = copy(transpose(A[1:ndofs,ndofs+1:end]))
    C2 = A[ndofs+1:end,1:ndofs]
    D = A[ndofs+1:end,ndofs+1:end]
    f = b[1:ndofs]
    g = b[ndofs+1:end]

    empty!(problem.assembly)
    problem.assembly.K = K
    problem.assembly.C1 = C1
    problem.assembly.C2 = C2
    problem.assembly.D = D
    problem.assembly.f = f
    problem.assembly.g = g

end


text:
using DistributedArrays
using Distances
using DistributedDistances
using Base.Test

macro test_colwise(dist, x, y, xd, yd, tol)
    quote
        local n = size($xd, 2)

        tfx = @timed colwise($dist, $x, $y)
        tdx = @timed dcolwise($dist, $xd, $yd)
        println("\t$(tdx[2]), $(tfx[2])")
        @test_approx_eq_eps tdx[1] tfx[1] $tol

        tfx = @timed colwise($dist, ($x)[:,1], $y)
        tdx = @timed dcolwise($dist, ($x)[:,1], $yd)
        println("\t$(tdx[2]), $(tfx[2])")
        @test_approx_eq_eps tdx[1] tfx[1] $tol

        tfx = @timed colwise($dist, $x, ($y)[:,1])
        tdx = @timed dcolwise($dist, $xd, ($y)[:,1])
        println("\t$(tdx[2]), $(tfx[2])")
        @test_approx_eq_eps tdx[1] tfx[1] $tol
    end
end

macro test_pairwise(dist, x, y, xd, yd, tol)
    quote
        tdx = @timed dpairwise($dist, $x, $yd)
        tfx = @timed pairwise($dist, $x, $y)
        println("\t$(tdx[2]), $(tfx[2])")
        @test_approx_eq_eps tdx[1] tfx[1] $tol
    end
end

# test colwise metrics
function colwise_test()
    println("colwise...")

    m = 5000
    n = 800

    X = rand(m, n);
    Y = rand(m, n);
    A = rand(1:3, m, n);
    B = rand(1:3, m, n);
    P = rand(m, n);
    Q = rand(m, n);
    P[P .< 0.3] = 0.;

    Xd = distribute(X; dist=(1,4));
    Yd = distribute(Y; dist=(1,4));
    Ad = distribute(A; dist=(1,4));
    Bd = distribute(B; dist=(1,4));
    Pd = distribute(P; dist=(1,4));
    Qd = distribute(Q; dist=(1,4));

    println("\tdistributed, singlenode")
    @test_colwise SqEuclidean() X Y Xd Yd eps()
    @test_colwise Euclidean() X Y Xd Yd eps()
    @test_colwise Cityblock() X Y Xd Yd eps()
    @test_colwise Chebyshev() X Y Xd Yd eps()
    @test_colwise Minkowski(2.5) X Y Xd Yd eps()
    @test_colwise Hamming() A B Ad Bd eps()

    @test_colwise CosineDist() X Y Xd Yd eps()
    @test_colwise CorrDist() X Y Xd Yd eps()

    @test_colwise ChiSqDist() X Y Xd Yd eps()
    @test_colwise KLDivergence() P Q Pd Qd eps()
    @test_colwise JSDivergence() P Q Pd Qd eps()
    @test_colwise SpanNormDist() X Y Xd Yd eps()

    @test_colwise BhattacharyyaDist() X Y Xd Yd eps()
    @test_colwise HellingerDist() X Y Xd Yd eps()
    nothing
end

# test pairwise metrics
function pairwise_test()
    println("pairwise...")

    m = 800
    n = 600
    nx = 600
    ny = 600

    X = rand(m, nx)
    Y = rand(m, ny)
    A = rand(1:3, m, nx)
    B = rand(1:3, m, ny)

    P = rand(m, nx)
    Q = rand(m, ny)

    Xd = distribute(X; dist=(1,4));
    Yd = distribute(Y; dist=(1,4));
    Ad = distribute(A; dist=(1,4));
    Bd = distribute(B; dist=(1,4));
    Pd = distribute(P; dist=(1,4));
    Qd = distribute(Q; dist=(1,4));

    println("\tdistributed, singlenode")
    @test_pairwise SqEuclidean() X Y Xd Yd eps()
    @test_pairwise Euclidean() X Y Xd Yd eps()
    @test_pairwise Cityblock() X Y Xd Yd eps()
    @test_pairwise Chebyshev() X Y Xd Yd eps()
    @test_pairwise Minkowski(2.5) X Y Xd Yd eps()
    @test_pairwise Hamming() A B Ad Bd eps()

    @test_pairwise CosineDist() X Y Xd Yd eps()
    @test_pairwise CorrDist() X Y Xd Yd eps()

    @test_pairwise ChiSqDist() X Y Xd Yd eps()
    @test_pairwise KLDivergence() P Q Pd Qd eps()
    @test_pairwise JSDivergence() P Q Pd Qd eps()

    @test_pairwise BhattacharyyaDist() X Y Xd Yd eps()
    @test_pairwise HellingerDist() X Y Xd Yd eps()

    w = rand(m)

    @test_pairwise WeightedSqEuclidean(w) X Y Xd Yd eps()
    @test_pairwise WeightedEuclidean(w) X Y Xd Yd eps()
    @test_pairwise WeightedCityblock(w) X Y Xd Yd eps()
    @test_pairwise WeightedMinkowski(w, 2.5) X Y Xd Yd eps()
    @test_pairwise WeightedHamming(w) A B Ad Bd eps()

    Q = rand(m, m)
    Q = Q * Q'  # make sure Q is positive-definite

    @test_pairwise SqMahalanobis(Q) X Y Xd Yd eps()
    @test_pairwise Mahalanobis(Q) X Y Xd Yd eps()
    nothing
end

colwise_test()
pairwise_test()

max_stars_repo_name: tanmaykm/DistributedDistances

max_issues_repo_name: tanmaykm/DistributedDistances

max_forks_repo_name: tanmaykm/DistributedDistances


text:
#=##############################################################################################
Filename: system.jl
Author: Ryan Anderson
Contact: rymanderson@gmail.com
README: this file defines environmental properties
=###############################################################################################

"""
    Environment{F}

Contains environmental properties.

# Fields:

* `ρ::TF` : freestream fluid density
* `ν::TF` : freestream fluid kinematic viscocity
* `μ::TF` : freestream fluid dynamic viscosity
* `g::TF` : acceleration due to gravity
* `T::TF` : freestream fluid temperature
* `γ::TF` : freestream fluid specific heat ratio
* `R::TF` : freestream fluid gas constant
* `a::TF` : speed of sound

"""
struct Environment{TF}
    ρ::TF
    ν::TF
    μ::TF
    g::TF
    T::TF
    γ::TF
    R::TF
    a::TF
end

"""
    Environment(;
        ρ = 1.225, # kg/m^3
        ν = 1.5e-5, # m^2/s
        μ = ν * ρ, # kg/m-s
        g = 9.81, # m/s^2
        T = 288.15, # K = 15 deg C
        γ = 1.4,
        R = 287.05, # J/kg-K
        a = sqrt(γ * R * T))

Convenience constructor for `Environment` struct.

# Keyword Arguments

* `ρ`: air density
* `ν`: air kinematic viscosity
* `μ`: air dynamic viscosity
* `g`: gravity acceleration
* `T`: temperature (K)
* `γ`:
* `R`:
* `a`: speed of sound

"""
function Environment(;
        ρ=1.225, # kg/m^3
        ν=1.5e-5, # m^2/s
        μ=ν * ρ, # kg/m-s
        g=9.81, # m/s^2
        T=288.15, # K = 15 deg C
        γ=1.4,
        R=287.05, # J/kg-K
        a=sqrt(γ * R * T))

    return Environment(ρ, ν, μ, g, T, γ, R, a)
end

max_stars_repo_name: byuflowlab/AircraftSystems

max_issues_repo_name: byuflowlab/AircraftSystems

max_forks_repo_name: byuflowlab/AircraftSystems


text:
using Dates
using DataStructures
using Printf

using .Networks

###########################################################
###         Network Checkers
###########################################################

function check(network::Network)
    checks = true

    for generator in Networks.get_generators(network)
        checks &= check(generator)
    end

    for branch in Networks.get_branches(network)
        checks &= check(branch)
    end

    checks &= check_ptdf(network.ptdf, network)

    return checks
end

####    PTDF
##################################

function check_ptdf(ptdf::Networks.PTDFDict, network::Network)::Bool
    checks = check_ptdf_case_entries(ptdf, network)
    for (_, ptdf_values) in ptdf
        checks &= check_ptdf(ptdf_values, network)
    end
    return checks
end

function check_ptdf(ptdf::Networks.PTDFValues, network::Network)::Bool
    return ( check_ptdf_branch_entries(ptdf, network)
            && check_ptdf_bus_entries(ptdf, network) )
end

function check_ptdf_branch_entries(ptdf::Networks.PTDFValues, network::Network)::Bool
    checks = true

    network_branch_ids = Set{String}(map(Networks.get_id, Networks.get_branches(network)))
    ptdf_branch_ids = Set{String}(keys(ptdf))

    missing_entries = setdiff(network_branch_ids, ptdf_branch_ids)
    if !isempty(missing_entries)
        checks = false
        msg = @sprintf("Missing branch entries `%s` in ptdf", missing_entries)
        @error(msg)
    end

    extra_entries = setdiff(ptdf_branch_ids, network_branch_ids)
    if !isempty(extra_entries)
        checks = false
        msg = @sprintf("Extra branch entries `%s` in ptdf", extra_entries)
        @error(msg)
    end

    return checks
end

function check_ptdf_bus_entries(ptdf::Networks.PTDFValues, network::Network)::Bool
    checks = true
    network_bus_ids = Set{String}(map(Networks.get_id, Networks.get_buses(network)))
    for (branch_id, branch_ptdf) in ptdf
        ptdf_bus_ids = Set{String}(keys(branch_ptdf))

        missing_entries = setdiff(network_bus_ids, ptdf_bus_ids)
        if !isempty(missing_entries)
            checks = false
            msg = @sprintf("Missing bus entries `%s` for branch `%s` in ptdf", missing_entries, branch_id)
            @error(msg)
        end

        extra_entries = setdiff(ptdf_bus_ids, network_bus_ids)
        if !isempty(extra_entries)
            checks = false
            msg = @sprintf("Extra bus entries `%s` for branch `%s` in ptdf", extra_entries, branch_id)
            @error(msg)
        end

    end
    return checks
end

"""
    Prints error messages but always returns true.
"""
function check_ptdf_case_entries(ptdf::Networks.PTDFDict, network::Network)::Bool
    checks = true

    required_cases = Set{String}(map(Networks.get_id, Networks.get_branches(network)))
    push!(required_cases, Networks.BASECASE)
    ptdf_cases = Set{String}(keys(ptdf))

    missing_entries = setdiff(required_cases, ptdf_cases)
    if !isempty(missing_entries)
        checks = false
        msg = @sprintf("Missing case entries `%s` in ptdf", missing_entries)
        @error(msg)
    end

    extra_entries = setdiff(ptdf_cases, required_cases)
    if !isempty(extra_entries)
        checks = false
        msg = @sprintf("Extra case entries `%s` in ptdf", extra_entries)
        @error(msg)
    end

    # return checks
    return true
end

####    Generator
##################################

function check(generator::Generator)
    checks = true

    msg_prefix = @sprintf("Generator %s : ", Networks.get_id(generator))

    if Networks.get_p_min(generator) < 0
        checks = false
        msg = @sprintf("Invalid input %f : p_min must be non-negative.", Networks.get_p_min(generator))
        @error(msg_prefix * msg)
    end

    if Networks.get_p_max(generator) < Networks.get_p_min(generator)
        checks = false
        msg = @sprintf("Invalid input %f : p_max must be greater than or equal to p_min (i.e. %f).", Networks.get_p_max(generator), Networks.get_p_min(generator))
        @error(msg_prefix * msg)
    end

    if Networks.get_start_cost(generator) < 0
        checks = false
        msg = @sprintf("Invalid input %f : start_cost must be non-negative.", Networks.get_start_cost(generator))
        @error(msg_prefix * msg)
    end

    if Networks.get_prop_cost(generator) < 0
        checks = false
        msg = @sprintf("Invalid input %f : prop_cost must be non-negative.", Networks.get_prop_cost(generator))
        @error(msg_prefix * msg)
    end

    if Networks.get_dp(generator) < Dates.Second(0)
        checks = false
        msg = @sprintf("Invalid input %s : dp must be non-negative.", Networks.get_dp(generator))
        @error(msg_prefix * msg)
    end

    if Networks.get_dmo(generator) < Networks.get_dp(generator)
        checks = false
        msg = @sprintf("Invalid input %s : dmo must be greater than or equal to dp (i.e. %s).",
                        Networks.get_dmo(generator), Networks.get_dp(generator))
        @error(msg_prefix * msg)
    end

    #if pmin=0, we can suppose that the generator is always
    # 1- the start cost is supposed to be paid in the far past => 0
    # 2- no need for DMO since the decision is always ON => DMO=DP (actually, infinite DMO)
    if Networks.get_p_min(generator) < 1e-09
        if Networks.get_start_cost(generator) > 0
            checks = false
            msg = @sprintf("Invalid input %s : generators with null p_min must have 0 start cost.", Networks.get_start_cost(generator))
            @error(msg_prefix * msg)
        end

        if Networks.get_dmo(generator) != Networks.get_dp(generator)
            checks = false
            msg = @sprintf("Invalid input %s : generators with null p_min must have DMO=DP (%s).",
                        Networks.get_dmo(generator), Networks.get_dp(generator))
            @error(msg_prefix * msg)
        end
    end

    if Networks.get_type(generator) == Networks.LIMITABLE
        if Networks.get_p_min(generator) > 0
            checks = false
            msg = @sprintf("Invalid input %f : Limitable units must have a minimum production capacity of 0.", Networks.get_p_min(generator))
            @error(msg_prefix * msg)
        end
    end

    return checks
end

####    Branch
##################################

function check(branch::Branch)
    if Networks.get_limit(branch) < 0
        msg = @sprintf("Branch %s: Invalid input %f : limit must be non-negative.",
                    Networks.get_id(branch), Networks.get_limit(branch))
        @error(msg)
        return false
    end
    return true
end

###########################################################
###         Uncertainties
###########################################################

function check_uncertainties(uncertainties::Uncertainties, network)
    return ( check_uncertainties_same_scenarios(uncertainties)
            && check_uncertainties_same_timesteps(uncertainties)
            && check_uncertainties_values(uncertainties, network)
            && check_uncertainties_limitables(uncertainties, network)
            && check_uncertainties_buses(uncertainties, network)
    )
end

function check_uncertainties_same_scenarios(uncertainties::Uncertainties)
    reference_scenarios = get_scenarios(uncertainties)
    for (ech,_) in uncertainties
        for (injection_name,_) in get_uncertainties(uncertainties, ech)
            for (ts, by_scenario_injections) in get_uncertainties(uncertainties, ech, injection_name)
                scenarios = collect(keys(by_scenario_injections))
                if scenarios != reference_scenarios
                    msg = @sprintf("Different scenarios list at ech=%s for name=%s and ts=%s. (Reference scenarios : %s)",
                                    ech, injection_name, ts, reference_scenarios)
                    @error(msg)
                    return false
                end
            end
        end
    end
    return true
end

function check_uncertainties_same_timesteps(uncertainties::Uncertainties)
    reference_TS = get_target_timepoints(uncertainties)
    for (ech,_) in uncertainties
        for (injection_name, by_ts_uncertainties) in get_uncertainties(uncertainties, ech)
            TS = collect(keys(by_ts_uncertainties))
            if TS != reference_TS
                msg = @sprintf("Different timesteps list at ech=%s for name=%s. (reference TS : %s)",
                                ech, injection_name, reference_TS)
                @error(msg)
                return false
            end
        end
    end
    return true
end

function check_uncertainties_values(uncertainties::Uncertainties, network::Network)
    checks = true
    for (ech,_) in uncertainties
        for (injection_name,_) in get_uncertainties(uncertainties, ech)
            gen_or_bus = Networks.get_generator_or_bus(network, injection_name)
            if ( ismissing(gen_or_bus)
                || (typeof(gen_or_bus) <: Networks.Generator) && (Networks.get_type(gen_or_bus) != Networks.LIMITABLE) )
                msg = @sprintf("Invalid injection name `%s` : injection names must refer to buses or limitables ids",
                                injection_name)
                @error(msg)
                checks = false
                continue
            end

            for (ts, _) in get_uncertainties(uncertainties, ech, injection_name)
                for (scenario, value) in get_uncertainties(uncertainties, ech, injection_name, ts)
                    if value < 0
                        msg = @sprintf("Invalid injection value %f at (ech=%s, name=%s, ts=%s, s=%s) : injections must be non-negative",
                                    value, ech, injection_name, ts, scenario)
                        @error(msg)
                        checks = false
                    elseif ( ( typeof(gen_or_bus) <: Networks.Generator )
                            && value > Networks.get_p_max(gen_or_bus) )
                        msg = @sprintf("Invalid injection value %f at (ech=%s, name=%s, ts=%s, s=%s) : injection is greater than pmax (%f)",
                        value, ech, injection_name, ts, scenario, Networks.get_p_max(gen_or_bus))
                        @error(msg)
                        checks = false
                    end
                end
            end
        end
    end
    return checks
end

"""
    make sure all limitables have listed values
"""
function check_uncertainties_limitables(uncertainties::Uncertainties, network)
    checks = true
    limitables = Networks.get_generators_of_type(network, Networks.LIMITABLE)
    limitables_ids = Set{String}( map(Networks.get_id, limitables) )
    for (ech,uncertainties_at_ech) in uncertainties
        injection_ids = Set{String}( keys(uncertainties_at_ech) )
        for missing_limitable_id in setdiff(limitables_ids, injection_ids)
            msg = @sprintf("Missing value for limitable injection `%s` at ech=%s",
                            missing_limitable_id, ech)
            @error(msg)
            checks = false
        end
    end
    return checks
end

"""
    make sure all buses have listed values
"""
function check_uncertainties_buses(uncertainties::Uncertainties, network)
    checks = true
    buses_ids = Set{String}( map(Networks.get_id, Networks.get_buses(network)) )
    for (ech,uncertainties_at_ech) in uncertainties
        injection_ids = Set{String}( keys(uncertainties_at_ech) )
        for missing_bus_id in setdiff(buses_ids, injection_ids)
            msg = @sprintf("Missing value for load `%s` at ech=%s",
                            missing_bus_id, ech)
            @error(msg)
            checks = false
        end
    end
    return checks
end

###########################################################
###         Context
###########################################################

function check(context)
    #TODO replace & with &&
    check_fo_compatibility(get_network(context), get_fo_length(get_management_mode(context)))
    return (
        check(get_network(context))
        & check_dmo_compatibility(get_network(context), get_horizon_timepoints(context)[1], get_target_timepoints(context)[1])
        & check_uncertainties(get_uncertainties(context), get_network(context))
        & check_initial_state(get_generators_initial_state(context), get_network(context))
        & check_target_timepoints(get_target_timepoints(context))
        & check_uncertainties_contain_ts(get_uncertainties(context), get_target_timepoints(context))
        & check_uncertainties_contains_ech(get_uncertainties(context), get_horizon_timepoints(context))
    )
end

####    Initial State
##################################

function check_initial_state(initial_state::SortedDict{String, GeneratorState}, network)
    checks = true
    gen_ids = Set(map( gen -> Networks.get_id(gen),
                        Networks.get_generators(network) ))
    must_list_generators = filter(gen -> Networks.get_p_min(gen) > 0,
                                collect(Networks.get_generators(network)) )
    must_list_gen_ids = Set(map( gen -> Networks.get_id(gen),
                                must_list_generators ))
    listed_gen_ids = Set(keys(initial_state))

    for missing_gen_id in setdiff(must_list_gen_ids, listed_gen_ids)
        msg = @sprintf("Initial state for generator %s is missing", missing_gen_id)
        @error(msg)
        checks = false
    end

    for extra_gen_id in setdiff(listed_gen_ids, must_list_gen_ids)
        if !(extra_gen_id in gen_ids)
            @warn(@sprintf("Unrecognized generator id %s.", extra_gen_id))
        elseif initial_state[extra_gen_id] == OFF
            msg = @sprintf("Initial state for generator %s must be ON (or can be ommited). \
                            Generators with no Pmin can always be considered on and no start cost is linked to them",
                            extra_gen_id)
            @error(msg)
            checks = false
        end
    end

    return checks
end

####    TS
##################################

function check_target_timepoints(target_timepoints)
    if !issorted(target_timepoints)
        @error("target timepoints vector is not sorted")
        return false
    end

    if length(target_timepoints) > length(unique(target_timepoints))
        @error("target timepoints vector contains duplicate values")
        return false
    end

    return true
end

####    Uncertainties-related
##################################

function check_uncertainties_contain_ts(uncertainties::Uncertainties, target_timepoints)
    checks = true
    uncertainties_target_timepoints = get_target_timepoints(uncertainties)
    if !issubset(target_timepoints, uncertainties_target_timepoints)
        for ts in setdiff(target_timepoints, uncertainties_target_timepoints)
            msg = @sprintf("Target timepoint %s is missing in uncertainties", ts)
            @error(msg)
            checks = false
        end
    end
    return checks
end

function check_uncertainties_contains_ech(uncertainties::Uncertainties, horizon_timepoints)
    checks = true
    uncertainties_horizon_timepoints = get_horizon_timepoints(uncertainties)
    if !issubset(horizon_timepoints, uncertainties_horizon_timepoints)
        for ech in setdiff(horizon_timepoints, uncertainties_horizon_timepoints)
            msg = @sprintf("Horizon timepoint %s is missing in uncertainties", ech)
            @error(msg)
            checks = false
        end
    end
    return checks
end

####    DMO-related
##################################

#avoid needing an initial schedule
function check_dmo_compatibility(network::Network, ech_1, ts_1)
    checks = true
    for generator in Networks.get_generators(network)
        dmo = Networks.get_dmo(generator)
        if ts_1 - dmo < ech_1
            msg = @sprintf("DMO of generator %s (corresponds to %s) must be at or after the farthest horizon timepoint (%s). \
                            Otherwise, commitment values of the corresponding generator should have already been decided at the first executed step!",
                            Networks.get_id(generator), ts_1-dmo, ech_1)
            @error(msg)
            checks = false
        end
    end
    return checks
end

####    FO-related
##################################

"""
FIXME
In mode 1, if DP>FO, energy market won't see the decided values at DP.
It is the TSO (in mode 1 before FO) who would make the production level decision,
 but market model will refer to the energy market schedule.

This might be solved if TSO updates market schedule, but this way :
    1 - TSO will hide the market schedule
    2 - In mode 1, TSO will affect the market
    3 - the solution would be equivalent to use tso schedule as a reference schedule when needed
"""
function check_fo_compatibility(network::Network, fo::Dates.Period)
    checks = true
    for generator in Networks.get_generators(network)
        dp = Networks.get_dp(generator)
        if dp > fo
            msg = @sprintf("DP of generator %s (i.e. %s) should be shorter than the FO length (%s)!",
                            Networks.get_id(generator), dp, fo)
            @warn(msg)
            checks = false
        end
    end
    return checks
end

max_stars_repo_name: klorel/pscopf

max_issues_repo_name: klorel/pscopf

max_forks_repo_name: klorel/pscopf


text:
using AstroTime: Epoch, Period, unit, value
# using DataInterpolations: CubicSpline
using DataInterpolations

export TimeSeries

struct TimeSeries{Scale, TType, Unit, PType, DType} <: AbstractArray{DType, 1}
    epoch::Epoch{Scale, TType}
    time::Vector{Period{Unit, PType}}
    data::Vector{DType}
    # interp::BSplineInterpolation{Array{DType,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},true,DType}
    interp
    function TimeSeries(epoch::Epoch{Scale, TType}, time, data) where {Scale, TType}
        time_array = collect(time)
        # interp = BSplineInterpolation(data, float(value.(time)), 3, :ArcLen, :Average)
        interp = CubicSpline(data, float(value.(time)))
        new{Scale, TType, unit(time[1]), eltype(time[1]), eltype(data)}(epoch, time_array, data, interp)
    end
end

function (ts::TimeSeries)(p::Period)
    p < ts.time[1] && throw(ArgumentError("`p` is too small"))
    p > ts.time[end] && throw(ArgumentError("`p` is too large"))
    return ts.interp(value(p))
end

function (ts::TimeSeries)(ep::Epoch)
    ep < ts.epoch && throw(ArgumentError("`ep` is too small"))
    ep > (ts.epoch + ts.time[end]) && throw(ArgumentError("`ep` is too large"))
    return ts.interp(value(unit(ts.time[1])(ep - ts.epoch)))
end

Base.getindex(ts::TimeSeries, idx) = ts.data[idx]
Base.size(ts::TimeSeries) = size(ts.data)
Base.eltype(ts::TimeSeries) = eltype(ts.data)


text:
function get_iodec(obj::IRNSSOrbitalElements)
    return jcall(obj, "getIODEC", jint, ())
end

function get_tgd(obj::IRNSSOrbitalElements)
    return jcall(obj, "getTGD", jdouble, ())
end


text:
testfolder = dirname(@__FILE__())

performancetests = []

println("==========================")
println("Running performance tests:")
println("==========================")

for t in performancetests
  tfile = joinpath(testfolder,"performance",string(t,".jl"))
  println("  * $(tfile) *")
  include(tfile)
  println()
end


text:
# script to run inference for the OU SDEMEM model
using Pkg
using LinearAlgebra
using DataFrames

# load functions
include(pwd()*"/src/SDEMEM OU process/ou_sdemem.jl")
#include(pwd()*"/src/SDEMEM OU process/mcmc.jl")

#M_subjects = parse(Int,ARGS[1])
#N_time = parse(Int,ARGS[2])

N_time = 200
M_subjects = 40
seed = 100

y,x,t_vec,dt,η,σ_ϵ,ϕ,prior_parameters_η,prior_parameters_σ_ϵ = set_up(M=M_subjects,N=N_time,seed=seed)

ϕ

exp.(ϕ)

using PyPlot

PyPlot.figure()
h = PyPlot.plt[:hist](ϕ[:,1],10)

PyPlot.figure()
h = PyPlot.plt[:hist](ϕ[:,2],10)

PyPlot.figure()
h = PyPlot.plt[:hist](ϕ[:,3],10)

PyPlot.figure()
h = PyPlot.plt[:hist](exp.(ϕ[:,1]),10)

PyPlot.figure()
h = PyPlot.plt[:hist](exp.(ϕ[:,2]),10)

PyPlot.figure()
h = PyPlot.plt[:hist](exp.(ϕ[:,3]),10)
h = PyPlot.plot(σ_ϵ, 0.12, "k*")

PyPlot.figure()
PyPlot.plot(Array(x'))

PyPlot.figure()
PyPlot.plot(Array(y'))

# prior predictive simulations

using Distributions

prior_dist_σ_ϵ = Gamma(prior_parameters_σ_ϵ[1],1/prior_parameters_σ_ϵ[2])

prior_dist_τ_1 = Gamma(prior_parameters_η[1,3],1/prior_parameters_η[1,4])
prior_dist_τ_2 = Gamma(prior_parameters_η[2,3],1/prior_parameters_η[2,4])
prior_dist_τ_3 = Gamma(prior_parameters_η[3,3],1/prior_parameters_η[3,4])

prior_dist_τ_2 = Gamma(2,2)
tau_2_samples = rand(prior_dist_τ_2,500)
PyPlot.figure()
h = PyPlot.plt[:hist](tau_2_samples,50)

# check loglik est val

for i in 1:10
    Random.seed!(i)

    σ_ϵ_new = rand(prior_dist_σ_ϵ)

    τ_1_new  = rand(prior_dist_τ_1)
    τ_2_new  = rand(prior_dist_τ_2)
    τ_3_new  = rand(prior_dist_τ_3)

    μ_1_new  = rand(Normal(prior_parameters_η[1,1], sqrt(1/(prior_parameters_η[1,2]*τ_1_new))))
    μ_2_new  = rand(Normal(prior_parameters_η[2,1], sqrt(1/(prior_parameters_η[2,2]*τ_2_new))))
    μ_3_new  = rand(Normal(prior_parameters_η[3,1], sqrt(1/(prior_parameters_η[3,2]*τ_3_new))))

    log_θ_1_new = μ_1_new .+ sqrt(1/τ_1_new)*randn(M_subjects)
    log_θ_2_new = μ_1_new .+ sqrt(1/τ_2_new)*randn(M_subjects)
    log_θ_3_new = μ_1_new .+ sqrt(1/τ_3_new)*randn(M_subjects)

    η_new = [μ_1_new; μ_2_new; μ_3_new; τ_1_new; τ_2_new; τ_3_new]
    ϕ_new = [log_θ_1_new log_θ_2_new log_θ_3_new]

    y,x,t_vec = generate_data(N_time, M_subjects, η_new, ϕ_new, σ_ϵ_new,dt)

    PyPlot.figure()
    PyPlot.plot(Array(y'))

end

max_stars_repo_path: src/SDEMEM OU process/generate_data.jl

max_issues_repo_path: src/SDEMEM OU process/generate_data.jl

max_forks_repo_path: src/SDEMEM OU process/generate_data.jl


text:
#!/bin/bash
# -*- mode: julia -*-
#=
exec julia --project=@. --color=yes --startup-file=no -e "include(popfirst!(ARGS))" "${BASH_SOURCE[0]}" "$@"
=#

using JuMP
import Gurobi

function alternating()
    m = direct_model(Gurobi.Optimizer())
    @variable(m, x[1:100000] >= 0, Int)
    for e in x
        unset_integer(e)
        is_integer(e)
        lower_bound(e)
    end
    return
end

function batch()
    m = direct_model(Gurobi.Optimizer())
    @variable(m, x[1:100000] >= 0, Int)
    unset_integer.(x)
    is_integer.(x)
    lower_bound.(x)
    return
end

struct SavedVarConf
  was_bin :: Bool
  was_int :: Bool
  was_fixed :: Bool
  fix_value :: Float64
  had_lb :: Bool
  lb :: Float64
  had_ub :: Bool
  ub :: Float64
end

function SavedVarConf(var :: VariableRef) :: SavedVarConf
  was_bin = is_binary(var)
  was_int = is_integer(var)
  was_fixed = is_fixed(var)
  had_lb = has_lower_bound(var)
  had_ub = has_upper_bound(var)
  return SavedVarConf(
    was_bin, was_int,
    was_fixed, (was_fixed ? fix_value(var) : 0.0),
    had_lb, (had_lb ? lower_bound(var) : 0.0),
    had_ub, (had_ub ? upper_bound(var) : 0.0)
  )
end

function relax!(var :: VariableRef) :: VariableRef
  if is_integer(var)
    unset_integer(var)
  elseif is_binary(var)
    unset_binary(var)
    if !has_lower_bound(var) || lower_bound(var) < 0.0
      set_lower_bound(var, 0.0)
    end
    if !has_upper_bound(var) || upper_bound(var) < 0.0
      set_upper_bound(var, 1.0)
    end
  end

  return var
end

function save_and_relax!(var :: VariableRef)
  svc = SavedVarConf(var)
  if svc.was_int
    unset_integer(var)
  elseif svc.was_bin
    unset_binary(var)
    if !svc.had_lb || svc.lb < 0.0
      set_lower_bound(var, 0.0)
    end
    if !svc.had_ub || svc.ub > 1.0
      set_upper_bound(var, 1.0)
    end
  end

  return svc
end

function alternating2()
    m = direct_model(Gurobi.Optimizer())
    @variable(m, x[1:100000] >= 0, Int)
    save_and_relax!.(x)
    return
end

function batch2()
    m = direct_model(Gurobi.Optimizer())
    @variable(m, x[1:100000] >= 0, Int)
    SavedVarConf.(x)
    relax!.(x)
    return
end

function test()
    println("-------------------- batch --------------------------")
    @time batch()
    println("-------------------- alternating --------------------")
    @time alternating()
    #=
    println("-------------------- batch2 -------------------------")
    @time batch2()
    println("-------------------- alternating2 -------------------")
    @time alternating2()
    =#
end

test()
#println("-------------------- second run ---------------------")
#test()