Working with networks¶
Writing files¶
Every network — downloaded or generated, first-order, memory or multilayer —
writes itself into a directory and returns the path, named after the network
unless you pass name=:
net.to_pajek("out/") # PosixPath('out/ca-GrQc.net')
net.to_link_list("out/", name="grqc") # PosixPath('out/grqc.txt')
Network |
|
|
|---|---|---|
first-order ( |
|
|
|
|
order 1 only |
|
|
— |
Integer node ids are written as they are, so a generated network’s file lines up
with its planted partition; other labels are numbered from 1 and kept as Pajek
names. A downloaded network’s nodes take the source’s own names where they
come in the same download: Netzschleuder’s name, label or nodeLabel column
and KONECT’s ent.* files. A node without a name shows its id, and
to_pajek("out/", names={}) writes ids alone. A node without links is
written too: every node of a generated network, and every node a downloaded
network’s source lists in its node table (nodes()), so to_pajek,
to_networkx and to_infomap hold the nodes the node table holds. A link
list has nowhere to put such a node. Nor has it anywhere to put names, so it
refuses non-integer labels rather than renumbering them silently. A written file loads the same network
Infomap gets from to_infomap(), weights included. Downloads are streamed, so
writing one never holds its links in memory.
The largest component¶
Many real networks fall apart into pieces: fewer than half of Netzschleuder’s
are connected. largest_component() keeps the largest piece, ignoring link
direction, so a directed network keeps its largest weakly connected component:
lcc = netgamut.load("com-Amazon").largest_component()
len(lcc.component) # how many nodes it keeps
lcc.to_pajek("out/") # PosixPath('out/com-Amazon-lcc.net')
im = lcc.to_infomap()
The result is a view on the same cached file, not a copy. Everything that
reads links (edges(), the exports, snapshots(), to_multilayer()) sees
only the component’s links, and nodes() only its nodes. Finding it is one
pass over the links that holds the nodes in memory but never the links. Files
it writes take a -lcc suffix. lcc.component is the set of node labels as
they appear in the file, which is what you need to restrict a reference
partition loaded from elsewhere. lcc.path is still the whole download.
Weights, times and layers¶
A third column is a weight in one file, a Unix timestamp in the next and a
sign or rating in a third, so nothing here guesses. Each source declares what
the extra columns of the file it loaded hold — Netzschleuder from the file’s
own header, KONECT from its fixed source target weight time layout, SNAP from
a table of its temporal and signed files — and to_infomap(), the writers and
everything below follow it:
net = snap.load("CollegeMsg")
print(net.columns) # source target time (time)
net.weighted_links # False: the timestamp is not a weight
Temporal networks expose their times as a numpy array, read once, so you can look before you bin:
t = net.times # int64 Unix seconds, in file order
t.min(), t.max()
net.time_bins("auto") # numpy.histogram_bin_edges(t, "auto")
for snapshot in net.snapshots(bins=12): # lazy: one network per bin
print(snapshot.info["start"], snapshot.info["end"], snapshot)
bins and range mean what they mean to numpy.histogram_bin_edges: a
number of equal-width bins, explicit edges, or an estimator name. Bin i is
[edges[i], edges[i+1]), the last one closed; every bin yields, empty ones
too, and a link’s weight in a snapshot is how often it occurs there (summed
weights where the file has a weight column). ISO dates come back as
datetime64.
The same bins, or a layer column, make a multilayer network: see Multilayer networks.
If a source has a column wrong, or you know better, declare it yourself:
dataclasses.replace(net, columns=netgamut.Columns(time=2)). Columns also
says how a row splits: delimiter="," reads a CSV whose quoted fields hold
commas or line breaks, delimiter="\t" a TSV whose fields hold spaces, and
header_row=True leaves out a first line that names the fields. Netzschleuder
declares its CSV, and SNAP its Reddit hyperlink TSVs.
Some entries are multi-file bundles:
net = snap.load("ego-Facebook", file="facebook.tar.gz")
directory = net.extract()
A Netzschleuder entry can hold several networks under one name, which it calls
subgraphs: one per community for add_health, one per period for
ambassador. The catalog’s figures describe the first, and load fetches the
first unless you name another:
netgamut.netzschleuder.info("ambassador").subgraph_names() # ('1985_1989', …, 'KINSHIP', 'ties')
net = netgamut.load("ambassador", source="netzschleuder", subgraph="2000")
Each subgraph loads as a network of its own. netgamut does not combine them,
because what they share differs by entry: ambassador’s are mostly periods,
plus a kinship network and a list of every tie. Where an entry’s subgraphs are
the layers of one network, netzschleuder.load_multilayer stacks them; see
Multilayer networks.
Multilayer networks¶
A multilayer network has nodes that appear in several layers: the lines of a
transport network, the kinds of tie between the same people. netgamut builds
one as a MultilayerNetwork, the shape Infomap’s multilayer model reads, from
wherever a source keeps its layers. Each layer’s links are summed, as counts or
as weights where the file has a weight column, and the source’s node names come
along.
A time column gives a layer per time bin, the same bins as snapshots:
ml = net.to_multilayer(bins=12) # layer i = bin i; ml.info["bin_edges"]
ml.to_infomap(multilayer_relax_rate=0.15, multilayer_relax_limit=1) # couple adjacent bins
A layer column gives a layer per value, and any categorical column can serve:
netgamut.load("london_transport").to_multilayer() # its layer column
snap.load("higgs-twitter", file="higgs-activity_time.txt.gz").to_multilayer(
layer="interaction" # retweet, mention, reply
)
Netzschleuder keeps the layers of its multilayer entries in different places.
netzschleuder.load_multilayer knows where:
from netgamut import netzschleuder as nz
nz.load_multilayer("london_transport") # its layer column
nz.load_multilayer("kujala_public_transport") # route_type: tram, rail, bus...
nz.load_multilayer("copenhagen") # subgraphs sms, fb_friends, calls and bt
copenhagen, malaria_genes, twitter_higgs, ugandan_village and
youtube_interactions keep each layer in a subgraph of its own. Each subgraph
numbers its nodes 0..n-1 by itself, so the stacked layers match a node by a
nodes.csv column: copenhagen’s id, the name of the others, and the row
number for youtube_interactions, whose subgraphs list the same users in the same
order. Name other subgraphs to stack, and the column that matches their nodes,
yourself:
nz.load_multilayer("ugandan_village", ["friendship-5", "health-advice_5"]) # village 5
nz.load_multilayer("copenhagen", ["calls", "sms"], key="id")
For column-layered entries with several subgraphs, load the subgraph you want
and lay it out: nz.load("kujala_public_transport", subgraph="berlin").to_multilayer().
Three entries are tagged multilayer, but their files hold no layers:
physician_trust merges its advice, discussion and friendship ties; iwdb is a
collection of separate studies; and in paris_transportation’s current upstream
file, every link has one end on a node without attributes. load_multilayer
raises LookupError for them.
Networks you loaded yourself stack with MultilayerNetwork.from_networks. Files
that share one id space, as SNAP’s higgs-twitter files do, need no key:
layers = {
kind: snap.load("higgs-twitter", file=f"{kind}_network.edgelist.gz")
for kind in ("retweet", "reply", "mention")
}
ml = netgamut.MultilayerNetwork.from_networks(layers, name="higgs")
None of these layerings links one layer to another: Infomap couples them with
its relax rate. ml.to_pajek(directory) writes Infomap’s multilayer format
(*Vertices, then *Intra lines of layer, node, node and weight), and
ml.to_infomap() builds the same network in Infomap. The web mirror keeps a
multilayer network’s layers as a layer column of its links file, with their
number of layers in index.json.
Node metadata, in one shape¶
Every source stores node attributes somewhere different — a separate SNAP artifact,
a member of a Netzschleuder bundle, KONECT’s ent.* files, OGB’s raw/ and
mapping/ folders, a materialised PyG table. Network.nodes() reads all of them the same way:
nodes = netgamut.load("ogbn-arxiv").nodes()
nodes.columns # ('arxiv_category', 'year', 'paper')
nodes["arxiv_category"] # {0: 'arxiv cs cr', ...} decoded where named
nodes.partition("arxiv_category") # {0: 6, ...} contiguous 0..k-1, for AMI/NMI/D
nodes.categories("arxiv_category") # ('arxiv cs ai', ...) categories[i] names module i
partition and categories are two halves of one answer: the first is what you score
a detected partition against, the second is what lets you say which category a module
lines up with. They always agree —
categories(c)[partition(c)[node]] is that node’s value.
The same four calls, whatever the source:
netgamut.load("email-Eu-core").nodes()["department"] # SNAP
netgamut.load("karate", source="netzschleuder").nodes()["groups"]
netgamut.load("ogbn-arxiv").nodes()["arxiv_category"] # OGB
netgamut.load("Planetoid/Cora", source="pyg").nodes()["label"]
To find datasets that have node metadata before downloading anything, ask across sources — by presence, or by column name:
netgamut.search(node_metadata=True) # 240 datasets known to carry some
netgamut.search(node_attribute="gender") # something to score a partition against
netgamut.search("karate")[0].node_attributes # ('name', 'groups')
Each source answers from its own catalog: Netzschleuder from the site’s analyses,
KONECT from its archive listings, SNAP from its label files, OGB and PyG from their
class labels. Netzschleuder, KONECT and SNAP name every column. OGB names only its
extra files (year, species) and PyG none, since the rest of their column names
come with the download. Datasets a catalog cannot answer for — all of AHORN, whose
node lines live inside the dataset file — are left out of either filter.
Column names come from the source, so a figure legend can use them as they are. Values
are decoded to names where the source ships names — OGB does, PyG does not:
Data.y is an integer tensor and PyG’s API has no class_names anywhere, so
categories("label") returns the codes and their meaning lives in the originating
paper. Nodes with no value are absent from partition rather than pooled into a
module of their own, which would invent a community the source never claimed.
Overlapping ground-truth communities are a different thing and stay where they were —
snap.load_ground_truth(...) and Network.communities() — because a cover is not a
column.
Node metadata is not ground truth for community structure. A partition that disagrees with an attribute is not thereby wrong; scoring against
partition(col)measures agreement with an attribute, which is a finding about the attribute as much as about the algorithm. See Peel, Larremore & Clauset, Science Advances 3, e1602548 (2017).