def count_matrices(data, start_state=None, threshold=None, display=False):
num_clusters = 2
if threshold is None:
clust = clusterer(data)
state = clust.fit_predict(data.reshape(-1, 1)).reshape(data.shape)
else:
logger.debug("Cluster data based on threshold = {}".format(threshold))
state = data > threshold
init_state = state[:,:,0]
final_state = state[:,:,1]
switched = np.logical_xor(init_state, final_state)
init_state_frac = [np.mean(init_state == ct) for ct in range(num_clusters)]
for ct, fraction in enumerate(init_state_frac):
logger.debug("Initial fraction of state %d: %f" %(ct, fraction))
if start_state is not None and start_state in range(num_clusters):
start_stt = start_state
else:
start_stt = np.argmax(init_state_frac)
logger.debug("Start state set to state: {}".format(start_stt))
logger.debug("Switched state is state: {}".format(1-start_stt))
# This array contains a 2x2 count_matrix for each coordinate tuple
count_mat = np.zeros((init_state.shape[0], 2, 2))
# count_mat = np.zeros((2,2), dtype=np.int)
count_mat[:,0,0] = np.logical_and(init_state == 0, np.logical_not(switched)).sum(axis=-1)
count_mat[:,0,1] = np.logical_and(init_state == 0, switched).sum(axis=-1)
count_mat[:,1,0] = np.logical_and(init_state == 1, switched).sum(axis=-1)
count_mat[:,1,1] = np.logical_and(init_state == 1, np.logical_not(switched)).sum(axis=-1)
return count_mat, start_stt
评论列表
文章目录