Skip to content

Instantly share code, notes, and snippets.

#= reparameterized truncated normal =#
using Random
using Zygote, Distributions, Plots, SpecialFunctions
import Zygote: @adjoint, Numeric
import Base.Broadcast: broadcasted
"""
ndtr(a)
@cossio
cossio / undropdims.jl
Created May 4, 2020 14:30
undropdims: type stable inverse of dropdims
function mix_dims(dims1, dims2, t)
if in(t[1], dims2)
return (1, mix_dims(dims1, Base.tail(dims2), Base.tail(t))...)
else
return (dims1[1], mix_dims(Base.tail(dims1), dims2, Base.tail(t))...)
end
end
function mix_dims(dims1, dims2, t::Tuple{})
return ()
end
@cossio
cossio / checked.jl
Last active January 14, 2020 13:53
Checks for type stability the first time a function is called with a type signature.
macro checked(fdef)
d = splitdef(fdef)
f = d[:name]
args = d[:args]
whereparams = d[:whereparams]
d[:name] = gensym()
shadow_fdef = combinedef(d)
M = __module__
@cossio
cossio / orphaned_procs.sh
Last active November 23, 2018 19:49
To find the list of processes with PPID = 1.
pgrep -P 1 -u cossio # lists the PID of processes with PPID = 1
pgrep -P 1 -u cossio | xargs ps -p # prints the PID and some more info
pgrep -P 1 -u cossio -f julia | xargs ps -o pid,ppid,command -p # print orphaned julia procs
pgrep -P 1 -u cossio -f julia | xargs kill # kill them
(Proj) pkg> rm Sp┌ Error: Error in the keymap
│ exception =
│ type Nothing has no field ver
│ Stacktrace:
│ [1] getproperty(::Any, ::Symbol) at ./sysimg.jl:18
│ [2] __installed(::Pkg.Types.PackageMode) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Pkg/src/API.jl:284
│ [3] complete_installed_package at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Pkg/src/REPLMode.jl:773 [inlined]
│ [4] complete_argument(::String, ::Int64, ::Int64, ::Pkg.REPLMode.CommandKind, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Pkg/src/REPLMode.jl:821
│ [5] completions(::String, ::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Pkg/src/REPLMode.jl:853
│ [6] complete_line(::Pkg.REPLMode.PkgCompletionProvider, ::REPL.LineEdit.PromptState) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Pkg/src/REPLMode.jl:761
@cossio
cossio / midpoint.jl
Created November 6, 2018 22:03
midpoint of interval
"""midpoint of the interval [a,b]"""
function midpoint(a::Float64, b::Float64)
#= Based on F. Goualard. 2014, Table VII
DOI: 10.1145/2493882 =#
if !(a ≤ b)
return NaN
elseif a == -Inf
if b == Inf
return 0.
@cossio
cossio / Distributions_error.jl
Created October 31, 2018 15:46
Distributions.jl numerical error on Multinomial sampling
using Distributions
n = 4969027388018185
p = [4.471459552709845e-5,
0.0021070048390116223,
0.0017485819413324416,
0.0022622153482103544,
0.0019479678673445784,
0.0015864519243622935,
0.00037279832670023585,
@cossio
cossio / find_max_key.jl
Last active August 14, 2018 15:37
Find maximum key=>value pair in a Dict
function findmaxkey(d::Dict)
maxk,maxv = first(d)
for k in keys(d)
if d[k] > maxv
maxk, maxv = k, d[k]
end
end
return maxk=>maxv
end
for i = 1:L, a = 1:A
@assert sub2ind((A,L),a,i) == (i-1)*A + a
end
for j=1:L, i=1:L, b=1:A, a=1:A
@assert sub2ind((A,A,L,L),a,b,i,j) == a + (b-1)*A + (i-1)*A*A + (j-1)*A*A*L
end
srand(2);
L = rand(2:10); A = rand(2:10,L);
function dnacompl(nt::Char)
@assert nt ∈ ('A', 'T', 'C', 'G', 'N', '*')
if nt == 'A'
return 'T'
elseif nt == 'T'
return 'A'
elseif nt == 'G'
return 'C'
elseif nt == 'C'
return 'G'