Skip to content

Instantly share code, notes, and snippets.

View jdelafon's full-sized avatar

Julien Delafontaine jdelafon

  • Lausanne, Switzerland
View GitHub Profile
@jdelafon
jdelafon / django_order_by_nulls_last.py
Last active February 3, 2016 11:55
Django order by NULLs last
qs = qs.extra(
select={'key_is_null': sort_key+' IS NULL'},
order_by = ['-key_is_null', sort_key]
)
@jdelafon
jdelafon / ajax_download.js
Last active February 11, 2016 09:33
Trigger a download from Ajax call
/**
* It is actually possible to trigger a download upon return from a GET or POST Ajax request.
* Source: http://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post/23797348#23797348
* Here with jQuery $ajax().
**/
$.ajax({...})
.then(function(response, status, xhr) {
/* Check for a filename */
var filename = "";
@jdelafon
jdelafon / readable_size.py
Created April 5, 2016 07:49
Change number of bytes to human-readable file size
def readable_size(num):
"""Change number of bytes to human-readable file size"""
for unit in ['','K','M','G','T','P','E','Z']:
if abs(num) < 1024.0:
return "%3.1f%sB" % (num, unit)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', 'B')
@jdelafon
jdelafon / homomorphic.jl
Created June 29, 2016 11:44
Attempt to implement simple homomorphic encryption
#!/usr/bin/env julia
// Attempt to implement simple homomorphic encryption
function generate_key(N=20)
key = rand(0:1, N)
while countnz(key) == 0 # on ne veut pas une cle avec 0 partout
key = rand(0:1, N)
end
return key
@jdelafon
jdelafon / hierholzer.py
Created June 29, 2016 11:45
Hierholzer's algorithm for Eulerian graphs
# Hierholzer's algorithm for Eulerian graphs #
"""
EXAMPLE
V = [1,2,3,4,5,6]
E = [(1,2),(2,3),(3,4),(4,5),(5,6),(6,1),(2,6),(6,4),(4,2)]
returns [1, 2, 6, 4, 2, 3, 4, 5, 6, 1]
V = ["AA","AB","BC","CD","DE","EF"]
E = [("AA","AB"),("AB","BC"),("BC","CD"),("CD","DE"),("DE","EF"),("EF","AA"),("AB","EF"),("EF","CD"),("CD","AB")]
@jdelafon
jdelafon / hamiltonian.py
Created June 29, 2016 11:47
Deep search algorithm to find an Hamiltonian path in a graph
# Deep search algorithm to find an Hamiltonian path in a graph
# (visiting every edge once and only once)
"""
EXAMPLES
V = ['AA','AB','BC','CD','DE','EF']
E = [('AA','AB'),('AB','BC'),('BC','CD'),('CD','DE'),('DE','EF'),('EF','AA'),('AB','EF'),('EF','CD'),('CD','AB')]
returns [['AA','AB','BC','CD','DE','EF'], ['AB','BC','CD','DE','EF','AA'], ['BC','CD','DE','EF','AA','AB'],
['CD','DE','EF','AA','AB','BC'], ['DE','EF','AA','AB','BC','CD'], ['EF','AA','AB','BC','CD','DE']]
@jdelafon
jdelafon / lsqnonneg.py
Last active August 17, 2024 09:56
A Python implementation of NNLS algorithm
"""
A Python implementation of NNLS algorithm
References:
[1] Lawson, C.L. and R.J. Hanson, Solving Least-Squares Problems, Prentice-Hall, Chapter 23, p. 161, 1974.
Based on MATLAB's lsqnonneg function
Edit: original authorship claimed by Stefano Palmieri, here is the source : https://github.com/stefanopalmieri/lsqnonneg.
Hence apparently GPL licensed : https://github.com/stefanopalmieri/lsqnonneg/blob/master/LICENSE
Feel free to verify the authorship, or just port yourself the nearly identical Matlab code with different variable names if licensing is an issue.
@jdelafon
jdelafon / gaussian_mixtures.r
Created June 29, 2016 11:50
Demonstration of Gaussian mixture models for bioinfo-fr
set.seed(100)
data = c(rnorm(1000,mean=2,sd=1),rnorm(400,mean=6,sd=1.5))
# linear
plot(sample(data), rep(0,length(data)), pch=4)
# with hist
hist(data, freq=FALSE, breaks=100)
points(sample(data), rep(0,length(data)), pch=4)
@jdelafon
jdelafon / receipes.scala
Last active July 3, 2017 12:44
Scala receipes
/**
* Return a Map[String,Any] of the field -> value pairs of the attributes of a class `c`.
*/
def classToMap(c: AnyRef): Map[String, Any] =
// 'fold' syntax, starting with an empty map
(Map[String, Any]() /: c.getClass.getDeclaredFields) {(a, f) =>
f.setAccessible(true)
a + (f.getName -> f.get(c))
}
@jdelafon
jdelafon / streaming.scala
Created July 5, 2016 08:31
Streaming a binary file in Scala Play 2.4.5
import java.io.FileInputStream
import java.nio.file.Paths
import play.api.http.HttpEntity
import akka.stream.scaladsl.StreamConverters
import play.api.mvc._
import play.api.http.MimeTypes._
import play.api.http.HeaderNames._
import play.api.http.Status._