def __init__(self, adherent_gml, nonadherent_gml, out_dir): self.G = nx.read_gml(str(adherent_gml)) self.G = transform_labels_to_nx(self.G) self.T = nx.read_gml(str(nonadherent_gml)) self.T = transform_labels_to_nx(self.T) self.dir = out_dir print "'self.dir", self.dir self.header = ["id","ck_id","initial_weight","weigh_ins",\ "activity","weight_change","percentage_weight_change","time_in_system",\ "age","height", "initial_bmi", "final_bmi"] self.look_up = look_up_table() self.activity = activity_table() self.adherent = self.G.copy() self.nonadherent = self.T.copy() print "adherent *gml file", len(self.G) selfloops = self.adherent.selfloop_edges() for u in selfloops: self.G.remove_edge(u[0], u[1]) for n in self.G.nodes(): if float(self.adherent.node[n]["initial_weight"])>500 or float(self.adherent.node[n]["initial_bmi"])>70 or\ self.adherent.node[n]["initial_weight"]<100 or float(self.adherent.node[n]["initial_bmi"])<15: self.adherent.remove_node(n) print "nonadherent *gml file", len(self.T) selfloops = self.nonadherent.selfloop_edges() for u in selfloops: self.T.remove_edge(u[0], u[1]) for n in self.T.nodes(): if float(self.nonadherent.node[n]["initial_weight"])>500 or float(self.nonadherent.node[n]["initial_bmi"])>70 or\ self.nonadherent.node[n]["initial_weight"]<100 or float(self.nonadherent.node[n]["initial_bmi"])<15: self.nonadherent.remove_node(n) print "size of the cleaned up nonadherent *gml file", len( self.nonadherent) nx.write_gml(self.adherent, str(self.dir) + "/networks/nethod2_adherent.gml") nx.write_gml(self.nonadherent, str(self.dir) + "/networks/nethod2_nonadherent.gml")
def _sc_graph(self): """function to create a graph for the small component nodes""" #sc = set(self.G.nodes()) - set(self.Gcc.nodes()) print "number of small component nodes should be 316", len(self.sc) look_up = look_up_table() uids = [look_up["ck_id"][n] for n in map(int,list(self.sc))] print "uids should be 316", len(uids) obj = CKGraph() self.SC_graph = obj.build_undirected_graph(uids = uids) return self.SC_graph[0]
def test_means(alpha, filename): filename = str(filename) x = [x.strip().split(";") for x in open(filename)\ .readlines()] x = sum(x, []) x = [x.split(",") for x in x] print "X", x #This is a list of labels, which need to be converted to a list of real user ids comlist = [[int(y) for y in x] for x in x] print "size of communities", comlist look_up = look_up_table() comlist_values = [[look_up["percentage_weight_change"][int(n)] for n in x] for x in comlist] avg_weightloss_per_com = [] for n in comlist_values: n = map(float, n) avg_weightloss_per_com.append(mean(n)) print "avg percentage weight change for communities", avg_weightloss_per_com #print "number of values",map(len,comlist_values) communitypairs = itertools.combinations(comlist_values, 2) p_values = [] for com in communitypairs: D, p = ks_2samp(com[0], com[1]) p_values.append(p) counter = 0 for i in p_values: if float(i) < alpha: counter = counter + 1 print "number of times that we can reject the null hypothesis that the two distributions are equivalent", counter overall_pvalue = counter / float(len(p_values)) print "confidence level that distributions are equivalent", 1 - overall_pvalue
def __init__(self, in_gml, out_dir): self.G = nx.read_gml(str(in_gml)) self.G = transform_labels_to_nx(self.G) self.dir = out_dir print "'self.dir", self.dir self.header = ["id","ck_id","initial_weight","weigh_ins",\ "activity","weight_change","percentage_weight_change","time_in_system",\ "age","height", "initial_bmi", "final_bmi"] self.look_up = look_up_table() self.activity = activity_table() self.H = self.G.copy() print "input *gml file", len(self.G) selfloops = self.H.selfloop_edges() for u in selfloops: self.H.remove_edge(u[0], u[1]) for n in self.H.nodes(): if float(self.H.node[n]["initial_weight"])>500 or float(self.H.node[n]["initial_bmi"])>70 or\ self.H.node[n]["initial_weight"]<100 or float(self.H.node[n]["initial_bmi"])<15: self.H.remove_node(n) print "size of the cleaned up *gml file", len(self.H) nx.write_gml(self.G, str(self.dir) + "/networks/engaged_users_network.gml") self.Gcc = nx.connected_component_subgraphs(self.H)[0] print "size of the giant component", len(self.Gcc) self.sc = set(self.H.nodes()) - set(self.Gcc.nodes()) print "number of small components", len(self.sc)
from look_up_table import * import itertools activity = activity_table() look_up = look_up_table() x = [x.strip().split(" ,")[0] for x in \ open("./method3/csv/nonadherent_not_networked_pwl.csv").readlines()] y = [y.strip().split(" ,")[0] for y in \ open("./method3/csv/adherent_not_networked_pwl.csv").readlines()] print "check adherent", len(x) print "check nonadherent", len(y) ius = list(itertools.chain(*[x, y])) print ius ius = map(int, ius) f = open("./method3/csv/engaged_not_networked_act_wi_ibmi.csv", "w") print >> f, ",".join(["pwc", "act", "wi", "ibmi", "time_in_sys"]) ov_ob = [] #select overweight and obese ius for n in ius: if float(look_up["initial_BMI"][n]) >= 25: ov_ob.append(n) for n in ov_ob: t = (look_up["percentage_weight_change"][n], activity["activity"][n], look_up["weigh_ins"][n], look_up["initial_BMI"][n],
from transform_labels_to_nx import * import networkx as nx import statistics from look_up_table import * from bootstrap2 import bootstrap from bootstrapfrac import * from bootstrapmeans import * """ Created by Rufaro Mukogo on 2011-05-23. Copyright (c) 2011 __Northwestern University__. All rights reserved. This script is used to calculate the values of figure 2 in the paper. """ data = look_up_table() dict = look_up_table() def mean_value(list,dict=dict, property ="percentage_weight_change"): count = 0.0 for n in list: try: if abs(float(dict[property][int(n)])<50.0): count += float(dict[property][int(n)]) #print count else: pass except ValueError: pass try:
self.look_up_table self._annotate() if __name__ == "__main__": try: import psycho pyscho.full() except: ImportError if len(sys.argv) > 1: in_gml = sys.argv[1] if len(sys.argv) > 2: out_gml = sys.argv[2] if len(sys.argv) > 3: metric = sys.argv[3] print "in_gml", in_gml print "out_gml", out_gml print "metric", metric look_up_table = look_up_table() obj = more_annotations(in_gml, out_gml, metric, look_up_table) obj._annotate() obj.main()
import scipy.stats as stat from math import * import random, scipy from random import gauss, randint from look_up_table import * dict = look_up_table() class bootstrap: def __init__(self,data,n): self.data = data self.n = n def _average(self,X): return sum(X)/float(len(X)) def mean(self,X): return sum(X)/float(len(X)) def sample_wr(self,population, k): """Chooses k random elements (with replacement) from a population""" n = len(population) _random, _int = random.random, int result = [None] * k for i in xrange(k): j = _int(_random() * n) result[i] = population[j] return result