Skip to content

Commit 4af5e8b

Browse files
author
Tomasz Latkowski
committed
fixed tests
1 parent b6e5899 commit 4af5e8b

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

experiments/experiment.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import tensorflow as tf
22

3-
from experiments.classifier import NeuralNetworkClassifier
4-
from methods.selection_wrapper import SelectionWrapper
53
from methods.selection import fisher, feature_correlation_with_class, t_test, random
4+
from methods.selection_wrapper import SelectionWrapper
65

76
methods = {
87
'fisher': fisher,
@@ -15,7 +14,6 @@
1514
class Experiment:
1615

1716
def __init__(self, experiment_config, num_instances, classifier, dataset):
18-
1917
selection_method = methods[experiment_config['SELECTION']['method']]
2018
num_features = int(experiment_config['SELECTION']['num_features'])
2119
hidden_sizes = int(experiment_config['CLASSIFIER']['hidden_sizes'])

run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ def main():
7070

7171

7272
if __name__ == '__main__':
73-
main()
73+
main()

tests/test_fisher.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import numpy as np
22
import tensorflow as tf
33

4-
from methods.selection import fisher, selection_wrapper
4+
from methods.selection import fisher
5+
from methods.selection_wrapper import SelectionWrapper
56

67

78
class TestFisherSelection(tf.test.TestCase):
@@ -28,10 +29,11 @@ def testFisherPickFirstSignificantFeature(self):
2829

2930
num_instances = [2, 2]
3031
top_k = 1
31-
_, actual_most_significant_features = test_session.run(selection_wrapper(data,
32-
num_instances,
33-
fisher,
34-
num_features=top_k))
32+
selection_wrapper = SelectionWrapper(data,
33+
num_instances,
34+
fisher,
35+
num_features=top_k)
36+
actual_most_significant_features = test_session.run(selection_wrapper.selected_data)
3537
correct_most_significant_features = [[2.], [4.], [6.], [6.]]
3638

3739
self.assertAllEqual(actual_most_significant_features, correct_most_significant_features)

utils/statistics.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,33 @@ def pearson_correlation(x1, x2):
1212
return l / p
1313

1414

15-
def f_test():
16-
pass
15+
def f_test(data, num_instances):
16+
"""
17+
Performs F-statistic between the genes and the classification variable h
18+
as the score of maximum relevance.
19+
20+
:param data:
21+
:param num_instances:
22+
:return:
23+
"""
24+
25+
data = tf.convert_to_tensor(data)
26+
class1, class2 = tf.split(data, num_instances)
27+
K = 2
28+
with tf.name_scope('f_statistic'):
29+
mean1, var1 = tf.nn.moments(class1, axes=0)
30+
mean2, var2 = tf.nn.moments(class2, axes=0)
31+
mean, var = tf.nn.moments(data, axes=0)
32+
33+
pooled_var = pooled_variance(data, num_instances)
34+
tf.reduce_sum(((mean1 - mean) + (mean2 - mean))/(K-1))/pooled_var
35+
36+
37+
def pooled_variance(data, num_instances):
38+
K = len(num_instances)
39+
n = sum(num_instances)
40+
41+
class1, class2 = tf.split(data, num_instances)
42+
mean1, var1 = tf.nn.moments(class1, axes=0)
43+
mean2, var2 = tf.nn.moments(class2, axes=0)
44+
return -1

0 commit comments

Comments
 (0)