Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 9753e50
  • /
  • nx.py
Raw File Download
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:94eefb2439c77ea1b4208e8018a8e757d080e613
directory badge Iframe embedding
swh:1:dir:9753e50b7268a47325aaba182ffb3074cc1a3a21
Citations

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
nx.py
import matplotlib.pyplot as plt
import networkx as nx
from paraview import simple


def use_networkx():
    def print_graph_stats(G):
        cc_size = dict()
        n_cc = nx.number_connected_components(G)
        print(nx.info(G))
        print("#Neighs\t#Nodes")
        for i, v in enumerate(nx.degree_histogram(G)):
            if v != 0:
                print(f" {i}\t{v}")
        density = nx.density(G)
        print(f"Density: {density}")
        print(f"#Connected Components: {n_cc}")
        print("#Nodes\t#Components")
        for cc in nx.connected_components(G):
            cc_size[len(cc)] = cc_size.get(len(cc), 0) + 1
        for k, v in sorted(cc_size.items(), key=lambda item: item[1]):
            print(f" {k}\t{v}")

    def get_neighs(G, nodes, acc=set()):
        for node in nodes:
            for neigh in nx.all_neighbors(G, node):
                acc.add(neigh)

    def get_neighborhood(G, seeds, radius=1):
        neighs = set(seeds)
        for i in range(radius):
            get_neighs(G, neighs.copy(), neighs)
        return nx.subgraph(G, neighs)

    def display_graph(G):
        color_map = list()
        for node in G:
            if "_s1" in node:
                color_map.append("cyan")
            elif "_s2" in node:
                color_map.append("orange")
            else:
                color_map.append("green")
        nx.draw_networkx(
            G,
            # nx.spectral_layout(G),
            node_color=color_map,
            node_size=100,
            font_size=8,
        )
        plt.show()

    with open("saddle2_graph.csv", "rb") as src:
        G = nx.readwrite.edgelist.read_edgelist(src, nodetype=str)
        # graph is bipartite
        nx.set_node_attributes(
            G, {node: (0 if "_s1" in node else 1) for node in G}, name="bipartite"
        )
        display_graph(G)


def use_mds():
    DistMat = simple.CSVReader(FileName=["saddle2_graph.csv"])
    DimRed = simple.TTKDimensionReduction(Input=DistMat)
    DimRed.SelectFieldswithaRegexp = 1
    DimRed.Regexp = "Dist.*"
    DimRed.Components = 3
    DimRed.InputIsaDistanceMatrix = 1
    DimRed.UseAllCores = 0
    # exclude distance matrix from the result, too many columns for SQLite
    Compress = simple.TTKCinemaQuery(InputTable=[DimRed])
    Compress.ExcludecolumnswithaRegexp = 1
    Compress.Regexp = "Dist.*"
    Compress.SQLStatement = "SELECT * FROM InputTable0"
    simple.SaveData("saddle2_red.csv", Input=Compress, Precision=8)


if __name__ == "__main__":
    use_networkx()

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Contact— JavaScript license information— Web API

back to top