Saving and loading files
Introduction
For some of our datatypes we provide a way to save them in and load them from JSON format. The most common OSCAR types are supported, but it will take some time until all corners of OSCAR are covered by this effort. Our overall goal is threefold:
- Avoid recomputation by providing an easy way to store data.
- Increase portability by giving a convenient possibility to transport data.
- Increase overall software quality by testing against existing data and tracking errors through data computed by different versions of OSCAR (or other computer algebra systems).
For more details read the developer documentation. Work on serialization is supported by the MaRDI project. You can find out more about its Task Area 1 (Computer Algebra) here.
save
— Functionsave(io::IO, obj::Any; metadata::MetaData=nothing, with_attrs::Bool=true)
save(filename::String, obj::Any, metadata::MetaData=nothing, with_attrs::Bool=true)
Save an object obj
to the given io stream respectively to the file filename
. When used with with_attrs=true
then the object will save it's attributes along with all the attributes of the types used in the object's struct. The attributes that will be saved are defined during type registration see @register_serialization_type
See load
.
Examples
julia> meta = metadata(author_orcid="0000-0000-0000-0042", name="42", description="The meaning of life, the universe and everything")
Oscar.MetaData("0000-0000-0000-0042", "42", "The meaning of life, the universe and everything")
julia> save("/tmp/fourtitwo.mrdi", 42; metadata=meta);
julia> read_metadata("/tmp/fourtitwo.mrdi")
{
"author_orcid": "0000-0000-0000-0042",
"name": "42",
"description": "The meaning of life, the universe and everything"
}
julia> load("/tmp/fourtitwo.mrdi")
42
load
— Functionload(io::IO; params::Any = nothing, type::Any = nothing, with_attrs::Bool=true)
load(filename::String; params::Any = nothing, type::Any = nothing, with_attrs::Bool=true)
Load the object stored in the given io stream respectively in the file filename
.
If params
is specified, then the root object of the loaded data either will attempt a load using these parameters. In the case of Rings this results in setting its parent, or in the case of a container of ring types such as Vector
or Tuple
, then the parent of the entries will be set using their params
.
If a type T
is given then attempt to load the root object of the data being loaded with this type; if this fails, an error is thrown.
If with_attrs=true
the object will be loaded with attributes available from the file (or serialized data).
See save
.
Examples
julia> save("/tmp/fourtitwo.mrdi", 42);
julia> load("/tmp/fourtitwo.mrdi")
42
julia> load("/tmp/fourtitwo.mrdi"; type=Int64)
42
julia> R, x = QQ[:x]
(Univariate polynomial ring in x over QQ, x)
julia> p = x^2 - x + 1
x^2 - x + 1
julia> save("/tmp/p.mrdi", p)
julia> p_loaded = load("/tmp/p.mrdi", params=R)
x^2 - x + 1
julia> parent(p_loaded) === R
true
julia> save("/tmp/p_v.mrdi", [p, p])
julia> loaded_p_v = load("/tmp/p_v.mrdi", params=R)
2-element Vector{QQPolyRingElem}:
x^2 - x + 1
x^2 - x + 1
julia> parent(loaded_p_v[1]) === parent(loaded_p_v[2]) === R
true
Objects that can be serialized
In this section we will list a selection of objects that may be (de-)serialized.
Many low level objects may be stored and these in turn allow serializing higher level objects. Such low level objects are various types of matrices, vectors and sets.
Combinatorics
Graph
SimplicialComplex
Commutative Algebra
Ideal
PolyRing
PolyRingElem
MPolyRing
MPolyRingElem
Groups
FPGroup
FinGenAbGroup
PcGroup
PermGroup
SubFPGroup
SubPcGroup
Polyhedral Geometry
Cone
LinearProgram
PolyhedralFan
PolyhedralComplex
Polyhedron
SubdivisionOfPoints
Toric Geometry
NormalToricVariety
ToricDivisor
Tropical Geometry
TropicalCurve
TropicalHypersurface
Listing all serializable types of the current session
If you are curious about whether your type can already be serialized given your version of Oscar you can run the following command in your active session.
julia> Oscar.reverse_type_map
Dict{String, Type} with 158 entries: "ArbFieldElem" => ArbFieldElem "MPolyRing" => MPolyRing "Floats" => Floats{Float64} "RationalFunctionFieldElem" => RationalFunctionFieldElem "Tuple" => Tuple "AbstractLieAlgebraElem" => AbstractLieAlgebraElem "MatSpace" => MatSpace "Float64" => Float64 "QQFieldElem" => QQFieldElem "ZZModRingElem" => ZZModRingElem "RationalFunctionField" => RationalFunctionField "Vector" => Vector "RelPowerSeriesRingElem" => RelPowerSeriesRingElem "LaurentSeriesFieldElem" => LaurentSeriesFieldElem "FreeAssociativeAlgebraElem" => FreeAssociativeAlgebraElem "Hecke.RelNonSimpleNumFieldEmbedding" => RelNonSimpleNumFieldEmbedding "Hecke.RelSimpleNumFieldElem" => RelSimpleNumFieldElem "PermGroup" => PermGroup "ArbField" => ArbField ⋮ => ⋮