Skip to content

Instantly share code, notes, and snippets.

@chebee7i
chebee7i / apis.py
Created February 1, 2013 19:36
An implementation of mock modules for separating the NetworkX API by graph type.
"""
Module implemented restricted APIs based on graph types.
"""
_graph_types = set(['graph', 'multigraph', 'digraph', 'multidigraph'])
class MockModule(object):
"""
A mock module.
// Useful for students in need of block quotes for their paper, etc.
// Execute the line in your JavaScript console
new_window=window.open();new_window.document.body.innerHTML = $('iframe').contents().find('iframe').contents().find('body').get(1).innerHTML;
javascript:new_window=window.open();new_window.document.body.innerHTML = $('iframe').contents().find('iframe').contents().find('body').get(1).innerHTML;
(function() {
function uncrippleFrame() {
var body = this.contentWindow.document.getElementsByTagName('body')[0];
this.contentWindow.onclick = null;
body._frame = this;
body.onmousemove = function() {
this._frame.contentWindow.onclick = null;
this.setAttribute('style', '-webkit-user-select: auto;');
@chebee7i
chebee7i / line_graph.py
Last active December 21, 2015 04:48
Quick and dirty line graph construction for multidigraphs.
import networkx as nx
def line_graph(G, create_using=None):
# This code works only for multidigraphs
if create_using is None:
L = G.__class__()
else:
# Already instantiated.
L = create_using
@chebee7i
chebee7i / numpy_to_latex.py
Last active July 24, 2018 12:02 — forked from malloc47/numpy_to_latex.py
Code snippet to convert NumPy arrays to LaTeX arrays.
# This is free and unencumbered software released into the public domain.
# See UNLICENSE.
import numpy as np
import numpy.core.arrayprint as arrayprint
import contextlib
#
# cc-wiki
# http://stackoverflow.com/questions/2891790/pretty-printing-of-numpy-array
def rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toend=True):
"""Create a view of `array` which for every point gives the n-dimensional
neighbourhood of size window. New dimensions are added at the end of
`array` or after the corresponding original dimension.
Parameters
----------
array : array_like
Array to which the rolling window is applied.
window : int or tuple
@chebee7i
chebee7i / bayesian_networkx.py
Last active September 10, 2018 09:25
Some functions related to Bayesian networks.
"""
Some functions related to Bayesian networks (DAGs).
Most of this was pulled from:
http://www.mi.parisdescartes.fr/~nuel/bntworkshop2010/lecture3.pdf
"""
import networkx as nx
# original XML at http://www.w3.org/Math/characters/unicode.xml
# XSL for conversion: https://gist.github.com/798546
unicode_to_latex = {
u"\u0020": "\\space ",
u"\u0023": "\\#",
u"\u0024": "\\textdollar ",
u"\u0025": "\\%",
u"\u0026": "\\&",
/*
I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
so it's better encapsulated. Now you can have multiple random number generators
and they won't stomp all over eachother's state.
If you want to use this as a substitute for Math.random(), use the random()
method like so:
var m = new MersenneTwister();
@chebee7i
chebee7i / _cydefordereddict.pyx
Last active August 29, 2015 14:09
DefaultOrderedDict
from collections import Callable
from cyordereddict import OrderedDict
class DefaultOrderedDict(OrderedDict):
# Source: http://stackoverflow.com/a/6190500/562769
def __init__(self, default_factory=None, *a, **kw):
if (default_factory is not None and
not isinstance(default_factory, Callable)):
raise TypeError('first argument must be callable')
OrderedDict.__init__(self, *a, **kw)