comparison pca.py @ 20:cd02bf168d23 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
author bgruening
date Thu, 01 Oct 2020 20:23:20 +0000
parents
children 1d3447c2203c
comparison
equal deleted inserted replaced
19:39ae3c043096 20:cd02bf168d23
1 import argparse
2 import numpy as np
3 from sklearn.decomposition import PCA, IncrementalPCA, KernelPCA
4 from galaxy_ml.utils import read_columns
5
6 def main():
7 parser = argparse.ArgumentParser(description='RDKit screen')
8 parser.add_argument('-i', '--infile',
9 help="Input file")
10 parser.add_argument('--header', action='store_true', help="Include the header row or skip it")
11 parser.add_argument('-c', '--columns', type=str.lower, default='all', choices=['by_index_number', 'all_but_by_index_number',\
12 'by_header_name', 'all_but_by_header_name', 'all_columns'],
13 help="Choose to select all columns, or exclude/include some")
14 parser.add_argument('-ci', '--column_indices', type=str.lower,
15 help="Choose to select all columns, or exclude/include some")
16 parser.add_argument('-n', '--number', nargs='?', type=int, default=None,\
17 help="Number of components to keep. If not set, all components are kept")
18 parser.add_argument('--whiten', action='store_true', help="Whiten the components")
19 parser.add_argument('-t', '--pca_type', type=str.lower, default='classical', choices=['classical', 'incremental', 'kernel'],
20 help="Choose which flavour of PCA to use")
21 parser.add_argument('-s', '--svd_solver', type=str.lower, default='auto', choices=['auto', 'full', 'arpack', 'randomized'],
22 help="Choose the type of svd solver.")
23 parser.add_argument('-b', '--batch_size', nargs='?', type=int, default=None,\
24 help="The number of samples to use for each batch")
25 parser.add_argument('-k', '--kernel', type=str.lower, default='linear',\
26 choices=['linear', 'poly', 'rbf', 'sigmoid', 'cosine', 'precomputed'],
27 help="Choose the type of kernel.")
28 parser.add_argument('-g', '--gamma', nargs='?', type=float, default=None,
29 help='Kernel coefficient for rbf, poly and sigmoid kernels. Ignored by other kernels')
30 parser.add_argument('-tol', '--tolerance', type=float, default=0.0,
31 help='Convergence tolerance for arpack. If 0, optimal value will be chosen by arpack')
32 parser.add_argument('-mi', '--max_iter', nargs='?', type=int, default=None,\
33 help="Maximum number of iterations for arpack")
34 parser.add_argument('-d', '--degree', type=int, default=3,\
35 help="Degree for poly kernels. Ignored by other kernels")
36 parser.add_argument('-cf', '--coef0', type=float, default=1.0,
37 help='Independent term in poly and sigmoid kernels')
38 parser.add_argument('-e', '--eigen_solver', type=str.lower, default='auto', choices=['auto', 'dense', 'arpack'],
39 help="Choose the type of eigen solver.")
40 parser.add_argument('-o', '--outfile',
41 help="Base name for output file (no extension).")
42 args = parser.parse_args()
43
44 usecols = None
45 cols = []
46 pca_params = {}
47
48 if args.columns == 'by_index_number' or args.columns == 'all_but_by_index_number':
49 usecols = [int(i) for i in args.column_indices.split(',')]
50 elif args.columns == 'by_header_name' or args.columns == 'all_but_by_header_name':
51 usecols = args.column_indices
52
53 header = 'infer' if args.header else None
54
55 pca_input = read_columns(
56 f=args.infile,
57 c=usecols,
58 c_option=args.columns,
59 sep='\t',
60 header=header,
61 parse_dates=True,
62 encoding=None,
63 index_col=None)
64
65 pca_params.update({'n_components': args.number})
66
67 if args.pca_type == 'classical':
68 pca_params.update({'svd_solver': args.svd_solver, 'whiten': args.whiten})
69 if args.svd_solver == 'arpack':
70 pca_params.update({'tol': args.tolerance})
71 pca = PCA()
72
73 elif args.pca_type == 'incremental':
74 pca_params.update({'batch_size': args.batch_size, 'whiten': args.whiten})
75 pca = IncrementalPCA()
76
77 elif args.pca_type == 'kernel':
78 pca_params.update({'kernel': args.kernel, 'eigen_solver': args.eigen_solver, 'gamma': args.gamma})
79
80 if args.kernel == 'poly':
81 pca_params.update({'degree': args.degree, 'coef0': args.coef0})
82 elif args.kernel == 'sigmoid':
83 pca_params.update({'coef0': args.coef0})
84 elif args.kernel == 'precomputed':
85 pca_input = np.dot(pca_input, pca_input.T)
86
87 if args.eigen_solver == 'arpack':
88 pca_params.update({'tol': args.tolerance, 'max_iter': args.max_iter})
89
90 pca = KernelPCA()
91
92 print(pca_params)
93 pca.set_params(**pca_params)
94 pca_output = pca.fit_transform(pca_input)
95 np.savetxt(fname=args.outfile, X=pca_output, fmt='%.4f', delimiter='\t')
96
97
98 if __name__ == "__main__":
99 main()