Skip to content

Instantly share code, notes, and snippets.

View wanirepo's full-sized avatar

Choong-Wan Woo wanirepo

View GitHub Profile
from os import listdir
from os.path import isfile, join
import multiprocessing
import networkx as nx
from sklearn.utils.graph import single_source_shortest_path_length as sssp
fb_folder = "just8"
def func_sssp(component):
@wanirepo
wanirepo / PS#1 6a-b
Created September 9, 2014 21:32
Network analysis PS#1, 6a, 6b
datdir = '/Users/clinpsywoo/Documents/2011-2016yr/2014-2015(4th_GS)/Network_modeling/Problem_sets/FB100 dataset/facebook100';
datfiles = filenames(fullfile(datdir, '*mat'), 'absolute');
for i = 1:numel(datfiles)
fprintf('\nWorking on %02d/%02d...\t', i, numel(datfiles));
tic;
load(datfiles{i});
@wanirepo
wanirepo / PS#1 6a-b
Last active August 29, 2015 14:06
Network analysis PS#1 6a, 6b
datdir = '/Users/clinpsywoo/Documents/2011-2016yr/2014-2015(4th_GS)/Network_modeling/Problem_sets/FB100 dataset/facebook100';
datfiles = filenames(fullfile(datdir, '*mat'), 'absolute');
for i = 1:numel(datfiles)
fprintf('\nWorking on %02d/%02d...\t', i, numel(datfiles));
tic;
load(datfiles{i});
@wanirepo
wanirepo / wani_example_choose_cluster_make_mask.m
Created September 11, 2014 15:19
Example of choose cluster and make mask
%% make a mask for MPFC
cl = region(which('atlas_labels_combined.img'), 'unique_mask_values'); %anat_lbpa_thal.img'); %% 'lpba40.spm5.avg152T1.label.nii');
cluster_orthviews(cl, 'unique');
clout = [];
% choose MPFC clusters
[clout,cl] = cluster_graphic_select(cl,clout);
@wanirepo
wanirepo / build_adjacency_from_list.m
Last active August 29, 2015 14:07
matlab function to build a adjacency matrix from a xy list
function A = build_adjacency_from_list(ij)
% function A = build_adjacency_from_list(ij)
%
% ij: row, column
A = zeros(max(max(ij)),max(max(ij)));
for i = 1:length(ij)
A(ij(i,1), ij(i,2)) = 1;
@wanirepo
wanirepo / community_modularity.m
Last active August 29, 2015 14:07
This function conduct the greedy agglomerative algorithm to find community structure that maximizes the network's modularity (Q)
function [max_z, max_q, outinfo] = community_modularity(A, varargin)
% function [z, q, outinfo] = community_modularity(A, optional_inputs)
%
% feature: This function conduct the greedy agglomerative algorithm to find
% community structure that maximizes the network's modularity (Q).
%
% input: A adjacency matrix
%
% output: max_z a vector of community membership that maximize Q
@wanirepo
wanirepo / norm_mutual_info.m
Created October 3, 2014 22:48
This function calculates the normalized mutual information. Reference: Danon et al. (2005), Karrer et al. (2008)
function nmi = norm_mutual_info(za, zb)
% function nmi = norm_mutual_info(za, zb)
%
% feature: This function calculates the normalized mutual information,
% which tells us how much information we learn about Community
% structure A (or B) if we know B (or A). If A and B are
% identical, we learn everything about A from B (or B from A).
% In this case, nmi returns 1. If they are entirely uncorrelated,
% we learn nothing. In this case, nmi returns 0.
@wanirepo
wanirepo / PS03_Woo_facebook.m
Created October 5, 2014 04:11
Network modeling, Problem Set #03-4.
% Data info:
% Each of the school .mat files has an A matrix (sparse) and a
% "local_info" variable, one row per node: a student/faculty status
% flag, gender, major, second major/minor (if applicable), dorm/house,
% year, and high school. Missing data is coded 0.
% datdir = '/Users/clinpsywoo/Documents/2011-2016yr/2014_2015_4th_GS/Network_modeling/Problem_sets/facebook100';
datdir = '/Volumes/engram/Users/wani/Documents/Network_modeling/Problem_sets/facebook100';
fnames = filenames(fullfile(datdir, '*mat'));
@wanirepo
wanirepo / SBM_likelihood.m
Created October 21, 2014 16:31
log-likelihood estimation for simple Stochastic Block Model (SBM) (undirected, unweighted) based on Maximum Likelihood estimation.
function L = SBM_likelihood(A, z, varargin)
% usage: L = SBM_likelihood(A, z)
%
% feature: log-likelihood estimation for simple Stochastic Block Model (SBM)
% (undirected, unweighted) based on Maximum Likelihood estimation.
%
% input: A adjacency matrix (simple graph)
% z group membership
%
@wanirepo
wanirepo / KL_heuristic_k2.m
Last active August 29, 2015 14:07
Uses the Kernighan-Lin (KL) heuristic to optimize any partition score function.
function [bestL, bestP, info] = KL_heuristic_k2(A, varargin)
% usage: [bestL, bestP, info] = KL_heuristic_k2(A, varargin)
%
% feature: use the Kernighan-Lin (KL) heuristic to optimize any partition
% score function, e.g., modularity Q or stochastic block model's
% likelihood function. This works only for 2 partitioning problem.
%
% input: A adjacency matrix
%