Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add broadcasting of AbstractDataFrame #1840

Merged
merged 30 commits into from
Jun 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8bb061d
add broadcasting of AbstractDataFrame
bkamins Jun 8, 2019
6a6e954
switch to Tables.allocatecolumn
bkamins Jun 8, 2019
6f151ac
one more fix
bkamins Jun 8, 2019
628229b
revert similar and fix tests
bkamins Jun 8, 2019
8af3a0f
Apply suggestions from code review
bkamins Jun 9, 2019
c9a98bd
corrections after code review
bkamins Jun 9, 2019
5b4cc36
fix typo
bkamins Jun 9, 2019
24566e4
fix broadcasting assignment bug
bkamins Jun 20, 2019
2b82f79
fix SubDataFrame case
bkamins Jun 20, 2019
3810f75
add unaliasing of data frame against data frame
bkamins Jun 20, 2019
8e335f1
small fixes in legacy code
bkamins Jun 20, 2019
80a131e
optimized broadcasting
bkamins Jun 20, 2019
82e53a6
correct unaliasing
bkamins Jun 20, 2019
87206a2
small performance optimization
bkamins Jun 20, 2019
fba7cef
performance improvements
bkamins Jun 20, 2019
0e63fb8
add more broadcasting tests
bkamins Jun 20, 2019
bb4862b
more tests
bkamins Jun 20, 2019
5b8d2ec
Merge branch 'master' into new_dataframe_broadcasting
bkamins Jun 21, 2019
699cb6b
Merge branch 'master' into new_dataframe_broadcasting
bkamins Jun 21, 2019
6780b26
even more tests
bkamins Jun 21, 2019
432a530
getcolbc cleanup
bkamins Jun 21, 2019
3fdf733
fix after a code review
bkamins Jun 21, 2019
a67a3f5
unalias optimizations
bkamins Jun 21, 2019
5784100
more tests for common cases
bkamins Jun 21, 2019
b1813e1
improve helper signature
bkamins Jun 21, 2019
6a87c42
minor improvements
bkamins Jun 22, 2019
27c730a
minor improvements 2
bkamins Jun 22, 2019
9a68d30
Apply suggestions from code review
bkamins Jun 23, 2019
5d3ec40
fixes after code review
bkamins Jun 23, 2019
1f46086
Fix indentation
nalimilan Jun 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions src/other/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,36 +128,41 @@ function Base.Broadcast.broadcast_unalias(dest::AbstractDataFrame, src)
src
end

function _broadcast_unalias_helper(dest, scol, src, col2, wascopied)
bkamins marked this conversation as resolved.
Show resolved Hide resolved
# 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 = dest[col1]
if Base.mightalias(dcol, scol)
if src isa SubDataFrame
if !wascopied
src =SubDataFrame(copy(parent(src), copycols=false),
index(src), rows(src))
end
parentidx = parentcols(index(src), col2)
parent(src)[parentidx] = Base.unaliascopy(parent(src)[parentidx])

else
if !wascopied
src = copy(src, copycols=false)
end
src[col2] = Base.unaliascopy(scol)
end
return src, true
end
end
return src, wascopied
end

function Base.Broadcast.broadcast_unalias(dest::AbstractDataFrame, src::AbstractDataFrame)
if size(dest, 2) != size(src, 2)
throw(ArgumentError("Dimension mismatch in broadcasting."))
end
# col2 can be checked from col1 point as we are writing broadcasting
# results from 1 to ncol
wascopied = false
for col1 in axes(dest, 2)
for col2 in col1:ncol(src)
dcol = dest[col1]
scol = src[col2]
if Base.mightalias(dcol, scol)
if src isa SubDataFrame
if !wascopied
src = SubDataFrame(copy(parent(src), copycols=false),
index(src), rows(src))
wascopied = true
end
parentidx = parentcols(index(src), col2)
parent(src)[parentidx] = Base.unaliascopy(parent(src)[parentidx])
else
if !wascopied
src = copy(src, copycols=false)
wascopied = true
end
src[col2] = Base.unaliascopy(scol)
end
break
end
end
for col2 in axes(dest, 2)
scol = src[col2]
src, wascopied = _broadcast_unalias_helper(dest, scol, src, col2, wascopied)
end
src
end
Expand Down
29 changes: 29 additions & 0 deletions test/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -841,4 +841,33 @@ end
@test df == DataFrame(dfm)
end

@testset "test common cases" begin
m = rand(1000, 10)
df = DataFrame(m)
@test df .+ 1 == DataFrame(m .+ 1)
@test df .+ transpose(1:10) == DataFrame(m .+ transpose(1:10))
@test df .+ (1:1000) == DataFrame(m .+ (1:1000))
@test df .+ m == DataFrame(m .+ m)
@test m .+ df == DataFrame(m .+ m)
@test df .+ df == DataFrame(m .+ m)

df .+= 1
m .+= 1
@test df == DataFrame(m)
df .+= transpose(1:10)
m .+= transpose(1:10)
@test df == DataFrame(m)
df .+= (1:1000)
m .+= (1:1000)
@test df == DataFrame(m)
df .+= df
m .+= m
@test df == DataFrame(m)
df2 = copy(df)
m2 = copy(m)
df .+= df .+ df2 .+ m2 .+ 1
m .+= m .+ df2 .+ m2 .+ 1
@test df == DataFrame(m)
end

end # module