-
Notifications
You must be signed in to change notification settings - Fork 16
/
dataset.jl
304 lines (263 loc) · 10 KB
/
dataset.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# ---------------------------------------------------------------------
"""
arrays2datablock(args::Vararg{AbstractArray, N}) where N
Convert one (or more) arrays into a `Vector{String}`.
This function performs the conversion from Julia arrays to a textual representation suitable to be sent to gnuplot as an *inline data block*.
"""
function arrays2datablock(args::Vararg{AbstractArray, N}) where N
tostring(v::AbstractString) = "\"" * string(v) * "\""
tostring(v::Real) = string(v)
tostring(::Missing) = "?"
#tostring(c::ColorTypes.RGB) = string(Int(c.r*255)) * " " * string(Int(c.g*255)) * " " * string(Int(c.b*255))
@assert length(args) > 0
# Collect lengths and number of dims
lengths = Vector{Int}()
dims = Vector{Int}()
firstMultiDim = 0
for i in 1:length(args)
d = args[i]
@assert ndims(d) <= 3 "Array dimensions must be <= 3"
push!(lengths, length(d))
push!(dims , ndims(d))
(firstMultiDim == 0) && (ndims(d) > 1) && (firstMultiDim = i)
end
@assert all((dims .== 1) .| (dims .== maximum(dims))) "Array size are incompatible"
accum = Vector{String}()
# All 1D
if firstMultiDim == 0
# @info "Case 1"
@assert minimum(lengths) == maximum(lengths) "Array size are incompatible"
for i in 1:lengths[1]
v = ""
for iarg in 1:length(args)
d = args[iarg]
v *= " " * tostring(d[i])
end
push!(accum, v)
end
return accum
end
# Multidimensional, no independent 1D indices
if firstMultiDim == 1
# @info "Case 2"
@assert minimum(lengths) == maximum(lengths) "Array size are incompatible"
i = 1
for CIndex in CartesianIndices(size(args[1]'))
indices = Tuple(CIndex)
(i > 1) && (indices[end-1] == 1) && (push!(accum, "")) # blank line
if length(args) == 1
# Add independent indices (starting from zero, useful when plotting "with image")
v = join(string.(getindex.(Ref(Tuple(indices)), 1:ndims(args[1])) .- 1), " ")
else
# Do not add independent indices since there is no way to distinguish a "z" array from additional arrays
v = ""
end
for iarg in 1:length(args)
d = args[iarg]'
v *= " " * tostring(d[i])
end
i += 1
push!(accum, v)
end
return accum
end
# Multidimensional (independent indices provided in input)
if firstMultiDim >= 2
refLength = lengths[firstMultiDim]
@assert all(lengths[firstMultiDim:end] .== refLength) "Array size are incompatible"
if lengths[1] < refLength
# @info "Case 3"
# Cartesian product of Independent variables
checkLength = prod(lengths[1:firstMultiDim-1])
@assert prod(lengths[1:firstMultiDim-1]) == refLength "Array size are incompatible"
i = 1
for CIndex in CartesianIndices(size(args[firstMultiDim]))
indices = Tuple(CIndex)
(i > 1) && (indices[end-1] == 1) && (push!(accum, "")) # blank line
v = ""
for iarg in 1:firstMultiDim-1
d = args[iarg]
v *= " " * tostring(d[indices[iarg]])
end
for iarg in firstMultiDim:length(args)
d = args[iarg]
v *= " " * tostring(d[i])
end
i += 1
push!(accum, v)
end
return accum
else
# @info "Case 4"
# All Independent variables have the same length as the main multidimensional data
@assert all(lengths[1:firstMultiDim-1] .== refLength) "Array size are incompatible"
i = 1
for CIndex in CartesianIndices(size(args[firstMultiDim]))
indices = Tuple(CIndex)
(i > 1) && (indices[end-1] == 1) && (push!(accum, "")) # blank line
v = ""
for iarg in 1:length(args)
d = args[iarg]
v *= " " * tostring(d[i])
end
i += 1
push!(accum, v)
end
return accum
end
end
return nothing
end
"""
Dataset
Abstract type for all dataset structures.
"""
abstract type Dataset end
"""
DatasetText
A dataset whose data are stored as a text buffer.
Transmission to gnuplot may be slow for large datasets, but no temporary file is involved, and the dataset can be saved directly into a gnuplot script. Also, the constructor allows to build more flexible datasets (i.e. mixing arrays with different dimensions).
Constructors are defined as follows:
```julia
DatasetText(data::Vector{String})
DatasetText(data::Vararg{AbstractArray, N}) where N
```
In the second form the type of elements of each array must be one of `Real`, `AbstractString` and `Missing`.
"""
mutable struct DatasetText <: Dataset
preview::Vector{String}
data::String
DatasetText(::Val{:inner}, preview, data) = new(preview, data)
end
DatasetText(args::Vararg{AbstractArray, N}) where N = DatasetText(arrays2datablock(args...))
function DatasetText(data::Vector{String})
preview = (length(data) <= 4 ? deepcopy(data) : [data[1:4]..., "..."])
d = DatasetText(Val(:inner), preview, join(data, "\n"))
return d
end
# ---------------------------------------------------------------------
"""
DatasetBin
A dataset whose data are stored as a binary file.
Ensure best performances for large datasets, but involve use of a temporary file. When saving a script the file is stored in a directory with the same name as the main script file.
Constructors are defined as follows:
```julia
DatasetBin(cols::Vararg{AbstractMatrix, N}) where N
DatasetBin(cols::Vararg{AbstractVector, N}) where N
```
In both cases the element of the arrays must be a numeric type.
"""
mutable struct DatasetBin <: Dataset
file::String
source::String
DatasetBin(::Val{:inner}, file, source) = new(file, source)
end
# ---------------------------------------------------------------------
#=
The following is dismissed since `binary matrix` do not allows to use
keywords such as `rotate`.
function write_binary(M::Matrix{T}) where T <: Real
x = collect(1:size(M)[1])
y = collect(1:size(M)[2])
MS = Float32.(zeros(length(x)+1, length(y)+1))
MS[1,1] = length(x)
MS[1,2:end] = y
MS[2:end,1] = x
MS[2:end,2:end] = M
(path, io) = mktemp()
write(io, MS)
close(io)
return (path, " '$path' binary matrix")
end
=#
# ---------------------------------------------------------------------
# using Base.Threads
function DatasetBin(VM::Vararg{AbstractMatrix, N}) where N
for i in 2:N
@assert size(VM[i]) == size(VM[1])
end
s = size(VM[1])
# path = tempname()
# run(`mkfifo $path`)
# Base.Threads.@spawn begin
# io = open(path, "w")
(path, io) = mktemp()
for i in 1:s[1]
for j in 1:s[2]
for k in 1:N
write(io, Float32(VM[k][i,j]))
end
end
end
close(io)
# end # use "volatile" keyword
source = " '$path' binary array=(" * join(string.(reverse(s)), ", ") * ")"
# Note: can't add `using` here, otherwise we can't append `flipy`.
return DatasetBin(Val(:inner), path, source)
end
# ---------------------------------------------------------------------
function DatasetBin(cols::Vararg{AbstractVector, N}) where N
source = "binary record=$(length(cols[1])) format='"
types = Vector{DataType}()
#(length(cols) == 1) && (source *= "%int")
for i in 1:length(cols)
@assert length(cols[1]) == length(cols[i])
if isa(cols[i][1], Int32); push!(types, Int32); source *= "%int32"
elseif isa(cols[i][1], Int); push!(types, Int64); source *= "%int64"
elseif isa(cols[i][1], Float32); push!(types, Float32); source *= "%float32"
elseif isa(cols[i][1], Float64); push!(types, Float64); source *= "%float64"
elseif isa(cols[i][1], Char); push!(types, Char); source *= "%char"
else
error("Unsupported data on column $i: $(typeof(cols[i][1]))")
end
end
source *= "'"
(path, io) = mktemp()
source = " '$path' $source"
for row in 1:length(cols[1])
#(length(cols) == 1) && (write(io, convert(Int32, row)))
for col in 1:length(cols)
write(io, convert(types[col], cols[col][row]))
end
end
close(io)
#=
The following using clause is needed to cope with the following case:
x = randn(10001)
@gp x x x "w p lc pal" # Error: Not enough columns for variable color
@gsp x x x "w p lc pal" # this works regardless of the using clause
But adding this clause here implies we should check for duplicated
using clause in collect_commands()
=#
source *= " using " * join(1:N, ":")
return DatasetBin(Val(:inner), path, source)
end
# ---------------------------------------------------------------------
function useBinaryMethod(args...)
@assert options.preferred_format in [:auto, :bin, :text] "Unexpected value for `options.preferred_format`: $(options.preferred_format)"
binary = false
if options.preferred_format == :bin
binary = true
elseif options.preferred_format == :auto
if (length(args) == 1) && isa(args[1], AbstractMatrix)
binary = true
elseif all(ndims.(args) .== 1) && all(Base.:<:.(eltype.(args), Real))
s = sum(length.(args))
if s > 1e4
binary = true
end
end
end
return binary
end
# ---------------------------------------------------------------------
function Dataset(args)
if useBinaryMethod(args...)
try
return DatasetBin(args...)
catch err
isa(err, MethodError) || rethrow()
end
end
return DatasetText(args...)
end