Writer
Basic Operation of Writers
Having launch
ed, a Writer
is triggered through its trigger
pin. When triggered, a Writer
reads its input and then writes it to its internal buffer databuf
. When databuf
is full, the data in databuf
is processed. Thus, the length of the data that is to be processed by the Writer
is determined by the length of their internal buffer databuf
.
Let us construct a Writer
.
julia> using Causal # hide
julia> w = Writer(input=Inport(), buflen=5)
Writer(path:/tmp/cba80d8a-452c-4b5b-9411-7fb6e2f22f4b.jld2, nin:1)
The file of w
is closed and the trigger
pin of w
is not writable. That is, it is not possible to trigger w
from its trigger
pin.
julia> w.file
JLDFile /tmp/cba80d8a-452c-4b5b-9411-7fb6e2f22f4b.jld2 (read/write)
(closed)
julia> w.trigger
Inpin(eltype:Float64, isbound:false)
To trigger w
, we need to open and launch it,
julia> oport, trg, hnd = Outport(), Outpin(), Inpin{Bool}()
(Outport(numpins:1, eltype:Outpin{Float64}), Outpin(eltype:Float64, isbound:false), Inpin(eltype:Bool, isbound:false))
julia> connect!(oport, w.input)
1-element Array{Link{Float64},1}:
Link(state:open, eltype:Float64, isreadable:false, iswritable:false)
julia> connect!(trg, w.trigger)
Link(state:open, eltype:Float64, isreadable:false, iswritable:false)
julia> connect!(w.handshake, hnd)
Link(state:open, eltype:Bool, isreadable:false, iswritable:false)
julia> open(w)
Writer(path:/tmp/cba80d8a-452c-4b5b-9411-7fb6e2f22f4b.jld2, nin:1)
julia> t = launch(w)
Task (runnable) @0x00007f1fcfb25390
Now, the internal file of w
is opened in read/write mode and its trigger
pin is writable.
julia> w.file
JLDFile /tmp/cba80d8a-452c-4b5b-9411-7fb6e2f22f4b.jld2 (read/write)
(no datasets)
julia> w.trigger.link
Link(state:open, eltype:Float64, isreadable:false, iswritable:true)
Let us now trigger w
.
julia> put!(trg, 1.)
The input
of w
is now readable and handshake
pin is not readable since w
have not signaled that its triggering is succeeded yet. To do that, we need to put a value to the input
of w
julia> put!(oport, [10.])
1-element Array{Float64,1}:
10.0
Now, w
signalled that its step is succeeded. It read the data from its input
and written it into is databuf
.
julia> hnd.link
Link(state:open, eltype:Bool, isreadable:true, iswritable:false)
julia> take!(hnd)
true
julia> w.databuf
5-element Buffer{Cyclic,Float64,1}:
10.0
0.0
0.0
0.0
0.0
Since the databuf
is not full nothing is written to the file
of w
.
julia> w.file
JLDFile /tmp/cba80d8a-452c-4b5b-9411-7fb6e2f22f4b.jld2 (read/write)
(no datasets)
Let us continue triggering w
until the databuf
of w
is full.
julia> for t in 2. : 5.
put!(trg, t)
put!(oport, [t * 10])
take!(hnd)
end
Now check that the content of the file
of w
.
julia> w.file
JLDFile /tmp/cba80d8a-452c-4b5b-9411-7fb6e2f22f4b.jld2 (read/write)
โโ๐ข [5.0, 4.0, 3.0, 2.0, 1.0]
Note that the content of databuf
is written to the file
of w
. The operation of w
can be terminated.
julia> put!(trg, NaN)
When terminated, the file
of w
is closed.
julia> w.file
JLDFile /tmp/cba80d8a-452c-4b5b-9411-7fb6e2f22f4b.jld2 (read/write)
(closed)
In this example, w
does not have a plugin
so nothing has been derived or computed from the data in databuf
. The data in databuf
is just written to file
of w
. To further data processing, see Plugins
Full API
Causal.Writer
โ Typemutable struct Writer{A, FL, var"253", var"254", var"255", Symbol, var"256", var"263", Int, var"264", var"265", var"266", var"267"} <: AbstractSink
Constructs a Writer
whose input bus is input
. buflen
is the length of the internal buffer of Writer
. If not nothing, plugin
is used to processes the incomming data. path
determines the path of the file of Writer
.
Fields
action::Any
Writer action to write data to file
path::String
File path of the writer
file::Any
File in which data is recorded
trigger::Any
handshake::Any
callbacks::Any
name::Any
id::Any
input::Any
buflen::Any
plugin::Any
timebuf::Any
databuf::Any
sinkcallback::Any
The type of file
of Writer
is JLD2
.
When initialized, the file
of Writer
is closed. See open(writer::Writer)
and close(writer::Writer)
.
Causal.write!
โ Methodwrite!(writer, td, xd)
Writes xd
corresponding to xd
to the file of writer
.
Example
julia> w = Writer(Inport(1))
Writer(path:/tmp/e907d6ad-8db2-4c4a-9959-5b8d33d32156.jld2, nin:1)
julia> open(w)
Writer(path:/tmp/e907d6ad-8db2-4c4a-9959-5b8d33d32156.jld2, nin:1)
julia> write!(w, 0., 10.)
10.0
julia> write!(w, 1., 20.)
20.0
julia> w.file
JLDFile /tmp/e907d6ad-8db2-4c4a-9959-5b8d33d32156.jld2 (read/write)
โโ๐ข 0.0
โโ๐ข 1.0
julia> w.file[string(0.)]
10.0
Base.read
โ Methodread(writer; flatten)
Read the contents of the file of writer
and returns the sorted content of the file. If flatten
is true
, the content is also flattened.
Causal.fread
โ Functionfread(path; flatten)
Reads the content of jld2
file and returns the sorted file content.
Base.Filesystem.mv
โ Methodmv(writer, dst; force)
Moves the file of writer
to dst
. If force
is true
, the if dst
is not a valid path, it is forced to be constructed.
Example
julia> mkdir(joinpath(tempdir(), "testdir1"))
"/tmp/testdir1"
julia> mkdir(joinpath(tempdir(), "testdir2"))
"/tmp/testdir2"
julia> w = Writer(Inport(), path="/tmp/testdir1/myfile.jld2")
Writer(path:/tmp/testdir1/myfile.jld2, nin:1)
julia> mv(w, "/tmp/testdir2")
Writer(path:/tmp/testdir2/myfile.jld2, nin:1)
Base.Filesystem.cp
โ Methodcp(writer, dst; force, follow_symlinks)
Copies the file of writer
to dst
. If force
is true
, the if dst
is not a valid path, it is forced to be constructed. If follow_symlinks
is true
, symbolinks are followed.
Example
julia> mkdir(joinpath(tempdir(), "testdir1"))
"/tmp/testdir1"
julia> mkdir(joinpath(tempdir(), "testdir2"))
"/tmp/testdir2"
julia> w = Writer(Inport(), path="/tmp/testdir1")
Writer(path:/tmp/testdir1.jld2, nin:1)
julia> cp(w, "/tmp/testdir2")
Writer(path:/tmp/testdir2/1e72bad1-9800-4ca0-bccd-702afe75e555, nin:1)
Base.open
โ Methodopen(writer)
open(writer, mode)
Opens writer
by opening the its file
in read/write
mode. When writer
is not openned, it is not possible to write data in writer
. See also close(writer::Writer)
Base.close
โ Methodclose(writer)
Closes writer
by closing its file
. When writer
is closed, it is not possible to write data in writer
. See also open(writer::Writer)