How to save a file in Julia - julia

At some point, (I think Julia v0.7) you could do #save savepath thingtosave in order to save files using Julia. I tried to run this on v0.7 to see if I got a deprecation warning but even on 0.7 it says that #save is undefined.
How can I programmatically save files using Julia?

Since you mention #save, presumably, you were using JLD.jl or its successor JLD2.jl.
A simple example for using JLD2 would be
julia> using JLD2
julia> #save "test.jld2" x
julia> x = nothing # "forgetting" x
julia> #load "test.jld2"
1-element Array{Symbol,1}:
:x
julia> x
2×2 Array{Float64,2}:
0.698264 0.319665
0.252174 0.80799
In contrast to write, those packages are based on HDF5 (through HDF5.jl). They pretty much allow you to store arbitrary Julia objects. HDF5 (not necessarily JLD/JLD2) is a file format which is supported by almost all programming languages and many programs (Mathematica for example). It is suitable for long-term storage in contrast to read/write which might change in future Julia versions.
Note that this doesn't show up in 0.7 since it is a package feature and not part of Base (or a stdlib).

From the julia docs, there is the write function:
write(io::IO, x)
write(filename::AbstractString, x)
Write the canonical binary representation of a value to the given I/O stream or file. Return the number of bytes written into the stream. See also print to write a text representation (with an encoding that may depend upon io).
You can write multiple values with the same write call. i.e. the following are equivalent:
write(io, x, y...)
write(io, x) + write(io, y...)
writing a text file:
write("text.txt","this is a test")

Related

Save a Function with JLD2

I want to save a function in a JLD2 file. Is this even possible and if so how?
using JLD2
a = [1,2,3]
function foo(x, y)
x .> y
end
foo(x) = foo(x, a)
save_object("stored.jld2", foo)
So, I guess my point here is actually two things
I want to save the function.
I want it to have the method foo(x) available, i.e. if the jld2 file is opened somewhere else it has the method foo(x) with a = [1,2,3].
When someone builds a machine learning model and saves it, it must work something like this, right?
Really hope it's understandable what I mean here. If not please let me know.
OK, after some more research and some thinking, I come to the conclusion (happy to be proven wrong...) that this is not possible and/or not sensible. The comments in the question suggest using machine learning packages to save models and I think this approach explains the underlying point...
You can store data in jld2 files and but you can not store functions in jld2 files. What I tried here is not going to work because
foo(x) = foo(x, a)
accesses object a in the global environment - it does not magically store the data of a within the function. Furthermore, functions cannot be saved with JLD2 - who know's, the latter one is maybe gonna change in the future.
Great, but what's the solution now??
What you can instead do is store the data...
using JLD2
a = [1,2,3]
save("bar.jld2", Dict("a" => a))
...and make the function available through a module or a script you include().
include("module_with_foo_function.jl")
using module_with_foo_function
using JLD2
my_dict = load("bar.jld2")
a = my_dict["a"]
foo(x) = foo(x, a)
And if you think about it, this is exactly what packages like MLJ are doing as well. There you store an object that contains the information, but the functionality comes from the package, not from your stored object.
Not sure anyone ever has a similar problem, but if so I hope these thoughts help.

How do I create a list in Julia?

I thought that a lst could be written as following.
But using the typeof() function one can see that its not a list.
julia> a = [1,"test", π]
typeof(a)
Vector{Any} (alias for Array{Any, 1})
If you come from Python then what Python calls a list is the same as Vector{Any} in Julia in your example.
However, if you are interested in a linked list data structure instead then you have it in DataStructures.jl package, see here.

Import a graph (network) in Julia

Does anybody now if there is any module that could let me import a graph (network) in Julia?
Working with Python, I used the graph-tool package, which served me very well! I have my graphs in .gt file format. Can I use any module in Julia, so that I can import them there?
I have searched for LightGraphs and Junet, which is fairly new but cannot seem to see any "import" section in the documentation.
The most straightforward solution is to convert your gt files to graphml format, which is compatible with LightGraphs, and is the recommended alternative format by graph-tool.
Suppose you have a ".gt" file that was generated in the past by the following python code:
from graph_tool.all import *
g = Graph()
v1 = g.add_vertex()
v2 = g.add_vertex()
e = g.add_edge(v1,v2)
g.save('g.gt')
Start a new python session, and convert from "gt" to "graphml" format:
import graph_tool as gt
g = gt.Graph()
g.load('g.gt')
g.save('g.xml.gz')
Then, in julia, use LightGraphs with the GraphIO package to load from the GraphML file:
using LightGraphs, GraphIO
D = loadgraphs("g.xml.gz", GraphMLFormat())
#> Dict{String,LightGraphs.AbstractGraph} with 1 entry:
# "G" => {2, 1} directed simple Int64 graph
If you'd like to use PyCall to perform the conversion directly from within julia (i.e. in a script), here's how:
using PyCall
#pyimport graph_tool as gt
G = gt.Graph()
G[:load]("g.gt")
G[:save]("g.xml.gz")
(Note that this implies python and the graph-tool library are already installed on your machine and accessible from julia).
In theory, if you prefer graph-tools and you're used to its syntax and wish to keep working directly with the .gt file format, you can use it via PyCall from within julia throughout as above. Whether this is preferable to migrating over to LightGraphs which is designed for julia though is another thing. It's your call :)
PS. Greetings from Leamington, fellow Leamingtonian!
Graph importing for LightGraphs is now in GraphIO.jl. Supported import formats currently include
GML
Graph6
GEXF
GraphML
Pajek NET
DOT
with more formats coming soon.

How to run plife.jl in julia/examples?

I've installed Julia Version 0.3.10-pre+4 and am trying to run the examples contained in julia/examples, in particular plife.jl, which seems to be a parallel simulation of Conway's game of life. I have the error
julia> include("plife.jl")
plife (generic function with 1 method)
julia> plife(100,100)
ERROR: Window not defined
in plife at /x/faia/julia/examples/plife.jl:44
where the error is caused by the code
function plife(m, n)
w = Window("parallel life", n, m)
c = Canvas(w)
I have searched for a package that defines the Window function, but have failed to find it. Does anyone know how to run this example code?
That example seems to be reliant on an external package, it looks like it might be Tk.jl but there have been some renames since then.
It has actually been removed on the development branch of Julia, but still lingers in the 0.3 series. I've filed a PR to remove it.

Run Julia function every time Julia environment launches

I am moving from R, and I use the head() function a lot. I couldn't find a similar method in Julia, so I wrote one for Julia Arrays. There are couple other R functions that I'm porting to Julia as well.
I need these methods to be available for use in every Julia instance that launches, whether through IJulia or through command line. Is there a "startup script" of sorts for Julia? How can I achieve this?
PS: In case someone else is interested, this is what I wrote. A lot needs to be done for general-purpose use, but it does what I need it to for now.
function head(obj::Array; nrows=5, ncols=size(obj)[2])
if (size(obj)[1] < nrows)
println("WARNING: nrows is greater than actual number of rows in the obj Array.")
nrows = size(obj)[1]
end
obj[[1:nrows], [1:ncols]]
end
You can make a ~/.juliarc.jl file, see the Getting Started section of the manual.
As for you head function, here is how I'd do it:
function head(obj::Array; nrows=5, ncols=size(obj,2))
if size(obj,1) < nrows
warn("nrows is greater than actual number of rows in the obj Array.")
nrows = size(obj,1)
end
obj[1:nrows, 1:ncols]
end

Resources