|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import pandas as pd |
| 4 | +import numpy as np |
| 5 | +import csv |
| 6 | +from csv import writer |
| 7 | +import readline |
| 8 | +import re |
| 9 | +import string |
| 10 | + |
| 11 | +import fim |
| 12 | +import functions.microdir as md |
| 13 | +import functions.microfim as mf |
| 14 | +import functions.microimport as mi |
| 15 | +import functions.microinterestmeasures as mim |
| 16 | + |
| 17 | + |
| 18 | +""" microFIM example code on test/test2.csv files |
| 19 | +of microFIM github repository |
| 20 | +Input files to run microFIM: |
| 21 | +- test2.csv |
| 22 | +- transactional file 2 (can be obtained with microFIM_example_code_1.py) |
| 23 | +- metadata_test2.csv |
| 24 | +- parameters_test2.csv |
| 25 | +
|
| 26 | +Default is itemsets patterns (i), but also closed (c) and maximal (m) can be calculated. |
| 27 | +
|
| 28 | +Finally, two pattern tables can be generated: |
| 29 | +- complete pattern table (patterns + interest measures) |
| 30 | +- clean pattern table |
| 31 | +
|
| 32 | +""" |
| 33 | + |
| 34 | +# set fim options |
| 35 | +to_calculate = 'i' # default (can be changed in c or m) |
| 36 | + |
| 37 | +# setting files |
| 38 | +## dir |
| 39 | +set_dir = 'test' |
| 40 | + |
| 41 | +## metadata |
| 42 | +metadata = 'metadata_test2.csv' |
| 43 | +meta_sep = ',' |
| 44 | + |
| 45 | +## otu/esv/taxa table |
| 46 | +data_table_name = 'test2.csv' |
| 47 | +data_sep = ',' |
| 48 | + |
| 49 | +## parameter file |
| 50 | +par_file = 'parameters_test2.csv' |
| 51 | + |
| 52 | +# transactional file |
| 53 | +trans_file = 'transactions_test2' |
| 54 | + |
| 55 | +# set output name |
| 56 | +output_file = 'patterns_test2' |
| 57 | +add_interest_file = 'addm_patterns_test2' |
| 58 | +output_pattern_table = 'pattern_table_test2' |
| 59 | + |
| 60 | + |
| 61 | +# SETTINGS |
| 62 | +## set dir |
| 63 | +data_dir = md.set_inputs_dir_rev(set_dir) |
| 64 | + |
| 65 | +## SET OUTPUT NAME |
| 66 | +file_name = mi.output_file_name(data_table_name) |
| 67 | + |
| 68 | + |
| 69 | +# import files |
| 70 | + |
| 71 | +# IMPORT FILES |
| 72 | +## import metadata |
| 73 | +metadata = mi.import_metadata(metadata, data_dir, meta_sep) |
| 74 | + |
| 75 | +## import data table (otu, esv or taxa table) |
| 76 | +data_table = mi.import_data_table(data_table_name, data_dir, data_sep) |
| 77 | + |
| 78 | +# import parameters file |
| 79 | + |
| 80 | +## import transactions |
| 81 | +t = mf.read_transaction(os.path.join(data_dir, trans_file)) |
| 82 | + |
| 83 | +## import file with paramaters |
| 84 | +minsupp, zmin, zmax= mi.itemsets_parameters(data_dir, par_file) |
| 85 | + |
| 86 | + |
| 87 | +# FILTER DATA TABLE VIA SAMPLE METADATA |
| 88 | +filter_table = mf.filter_data_table(metadata, data_table) |
| 89 | + |
| 90 | + |
| 91 | +# import transactions and file with paramaters |
| 92 | +t = mf.read_transaction(os.path.join(data_dir, trans_file)) |
| 93 | +print(t) |
| 94 | + |
| 95 | +minsupp, zmin, zmax= mi.itemsets_parameters(data_dir, par_file) |
| 96 | + |
| 97 | + |
| 98 | +# calculate patterns |
| 99 | +results = mf.fim_calculation(t, to_calculate, minsupp, zmin, zmax) |
| 100 | + |
| 101 | + |
| 102 | +# write patterns results |
| 103 | +file, out_file, new_out_file = mf.write_results(results, data_dir, output_file) |
| 104 | + |
| 105 | + |
| 106 | +# convert itemsets results into a dataframe |
| 107 | +df = mf.itemsets_dataframe(new_out_file) |
| 108 | +print(df) |
| 109 | + |
| 110 | +# export as a csv |
| 111 | +mf.export_dataframe(df, data_dir, output_file) |
| 112 | + |
| 113 | +print('Results saved as ' + new_out_file + ' in ' + data_dir + '\n\n') |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +# CALCULATE ADDITIONAL METRICS |
| 118 | +## import patterns dataframe |
| 119 | +df = mi.import_pattern_dataframe(data_dir, output_file) |
| 120 | + |
| 121 | +# calculate and add all-confidence values |
| 122 | +data_allc_update = mim.add_interest_measures(data_table, df, trans_file, data_dir) |
| 123 | + |
| 124 | +# export dataframe |
| 125 | +mim.add_table_export(data_allc_update, data_dir, add_interest_file) |
| 126 | +print('Results saved as df_' + add_interest_file + '.csv in ' + data_dir + '\n\n') |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +# GENERATE PATTERN TABLE |
| 131 | +## generate pattern table with patterns and interest measures |
| 132 | +pattern_table_complete = mf.generate_pattern_table(data_allc_update, df, data_dir, trans_file, metadata, meta_sep) |
| 133 | +print(pattern_table_complete) |
| 134 | + |
| 135 | +## export pattern table as cv |
| 136 | +mf.export_pattern_tables(pattern_table_complete, data_dir, output_pattern_table) |
0 commit comments