Skip to content

Commit

Permalink
Adapt python examples to new transformer api
Browse files Browse the repository at this point in the history
  • Loading branch information
vinx13 authored and vigsterkr committed Jun 8, 2018
1 parent 203f260 commit 6636bf4
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 85 deletions.
10 changes: 4 additions & 6 deletions examples/undocumented/python/distance_canberraword.py
Expand Up @@ -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)

Expand Down
8 changes: 3 additions & 5 deletions examples/undocumented/python/distance_hammingword.py
Expand Up @@ -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)

Expand Down
10 changes: 4 additions & 6 deletions examples/undocumented/python/distance_manhattenword.py
Expand Up @@ -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)

Expand Down
11 changes: 4 additions & 7 deletions examples/undocumented/python/kernel_comm_ulong_string.py
Expand Up @@ -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

Expand Down
10 changes: 4 additions & 6 deletions examples/undocumented/python/kernel_comm_word_string.py
Expand Up @@ -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)

Expand Down
10 changes: 4 additions & 6 deletions examples/undocumented/python/kernel_weighted_comm_word_string.py
Expand Up @@ -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)
Expand Down
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions examples/undocumented/python/preprocessor_fisherlda.py
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions examples/undocumented/python/preprocessor_kernelpca.py
Expand Up @@ -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

Expand Down
11 changes: 4 additions & 7 deletions examples/undocumented/python/preprocessor_logplusone.py
Expand Up @@ -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)

Expand Down
8 changes: 3 additions & 5 deletions examples/undocumented/python/preprocessor_normone.py
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions examples/undocumented/python/preprocessor_pca.py
Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions examples/undocumented/python/preprocessor_prunevarsubmean.py
Expand Up @@ -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)

Expand Down
Expand Up @@ -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)

Expand Down
10 changes: 4 additions & 6 deletions examples/undocumented/python/preprocessor_sortulongstring.py
Expand Up @@ -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)

Expand Down
10 changes: 4 additions & 6 deletions examples/undocumented/python/preprocessor_sortwordstring.py
Expand Up @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions examples/undocumented/python/serialization_string_kernels.py
Expand Up @@ -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

Expand Down
Expand Up @@ -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)
Expand Down

0 comments on commit 6636bf4

Please sign in to comment.