This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
forked from ddarmon/transCSSR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo_transCSSR.py
123 lines (89 loc) · 3.75 KB
/
demo_transCSSR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import numpy
import scipy.stats
import itertools
import copy
import string
import os
from collections import Counter, defaultdict
from filter_data_methods import *
from igraph import *
from transCSSR import *
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#
# The various test transducers. Xt is the input
# and Yt is the output.
#
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data_prefix = ''
# Yt_name = 'coinflip_through_even'
# Yt_name = 'coinflip_through_evenflip'
# Yt_name = 'coinflip_through_periodickick'
# Yt_name = 'coinflip_through_periodicevenkick'
# Yt_name = 'even_through_even'
# Yt_name = 'even'
# Yt_name = 'rip'
# Yt_name = 'rip-rev'
# Yt_name = 'barnettY'
# Yt_name = 'even-excite_w_refrac'
Yt_name = 'coinflip-excite_w_refrac'
# Yt_name = 'coinflip'
# Yt_name = 'period4'
# Yt_name = 'golden-mean'
# Yt_name = 'golden-mean-rev'
# Yt_name = 'complex-csm'
# Yt_name = 'tricoin_through_singh-machine'
# Yt_name = 'coinflip_through_floatreset'
Xt_name = 'coinflip'
# Xt_name = 'even'
# Xt_name = ''
# Xt_name = 'barnettX'
# Xt_name = 'even-excite_w_refrac'
# Xt_name = 'tricoin'
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#
# Load in the data for each process.
#
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
stringY = open('data/{}{}.dat'.format(data_prefix, Yt_name)).readline().strip()
if Xt_name == '':
stringX = '0'*len(stringY)
else:
stringX = open('data/{}{}.dat'.format(data_prefix, Xt_name)).readline().strip()
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#
# Set the parameters and associated quantities:
# axs, ays -- the input / output alphabets
# alpha -- the significance level associated with
# CSSR's hypothesis tests.
# L -- The maximum history length to look
# back when inferring predictive
# distributions.
#
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# axs = ['0']
# ays = ['0', '1']
axs = ['0', '1']
ays = ['0', '1']
e_symbols = list(itertools.product(axs, ays)) # All of the possible pairs of emission
# symbols for (x, y)
alpha = 0.001
verbose = False
# L is the maximum amount we want to ever look back.
L_max = 1
Tx = len(stringX); Ty = len(stringY)
assert Tx == Ty, 'The two time series must have the same length.'
T = Tx
word_lookup_marg, word_lookup_fut = estimate_predictive_distributions(stringX, stringY, L_max)
epsilon, invepsilon, morph_by_state = run_transCSSR(word_lookup_marg, word_lookup_fut, L_max, axs, ays, e_symbols, Xt_name, Yt_name, alpha = alpha)
print('The epsilon-transducer has {} states.'.format(len(invepsilon)))
print_morph_by_states(morph_by_state)
prior_pred = [word_lookup_fut[('', ay)] for ay in ays]
prior_pred = numpy.array(prior_pred)/float(numpy.sum(prior_pred))
print('\nDemonstration of filtering...\n')
filtered_states, filtered_probs, stringY_pred = filter_and_predict(stringX, stringY, epsilon, invepsilon, morph_by_state, axs, ays, e_symbols, L_max, prior_pred)
print('t\tS_\{t\}\tP(Y_\{t\} | S_\{t-1\})\that(Y)_\{t\}\tY_\{t\}')
print('-----------------------------------------------------------')
for t_ind in range(int(numpy.min([100, len(stringX)]))):
print(t_ind, filtered_states[t_ind], filtered_probs[t_ind, :], stringY_pred[t_ind], stringY[t_ind])
test_out = run_tests_transCSSR('data/{}{}'.format(data_prefix, Xt_name), 'data/{}{}'.format(data_prefix, Yt_name), epsilon, invepsilon, morph_by_state, axs, ays, e_symbols, L = L_max, L_max = L_max, metric = None, memoryless = False, verbose = True, prior_pred = prior_pred)
preds_all, possible_states_all = filter_and_pred_probs(stringX, stringY, machine_fname = 'transCSSR_results/+{}.dot'.format(Xt_name), transducer_fname = 'transCSSR_results/{}+{}.dot'.format(Xt_name, Yt_name), axs = axs, ays = ays, inf_alg = 'transCSSR')