Coverage for SynRBL/SynProcessor/rsmi_both_side_process.py: 22%

45 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-04-11 11:17 +0200

1from joblib import Parallel, delayed 

2 

3 

4class BothSideReact: 

5 """ 

6 Class to process chemical reactions on both sides (reactants and products). 

7 

8 Attributes: 

9 react_dict (list): A list of dictionaries representing reactants. 

10 product_dict (list): A list of dictionaries representing products. 

11 unbalance (list): A list indicating the balance status of each reaction. 

12 diff_formula (list): A list containing the differential formula for each reaction. 

13 """ 

14 

15 def __init__(self, react_dict, product_dict, unbalance, diff_formula): 

16 """ 

17 Initializes the BothSideReact class with reaction dictionaries, unbalance, and differential formulas. 

18 

19 Parameters: 

20 react_dict (list): List of dictionaries representing reactants. 

21 product_dict (list): List of dictionaries representing products. 

22 unbalance (list): List indicating the balance status of each reaction. 

23 diff_formula (list): List containing the differential formula for each reaction. 

24 """ 

25 self.react_dict = react_dict 

26 self.product_dict = product_dict 

27 

28 # Ensure 'Q' key is present in all reactant and product dictionaries 

29 for d in [self.react_dict, self.product_dict]: 

30 for value in d: 

31 if "Q" not in value: 

32 value["Q"] = 0 

33 

34 self.unbalance = unbalance 

35 self.diff_formula = diff_formula 

36 

37 @staticmethod 

38 def enforce_product_side(react_dict, product_dict): 

39 """ 

40 Enforces the product side of the reaction by calculating the difference between reactant and product counts. 

41 

42 Parameters: 

43 react_dict (dict): Dictionary representing a single reactant. 

44 product_dict (dict): Dictionary representing a single product. 

45 

46 Returns: 

47 dict: Dictionary representing the differential count between reactant and product. 

48 """ 

49 diff_dict = {} 

50 # Calculate the difference between reactants and products 

51 for key, value in react_dict.items(): 

52 diff_value = value - product_dict.get(key, 0) 

53 if diff_value != 0: 

54 diff_dict[key] = diff_value 

55 

56 # Add keys only present in product_dict 

57 for key, value in product_dict.items(): 

58 if key not in react_dict: 

59 diff_dict[key] = -value 

60 

61 return diff_dict 

62 

63 @staticmethod 

64 def filter_list_by_indices(data, indices): 

65 """ 

66 Filters a list by specified indices. 

67 

68 Parameters: 

69 data (list): The original list to filter. 

70 indices (list): List of indices to include in the filtered list. 

71 

72 Returns: 

73 list: A filtered list containing elements at the specified indices. 

74 """ 

75 # Use list comprehension for efficient filtering 

76 return [data[i] for i in indices if i < len(data)] 

77 

78 @staticmethod 

79 def reverse_values_if_negative_except_Q(diff_dict): 

80 """ 

81 Reverses the values in the dictionary if negative, except for the key 'Q'. 

82 

83 Parameters: 

84 diff_dict (dict): The dictionary with potential negative values. 

85 

86 Returns: 

87 tuple: A tuple containing the updated dictionary and a string indicating the balance status. 

88 """ 

89 if len(diff_dict) == 2 and "Q" in diff_dict.keys(): 

90 if any(value < 0 for key, value in diff_dict.items() if key != "Q"): 

91 # Reverse all values except for 'Q' 

92 return {key: -value for key, value in diff_dict.items()}, "Reactants" 

93 else: 

94 # Original dictionary if no negative values found 

95 return diff_dict, "Products" 

96 else: 

97 # Return as is if conditions not met 

98 return diff_dict, "Both" 

99 

100 def fit(self, n_jobs=4): 

101 """ 

102 Processes the reactions by balancing reactants and products and updating the unbalance status. 

103 

104 Returns: 

105 tuple: A tuple containing the updated diff_formula and unbalance lists. 

106 """ 

107 # Filter indices where balance status is 'Both' 

108 both_index = [i for i, val in enumerate(self.unbalance) if val == "Both"] 

109 react_dict_both = self.filter_list_by_indices(self.react_dict, both_index) 

110 product_dict_both = self.filter_list_by_indices(self.product_dict, both_index) 

111 

112 # Process reactions in parallel for efficiency 

113 diff_dict = Parallel(n_jobs=n_jobs, verbose=0)( 

114 delayed(self.enforce_product_side)(react, prod) 

115 for react, prod in zip(react_dict_both, product_dict_both) 

116 ) 

117 

118 # Post-process to determine the new balance status 

119 results = [self.reverse_values_if_negative_except_Q(item) for item in diff_dict] 

120 diff_dict_both, unbalance_both = [], [] 

121 if len(results) == 2: 

122 diff_dict_both, unbalance_both = zip(*results) 

123 

124 # Update diff_formula and unbalance lists 

125 for index, diff_new, unbalance_new in zip( 

126 both_index, diff_dict_both, unbalance_both 

127 ): 

128 self.diff_formula[index] = diff_new 

129 self.unbalance[index] = unbalance_new 

130 

131 return self.diff_formula, self.unbalance