-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbroadcasting.jl
384 lines (345 loc) · 14.8 KB
/
broadcasting.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
### Broadcasting
Base.getindex(ds::AbstractDataset, idx::CartesianIndex{2}) = ds[idx[1], idx[2]]
Base.view(ds::AbstractDataset, idx::CartesianIndex{2}) = view(ds, idx[1], idx[2])
Base.setindex!(ds::AbstractDataset, val, idx::CartesianIndex{2}) =
(ds[idx[1], idx[2]] = val)
Base.broadcastable(ds::AbstractDataset) = ds
Base.broadcastable(col::DatasetColumn) = __!(col)
Base.broadcastable(col::SubDatasetColumn) = __!(col)
function _our_copy(ds::Dataset; copycols = true)
# TODO currently if the observation is mutable, copying data set doesn't protect it
# Create Dataset
newds = Dataset(copy(_columns(ds)), copy(index(ds)); copycols = copycols)
setinfo!(newds, _attributes(ds).meta.info[])
return newds
end
struct DatasetStyle <: Base.Broadcast.BroadcastStyle end
Base.Broadcast.BroadcastStyle(::Type{<:AbstractDataset}) =
DatasetStyle()
Base.Broadcast.BroadcastStyle(::DatasetStyle, ::Base.Broadcast.BroadcastStyle) =
DatasetStyle()
Base.Broadcast.BroadcastStyle(::Base.Broadcast.BroadcastStyle, ::DatasetStyle) =
DatasetStyle()
Base.Broadcast.BroadcastStyle(::DatasetStyle, ::DatasetStyle) = DatasetStyle()
function copyto_widen!(res::AbstractVector{T}, bc::Base.Broadcast.Broadcasted,
pos, col) where T
for i in pos:length(axes(bc)[1])
val = bc[CartesianIndex(i, col)]
S = typeof(val)
if S <: T || promote_type(S, T) <: T
res[i] = val
else
newres = similar(Vector{promote_type(S, T)}, length(res))
copyto!(newres, 1, res, 1, i-1)
newres[i] = val
return copyto_widen!(newres, bc, i + 1, col)
end
end
return res
end
function getcolbc(bcf::Base.Broadcast.Broadcasted{Style}, colind) where {Style}
# we assume that bcf is already flattened and unaliased
newargs = map(bcf.args) do x
Base.Broadcast.extrude(x isa AbstractDataset ? _columns(x)[colind] : x)
end
return Base.Broadcast.Broadcasted{Style}(bcf.f, newargs, bcf.axes)
end
function Base.copy(bc::Base.Broadcast.Broadcasted{DatasetStyle})
ndim = length(axes(bc))
if ndim != 2
throw(DimensionMismatch("cannot broadcast a data set into $ndim dimensions"))
end
bcf = Base.Broadcast.flatten(bc)
colnames = unique!(Any[_names(ds) for ds in bcf.args if ds isa AbstractDataset])
if length(colnames) != 1
wrongnames = setdiff(union(colnames...), intersect(colnames...))
if isempty(wrongnames)
throw(ArgumentError("Column names in broadcasted data sets " *
"must have the same order"))
else
msg = join(wrongnames, ", ", " and ")
throw(ArgumentError("Column names in broadcasted data sets must match. " *
"Non matching column names are $msg"))
end
end
nrows = length(axes(bcf)[1])
ds = Dataset()
for i in axes(bcf)[2]
if nrows == 0
col = Any[]
else
bcf′ = getcolbc(bcf, i)
v1 = bcf′[CartesianIndex(1, i)]
startcol = similar(Vector{typeof(v1)}, nrows)
startcol[1] = v1
col = copyto_widen!(startcol, bcf′, 2, i)
end
ds[!, colnames[1][i]] = col
end
return ds
end
### Broadcasting assignment
struct LazyNewColDataset{T}
ds::Dataset
col::T
end
Base.axes(x::LazyNewColDataset) = (Base.OneTo(nrow(x.ds)),)
Base.ndims(::Type{<:LazyNewColDataset}) = 1
Base.materialize!(::DatasetColumn, ::Base.Broadcast.Broadcasted) =
throw(ArgumentError("syntax ds.column .= val is not supported, use ds[!, column] .= val instead"))
struct ColReplaceDataset
ds::Dataset
cols::Vector{Int}
end
Base.axes(x::ColReplaceDataset) = (axes(x.ds, 1), Base.OneTo(length(x.cols)))
Base.ndims(::Type{ColReplaceDataset}) = 2
Base.maybeview(ds::AbstractDataset, idx::CartesianIndex{2}) = ds[idx]
Base.maybeview(ds::AbstractDataset, row::Integer, col::ColumnIndex) = ds[row, col]
Base.maybeview(ds::AbstractDataset, rows, cols) = view(ds, rows, cols)
Base.maybeview(col1::DatasetColumn, rows) = view(col1, rows)
Base.maybeview(col1::SubDatasetColumn, rows) = view(col1, rows)
function Base.dotview(ds::Dataset, ::Colon, cols::ColumnIndex)
haskey(index(ds), cols) && return view(ds, :, cols)
if !(cols isa SymbolOrString)
throw(ArgumentError("creating new columns using an integer index is disallowed"))
end
return LazyNewColDataset(ds, Symbol(cols))
end
function Base.dotview(ds::Dataset, ::typeof(!), cols)
if !(cols isa ColumnIndex)
return ColReplaceDataset(ds, index(ds)[cols])
end
if !(cols isa SymbolOrString) && cols > ncol(ds)
throw(ArgumentError("creating new columns using an integer index is disallowed"))
end
return LazyNewColDataset(ds, cols isa AbstractString ? Symbol(cols) : cols)
end
# Base.dotview(ds::SubDataset, ::typeof(!), idxs) =
# throw(ArgumentError("broadcasting with ! row selector is not allowed for SubDataset"))
# TODO: remove the deprecations when Julia 1.7 functionality is commonly used
# by the community
if isdefined(Base, :dotgetproperty)
function Base.dotgetproperty(ds::Dataset, col::SymbolOrString)
if columnindex(ds, col) == 0
return LazyNewColDataset(ds, Symbol(col))
else
Base.depwarn("In the future this operation will allocate a new column " *
"instead of performing an in-place assignment.", :dotgetproperty)
return getproperty(ds, col)
end
end
function Base.dotgetproperty(ds::SubDataset, col::SymbolOrString)
Base.depwarn("broadcasting getproperty is deprecated for SubDataset and " *
"will be disallowed in the future. Use `ds[:, $(repr(col))] .= ... instead",
:dotgetproperty)
return getproperty(ds, col)
end
end
# TODO we shouldn't allocate a vector here
function Base.copyto!(lazyds::LazyNewColDataset, bc::Base.Broadcast.Broadcasted{T}) where T
if bc isa Base.Broadcast.Broadcasted{<:Base.Broadcast.AbstractArrayStyle{0}}
bc_tmp = Base.Broadcast.Broadcasted{T}(bc.f, bc.args, ())
v = Base.Broadcast.materialize(bc_tmp)
col = similar(Vector{Union{typeof(v), Missing}}, nrow(lazyds.ds))
copyto!(col, bc)
else
col = Base.Broadcast.materialize(bc)
end
lazyds.ds[!, lazyds.col] = col
end
function Base.copyto!(col1::SubDatasetColumn, bc::Base.Broadcast.Broadcasted{T}) where T
if bc isa Base.Broadcast.Broadcasted{<:Base.Broadcast.AbstractArrayStyle{0}}
bc_tmp = Base.Broadcast.Broadcasted{T}(bc.f, bc.args, ())
v = Base.Broadcast.materialize(bc_tmp)
col = similar(Vector{Union{Missing, typeof(v)}}, length(col1))
copyto!(col, bc)
else
col = Base.Broadcast.materialize(bc)
end
col1.val[col1.selected_index] = col
if col1.col ∈ index(col1.ds).sortedcols
_reset_grouping_info!(col1.ds)
end
_modified(_attributes(col1.ds))
col
end
function _fill_copyto_helper!(incol, bc, col)
@inbounds for row in eachindex(incol)
incol[row] = bc[CartesianIndex(row, col)]
end
end
function _copyto_helper!(dscol::AbstractVector, bc::Base.Broadcast.Broadcasted, col::Int)
if axes(dscol, 1) != axes(bc)[1]
# this should never happen unless data frame is corrupted (has unequal column lengths)
throw(DimensionMismatch("Dimension mismatch in broadcasting. The updated" *
" data frame is invalid and should not be used"))
end
_fill_copyto_helper!(dscol, bc, col)
end
function _copyto_helper!(dscol::Union{SubDatasetColumn, DatasetColumn}, bc::Base.Broadcast.Broadcasted, col::Int)
if axes(__!(dscol), 1) != axes(bc)[1]
# this should never happen unless data frame is corrupted (has unequal column lengths)
throw(DimensionMismatch("Dimension mismatch in broadcasting. The updated" *
" data frame is invalid and should not be used"))
end
_fill_copyto_helper!(__!(dscol), bc, col)
_modified(_attributes(dscol.ds))
end
function Base.Broadcast.broadcast_unalias(dest::AbstractDataset, src)
for i in 1:ncol(dest)
src = Base.Broadcast.unalias(_columns(dest)[i], src)
end
return src
end
function Base.Broadcast.broadcast_unalias(dest, src::AbstractDataset)
wascopied = false
for (i, col) in enumerate(eachcol(src))
if Base.mightalias(dest, __!(col))
if src isa SubDataset
if !wascopied
src = SubDataset(_our_copy(parent(src), copycols=false),
index(src), rows(src))
end
parentidx = parentcols(index(src), i)
parent(src)[!, parentidx] = Base.unaliascopy(_columns(parent(src))[parentidx])
else
if !wascopied
src = _our_copy(src, copycols=false)
end
src[!, i] = Base.unaliascopy(__!(col))
end
wascopied = true
end
end
return src
end
#TODO for view of data sets parent columns are copyied, e.g. view(ds, 1:10, :) .+= 1
function _broadcast_unalias_helper(dest::AbstractDataset, scol::AbstractVector,
src::AbstractDataset, col2::Int, wascopied::Bool)
# col1 can be checked till col2 point as we are writing broadcasting
# results from 1 to ncol
# we go downwards because aliasing when col1 == col2 is most probable
for col1 in col2:-1:1
dcol = _columns(dest)[col1] #dest[!, col1]
if Base.mightalias(dcol, scol)
if src isa SubDataset
if !wascopied
src =SubDataset(_our_copy(parent(src), copycols=false),
index(src), rows(src))
end
parentidx = parentcols(index(src), col2)
parent(src)[!, parentidx] = Base.unaliascopy(_columns(parent(src))[parentidx])
# parent(src)[!, parentidx] = Base.unaliascopy(parent(src)[!, parentidx])
else
if !wascopied
src = _our_copy(src, copycols=false)
end
src[!, col2] = Base.unaliascopy(scol)
end
return src, true
end
end
return src, wascopied
end
_broadcast_unalias_helper(dest::AbstractDataset, scol::DatasetColumn,
src::AbstractDataset, col2::Int, wascopied::Bool) = _broadcast_unalias_helper(dest, scol.val,
src, col2, wascopied)
_broadcast_unalias_helper(dest::AbstractDataset, scol::SubDatasetColumn,
src::AbstractDataset, col2::Int, wascopied::Bool) = _broadcast_unalias_helper(dest, __!(scol),
src, col2, wascopied)
function Base.Broadcast.broadcast_unalias(dest::AbstractDataset, src::AbstractDataset)
if size(dest, 2) != size(src, 2)
throw(DimensionMismatch("Dimension mismatch in broadcasting."))
end
wascopied = false
for col2 in axes(dest, 2)
scol = src[!, col2]
src, wascopied = _broadcast_unalias_helper(dest, scol, src, col2, wascopied)
end
_modified(_attributes(dest))
return src
end
function Base.copyto!(ds::AbstractDataset, bc::Base.Broadcast.Broadcasted)
bcf = Base.Broadcast.flatten(bc)
colnames = unique!(Any[_names(x) for x in bcf.args if x isa AbstractDataset])
if length(colnames) > 1 || (length(colnames) == 1 && _names(ds) != colnames[1])
push!(colnames, _names(ds))
wrongnames = setdiff(union(colnames...), intersect(colnames...))
if isempty(wrongnames)
throw(ArgumentError("Column names in broadcasted data frames " *
"must have the same order"))
else
msg = join(wrongnames, ", ", " and ")
throw(ArgumentError("Column names in broadcasted data frames must match. " *
"Non matching column names are $msg"))
end
end
bcf′ = Base.Broadcast.preprocess(ds, bcf)
for i in axes(ds, 2)
_copyto_helper!(ds[!, i], getcolbc(bcf′, i), i)
end
return ds
end
function Base.copyto!(ds::AbstractDataset,
bc::Base.Broadcast.Broadcasted{<:Base.Broadcast.AbstractArrayStyle{0}})
# special case of fast approach when bc is providing an untransformed scalar
if bc.f === identity && bc.args isa Tuple{Any} && Base.Broadcast.isflat(bc)
for col in axes(ds, 2)
fill!(_columns(ds)[col], bc.args[1][])
end
_modified(_attributes(ds))
return ds
else
return copyto!(ds, convert(Base.Broadcast.Broadcasted{Nothing}, bc))
end
end
create_bc_tmp(bcf′_col::Base.Broadcast.Broadcasted{T}) where {T} =
Base.Broadcast.Broadcasted{T}(bcf′_col.f, bcf′_col.args, ())
function Base.copyto!(crds::ColReplaceDataset, bc::Base.Broadcast.Broadcasted)
bcf = Base.Broadcast.flatten(bc)
colnames = unique!(Any[_names(x) for x in bcf.args if x isa AbstractDataset])
if length(colnames) > 1 ||
(length(colnames) == 1 && view(_names(crds.ds), crds.cols) != colnames[1])
push!(colnames, view(_names(crds.ds), crds.cols))
wrongnames = setdiff(union(colnames...), intersect(colnames...))
if isempty(wrongnames)
throw(ArgumentError("Column names in broadcasted data frames " *
"must have the same order"))
else
msg = join(wrongnames, ", ", " and ")
throw(ArgumentError("Column names in broadcasted data frames must match. " *
"Non matching column names are $msg"))
end
end
bcf′ = Base.Broadcast.preprocess(crds, bcf)
nrows = length(axes(bcf′)[1])
for (i, col_idx) in enumerate(crds.cols)
bcf′_col = getcolbc(bcf′, i)
if bcf′_col isa Base.Broadcast.Broadcasted{<:Base.Broadcast.AbstractArrayStyle{0}}
bc_tmp = create_bc_tmp(bcf′_col)
v = Base.Broadcast.materialize(bc_tmp)
newcol = similar(Vector{Union{Missing, typeof(v)}}, nrow(crds.ds))
copyto!(newcol, bc)
else
if nrows == 0
newcol = Any[]
else
v1 = bcf′_col[CartesianIndex(1, i)]
startcol = similar(Vector{Union{Missing, typeof(v1)}}, nrows)
startcol[1] = v1
newcol = copyto_widen!(startcol, bcf′_col, 2, i)
end
end
crds.ds[!, col_idx] = newcol
end
return crds.ds
end
Base.Broadcast.broadcast_unalias(dest::DatasetRow, src) =
Base.Broadcast.broadcast_unalias(parent(dest), src)
function Base.copyto!(dsr::DatasetRow, bc::Base.Broadcast.Broadcasted)
bc′ = Base.Broadcast.preprocess(dsr, bc)
for I in eachindex(bc′)
dsr[I] = bc′[I]
end
return dsr
end