comparison association_rules.py @ 15:2eb5c017958d draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
author bgruening
date Wed, 09 Aug 2023 13:15:27 +0000
parents caf7d2b71a48
children
comparison
equal deleted inserted replaced
14:4d1637cac794 15:2eb5c017958d
5 import pandas as pd 5 import pandas as pd
6 from mlxtend.frequent_patterns import association_rules, fpgrowth 6 from mlxtend.frequent_patterns import association_rules, fpgrowth
7 from mlxtend.preprocessing import TransactionEncoder 7 from mlxtend.preprocessing import TransactionEncoder
8 8
9 9
10 def main(inputs, infile, outfile, min_support=0.5, min_confidence=0.5, min_lift=1.0, min_conviction=1.0, max_length=None): 10 def main(
11 inputs,
12 infile,
13 outfile,
14 min_support=0.5,
15 min_confidence=0.5,
16 min_lift=1.0,
17 min_conviction=1.0,
18 max_length=None,
19 ):
11 """ 20 """
12 Parameter 21 Parameter
13 --------- 22 ---------
14 input : str 23 input : str
15 File path to galaxy tool parameter 24 File path to galaxy tool parameter
34 43
35 max_length: int 44 max_length: int
36 Maximum length 45 Maximum length
37 46
38 """ 47 """
39 warnings.simplefilter('ignore') 48 warnings.simplefilter("ignore")
40 49
41 with open(inputs, 'r') as param_handler: 50 with open(inputs, "r") as param_handler:
42 params = json.load(param_handler) 51 params = json.load(param_handler)
43 52
44 input_header = params['header0'] 53 input_header = params["header0"]
45 header = 'infer' if input_header else None 54 header = "infer" if input_header else None
46 55
47 with open(infile) as fp: 56 with open(infile) as fp:
48 lines = fp.read().splitlines() 57 lines = fp.read().splitlines()
49 58
50 if header is not None: 59 if header is not None:
63 # Turn the encoded NumPy array into a DataFrame 72 # Turn the encoded NumPy array into a DataFrame
64 df = pd.DataFrame(te_ary, columns=te.columns_) 73 df = pd.DataFrame(te_ary, columns=te.columns_)
65 74
66 # Extract frequent itemsets for association rule mining 75 # Extract frequent itemsets for association rule mining
67 # use_colnames: Use DataFrames' column names in the returned DataFrame instead of column indices 76 # use_colnames: Use DataFrames' column names in the returned DataFrame instead of column indices
68 frequent_itemsets = fpgrowth(df, min_support=min_support, use_colnames=True, max_len=max_length) 77 frequent_itemsets = fpgrowth(
78 df, min_support=min_support, use_colnames=True, max_len=max_length
79 )
69 80
70 # Get association rules, with confidence larger than min_confidence 81 # Get association rules, with confidence larger than min_confidence
71 rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=min_confidence) 82 rules = association_rules(
83 frequent_itemsets, metric="confidence", min_threshold=min_confidence
84 )
72 85
73 # Filter association rules, keeping rules with lift and conviction larger than min_liftand and min_conviction 86 # Filter association rules, keeping rules with lift and conviction larger than min_liftand and min_conviction
74 rules = rules[(rules['lift'] >= min_lift) & (rules['conviction'] >= min_conviction)] 87 rules = rules[(rules["lift"] >= min_lift) & (rules["conviction"] >= min_conviction)]
75 88
76 # Convert columns from frozenset to list (more readable) 89 # Convert columns from frozenset to list (more readable)
77 rules['antecedents'] = rules['antecedents'].apply(list) 90 rules["antecedents"] = rules["antecedents"].apply(list)
78 rules['consequents'] = rules['consequents'].apply(list) 91 rules["consequents"] = rules["consequents"].apply(list)
79 92
80 # The next 3 steps are intended to fix the order of the association 93 # The next 3 steps are intended to fix the order of the association
81 # rules generated, so tests that rely on diff'ing a desired output 94 # rules generated, so tests that rely on diff'ing a desired output
82 # with an expected output can pass 95 # with an expected output can pass
83 96
84 # 1) Sort entry in every row/column for columns 'antecedents' and 'consequents' 97 # 1) Sort entry in every row/column for columns 'antecedents' and 'consequents'
85 rules['antecedents'] = rules['antecedents'].apply(lambda row: sorted(row)) 98 rules["antecedents"] = rules["antecedents"].apply(lambda row: sorted(row))
86 rules['consequents'] = rules['consequents'].apply(lambda row: sorted(row)) 99 rules["consequents"] = rules["consequents"].apply(lambda row: sorted(row))
87 100
88 # 2) Create two temporary string columns to sort on 101 # 2) Create two temporary string columns to sort on
89 rules['ant_str'] = rules['antecedents'].apply(lambda row: " ".join(row)) 102 rules["ant_str"] = rules["antecedents"].apply(lambda row: " ".join(row))
90 rules['con_str'] = rules['consequents'].apply(lambda row: " ".join(row)) 103 rules["con_str"] = rules["consequents"].apply(lambda row: " ".join(row))
91 104
92 # 3) Sort results so they are re-producable 105 # 3) Sort results so they are re-producable
93 rules.sort_values(by=['ant_str', 'con_str'], inplace=True) 106 rules.sort_values(by=["ant_str", "con_str"], inplace=True)
94 del rules['ant_str'] 107 del rules["ant_str"]
95 del rules['con_str'] 108 del rules["con_str"]
96 rules.reset_index(drop=True, inplace=True) 109 rules.reset_index(drop=True, inplace=True)
97 110
98 # Write association rules and metrics to file 111 # Write association rules and metrics to file
99 rules.to_csv(outfile, sep="\t", index=False) 112 rules.to_csv(outfile, sep="\t", index=False)
100 113
101 114
102 if __name__ == '__main__': 115 if __name__ == "__main__":
103 aparser = argparse.ArgumentParser() 116 aparser = argparse.ArgumentParser()
104 aparser.add_argument("-i", "--inputs", dest="inputs", required=True) 117 aparser.add_argument("-i", "--inputs", dest="inputs", required=True)
105 aparser.add_argument("-y", "--infile", dest="infile", required=True) 118 aparser.add_argument("-y", "--infile", dest="infile", required=True)
106 aparser.add_argument("-o", "--outfile", dest="outfile", required=True) 119 aparser.add_argument("-o", "--outfile", dest="outfile", required=True)
107 aparser.add_argument("-s", "--support", dest="support", default=0.5) 120 aparser.add_argument("-s", "--support", dest="support", default=0.5)
109 aparser.add_argument("-l", "--lift", dest="lift", default=1.0) 122 aparser.add_argument("-l", "--lift", dest="lift", default=1.0)
110 aparser.add_argument("-v", "--conviction", dest="conviction", default=1.0) 123 aparser.add_argument("-v", "--conviction", dest="conviction", default=1.0)
111 aparser.add_argument("-t", "--length", dest="length", default=5) 124 aparser.add_argument("-t", "--length", dest="length", default=5)
112 args = aparser.parse_args() 125 args = aparser.parse_args()
113 126
114 main(args.inputs, args.infile, args.outfile, 127 main(
115 min_support=float(args.support), min_confidence=float(args.confidence), 128 args.inputs,
116 min_lift=float(args.lift), min_conviction=float(args.conviction), max_length=int(args.length)) 129 args.infile,
130 args.outfile,
131 min_support=float(args.support),
132 min_confidence=float(args.confidence),
133 min_lift=float(args.lift),
134 min_conviction=float(args.conviction),
135 max_length=int(args.length),
136 )