Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/**/*.pyc


.idea/

Results.txt
8 changes: 5 additions & 3 deletions TICC_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class TICC:
def __init__(self, window_size=10, number_of_clusters=5, lambda_parameter=11e-2,
beta=400, maxIters=1000, threshold=2e-5, write_out_file=False,
prefix_string="", num_proc=1, compute_BIC=False, cluster_reassignment=20):
prefix_string="", num_proc=1, compute_BIC=False, cluster_reassignment=20, biased=False):
"""
Parameters:
- window_size: size of the sliding window
Expand All @@ -28,6 +28,7 @@ def __init__(self, window_size=10, number_of_clusters=5, lambda_parameter=11e-2,
- write_out_file: (bool) if true, prefix_string is output file dir
- prefix_string: output directory if necessary
- cluster_reassignment: number of points to reassign to a 0 cluster
- biased: Using the biased or the unbiased covariance
"""
self.window_size = window_size
self.number_of_clusters = number_of_clusters
Expand All @@ -41,6 +42,7 @@ def __init__(self, window_size=10, number_of_clusters=5, lambda_parameter=11e-2,
self.compute_BIC = compute_BIC
self.cluster_reassignment = cluster_reassignment
self.num_blocks = self.window_size + 1
self.biased = biased
pd.set_option('display.max_columns', 500)
np.set_printoptions(formatter={'float': lambda x: "{0:0.4f}".format(x)})
np.random.seed(102)
Expand Down Expand Up @@ -328,7 +330,7 @@ def train_clusters(self, cluster_mean_info, cluster_mean_stacked_info, complete_
##Fit a model - OPTIMIZATION
probSize = self.window_size * size_blocks
lamb = np.zeros((probSize, probSize)) + self.lambda_parameter
S = np.cov(np.transpose(D_train))
S = np.cov(np.transpose(D_train), bias=self.biased)
empirical_covariances[cluster] = S

rho = 1
Expand Down Expand Up @@ -398,4 +400,4 @@ def predict_clusters(self, test_data = None):
# Update cluster points - using NEW smoothening
clustered_points = updateClusters(lle_all_points_clusters, switch_penalty=self.switch_penalty)

return(clustered_points)
return(clustered_points)
25 changes: 25 additions & 0 deletions UnitTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ def test_multiExample(self):
#Test failed
self.assertTrue(1==0)

def test_biased_vs_unbiased(self):
fname = "example_data.txt"
unbiased_ticc = TICC(window_size=1, number_of_clusters=8, lambda_parameter=11e-2, beta=600, maxIters=100,
threshold=2e-5,
write_out_file=False, prefix_string="output_folder/", num_proc=1)
(unbiased_cluster_assignment, unbiased_cluster_MRFs) = unbiased_ticc.fit(input_file=fname)

biased_ticc = TICC(window_size=1, number_of_clusters=8, lambda_parameter=11e-2, beta=600, maxIters=100,
threshold=2e-5,
write_out_file=False, prefix_string="output_folder/", num_proc=1, biased=True)
(biased_cluster_assignment, biased_cluster_MRFs) = biased_ticc.fit(input_file=fname)

np.testing.assert_array_equal(np.array(biased_cluster_assignment), np.array(unbiased_cluster_assignment), "Biased assignment is not equel to unbiased assignment!")

def test_failed_unbiased(self):
with self.assertRaises(Exception) as context:
# TICC will fail in Iteration 2, because cluster 9 has only one observation.
fname = "example_data.txt"
ticc = TICC(window_size=1, number_of_clusters=50, lambda_parameter=11e-2, beta=600, maxIters=100,
threshold=2e-5,
write_out_file=False, prefix_string="output_folder/", num_proc=1)
(cluster_assignment, cluster_MRFs) = ticc.fit(input_file=fname)

self.assertTrue('This is broken {}'.format(context.exception))


if __name__ == '__main__':
unittest.main()
Expand Down