Skip to content

Instantly share code, notes, and snippets.

@Godisemo
Godisemo / matlab_dates.jl
Created February 22, 2019 12:50
Matlab date conversion in Julia
const MATLAB_EPOCH = Dates.DateTime(-1,12,31)
date2num(d::Dates.DateTime) = Dates.value(d-MATLAB_EPOCH)/(1000*60*60*24)
num2date(n::Number) = MATLAB_EPOCH + Dates.Millisecond(round(Int64, n*1000*60*60*24))
@Godisemo
Godisemo / idx_io.jl
Last active April 29, 2020 13:18
Julia parser and writer for the IDX file format used by e.g. the MNIST dataset
const idx_types = [(UInt8, 0x08), (Int8, 0x09), (Int16, 0x0B), (Int32, 0x0C), (Float32, 0x0D), (Float64, 0x0E)]
const typefromcode = Dict(idx_types)
const codefromtype = Dict(map(reverse, idx_types))
function write_idx(stream::Union{IOStream,String}, data::Array{T,N}) where {T,N}
dims = convert(Array{Int32}, collect(size(data)))
write(stream, 0x00, 0x00, typefromcode[T], UInt8(N), hton.(dims), hton.(data))
end
read_idx(filename::String) = open(read_idx, filename)