Exemplo n.º 1
0
from compas.datastructures import Network


HERE = os.path.dirname(__file__)
FILE = os.path.join(HERE, 'clusters.json')

network = Network()
network.update_default_node_attributes({'cluster': None, 'base': False})

cloud = Pointcloud.from_bounds(10, 5, 3, 100)
kmeans = KMeans(n_clusters=10, n_init=500, max_iter=100).fit(array(cloud, dtype=float))

clusters = {}
for i, point in zip(kmeans.labels_, cloud):
    print(i)
    if i not in clusters:
        clusters[i] = []
    clusters[i].append(point)

for index in clusters:
    nodes = []
    for point in clusters[index]:
        node = network.add_node(x=point[0], y=point[1], z=point[2], cluster=index)
        nodes.append(node)
    x, y, z = centroid_points(clusters[index])
    base = network.add_node(x=x, y=y, z=z, cluster=index, base=True)
    for node in nodes:
        network.add_edge(base, node)

network.to_json(FILE)
Exemplo n.º 2
0
        self.value = value or random.choice(range(20))

    @property
    def data(self):
        return {'value': self.value}

    @data.setter
    def data(self, data):
        self.value = data['value']


network = Network()

last_node = None
for i in range(12):
    node = network.add_node(x=i // 3, y=i % 3, z=0)
    network.node_attribute(node, 'weight', Weight())

    if last_node:
        network.add_edge(node, i - 1)
    last_node = node

print(network.summary())
# print(network.to_data())

network.to_json(__file__ + '.json')

network2 = Network.from_json(__file__ + '.json')
print(network2.summary())
# print(network2.to_data())