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
  • /
  • compare_diags.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:5394e34f04db36d850e306f4c8c557a79abc7816
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 ...
compare_diags.py
#!/usr/bin/env python3

import difflib
import glob
import math
import sys

import topologytoolkit as ttk
import vtk


def read_file(fname):
    ext = fname.split(".")[-1]
    if ext == "vtu":
        reader = vtk.vtkXMLUnstructuredGridReader()
    elif ext == "dipha":
        reader = ttk.ttkDiphaReader()
    else:
        return None
    reader.SetFileName(fname)
    reader.Update()
    return reader.GetOutput()


def read_diag(diag):
    diag = read_file(diag)
    ptype = diag.GetCellData().GetArray("PairType")
    pts = diag.GetPoints()
    assert 2 * ptype.GetNumberOfTuples() - 2 == pts.GetNumberOfPoints()
    pairs = [list() for i in range(3)]
    for i in range(ptype.GetNumberOfTuples()):
        j = int(ptype.GetTuple1(i))
        if j == -1:
            continue
        pairs[j].append(pts.GetPoint(2 * i + 1)[0:2])
    for pr in pairs:
        pr.sort(key=lambda x: x[1])
    return pairs


def compare_pairs(pairs0, pairs1, type, show_diff):
    sm = difflib.SequenceMatcher(isjunk=None, a=pairs0, b=pairs1)
    diffrat = sm.ratio()
    if math.isclose(diffrat, 1.0):
        print(f"> Identical {type} pairs")
    else:
        print(f"> Differences in {type} pairs (similarity ratio: {diffrat})")
        if show_diff:
            p0 = [str(a) + " " + str(b) for (a, b) in pairs0]
            p1 = [str(a) + " " + str(b) for (a, b) in pairs1]
            diff = difflib.unified_diff(p0, p1)
            GREEN = "\033[92m"
            RED = "\033[91m"
            ENDC = "\033[0m"
            for d in diff:
                if d.startswith("+"):
                    print(f"{GREEN}{d}{ENDC}")
                elif d.startswith("-"):
                    print(f"{RED}{d}{ENDC}")
                else:
                    print(d)


def main(diag0, diag1, show_diff=True):
    print(f"Comparing {diag0} and {diag1}...")
    pairs0 = read_diag(diag0)
    pairs1 = read_diag(diag1)
    diag_type = ["min-saddle", "saddle-saddle", "saddle-max"]
    for p0, p1, t in zip(pairs0, pairs1, diag_type):
        compare_pairs(p0, p1, t, show_diff)


if __name__ == "__main__":
    if len(sys.argv) == 3:
        main(sys.argv[1], sys.argv[2], True)
    else:
        expl_diags = sorted(glob.glob("diagrams/*_order_sfnorm_expl*"))
        for i in range(len(expl_diags) // 2):
            main(expl_diags[2 * i], expl_diags[2 * i + 1])

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