text:
using Revise

using Douglass
using RDatasets, Test
using DataFrames
using DataFramesMeta

df = dataset("datasets", "iris")
Douglass.@use(df)

# `gen` without anything else ****************************************************************************
d"generate :x = 1.0"
@test all(df.x .== 1.0)
select!(df, Not(:x))
d"generate :x = :SepalLength"
@test all(df.x .== df.SepalLength)
select!(df, Not(:x))
d"generate :x = :SepalWidth + :PetalLength"
@test (:x ∈ propertynames(df))
@test df[1,:x] ≈ 4.9 atol = 1e-4
select!(df, Not(:x))    # this drops the column, and we can re-start
d"generate :x = :SepalWidth + :PetalLength if :PetalLength .> 1.3"
@test all((df.x .== df.SepalWidth .+ df.PetalLength) .| (df.PetalLength .<= 1.3))
@test sum(skipmissing(df.x)) ≈ 972.3 atol = 1e-4
#select!(df, Not(:x))
d"generate :y = 99 if :PetalLength .> 1.3"
@test (eltype(df.y) == Union{Missing, Int64})
d"generate :x2 = _n"
@test (df.x2[1] == 1) && (df.x2[end] == 150)
select!(df, Not(:x2))
# can we use stuff from julia?
d"generate :z = rand(Float64)"

# `gen` with `bysort` *************************************************************************
df = dataset("datasets", "iris")
Douglass.set_active_df(:df)

d"by :Species : generate :x = 1.0"
@test all(df.x .== 1.0)
select!(df, Not(:x))
d"by :Species : generate :x = :SepalLength"
@test all(df.x .== df.SepalLength)
select!(df, Not(:x))
d"by :Species : generate :x = :SepalWidth + :PetalLength"
@test (:x ∈ propertynames(df))
@test df[1,:x] ≈ 4.9 atol = 1e-4
select!(df, Not(:x))    # this drops the column, and we can re-start
d"bysort :Species (:SepalLength) : generate :x = :SepalWidth + :PetalLength if :PetalLength .> 1.3"
@test ismissing(df[1,:x])
@test sum(skipmissing(df.x)) ≈ 972.3 atol = 1e-4
#select!(df, Not(:x))
d"bysort :Species (:SepalLength) : generate :y = 99 if :PetalLength .> 1.3"
@test (eltype(df.y) == Union{Missing, Int64})
d"by :Species : generate :x2 = _n"
@test (df.x2[1] == 1) && (df.x2[end] == 50)
select!(df, Not(:x2))
# this should keep :y as a Int64
d"bysort :Species (:SepalLength) : replace :y = 99.0 if :SepalLength > 7.8"
@test (eltype(df.y) == Union{Missing, Int64})
d"gen :z = 0.0"
d"bysort :Species (:SepalLength) : replace :z = :x[_n - 2]"
sort!(df, [:Species, :SepalLength])
select!(df, Not(:y))
# Test the generate without by/bysort
select!(df, Not(:z))
d"gen :z = 1.0"
@test (:z ∈ propertynames(df))
@test df[1,:z] == 1.0
select!(df, Not(:z))
d"generate :z = :SepalWidth + :PetalLength"
@test sum(skipmissing(df.z)) ≈ 1022.3  atol = 1e-4
select!(df, Not(:z))
d"gen :z = :SepalWidth + :PetalLength if :PetalWidth > 1.8"
@test sum(skipmissing(df.z)) ≈ 295.9  atol = 1e-4
d"gen :z2 = _n"
d"drop :z2"
@test :z2 ∉ propertynames(df)
d"gen :z2 = :SepalLength"

# `drop` *************************************************************************

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)

d"drop :SepalLength"
@test :SepalLength ∉ propertynames(df)
d"drop :PetalWidth :PetalLength"
@test :PetalWidth ∉ propertynames(df)
@test :PetalLength ∉ propertynames(df)

d"drop if :SepalWidth > 3.0"
@test size(df, 1) == 83
d"drop if _n > 10"
@test size(df, 1) == 10
d"drop if _n < _N / 2"
@test size(df, 1) == 6

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)
d"bysort :Species : drop if :SepalLength == 5.1"
@test size(df,1) == 141
d"bysort :Species : drop if _n > 20"
@test size(df,1) == 60

# `keep` *************************************************************************

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)

d"keep :SepalLength :SepalWidth"
@test :PetalWidth ∉ propertynames(df)
d"keep if :SepalWidth > 3.1"
@test size(df,1) == 56
d"keep if _n <= 10"
@test size(df,1) == 10

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)
d"bysort :Species : keep if :SepalLength != 5.1"
@test size(df,1) == 141
d"bysort :Species : keep if _n <= 20"
@test size(df,1) == 60

# `egen` *************************************************************************

# with `by` and `if`

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)
using Statistics

d"bysort :Species (:SepalLength) : egen :x = mean(:PetalWidth) if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
select!(df, Not([:x]))
d"bysort :Species (:SepalLength) : egen :x = :SepalLength if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
select!(df, Not([:x]))
d"bysort :Species (:SepalLength) : egen :x = 1.0 if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
select!(df, Not([:x]))

# with `by` but not `if`

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)
using Statistics

d"bysort :Species (:SepalLength) : egen :x = mean(:PetalWidth) "
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
select!(df, Not([:x]))
d"bysort :Species (:SepalLength) : egen :x = :SepalLength"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
select!(df, Not([:x]))
d"bysort :Species (:SepalLength) : egen :x = 1.0 "
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
select!(df, Not([:x]))

# without `by` but with `if`

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)
using Statistics

d"egen :x = mean(:PetalWidth) if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
select!(df, Not([:x]))
d"egen :x = :SepalLength if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
select!(df, Not([:x]))
d"egen :x = 1.0 if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
select!(df, Not([:x]))

# without `by` and without `if`

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)
using Statistics

d"egen :x = mean(:PetalWidth)"
@test all(.!ismissing.(df.x) )
@test df.x[1] ≈ 1.19933333 atol = 1e-4
select!(df, Not([:x]))
d"egen :x = :SepalLength "
@test all(.!ismissing.(df.x)  )
@test df.x[1] ≈ 5.1 atol = 1e-4
select!(df, Not([:x]))
d"egen :x = 1.0 "
@test all(.!ismissing.(df.x)  )
@test df.x[1] ≈ 1.0 atol = 1e-4
select!(df, Not([:x]))

# ereplace *************

# with `by` and `if`

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)
using Statistics

d"gen :x = 1.0"

d"bysort :Species (:SepalLength) : erep :x = mean(:PetalWidth) if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
d"bysort :Species (:SepalLength) : erep :x = :SepalLength if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
d"bysort :Species (:SepalLength) : erep :x = 1.0 if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )

d"bysort :Species (:SepalLength) : erep :x = mean(:PetalWidth) "
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
d"bysort :Species (:SepalLength) : erep :x = :SepalLength"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
d"bysort :Species (:SepalLength) : erep :x = 1.0 "
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )

d"erep :x = mean(:PetalWidth) if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
d"erep :x = :SepalLength if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )
d"erep :x = 1.0 if :SepalWidth .< 3.4"
@test all(.!ismissing.(df.x) .| (df.SepalWidth .>= 3.4) )

d"erep :x = mean(:PetalWidth)"
@test all(.!ismissing.(df.x) )
@test df.x[1] ≈ 1.19933333 atol = 1e-4
d"erep :x = :SepalLength "
@test all(.!ismissing.(df.x)  )
d"erep :x = 1.0 "
@test all(.!ismissing.(df.x)  )
@test df.x[1] ≈ 1.0 atol = 1e-4

# `merge` ************************************************************************

# 1:1
people = DataFrame(ID = [20, 40], Name = ["John Doe", "Jane Doe"])
jobs = DataFrame(ID = [20, 40], Job = ["Lawyer", "Doctor"])
Douglass.set_active_df(:people)
d"merge_11 :ID using jobs"
@test size(people) == (2,3)

people = DataFrame(ID = [20, 40], Name = ["John Doe", "Jane Doe"])
jobs = DataFrame(ID = [20, 60], Job = ["Lawyer", "Astronaut"])
Douglass.set_active_df(:people)
d"merge_11 :ID using jobs"
@test size(people) == (3,3)

# multiple keys
people = DataFrame(ID = [20, 40], Birthday = ["1980-01-01", "1970-01-01"], Name = ["John Doe", "Jane Doe"])
jobs = DataFrame(ID = [20, 40], Birthday = ["1980-01-01", "1970-01-01"], Job = ["Lawyer", "Doctor"])
Douglass.set_active_df(:people)
d"merge_11 :ID :Birthday using jobs"
@test size(people) == (2,4)

# 1:m
people = DataFrame(ID = [20, 40], Name = ["John Doe", "Jane Doe"])
jobs = DataFrame(ID = [20, 20, 60], Job = ["Lawyer", "Economist", "Astronaut"])
Douglass.set_active_df(:people)
d"merge_1m :ID using jobs"
@test size(people) == (4,3)

# m:1
people = DataFrame(ID = [20, 40, 40], Name = ["John Doe", "Jane Doe", "The CEO"])
jobs = DataFrame(ID = [20, 40, 60], Job = ["Lawyer", "Doctor", "Astronaut"])
Douglass.set_active_df(:people)
d"merge_m1 :ID using jobs"
@test size(people) == (4,3)

# `rename` ***********************************************************************

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)

d"rename :SepalLength :SL"
@test :SL ∈ propertynames(df)

# `replace` **********************************************************************

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)

d"gen :x = 1.0"

# `replace` without anything else
d"replace :x = 1.0"
@test all(df.x .== 1.0)
d"replace :x = :SepalLength"
@test all(df.x .== df.SepalLength)
d"replace :x = :SepalWidth + :PetalLength"
@test (:x ∈ propertynames(df))
@test df[1,:x] ≈ 4.9 atol = 1e-4
d"replace :x = :SepalWidth + :PetalLength if :PetalLength .> 1.3"
@test all((df.x .== df.SepalWidth .+ df.PetalLength) .| (df.PetalLength .<= 1.3))
d"replace :x = 99 if :PetalLength .> 1.3"
# replace does not change the type
@test (eltype(df.x) == Union{Missing, Float64})
df[!,:x] = ones(Int64,150)
allowmissing!(df, :x)
d"replace :x = _n"
@test (df.x[1] == 1) && (df.x[end] == 150)
# can we use stuff from julia?
d"replace :x = rand(0:10)"
@test (eltype(df.x) == Union{Missing, Int64})

# `replace` with `bysort`

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)
d"gen :x = 1.0"

d"by :Species : replace :x = 1.0"
@test all(df.x .== 1.0)
d"by :Species : replace :x = :SepalLength"
@test all(df.x .== df.SepalLength)
d"by :Species : replace :x = :SepalWidth + :PetalLength"
@test (:x ∈ propertynames(df))
@test df[1,:x] ≈ 4.9 atol = 1e-4
d"bysort :Species (:SepalLength) : replace :x = :SepalWidth + :PetalLength if :PetalLength .> 1.3"
@test sum(skipmissing(df.x)) ≈ 1022.3 atol = 1e-4
d"gen :y = 1"
d"bysort :Species (:SepalLength) : replace :y = 99 if :PetalLength .> 1.3"
@test (eltype(df.y) == Union{Missing, Int64})
d"by :Species : replace :y = _n"
@test (df.y[1] == 1) && (df.y[end] == 50)
# this should keep :y as a Int64
d"bysort :Species (:SepalLength) : replace :y = 99.0 if :SepalLength > 7.8"
@test (eltype(df.y) == Union{Missing, Int64})
d"gen :z = 0.0"
d"bysort :Species (:SepalLength) : replace :z = :x[_n - 2]"
sort!(df, [:Species, :SepalLength])
d"replace :z = 1.0"
@test (:z ∈ propertynames(df))
@test df[1,:z] == 1.0
d"replace :z = :SepalWidth + :PetalLength"
@test sum(skipmissing(df.z)) ≈ 1022.3  atol = 1e-4
select!(df, Not(:z))
d"gen :z = 0.0"
d"replace :z = :SepalWidth + :PetalLength if :PetalWidth > 1.8"
@test sum(skipmissing(df.z)) ≈ 295.9  atol = 1e-4
d"replace :z = _n"

# `reshape` **********************************************************************

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)

# with one variable
df.id = collect(1:(size(df,1)))
select!(df,Not([:PetalLength,:PetalWidth]))
d"reshape_long :Sepal , i(:id) j(:Var)"
@test :Var ∈ propertynames(df)
@test :Sepal ∈ propertynames(df)
@test size(df,1) == 300
d"reshape_wide :Sepal , i(:id) j(:Var)"
@test :SepalLength ∈ propertynames(df)
@test :SepalWidth ∈ propertynames(df)
@test size(df,1) == 150
df2 = dataset("datasets", "iris")
for v in [:SepalLength, :SepalWidth]
    @test all(df[!,v] .== df2[!,v])
end

# with two variables
df = dataset("datasets", "iris")
Douglass.set_active_df(:df)
df.id = collect(1:(size(df,1)))

d"reshape_long :Sepal :Petal, i(:id) j(:Var)"
@test :Var ∈ propertynames(df)
@test :Sepal ∈ propertynames(df)
@test :Petal ∈ propertynames(df)
@test size(df,1) == 300
d"reshape_wide :Sepal :Petal, i(:id) j(:Var)"
@test :SepalLength ∈ propertynames(df)
@test :SepalWidth ∈ propertynames(df)
@test :PetalLength ∈ propertynames(df)
@test :PetalWidth ∈ propertynames(df)
@test size(df,1) == 150
df2 = dataset("datasets", "iris")
for v in [:SepalLength, :SepalWidth, :PetalLength, :PetalWidth]
    @test all(df[!,v] .== df2[!,v])
end

# with two index variables:
wide = DataFrame(x = 1:12,
       a  = 2:13,
       b  = 3:14,
       val1  = randn(12),
       val2  = randn(12),
       cname = repeat(["c", "d"], inner =6)
       )
Douglass.set_active_df(:wide)
d"reshape_wide :val1 :val2, i(:x :a :b) j(:cname)"
@test [:x,:a,:b,:val1c,:val1d,:val2c,:val2d] ⊆ propertynames(wide)
d"reshape_long :val1 :val2, i(:x :a :b) j(:cname)"
@test [:x,:a,:b,:cname,:val1,:val2] ⊆ propertynames(wide)

# `sort` *************************************************************************

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)

@test df.SepalLength[1] ≈ 5.1 atol = 1e-4
d"sort :SepalLength"
@test df.SepalLength[1] ≈ 4.3 atol = 1e-4
d"sort :PetalLength :SepalLength"
@test df.SepalLength[1] ≈ 4.6 atol = 1e-4

# `duplicates_drop` *************************************************************************

using Douglass
df = dataset("datasets", "iris")
Douglass.set_active_df(:df)

d"duplicates_drop :SepalLength :SepalWidth"
@test size(df,1) == size(unique([(df.SepalLength[i],df.SepalWidth[i]) for i=1:size(df,1)]),1)
people = DataFrame(ID = [20, 20, 40], Name = ["John Doe", "John Doe", "Jane Doe"])
Douglass.set_active_df(:people)
d"duplicates_drop"
@test size(people,1) == 2

# Pseudo-String Interpolation *******************

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)

name_of_new_var = "myname"
d"gen :$$(name_of_new_var) = 1.0"
@test :myname ∈ propertynames(df)
@test df[1,:myname] == 1.0

# Multiline expressions *******************

df = dataset("datasets", "iris")
Douglass.set_active_df(:df)

d"""
gen :x = 5
gen :y = 6
"""
@test [:x,:y] ⊆ propertynames(df)

# *******************

# cheatsheet
macroexpand(Main, :(@d_str("rename :SepalLength :SomethingBetter")))
macroexpand(Main, :(@d_str("bysort :Species (:SepalLength) : gen :z = 1.0 if :SepalLength .< 1000.0")))

# # Examples from the Readme.md

# using Douglass, RDatasets
# df = dataset("datasets", "iris")
# # set the active DataFrame
# Douglass.set_active_df(:df)

# # create a variable `z` that is the sum of `SepalLength` and `SepalWidth`, for each row
# d"gen :z = :SepalLength + :SepalWidth"
# # replace `z` by the row index for the first 10 observations
# d"replace :z = _n if _n <= 10"
# # drop a variable
# d"drop :z"
# # construct the within-group mean for a subset of the observations
# d"bysort :Species : egen :z = mean(:SepalLength) if :SepalWidth .> 3.0"


text:
using DataAndPlotting
using Test

#@show DataAndPlotting.initialize()
arr=initialize()
filename="array.h5"
rm(filename, force=true)
output(arr,filename)
arr2=readfile(filename)
@show maximum(abs.(arr2-arr))

@test isequal(arr2,arr)

#myplot(arr,"array.png")

ext: jl

max_stars_repo_name: laurasberna/DataAndPlotting

max_issues_repo_name: laurasberna/DataAndPlotting

max_forks_repo_name: laurasberna/DataAndPlotting


text:
#
# Protein - TMAO (compare new and old implementations)
#
# Run with: julia -t 2 --track-allocation=user

using Profile

include("../../src/MDDF.jl")
using PDBTools

dir="/home/leandro/Drive/Alunos/Ivan/PCCP_revision"

atoms = PDBTools.readPDB("$dir/6Mnative.pdb")

protein = PDBTools.select(atoms,"protein")
solute = MDDF.Selection(protein,nmols=1 )

water = PDBTools.select(atoms,"water")
solvent = MDDF.Selection(water,natomspermol=3)

options = MDDF.Options(n_random_samples=1,lastframe=1)

trajectory = MDDF.Trajectory("$dir/6Mnative.dcd",solute,solvent)
lcP = MDDF.mddf(trajectory,options)

trajectory = MDDF.Trajectory("$dir/6Mnative.dcd",solute,solvent)

Profile.clear_malloc_data()
lcP = MDDF.mddf(trajectory,options)


text:
using InMemoryDatasets, Random, Test

# From DataFrames.jl

@testset "copy - SubDataset" begin
    ds = Dataset(x=1:10, y=1.0:10.0)
    sds = view(ds, 1:2, 1:1)
    @test sds isa SubDataset
    @test copy(sds) isa Dataset
    @test sds == copy(sds)
    @test view(sds, :, :) === sds
    @test view(sds, :, r"") == sds
end

@testset "view -- Dataset" begin
    ds = Dataset(x=1:10, y=1.0:10.0)
    @test view(ds, 1, :) == IMD.DatasetRow(ds, 1, :)
    @test view(ds, UInt(1), :) == IMD.DatasetRow(ds, 1, :)
    @test view(ds, BigInt(1), :) == IMD.DatasetRow(ds, 1, :)
    @test view(ds, UInt(1):UInt(1), :) == SubDataset(ds, 1:1, :)
    @test view(ds, BigInt(1):BigInt(1), :) == SubDataset(ds, 1:1, :)
    @test view(ds, 1:2, :) == first(ds, 2)
    @test view(ds, vcat(trues(2), falses(8)), :) == first(ds, 2)
    @test view(ds, [1, 2], :) == first(ds, 2)

    @test view(ds, 1, r"") == IMD.DatasetRow(ds, 1, :)
    @test view(ds, UInt(1), r"") == IMD.DatasetRow(ds, 1, :)
    @test view(ds, BigInt(1), r"") == IMD.DatasetRow(ds, 1, :)
    @test view(ds, UInt(1):UInt(1), r"") == SubDataset(ds, 1:1, :)
    @test view(ds, BigInt(1):BigInt(1), r"") == SubDataset(ds, 1:1, :)
    @test view(ds, 1:2, r"") == first(ds, 2)
    @test view(ds, vcat(trues(2), falses(8)), r"") == first(ds, 2)
    @test view(ds, [1, 2], r"") == first(ds, 2)

    @test view(ds, 1, r"") == IMD.DatasetRow(ds, 1, r"")
    @test view(ds, UInt(1), r"") == IMD.DatasetRow(ds, 1, r"")
    @test view(ds, BigInt(1), r"") == IMD.DatasetRow(ds, 1, r"")
    @test view(ds, UInt(1):UInt(1), r"") == SubDataset(ds, 1:1, r"")
    @test view(ds, BigInt(1):BigInt(1), r"") == SubDataset(ds, 1:1, r"")

    @test view(ds, 1, :x) == view(ds[!, :x], 1)
    @test view(ds, 1, :x) isa SubDatasetColumn
    @test size(view(ds, 1, :x)) == ()
    @test view(ds, 1:2, :x) == ds[!, :x][1:2]
    @test view(ds, 1:2, :x) isa SubDatasetColumn
    @test view(ds, vcat(trues(2), falses(8)), :x) == view(ds[!, :x], vcat(trues(2), falses(8)))
    @test view(ds, [1, 2], :x) == view(ds[!, :x], [1, 2])
    @test view(ds, 1, 1) == view(ds[!, 1], 1)
    @test view(ds, 1, 1) isa SubDatasetColumn
    @test size(view(ds, 1, 1)) == ()
    @test view(ds, 1:2, 1) == ds[!, 1][1:2]
    @test view(ds, 1:2, 1) isa SubDatasetColumn
    @test view(ds, vcat(trues(2), falses(8)), 1) == view(ds[!, 1], vcat(trues(2), falses(8)))
    @test view(ds, [1, 2], 1) == view(ds[!, 1], [1, 2])
    @test view(ds, 1:2, 1) == ds[!, 1][1:2]
    @test view(ds, 1:2, 1) isa SubDatasetColumn

    @test view(ds, 1, [:x, :y]) == IMD.DatasetRow(ds[:, [:x, :y]], 1, :)
    @test view(ds, 1, [:x, :y]) == IMD.DatasetRow(ds, 1, [:x, :y])
    @test view(ds, 1:2, [:x, :y]) == first(ds, 2)
    @test view(ds, vcat(trues(2), falses(8)), [:x, :y]) == first(ds, 2)
    @test view(ds, [1, 2], [:x, :y]) == first(ds, 2)

    @test view(ds, 1, r"[xy]") == IMD.DatasetRow(ds[:, [:x, :y]], 1, :)
    @test view(ds, 1, r"[xy]") == IMD.DatasetRow(ds, 1, [:x, :y])
    @test view(ds, 1:2, r"[xy]") == first(ds, 2)
    @test view(ds, vcat(trues(2), falses(8)), r"[xy]") == first(ds, 2)
    @test view(ds, [1, 2], r"[xy]") == first(ds, 2)

    @test view(ds, 1, [1, 2]) == IMD.DatasetRow(ds[:, 1:2], 1, :)
    @test view(ds, 1, [1, 2]) == IMD.DatasetRow(ds, 1, 1:2)
    @test view(ds, 1:2, [1, 2]) == first(ds, 2)
    @test view(ds, vcat(trues(2), falses(8)), [1, 2]) == first(ds, 2)
    @test view(ds, [1, 2], [1, 2]) == first(ds, 2)

    @test view(ds, 1, trues(2)) == IMD.DatasetRow(ds[:, trues(2)], 1, :)
    @test view(ds, 1, trues(2)) == IMD.DatasetRow(ds, 1, trues(2))
    @test view(ds, 1:2, trues(2)) == first(ds, 2)
    @test view(ds, vcat(trues(2), falses(8)), trues(2)) == first(ds, 2)
    @test view(ds, [1, 2], trues(2)) == first(ds, 2)

    @test view(ds, Integer[1, 2], :) == first(ds, 2)
    @test view(ds, UInt[1, 2], :) == first(ds, 2)
    @test view(ds, BigInt[1, 2], :) == first(ds, 2)
    @test view(ds, Union{Int, Missing}[1, 2], :) == first(ds, 2)
    @test view(ds, Union{Integer, Missing}[1, 2], :) == first(ds, 2)
    @test view(ds, Union{UInt, Missing}[1, 2], :) == first(ds, 2)
    @test view(ds, Union{BigInt, Missing}[1, 2], :) == first(ds, 2)

    @test view(ds, :, :) == ds
    @test view(ds, 1, :) == IMD.DatasetRow(ds, 1, :)
    @test view(ds, :, 1) == ds[:, 1]
    @test view(ds, :, 1) isa SubDatasetColumn

    @test view(ds, 1, r"") == IMD.DatasetRow(ds, 1, :)
    @test view(ds, :, 1) == ds[:, 1]
    @test view(ds, :, 1) isa SubDatasetColumn

    @test size(view(ds, :, r"a")) == (0, 0)
    @test size(view(ds, 1:2, r"a")) == (0, 0)
    @test size(view(ds, 2, r"a")) == (0,)

    @test_throws ArgumentError view(ds, :, [missing, 1])
    @test_throws ArgumentError view(ds, [missing, 1], :)
    @test_throws ArgumentError view(ds, [missing, 1], r"")
end

@testset "view -- SubDataset" begin
    ds = view(Dataset(x=1:10, y=1.0:10.0), 1:10, :)

    @test view(ds, 1, :) == IMD.DatasetRow(ds, 1, :)
    @test view(ds, UInt(1), :) == IMD.DatasetRow(ds, 1, :)
    @test view(ds, BigInt(1), :) == IMD.DatasetRow(ds, 1, :)
    @test view(ds, 1:2, :) == first(ds, 2)
    @test view(ds, vcat(trues(2), falses(8)), :) == first(ds, 2)
    @test view(ds, [1, 2], :) == first(ds, 2)

    @test view(ds, 1, r"") == IMD.DatasetRow(ds, 1, :)
    @test view(ds, UInt(1), r"") == IMD.DatasetRow(ds, 1, :)
    @test view(ds, BigInt(1), r"") == IMD.DatasetRow(ds, 1, :)
    @test view(ds, 1:2, r"") == first(ds, 2)
    @test view(ds, vcat(trues(2), falses(8)), r"") == first(ds, 2)
    @test view(ds, [1, 2], r"") == first(ds, 2)

    @test view(ds, 1, r"") == IMD.DatasetRow(ds, 1, r"")
    @test view(ds, UInt(1), r"") == IMD.DatasetRow(ds, 1, r"")
    @test view(ds, BigInt(1), r"") == IMD.DatasetRow(ds, 1, r"")

    @test view(ds, 1, :x) == view(ds[!, :x], 1)
    @test view(ds, 1, 1) isa SubDatasetColumn
    @test size(view(ds, 1, 1)) == ()
    @test view(ds, 1:2, :x) == view(ds[!, :x], 1:2)
    @test view(ds, vcat(trues(2), falses(8)), :x) == view(ds[!, :x], vcat(trues(2), falses(8)))
    @test view(ds, [1, 2], :x) == view(ds[!, :x], [1, 2])

    @test view(ds, 1, 1) == view(ds[!, 1], 1)
    @test view(ds, 1, 1) isa SubDatasetColumn
    @test size(view(ds, 1, 1)) == ()
    @test view(ds, 1:2, 1) == view(ds[!, :x], 1:2)
    @test view(ds, vcat(trues(2), falses(8)), 1) == view(ds[!, :x], vcat(trues(2), falses(8)))
    @test view(ds, [1, 2], 1) == view(ds[!, :x], [1, 2])

    @test view(ds, 1, [:x, :y]) == IMD.DatasetRow(ds[:, [:x, :y]], 1, :)
    @test view(ds, 1, [:x, :y]) == IMD.DatasetRow(ds, 1, [:x, :y])
    @test view(ds, 1:2, [:x, :y]) == first(ds, 2)

    @test view(ds, 1, r"[xy]") == IMD.DatasetRow(ds[:, [:x, :y]], 1, :)
    @test view(ds, 1, r"[xy]") == IMD.DatasetRow(ds, 1, [:x, :y])
    @test view(ds, 1:2, r"[xy]") == first(ds, 2)

    @test view(ds, vcat(trues(2), falses(8)), [:x, :y]) == first(ds, 2)
    @test view(ds, [1, 2], [:x, :y]) == first(ds, 2)
    @test view(ds, 1, [1, 2]) == IMD.DatasetRow(ds[:, 1:2], 1, :)
    @test view(ds, 1, [1, 2]) == IMD.DatasetRow(ds, 1, 1:2)
    @test view(ds, 1:2, [1, 2]) == first(ds, 2)
    @test view(ds, vcat(trues(2), falses(8)), [1, 2]) == first(ds, 2)
    @test view(ds, [1, 2], [1, 2]) == first(ds, 2)
    @test view(ds, 1, trues(2)) == IMD.DatasetRow(ds[:, trues(2)], 1, :)
    @test view(ds, 1, trues(2)) == IMD.DatasetRow(ds, 1, trues(2))
    @test view(ds, 1:2, trues(2)) == first(ds, 2)
    @test view(ds, vcat(trues(2), falses(8)), trues(2)) == first(ds, 2)
    @test view(ds, [1, 2], trues(2)) == first(ds, 2)
    @test view(ds, Integer[1, 2], :) == first(ds, 2)
    @test view(ds, UInt[1, 2], :) == first(ds, 2)
    @test view(ds, BigInt[1, 2], :) == first(ds, 2)
    @test view(ds, Union{Int, Missing}[1, 2], :) == first(ds, 2)
    @test view(ds, Union{Integer, Missing}[1, 2], :) == first(ds, 2)
    @test view(ds, Union{UInt, Missing}[1, 2], :) == first(ds, 2)
    @test view(ds, Union{BigInt, Missing}[1, 2], :) == first(ds, 2)

    @test view(ds, :, :) == ds
    @test view(ds, 1, :) == IMD.DatasetRow(ds, 1, :)
    @test view(ds, :, 1) == ds[:, 1]
    @test view(ds, :, 1) isa SubDatasetColumn

    @test view(ds, :, r"") == ds
    @test view(ds, 1, r"") == IMD.DatasetRow(ds, 1, :)

    @test_throws ArgumentError view(ds, :, [missing, 1])
    @test_throws ArgumentError view(ds, [missing, 1], :)
    @test_throws ArgumentError view(ds, [missing, 1], r"")
    @test_throws ArgumentError view(ds, :, true)
end

@testset "getproperty, setproperty! and propertynames" begin
    x = collect(1:10)
    y = collect(1.0:10.0)
    ds = view(Dataset(:x=>x, :y=>y), 2:6, :)

    @test propertynames(ds) == Symbol.(names(ds))

    @test ds.x == 2:6
    @test ds.y == 2:6
    @test_throws ArgumentError ds.z

    ds[:, :x] = 1:5
    @test ds.x == 1:5
    @test parent(ds)[:, :x] == [1; 1:5; 7:10]
    ds[:, :y] .= 1
    @test ds.y == [1, 1, 1, 1, 1]
    @test parent(ds)[:, :y] == [1; 1; 1; 1; 1; 1; 7:10]
end

@testset "index" begin
    y = 1.0:10.0
    ds = view(Dataset(y=y), 2:6, :)
    ds2 = view(Dataset(x=y, y=y), 2:6, 2:2)
    @test IMD.index(ds) == IMD.index(ds2)
    @test haskey(IMD.index(ds2), :y)
    @test !haskey(IMD.index(ds2), :x)
    @test haskey(IMD.index(ds2), 1)
    @test !haskey(IMD.index(ds2), 2)
    @test !haskey(IMD.index(ds2), 0)
    @test_throws ArgumentError haskey(IMD.index(ds2), true)
    @test names(IMD.index(ds2)) == ["y"]
    @test IMD._names(IMD.index(ds2)) == [:y]

    x = Dataset(ones(5, 4), :auto)
    ds = view(x, 2:3, 2:3)
    @test names(ds) == names(x)[2:3]
    ds = view(x, 2:3, [4, 2])
    @test names(ds) == names(x)[[4, 2]]
end

@testset "deleteat!" begin
    y = 1.0:10.0
    ds = view(Dataset(y=y), 2:6, :)
    @test_throws ArgumentError deleteat!(ds, 1)
end

@testset "parent" begin
    ds = Dataset(a=Union{Int, Missing}[1, 2, 3, 1, 2, 2],
                   b=[2.0, missing, 1.2, 2.0, missing, missing],
                   c=["A", "B", "C", "A", "B", missing])
    @test parent(view(ds, [4, 2], :)) === ds
    @test parent(view(ds, [4, 2], r"")) === ds
    @test parentindices(view(ds, [4, 2], :)) == ([4, 2], Base.OneTo(3))
    @test parentindices(view(ds, [4, 2], r"")) == ([4, 2], [1, 2, 3])
    @test parent(view(ds, [4, 2], 1:3)) === ds
    @test parentindices(view(ds, [4, 2], 1:3)) == ([4, 2], Base.OneTo(3))
end

@testset "duplicate column" begin
    ds = Dataset([11:16 21:26 31:36 41:46], :auto)
    @test_throws ArgumentError view(ds, [3, 1, 4], [3, 3, 3])
end

@testset "conversion to Dataset" begin
    ds = Dataset([11:16 21:26 31:36 41:46], :auto)
    sds = view(ds, [3, 1, 4], [3, 2, 1])
    ds2 = Dataset(sds)
    @test ds2 isa Dataset
    @test ds2 == ds[[3, 1, 4], [3, 2, 1]]
    @test all(x -> IMD.__!(x) isa Vector{Union{Missing, Int}}, eachcol(ds2))
    ds2 = convert(Dataset, sds)
    @test ds2 isa Dataset
    @test ds2 == ds[[3, 1, 4], [3, 2, 1]]
    @test all(x -> IMD.__!(x) isa Vector{Union{Missing, Int}}, eachcol(ds2))

    ds = Dataset(x=1:4, y=11:14, z=21:24)
    sds = @view ds[2:3, [2]]
    ds2 = Dataset(sds)
    @test size(ds2) == (2, 1)
    @test ds2.y.val isa Vector{Union{Missing, Int}}
    @test ds2.y == [12, 13]

end

@testset "setindex! in view" begin
    ds = Dataset(A=Vector{Union{Int, Missing}}(1:4), B=Union{String, Missing}["M", "F", "F", "M"])

    s1 = view(ds, 1:3, :)
    s1[2, :A] = 4
    @test ds[2, :A] == 4
    @test view(s1, 1:2, :) == view(ds, 1:2, :)

    s2 = view(ds, 1:2:3, :)
    s2[2, :B] = "M"
    @test ds[3, :B] == "M"
    @test view(s2, 1:1:2, :) == view(ds, [1, 3], :)
end

@testset "SubDataset corner cases" begin
    ds = Dataset(a=[1, 2], b=[3, 4])
    @test_throws ArgumentError SubDataset(ds, Integer[true], 1)
    @test_throws ArgumentError SubDataset(ds, [true], 1)

    sds = @view ds[:, :]
    @test_throws ArgumentError SubDataset(sds, true, 1)
    @test_throws ArgumentError SubDataset(sds, true, :)
    @test_throws ArgumentError SubDataset(sds, Integer[true], 1)

    if VERSION >= v"1.7.0-DEV"
        @test_throws ArgumentError SubDataset(sds, Integer[true, true, true], :)
    else
        @test SubDataset(sds, Integer[true, true, true], :) ==
              SubDataset(sds, [1, 1, 1], :)
    end
end


text:
# function gen_helper_ex(::Type{OrderbyHelper}, ex)::Expr
#     arg_parameters = Set{Symbol}()
#     f_ex, arg_fields = build_kernel_ex!(ex, arg_parameters)
#     return Expr(:call, OrderbyHelper, f_ex, arg_fields)
# end


text:
#################### CSR

function _mul(A::SparseCSR{Tv,Ti}, x::Vector{Tv})::Vector{Tv} where {Tv, Ti}
    m, n = size(A)
    y = zeros(m)
    for i = 1:m
        for z = A.rowptr[i]:A.rowptr[i+1]-1
            j = A.colind[z]
            y[i] += A.val[z] * x[j]
        end
    end
    return y
end

function _mul(At::Adjoint{SparseCSR{Tv,Ti}}, x::Vector{Tv})::Vector{Tv} where {Tv, Ti}
    A = At.parent
    n, m = size(A)
    y = zeros(m)
    for j = 1:n
        for z = A.rowptr[j]:A.rowptr[j+1]-1
            i = A.colind[z]
            y[i] += A.val[z] * x[j]
        end
    end
    return y
end

###

function _mul(A::SparseCSR{Tv,Ti}, B::Matrix{Tv})::Matrix{Tv} where {Tv, Ti}
    m, k = size(A)
    k, n = size(B)
    C = zeros(m,n)
    for i = 1:m
        for z = A.rowptr[i]:A.rowptr[i+1]-1
            l = A.colind[z]
            for j = 1:n
                C[i,j] += A.val[z] * B[l,j]
            end
        end
    end
    return C
end

function _mul(A::SparseCSR{Tv,Ti}, Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}})::Matrix{Tv} where {Tv, Ti}
    B = Bt.parent
    m, k = size(A)
    n, k = size(B)
    C = zeros(m,n)
    for i = 1:m
        for z = A.rowptr[i]:A.rowptr[i+1]-1
            l = A.colind[z]
            for j = 1:n
                C[i,j] += A.val[z] * B[j,l]
            end
        end
    end
    return C
end

function _mul(At::Adjoint{SparseCSR{Tv,Ti}}, B::Matrix{Tv})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    k, m = size(A)
    k, n = size(B)
    C = zeros(m,n)
    for l = 1:k
        for z = A.rowptr[l]:A.rowptr[l+1]-1
            i = A.colind[z]
            for j = 1:n
                C[i,j] += A.val[z] * B[l,j]
            end
        end
    end
    return C
end

function _mul(At::Adjoint{SparseCSR{Tv,Ti}}, Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    B = Bt.parent
    k, m = size(A)
    n, k = size(B)
    C = zeros(m,n)
    for l = 1:k
        for z = A.rowptr[l]:A.rowptr[l+1]-1
            i = A.colind[z]
            for j = 1:n
                C[i,j] += A.val[z] * B[j,l]
            end
        end
    end
    return C
end

###

function _mul(B::Matrix{Tv}, A::SparseCSR{Tv,Ti})::Matrix{Tv} where {Tv, Ti}
    m, k = size(B)
    k, n = size(A)
    C = zeros(m,n)
    for l = 1:k
        for z = A.rowptr[l]:A.rowptr[l+1]-1
            j = A.colind[z]
            for i = 1:m
                C[i,j] += A.val[z] * B[i,l]
            end
        end
    end
    return C
end

function _mul(Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, A::SparseCSR{Tv,Ti})::Matrix{Tv} where {Tv, Ti}
    B = Bt.parent
    k, m = size(B)
    k, n = size(A)
    C = zeros(m,n)
    for l = 1:k
        for z = A.rowptr[l]:A.rowptr[l+1]-1
            j = A.colind[z]
            for i = 1:m
                C[i,j] += A.val[z] * B[l,i]
            end
        end
    end
    return C
end

function _mul(B::Matrix{Tv}, At::Adjoint{SparseCSR{Tv,Ti}})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    m, k = size(B)
    n, k = size(A)
    C = zeros(m,n)
    for j = 1:n
        for z = A.rowptr[j]:A.rowptr[j+1]-1
            l = A.colind[z]
            for i = 1:m
                C[i,j] += A.val[z] * B[i,l]
            end
        end
    end
    return C
end

function _mul(Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, At::Adjoint{SparseCSR{Tv,Ti}})::Matrix{Tv} where {Tv, Ti}
    B = Bt.parent
    A = At.parent
    k, m = size(B)
    n, k = size(A)
    C = zeros(m,n)
    for j = 1:n
        for z = A.rowptr[j]:A.rowptr[j+1]-1
            l = A.colind[z]
            for i = 1:m
                C[i,j] += A.val[z] * B[l,i]
            end
        end
    end
    return C
end

#################### CSC

function _mul(A::SparseCSC{Tv,Ti}, x::Vector{Tv})::Vector{Tv} where {Tv, Ti}
    m, n = size(A)
    y = zeros(m)
    for j = 1:n
        for z = A.colptr[j]:A.colptr[j+1]-1
            i = A.rowind[z]
            y[i] += A.val[z] * x[j]
        end
    end
    return y
end

function _mul(At::Adjoint{SparseCSC{Tv,Ti}}, x::Vector{Tv})::Vector{Tv} where {Tv, Ti}
    A = At.parent
    n, m = size(A)
    y = zeros(m)
    for i = 1:m
        for z = A.colptr[i]:A.colptr[i+1]-1
            j = A.rowind[z]
            y[i] += A.val[z] * x[j]
        end
    end
    return y
end

###

function _mul(A::SparseCSC{Tv,Ti}, B::Matrix{Tv})::Matrix{Tv} where {Tv, Ti}
    m, k = size(A)
    k, n = size(B)
    C = zeros(m,n)
    for l = 1:k
        for z = A.colptr[l]:A.colptr[l+1]-1
            i = A.rowind[z]
            for j = 1:n
                C[i,j] += A.val[z] * B[l,j]
            end
        end
    end
    return C
end

function _mul(A::SparseCSC{Tv,Ti}, Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}})::Matrix{Tv} where {Tv, Ti}
    B = Bt.parent
    m, k = size(A)
    n, k = size(B)
    C = zeros(m,n)
    for l = 1:k
        for z = A.colptr[l]:A.colptr[l+1]-1
            i = A.rowind[z]
            for j = 1:n
                C[i,j] += A.val[z] * B[j,l]
            end
        end
    end
    return C
end

function _mul(At::Adjoint{SparseCSC{Tv,Ti}}, B::Matrix{Tv})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    k, m = size(A)
    k, n = size(B)
    C = zeros(m,n)
    for i = 1:m
        for z = A.colptr[i]:A.colptr[i+1]-1
            l = A.rowind[z]
            for j = 1:n
                C[i,j] += A.val[z] * B[l,j]
            end
        end
    end
    return C
end

function _mul(At::Adjoint{SparseCSC{Tv,Ti}}, Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    B = Bt.parent
    k, m = size(A)
    n, k = size(B)
    C = zeros(m,n)
    for i = 1:m
        for z = A.colptr[i]:A.colptr[i+1]-1
            l = A.rowind[z]
            for j = 1:n
                C[i,j] += A.val[z] * B[j,l]
            end
        end
    end
    return C
end

###

function _mul(B::Matrix{Tv}, A::SparseCSC{Tv,Ti})::Matrix{Tv} where {Tv, Ti}
    m, k = size(B)
    k, n = size(A)
    C = zeros(m,n)
    for j = 1:n
        for z = A.colptr[j]:A.colptr[j+1]-1
            l = A.rowind[z]
            for i = 1:m
                C[i,j] += A.val[z] * B[i,l]
            end
        end
    end
    return C
end

function _mul(Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, A::SparseCSC{Tv,Ti})::Matrix{Tv} where {Tv, Ti}
    B = Bt.parent
    k, m = size(B)
    k, n = size(A)
    C = zeros(m,n)
    for j = 1:n
        for z = A.colptr[j]:A.colptr[j+1]-1
            l = A.rowind[z]
            for i = 1:m
                C[i,j] += A.val[z] * B[l,i]
            end
        end
    end
    return C
end

function _mul(B::Matrix{Tv}, At::Adjoint{SparseCSC{Tv,Ti}})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    m, k = size(B)
    n, k = size(A)
    C = zeros(m,n)
    for l = 1:k
        for z = A.colptr[l]:A.colptr[l+1]-1
            j = A.rowind[z]
            for i = 1:m
                C[i,j] += A.val[z] * B[i,l]
            end
        end
    end
    return C
end

function _mul(Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, At::Adjoint{SparseCSC{Tv,Ti}})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    B = Bt.parent
    k, m = size(B)
    n, k = size(A)
    C = zeros(m,n)
    for l = 1:k
        for z = A.colptr[l]:A.colptr[l+1]-1
            j = A.rowind[z]
            for i = 1:m
                C[i,j] += A.val[z] * B[l,i]
            end
        end
    end
    return C
end

#################### COO

function _mul(A::SparseCOO{Tv,Ti}, x::Vector{Tv})::Vector{Tv} where {Tv, Ti}
    m, n = size(A)
    y = zeros(m)
    for z = 1:SparseArrays.nnz(A)
        i = A.rowind[z]
        j = A.colind[z]
        y[i] += A.val[z] * x[j]
    end
    return y
end

function _mul(At::Adjoint{SparseCOO{Tv,Ti}}, x::Vector{Tv})::Vector{Tv} where {Tv, Ti}
    A = At.parent
    n, m = size(A)
    y = zeros(m)
    for z = 1:SparseArrays.nnz(A)
        j = A.rowind[z]
        i = A.colind[z]
        y[i] += A.val[z] * x[j]
    end
    return y
end

###

function _mul(A::SparseCOO{Tv,Ti}, B::Matrix{Tv})::Matrix{Tv} where {Tv, Ti}
    m, k = size(A)
    k, n = size(B)
    C = zeros(m,n)
    for z = 1:SparseArrays.nnz(A)
        i = A.rowind[z]
        l = A.colind[z]
        for j = 1:n
            C[i,j] += A.val[z] * B[l,j]
        end
    end
    return C
end

function _mul(A::SparseCOO{Tv,Ti}, Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}})::Matrix{Tv} where {Tv, Ti}
    B = Bt.parent
    m, k = size(A)
    n, k = size(B)
    C = zeros(m,n)
    for z = 1:SparseArrays.nnz(A)
        i = A.rowind[z]
        l = A.colind[z]
        for j = 1:n
            C[i,j] += A.val[z] * B[j,l]
        end
    end
    return C
end

function _mul(At::Adjoint{SparseCOO{Tv,Ti}}, B::Matrix{Tv})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    k, m = size(A)
    k, n = size(B)
    C = zeros(m,n)
    for z = 1:SparseArrays.nnz(A)
        l = A.rowind[z]
        i = A.colind[z]
        for j = 1:n
            C[i,j] += A.val[z] * B[l,j]
        end
    end
    return C
end

function _mul(At::Adjoint{SparseCOO{Tv,Ti}}, Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    B = Bt.parent
    k, m = size(A)
    n, k = size(B)
    C = zeros(m,n)
    for z = 1:SparseArrays.nnz(A)
        l = A.rowind[z]
        i = A.colind[z]
        for j = 1:n
            C[i,j] += A.val[z] * B[j,l]
        end
    end
    return C
end

###

function _mul(B::Matrix{Tv}, A::SparseCOO{Tv,Ti})::Matrix{Tv} where {Tv, Ti}
    m, k = size(B)
    k, n = size(A)
    C = zeros(m,n)
    for z = 1:SparseArrays.nnz(A)
        l = A.rowind[z]
        j = A.colind[z]
        for i = 1:m
            C[i,j] += A.val[z] * B[i,l]
        end
    end
    return C
end

function _mul(Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, A::SparseCOO{Tv,Ti})::Matrix{Tv} where {Tv, Ti}
    B = Bt.parent
    k, m = size(B)
    k, n = size(A)
    C = zeros(m,n)
    for z = 1:SparseArrays.nnz(A)
        l = A.rowind[z]
        j = A.colind[z]
        for i = 1:m
            C[i,j] += A.val[z] * B[l,i]
        end
    end
    return C
end

function _mul(B::Matrix{Tv}, At::Adjoint{SparseCOO{Tv,Ti}})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    m, k = size(B)
    n, k = size(A)
    C = zeros(m,n)
    for z = 1:SparseArrays.nnz(A)
        j = A.rowind[z]
        l = A.colind[z]
        for i = 1:m
            C[i,j] += A.val[z] * B[i,l]
        end
    end
    return C
end

function _mul(Bt::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, At::Adjoint{SparseCOO{Tv,Ti}})::Matrix{Tv} where {Tv, Ti}
    A = At.parent
    B = Bt.parent
    k, m = size(B)
    n, k = size(A)
    C = zeros(m,n)
    for z = 1:SparseArrays.nnz(A)
        j = A.rowind[z]
        l = A.colind[z]
        for i = 1:m
            C[i,j] += A.val[z] * B[l,i]
        end
    end
    return C
end

######## override

function Base.:*(x::Tv, A::AbstractSparseM{Tv,Ti}) where {Tv<:Number,Ti}
    B = copy(A)
    B.val .*= x
    return B
end

function Base.:*(A::AbstractSparseM{Tv,Ti}, x::Tv) where {Tv<:Number,Ti}
    B = copy(A)
    B.val .*= x
    return B
end

function Base.:/(A::AbstractSparseM{Tv,Ti}, x::Tv) where {Tv<:Number,Ti}
    B = copy(A)
    B.val ./= x
    return B
end

(Base.:*)(A::SparseCSR{Tv,Ti}, x::Vector{Tv}) where {Tv,Ti} = _mul(A, x)
(Base.:*)(A::Adjoint{SparseCSR{Tv,Ti}}, x::Vector{Tv}) where {Tv,Ti} = _mul(A, x)

(Base.:*)(A::SparseCSR{Tv,Ti}, B::Matrix{Tv}) where {Tv,Ti} = _mul(A, B)
(Base.:*)(A::SparseCSR{Tv,Ti}, B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}) where {Tv,Ti} = _mul(A, B)
(Base.:*)(A::Adjoint{SparseCSR{Tv,Ti}}, B::Matrix{Tv}) where {Tv,Ti} = _mul(A, B)
(Base.:*)(A::Adjoint{SparseCSR{Tv,Ti}}, B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}) where {Tv,Ti} = _mul(A, B)

(Base.:*)(B::Matrix{Tv}, A::SparseCSR{Tv,Ti}) where {Tv,Ti} = _mul(B, A)
(Base.:*)(B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, A::SparseCSR{Tv,Ti}) where {Tv,Ti} = _mul(B, A)
(Base.:*)(B::Matrix{Tv}, A::Adjoint{SparseCSR{Tv,Ti}}) where {Tv,Ti} = _mul(B, A)
(Base.:*)(B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, A::Adjoint{SparseCSR{Tv,Ti}}) where {Tv,Ti} = _mul(B, A)

###

(Base.:*)(A::SparseCSC{Tv,Ti}, x::Vector{Tv}) where {Tv,Ti} = _mul(A, x)
(Base.:*)(A::Adjoint{SparseCSC{Tv,Ti}}, x::Vector{Tv}) where {Tv,Ti} = _mul(A, x)

(Base.:*)(A::SparseCSC{Tv,Ti}, B::Matrix{Tv}) where {Tv,Ti} = _mul(A, B)
(Base.:*)(A::SparseCSC{Tv,Ti}, B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}) where {Tv,Ti} = _mul(A, B)
(Base.:*)(A::Adjoint{SparseCSC{Tv,Ti}}, B::Matrix{Tv}) where {Tv,Ti} = _mul(A, B)
(Base.:*)(A::Adjoint{SparseCSC{Tv,Ti}}, B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}) where {Tv,Ti} = _mul(A, B)

(Base.:*)(B::Matrix{Tv}, A::SparseCSC{Tv,Ti}) where {Tv,Ti} = _mul(B, A)
(Base.:*)(B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, A::SparseCSC{Tv,Ti}) where {Tv,Ti} = _mul(B, A)
(Base.:*)(B::Matrix{Tv}, A::Adjoint{SparseCSC{Tv,Ti}}) where {Tv,Ti} = _mul(B, A)
(Base.:*)(B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, A::Adjoint{SparseCSC{Tv,Ti}}) where {Tv,Ti} = _mul(B, A)

###

(Base.:*)(A::SparseCOO{Tv,Ti}, x::Vector{Tv}) where {Tv,Ti} = _mul(A, x)
(Base.:*)(A::Adjoint{SparseCOO{Tv,Ti}}, x::Vector{Tv}) where {Tv,Ti} = _mul(A, x)

(Base.:*)(A::SparseCOO{Tv,Ti}, B::Matrix{Tv}) where {Tv,Ti} = _mul(A, B)
(Base.:*)(A::SparseCOO{Tv,Ti}, B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}) where {Tv,Ti} = _mul(A, B)
(Base.:*)(A::Adjoint{SparseCOO{Tv,Ti}}, B::Matrix{Tv}) where {Tv,Ti} = _mul(A, B)
(Base.:*)(A::Adjoint{SparseCOO{Tv,Ti}}, B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}) where {Tv,Ti} = _mul(A, B)

(Base.:*)(B::Matrix{Tv}, A::SparseCOO{Tv,Ti}) where {Tv,Ti} = _mul(B, A)
(Base.:*)(B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, A::SparseCOO{Tv,Ti}) where {Tv,Ti} = _mul(B, A)
(Base.:*)(B::Matrix{Tv}, A::Adjoint{SparseCOO{Tv,Ti}}) where {Tv,Ti} = _mul(B, A)
(Base.:*)(B::LinearAlgebra.Adjoint{Tv,Matrix{Tv}}, A::Adjoint{SparseCOO{Tv,Ti}}) where {Tv,Ti} = _mul(B, A)


text:
#!/usr/bin/env julia

# Calculates some limits of the problem just using the dataset and parameters in the config file.
# * the largest baseline in u and v, separately.
# * the mean velocity of the dataset.
# * the physical scale of the image, and whether it fully encapsulates the model grid for
#   all of the possible distances to explore.

using ArgParse

s = ArgParseSettings()
@add_arg_table s begin
    "--config"
    help = "a YAML configuration file"
    default = "config.yaml"
end

# Load the file
parsed_args = parse_args(ARGS, s)

import YAML
config = YAML.load(open(parsed_args["config"]))

using DiskJockey.visibilities
using DiskJockey.constants
using DiskJockey.model

species = config["species"]
transition = config["transition"]
lam0 = lam0s[species*transition]
grid = Grid(config["grid"])

function wl_to_vel{T}(wl::T)
    return c_kms * (wl - lam0)/lam0
end

function vel_to_wl{T}(vel::T)
    beta = vel/c_kms # relativistic Doppler formula
    return lam0 * sqrt((1. - beta) / (1. + beta)) # [microns]
end

dvarr = DataVis(config["data_file"])

lam_start = dvarr[1].lam
lam_end = dvarr[end].lam

vmin = wl_to_vel(lam_start)
vmax = wl_to_vel(lam_end)
println("Dataset channels are velocities from $vmin to $vmax and span ", vmax - vmin, " km/s.")
println("Midpoint is ", (vmax + vmin)/2, " km/s.")

max_base = max_baseline(dvarr)

println("Max baseline ", max_base, " kilolambda")

# Given the size of the image in arcseconds and the number of pixels, figure out the nyquist
# sampling and whether or not this is sufficiently higher than our largest baseline
dRA = config["size_arcsec"] / config["npix"] # [arcsec/pix]

# Convert this to dRA or dDEC
dRA_max = 1/(nyquist_factor * max_base * 1e3) / arcsec # [arcsec / pix]

@assert dRA < dRA_max "Nyquist sampling not satisfied: Either increase the number of pixels in your image, or decrease the angular size of your image. dRA ($dRA [arcsec/pix]) is greater than dRA_max ($dRA_max [arcsec/pix]), which is determined from the maximum baseline of your dataset."

println("Nyquist sampling satisfied. dRA: $dRA [arcsec/pix] ; dRA_max: $dRA_max [arcsec/pix]")

# Given the angular size of the image, calculate what the physical size of the image would be
# at the closest source distance, and make sure that this is still larger than outer radius of
# the model grid.

mu_d = config["dpc_prior"]["mu"]
sig_d = config["dpc_prior"]["sig"]

dlow = mu_d - 3. * sig_d # [AU]

# Calculate the physical size of the image at the closer distance, and make sure it's still larger
# than 110% of the outer grid radius
sizeau = 0.5 * config["size_arcsec"] * dlow # [AU]
outer_radius = 1.1 * config["grid"]["r_out"]
@assert sizeau > outer_radius "The angular size of your image is too small to fully encapsulate the model grid at the closest distances to the source. Increase the angular size of your image via `size_arcsec` or decrease the outer radius of your model grid. Size at the closest distances $sizeau [AU]; outer radius of the grid + 10% $outer_radius [AU]"

println("Image size satisfied. Half-Image size at the closest distances: $sizeau [AU]; outer radius of the grid + 10%: $outer_radius [AU]")

max_stars_repo_name: elnjensen/DiskJockey

max_issues_repo_name: elnjensen/DiskJockey

max_forks_repo_name: elnjensen/DiskJockey


text:
# Create a maze using the depth-first algorithm described at
# https://scipython.com/blog/making-a-maze/
# Christian Hill, April 2017.

using DataStructures

@enum Direction North South East West

# A cell in the maze.
# A maze "Cell" is a point in the grid which may be surrounded by walls to
# the north, east, south or west.
struct Cell
    x::Int
    y::Int
    walls::Dict

    # Initialize the cell at (x,y). At first it is surrounded by walls.
    function Cell(x, y)
        walls = Dict(North => true, South => true, East => true, West => true)
        new(x, y, walls)
    end
end

# Does this cell still have all its walls?
function has_all_walls(cell::Cell)
    return all(values(cell.walls))
end

# Knock down the wall between cells self and other.
function knock_down_wall(cell::Cell, other::Cell, wall::Direction)

    # A wall separates a pair of cells in the N-S or W-E directions.
    wall_pairs = Dict(North => South, South => North, East => West, West => East)

    cell.walls[wall] = false
    other.walls[wall_pairs[wall]] = false
end

# Return a list of unvisited neighbours to cell
function find_valid_neighbours(maze::Matrix{Cell}, cell::Cell)

    delta = OrderedDict(West => [-1 0], East => [1 0], South => [0 1], North => [0 -1])

    neighbours = []

    nx, ny = size(maze)

    for (direction, (dx, dy)) in delta

        x2, y2 = cell.x + dx, cell.y + dy

        if (1 ≤ x2 ≤ nx) && (1 ≤ y2 ≤ ny)
            neighbour = maze[x2, y2]
            if has_all_walls(neighbour)
                push!(neighbours, (direction, neighbour))
            end
        end
    end

    return neighbours
end

function make_maze(nx::Int, ny::Int, randoms=nothing)

    # Total number of cells.
    n = nx * ny

    maze = [Cell(x, y) for x = 1:nx, y = 1:ny]
    cell_stack = []

    # Total number of visited cells during maze construction.
    nv = 1

    current_cell = maze[1, 1]

    while nv < n
        neighbours = find_valid_neighbours(maze, current_cell)

        if isempty(neighbours)
            # We've reached a dead end: backtrack.
            current_cell = pop!(cell_stack)
            continue
        end

        # Choose a random neighbouring cell and move to it.
        if randoms === nothing
            k = rand(1:length(neighbours))
        else
            k = randoms[nv]
        end

        direction, next_cell = neighbours[k]
        knock_down_wall(current_cell, next_cell, direction)
        push!(cell_stack, current_cell)
        current_cell = next_cell
        nv += 1
    end

    return maze
end

function get_coordinates_maze_cell(cell_x::Int, cell_y::Int, maze_cell_size::Int)

    x_left = maze_cell_size * (cell_x - 1)
    x_right = maze_cell_size * cell_x
    y_top = maze_cell_size * (cell_y - 1)
    y_bottom = maze_cell_size * cell_y

    return x_left, x_right, y_top, y_bottom

end


text:
function createNewMap(widget=nothing)
    button, name = input_dialog("Enter package name for new map", "11-Example", (("Cancel", 0), ("Create Map", 1)), window)

    dialogText = "You might have unsaved changes in your map.\nPlease confirm that you want to create the new map '$name'."
    res = button == 1 && (Ahorn.loadedState.lastSavedHash == hash(Ahorn.loadedState.side) || ask_dialog(dialogText, Ahorn.window))

    if button == 1 && res
        # Clear render cache from last map
        if loadedState.map !== nothing && get(config, "clear_render_cache_on_map_load", true)
            deleteDrawableRoomCache(loadedState.map)
        end

        loadedState.filename = ""
        loadedState.roomName = ""
        loadedState.room = nothing

        loadedState.map = Map(name)
        loadedState.side = Side(loadedState.map, Dict{String, Any}())

        EntityIds.updateValidIds(loadedState.map)

        updateTreeView!(roomList, getTreeData(loadedState.map))
        handleRoomChanged(loadedState.map, loadedState.room)

        GAccessor.title(window, "$baseTitle - $name")

        draw(canvas)
    end
end


text:
using SimJulia

struct TestException <: Exception end

function test_callback_event(ev::Event)
  println("Hi $ev has value $(value(ev))")
end

function test_callback_Timeout(ev::AbstractEvent)
  println("Hi $ev timed out at $(now(environment(ev)))")
end

sim = Simulation()
ev1 = Event(sim)
@callback test_callback_event(ev1)
succeed(ev1, value="Succes")
ev2 = Event(sim)
@callback test_callback_event(ev2)
fail(ev2, TestException())
try
  succeed(ev2)
catch exc
  println("$exc has been thrown!")
end
@callback test_callback_Timeout(timeout(sim, 1))
run(sim)


text:
6.829243291077833e-5
3.434878183968345e-5
0.0008216229330892649
-0.000581988879049661
0.06155840513482077
0.06554710709090356
-0.09866394333901657
0.116947264552445
-0.25878586618325355
0.03177577190964304
0.14412170022133045
0.030454378554912186
0.024718718097048634
-0.3414334420976965
-0.040071052414555476
0.218330958433261
-0.13956620885729176
-0.12911808740763595
-0.09683117732331718
-0.07359701187251337
-0.022446414348095328
0.32761035972021996
0.036733190050246035
0.06785148610066312
-0.09843328991138175
-0.0421606099190659
-0.0371341751516621
0.22929193892003766
-0.4000593695131863
0.00652260929226957
0.06282041174124728
0.03904605046767447
0.14163148255064506
0.18833975652478777
-0.1365359715867163
0.022684456874556766
-0.019144770663276856
-3.985044048692918e-5
-0.009480480395882075
0.09461567515805262
0.08790013212266694
-0.05489329126368636
-0.08732267393192181
-0.002225277782904033
0.07557102031193645
-0.14570704930547385
0.01663193075833319
-0.05542271179121729
-0.11986411718444501
0.21799883385697374
0.003006109097820892
0.06252709509228656

max_stars_repo_name: GiamFbn/MyULTRAFAST

max_issues_repo_name: GiamFbn/MyULTRAFAST

max_forks_repo_name: GiamFbn/MyULTRAFAST


text:
## test_separable.jl : test separable kernels for GRF generation

@testset "separable GRFs           " begin

## 1d Exponential ##
cov = SeparableCovarianceFunction(Exponential(0.1))
pts = range(0,stop = 1,length = 1001)
grf = GaussianRandomField(cov,KarhunenLoeve(1000),pts)
@test isa(grf,GaussianRandomField)
@test isa(grf.cov,SeparableCovarianceFunction)
@test isa(grf.cov.cov,Vector)
@test isa(grf.cov.cov[1],Exponential)
@test ndims(grf.cov) == 1
@test isa(grf,GaussianRandomField{C,KarhunenLoeve{1000}} where {C})
@test length(grf.pts[1]) == length(pts)
@test length(sample(grf)) == length(pts)

## 2d Exponential ##
e1 = Exponential(0.1)
e2 = Exponential(0.01)
cov = SeparableCovarianceFunction(e1,e2)
pts1 = 0:0.05:1
pts2 = 0:0.05:1
grf = GaussianRandomField(cov,KarhunenLoeve(500),pts1,pts2)
@test isa(grf,GaussianRandomField)
@test isa(grf.cov,SeparableCovarianceFunction)
@test isa(grf.cov.cov,Vector)
@test isa(grf.cov.cov[1],Exponential)
@test isa(grf.cov.cov[2],Exponential)
@test ndims(grf.cov) == 2
@test isa(grf,GaussianRandomField{C,KarhunenLoeve{500}} where {C})
@test length(grf.pts) == 2
@test length(grf.pts[1]) == length(pts1)
@test length(grf.pts[2]) == length(pts2)
@test size(sample(grf),1) == length(pts1)
@test size(sample(grf),2) == length(pts2)

## 2d Exponential ##
e1 = Exponential(0.1)
e2 = Exponential(0.01)
cov = SeparableCovarianceFunction(e1,e2)
pts1 = 0:0.05:1
pts2 = 0:0.05:1
grf = GaussianRandomField(cov,KarhunenLoeve(500),pts1,pts2)
@test isa(grf,GaussianRandomField)
@test isa(grf.cov,SeparableCovarianceFunction)
@test isa(grf.cov.cov,Vector)
@test isa(grf.cov.cov[1],Exponential)
@test isa(grf.cov.cov[2],Exponential)
@test ndims(grf.cov) == 2
@test isa(grf,GaussianRandomField{C,KarhunenLoeve{500}} where {C})
@test length(grf.pts) == 2
@test length(grf.pts[1]) == length(pts1)
@test length(grf.pts[2]) == length(pts2)
@test size(sample(grf),1) == length(pts1)
@test size(sample(grf),2) == length(pts2)

# matern/exponential 2d
m1 = Matern(0.2,1.0)
e1 = Exponential(0.01)
cov = SeparableCovarianceFunction([m1,e1])
pts1 = 0:0.05:1
pts2 = 0:0.05:1
grf = GaussianRandomField(cov,KarhunenLoeve(250),pts1,pts2,quad=EOLE())
@test isa(grf,GaussianRandomField)
@test isa(grf.cov,SeparableCovarianceFunction)
@test isa(grf.cov.cov,Vector)
@test isa(grf.cov.cov[1],Matern)
@test isa(grf.cov.cov[2],Exponential)
@test ndims(grf.cov) == 2
@test isa(grf,GaussianRandomField{C,KarhunenLoeve{250}} where {C})
@test length(grf.pts) == 2
@test length(grf.pts[1]) == length(pts1)
@test length(grf.pts[2]) == length(pts2)
@test size(sample(grf),1) == length(pts1)
@test size(sample(grf),2) == length(pts2)

## 3d separable Matern ##
m1 = Matern(0.2,1.0)
m2 = Matern(0.4,3.0)
m3 = Matern(0.6,4.0)
cov = SeparableCovarianceFunction(m1,m1,m2)
pts1 = 0:0.1:1
pts2 = 0:0.1:1
pts3 = 0:0.1:1
grf = GaussianRandomField(cov,KarhunenLoeve(100),pts1,pts2,pts3)
@test isa(grf,GaussianRandomField)
@test isa(grf.cov,SeparableCovarianceFunction)
@test isa(grf.cov.cov,Vector)
@test isa(grf.cov.cov[1],Matern)
@test isa(grf.cov.cov[2],Matern)
@test isa(grf.cov.cov[3],Matern)
@test ndims(grf.cov) == 3
@test isa(grf,GaussianRandomField{C,KarhunenLoeve{100}} where {C})
@test length(grf.pts) == 3
@test length(grf.pts[1]) == length(pts1)
@test length(grf.pts[2]) == length(pts2)
@test length(grf.pts[3]) == length(pts3)
@test size(sample(grf),1) == length(pts1)
@test size(sample(grf),2) == length(pts2)
@test size(sample(grf),3) == length(pts3)

end


text:
f = 0
for i = 999:-1:899
    for j = i:-1:899
        p = i*j
        a = "$p"
        if a[1:3] == a[6:-1:4]
            println(p);
            f = 1
        end
    end
    if( f== 1)
        break
    end
end


text:
import Distances: Metric, SqEuclidean, Euclidean

"""
    SMeasureTest(m, τ, K)
    SMeasureTest(m::Int, τ, K; metric::Metric = SqEuclidean(),
        tree_metric::Metric = Euclidean())

S-measure test [1] for the directional dependence between data series.

## Algorithm

1. Create an `m-`dimensional embeddings of both `x` and `y`, resulting in
    `N` different `m`-dimensional embedding points
    ``X = \\{x_1, x_2, \\ldots, x_N \\}`` and ``X = \\{y_1, y_2, \\ldots, y_N \\}``.
    `τ` controls the embedding lag.
2. Let ``r_{i,j}`` and ``s_{i,j}`` be the indices of the `k`-th nearest neighbors
    of ``x_i`` and ``y_i``, respectively.
3. Compute the the mean squared Euclidean distance to the ``k`` nearest neighbors
    for each ``x_i``, using the indices ``r_{i, j}``.

```math
R_i^{(k)}(x) = \\dfrac{1}{k} \\sum_{i=1}^{k}(x_i, x_{r_{i,j}})^2
```

- Compute the y-conditioned mean squared Euclidean distance to the ``k`` nearest
    neighbors for each ``x_i``, now using the indices ``s_{i,j}``.

```math
R_i^{(k)}(x|y) = \\dfrac{1}{k} \\sum_{i=1}^{k}(x_i, x_{s_{i,j}})^2
```

- Define the following measure of independence, where ``0 \\leq S \\leq 1``, and
    low values indicate independence and values close to one occur for
    synchronized signals.

```math
S^{(k)}(x|y) = \\dfrac{1}{N} \\sum_{i=1}^{N} \\dfrac{R_i^{(k)}(x)}{R_i^{(k)}(x|y)}
```

## Examples

```julia
# Initialise test, specifying embedding dimension, embedding lag
# and number of nearest neighbors
test = SMeasureTest(m = 4, τ = 3, K = 2:10)
```

## References

1.  Quian Quiroga, R., Arnhold, J. & Grassberger, P. [2000] “Learning
    driver-response relationships from synchronization patterns,”
    Phys. Rev. E61(5), 5142–5148.
"""
Base.@kwdef struct SMeasureTest{N, M, TM} <: DistanceBasedCausalityTest{N} where {M, TM}
    """ The embedding dimension. Defaults to 2. """
    m::Int = 2

    """ The embedding lag. Defaults to 1. """
    τ::Int = 1

    """ Neighborhood size(s). Defaults to 2:10. """
    K = 2:10

    """ The distance metric used when computing distances. Defaults to `SqEuclidean()`"""
    metric::M = SqEuclidean()

    """
    The distance metric used when computing `KDTree` for nearest neighbor searches.
    Defaults to `Euclidean()`.
    """
    tree_metric::TM = Euclidean()

    function SMeasureTest(m, τ, K, metric::M, tree_metric::TM) where {M, TM}
        N = length(K)
        new{N, M, TM}(m, τ, K, metric, tree_metric)
    end
end

function SMeasureTest(m, τ, K; metric::Metric = SqEuclidean(), tree_metric::Metric = Euclidean())
    SMeasureTest(m = m, τ = τ, K = K; metric = metric, tree_metric = tree_metric)
end

"""

    causality(x, y, test::SMeasureTest)

## Examples

```julia
using CausalityTools, DynamicalSystemsBase, Plots

# Create an orbit of the built-in `henon2` map
npts, Ttr = 5000, 500
x, y = columns(trajectory(henon2(c_xy = 1.0), npts, Ttr = Ttr));

# Some random time series
xr, yr = rand(npts), rand(npts);

# Initialise test, specifying embedding dimension, emebdding lag
# and number of nearest neighbors
Ks = 2:10
test = SMeasureTest(m = 4, τ = 1, K = Ks)

# Compute causality statistic on random time series, and on Henon map
# time series.
Ss_r_xy = causality(xr, yr, test)
Ss_r_yx = causality(yr, xr, test)
Ss_henon_xy = causality(x, y, test)
Ss_henon_yx = causality(y, x, test);

plot(xlabel = "# nearest neighbors (k)", ylabel = "S", ylims = (-0.05, 1.05))
plot!(Ks, Ss_r_xy,  label = "random uncoupled system (x -> y)", marker = stroke(2), c = :black)
plot!(Ks, Ss_r_yx,  label = "random uncoupled system (y -> x)", marker = stroke(2), c = :red)
plot!(Ks, Ss_henon_xy, marker = stroke(2), label = "henon unidir (x -> y)")
plot!(Ks, Ss_henon_yx, marker = stroke(2), label = "henon unidir (y -> x)")
```
"""
function causality(x::AbstractVector, y::AbstractVector, test::SMeasureTest)
    Ss = zeros(Float64, length(test.K))

    for (i, k) in enumerate(test.K)
        Ss[i] = s_measure(x, y,test.m, test.τ, k, test.metric, test.tree_metric)
    end

    return Ss
end

export SMeasureTest, causality


text:
export SolitaryWaveSerreGreenNaghdi

"""
    SolitaryWaveSerreGreenNaghdi(param; kwargs...)

Compute the Serre-Green-Naghdi solitary wave with prescribed velocity.

# Arguments
- `param :: NamedTuple`: parameters of the problem containing velocity `c` and dimensionless parameters `ϵ` and `μ`, and mesh size `L` and number of collocation points `N`;
- `x₀ :: Real`: (keyword, optional, default = 0) center of solitary wave.

# Return values
`(η,u,v)` with
- `η :: Vector{Float64}`: surface deformation;
- `u :: Vector{Float64}`: layer-averaged velocity;
- `v :: Vector{Float64}`: tangential velocity;
- `mesh :: Mesh`: mesh collocation points.

"""
function SolitaryWaveSerreGreenNaghdi(
                param :: NamedTuple;
                x₀ = 0 :: Real
                        )

        c = param.c
        ϵ = param.ϵ
        μ = param.μ

        mesh = Mesh(param)

        η = (c^2-1)/ϵ*sech.(sqrt(3*(c^2-1)/(c^2)/μ)/2*(mesh.x.-x₀)).^2

        h = 1 .+ ϵ*η
        u = c*η./h

	k = mesh.k
        Dx       =  1im * k

        DxF(v) = real.(ifft(Dx .* fft(v)))
	v = u - μ/3 ./h .* (DxF(h.^3 .*DxF(u)))

        return (η,u,v,mesh)

end


text:
using QXGraphDecompositions
using Documenter

DocMeta.setdocmeta!(QXGraphDecompositions, :DocTestSetup, :(using QXGraphDecompositions); recursive=true)
makedocs(;
    modules=[QXGraphDecompositions],
    authors="QuantEx team",
    repo="https://github.com/JuliaQX/QXGraphDecompositions.jl/blob/{commit}{path}#L{line}",
    sitename="QXGraphDecompositions.jl",
    format=Documenter.HTML(;
        prettyurls=get(ENV, "CI", "false") == "true",
        canonical="https://JuliaQX.github.io/QXGraphDecompositions.jl",
        assets=String[],
    ),
    pages=[
        "Home" => "index.md",
        "Labeled Graphs" => "LabeledGraph.md",
        "Treewidth" => "treewidth.md",
        "Index" => "docs_index.md",
        "LICENSE" => "license.md"
    ],
)

deploydocs(;
    repo="github.com/JuliaQX/QXGraphDecompositions.jl",
)


text:
__precompile__()

module Tableaus

    using ..Integrators

    include("tableaus/tableaus_erk.jl")

    export getTableauExplicitEuler, getTableauExplicitMidpoint, getTableauHeun,
           getTableauKutta, getTableauERK4, getTableauERK438

    include("tableaus/tableaus_dirk.jl")

    export getTableauCrouzeix

    include("tableaus/tableaus_firk.jl")

    export getTableauImplicitEuler, getTableauImplicitMidpoint,
           getTableauGLRK1, getTableauGLRK2, getTableauGLRK3

    include("tableaus/tableaus_sirk.jl")

    include("tableaus/tableaus_eprk.jl")

    export getTableauSymplecticEulerA, getTableauSymplecticEulerB

    include("tableaus/tableaus_spark.jl")

end


text:
import ITensors.NDTensors: NeverAlias, AliasStyle, AllowAlias
import ITensors: ITensor
import CUDA: CuArray

function cuITensor(::Type{T}, inds::IndexSet) where {T<:Number}
  return ITensor(Dense{float(T)}(CUDA.zeros(float(T), dim(inds))), inds)
end
cuITensor(::Type{T}, inds::Index...) where {T<:Number} = cuITensor(T, IndexSet(inds...))

cuITensor(is::IndexSet) = cuITensor(Float64, is)
cuITensor(inds::Index...) = cuITensor(IndexSet(inds...))

cuITensor() = ITensor()
function cuITensor(x::S, inds::IndexSet{N}) where {S<:Number,N}
  dat = CuVector{float(S)}(undef, dim(inds))
  fill!(dat, float(x))
  return ITensor(Dense{S}(dat), inds)
end
cuITensor(x::S, inds::Index...) where {S<:Number} = cuITensor(x, IndexSet(inds...))

function ITensor(
  as::AliasStyle,
  eltype::Type{<:Number},
  A::CuArray{<:Number},
  inds::Indices{Index{Int}};
  kwargs...,
)
  length(A) ≠ dim(inds) && throw(
    DimensionMismatch(
      "In ITensor(::CuArray, inds), length of AbstractArray ($(length(A))) must match total dimension of IndexSet ($(dim(inds)))",
    ),
  )
  data = CuArray{eltype}(as, A)
  return itensor(Dense(data), inds)
end
# Helper functions for different view behaviors
CuArray{ElT,N}(::NeverAlias, A::AbstractArray) where {ElT,N} = CuArray{ElT,N}(A)
function CuArray{ElT,N}(::AllowAlias, A::AbstractArray) where {ElT,N}
  return convert(CuArray{ElT,N}, A)
end
function CuArray{ElT}(as::AliasStyle, A::AbstractArray{ElTA,N}) where {ElT,N,ElTA}
  return CuArray{ElT,N}(as, A)
end

# TODO: Change to:
# (Array{ElT, N} where {ElT})([...]) = [...]
# once support for `VERSION < v"1.6"` is dropped.
# Previous to Julia v1.6 `where` syntax couldn't be used in a function name
function CuArray{<:Any,N}(as::AliasStyle, A::AbstractArray{ElTA,N}) where {N,ElTA}
  return CuArray{ElTA,N}(as, A)
end

#TODO: check that the size of the Array matches the Index dimensions
function cuITensor(A::Array{S}, inds) where {S<:Number}
  return ITensor(Dense(CuArray{S}(A)), inds)
end
function cuITensor(A::CuArray{S}, inds::IndexSet) where {S<:Number}
  return ITensor(Dense(A), inds)
end
cuITensor(A::Array{S}, inds::Index...) where {S<:Number} = cuITensor(A, IndexSet(inds...))
cuITensor(A::CuArray{S}, inds::Index...) where {S<:Number} = cuITensor(A, IndexSet(inds...))

function cuITensor(A::ITensor)
  return if storage(tensor(A)) isa ITensors.EmptyStorage
    cuITensor(zero(eltype(storage(tensor(A)))), inds(A)...)
  else
    cuITensor(data(tensor(A)), inds(A)...)
  end
end

cu(A::ITensor) = cuITensor(A)

# Helpful for moving gate structures to GPU
cu(A::Array{ITensor}) = map(cu, A)
cu(A::Array{<:Array{ITensor}}) = map(cu, A)

function cpu(A::ITensor)
  typeof(data(storage(A))) <: CuArray && return ITensor(cpu(storage(A)), inds(A))
  return A
end

# Helpful for moving gate structures to CPU
cpu(A::Array{ITensor}) = map(cpu, A)
cpu(A::Array{<:Array{ITensor}}) = map(cpu, A)

function randomCuITensor(::Type{S}, inds::Indices) where {S<:Real}
  T = cuITensor(S, inds)
  randn!(T)
  return T
end
function randomCuITensor(::Type{S}, inds::Indices) where {S<:Complex}
  Tr = cuITensor(real(S), inds)
  Ti = cuITensor(real(S), inds)
  randn!(Tr)
  randn!(Ti)
  return complex(Tr) + im * Ti
end
function randomCuITensor(::Type{S}, inds::Index...) where {S<:Number}
  return randomCuITensor(S, IndexSet(inds...))
end
randomCuITensor(inds::IndexSet) = randomCuITensor(Float64, inds)
randomCuITensor(inds::Index...) = randomCuITensor(Float64, IndexSet(inds...))

CuArray(T::ITensor) = CuArray(tensor(T))

function CuArray{ElT,N}(T::ITensor, is::Vararg{Index,N}) where {ElT,N}
  ndims(T) != N && throw(
    DimensionMismatch(
      "cannot convert an $(ndims(T)) dimensional ITensor to an $N-dimensional CuArray."
    ),
  )
  TT = tensor(permute(T, is...; allow_alias=true))
  return CuArray{ElT,N}(TT)::CuArray{ElT,N}
end

function CuArray{ElT}(T::ITensor, is::Vararg{Index,N}) where {ElT,N}
  return CuArray{ElT,N}(T, is...)
end

function CuArray(T::ITensor, is::Vararg{Index,N}) where {N}
  return CuArray{eltype(T),N}(T, is...)::CuArray{<:Number,N}
end

CUDA.CuMatrix(A::ITensor) = CuArray(A)

function CuVector(A::ITensor)
  if ndims(A) != 1
    throw(DimensionMismatch("Vector() expected a 1-index ITensor"))
  end
  return CuArray(A)
end

function CuMatrix(T::ITensor, i1::Index, i2::Index)
  ndims(T) != 2 &&
    throw(DimensionMismatch("ITensor must be order 2 to convert to a Matrix"))
  return CuArray(T, i1, i2)
end


text: # Automatically generated using Clang.jl


text:
module FiniteFields

# taken from https://github.com/kalmarek/RamanujanGraphs.jl/blob/master/src/gf.jl

using Primes
export GF, int, characteristic
export generator, issqrt

struct GF{q} <: Number
    value::Int

    function GF{q}(n, check = true) where {q}
        if check
            @assert q > 1
            @assert isprime(q)
        end
        return new{q}(mod(n, q))
    end
end

GF{q}(n::GF{q}) where {q} = GF{q}(Int(n), false)

Base.Int(n::GF) = Int(n.value)
characteristic(::Type{GF{q}}) where {q} = q
characteristic(::GF{q}) where {q} = q

Base.:(==)(n::GF{q}, m::GF{q}) where {q} = Int(n) == Int(m)
# hash(GF) == 0x04fd9e474909f8bf
Base.hash(n::GF{q}, h::UInt) where {q} =
    xor(0x04fd9e474909f8bf, hash(q, hash(Int(n), h)))

Base.:+(n::GF{q}, m::GF{q}) where {q} = GF{q}(Int(n) + Int(m), false)
Base.:-(n::GF{q}, m::GF{q}) where {q} = GF{q}(Int(n) - Int(m), false)
Base.:*(n::GF{q}, m::GF{q}) where {q} = GF{q}(Int(n) * Int(m), false)
Base.:*(n::Integer, m::GF{q}) where {q} = GF{q}(n * Int(m), false)
Base.:/(n::GF{q}, m::GF{q}) where {q} = n * inv(m)

Base.:-(n::GF{q}) where {q} = GF{q}(q - Int(n), false)
Base.inv(n::GF{q}) where {q} = GF{q}(invmod(Int(n), q), false)

function Base.:^(n::GF{q}, i::Integer) where {q}
    i < 0 && return inv(n)^-i
    return GF{q}(powermod(Int(n), i, q), false)
end

Base.zero(::Type{GF{q}}) where {q} = GF{q}(0, false)
Base.one(::Type{GF{q}}) where {q} = GF{q}(1, false)
Base.iszero(n::GF) = iszero(Int(n))
Base.isone(n::GF) = isone(Int(n))

Base.promote_rule(::Type{GF{q}}, ::Type{I}) where {q,I<:Integer} = GF{q}
Base.promote_rule(::Type{GF{p}}, ::Type{GF{q}}) where {p,q} = throw(
    DomainError(
        (GF{p}, GF{q}),
        "Cannot perform arithmetic on elements from different fields",
    ),
)

Base.div(n::GF{q}, m::Integer) where {q} = n / GF{q}(m, false)
Base.div(m::Integer, n::GF{q}) where {q} = GF{q}(m, false) / n

# taken from ValidatedNumerics, under under the MIT "Expat" License:
# https://github.com/JuliaIntervals/ValidatedNumerics.jl/blob/master/LICENSE.md
function subscriptify(n::Integer)
    subscript_0 = Int(0x2080) # Char(0x2080) -> subscript 0
    return join((Char(subscript_0 + d) for d in reverse(digits(n))), "")
end

Base.show(io::IO, n::GF{q}) where {q} = print(io, "$(Int(n))" * subscriptify(q))
Base.isless(n::GF{q}, m::GF{q}) where {q} = isless(Int(n), Int(m))

function legendresymbol(n, q)
    iszero(mod(n, q)) && return zero(n)
    isone(powermod(n, (q - 1) ÷ 2, q)) && return one(n)
    return -one(n)
end

function generator(::Type{GF{q}}) where {q}
    q == 2 && return one(GF{2})
    return rootofunity(GF{q}, q - 1)
end

function rootofunity(::Type{GF{q}}, ord::Integer) where {q}
    d, r = divrem(q - 1, ord)
    !iszero(r) &&
        throw(DomainError(GF{q}, "No root of unity of order $ord in $(GF{q})."))
    for i = 2:q-1
        g = GF{q}(i, false)
        acc = g
        for j = 2:ord-1
            acc *= g
            isone(acc) && @goto next_elt
        end
        isone(acc * g) && return g
        @label next_elt
    end
    return zero(GF{q}) # never hit, to keep compiler happy
end

Base.sqrt(n::GF{q}) where {q} = GF{q}(sqrtmod(Int(n), q), false)
issquare(n::GF{q}) where {q} = legendresymbol(Int(n), q) >= 0

function sqrtmod(n::Integer, q::Integer)
    l = legendresymbol(n, q)
    l == 0 && return zero(n)
    l == -1 && throw(DomainError(n, "$n is not a square modulo $q"))
    for i = 1:q # bruteforce loop
        y = powermod(i, 2, q)
        y == n && return oftype(n, i)
    end
    return zero(n) # never hit, to keep compiler happy
end

Base.iterate(::Type{GF{q}}, s = 0) where {q} =
    s >= q ? nothing : (GF{q}(s, false), s + 1)
Base.eltype(::Type{GF{q}}) where {q} = GF{q}
Base.size(gf::Type{<:GF}) = (characteristic(gf),)

# needed for basic linear algebra over GF{q}:
Base.abs(x::GF) = x
Base.conj(x::GF) = x

end # module


text:
using ProgressMeter
using TimerOutputs
using ParticleInCell

const to = TimerOutput()

function run( nsteps)

    dt = 0.1
    alpha = 0.1
    kx = 0.5

    nx = 128
    ny = 16
    xmin, xmax = 0.0, 4π
    ymin, ymax = 0.0, 1.0

    n_particles = 100000
    degree_smoother = 3

    mesh = TwoDGrid( xmin, xmax, nx, ymin, ymax, ny)

    particles = ParticleGroup{2,2}( n_particles, charge=1.0, mass=1.0, n_weights=1)

    sampler = LandauDamping( alpha, kx )

    sample!( particles, mesh, sampler)

    poisson = Poisson2DPeriodic( mesh )
    kernel = ParticleMeshCoupling2D( particles, mesh, degree_smoother, :collocation)

    problem = PICPoisson2D(  poisson, kernel )

    particles.array[2,:] .*= ( ymax - ymin)
    particles.array[5,:]  .= (xmax - xmin) * (ymax - ymin) / n_particles;

    propagator = SplittingOperator( problem, particles )

    time = Float64[0.0]
    energy = Float64[compute_field_energy(problem, 1)]

    reset_timer!()
    @showprogress 1 for j=1:nsteps

        @timeit to "operatorT" ParticleInCell.operator_t!(propagator, 0.5dt)
        @timeit to "charge computation" ParticleInCell.charge_deposition!(propagator)
        @timeit to "fields solver" ParticleInCell.solve_fields!(propagator)
        @timeit to "operatorV" ParticleInCell.operator_v!(propagator, dt)
        @timeit to "operatorT" ParticleInCell.operator_t!(propagator, 0.5dt)

        push!(time, j*dt)
        push!(energy, compute_field_energy(problem, 1))

    end

    time, energy

end

t, energy = run( 1 ) # trigger building
@show nstep = 1000
@time t, energy = run( nstep )
show(to)

open("results_jl.dat", "w") do f

    for i in 1:nstep
        println(f, t[i], " ", energy[i])
    end

end

println()


text:
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Base: get_world_counter

# DO NOT ALTER ORDER OR SPACING OF METHODS BELOW
const lineoffset = @__LINE__
ambig(x, y) = 1
ambig(x::Integer, y) = 2
ambig(x, y::Integer) = 3
ambig(x::Int, y::Int) = 4
ambig(x::Number, y) = 5
# END OF LINE NUMBER SENSITIVITY

# For curmod_*
include("testenv.jl")

@test length(methods(ambig)) == 5
@test length(Base.methods_including_ambiguous(ambig, Tuple)) == 5

@test length(methods(ambig, (Int, Int))) == 1
@test length(methods(ambig, (UInt8, Int))) == 0
@test length(Base.methods_including_ambiguous(ambig, (UInt8, Int))) == 3

@test ambig("hi", "there") == 1
@test ambig(3.1, 3.2) == 5
@test ambig(3, 4) == 4
@test_throws MethodError ambig(0x03, 4)
@test_throws MethodError ambig(0x03, 4)  # test that not inserted into cache

# Ensure it still works with potential inlining
callambig(x, y) = ambig(x, y)
@test_throws MethodError callambig(0x03, 4)

# Printing ambiguity errors
let err = try
              ambig(0x03, 4)
          catch _e_
              _e_
          end
    io = IOBuffer()
    Base.showerror(io, err)
    lines = split(String(take!(io)), '\n')
    ambig_checkline(str) = startswith(str, "  ambig(x, y::Integer) in $curmod_str at") ||
                           startswith(str, "  ambig(x::Integer, y) in $curmod_str at") ||
                           startswith(str, "  ambig(x::Number, y) in $curmod_str at")
    @test ambig_checkline(lines[2])
    @test ambig_checkline(lines[3])
    @test ambig_checkline(lines[4])
    @test lines[5] == "Possible fix, define"
    @test lines[6] == "  ambig(::Integer, ::Integer)"
end

ambig_with_bounds(x, ::Int, ::T) where {T<:Integer,S} = 0
ambig_with_bounds(::Int, x, ::T) where {T<:Integer,S} = 1
let err = try
              ambig_with_bounds(1, 2, 3)
          catch _e_
              _e_
          end
    io = IOBuffer()
    Base.showerror(io, err)
    lines = split(String(take!(io)), '\n')
    @test lines[end] == "  ambig_with_bounds(::$Int, ::$Int, ::T) where T<:Integer"
end

## Other ways of accessing functions
# Test that non-ambiguous cases work
let io = IOBuffer()
    @test precompile(ambig, (Int, Int)) == true
    cf = @eval @cfunction(ambig, Int, (Int, Int))
    @test ccall(cf, Int, (Int, Int), 1, 2) == 4
    @test length(code_lowered(ambig, (Int, Int))) == 1
    @test length(code_typed(ambig, (Int, Int))) == 1
end

# Test that ambiguous cases fail appropriately
let io = IOBuffer()
    @test precompile(ambig, (UInt8, Int)) == false
    cf = @eval @cfunction(ambig, Int, (UInt8, Int))  # test for a crash (doesn't throw an error)
    @test_throws(MethodError(ambig, (UInt8(1), Int(2)), get_world_counter()),
                 ccall(cf, Int, (UInt8, Int), 1, 2))
    @test_throws(ErrorException("no unique matching method found for the specified argument types"),
                 which(ambig, (UInt8, Int)))
    @test length(code_typed(ambig, (UInt8, Int))) == 0
end

# Method overwriting doesn't destroy ambiguities
@test_throws MethodError ambig(2, 0x03)
ambig(x, y::Integer) = 3
@test_throws MethodError ambig(2, 0x03)

# Method overwriting by an ambiguity should also invalidate the method cache (#21963)
ambig(x::Union{Char, Int8}) = 'r'
@test ambig('c') == 'r'
@test ambig(Int8(1)) == 'r'
@test_throws MethodError ambig(Int16(1))
ambig(x::Union{Char, Int16}) = 's'
@test_throws MethodError ambig('c')
@test ambig(Int8(1)) == 'r'
@test ambig(Int16(1)) == 's'

# Automatic detection of ambiguities
module Ambig1
ambig(x, y) = 1
ambig(x::Integer, y) = 2
ambig(x, y::Integer) = 3
end

ambs = detect_ambiguities(Ambig1)
@test length(ambs) == 1

module Ambig2
ambig(x, y) = 1
ambig(x::Integer, y) = 2
ambig(x, y::Integer) = 3
ambig(x::Number, y) = 4
end

ambs = detect_ambiguities(Ambig2)
@test length(ambs) == 2

module Ambig3
ambig(x, y) = 1
ambig(x::Integer, y) = 2
ambig(x, y::Integer) = 3
ambig(x::Int, y::Int) = 4
end

ambs = detect_ambiguities(Ambig3)
@test length(ambs) == 1

module Ambig4
ambig(x, y) = 1
ambig(x::Int, y) = 2
ambig(x, y::Int) = 3
ambig(x::Int, y::Int) = 4
end
ambs = detect_ambiguities(Ambig4)
@test length(ambs) == 0

module Ambig5
ambig(x::Int8, y) = 1
ambig(x::Integer, y) = 2
ambig(x, y::Int) = 3
end

ambs = detect_ambiguities(Ambig5)
@test length(ambs) == 2

using LinearAlgebra, SparseArrays, SuiteSparse

# Test that Core and Base are free of ambiguities
# not using isempty so this prints more information when it fails
@testset "detect_ambiguities" begin
    let ambig = Set{Any}(((m1.sig, m2.sig) for (m1, m2) in detect_ambiguities(Core, Base; recursive=true, ambiguous_bottom=false)))
        @test_broken isempty(ambig)

        # this is basically just a list of everything for which typeintersection is too conservative,
        # and ends up matching some methods that don't actually have an interesting intersection
        expect = []
        push!(expect, (Tuple{typeof(convert),Type{T},Any} where T>:Union{Missing, Nothing}, Tuple{typeof(convert),Type{T},T} where T>:Nothing))
        push!(expect, (Tuple{typeof(convert),Type{T},T} where T, Tuple{typeof(convert),Type{T},Any} where T>:Missing))
        push!(expect, (Tuple{typeof(convert),Type{T},T} where T, Tuple{typeof(convert),Type{T},Any} where T>:Nothing))
        push!(expect, (Tuple{typeof(convert),Type{T},T} where T, Tuple{typeof(convert),Type{T},Any} where T>:Union{Missing, Nothing}))
        push!(expect, (Tuple{typeof(convert),Type{T},T} where T>:Missing, Tuple{typeof(convert),Type{T},Any} where T>:Union{Missing, Nothing}))
        push!(expect, (Tuple{typeof(hcat),Vararg{Union{Vector{T}, Matrix{T}, LinearAlgebra.Adjoint{T,Vector{T}}, LinearAlgebra.Transpose{T,Vector{T}}, LinearAlgebra.AbstractTriangular{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Hermitian{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Symmetric{T,A} where A<:(Matrix{T} where T)},N} where N} where T, Tuple{typeof(hcat),Vararg{BitVector,N} where N}))
        push!(expect, (Tuple{typeof(hcat),Vararg{Union{Vector{T}, Matrix{T}, LinearAlgebra.Adjoint{T,Vector{T}}, LinearAlgebra.Transpose{T,Vector{T}}, LinearAlgebra.AbstractTriangular{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Hermitian{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Symmetric{T,A} where A<:(Matrix{T} where T)},N} where N} where T, Tuple{typeof(hcat),Vararg{Number,N} where N}))
        push!(expect, (Tuple{typeof(hcat),Vararg{Union{Vector{T}, Matrix{T}, LinearAlgebra.Adjoint{T,Vector{T}}, LinearAlgebra.Transpose{T,Vector{T}}, LinearAlgebra.AbstractTriangular{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Hermitian{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Symmetric{T,A} where A<:(Matrix{T} where T)},N} where N} where T, Tuple{typeof(hcat),Vararg{Union{BitMatrix, BitVector},N} where N}))
        push!(expect, (Tuple{typeof(hvcat),Tuple{Vararg{Int,N} where N},Vararg{Union{Vector{T}, Matrix{T}, LinearAlgebra.Adjoint{T,Vector{T}}, LinearAlgebra.Transpose{T,Vector{T}}, LinearAlgebra.AbstractTriangular{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Hermitian{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Symmetric{T,A} where A<:(Matrix{T} where T)},N} where N} where T, Tuple{typeof(hvcat),Tuple{Vararg{Int,N} where N},Vararg{Number,N} where N}))
        push!(expect, (Tuple{typeof(vcat),Vararg{Union{Vector{T} where T, Matrix{T} where T, LinearAlgebra.AbstractTriangular{T,A} where A<:(Matrix{T} where T) where T, LinearAlgebra.AbstractTriangular{T,A} where A<:Union{LinearAlgebra.Adjoint{var"#s849",var"#s848"} where var"#s848"<:SparseArrays.SparseVector where var"#s849", LinearAlgebra.Bidiagonal, LinearAlgebra.Diagonal, LinearAlgebra.SymTridiagonal, LinearAlgebra.Transpose{var"#s847",var"#s846"} where var"#s846"<:SparseArrays.SparseVector where var"#s847", LinearAlgebra.Tridiagonal, SparseArrays.AbstractSparseMatrixCSC, SparseArrays.SparseVector} where T, LinearAlgebra.Adjoint{var"#s849",var"#s848"} where var"#s848"<:(Vector{T} where T) where var"#s849", LinearAlgebra.Adjoint{var"#s849",var"#s848"} where var"#s848"<:SparseArrays.SparseVector where var"#s849", LinearAlgebra.Bidiagonal, LinearAlgebra.Diagonal, LinearAlgebra.Hermitian{T,A} where A<:(Matrix{T} where T) where T, LinearAlgebra.Hermitian{T,A} where A<:Union{LinearAlgebra.Adjoint{var"#s849",var"#s848"} where var"#s848"<:SparseArrays.SparseVector where var"#s849", LinearAlgebra.Bidiagonal, LinearAlgebra.Diagonal, LinearAlgebra.SymTridiagonal, LinearAlgebra.Transpose{var"#s847",var"#s846"} where var"#s846"<:SparseArrays.SparseVector where var"#s847", LinearAlgebra.Tridiagonal, SparseArrays.AbstractSparseMatrixCSC, SparseArrays.SparseVector} where T, LinearAlgebra.SymTridiagonal, LinearAlgebra.Symmetric{T,A} where A<:(Matrix{T} where T) where T, LinearAlgebra.Symmetric{T,A} where A<:Union{LinearAlgebra.Adjoint{var"#s849",var"#s848"} where var"#s848"<:SparseArrays.SparseVector where var"#s849", LinearAlgebra.Bidiagonal, LinearAlgebra.Diagonal, LinearAlgebra.SymTridiagonal, LinearAlgebra.Transpose{var"#s847",var"#s846"} where var"#s846"<:SparseArrays.SparseVector where var"#s847", LinearAlgebra.Tridiagonal, SparseArrays.AbstractSparseMatrixCSC, SparseArrays.SparseVector} where T, LinearAlgebra.Transpose{var"#s847",var"#s846"} where var"#s846"<:(Vector{T} where T) where var"#s847", LinearAlgebra.Transpose{var"#s847",var"#s846"} where var"#s846"<:SparseArrays.SparseVector where var"#s847", LinearAlgebra.Tridiagonal, SparseArrays.AbstractSparseMatrixCSC, SparseArrays.SparseVector},N} where N}, Tuple{typeof(vcat),Vararg{AbstractRange{T},N} where N} where T))
        push!(expect, (Tuple{typeof(vcat),Vararg{Union{Vector{T} where T, Matrix{T} where T, LinearAlgebra.AbstractTriangular{T,A} where A<:(Matrix{T} where T) where T, LinearAlgebra.Adjoint{var"#s849",var"#s848"} where var"#s848"<:(Vector{T} where T) where var"#s849", LinearAlgebra.Hermitian{T,A} where A<:(Matrix{T} where T) where T, LinearAlgebra.Symmetric{T,A} where A<:(Matrix{T} where T) where T, LinearAlgebra.Transpose{var"#s847",var"#s846"} where var"#s846"<:(Vector{T} where T) where var"#s847"},N} where N}, Tuple{typeof(vcat),Vararg{AbstractRange{T},N} where N} where T))
        push!(expect, (Tuple{typeof(vcat),Vararg{Union{Vector{T}, Matrix{T}, LinearAlgebra.Adjoint{T,Vector{T}}, LinearAlgebra.Transpose{T,Vector{T}}, LinearAlgebra.AbstractTriangular{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Hermitian{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Symmetric{T,A} where A<:(Matrix{T} where T)},N} where N} where T, Tuple{typeof(vcat),Vararg{AbstractRange{T},N} where N} where T))
        push!(expect, (Tuple{typeof(vcat),Vararg{Union{Vector{T}, Matrix{T}, LinearAlgebra.Adjoint{T,Vector{T}}, LinearAlgebra.Transpose{T,Vector{T}}, LinearAlgebra.AbstractTriangular{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Hermitian{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Symmetric{T,A} where A<:(Matrix{T} where T)},N} where N} where T, Tuple{typeof(vcat),Vararg{BitMatrix,N} where N}))
        push!(expect, (Tuple{typeof(vcat),Vararg{Union{Vector{T}, Matrix{T}, LinearAlgebra.Adjoint{T,Vector{T}}, LinearAlgebra.Transpose{T,Vector{T}}, LinearAlgebra.AbstractTriangular{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Hermitian{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Symmetric{T,A} where A<:(Matrix{T} where T)},N} where N} where T, Tuple{typeof(vcat),Vararg{BitVector,N} where N}))
        push!(expect, (Tuple{typeof(vcat),Vararg{Union{Vector{T}, Matrix{T}, LinearAlgebra.Adjoint{T,Vector{T}}, LinearAlgebra.Transpose{T,Vector{T}}, LinearAlgebra.AbstractTriangular{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Hermitian{T,A} where A<:(Matrix{T} where T), LinearAlgebra.Symmetric{T,A} where A<:(Matrix{T} where T)},N} where N} where T, Tuple{typeof(vcat),Vararg{Number,N} where N}))

        while !isempty(ambig)
            sigs = pop!(ambig)
            i = findfirst(==(sigs), expect)
            if i === nothing
                println(stderr, "push!(expect, (", sigs[1], ", ", sigs[2], "))")
                continue
            end
            deleteat!(expect, i)
        end
        @test isempty(expect)
    end

    # some ambiguities involving Union{} type parameters are expected, but not required
    let ambig = Set(detect_ambiguities(Core; recursive=true, ambiguous_bottom=true))
        @test !isempty(ambig)
    end

    STDLIB_DIR = Sys.STDLIB
    STDLIBS = filter!(x -> x != "LinearAlgebra" && x != "SparseArrays" && # Some packages run this test themselves
                           isfile(joinpath(STDLIB_DIR, x, "src", "$(x).jl")),
                      readdir(STDLIB_DIR))

    # List standard libraries. Exclude modules such as Main, Base, and Core.
    let modules = [mod for (pkg, mod) in Base.loaded_modules if pkg.uuid !== nothing && String(pkg.name) in STDLIBS]
        @test isempty(detect_ambiguities(modules...; recursive=true))
    end
end

amb_1(::Int8, ::Int) = 1
amb_1(::Integer, x) = 2
amb_1(x, ::Int) = 3
# if there is an ambiguity with some methods and not others, `methods`
# should return just the non-ambiguous ones, i.e. the ones that could actually
# be called.
@test length(methods(amb_1, Tuple{Integer, Int})) == 1

amb_2(::Int, y) = 1
amb_2(x, ::Int) = 2
amb_2(::Int8, y) = 3
@test length(methods(amb_2)) == 3  # make sure no duplicates

amb_3(::Int8, ::Int8) = 1
amb_3(::Int16, ::Int16) = 2
amb_3(::Integer, ::Integer) = 3
amb_3(::Integer, x) = 4
amb_3(x, ::Integer) = 5
# ambiguous definitions exist, but are covered by multiple more specific definitions
let ms = methods(amb_3).ms
    @test !Base.isambiguous(ms[4], ms[5])
end

amb_4(::Int8, ::Int8) = 1
amb_4(::Int16, ::Int16) = 2
amb_4(::Integer, x) = 4
amb_4(x, ::Integer) = 5
# as above, but without sufficient definition coverage
let ms = methods(amb_4).ms
    @test Base.isambiguous(ms[3], ms[4])
end

g16493(x::T, y::Integer) where {T<:Number} = 0
g16493(x::Complex{T}, y) where {T} = 1
let ms = methods(g16493, (Complex, Any))
    @test length(ms) == 1
    @test first(ms).sig == (Tuple{typeof(g16493), Complex{T}, Any} where T)
end

# issue #17350
module Ambig6
struct ScaleMinMax{To,From} end
map1(mapi::ScaleMinMax{To,From}, val::From) where {To<:Union{Float32,Float64},From<:Real} = 1
map1(mapi::ScaleMinMax{To,From}, val::Union{Real,Complex}) where {To<:Union{Float32,Float64},From<:Real} = 2
end

@test isempty(detect_ambiguities(Ambig6))

module Ambig7
struct T end
(::T)(x::Int8, y) = 1
(::T)(x, y::Int8) = 2
end
@test length(detect_ambiguities(Ambig7)) == 1

module Ambig17648
struct MyArray{T,N} <: AbstractArray{T,N}
    data::Array{T,N}
end

foo(::Type{Array{T,N}}, A::MyArray{T,N}) where {T,N} = A.data
foo(::Type{Array{T,N}}, A::MyArray{T,N}) where {T<:AbstractFloat,N} = A.data
foo(::Type{Array{S,N}}, A::MyArray{T,N}) where {S<:AbstractFloat,N,T<:AbstractFloat} =
    copyto!(Array{S}(undef, unsize(A)), A.data)
foo(::Type{Array{S,N}}, A::AbstractArray{T,N}) where {S<:AbstractFloat,N,T<:AbstractFloat} =
    copyto!(Array{S}(undef, size(A)), A)
end

@test isempty(detect_ambiguities(Ambig17648))

module Ambig8
# complex / unsorted(-able) ambiguities
f(::Union{typeof(pi), Integer}) =  1
f(::Union{AbstractIrrational, Int}) =  2
f(::Irrational) = 3
f(::Signed) = 4
g(::Irrational) = 3
g(::Signed) = 4
g(::Union{typeof(pi), Integer}) =  1
g(::Union{AbstractIrrational, Int}) =  2
struct Irrational2 <: AbstractIrrational; end
end
@test isempty(methods(Ambig8.f, (Int,)))
@test isempty(methods(Ambig8.g, (Int,)))
for f in (Ambig8.f, Ambig8.g)
    @test length(methods(f, (Integer,))) == 2 # 1 is also acceptable
    @test length(methods(f, (Signed,))) == 1 # 2 is also acceptable
    @test length(Base.methods_including_ambiguous(f, (Signed,))) == 2
    @test f(0x00) == 1
    @test f(Ambig8.Irrational2()) == 2
    @test f(MathConstants.γ) == 3
    @test f(Int8(0)) == 4
    @test_throws MethodError f(0)
    @test_throws MethodError f(pi)
end

module Ambig9
f(x::Complex{<:Integer}) = 1
f(x::Complex{<:Rational}) = 2
end
@test !Base.isambiguous(methods(Ambig9.f)..., ambiguous_bottom=false)
@test Base.isambiguous(methods(Ambig9.f)..., ambiguous_bottom=true)
@test !Base.isambiguous(methods(Ambig9.f)...)
@test length(detect_ambiguities(Ambig9, ambiguous_bottom=false)) == 0
@test length(detect_ambiguities(Ambig9, ambiguous_bottom=true)) == 1
@test length(detect_ambiguities(Ambig9)) == 0

# issue #25341
module M25341
_totuple(::Type{Tuple{Vararg{E}}}, itr, s...) where {E} = E
end
@test length(detect_unbound_args(M25341; recursive=true)) == 1

# Test that Core and Base are free of UndefVarErrors
# not using isempty so this prints more information when it fails
@testset "detect_unbound_args in Base and Core" begin
    # TODO: review this list and remove everything between test_broken and test
    let need_to_handle_undef_sparam =
            Set{Method}(detect_unbound_args(Core; recursive=true))
        pop!(need_to_handle_undef_sparam, which(Core.Compiler.eltype, Tuple{Type{Tuple{Any}}}))
        @test_broken need_to_handle_undef_sparam == Set()
        pop!(need_to_handle_undef_sparam, which(Core.Compiler._cat, Tuple{Any, AbstractArray}))
        pop!(need_to_handle_undef_sparam, first(methods(Core.Compiler.same_names)))
        @test need_to_handle_undef_sparam == Set()
    end
    let need_to_handle_undef_sparam =
            Set{Method}(detect_unbound_args(Base; recursive=true))
        pop!(need_to_handle_undef_sparam, which(Base._totuple, (Type{Tuple{Vararg{E}}} where E, Any, Any)))
        pop!(need_to_handle_undef_sparam, which(Base.eltype, Tuple{Type{Tuple{Any}}}))
        pop!(need_to_handle_undef_sparam, first(methods(Base.same_names)))
        @test_broken need_to_handle_undef_sparam == Set()
        pop!(need_to_handle_undef_sparam, which(Base._cat, Tuple{Any, AbstractArray}))
        pop!(need_to_handle_undef_sparam, which(Base.byteenv, (Union{AbstractArray{Pair{T,V}, 1}, Tuple{Vararg{Pair{T,V}}}} where {T<:AbstractString,V},)))
        pop!(need_to_handle_undef_sparam, which(Base.float, Tuple{AbstractArray{Union{Missing, T},N} where {T, N}}))
        pop!(need_to_handle_undef_sparam, which(Base.zero, Tuple{Type{Union{Missing, T}} where T}))
        pop!(need_to_handle_undef_sparam, which(Base.one, Tuple{Type{Union{Missing, T}} where T}))
        pop!(need_to_handle_undef_sparam, which(Base.oneunit, Tuple{Type{Union{Missing, T}} where T}))
        @test need_to_handle_undef_sparam == Set()
    end
end

@testset "has_bottom_parameter with Union{} in tvar bound" begin
    @test Base.has_bottom_parameter(Ref{<:Union{}})
end

# test a case where specificity is not transitive over subtyping
f35983(::T, ::T) where {T} = 1
f35983(::Type, ::Type) = 2
@test f35983(10, 12) == 1
@test f35983(Int32, Int32) == 2
@test f35983(Int32, Int64) == 2
@test f35983(Int32, Complex) == 2
@test only(Base.methods_including_ambiguous(f35983, (Type, Type))).sig == Tuple{typeof(f35983), Type, Type}
@test only(Base.methods(f35983, (Type, Type))).sig == Tuple{typeof(f35983), Type, Type}
@test length(Base.methods_including_ambiguous(f35983, (Any, Any))) == 2
@test first(Base.methods_including_ambiguous(f35983, (Any, Any))).sig == Tuple{typeof(f35983), Type, Type}
@test length(Base.methods(f35983, (Any, Any))) == 2
@test first(Base.methods(f35983, (Any, Any))).sig == Tuple{typeof(f35983), Type, Type}
let ambig = Int32[0]
    ms = Base._methods_by_ftype(Tuple{typeof(f35983), Type, Type}, -1, typemax(UInt), true, UInt[typemin(UInt)], UInt[typemax(UInt)], ambig)
    @test length(ms) == 1
    @test ambig[1] == 0
end
f35983(::Type{Int16}, ::Any) = 3
@test length(Base.methods_including_ambiguous(f35983, (Type, Type))) == 2
@test length(Base.methods(f35983, (Type, Type))) == 2
let ambig = Int32[0]
    ms = Base._methods_by_ftype(Tuple{typeof(f35983), Type, Type}, -1, typemax(UInt), true, UInt[typemin(UInt)], UInt[typemax(UInt)], ambig)
    @test length(ms) == 2
    @test ambig[1] == 1
end

nothing

max_stars_repo_name: ExpandingMan/julia

max_issues_repo_name: ExpandingMan/julia

max_forks_repo_name: ExpandingMan/julia


text: sqrt(sum(A.^2.) / length(A))

max_stars_repo_name: ethansaxenian/RosettaDecode

max_issues_repo_name: ethansaxenian/RosettaDecode

max_forks_repo_name: ethansaxenian/RosettaDecode


text:
abstract type AbstractLoss end

# Triplet Embeddings
include("algorithms/triplet_embeddings.jl")

# Alternating Descent
include("algorithms/alternating_descent.jl")

# SDP
include("algorithms/sdp.jl")


text:
function CUDACompilerTarget(dev::CuDevice; kwargs...)
    cap = supported_capability(dev)

    exitable = true
    if cap < v"7"
        # JuliaGPU/CUDAnative.jl#4
        # ptxas for old compute capabilities has a bug where it messes up the
        # synchronization stack in the presence of shared memory and thread-divergent exit.
        exitable = false
    end
    if !has_nvml() || NVML.driver_version() < v"460"
        # JuliaGPU/CUDA.jl#431
        # TODO: tighten this conditional
        exitable = false
    end

    debuginfo = false

    PTXCompilerTarget(; cap, exitable, debuginfo, kwargs...)
end

struct CUDACompilerParams <: AbstractCompilerParams end

CUDACompilerJob = CompilerJob{PTXCompilerTarget,CUDACompilerParams}

GPUCompiler.runtime_module(::CUDACompilerJob) = CUDA

# filter out functions from libdevice and cudadevrt
GPUCompiler.isintrinsic(job::CUDACompilerJob, fn::String) =
    invoke(GPUCompiler.isintrinsic,
           Tuple{CompilerJob{PTXCompilerTarget}, typeof(fn)},
           job, fn) ||
    fn == "__nvvm_reflect" || startswith(fn, "cuda")

function GPUCompiler.link_libraries!(job::CUDACompilerJob, mod::LLVM.Module,
                                     undefined_fns::Vector{String})
    invoke(GPUCompiler.link_libraries!,
           Tuple{CompilerJob{PTXCompilerTarget}, typeof(mod), typeof(undefined_fns)},
           job, mod, undefined_fns)
    link_libdevice!(mod, job.target.cap, undefined_fns)
end

GPUCompiler.ci_cache(::CUDACompilerJob) = ci_cache

GPUCompiler.method_table(::CUDACompilerJob) = method_table


text: println("Hello, world from Julia!")

max_stars_repo_name: RomKart/bundle-calculator

max_issues_repo_name: RomKart/bundle-calculator

max_forks_repo_name: RomKart/bundle-calculator


text:
# basic integral struct with upper bound and lower bound.
struct Integral{T <: Symbolics.VarDomainPairing} <: Function
    domain::T
    Integral(domain) = new{typeof(domain)}(domain)
end

(I::Integral)(x) = Term{SymbolicUtils.symtype(x)}(I, [x])
(I::Integral)(x::Num) = Num(I(Symbolics.value(x)))
SymbolicUtils.promote_symtype(::Integral, x) = x

function Base.show(io::IO, I::Integral)
    print(io, "Integral(", I.domain.variables, ", ", I.domain.domain, ")")
end

Base.:(==)(I1::Integral, I2::Integral) = convert(Bool, simplify(isequal(I1.domain, I2.domain)))