E. Network datastructure
1. COMPAS Network Constructor
COMPAS Network is a connectivity graph that encodes the relationships between nodes
and edges
, which is a useful data structure to represent elements in a cable-net.
1.a: construct Network by addnode
, addedge
addnode
, addedge
The simplest way to build a network is by using Network.addnode
and Network.addedge
.
Network summary
nodes: 32
edges: 43
1.b: construct Network by from_node_and_edges
from_node_and_edges
With the nodes
and edges
list from the last session, a Network could also be constructed by using Network.from_nodes_and_edges()
.
2. NetworkPlotter
A Network could be visualized in Plotter using NetworkPlotter.
3. COMPAS Network Data-structure
So, what information is contained in the cable-net Network?
{
"compas": "0.18.0",
"data": {
"adjacency": { "0": { "1": null, "6": null }, "1": { "0": null, "2": null, "7": null }, "10": { "11": null, "16": null, "4": null, "9": null }, "11": { "10": null, "17": null, "5": null }, "12": { "13": null, "18": null, "6": null }, "13": { "12": null, "14": null, "19": null, "7": null }, "14": { "13": null, "15": null, "20": null, "8": null }, "15": { "14": null, "16": null, "21": null, "9": null }, "16": { "10": null, "15": null, "17": null, "22": null }, "17": { "11": null, "16": null, "23": null }, "18": { "12": null, "19": null, "24": null }, "19": { "13": null, "18": null, "20": null, "25": null }, "2": { "1": null, "3": null, "8": null }, "20": { "14": null, "19": null, "21": null, "26": null }, "21": { "15": null, "20": null, "22": null, "27": null }, "22": { "16": null, "21": null, "23": null, "28": null }, "23": { "17": null, "22": null, "29": null }, "24": { "18": null, "25": null, "30": null }, "25": { "19": null, "24": null, "26": null, "31": null }, "26": { "20": null, "25": null, "27": null, "32": null }, "27": { "21": null, "26": null, "28": null, "33": null }, "28": { "22": null, "27": null, "29": null, "34": null }, "29": { "23": null, "28": null, "35": null }, "3": { "2": null, "4": null, "9": null }, "30": { "24": null, "31": null }, "31": { "25": null, "30": null, "32": null }, "32": { "26": null, "31": null, "33": null }, "33": { "27": null, "32": null, "34": null }, "34": { "28": null, "33": null, "35": null }, "35": { "29": null, "34": null }, "4": { "10": null, "3": null, "5": null }, "5": { "11": null, "4": null }, "6": { "0": null, "12": null, "7": null }, "7": { "1": null, "13": null, "6": null, "8": null }, "8": { "14": null, "2": null, "7": null, "9": null }, "9": { "10": null, "15": null, "3": null, "8": null } },
"attributes": { "name": "Network" },
"edge": { "0": { "1": {}, "6": {} }, "1": { "2": {}, "7": {} }, "10": { "11": {}, "16": {} }, "11": { "17": {} }, "12": { "13": {}, "18": {} }, "13": { "14": {}, "19": {} }, "14": { "15": {}, "20": {} }, "15": { "16": {}, "21": {} }, "16": { "17": {}, "22": {} }, "17": { "23": {} }, "18": { "19": {}, "24": {} }, "19": { "20": {}, "25": {} }, "2": { "3": {}, "8": {} }, "20": { "21": {}, "26": {} }, "21": { "22": {}, "27": {} }, "22": { "23": {}, "28": {} }, "23": { "29": {} }, "24": { "25": {}, "30": {} }, "25": { "26": {}, "31": {} }, "26": { "27": {}, "32": {} }, "27": { "28": {}, "33": {} }, "28": { "29": {}, "34": {} }, "29": { "35": {} }, "3": { "4": {}, "9": {} }, "30": { "31": {} }, "31": { "32": {} }, "32": { "33": {} }, "33": { "34": {} }, "34": { "35": {} }, "35": {}, "4": { "10": {}, "5": {} }, "5": { "11": {} }, "6": { "12": {}, "7": {} }, "7": { "13": {}, "8": {} }, "8": { "14": {}, "9": {} }, "9": { "10": {}, "15": {} } },
"edge_attributes": {},
"max_int_key": 35,
"node": { "0": { "x": 0, "y": 0, "z": 0 }, "1": { "x": 0, "y": 10, "z": 0 }, "10": { "x": 10, "y": 40, "z": 0 }, "11": { "x": 10, "y": 50, "z": 0 }, "12": { "x": 20, "y": 0, "z": 0 }, "13": { "x": 20, "y": 10, "z": 0 }, "14": { "x": 20, "y": 20, "z": 0 }, "15": { "x": 20, "y": 30, "z": 0 }, "16": { "x": 20, "y": 40, "z": 0 }, "17": { "x": 20, "y": 50, "z": 0 }, "18": { "x": 30, "y": 0, "z": 0 }, "19": { "x": 30, "y": 10, "z": 0 }, "2": { "x": 0, "y": 20, "z": 0 }, "20": { "x": 30, "y": 20, "z": 0 }, "21": { "x": 30, "y": 30, "z": 0 }, "22": { "x": 30, "y": 40, "z": 0 }, "23": { "x": 30, "y": 50, "z": 0 }, "24": { "x": 40, "y": 0, "z": 0 }, "25": { "x": 40, "y": 10, "z": 0 }, "26": { "x": 40, "y": 20, "z": 0 }, "27": { "x": 40, "y": 30, "z": 0 }, "28": { "x": 40, "y": 40, "z": 0 }, "29": { "x": 40, "y": 50, "z": 0 }, "3": { "x": 0, "y": 30, "z": 0 }, "30": { "x": 50, "y": 0, "z": 0 }, "31": { "x": 50, "y": 10, "z": 0 }, "32": { "x": 50, "y": 20, "z": 0 }, "33": { "x": 50, "y": 30, "z": 0 }, "34": { "x": 50, "y": 40, "z": 0 }, "35": { "x": 50, "y": 50, "z": 0 }, "4": { "x": 0, "y": 40, "z": 0 }, "5": { "x": 0, "y": 50, "z": 0 }, "6": { "x": 10, "y": 0, "z": 0 }, "7": { "x": 10, "y": 10, "z": 0 }, "8": { "x": 10, "y": 20, "z": 0 }, "9": { "x": 10, "y": 30, "z": 0 } },
"node_attributes": { "x": 0.0, "y": 0.0, "z": 0.0 }
},
"datatype": "compas.datastructures/Network"
}
Print the network would output the data of the Network, which is useful for serialization. It contains the following data:
compas
data
adjacency
attributes
edge
edge_attributes
max_int_key
node
node_attributes
datatype
It includes the compas version
, under which is the Network created. datatype
shows the data type of the data-structure, now it's a Network. adjacency
and edges
are both dictionaries of dictionaries, which is similar to the node_neighbors
dictionary we have learned in the last session. The keys are node index. In edges
, the dictionary values are dictionaries of edge attribute, for example: cablenet.edge[1][2] -> {...}
. Whereasadjacency
shows the undirected connectivity information. edge_attributes
is a dictionary mapping edge attribute names to their default values. attributes
is a dictionary of miscellaneous information. max_int_key
is the max number of the key index. node
is a dictionary, where each key represents a node of the Network and maps to a dictionary of node attributes. node_attribute
is a dictionary and by default is the xyz coordinates of the node.
4. Network Nodes, Edges
Network.nodes()
and Network.edges()
would return generator objects containing key, the identifier of the node, and key pairs. A generator is very similar to list, which can both be iterated by a for
loop, but a generator is more time- and space-efficient compared to a list. Network.node_coordinates(key)
would return a list containing xyz coordinates of the node.
5. Network methods
Network
contains useful geometrical and topological methods.
5.a: Ex: Highlight nodes and edges
Question:
Highlight node 20 as well as its neighbors. Visualize them with a plotter.
Answer:
[14 19 26 21]
5.b: Ex: Update node
Question:
Move node 20 along positive x-axis 1 unit and along positive y-axis 2 unit. Update the cable-net.
Answer:
5.c: Ex: Calculate cable length
Question:
Calculate the length of the cables, which are connected with node 20.
Answer:
Cable between node 14 and node 12 is 11.18 in length.
Cable between node 19 and node 12 is 12.04 in length.
Cable between node 26 and node 12 is 9.22 in length.
Cable between node 21 and node 12 is 8.06 in length.
5.d: Ex: Find the node and neighborhood
Question:
You know a node whose xyz coordinates are [20, 10, 0]
. Find its index key and highlights 2 rings of neighbor nodes away from the node.
Answer:
{'0.000,0.000,0.000': 0, '0.000,10.000,0.000': 1, '0.000,20.000,0.000': 2, '0.000,30.000,0.000': 3, '0.000,40.000,0.000': 4, '0.000,50.000,0.000': 5, '10.000,0.000,0.000': 6, '10.000,10.000,0.000': 7, '10.000,20.000,0.000': 8, '10.000,30.000,0.000': 9, '10.000,40.000,0.000': 10, '10.000,50.000,0.000': 11, '20.000,0.000,0.000': 12, '20.000,10.000,0.000': 13, '20.000,20.000,0.000': 14, '20.000,30.000,0.000': 15, '20.000,40.000,0.000': 16, '20.000,50.000,0.000': 17, '30.000,0.000,0.000': 18, '30.000,10.000,0.000': 19, '31.000,22.000,0.000': 20, '30.000,30.000,0.000': 21, '30.000,40.000,0.000': 22, '30.000,50.000,0.000': 23, '40.000,0.000,0.000': 24, '40.000,10.000,0.000': 25, '40.000,20.000,0.000': 26, '40.000,30.000,0.000': 27, '40.000,40.000,0.000': 28, '40.000,50.000,0.000': 29, '50.000,0.000,0.000': 30, '50.000,10.000,0.000': 31, '50.000,20.000,0.000': 32, '50.000,30.000,0.000': 33, '50.000,40.000,0.000': 34, '50.000,50.000,0.000': 35}
Network.gkey_key([precision])
returns a dictionary that maps geometric keys of a certain precision to the keys of the corresponding nodes. The type of geometric keys is a string. Thus, the unknown node's xyz coordinates firstly need to be converted to a string so as to be used as a dictionary key.
29
6. Serialization
All COMPAS data-structures and geometries can be serialized to JSON
format, and unserialized from such a representation to reconstruct an equivalent object without loss of information.
6.a: Network Serialization
Network.to_json(path)
would serialize the Network to a JSON
file. Here, a new file called cablenet.json
should be created in the data
folder in the same directory of the python script.
6.b: Network unserialization
Network.from_json(path)
would unserialize the Network
.
The sequence of items in the dictionary might change during the serialization and unserialization. This is because the dictionary stores key-value pairs and doesn't keep the keys' order, which makes look-up faster.
7. Load and Visualize the Cablenet in Rhino
Now load cablenet.json
in Rhino and visualize it with compas_rhino.artists.NetworkArtist
.
Last updated