Beispiel #1
0
def distribution():
    data = loader.loadAllAndClean("gpw_2007_list.txt")
    networks = analyze.createNetworksSeries(data, start_date=datetime.date(year=2005, month=1, day=3), end_date=datetime.date(year=2009, month=12, day=31))
    dates = pd.DatetimeIndex(networks.index)
    a = 0
    for i in dates:
        G = nx.Graph()
        if a < 40:
            distance = analyze.calculateDistances(data[a:a + 40])
        else:
            distance = analyze.calculateDistances(data[a-20:a+20])
        a +=1
        for n in distance.index:
            for j in distance.index:
                if n != j:
                    if G.has_edge(n,j):
                        continue
                    else:
                        G.add_edge(n,j,weight = distance[n][j])
        try:
            network2 = mst(G)
        except: ValueError

        degrees = [len(list(network2.neighbors(n))) for n in network2.nodes()]
        for r in degrees:
            for s in z.keys():
                if s == r:
                    z[s] +=1
            if r not in z.keys():
                z[r] = 1
Beispiel #2
0
def mol():
    data = loader.loadAllAndClean("gpw_2007_list.txt")
    networks = analyze.createNetworksSeries(data,
                                            start_date=datetime.date(year=2005,
                                                                     month=1,
                                                                     day=3),
                                            end_date=datetime.date(year=2009,
                                                                   month=12,
                                                                   day=31))
    dates = pd.DatetimeIndex(networks.index)
    a = 0
    for i in dates:
        G = nx.Graph()
        apex = []
        x.append(i)
        if a < 30:
            distance = analyze.calculateDistances(data[a:a + 30])
        else:
            distance = analyze.calculateDistances(data[a - 15:a + 15])
        a += 1
        for n in distance.index:
            for j in distance.index:
                if n != j:
                    if G.has_edge(n, j):
                        continue
                    else:
                        G.add_edge(n, j, weight=distance[n][j])
        try:
            network2 = mst(G)
            for l in nx.degree(network2):
                apex.append(l[0])
            q = max(nx.degree_centrality(network2).values())
            for key in nx.degree_centrality(network2).keys():
                if nx.degree_centrality(network2)[key] == q:
                    center = key
            way = 0
            for e in apex:
                way += len(nx.shortest_path(network2, center, e)) - 1
            mol = way // len(apex)
        except ValueError:
            mol = y[-1]
        y.append(mol)
Beispiel #3
0
def entropy():
    data = loader.loadAllAndClean("gpw_2007_list.txt")
    networks = analyze.createNetworksSeries(data,
                                            start_date=datetime.date(year=2005,
                                                                     month=1,
                                                                     day=1),
                                            end_date=datetime.date(year=2009,
                                                                   month=12,
                                                                   day=31))
    dates = pd.DatetimeIndex(networks.index)
    a = 0
    for i in dates:
        G = nx.Graph()
        apex = []
        x.append(i)
        if a < 30:
            distance = analyze.calculateDistances(data[a:a + 30])
        else:
            distance = analyze.calculateDistances(data[a - 15:a + 15])
        for n in distance.index:
            for j in distance.index:
                if n != j:
                    if G.has_edge(n, j):
                        continue
                    else:
                        G.add_edge(n, j, weight=distance[n][j])
        try:
            network2 = mst(G)
            for n in network2.nodes():
                for j in network2.nodes():
                    if n != j:
                        if distance[n][j] not in apex:
                            if distance[j][n] not in apex:
                                apex.append(distance[n][j])
            entrop = sts.entropy(
                apex
            )  #próbowałem policzyć to "ręcznie" - wychodzi tak samo, ale wartości oscylują w okolicy 0,5, więc tu jest bliżej tego co na wykładzie
        except ValueError:
            entrop = y[-1]
        y.append(entrop)
        a += 1
Beispiel #4
0
# helper function for smooth animation - calculates nodes positions in transition frames
def averageDict(d1: dict, d2: dict, alpha):
    return {n: (1 - alpha) * d1[n] + alpha * d2[n] for n in d1.keys()}


# reduces the graph
def mst(G):
    return nx.algorithms.tree.mst.minimum_spanning_tree(G)


# load the data from downloaded stooq files and construct correlation networks (for now, full networks, no mst applied)
data = loader.loadCountAndClean("gpw_2007_list.txt", 30)
networks = analyze.createNetworksSeries(data,
                                        start_date=datetime.date(year=2007,
                                                                 month=12,
                                                                 day=1),
                                        end_date=datetime.date(year=2008,
                                                               month=12,
                                                               day=1))
dates = pandas.DatetimeIndex(networks.index)

# prepare subplots for plotting. ax_graph is the area where we visualize the graph (top). ax_plot is the area for plotting how measured values change (bottow)
fig = plt.figure()
gs = matplotlib.gridspec.GridSpec(2, 1, height_ratios=[3, 1])
ax_graph = fig.add_subplot(gs[0])
ax_plot = fig.add_subplot(gs[1])

# here we will store measured values in every step
values = []

# this means, transition between two steps (two networks) takes 10 frames of animation