Python 计算晶格中大小为l的窗口中的节点

Python 计算晶格中大小为l的窗口中的节点,python,python-3.x,numpy,graph,networkx,Python,Python 3.x,Numpy,Graph,Networkx,我有一个networkx晶格- G=nx.grid_2d_graph(5,5,periodic=True) 现在我想创建一个大小为l的窗口,其中l可以在晶格上变化,其中窗口的中心是晶格的中心 我的所有节点都有一个附加值。功能是- def node_status(self, node, time): r''' returns the status of a given node at a given time. :Arguments:

我有一个networkx晶格-

G=nx.grid_2d_graph(5,5,periodic=True)
现在我想创建一个大小为l的窗口,其中l可以在晶格上变化,其中窗口的中心是晶格的中心

我的所有节点都有一个附加值。功能是-

 def node_status(self, node, time):
        r'''
        returns the status of a given node at a given time.

        :Arguments:

        **node**
            the node
        **time** float
            the time of interest.

        :Returns:

        **status** string ('S', 'I', or 'R')
            status of node at time.
        '''
因此,我想计算大小为l的窗口中“R”的节点数。我可以通过调用窗口中每个节点上的函数来获取节点的状态

供参考-

完整代码-

import matplotlib.pyplot as plt
import networkx as nx
import EoN

G=nx.grid_2d_graph(5,5)
m=5

initial_infections = [print(type()) for (u,v) in G if u==int(m/2) and v==int(m/2)]

sim = EoN.basic_discrete_SIR(G,0.5,initial_infecteds = initial_infections,
               return_full_data=True, tmax = 25)

pos = {node:node for node in G}
sim.set_pos(pos)
sim.display(1, node_size = 40) #display time 6
plt.show()

def linear_distance(x1, x2, m):
    if x2>x1:
        return min(x2-x1, x1+m-x2)
    else:
        return min(x1-x2, x2+m-x1)

def in_window(node1, node2, m,L):
    D1 = linear_distance(node1[0], node2[0], m)
    D2 = linear_distance(node1[1], node2[1], m)
    max_distance = max(D1, D2)
    return max_distance <= L #True if in window

def count_nodes(G, center, m, L):
    nearby_R = [node for node in G if sim.node_status(node,1) is 'R' and in_window(node, center, m,L)]
    return len(nearby_R)

count_nodes(G,(2,2),5,3)
导入matplotlib.pyplot作为plt
将networkx导入为nx
导入EoN
G=nx.网格二维图(5,5)
m=5
初始值=如果u==int(m/2)和v==int(m/2),则G中(u,v)的[print(type())]
sim=EoN.基本离散SIR(G,0.5,初始感染=初始感染,
返回(完整数据=真,tmax=25)
pos={node:G}中节点的节点
模拟设置位置(pos)
模拟显示(1,节点大小=40)#显示时间6
plt.show()
def线性_距离(x1,x2,m):
如果x2>x1:
返回最小值(x2-x1,x1+m-x2)
其他:
返回最小值(x1-x2,x2+m-x1)
def在_窗口中(节点1、节点2、m、L):
D1=线性距离(节点1[0],节点2[0],m)
D2=线性距离(节点1[1],节点2[1],m)
最大距离=最大(D1,D2)

return max_distance挑战可能是计算节点是否在中心的给定距离内,特别是因为它是周期性的。因此,我定义了一个函数
linear\u distance
,它检查一维距离,考虑到周期性。然后我在_window
中定义了一个函数
,它使用
线性_距离
来判断节点是否在给定的窗口内。最后,我创建了一个列表,其中包括窗口中具有所需状态的所有节点。您需要根据需要更新命令
状态(节点)

def linear_distance(x1, x2, m):
    if x2>x1:
        return min(x2-x1, x1+m-x2)
    else:
        return min(x1-x2, x2+m-x1)

def in_window(node1, node2, m,L):
    D1 = linear_distance(node1[0], node2[0], m)
    D2 = linear_distance(node1[1], node2[1], m)
    max_distance = max(D1, D2)
    return max_distance <= L #True if in window

def count_nodes(G, center, m, L):
    nearby_R = [node for node in G if status(node) is 'R' and in_window(node, center, m,L)]
    return len(nearby_R)
def线性距离(x1,x2,m):
如果x2>x1:
返回最小值(x2-x1,x1+m-x2)
其他:
返回最小值(x1-x2,x2+m-x1)
def在_窗口中(节点1、节点2、m、L):
D1=线性距离(节点1[0],节点2[0],m)
D2=线性距离(节点1[1],节点2[1],m)
最大距离=最大(D1,D2)
返回最大距离