Skip to content

Instantly share code, notes, and snippets.

View jg-you's full-sized avatar
🐢

Jean-Gabriel Young jg-you

🐢
View GitHub Profile
@jg-you
jg-you / HDF5VL.cpp
Last active August 29, 2015 14:17
HDF5, C interface: From jagged array to variable lenght
// Test data
std::vector< std::vector<int> > jagged_array(3);
jagged_array[0] = {0};
jagged_array[1] = {0, 1, 2, 3};
jagged_array[2] = {0, 1, 2};
hvl_t * X = (hvl_t *)malloc(jagged_array.size() * sizeof(hvl_t))
for (unsigned int i = 0; i < jagged_array.size(); ++i) {
X[i].len = jagged_array[i].size();
int * ptr = (int *) malloc (X[i].len * sizeof(int));
@jg-you
jg-you / degree_of_neighbors.py
Created April 13, 2015 20:18
NetworkX: Degree of neighbors, by degree
import networkx as nx
# Mean function (no statistics module in python2)
mean = lambda l: sum(l)/len(l)
# Declare some graph
G = nx.erdos_renyi_graph(200,0.1)
# Prepare raw results container
result = dict()
@jg-you
jg-you / dirichlet_process.r
Last active August 29, 2015 14:20
Finite Dirichlet process with N(0,1)
diri <- function(alpha) {
# Sample from the Dirichlet distribution with parameter (vector) alpha
k <- length (alpha)
Z <- rep(0,k)
for(i in 1:k) {
Z[i] <- rgamma(n=1,shape=alpha[i], rate =1)
S <- sum(Z)
P <- Z/S
}
return(P)
@jg-you
jg-you / main_master_slave.cpp
Last active October 15, 2020 02:13
Master / Slave example with boost::mpi
// STL
#include <cstdlib>
#include <iostream>
// boost::mpi
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/mpi/status.hpp>
namespace mpi = boost::mpi;
// Definitions
#define NUMBER_OF_JOBS 12
@jg-you
jg-you / supersets.py
Last active August 29, 2015 14:21
Find all supersets of a collection
collection = [{1, 2, 3, 4, 5},
{1, 2},
{1, 2, 3, 4, 5, 6},
{3, 4, 8},
{3, 4, 11},
{3},
{3},
{12},
{1, 2, 3, 4, 5, 6},
{1, 2, 3, 4, 7, 9},
@jg-you
jg-you / extraction.py
Last active October 14, 2015 23:35
Extract parts of a pdf file as a png (dirty hack)
#!/usr/bin/env python3
# Author: Jean-Gabriel Young
# Email: [email protected]
# -*- coding: utf-8 -*-
import argparse
import subprocess
import os
from PIL import Image
@jg-you
jg-you / mdown_toc.py
Last active July 7, 2020 06:05
Generate TOC for a markdown file
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @author: Jean-Gabriel Young <[email protected]>
"""Generate TOC for a markdown file."""
import re
# Match between 1 and 4 #
section = re.compile('^\s*(#){1,4}\s?')
strip_url = re.compile('[\W_]+', re.UNICODE)
@jg-you
jg-you / draw_nx_beautiful.py
Last active October 30, 2022 00:04
Beautiful networkx graph
import copy
import networkx
import matplotlib.pyplot as plt
# Generate a graph.
# Here I chose an ER graph.
g = nx.erdos_renyi_graph(20, 0.3)
# Get positions.
# Here I use the spectral layout and add a little bit of noise.
@jg-you
jg-you / draw_graphon.py
Created August 25, 2016 21:02
Draw graphon Stochastic Block Models
import matplotlibt.pyplot as plt
import numpy as np
plt.figure(figsize=(5,4))
X, Y = np.meshgrid(np.linspace(0,1), np.linspace(0,1))
plt.pcolormesh(X,Y,graphon_val(X,Y,p,n))
plt.colorbar()
@jg-you
jg-you / inline_gt_fig.py
Last active July 19, 2017 15:56
Inline graph-tool figures in jupyter-notebook
# The newest version of graph-tool plots directly in a gtk window, by default.
# The following allow you to add inline plots in a jupyter-notebook (this was previously trivial).
# [The following code must appear in a notebook, obviously]
import graph_tool as gt
import graph_tool.draw
import graph_tool.collection
import matplotlib.pyplot as plt