Skip to content

Instantly share code, notes, and snippets.

View disa-mhembere's full-sized avatar

Disa Mhembere disa-mhembere

View GitHub Profile
@disa-mhembere
disa-mhembere / nmf.R
Created March 13, 2015 21:49
simple nmf implementation
#!/usr/bin/Rscript
frobenius.norm <- function(V, W, H) {
sqrt(sum((V - (W%*%H))^2)/prod(dim(V)))
}
nmf <- function (V, r, H=abs(replicate(dim(V)[2], rnorm(r))),
W=abs(replicate(r, rnorm(dim(V)[1]))), max.iters=1000) {
set.seed(12345)
converged <- FALSE
@disa-mhembere
disa-mhembere / scriptcreate
Created January 16, 2015 21:17
createfiles
#!/usr/bin/python
# Disa Mhembere
# Make me a new file with a nice comment block and optinal
# things like a main, and argparser
# This is free software and comes with not guarantee of correct
# functionality. Use at your own peril
import argparse
import os
@disa-mhembere
disa-mhembere / get_degree.py
Created December 8, 2014 18:42
Get the degree of the kki dataset
import mrcap.utils.igraph_io as igraph_io
import igraph
from glob import glob
import cPickle as pickle
import os
def get_dist(directory):
deg_map = {}
for fn in glob(os.path.join(directory, "*.zip")):
g = igraph_io.read_arbitrary(fn, informat="graphml")
@disa-mhembere
disa-mhembere / mm.c
Last active August 29, 2015 14:03
Iterate all edges and src, target and weight
#include <igraph.h>
#include <stdio.h>
void custom_warning_handler (const char *reason, const char *file,
int line, int igraph_errno) {
printf("Warning: %s\n", reason);
}
int main(int argc, char* argv[])
{
@disa-mhembere
disa-mhembere / zip_and_output.py
Created June 17, 2014 21:27
Zip and output with a changed name
import subprocess
import os
for _file_ in os.listdir("."):
if not _file_.startswith(".") and not _file_.endswith("py"):
subprocess.call(["zip", "-v", _file_+".zip", _file_])
print "Deleting,", _file_
os.remove(_file_)
@disa-mhembere
disa-mhembere / simple_attr.py
Created June 17, 2014 21:26
Add a simple attribute to a graphml big graph
import os
import sys
value = sys.argv[1]
for _file_ in os.listdir("."):
if not _file_.startswith(".") and not _file_.endswith("py"):
print "Attempting to read and split %s ..." % _file_
f = open(_file_, "r")
print "Renaming uncompressed"
@disa-mhembere
disa-mhembere / get.clusters.R
Created March 24, 2014 16:22
Get components in a graph
require(igraph)
g <- read.graph("./web-Google.txt") # Change fn
cl <- clusters(g, "strong") # "weak"
unique.clusters <- unique(cl$csize)
for (i in 1: length(unique.clusters) ) {
cat(unique.clusters[i], length(which(cl$csize == unique.clusters[i])),"\n")
}
@disa-mhembere
disa-mhembere / makefly.R
Created March 8, 2014 22:02
Make fly graph
require(igraph)
dat <- read.table("./fly.csv", header=TRUE, sep=",")
dat <- dat[c("presynaptic", "postsynaptic" , "pre.x", "post.x", "pre.y", "post.y", "pre.z", "post.z","proofreading.details")]
g <- graph.data.frame(dat, directed=TRUE)
g <- set.graph.attribute(g, "source", value="http://www.nature.com/nature/journal/v500/n7461/full/nature12450.html")
g <- set.graph.attribute(g, "info", value="source=presynaptic, targe=postsynaptic")
write.graph(g, "drosophila_retina.graphml", format="graphml")
@disa-mhembere
disa-mhembere / get_eigs.R
Last active December 29, 2015 06:29
Compute top eigs
require(Matrix)
require(igraph)
require(argparse)
parser <- ArgumentParser(description="Process some integers")
parser$add_argument("gfn", help="The graph file name")
parser$add_argument("nev", type="integer", help="Number of eigenvectors to compute")
parser$add_argument("ncv", type="integer",help="Number of Lanczos vectors to compute")
parser$add_argument("maxiter", type="integer",help="Max number of iterations to compute eigenvectors")
@disa-mhembere
disa-mhembere / create_graph.py
Created November 21, 2013 21:38
create your own ER graph using networkx
import networkx as nx
import numpy as np
p = 0.3 # Alter as necessary
n = 1000 # Alter as necessary
fn = "graph%d"%n # Alter as necessary
g = nx.erdos_renyi_graph(n, p,False)
gsp = nx.to_scipy_sparse_matrix(g)