SOC-Observation-Code/tools/removeiso.py

78 lines
2.0 KiB
Python
Raw Normal View History

#!/usr/bin/python3
"""
SPDX-FileCopyrightText: 2023 Ämin Baumeler <amin@indyfac.ch> and Eleftherios-Ermis Tselentis <eleftheriosermis.tselentis@oeaw.ac.at>
SPDX-License-Identifier: GPL-3.0-or-later
"""
import sys
import getopt
import numpy as np
import networkx as nx
"""Parse command line arguments first."""
helpstr = """Usage: """+sys.argv[0]+""" -f <filename>
Filters out the isomorphic graphs in `filename'."""
FILENAME = False
try:
opts, args = getopt.getopt(sys.argv[1:], "hf:")
except getopt.GetoptError:
print(helpstr, file=sys.stderr)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(helpstr)
sys.exit()
elif opt == '-f':
FILENAME = arg
if not FILENAME:
print(helpstr, file=sys.stderr)
sys.exit(2)
with open(FILENAME, 'rb') as f:
num_lines = sum(1 for _ in f)
print(f"Total number of digraphs is {num_lines}", file=sys.stderr)
nonisodigraphs = set()
cnt = 0
fnd = 0
with open(FILENAME, 'r') as f:
while 1:
cnt = cnt + 1
print("\r{:.2f}%".format(100*cnt/num_lines), end='', file=sys.stderr)
line = f.readline().strip('{}\n')
if not line:
break
G = nx.from_numpy_matrix(np.vectorize(int)(np.matrix([x.split(',') for x in line.split('},{')])), create_using=nx.DiGraph)
couldbeold = False
for H in nonisodigraphs:
# Returns False if graphs are definitely not isomorphic.
# True does NOT guarantee isomorphism.
if nx.faster_could_be_isomorphic(G, H):
couldbeold = True
break
if not couldbeold:
nonisodigraphs.add(G)
fnd = fnd + 1
print(f"{{{{{line}}}}}")
continue
new = True
for H in nonisodigraphs:
if nx.is_isomorphic(G, H):
new = False
break
if new:
nonisodigraphs.add(G)
fnd = fnd + 1
print(f"{{{{{line}}}}}")
print(f"\r100% Found {fnd} non-isomoric graphs", file=sys.stderr)