From 5233d5cbd9c2d317d2a7bf98e94d892c37f7a830 Mon Sep 17 00:00:00 2001 From: Wuwei Lin Date: Wed, 16 May 2018 14:56:07 +0800 Subject: [PATCH] Adapt python examples to new transformer api --- examples/undocumented/python/distance_canberraword.py | 10 ++++------ examples/undocumented/python/distance_hammingword.py | 8 +++----- .../undocumented/python/distance_manhattenword.py | 10 ++++------ .../undocumented/python/kernel_comm_ulong_string.py | 11 ++++------- .../undocumented/python/kernel_comm_word_string.py | 10 ++++------ .../python/kernel_weighted_comm_word_string.py | 10 ++++------ .../preprocessor_dimensionreductionpreprocessor.py | 4 ++-- .../undocumented/python/preprocessor_fisherlda.py | 6 +++--- .../undocumented/python/preprocessor_kernelpca.py | 4 ++-- .../undocumented/python/preprocessor_logplusone.py | 11 ++++------- examples/undocumented/python/preprocessor_normone.py | 8 +++----- examples/undocumented/python/preprocessor_pca.py | 4 ++-- .../python/preprocessor_prunevarsubmean.py | 8 +++----- .../python/preprocessor_randomfouriergausspreproc.py | 8 +++----- .../python/preprocessor_sortulongstring.py | 10 ++++------ .../python/preprocessor_sortwordstring.py | 10 ++++------ .../python/serialization_string_kernels.py | 5 ++--- .../python/tests_check_commwordkernel_memleak.py | 5 ++--- 18 files changed, 57 insertions(+), 85 deletions(-) diff --git a/examples/undocumented/python/distance_canberraword.py b/examples/undocumented/python/distance_canberraword.py index b01198783b6..a07814eda51 100644 --- a/examples/undocumented/python/distance_canberraword.py +++ b/examples/undocumented/python/distance_canberraword.py @@ -17,17 +17,15 @@ def distance_canberraword (fm_train_dna=traindna,fm_test_dna=testdna,order=3,gap charfeat.set_features(fm_train_dna) feats_train=StringWordFeatures(charfeat.get_alphabet()) feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse) - preproc=SortWordString() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() + preproc = SortWordString() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) charfeat=StringCharFeatures(DNA) charfeat.set_features(fm_test_dna) feats_test=StringWordFeatures(charfeat.get_alphabet()) feats_test.obtain_from_char(charfeat, order-1, order, gap, reverse) - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() + feats_test = preproc.apply(feats_test) distance=CanberraWordDistance(feats_train, feats_train) diff --git a/examples/undocumented/python/distance_hammingword.py b/examples/undocumented/python/distance_hammingword.py index e0534c0cbf9..71f89381c27 100644 --- a/examples/undocumented/python/distance_hammingword.py +++ b/examples/undocumented/python/distance_hammingword.py @@ -21,16 +21,14 @@ def distance_hammingword (fm_train_dna=traindna,fm_test_dna=testdna, feats_train=StringWordFeatures(charfeat.get_alphabet()) feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse) preproc=SortWordString() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) charfeat=StringCharFeatures(DNA) charfeat.set_features(fm_test_dna) feats_test=StringWordFeatures(charfeat.get_alphabet()) feats_test.obtain_from_char(charfeat, order-1, order, gap, reverse) - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() + feats_test = preproc.apply(feats_test) distance=HammingWordDistance(feats_train, feats_train, use_sign) diff --git a/examples/undocumented/python/distance_manhattenword.py b/examples/undocumented/python/distance_manhattenword.py index e377f757285..14a933e4e85 100644 --- a/examples/undocumented/python/distance_manhattenword.py +++ b/examples/undocumented/python/distance_manhattenword.py @@ -11,16 +11,14 @@ def distance_manhattenword (train_fname=traindna,test_fname=testdna,order=3,gap= charfeat=StringCharFeatures(CSVFile(train_fname), DNA) feats_train=StringWordFeatures(charfeat.get_alphabet()) feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse) - preproc=SortWordString() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() + preproc = SortWordString() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) charfeat=StringCharFeatures(CSVFile(test_fname), DNA) feats_test=StringWordFeatures(charfeat.get_alphabet()) feats_test.obtain_from_char(charfeat, order-1, order, gap, reverse) - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() + feats_test = preproc.apply(feats_test) distance=ManhattanWordDistance(feats_train, feats_train) diff --git a/examples/undocumented/python/kernel_comm_ulong_string.py b/examples/undocumented/python/kernel_comm_ulong_string.py index f6f04e45521..44b13f54550 100644 --- a/examples/undocumented/python/kernel_comm_ulong_string.py +++ b/examples/undocumented/python/kernel_comm_ulong_string.py @@ -16,18 +16,15 @@ def kernel_comm_ulong_string (fm_train_dna=traindat,fm_test_dna=testdat, order=3 charfeat.set_features(fm_train_dna) feats_train=StringUlongFeatures(charfeat.get_alphabet()) feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse) - preproc=SortUlongString() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() - + preproc = SortUlongString() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) charfeat=StringCharFeatures(DNA) charfeat.set_features(fm_test_dna) feats_test=StringUlongFeatures(charfeat.get_alphabet()) feats_test.obtain_from_char(charfeat, order-1, order, gap, reverse) - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() + feats_test = preproc.apply(feats_test) use_sign=False diff --git a/examples/undocumented/python/kernel_comm_word_string.py b/examples/undocumented/python/kernel_comm_word_string.py index 8054abb9081..b480a98220a 100644 --- a/examples/undocumented/python/kernel_comm_word_string.py +++ b/examples/undocumented/python/kernel_comm_word_string.py @@ -16,17 +16,15 @@ def kernel_comm_word_string (fm_train_dna=traindat, fm_test_dna=testdat, order=3 charfeat.set_features(fm_train_dna) feats_train=StringWordFeatures(charfeat.get_alphabet()) feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse) - preproc=SortWordString() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() + preproc = SortWordString() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) charfeat=StringCharFeatures(DNA) charfeat.set_features(fm_test_dna) feats_test=StringWordFeatures(charfeat.get_alphabet()) feats_test.obtain_from_char(charfeat, order-1, order, gap, reverse) - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() + feats_test = preproc.apply(feats_test) kernel=CommWordStringKernel(feats_train, feats_train, use_sign) diff --git a/examples/undocumented/python/kernel_weighted_comm_word_string.py b/examples/undocumented/python/kernel_weighted_comm_word_string.py index 763d4705b27..26f374a2ba9 100644 --- a/examples/undocumented/python/kernel_weighted_comm_word_string.py +++ b/examples/undocumented/python/kernel_weighted_comm_word_string.py @@ -14,16 +14,14 @@ def kernel_weighted_comm_word_string (fm_train_dna=traindat,fm_test_dna=testdat, charfeat=StringCharFeatures(fm_train_dna, DNA) feats_train=StringWordFeatures(charfeat.get_alphabet()) feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse) - preproc=SortWordString() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() + preproc = SortWordString() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) charfeat=StringCharFeatures(fm_test_dna, DNA) feats_test=StringWordFeatures(charfeat.get_alphabet()) feats_test.obtain_from_char(charfeat, order-1, order, gap, reverse) - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() + feats_test = preproc.apply(feats_test) use_sign=False kernel=WeightedCommWordStringKernel(feats_train, feats_train, use_sign) diff --git a/examples/undocumented/python/preprocessor_dimensionreductionpreprocessor.py b/examples/undocumented/python/preprocessor_dimensionreductionpreprocessor.py index 577def4dc9a..aed7d68a509 100644 --- a/examples/undocumented/python/preprocessor_dimensionreductionpreprocessor.py +++ b/examples/undocumented/python/preprocessor_dimensionreductionpreprocessor.py @@ -21,8 +21,8 @@ def preprocessor_dimensionreductionpreprocessor (data, k): converter.set_k(k) preprocessor = DimensionReductionPreprocessor(converter) - preprocessor.init(features) - preprocessor.apply_to_feature_matrix(features) + preprocessor.fit(features) + features = preprocessor.apply(features) return features diff --git a/examples/undocumented/python/preprocessor_fisherlda.py b/examples/undocumented/python/preprocessor_fisherlda.py index 5ff12da25dd..645e9711024 100644 --- a/examples/undocumented/python/preprocessor_fisherlda.py +++ b/examples/undocumented/python/preprocessor_fisherlda.py @@ -17,9 +17,9 @@ def preprocessor_fisherlda (data, labels, method): sg_features = RealFeatures(data) sg_labels = MulticlassLabels(labels) - preprocessor=FisherLda(method) - preprocessor.fit(sg_features, sg_labels, 1) - yn=preprocessor.apply_to_feature_matrix(sg_features) + preprocessor=FisherLda(1, method) + preprocessor.fit(sg_features, sg_labels) + yn = preprocessor.apply(sg_features).get_real_matrix('feature_matrix') return yn diff --git a/examples/undocumented/python/preprocessor_kernelpca.py b/examples/undocumented/python/preprocessor_kernelpca.py index 40905167fb2..167eba8f17a 100644 --- a/examples/undocumented/python/preprocessor_kernelpca.py +++ b/examples/undocumented/python/preprocessor_kernelpca.py @@ -16,9 +16,9 @@ def preprocessor_kernelpca (data, threshold, width): kernel = GaussianKernel(features,features,width) preprocessor = KernelPCA(kernel) - preprocessor.init(features) + preprocessor.fit(features) preprocessor.set_target_dim(2) - preprocessor.apply_to_feature_matrix(features) + features = preprocessor.apply(features) return features diff --git a/examples/undocumented/python/preprocessor_logplusone.py b/examples/undocumented/python/preprocessor_logplusone.py index 11115edac6b..96b5e193f95 100644 --- a/examples/undocumented/python/preprocessor_logplusone.py +++ b/examples/undocumented/python/preprocessor_logplusone.py @@ -16,13 +16,10 @@ def preprocessor_logplusone (fm_train_real=traindat,fm_test_real=testdat,width=1 feats_train=RealFeatures(fm_train_real) feats_test=RealFeatures(fm_test_real) - preproc=LogPlusOne() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() - + preproc = LogPlusOne() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) + feats_test = preproc.apply(feats_test) kernel=Chi2Kernel(feats_train, feats_train, width, size_cache) diff --git a/examples/undocumented/python/preprocessor_normone.py b/examples/undocumented/python/preprocessor_normone.py index 24afe917cdd..20f0276b95e 100644 --- a/examples/undocumented/python/preprocessor_normone.py +++ b/examples/undocumented/python/preprocessor_normone.py @@ -17,11 +17,9 @@ def preprocessor_normone (fm_train_real=traindat,fm_test_real=testdat,width=1.4, feats_test=RealFeatures(fm_test_real) preprocessor=NormOne() - preprocessor.init(feats_train) - feats_train.add_preprocessor(preprocessor) - feats_train.apply_preprocessor() - feats_test.add_preprocessor(preprocessor) - feats_test.apply_preprocessor() + preprocessor.fit(feats_train) + feats_train = preprocessor.apply(feats_train) + feats_test = preprocessor.apply(feats_test) kernel=Chi2Kernel(feats_train, feats_train, width, size_cache) diff --git a/examples/undocumented/python/preprocessor_pca.py b/examples/undocumented/python/preprocessor_pca.py index 23149502390..c5895709398 100644 --- a/examples/undocumented/python/preprocessor_pca.py +++ b/examples/undocumented/python/preprocessor_pca.py @@ -13,8 +13,8 @@ def preprocessor_pca (data): features = RealFeatures(data) preprocessor = PCA() - preprocessor.init(features) - preprocessor.apply_to_feature_matrix(features) + preprocessor.fit(features) + features = preprocessor.apply(features) return features diff --git a/examples/undocumented/python/preprocessor_prunevarsubmean.py b/examples/undocumented/python/preprocessor_prunevarsubmean.py index 91bcc0376a4..4f371cc4931 100644 --- a/examples/undocumented/python/preprocessor_prunevarsubmean.py +++ b/examples/undocumented/python/preprocessor_prunevarsubmean.py @@ -16,11 +16,9 @@ def preprocessor_prunevarsubmean (fm_train_real=traindat,fm_test_real=testdat,wi feats_test=RealFeatures(fm_test_real) preproc=PruneVarSubMean() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) + feats_test = preproc.apply(feats_test) kernel=Chi2Kernel(feats_train, feats_train, width, size_cache) diff --git a/examples/undocumented/python/preprocessor_randomfouriergausspreproc.py b/examples/undocumented/python/preprocessor_randomfouriergausspreproc.py index f1e9a0dc656..d45bb9c4b8e 100644 --- a/examples/undocumented/python/preprocessor_randomfouriergausspreproc.py +++ b/examples/undocumented/python/preprocessor_randomfouriergausspreproc.py @@ -19,11 +19,9 @@ def preprocessor_randomfouriergausspreproc (fm_train_real=traindat,fm_test_real= feats_test=RealFeatures(fm_test_real) preproc=RandomFourierGaussPreproc() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) + feats_test = preproc.apply(feats_test) kernel=Chi2Kernel(feats_train, feats_train, width, size_cache) diff --git a/examples/undocumented/python/preprocessor_sortulongstring.py b/examples/undocumented/python/preprocessor_sortulongstring.py index 4b817b2996f..df8d9d3dff8 100644 --- a/examples/undocumented/python/preprocessor_sortulongstring.py +++ b/examples/undocumented/python/preprocessor_sortulongstring.py @@ -24,12 +24,10 @@ def preprocessor_sortulongstring (fm_train_dna=traindna,fm_test_dna=testdna,orde feats_test=StringUlongFeatures(charfeat.get_alphabet()) feats_test.obtain_from_char(charfeat, order-1, order, gap, reverse) - preproc=SortUlongString() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() + preproc = SortUlongString() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) + feats_test = preproc.apply(feats_test) kernel=CommUlongStringKernel(feats_train, feats_train, use_sign) diff --git a/examples/undocumented/python/preprocessor_sortwordstring.py b/examples/undocumented/python/preprocessor_sortwordstring.py index 231d334298a..ab6d1e1fb5e 100644 --- a/examples/undocumented/python/preprocessor_sortwordstring.py +++ b/examples/undocumented/python/preprocessor_sortwordstring.py @@ -16,16 +16,14 @@ def preprocessor_sortwordstring (fm_train_dna=traindna,fm_test_dna=testdna,order charfeat=StringCharFeatures(fm_train_dna, DNA) feats_train=StringWordFeatures(charfeat.get_alphabet()) feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse) - preproc=SortWordString() - preproc.init(feats_train) - feats_train.add_preprocessor(preproc) - feats_train.apply_preprocessor() + preproc = SortWordString() + preproc.fit(feats_train) + feats_train = preproc.apply(feats_train) charfeat=StringCharFeatures(fm_test_dna, DNA) feats_test=StringWordFeatures(charfeat.get_alphabet()) feats_test.obtain_from_char(charfeat, order-1, order, gap, reverse) - feats_test.add_preprocessor(preproc) - feats_test.apply_preprocessor() + feats_test = preproc.apply(feats_test) kernel=CommWordStringKernel(feats_train, feats_train, use_sign) diff --git a/examples/undocumented/python/serialization_string_kernels.py b/examples/undocumented/python/serialization_string_kernels.py index 9971b8532b8..2a87194ab87 100644 --- a/examples/undocumented/python/serialization_string_kernels.py +++ b/examples/undocumented/python/serialization_string_kernels.py @@ -105,9 +105,8 @@ def get_spectrum_features(data, order=3, gap=0, reverse=True): feat = StringWordFeatures(charfeat.get_alphabet()) feat.obtain_from_char(charfeat, order-1, order, gap, reverse) preproc = SortWordString() - preproc.init(feat) - feat.add_preprocessor(preproc) - feat.apply_preprocessor() + preproc.fit(feat) + feat = preproc.apply(feat) return feat diff --git a/examples/undocumented/python/tests_check_commwordkernel_memleak.py b/examples/undocumented/python/tests_check_commwordkernel_memleak.py index 5da5b8b07df..bd0619a99b2 100644 --- a/examples/undocumented/python/tests_check_commwordkernel_memleak.py +++ b/examples/undocumented/python/tests_check_commwordkernel_memleak.py @@ -66,9 +66,8 @@ def tests_check_commwordkernel_memleak (num, order, gap, reverse): #trainudat.io.set_loglevel(MSG_DEBUG) pre = SortWordString() #pre.io.set_loglevel(MSG_DEBUG) - pre.init(trainudat) - trainudat.add_preprocessor(pre) - trainudat.apply_preprocessor() + pre.fit(trainudat) + trainudat = pre.apply(trainudat) spec = CommWordStringKernel(10, False) spec.set_normalizer(IdentityKernelNormalizer()) spec.init(trainudat, trainudat)