- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望使用 JLD将 OrderedDict
写入文件的包,以便我随后可以原样读回它。
这是我的第一个尝试:
using JLD, HDF5, DataStructures
function testjld()
res = OrderedDict("A" => 1, "B" => 2)
filename = "c:/temp/test.jld"
save(File(format"JLD", filename), "res", res)
res2 = load(filename)["res"]
#Check if round-tripping works
res == res2
end
但是“往返”不起作用 - 函数返回 false
。它还会发出警告:
julia> testjld()
┌ Warning: type JLD.AssociativeWrapper{Core.String,Core.Int64,OrderedCollections.OrderedDict{Core.String,Core.Int64}} not present in workspace; reconstructing
└ @ JLD C:\Users\Philip\.julia\packages\JLD\1BoSz\src\jld_types.jl:703
false
阅读文档后,我认为 JLD
不支持 OrderedDict
“开箱即用”,但支持 Dict
并且我可以用这个事实写我自己的 custom serialisation对于 OrderedDict
。像这样:
struct OrderedDictSerializer
d::Dict
end
JLD.writeas(data::OrderedDict) = OrderedDictSerializer(Dict("contents" => convert(Dict, data),
"keyorder" => [k for (k, v) in data]))
function JLD.readas(serdata::OrderedDictSerializer)
unordered = serdata.d["contents"]
keyorder = serdata.d["keyorder"]
OrderedDict((k, unordered[k]) for k in keyorder)
end
这不是一个详尽的测试,但这似乎确实有效:
julia> testjld()
true
我认为我需要为 OrderedDict
编写自己的序列化程序是否正确,我的序列化程序是否可以改进?
编辑
我的问题“我的序列化程序可以改进吗?”的答案似乎是“它必须是,虽然我还不知道怎么做。”
考虑以下两个测试函数:
function testjld2()
res = OrderedDict("A" => [1.0,2.0],"B" => [3.0,4.0])
#check if round-tripping of readas and writeas methods works:
JLD.readas(JLD.writeas(res)) == res
end
function testjld3()
res = OrderedDict("A" => [1.0,2.0],"B" => [3.0,4.0])
filename = "c:/temp/test.jld"
save(File(format"JLD", filename), "res", res)
res2 = load(filename)["res"]
#Check if round-tripping to jld file and back works
res == res2
end
testjld2
显示我的 writeas
和 readas
方法正确地往返于 OrderedDict{String,Array{Float64,1 }} 有 2 个条目
julia> testjld2()
true
然而 testjld3
根本不起作用,但会产生错误:
julia> testjld3()
HDF5-DIAG: Error detected in HDF5 (1.10.5) thread 0:
#000: E:/mingwbuild/mingw-w64-hdf5/src/hdf5-1.10.5/src/H5Tfields.c line 60 in H5Tget_nmembers(): not a datatype
major: Invalid arguments to routine
minor: Inappropriate type
HDF5-DIAG: Error detected in HDF5 (1.10.5) thread 0:
#000: E:/mingwbuild/mingw-w64-hdf5/src/hdf5-1.10.5/src/H5Tfields.c line 60 in H5Tget_nmembers(): not a datatype
major: Invalid arguments to routine
minor: Inappropriate type
ERROR: Error getting the number of members
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] h5t_get_nmembers at C:\Users\Philip\.julia\packages\HDF5\rF1Fe\src\HDF5.jl:2279 [inlined]
[3] _gen_h5convert!(::Any) at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\jld_types.jl:638
[4] #s27#9(::Any, ::Any, ::Any, ::Any, ::Any, ::Any) at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\jld_types.jl:664
[5] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at .\boot.jl:524
[6] #write_compound#24(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(JLD.write_compound), ::JLD.JldGroup, ::String, ::JLD.AssociativeWrapper{String,Any,Dict{String,Any}}, ::JLD.JldWriteSession) at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:700
[7] write_compound at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:694 [inlined]
[8] #_write#23 at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:690 [inlined]
[9] _write at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:690 [inlined]
[10] write_ref(::JLD.JldFile, ::Dict{String,Any}, ::JLD.JldWriteSession) at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:658
[11] macro expansion at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\jld_types.jl:648 [inlined]
[12] h5convert!(::Ptr{UInt8}, ::JLD.JldFile, ::OrderedDictSerializer, ::JLD.JldWriteSession) at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\jld_types.jl:664
[13] #write_compound#24(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(JLD.write_compound), ::JLD.JldFile, ::String, ::OrderedDictSerializer, ::JLD.JldWriteSession) at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:700
[14] write_compound at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:694 [inlined]
[15] #_write#23 at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:690 [inlined]
[16] _write at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:690 [inlined]
[17] #write#17(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(write), ::JLD.JldFile, ::String, ::OrderedDict{String,Array{Float64,1}}, ::JLD.JldWriteSession) at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:514
[18] write at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:514 [inlined]
[19] #35 at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:1223 [inlined]
[20] #jldopen#14(::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol,Symbol},NamedTuple{(:compatible, :compress),Tuple{Bool,Bool}}}, ::typeof(jldopen), ::getfield(JLD, Symbol("##35#36")){String,OrderedDict{String,Array{Float64,1}},Tuple{}},
::String, ::Vararg{String,N} where N) at C:\Users\Philip\.julia\packages\JLD\1BoSz\src\JLD.jl:246
[21] testjld3() at .\none:0
[22] top-level scope at REPL[48]:1
最佳答案
改用 JLD2:
using JLD2, DataStructures, FileIO
function testjld2()
res = OrderedDict("A" => 1, "B" => 2)
myfilename = "c:/temp/test.jld2"
save(myfilename, "res", res)
res2 = load(myfilename)["res"]
#Check if round-tripping works
res == res2
end
测试:
julia> testjld2()
true
就个人而言,无论何时我都可以使用 BJSON:
using DataStructures, BSON, OrderedCollections
function testbson()
res = OrderedDict("A" => 1, "B" => 2)
myfilename = "c:/temp/test.bjson"
BSON.bson(myfilename, Dict("res" => res))
res2 = BSON.load(myfilename)["res"]
#Check if round-tripping works
res == res2
end
julia> testbson()
true
关于julia - 将 OrderedDict 保存为 Julia 数据格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58716347/
使用 julia 控制台时,您输入如下内容: [10,20]*[1:100,1:100]' 你会得到这样的输出: 2x200 Array{Int64,2}: 10 20 30 40 50
Julia Computing 提供的 Julia 和 Julia Pro 有什么区别? Julia Pro 是否有任何在 Julia 中不可用的企业库? 最佳答案 正如您在 project desc
我最近将我的一个模拟移植到 Julia 中,我仅在运行时发现了几个类型错误。我希望静态分析我的 Julia 代码。 MATLAB 也有类似的问题,只在运行时发现很多错误。 我发现的唯一工具 ( Typ
是否有一种简单的方法来监控 julia 和所有 julia 包的提交和开发?我知道 https://github.com/JuliaLang/julia/commits/master 最佳答案 如果您
我正在从 R 迁移,我使用 head() function很多。我在 Julia 中找不到类似的方法,所以我为 Julia Arrays 写了一个。我还将其他几个 R 函数移植到 Julia。 我需要
在某些语言(如 Python)中,有函数装饰器,它们看起来像宏,位于函数定义之上。装饰器为函数本身提供了一些额外的功能。 Julia 是否以任何方式支持函数装饰器的想法?是否可以使用宏来实现相同的目标
我用Julia中的pmap()函数写了一段并行代码。 然后我在集群上保护了四个核心并运行了一个脚本: julia -p 12 my_parallel_program.jl 我现在应该取消我的工作吗?现
谁能帮我理解接下来的事情: 1)为什么我们需要在制作链表的同时制作一个 future 结构的新抽象类? 2) 为什么有参数 T? 3)这个操作符是干什么的 struct BrokenList
我在 Julia 中有一个数组 Z,它表示二维高斯函数的图像。 IE。 Z[i,j] 是像素 i,j 处的高斯高度。我想确定高斯的参数(均值和协方差),大概是通过某种曲线拟合。 我研究了各种拟合 Z
假设,我们有如下数据结构 struct MyStruct{T} t :: Union{Nothing, T} end 并且我们希望允许用户在不添加任何数据的情况下初始化结构,例如 MyStru
我有一个包含相同类型字段的结构,我无法在创建时分配该字段。 Julia 似乎不喜欢以下内容。 (它吐出一个循环引用投诉。)我打算将问题归结为它的本质 mutable struct Test t
我正在尝试使用最大似然估计 Julia 中的正态线性模型。根据 Optim 文档中关于不更改的值,我使用以下代码通过拦截和匿名函数来模拟该过程: using Optim nobs = 500 nvar
有没有办法从命令行更新 Julia?我浏览了 documentation ,但我找不到任何东西。 最佳答案 我建议尝试 asdf如果您使用的是 MacOS、Linux 或 Linux 的 Window
我想对维度为 n 乘以 n 的矩阵 A 中的所有元素求和。该矩阵是对称的并且对角线上有 0。我发现最快的方法就是求和(A)。然而,这似乎很浪费,因为它没有使用我只需要计算矩阵的下三角这一事实。但是,s
假设你有一个向量元组 $a$,我想在 julia 中定义一个函数 p(x)=x^a。 例如,如果 a=(1,2,3),则结果函数将为 x^1 *y^2 * z^3。 我想为任何元组提供一个通用方法,但
例如,我希望能够按照以下方式做一些事情: abstract Tree abstract SupervisedModel type DecisionTree <: Tree, SupervisedMod
在 Julia 中构建复杂表达式时,是否可以使用列表推导式之类的东西? 例如,假设我有一些符号和类型,并想从它们构建一个类型。现在,我必须做类似的事情。 syms = [:a, :b, :c] typ
在 MATLAB 中,[N,edges,bin] = histcounts (___) 可以获得相应元素的 bin 索引。 Julia 有什么等价的功能吗?谢谢! 我已经尝试过 StatsBase.j
我有一个 Julia 脚本,它反复调用 C++ 程序来执行优化。 C++ 程序写入一个文本文件,然后我让 Julia 读取结果并决定下一步做什么。问题是偶尔(可能是 1000 多次)C++ 程序卡住(
我使用了一些需要特定版本的 Julia 包(即 ≥ v0.3 和 0.4 ≤)。我找不到编译 Julia 的方法来自特定版本的源代码(我正在使用 Linux )。有没有办法做到这一点,我不知道? Gi
我是一名优秀的程序员,十分优秀!