From 7fc0bbc6e314b4f123954ea49e4c89fe0f4f2e15 Mon Sep 17 00:00:00 2001 From: zhaoyangwx <32697586+zhaoyangwx@users.noreply.github.com> Date: Mon, 9 May 2022 21:26:34 +0800 Subject: [PATCH 1/6] Check isomorphism of lists with 4+ species Sometimes need to compare reaction with 4+ reactants/products. Improved performance by avoid unnecessary calculation of list length. --- rmgpy/reaction.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/rmgpy/reaction.py b/rmgpy/reaction.py index b4a808790e..88e96c13e2 100644 --- a/rmgpy/reaction.py +++ b/rmgpy/reaction.py @@ -1501,7 +1501,7 @@ def generate_high_p_limit_kinetics(self): def same_species_lists(list1, list2, check_identical=False, only_check_label=False, generate_initial_map=False, - strict=True, save_order=False): + strict=True, save_order=False, maximum_length=10): """ This method compares whether two lists of species or molecules are the same, given the comparison options provided. It is used for the `is_same` method @@ -1529,16 +1529,21 @@ def same(object1, object2, _check_identical=check_identical, _only_check_label=o else: return object1.is_isomorphic(object2, generate_initial_map=_generate_initial_map, strict=_strict, save_order=_save_order) - - if len(list1) == len(list2) == 1: + len_list1 = len(list1) + if len_list1 != len(list2): + return False + if len_list1 == 0: + # List is empty + raise NotImplementedError("Can't check isomorphism of lists with {0} species/molecules".format(len_list1)) + elif len_list1 == 1: if same(list1[0], list2[0]): return True - elif len(list1) == len(list2) == 2: + elif len_list1 == 2: if same(list1[0], list2[0]) and same(list1[1], list2[1]): return True elif same(list1[0], list2[1]) and same(list1[1], list2[0]): return True - elif len(list1) == len(list2) == 3: + elif len_list1 == 3: if same(list1[0], list2[0]): if same(list1[1], list2[1]): if same(list1[2], list2[2]): @@ -1560,7 +1565,15 @@ def same(object1, object2, _check_identical=check_identical, _only_check_label=o elif same(list1[1], list2[1]): if same(list1[2], list2[0]): return True - elif len(list1) == len(list2): - raise NotImplementedError("Can't check isomorphism of lists with {0} species/molecules".format(len(list1))) - # nothing found + elif len_list1 <= maximum_length: + def inlist(x,list0): + for y in list0: + if same(x,y): + return True + return False + return len([x for x in list1 if not(inlist(x,list2))])==0 + else: + # List is too long + raise NotImplementedError("Can't check isomorphism of lists with {0} species/molecules".format(len_list1)) + return False From 4bc76a6d3ec561746a43a3ae1804bbd156c14717 Mon Sep 17 00:00:00 2001 From: zhaoyangwx <32697586+zhaoyangwx@users.noreply.github.com> Date: Mon, 9 May 2022 21:39:22 +0800 Subject: [PATCH 2/6] Update reaction.py --- rmgpy/reaction.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rmgpy/reaction.py b/rmgpy/reaction.py index 88e96c13e2..9cd2a9e723 100644 --- a/rmgpy/reaction.py +++ b/rmgpy/reaction.py @@ -1567,10 +1567,10 @@ def same(object1, object2, _check_identical=check_identical, _only_check_label=o return True elif len_list1 <= maximum_length: def inlist(x,list0): - for y in list0: - if same(x,y): - return True - return False + for y in list0: + if same(x,y): + return True + return False return len([x for x in list1 if not(inlist(x,list2))])==0 else: # List is too long From d92833ebf30cf51edb72b4bd9a34b458284aedaf Mon Sep 17 00:00:00 2001 From: zhaoyangwx <32697586+zhaoyangwx@users.noreply.github.com> Date: Mon, 9 May 2022 22:31:11 +0800 Subject: [PATCH 3/6] Fix comparison problem Updated comparison for two lists with same elements but different sequence. --- rmgpy/reaction.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/rmgpy/reaction.py b/rmgpy/reaction.py index 9cd2a9e723..99c1d8617c 100644 --- a/rmgpy/reaction.py +++ b/rmgpy/reaction.py @@ -1566,12 +1566,21 @@ def same(object1, object2, _check_identical=check_identical, _only_check_label=o if same(list1[2], list2[0]): return True elif len_list1 <= maximum_length: - def inlist(x,list0): - for y in list0: - if same(x,y): - return True - return False - return len([x for x in list1 if not(inlist(x,list2))])==0 + def issamelist(l1,l2): + # Copy list to prevent changes to original input + l1=l1[:] + l2=l2[:] + x=0 + while x Date: Mon, 9 May 2022 22:41:02 +0800 Subject: [PATCH 4/6] Update reaction.py Reduce calculations --- rmgpy/reaction.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rmgpy/reaction.py b/rmgpy/reaction.py index 99c1d8617c..ba7f744de0 100644 --- a/rmgpy/reaction.py +++ b/rmgpy/reaction.py @@ -1572,12 +1572,16 @@ def issamelist(l1,l2): l2=l2[:] x=0 while x Date: Mon, 9 May 2022 23:37:37 +0800 Subject: [PATCH 5/6] Fix compile error Argument list changes cause error when compiling. No need to add new argument maximum_length. Also no need to define individual function in list comparison. --- rmgpy/reaction.py | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/rmgpy/reaction.py b/rmgpy/reaction.py index ba7f744de0..edf822f3ad 100644 --- a/rmgpy/reaction.py +++ b/rmgpy/reaction.py @@ -1501,7 +1501,7 @@ def generate_high_p_limit_kinetics(self): def same_species_lists(list1, list2, check_identical=False, only_check_label=False, generate_initial_map=False, - strict=True, save_order=False, maximum_length=10): + strict=True, save_order=False): """ This method compares whether two lists of species or molecules are the same, given the comparison options provided. It is used for the `is_same` method @@ -1529,6 +1529,7 @@ def same(object1, object2, _check_identical=check_identical, _only_check_label=o else: return object1.is_isomorphic(object2, generate_initial_map=_generate_initial_map, strict=_strict, save_order=_save_order) + maximum_length = 10 len_list1 = len(list1) if len_list1 != len(list2): return False @@ -1566,25 +1567,23 @@ def same(object1, object2, _check_identical=check_identical, _only_check_label=o if same(list1[2], list2[0]): return True elif len_list1 <= maximum_length: - def issamelist(l1,l2): - # Copy list to prevent changes to original input - l1=l1[:] - l2=l2[:] - x=0 - while x Date: Mon, 9 May 2022 23:43:42 +0800 Subject: [PATCH 6/6] Fix typo --- rmgpy/reaction.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rmgpy/reaction.py b/rmgpy/reaction.py index edf822f3ad..b5cc88395f 100644 --- a/rmgpy/reaction.py +++ b/rmgpy/reaction.py @@ -1568,8 +1568,8 @@ def same(object1, object2, _check_identical=check_identical, _only_check_label=o return True elif len_list1 <= maximum_length: # Copy list to prevent changes to original input - l1=l1[:] - l2=l2[:] + l1=list1[:] + l2=list2[:] x=0 while x