[Nauty] Fw: Filters graphs with diameters greater than 2 and less than 4
lczhangmath
lczhangmath at 163.com
Sat Sep 30 23:26:04 AEST 2023
Hello,
Thank you for your reminder. Your first way is nice. I tried your translate function, but it didn't work for me. If you have specific steps, please let me know. Anyway, I have two other methods that seem pythonic.
One is to use regular expressions.
```
import re
import networkx as nx
char_map = {
'\a': r'\a',
'\b': r'\b',
'\c': r'\c',
'\f': r'\f',
'\n': r'\n',
'\r': r'\r',
'\t': r'\t',
'\v': r'\v',
'\'': r'\'',
'\"': r'\"'
}
pattern = re.compile('|'.join(re.escape(key) for key in char_map.keys()))
line = 'ihChWC@?gE_@?@?A_ at g?@??C??j?@l??E??Ao??\???m??@I??DF??E`O??GM??@?g??S\@o?@g at O??G?w??C?I??D?@o?@g?D???_?M??@??I??D??FK?E_?@Q??G??N??@???CPCOaG\a??'
line1 = pattern.sub(lambda x: char_map[x.group(0)], line)
G = nx.from_graph6_bytes(line1.encode())
print(G)
```
Another way to write it.
```
import functools
import networkx as nx
char_map = [
('\a', r'\a'),
('\b', r'\b'),
('\c', r'\c'),
('\f', r'\f'),
('\n', r'\n'),
('\r', r'\r'),
('\t', r'\t'),
('\v', r'\v'),
('\'', r'\''),
('\"', r'\"')
]
def replace_chars(s, mapping):
return functools.reduce(lambda s, replacement: s.replace(*replacement), mapping, s)
line = 'ihChWC@?gE_@?@?A_ at g?@??C??j?@l??E??Ao??\???m??@I??DF??E`O??GM??@?g??S\@o?@g at O??G?w??C?I??D?@o?@g?D???_?M??@??I??D??FK?E_?@Q??G??N??@???CPCOaG\a??'
line1 = replace_chars(line, char_map)
G = nx.from_graph6_bytes(line1.encode())
print(G)
```
By the way, I made some improvements to the code before, such as passing the filter through a pipe directly from geng instead of exporting it to a file (using universal_newlines=True).
import subprocess
import sys
import networkx as nx
import functools
escape_dict={'\a':r'\a',
'\b':r'\b',
'\c':r'\c',
'\f':r'\f',
'\n':r'\n',
'\r':r'\r',
'\t':r'\t',
'\v':r'\v',
'\'':r'\'',
'\"':r'\"'}
process = subprocess.Popen(['geng', '19', '22', '-b'], stdout=subprocess.PIPE, universal_newlines=True)
count = 0
for line in process.stdout:
line=line.strip()
#Eliminate the impact of escape characters
line=''.join((escape_dict[x] if x in escape_dict else x) for x in line)
G = nx.from_graph6_bytes(line.encode())
if len(nx.max_weight_matching(G))==4:
count+=1
sg=nx.to_graph6_bytes(G)
cleaned_g6string = sg[10:-1].decode()
print(cleaned_g6string)
print(count,"graphs written to stdout")
One of the more interesting things is when does geng produce graphs with \ characters. I've been looking to check if my code is working.
Best wishes.
Licheng.
在 2023-09-30 18:54:24,keith.briggs at bt.com 写道:
That code is quite unpythonic. You should not use try...except for checking keys in dictionaries. It could be done in one line like this:
line=''.join((escape_dict[x] if x in escape_dict else x) for x in line)
But much better is just to use the built-in string translate method:
line=line.translate(r'\a\b...','\a\b...')
Also, count=count+1 should be count+=1.
Keith
From: lczhangmath <lczhangmath at 163.com>
Sent: Saturday, September 30, 2023 01:25
To: Briggs,KM,Keith,TUD2 R <keith.briggs at bt.com>
Cc: nauty at anu.edu.au <nauty at anu.edu.au>
Subject: Re:Re: Re:Re: Re:Re: [Nauty] Fw: Filters graphs with diameters greater than 2 and less than 4
Dear Keith,
Fortunately, the problem with escape characters here can now be solved in python. We always want graph6 strings involving \ not escaped, I attach the full code below.
import subprocess
import sys
import networkx as nx
escape_dict={'\a':r'\a',
'\b':r'\b',
'\c':r'\c',
'\f':r'\f',
'\n':r'\n',
'\r':r'\r',
'\t':r'\t',
'\v':r'\v',
'\'':r'\'',
'\"':r'\"'}
def raw(text):
"""Returns a raw string representation of text"""
new_string=''
for char in text:
try:
new_string += escape_dict[char]
except KeyError:
new_string += char
return new_string
completed_process = subprocess.run(['geng', '9', '8', '-b', '-c'], stdout=subprocess.PIPE, text=True)
output_lines = completed_process.stdout.splitlines()
count = 0
for line in output_lines:
line=line.strip()
#Eliminate the impact of escape characters
line=raw(line)
G = nx.from_graph6_bytes(line.encode())
if nx.diameter(G)<=4 and nx.radius(G)>=2:
count=count+1
sg=nx.to_graph6_bytes(G)
cleaned_g6string = sg[10:-1].decode()
print(cleaned_g6string)
print(count,"graphs written to stdout")
Also see the link: https://stackoverflow.com/questions/12605090/how-to-prevent-automatic-escaping-of-special-characters-in-python
If you have any further questions, please feel free to contact me. Thank you again for reminding me of the previous question.
Best wishes,
Licheng.
在 2023-09-29 22:56:40,keith.briggs at bt.com 写道:
r'...' is for literal strings only.
Keith
From: lczhangmath <lczhangmath at 163.com>
Sent: Friday, September 29, 2023 3:13:31 pm
To: Briggs,KM,Keith,TUD2 R <keith.briggs at bt.com>
Cc: nauty at anu.edu.au <nauty at anu.edu.au>
Subject: Re:Re: Re:Re: [Nauty] Fw: Filters graphs with diameters greater than 2 and less than 4
It's off-topic in a sense, but it doesn't seem to be too far off, and there might be problems with calling these programs from python scripts. Although I don't see much of a problem for the moment, for the small graphs from geng, even if I don't replace \ to \\. You said r+string, I think that's right, but I have already got the string in the pipeline and it can't append r. This is frustrating.
Licheng.
在 2023-09-29 22:01:01,keith.briggs at bt.com 写道:
This is getting off-topic, but in python it's much safer to use r-strings when you have backslashes in the string, like this: r'\'.
Keith
From: lczhangmath <lczhangmath at 163.com>
Sent: Friday, September 29, 2023 2:50:59 pm
To: Briggs,KM,Keith,TUD2 R <keith.briggs at bt.com>
Cc: nauty at anu.edu.au <nauty at anu.edu.au>
Subject: Re:Re: [Nauty] Fw: Filters graphs with diameters greater than 2 and less than 4
| |
| |
Hello, Keith
You are right. May be I need write line = line.replace("\\", "\\\\"). I'm worried about the effect of escape characters, for example,
import networkx as nx
line="ihChWC@?gE_@?@?A_ at g?@??C??j?@l??E??Ao??\???m??@I??DF??E`O??GM??@?g??S\@o?@g at O??G?w??C?I??D?@o?@g?D???_?M??@??I??D??FK?E_?@Q??G??N??@???CPCOaG\a??"
G = nx.from_graph6_bytes(line.encode())
It gives a error: File "C:\Users\asus\AppData\Roaming\Python\Python310\site-packages\networkx\readwrite\graph6.py", line 118, in from_graph6_bytes
raise NetworkXError(
networkx.exception.NetworkXError: Expected 861 bits but got 858 in graph6
So I replace all \\ to \\\\ But it remains wrong.
import networkx as nx
line="ihChWC@?gE_@?@?A_ at g?@??C??j?@l??E??Ao??\???m??@I??DF??E`O??GM??@?g??S\@o?@g at O??G?w??C?I??D?@o?@g?D???_?M??@??I??D??FK?E_?@Q??G??N??@???CPCOaG\a??"
G = nx.from_graph6_bytes(line.encode())
Because the lower \a in the string is treated as x07 in advance. I can't do anything about it. I know that we can add a prefix r to the string and leave it as it is, without escaping. As follows:
import networkx as nx
line=r"ihChWC@?gE_@?@?A_ at g?@??C??j?@l??E??Ao??\???m??@I??DF??E`O??GM??@?g??S\@o?@g at O??G?w??C?I??D?@o?@g?D???_?M??@??I??D??FK?E_?@Q??G??N??@???CPCOaG\a??"
G = nx.from_graph6_bytes(line.encode())
print(G)
Output: Graph with 42 nodes and 138 edges
But we have exported the string, it is already escaped (completed_process.stdout.splitlines()), I can't add the prefix r in front, do you have any good ideas? Of course, it seems that there are no annoying characters like \ in graphh6 when there are fewer vertices.
Best wishes,
Licheng
在 2023-09-29 15:26:10,keith.briggs at bt.com 写道:
The line.replace statement won't do anything, since you throw away the result.
Keith
From: Nauty <nauty-bounces at anu.edu.au> on behalf of lczhangmath <lczhangmath at 163.com>
Sent: Friday, September 29, 2023 6:37:51 am
To: nauty <nauty at anu.edu.au>
Subject: [Nauty] Fw: Filters graphs with diameters greater than 2 and less than 4
[
---- Forwarded Message ----
| From | lczhangmath<lczhangmath at 163.com> |
| Date | 09/29/2023 13:08 |
| To | Brendan McKay<brendan.mckay at anu.edu.au> |
| Subject | Re:Re: [Nauty] Filters graphs with diameters greater than 2 and less than 4 |
Dear Prof. Brendan,
Thank you for informing me of the overlooked option. Before you informed me, I wrote a Python script. Of course, this script can combine some parameter filtering, a bit like the SageMath call. But I am very looking forward to your handling in C language.
Python script:
import subprocess
import sys
import networkx as nx
completed_process = subprocess.run(['geng', '9', '8', '-b', '-c'], stdout=subprocess.PIPE, text=True)
output_lines = completed_process.stdout.splitlines()
count = 0
for line in output_lines:
line=line.strip()
#Eliminate the impact of escape characters
line.replace("\\", "\\\\")
G = nx.from_graph6_bytes(line.encode())
if nx.diameter(G)<=4 and nx.radius(G)>=2:
count=count+1
sg=nx.to_graph6_bytes(G)
cleaned_g6string = sg[10:-1].decode()
print(cleaned_g6string)
print(count,"graphs written to stdout")
Best wishes,
Licheng Zhang.
在 2023-09-28 20:19:36,"Brendan McKay" <Brendan.McKay at anu.edu.au> 写道:
>Hi Licheng,
>
>geng -c 7 | pickg -Z2:4
>
>You can use a range like that for all numerical parameters. You can
>also use -Z:4 for <=4 and -Z2: for >=2.
>
>I'll answer your earlier question tomorrow.
>
>Brendan.
>
>On 28/9/2023 9:43 pm, lczhangmath wrote:
>> Hello,
>>
>>
>> The second question in my email, titled "matching number and operations between parameters", contains this question, and I recount it in detail here.
>>
>>
>> As with equations, we sometimes need to filter for cases that satisfy a parametric inequality. For example, I
>> would like to get all 7-vertex graphs with diameter k where k is greater than or equal to 2 and less than or equal to 4.
>>
>>
>> At this time, either I use the external script (e.g. sagemath) or I run the following shell script, but the sctipt always repeats the execution of geng, so the efficiency is not high.
>>
>>
>> for i in {2..4}
>> do
>> echo "Running geng 7 -c | pickg -V -Z$i"
>> geng 7 -c | pickg -Z$i >> output.txt
>> done
>>
>>
>> I don't know where I can change the source code to achieve this.
>>
>>
>>
>>
>> Best regards,
>> Licheng Zhang
>> _______________________________________________
>> Nauty mailing list
>> Nauty at anu.edu.au
>> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmailman.anu.edu.au%2Fmailman%2Flistinfo%2Fnauty&data=05%7C01%7Ckeith.briggs%40bt.com%7C99e07c1b3fac49b7f7b308dbc0ae36a5%7Ca7f356889c004d5eba4129f146377ab0%7C0%7C0%7C638315626714540227%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=l55NjJSdBPmgVq9W2vulGj5HU6rrKFMZFsoExrdElxM%3D&reserved=0
>
>_______________________________________________
>Nauty mailing list
>Nauty at anu.edu.au
>https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmailman.anu.edu.au%2Fmailman%2Flistinfo%2Fnauty&data=05%7C01%7Ckeith.briggs%40bt.com%7C99e07c1b3fac49b7f7b308dbc0ae36a5%7Ca7f356889c004d5eba4129f146377ab0%7C0%7C0%7C638315626714540227%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=l55NjJSdBPmgVq9W2vulGj5HU6rrKFMZFsoExrdElxM%3D&reserved=0
_______________________________________________
Nauty mailing list
Nauty at anu.edu.au
https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmailman.anu.edu.au%2Fmailman%2Flistinfo%2Fnauty&data=05%7C01%7Ckeith.briggs%40bt.com%7C99e07c1b3fac49b7f7b308dbc0ae36a5%7Ca7f356889c004d5eba4129f146377ab0%7C0%7C0%7C638315626714540227%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=l55NjJSdBPmgVq9W2vulGj5HU6rrKFMZFsoExrdElxM%3D&reserved=0
More information about the Nauty
mailing list