Skip to content

Instantly share code, notes, and snippets.

@jsams
jsams / scale_sparse.jl
Created October 9, 2017 04:44
metaprogramming error
function scale_sd!(A::SparseMatrixCSC; corrected=true)
n = size(A, 1)
if corrected
sdcall = :(sqrt(sum(vi^2 for vi in v) / (n - 1) - (n / (n - 1)) * mn^2))
else
sdcall = :(sqrt(sum(vi^2 for vi in v) / n - mn^2))
end
for j in 1:size(A, 2)
k = A.colptr[j]:(A.colptr[j+1] - 1)
if length(k) == 0
@jsams
jsams / sd_weirdness.jl
Created October 9, 2017 16:42
function call different result than inline, also not defined on one branch
function scale_sd!(A::SparseMatrixCSC; corrected=true)
n = size(A, 1)
if corrected
sdcall(v,n,mn) = sqrt(sum(vi^2 for vi in v) / (n - 1) - (n / (n - 1)) * mn^2)
else
sdcall(v,n,mn) = sqrt(sum(vi^2 for vi in v) / n - mn^2)
end
for j in 1:size(A, 2)
k = A.colptr[j]:(A.colptr[j+1] - 1)
if length(k) > 0
@jsams
jsams / intset.jl
Created April 16, 2018 22:43
benchmark hiding function arguments?
using BenchmarkTools
function _test(N, prods, pos_prods, maxseen)
z = 0
for i in 1:N
neg = setdiff(prods, pos_prods[i])
z += rand(1:maxseen)
end
return z
end
@jsams
jsams / gradient_scope_test.jl
Last active May 4, 2018 05:48
how to get the gradient wrt to some inputs but not others? scoping?
using ReverseDiff
using Base.Test
mutable struct data
X::Array{Float64, 2}
end
const D = data(zeros(Float64, 2, 2))
@jsams
jsams / broken_benchmark.jl
Created May 6, 2018 00:29
running with @benchmarks throws an error but runs fine without
function get_all_tracks_info(db, track_id, ca; num=49)
res = vcat(SQLite.query(db, sr"""
SELECT track_id, track_index, author_id, genre, 1 AS listened
FROM tracks
WHERE track_id = ?;""",
values=[track_id]),
SQLite.query(db, sr"""
SELECT track_id, track_index, author_id, genre, 0 AS listened
FROM tracks
WHERE created_at < ? AND track_id <> ?
struct ChanWrap
iter_chan::RemoteChannel
end
Base.next(d::ChanWrap) = take!(d.iter_chan)
const rchan = RemoteChannel(() -> Channel{StructCompData}(args["max_channel_size"]))
@everywhere function produce_data(iter_chan, dbfile, nworkers, batch_size,
niters; kwargs...)
total_iters = batch_size * (niters + 2)
@jsams
jsams / short_circuit.sql
Created May 14, 2018 05:14
Way to short circuit logic in SQL(ite)?
/*
* table plays lists all plays by all users across time
* table affils lists who (fan) follows whom (contact) as of a given point of time
* table reposts lists who reposted what song at what time
* I want to know for each row in plays if the user was following someone who reposted the track
* I also want to know for each row in plays if the user followed someone who was the author of the track
* the author is frequently NULL
I think the below query is correct, but it looks like it realizes the full join of everyone who
may have reposted the track that the listener follows. I am wondering if there is a way to write it
function get_tracks_structured(db, user, track_id, author_id, genre, ca, weeks;
random_type::Symbol=:fake, rand_pct=0.20, count_each=2,
nrepost=count_each,
nartist=count_each, nartist_repost=count_each,
nseen=count_each, nseen_repost=count_each,
nsameartist=count_each, nsartist_repost=count_each,
do_selection=false)
if random_type == :fake
do_random = "AND rand01() < $(rand_pct)"
elseif random_type == :sort
@jsams
jsams / dsetin.R
Last active September 12, 2018 23:05
help charlie
library(data.table)
dt = data.table(i=rep(1:5, each=10), t=rep(1:5, times=2), x=runif(50), key=c('i', 't'))
myfunc = function(x) {
return(data.frame(sum=sum(x), mean=mean(x)))
}
dt[, myfunc(x), by=.(i, t)]
@jsams
jsams / jessica.jl
Created January 21, 2019 20:34
jessica's slowdown
function getprobs(x, a, b)
ea = exp.(x * a)
eb = exp.(x * b)
denom = 1 .+ ea .+ eb
proba = ea ./ denom
probb = eb ./ denom
return proba, probb, 1 .- proba, 1 .- probb
end