diff --git a/examples/3.pautomac_light.train.json b/examples/3.pautomac_light.train.json index e77f61549383993bfd88d1693caf1b8a49cd23f1..56415b8ee3cc099afc1dc377605708d8a08860b4 100644 --- a/examples/3.pautomac_light.train.json +++ b/examples/3.pautomac_light.train.json @@ -1 +1 @@ -{"automaton": {"transitions": [{"numpy.ndarray": {"values": [[0.045121209595118526, -0.24038969827843812, 0.34944999592135073, -0.2811680730534614, -0.2140252337749723], [0.0692580056243754, -0.30062293462828926, 0.20641375368520323, -0.14960814319757162, -0.5580573163749147], [0.029801151921765866, -0.13866480809160275, 0.18362212572805375, -0.20969545230657946, -0.14481622025561033], [0.005699344003197941, -0.023385825120200612, -0.0660066537398175, 0.10749935271465784, -0.1510365460415996], [-0.020086551931478853, 0.09026347555230403, -0.005525585655539282, -0.03135531709030631, 0.24329022420477361]], "dtype": "float64"}}, {"numpy.ndarray": {"values": [[0.07744772079170577, 0.09007073705761888, -0.3047220063293004, 0.2767624549859125, 0.2028939603062788], [-0.0990298048367086, -0.08061846818727912, 0.2585317069225045, -0.12086330214608965, -0.11085207725068019], [-0.061710792028537555, -0.06244151779954693, 0.12007654564862041, 0.0025063746277926833, -0.15679674731455712], [-0.002736973749965318, -0.009005721984277385, -0.0004600329590919212, -0.008550426472005884, -0.05375464678968064], [0.030987327588710818, 0.03972680066723204, -0.049971133509102365, 0.0035769411874977224, 0.14182576205856343]], "dtype": "float64"}}, {"numpy.ndarray": {"values": [[-0.06791915236220329, -0.1135793765908807, 0.3795539260405443, -0.21784979894047046, -0.22977695089937855], [0.11596642335411368, 0.14914956804629215, -0.1335750837668692, -0.008916063072030857, 0.3484153673774847], [0.011730817547426591, 0.01927380053195553, 0.04142658345867102, -0.03534658856098166, 0.02316491010895659], [0.0073289110755416255, 0.005536509132796289, -0.02245608295066666, 0.03611543477693135, -0.03851433900140641], [-0.010589894686551481, -0.010626616553723545, -0.0005431056456618302, -0.02556747670016023, 0.04984888818929077]], "dtype": "float64"}}, {"numpy.ndarray": {"values": [[0.0727621142778044, -0.01571955768557908, 0.07428592814589835, -0.10369861539250237, 0.024753473688334762], [-0.05607105449779192, -0.08896207276035564, 0.2763822539752204, -0.23711255828386232, 0.07372294122304948], [-0.007391294007753285, -0.04874179796387578, -0.6291239733858517, 0.4681627652157708, 0.09251699239093028], [-0.007110224931878148, -0.056233177358980875, -0.3660665856762066, -0.013297798115226096, 0.649103317749269], [0.0023355150085570832, -0.021561151264484168, 0.09096243479437624, -0.3843882349306193, 0.6616477207948629]], "dtype": "float64"}}], "type": "classic", "nbL": 4, "nbS": 5, "initial": {"numpy.ndarray": {"values": [-0.0004934419970497477, 0.003063469710791532, -0.0440739320155803, -0.10777702616547354, -0.0866391379316936], "dtype": "float64"}}, "final": {"numpy.ndarray": {"values": [0.07757136847945034, -0.02422029400314446, -0.4468125366321277, 0.6277320840897538, -0.554674433356226], "dtype": "float64"}}}} \ No newline at end of file +{"automaton": {"nbL": 4, "nbS": 5, "initial": {"numpy.ndarray": {"values": [-0.000493441997049692, 0.003063469710791502, -0.04407393201558057, -0.10777702616547158, -0.0866391379316951], "dtype": "float64"}}, "final": {"numpy.ndarray": {"values": [0.07757136847945678, -0.024220294003121035, -0.4468125366321232, 0.6277320840897611, -0.5546744333562244], "dtype": "float64"}}, "transitions": [{"numpy.ndarray": {"values": [[0.045121209595118054, -0.24038969827844267, 0.3494499959213531, -0.2811680730534573, -0.21402523377497668], [0.06925800562437727, -0.3006229346282947, 0.2064137536852026, -0.14960814319756102, -0.5580573163749218], [0.02980115192176601, -0.13866480809160522, 0.18362212572805475, -0.20969545230657657, -0.14481622025561255], [0.005699344003198416, -0.023385825120200706, -0.06600665373981858, 0.10749935271466031, -0.1510365460416008], [-0.020086551931479287, 0.09026347555230453, -0.005525585655539293, -0.031355317090309115, 0.2432902242047725]], "dtype": "float64"}}, {"numpy.ndarray": {"values": [[0.07744772079170498, 0.09007073705762163, -0.3047220063293005, 0.27676245498591084, 0.20289396030627946], [-0.09902980483670908, -0.08061846818728234, 0.2585317069225073, -0.12086330214608876, -0.11085207725068613], [-0.06171079202853737, -0.06244151779954791, 0.12007654564862096, 0.0025063746277944722, -0.1567967473145574], [-0.0027369737499654224, -0.009005721984277773, -0.0004600329590916909, -0.00855042647200538, -0.053754646789682024], [0.03098732758871066, 0.03972680066723251, -0.049971133509102664, 0.003576941187496189, 0.14182576205856365]], "dtype": "float64"}}, {"numpy.ndarray": {"values": [[-0.06791915236220136, -0.11357937659088249, 0.379553926040543, -0.21784979894046527, -0.229776950899381], [0.11596642335411327, 0.14914956804629298, -0.13357508376686894, -0.008916063072034729, 0.3484153673774827], [0.01173081754742677, 0.019273800531955328, 0.04142658345867112, -0.03534658856098203, 0.02316491010895624], [0.007328911075541722, 0.005536509132796604, -0.022456082950666895, 0.03611543477693201, -0.03851433900140671], [-0.010589894686551596, -0.010626616553723715, -0.0005431056456617233, -0.02556747670016042, 0.049848888189290286]], "dtype": "float64"}}, {"numpy.ndarray": {"values": [[0.07276211427780344, -0.015719557685580397, 0.07428592814590271, -0.10369861539249554, 0.02475347368832667], [-0.05607105449779084, -0.08896207276035853, 0.2763822539752058, -0.23711255828384722, 0.07372294122304912], [-0.007391294007754002, -0.048741797963871694, -0.6291239733858526, 0.46816276521577743, 0.09251699239092943], [-0.007110224931879211, -0.05623317735897968, -0.366066585676203, -0.013297798115225577, 0.649103317749257], [0.0023355150085563733, -0.02156115126448696, 0.09096243479437824, -0.3843882349306287, 0.6616477207948644]], "dtype": "float64"}}], "type": "classic"}} \ No newline at end of file diff --git a/examples/3.pautomac_light.train.yaml b/examples/3.pautomac_light.train.yaml index 125acbb420b9067bb9ee4097e4ed5312dc268f64..c71fe8099efcf0c5e68836c5b24d6936c5a92cbe 100644 --- a/examples/3.pautomac_light.train.yaml +++ b/examples/3.pautomac_light.train.yaml @@ -2,66 +2,66 @@ automaton: final: numpy.ndarray: dtype: float64 - values: [0.07757136847945034, -0.02422029400314446, -0.4468125366321277, 0.6277320840897538, - -0.554674433356226] + values: [0.07757136847945678, -0.024220294003121035, -0.4468125366321232, 0.6277320840897611, + -0.5546744333562244] initial: numpy.ndarray: dtype: float64 - values: [-0.0004934419970497477, 0.003063469710791532, -0.0440739320155803, - -0.10777702616547354, -0.0866391379316936] + values: [-0.000493441997049692, 0.003063469710791502, -0.04407393201558057, + -0.10777702616547158, -0.0866391379316951] nbL: 4 nbS: 5 transitions: - numpy.ndarray: dtype: float64 values: - - [0.045121209595118526, -0.24038969827843812, 0.34944999592135073, -0.2811680730534614, - -0.2140252337749723] - - [0.0692580056243754, -0.30062293462828926, 0.20641375368520323, -0.14960814319757162, - -0.5580573163749147] - - [0.029801151921765866, -0.13866480809160275, 0.18362212572805375, -0.20969545230657946, - -0.14481622025561033] - - [0.005699344003197941, -0.023385825120200612, -0.0660066537398175, 0.10749935271465784, - -0.1510365460415996] - - [-0.020086551931478853, 0.09026347555230403, -0.005525585655539282, -0.03135531709030631, - 0.24329022420477361] + - [0.045121209595118054, -0.24038969827844267, 0.3494499959213531, -0.2811680730534573, + -0.21402523377497668] + - [0.06925800562437727, -0.3006229346282947, 0.2064137536852026, -0.14960814319756102, + -0.5580573163749218] + - [0.02980115192176601, -0.13866480809160522, 0.18362212572805475, -0.20969545230657657, + -0.14481622025561255] + - [0.005699344003198416, -0.023385825120200706, -0.06600665373981858, 0.10749935271466031, + -0.1510365460416008] + - [-0.020086551931479287, 0.09026347555230453, -0.005525585655539293, -0.031355317090309115, + 0.2432902242047725] - numpy.ndarray: dtype: float64 values: - - [0.07744772079170577, 0.09007073705761888, -0.3047220063293004, 0.2767624549859125, - 0.2028939603062788] - - [-0.0990298048367086, -0.08061846818727912, 0.2585317069225045, -0.12086330214608965, - -0.11085207725068019] - - [-0.061710792028537555, -0.06244151779954693, 0.12007654564862041, 0.0025063746277926833, - -0.15679674731455712] - - [-0.002736973749965318, -0.009005721984277385, -0.0004600329590919212, -0.008550426472005884, - -0.05375464678968064] - - [0.030987327588710818, 0.03972680066723204, -0.049971133509102365, 0.0035769411874977224, - 0.14182576205856343] + - [0.07744772079170498, 0.09007073705762163, -0.3047220063293005, 0.27676245498591084, + 0.20289396030627946] + - [-0.09902980483670908, -0.08061846818728234, 0.2585317069225073, -0.12086330214608876, + -0.11085207725068613] + - [-0.06171079202853737, -0.06244151779954791, 0.12007654564862096, 0.0025063746277944722, + -0.1567967473145574] + - [-0.0027369737499654224, -0.009005721984277773, -0.0004600329590916909, -0.00855042647200538, + -0.053754646789682024] + - [0.03098732758871066, 0.03972680066723251, -0.049971133509102664, 0.003576941187496189, + 0.14182576205856365] - numpy.ndarray: dtype: float64 values: - - [-0.06791915236220329, -0.1135793765908807, 0.3795539260405443, -0.21784979894047046, - -0.22977695089937855] - - [0.11596642335411368, 0.14914956804629215, -0.1335750837668692, -0.008916063072030857, - 0.3484153673774847] - - [0.011730817547426591, 0.01927380053195553, 0.04142658345867102, -0.03534658856098166, - 0.02316491010895659] - - [0.0073289110755416255, 0.005536509132796289, -0.02245608295066666, 0.03611543477693135, - -0.03851433900140641] - - [-0.010589894686551481, -0.010626616553723545, -0.0005431056456618302, -0.02556747670016023, - 0.04984888818929077] + - [-0.06791915236220136, -0.11357937659088249, 0.379553926040543, -0.21784979894046527, + -0.229776950899381] + - [0.11596642335411327, 0.14914956804629298, -0.13357508376686894, -0.008916063072034729, + 0.3484153673774827] + - [0.01173081754742677, 0.019273800531955328, 0.04142658345867112, -0.03534658856098203, + 0.02316491010895624] + - [0.007328911075541722, 0.005536509132796604, -0.022456082950666895, 0.03611543477693201, + -0.03851433900140671] + - [-0.010589894686551596, -0.010626616553723715, -0.0005431056456617233, -0.02556747670016042, + 0.049848888189290286] - numpy.ndarray: dtype: float64 values: - - [0.0727621142778044, -0.01571955768557908, 0.07428592814589835, -0.10369861539250237, - 0.024753473688334762] - - [-0.05607105449779192, -0.08896207276035564, 0.2763822539752204, -0.23711255828386232, - 0.07372294122304948] - - [-0.007391294007753285, -0.04874179796387578, -0.6291239733858517, 0.4681627652157708, - 0.09251699239093028] - - [-0.007110224931878148, -0.056233177358980875, -0.3660665856762066, -0.013297798115226096, - 0.649103317749269] - - [0.0023355150085570832, -0.021561151264484168, 0.09096243479437624, -0.3843882349306193, - 0.6616477207948629] + - [0.07276211427780344, -0.015719557685580397, 0.07428592814590271, -0.10369861539249554, + 0.02475347368832667] + - [-0.05607105449779084, -0.08896207276035853, 0.2763822539752058, -0.23711255828384722, + 0.07372294122304912] + - [-0.007391294007754002, -0.048741797963871694, -0.6291239733858526, 0.46816276521577743, + 0.09251699239092943] + - [-0.007110224931879211, -0.05623317735897968, -0.366066585676203, -0.013297798115225577, + 0.649103317749257] + - [0.0023355150085563733, -0.02156115126448696, 0.09096243479437824, -0.3843882349306287, + 0.6616477207948644] type: classic diff --git a/examples/automaton_io.py b/examples/automaton_io.py index 59559e3970797bf427256a106a6a6fbfcf859998..66eb5db268543f69e72ef9d14c103308dec87699 100644 --- a/examples/automaton_io.py +++ b/examples/automaton_io.py @@ -1,11 +1,37 @@ from splearn.automaton import Automaton from graphviz import Source +from splearn.hankel import Hankel -input_file = 'simple_automata' -format = ['json', 'yaml'] -for f in format: - A = Automaton.read(input_file + "." + f, format=f) - dot = A.get_dot(title = "Simple Automata") +title = "Simple Example" +input_file = 'simple_example' +A = Automaton.SimpleExample() +formats = ['json', 'yaml'] +words = [[], [0], [0, 0], [1], [1, 1], [0, 1, 0], [1, 0, 1]] +d_str = "Value for word \"{:s}\" = {:.2f}" +# read simple automata in each format and write hankel +for f in formats: + #A = Automaton.read(input_file + "." + f, format=f) + Automaton.write(A, input_file + '.' + f, format = f) + dot = A.get_dot(title = title) src = Source(dot) - src.render(input_file + '.' + f + '.gv', view=True) + src.render(input_file + '.' + f + '.gv', view=False) + # Display some values on words + print("First Automata") + for w in words: + print(d_str.format(str(w), A.val(w))) + H = A.to_hankel([(), (0,), (1,)], [(), (0,), (1,)]) + Hankel.write(H, input_file + "_hankel" + "." + f, format=f) + +# read hankel and generate Automata +for f in formats: + H = Hankel.read(input_file + "_hankel" + "." + f, format = f) + A = H.to_automaton(2) + dot = A.get_dot(title = title) + src = Source(dot) + src.render(input_file + '-2.' + f + '.gv', view=False) + # Display some values on words + print("Second Automata") + for w in words: + print(d_str.format(str(w), A.val(w))) + diff --git a/examples/json_save.ipynb b/examples/json_save.ipynb index 1af50f640ead04aa0456773561b9b06f5926ce06..d42284d42a8fee69e4d145156c91fb7cb05c0d68 100644 --- a/examples/json_save.ipynb +++ b/examples/json_save.ipynb @@ -39,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -48,7 +48,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -84,7 +84,7 @@ "True" ] }, - "execution_count": 7, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -95,16 +95,16 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'{\"automaton\": {\"transitions\": [{\"numpy.ndarray\": {\"values\": [[0.045121209595118526, -0.24038969827843812, 0.34944999592135073, -0.2811680730534614, -0.2140252337749723], [0.0692580056243754, -0.30062293462828926, 0.20641375368520323, -0.14960814319757162, -0.5580573163749147], [0.029801151921765866, -0.13866480809160275, 0.18362212572805375, -0.20969545230657946, -0.14481622025561033], [0.005699344003197941, -0.023385825120200612, -0.0660066537398175, 0.10749935271465784, -0.1510365460415996], [-0.020086551931478853, 0.09026347555230403, -0.005525585655539282, -0.03135531709030631, 0.24329022420477361]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[0.07744772079170577, 0.09007073705761888, -0.3047220063293004, 0.2767624549859125, 0.2028939603062788], [-0.0990298048367086, -0.08061846818727912, 0.2585317069225045, -0.12086330214608965, -0.11085207725068019], [-0.061710792028537555, -0.06244151779954693, 0.12007654564862041, 0.0025063746277926833, -0.15679674731455712], [-0.002736973749965318, -0.009005721984277385, -0.0004600329590919212, -0.008550426472005884, -0.05375464678968064], [0.030987327588710818, 0.03972680066723204, -0.049971133509102365, 0.0035769411874977224, 0.14182576205856343]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[-0.06791915236220329, -0.1135793765908807, 0.3795539260405443, -0.21784979894047046, -0.22977695089937855], [0.11596642335411368, 0.14914956804629215, -0.1335750837668692, -0.008916063072030857, 0.3484153673774847], [0.011730817547426591, 0.01927380053195553, 0.04142658345867102, -0.03534658856098166, 0.02316491010895659], [0.0073289110755416255, 0.005536509132796289, -0.02245608295066666, 0.03611543477693135, -0.03851433900140641], [-0.010589894686551481, -0.010626616553723545, -0.0005431056456618302, -0.02556747670016023, 0.04984888818929077]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[0.0727621142778044, -0.01571955768557908, 0.07428592814589835, -0.10369861539250237, 0.024753473688334762], [-0.05607105449779192, -0.08896207276035564, 0.2763822539752204, -0.23711255828386232, 0.07372294122304948], [-0.007391294007753285, -0.04874179796387578, -0.6291239733858517, 0.4681627652157708, 0.09251699239093028], [-0.007110224931878148, -0.056233177358980875, -0.3660665856762066, -0.013297798115226096, 0.649103317749269], [0.0023355150085570832, -0.021561151264484168, 0.09096243479437624, -0.3843882349306193, 0.6616477207948629]], \"dtype\": \"float64\"}}], \"type\": \"classic\", \"nbL\": 4, \"nbS\": 5, \"initial\": {\"numpy.ndarray\": {\"values\": [-0.0004934419970497477, 0.003063469710791532, -0.0440739320155803, -0.10777702616547354, -0.0866391379316936], \"dtype\": \"float64\"}}, \"final\": {\"numpy.ndarray\": {\"values\": [0.07757136847945034, -0.02422029400314446, -0.4468125366321277, 0.6277320840897538, -0.554674433356226], \"dtype\": \"float64\"}}}}'" + "'{\"automaton\": {\"nbL\": 4, \"nbS\": 5, \"initial\": {\"numpy.ndarray\": {\"values\": [-0.000493441997049692, 0.003063469710791502, -0.04407393201558057, -0.10777702616547158, -0.0866391379316951], \"dtype\": \"float64\"}}, \"final\": {\"numpy.ndarray\": {\"values\": [0.07757136847945678, -0.024220294003121035, -0.4468125366321232, 0.6277320840897611, -0.5546744333562244], \"dtype\": \"float64\"}}, \"transitions\": [{\"numpy.ndarray\": {\"values\": [[0.045121209595118054, -0.24038969827844267, 0.3494499959213531, -0.2811680730534573, -0.21402523377497668], [0.06925800562437727, -0.3006229346282947, 0.2064137536852026, -0.14960814319756102, -0.5580573163749218], [0.02980115192176601, -0.13866480809160522, 0.18362212572805475, -0.20969545230657657, -0.14481622025561255], [0.005699344003198416, -0.023385825120200706, -0.06600665373981858, 0.10749935271466031, -0.1510365460416008], [-0.020086551931479287, 0.09026347555230453, -0.005525585655539293, -0.031355317090309115, 0.2432902242047725]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[0.07744772079170498, 0.09007073705762163, -0.3047220063293005, 0.27676245498591084, 0.20289396030627946], [-0.09902980483670908, -0.08061846818728234, 0.2585317069225073, -0.12086330214608876, -0.11085207725068613], [-0.06171079202853737, -0.06244151779954791, 0.12007654564862096, 0.0025063746277944722, -0.1567967473145574], [-0.0027369737499654224, -0.009005721984277773, -0.0004600329590916909, -0.00855042647200538, -0.053754646789682024], [0.03098732758871066, 0.03972680066723251, -0.049971133509102664, 0.003576941187496189, 0.14182576205856365]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[-0.06791915236220136, -0.11357937659088249, 0.379553926040543, -0.21784979894046527, -0.229776950899381], [0.11596642335411327, 0.14914956804629298, -0.13357508376686894, -0.008916063072034729, 0.3484153673774827], [0.01173081754742677, 0.019273800531955328, 0.04142658345867112, -0.03534658856098203, 0.02316491010895624], [0.007328911075541722, 0.005536509132796604, -0.022456082950666895, 0.03611543477693201, -0.03851433900140671], [-0.010589894686551596, -0.010626616553723715, -0.0005431056456617233, -0.02556747670016042, 0.049848888189290286]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[0.07276211427780344, -0.015719557685580397, 0.07428592814590271, -0.10369861539249554, 0.02475347368832667], [-0.05607105449779084, -0.08896207276035853, 0.2763822539752058, -0.23711255828384722, 0.07372294122304912], [-0.007391294007754002, -0.048741797963871694, -0.6291239733858526, 0.46816276521577743, 0.09251699239092943], [-0.007110224931879211, -0.05623317735897968, -0.366066585676203, -0.013297798115225577, 0.649103317749257], [0.0023355150085563733, -0.02156115126448696, 0.09096243479437824, -0.3843882349306287, 0.6616477207948644]], \"dtype\": \"float64\"}}], \"type\": \"classic\"}}'" ] }, - "execution_count": 8, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -115,16 +115,16 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'{\"automaton\": {\"transitions\": [{\"numpy.ndarray\": {\"values\": [[0.045121209595118526, -0.24038969827843812, 0.34944999592135073, -0.2811680730534614, -0.2140252337749723], [0.0692580056243754, -0.30062293462828926, 0.20641375368520323, -0.14960814319757162, -0.5580573163749147], [0.029801151921765866, -0.13866480809160275, 0.18362212572805375, -0.20969545230657946, -0.14481622025561033], [0.005699344003197941, -0.023385825120200612, -0.0660066537398175, 0.10749935271465784, -0.1510365460415996], [-0.020086551931478853, 0.09026347555230403, -0.005525585655539282, -0.03135531709030631, 0.24329022420477361]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[0.07744772079170577, 0.09007073705761888, -0.3047220063293004, 0.2767624549859125, 0.2028939603062788], [-0.0990298048367086, -0.08061846818727912, 0.2585317069225045, -0.12086330214608965, -0.11085207725068019], [-0.061710792028537555, -0.06244151779954693, 0.12007654564862041, 0.0025063746277926833, -0.15679674731455712], [-0.002736973749965318, -0.009005721984277385, -0.0004600329590919212, -0.008550426472005884, -0.05375464678968064], [0.030987327588710818, 0.03972680066723204, -0.049971133509102365, 0.0035769411874977224, 0.14182576205856343]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[-0.06791915236220329, -0.1135793765908807, 0.3795539260405443, -0.21784979894047046, -0.22977695089937855], [0.11596642335411368, 0.14914956804629215, -0.1335750837668692, -0.008916063072030857, 0.3484153673774847], [0.011730817547426591, 0.01927380053195553, 0.04142658345867102, -0.03534658856098166, 0.02316491010895659], [0.0073289110755416255, 0.005536509132796289, -0.02245608295066666, 0.03611543477693135, -0.03851433900140641], [-0.010589894686551481, -0.010626616553723545, -0.0005431056456618302, -0.02556747670016023, 0.04984888818929077]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[0.0727621142778044, -0.01571955768557908, 0.07428592814589835, -0.10369861539250237, 0.024753473688334762], [-0.05607105449779192, -0.08896207276035564, 0.2763822539752204, -0.23711255828386232, 0.07372294122304948], [-0.007391294007753285, -0.04874179796387578, -0.6291239733858517, 0.4681627652157708, 0.09251699239093028], [-0.007110224931878148, -0.056233177358980875, -0.3660665856762066, -0.013297798115226096, 0.649103317749269], [0.0023355150085570832, -0.021561151264484168, 0.09096243479437624, -0.3843882349306193, 0.6616477207948629]], \"dtype\": \"float64\"}}], \"type\": \"classic\", \"nbL\": 4, \"nbS\": 5, \"initial\": {\"numpy.ndarray\": {\"values\": [-0.0004934419970497477, 0.003063469710791532, -0.0440739320155803, -0.10777702616547354, -0.0866391379316936], \"dtype\": \"float64\"}}, \"final\": {\"numpy.ndarray\": {\"values\": [0.07757136847945034, -0.02422029400314446, -0.4468125366321277, 0.6277320840897538, -0.554674433356226], \"dtype\": \"float64\"}}}}'" + "'{\"automaton\": {\"nbL\": 4, \"nbS\": 5, \"initial\": {\"numpy.ndarray\": {\"values\": [-0.000493441997049692, 0.003063469710791502, -0.04407393201558057, -0.10777702616547158, -0.0866391379316951], \"dtype\": \"float64\"}}, \"final\": {\"numpy.ndarray\": {\"values\": [0.07757136847945678, -0.024220294003121035, -0.4468125366321232, 0.6277320840897611, -0.5546744333562244], \"dtype\": \"float64\"}}, \"transitions\": [{\"numpy.ndarray\": {\"values\": [[0.045121209595118054, -0.24038969827844267, 0.3494499959213531, -0.2811680730534573, -0.21402523377497668], [0.06925800562437727, -0.3006229346282947, 0.2064137536852026, -0.14960814319756102, -0.5580573163749218], [0.02980115192176601, -0.13866480809160522, 0.18362212572805475, -0.20969545230657657, -0.14481622025561255], [0.005699344003198416, -0.023385825120200706, -0.06600665373981858, 0.10749935271466031, -0.1510365460416008], [-0.020086551931479287, 0.09026347555230453, -0.005525585655539293, -0.031355317090309115, 0.2432902242047725]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[0.07744772079170498, 0.09007073705762163, -0.3047220063293005, 0.27676245498591084, 0.20289396030627946], [-0.09902980483670908, -0.08061846818728234, 0.2585317069225073, -0.12086330214608876, -0.11085207725068613], [-0.06171079202853737, -0.06244151779954791, 0.12007654564862096, 0.0025063746277944722, -0.1567967473145574], [-0.0027369737499654224, -0.009005721984277773, -0.0004600329590916909, -0.00855042647200538, -0.053754646789682024], [0.03098732758871066, 0.03972680066723251, -0.049971133509102664, 0.003576941187496189, 0.14182576205856365]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[-0.06791915236220136, -0.11357937659088249, 0.379553926040543, -0.21784979894046527, -0.229776950899381], [0.11596642335411327, 0.14914956804629298, -0.13357508376686894, -0.008916063072034729, 0.3484153673774827], [0.01173081754742677, 0.019273800531955328, 0.04142658345867112, -0.03534658856098203, 0.02316491010895624], [0.007328911075541722, 0.005536509132796604, -0.022456082950666895, 0.03611543477693201, -0.03851433900140671], [-0.010589894686551596, -0.010626616553723715, -0.0005431056456617233, -0.02556747670016042, 0.049848888189290286]], \"dtype\": \"float64\"}}, {\"numpy.ndarray\": {\"values\": [[0.07276211427780344, -0.015719557685580397, 0.07428592814590271, -0.10369861539249554, 0.02475347368832667], [-0.05607105449779084, -0.08896207276035853, 0.2763822539752058, -0.23711255828384722, 0.07372294122304912], [-0.007391294007754002, -0.048741797963871694, -0.6291239733858526, 0.46816276521577743, 0.09251699239092943], [-0.007110224931879211, -0.05623317735897968, -0.366066585676203, -0.013297798115225577, 0.649103317749257], [0.0023355150085563733, -0.02156115126448696, 0.09096243479437824, -0.3843882349306287, 0.6616477207948644]], \"dtype\": \"float64\"}}], \"type\": \"classic\"}}'" ] }, - "execution_count": 9, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -135,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -144,7 +144,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -155,68 +155,68 @@ " final:\n", " numpy.ndarray:\n", " dtype: float64\n", - " values: [0.07757136847945034, -0.02422029400314446, -0.4468125366321277, 0.6277320840897538,\n", - " -0.554674433356226]\n", + " values: [0.07757136847945678, -0.024220294003121035, -0.4468125366321232, 0.6277320840897611,\n", + " -0.5546744333562244]\n", " initial:\n", " numpy.ndarray:\n", " dtype: float64\n", - " values: [-0.0004934419970497477, 0.003063469710791532, -0.0440739320155803,\n", - " -0.10777702616547354, -0.0866391379316936]\n", + " values: [-0.000493441997049692, 0.003063469710791502, -0.04407393201558057,\n", + " -0.10777702616547158, -0.0866391379316951]\n", " nbL: 4\n", " nbS: 5\n", " transitions:\n", " - numpy.ndarray:\n", " dtype: float64\n", " values:\n", - " - [0.045121209595118526, -0.24038969827843812, 0.34944999592135073, -0.2811680730534614,\n", - " -0.2140252337749723]\n", - " - [0.0692580056243754, -0.30062293462828926, 0.20641375368520323, -0.14960814319757162,\n", - " -0.5580573163749147]\n", - " - [0.029801151921765866, -0.13866480809160275, 0.18362212572805375, -0.20969545230657946,\n", - " -0.14481622025561033]\n", - " - [0.005699344003197941, -0.023385825120200612, -0.0660066537398175, 0.10749935271465784,\n", - " -0.1510365460415996]\n", - " - [-0.020086551931478853, 0.09026347555230403, -0.005525585655539282, -0.03135531709030631,\n", - " 0.24329022420477361]\n", + " - [0.045121209595118054, -0.24038969827844267, 0.3494499959213531, -0.2811680730534573,\n", + " -0.21402523377497668]\n", + " - [0.06925800562437727, -0.3006229346282947, 0.2064137536852026, -0.14960814319756102,\n", + " -0.5580573163749218]\n", + " - [0.02980115192176601, -0.13866480809160522, 0.18362212572805475, -0.20969545230657657,\n", + " -0.14481622025561255]\n", + " - [0.005699344003198416, -0.023385825120200706, -0.06600665373981858, 0.10749935271466031,\n", + " -0.1510365460416008]\n", + " - [-0.020086551931479287, 0.09026347555230453, -0.005525585655539293, -0.031355317090309115,\n", + " 0.2432902242047725]\n", " - numpy.ndarray:\n", " dtype: float64\n", " values:\n", - " - [0.07744772079170577, 0.09007073705761888, -0.3047220063293004, 0.2767624549859125,\n", - " 0.2028939603062788]\n", - " - [-0.0990298048367086, -0.08061846818727912, 0.2585317069225045, -0.12086330214608965,\n", - " -0.11085207725068019]\n", - " - [-0.061710792028537555, -0.06244151779954693, 0.12007654564862041, 0.0025063746277926833,\n", - " -0.15679674731455712]\n", - " - [-0.002736973749965318, -0.009005721984277385, -0.0004600329590919212, -0.008550426472005884,\n", - " -0.05375464678968064]\n", - " - [0.030987327588710818, 0.03972680066723204, -0.049971133509102365, 0.0035769411874977224,\n", - " 0.14182576205856343]\n", + " - [0.07744772079170498, 0.09007073705762163, -0.3047220063293005, 0.27676245498591084,\n", + " 0.20289396030627946]\n", + " - [-0.09902980483670908, -0.08061846818728234, 0.2585317069225073, -0.12086330214608876,\n", + " -0.11085207725068613]\n", + " - [-0.06171079202853737, -0.06244151779954791, 0.12007654564862096, 0.0025063746277944722,\n", + " -0.1567967473145574]\n", + " - [-0.0027369737499654224, -0.009005721984277773, -0.0004600329590916909, -0.00855042647200538,\n", + " -0.053754646789682024]\n", + " - [0.03098732758871066, 0.03972680066723251, -0.049971133509102664, 0.003576941187496189,\n", + " 0.14182576205856365]\n", " - numpy.ndarray:\n", " dtype: float64\n", " values:\n", - " - [-0.06791915236220329, -0.1135793765908807, 0.3795539260405443, -0.21784979894047046,\n", - " -0.22977695089937855]\n", - " - [0.11596642335411368, 0.14914956804629215, -0.1335750837668692, -0.008916063072030857,\n", - " 0.3484153673774847]\n", - " - [0.011730817547426591, 0.01927380053195553, 0.04142658345867102, -0.03534658856098166,\n", - " 0.02316491010895659]\n", - " - [0.0073289110755416255, 0.005536509132796289, -0.02245608295066666, 0.03611543477693135,\n", - " -0.03851433900140641]\n", - " - [-0.010589894686551481, -0.010626616553723545, -0.0005431056456618302, -0.02556747670016023,\n", - " 0.04984888818929077]\n", + " - [-0.06791915236220136, -0.11357937659088249, 0.379553926040543, -0.21784979894046527,\n", + " -0.229776950899381]\n", + " - [0.11596642335411327, 0.14914956804629298, -0.13357508376686894, -0.008916063072034729,\n", + " 0.3484153673774827]\n", + " - [0.01173081754742677, 0.019273800531955328, 0.04142658345867112, -0.03534658856098203,\n", + " 0.02316491010895624]\n", + " - [0.007328911075541722, 0.005536509132796604, -0.022456082950666895, 0.03611543477693201,\n", + " -0.03851433900140671]\n", + " - [-0.010589894686551596, -0.010626616553723715, -0.0005431056456617233, -0.02556747670016042,\n", + " 0.049848888189290286]\n", " - numpy.ndarray:\n", " dtype: float64\n", " values:\n", - " - [0.0727621142778044, -0.01571955768557908, 0.07428592814589835, -0.10369861539250237,\n", - " 0.024753473688334762]\n", - " - [-0.05607105449779192, -0.08896207276035564, 0.2763822539752204, -0.23711255828386232,\n", - " 0.07372294122304948]\n", - " - [-0.007391294007753285, -0.04874179796387578, -0.6291239733858517, 0.4681627652157708,\n", - " 0.09251699239093028]\n", - " - [-0.007110224931878148, -0.056233177358980875, -0.3660665856762066, -0.013297798115226096,\n", - " 0.649103317749269]\n", - " - [0.0023355150085570832, -0.021561151264484168, 0.09096243479437624, -0.3843882349306193,\n", - " 0.6616477207948629]\n", + " - [0.07276211427780344, -0.015719557685580397, 0.07428592814590271, -0.10369861539249554,\n", + " 0.02475347368832667]\n", + " - [-0.05607105449779084, -0.08896207276035853, 0.2763822539752058, -0.23711255828384722,\n", + " 0.07372294122304912]\n", + " - [-0.007391294007754002, -0.048741797963871694, -0.6291239733858526, 0.46816276521577743,\n", + " 0.09251699239092943]\n", + " - [-0.007110224931879211, -0.05623317735897968, -0.366066585676203, -0.013297798115225577,\n", + " 0.649103317749257]\n", + " - [0.0023355150085563733, -0.02156115126448696, 0.09096243479437824, -0.3843882349306287,\n", + " 0.6616477207948644]\n", " type: classic\n", "\n" ] @@ -228,7 +228,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -237,7 +237,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -265,7 +265,7 @@ " [ 0.00233552, -0.02156115, 0.09096243, -0.38438823, 0.66164772]])]" ] }, - "execution_count": 14, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -276,7 +276,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -285,7 +285,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -294,7 +294,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -322,7 +322,7 @@ " [ 0.00233552, -0.02156115, 0.09096243, -0.38438823, 0.66164772]])]" ] }, - "execution_count": 19, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -355,7 +355,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.2" + "version": "3.6.3" } }, "nbformat": 4, diff --git a/examples/simple_automata.json.gv.pdf b/examples/simple_automata.json.gv.pdf index bcedcde8006bcfa94c68c2c82199bd53b985503d..9253c0b86d51eaa9a3f4014c4c4202bbf69c7aa4 100644 Binary files a/examples/simple_automata.json.gv.pdf and b/examples/simple_automata.json.gv.pdf differ diff --git a/examples/simple_automata.yaml.gv.pdf b/examples/simple_automata.yaml.gv.pdf index bcedcde8006bcfa94c68c2c82199bd53b985503d..9253c0b86d51eaa9a3f4014c4c4202bbf69c7aa4 100644 Binary files a/examples/simple_automata.yaml.gv.pdf and b/examples/simple_automata.yaml.gv.pdf differ diff --git a/splearn/automaton.py b/splearn/automaton.py index 35e48e36c68df2d4a63298b13c9502ad47bf5431..9a980d7b9f74a1b0fe10d43a8903a51a35dd36bf 100644 --- a/splearn/automaton.py +++ b/splearn/automaton.py @@ -304,8 +304,8 @@ class Automaton(object): :rtype: Hankel """ from splearn.hankel import Hankel - lhankels = self.BuildHankels(lrows, lcolumns) - return Hankel(mode_quiet=mode_quiet, lhankels=lhankels, rank=self.nbS) + lhankel = self.BuildHankels(lrows, lcolumns) + return Hankel(mode_quiet=mode_quiet, lhankel=lhankel) def mirror(self): """ Compute the mirror automaton diff --git a/splearn/hankel.py b/splearn/hankel.py index 41d58ea824dc070c6ca485e8399eece504b3abc7..d91ff7220854c622f06f92c8a0e18594902c5431 100644 --- a/splearn/hankel.py +++ b/splearn/hankel.py @@ -48,22 +48,21 @@ class Hankel(object): :param boolean mode_quiet: (default value = False) True for no output message. :param list of all Hankel matrices. At least one of the two parameters - *sample_instance* or *lhankels* has to be not None. If *sample_instance* is given, + *sample_instance* or *lhankel* has to be not None. If *sample_instance* is given, the **Hankel** instance is built directly from the sample dictionnary, - else it is deduced from the *lhankels* list of matrices. - :param int rank: the rank of the Hankel matrices. Required if **Hankel** is build from *lhankels*. + else it is deduced from the *lhankels* list of matrices. """ def __init__( self, sample_instance=None, lrows=[], lcolumns=[], version="classic", partial=False, - sparse=False, mode_quiet=False, lhankels=None, rank = -1): + sparse=False, mode_quiet=False, lhankel=None): self.version = version self.partial = partial self.sparse = sparse - self.buildFromSample = True + self.build_from_sample = True if sample_instance is not None: # Size of the alphabet self.nbL = sample_instance.nbL @@ -75,15 +74,12 @@ class Hankel(object): fact=sample_instance.fact, lrows=lrows, lcolumns=lcolumns, mode_quiet=mode_quiet) - elif lhankels is not None: + elif lhankel is not None: # Size of the alphabet - self.nbL = len(lhankels) - 1 - # Number of samples - if rank == -1: - raise ValueError("Building an Hankel instance from list of hankel matrices required a valid rank.") - self.nbS = rank - self.lhankel = lhankels - self.buildFromSample = False + self.nbL = len(lhankel) - 1 + self.lhankel = lhankel + self.build_from_sample = False + self._nbEx = -1 else: raise ValueError("At least sample_instance or lhankel has to be not None.") @@ -114,6 +110,18 @@ class Hankel(object): raise ValueError("The number of examples should be " + " an integer >= 0") self._nbEx = nbEx + + @property + def build_from_sample(self): + return self._build_from_sample + + @build_from_sample.setter + def build_from_sample(self, val): + if val: + self._build_from_sample = True + else: + self._build_from_sample = False + def build(self, sample, pref, suff, fact, lrows, lcolumns, mode_quiet): @@ -265,12 +273,12 @@ class Hankel(object): return (drows, dcolumns) - def to_automaton(self, rank, mode_quiet): + def to_automaton(self, rank, mode_quiet=False): """ Return an automaton from the current Hankel matrix - Input: - :param int rank: the ranking number + :param int rank: the matrix rank :param boolean mode_quiet: True for no output message. - Output: @@ -322,7 +330,7 @@ class Hankel(object): A = Automaton(nbL=self.nbL, nbS=rank, initial=init, final=term, transitions=trans, type=self.version) - if self.buildFromSample: + if self.build_from_sample: A.initial = A.initial / self.nbEx if self.version == "prefix": A = A.transformation(source="prefix", target="classic") @@ -333,3 +341,47 @@ class Hankel(object): if not mode_quiet: print ("End of Automaton computation") return A + + @staticmethod + def write(hankel_in, filename, format='json'): + """ write input hankel into a file with the given format. + + - Input: + + :param Hankel hankel_in: hankel to write into the file + :param str filename: the name of the file. If it does not exist, + the file is created. + :param str format: 'json' or yaml' + """ + from splearn.serializer import Serializer + if format == 'json': + data_str = Serializer.data_to_json(hankel_in) + elif format == 'yaml': + data_str = Serializer.data_to_yaml(hankel_in) + else: + raise ValueError("Invalid input format. Should be \"json\" or \"yaml\"") + with open(filename, 'w') as outfile: + outfile.write(data_str) + + @staticmethod + def read(filename, format='json'): + """ return a Hankel build with attributes read from a file + + - Input: + + :param str filename: the name of the input file. + :param str format: 'json' or yaml' + + - Output: + + :returns: the output hankel + :rtype: Hankel + """ + from splearn.serializer import Serializer + with open(filename) as infile: + datastr = infile.read() + if format == 'json': + return Serializer.json_to_data(datastr) + if format == 'yaml': + return Serializer.yaml_to_data(datastr) + raise ValueError("Invalid input format. Should be \"json\" or \"yaml\"") diff --git a/splearn/serializer.py b/splearn/serializer.py index 2ba5e90d7a6a2d201fe4ec2ad7b72af00ae54b2e..5109cdf3fc685b268719ad38f6db69affe3282b0 100644 --- a/splearn/serializer.py +++ b/splearn/serializer.py @@ -5,6 +5,8 @@ import numpy as np from splearn.automaton import Automaton +from splearn.hankel import Hankel +from numpy.f2py.common_rules import findcommonblocks class Serializer(object): """ Serializer is an helping object for data serialization @@ -32,6 +34,11 @@ class Serializer(object): data_dict = {"nbL":data.nbL, "nbS":data.nbS, "initial":data.initial, "final":data.final, "transitions":data.transitions, "type":data.type} return {"automaton" : Serializer.__serialize(data_dict)} + if isinstance(data, Hankel): + data_dict = {"nbL":data.nbL, "lhankel" : data.lhankel, "version" : data.version, + "partial" : data.partial, "sparse" : data.sparse, + "build_from_sample" : data.build_from_sample, "ndEx" : data.nbEx} + return {"hankel" : Serializer.__serialize(data_dict)} raise TypeError("Type %s is not serializabled" % type(data)) @staticmethod @@ -59,6 +66,21 @@ class Serializer(object): '\", \"'.join(keys) + "\"") return Automaton(nbL=data["nbL"], nbS=data["nbS"], initial=data["initial"], final=data["final"], transitions=data["transitions"], type=data["type"]) + if "hankel" in data_str: + data = Serializer.__restore_json(data_str["hankel"]) + keys = {"nbL", "lhankel", "version", "partial", "sparse", "build_from_sample", "ndEx"} + if not keys.issubset(set(data.keys())): + raise ValueError("The input data string (" + data_str + + ") should contain the following keys : \"" + + '\", \"'.join(keys) + "\"") + H = Hankel(version=data["version"], partial=data["partial"], sparse=data["sparse"], + lhankel = data["lhankel"]) + if data["build_from_sample"]: + H.build_from_sample = True + H.nbL = data["nbL"] + H.nbEx = data["nbEx"] + return H + return data_str @staticmethod @@ -88,6 +110,20 @@ class Serializer(object): final=Serializer.__restore_yaml(data["final"]), transitions=[Serializer.__restore_yaml(k) for k in data["transitions"]], type=data["type"]) + if "hankel" in data_str: + data = Serializer.__restore_json(data_str["hankel"]) + keys = {"nbL", "lhankel", "version", "partial", "sparse", "build_from_sample", "ndEx"} + if not keys.issubset(set(data.keys())): + raise ValueError("The input data string (" + data_str + + ") should contain the following keys : \"" + + '\", \"'.join(keys) + "\"") + H = Hankel(version=data["version"], partial=data["partial"], sparse=data["sparse"], + lhankel = [Serializer.__restore_yaml(k) for k in data["lhankel"]]) + if data["build_from_sample"]: + H.build_from_sample = True + H.nbL = data["nbL"] + H.nbEx = data["nbEx"] + return H return data_str @staticmethod diff --git a/splearn/tests/datasets/simple_example.json b/splearn/tests/datasets/simple_example.json new file mode 100644 index 0000000000000000000000000000000000000000..fdf1119cb581e77b9dab380d5c7a4c0bd4b7e48c --- /dev/null +++ b/splearn/tests/datasets/simple_example.json @@ -0,0 +1 @@ +{"automaton": {"nbL": 2, "nbS": 2, "initial": {"numpy.ndarray": {"values": [0.5, 0.5], "dtype": "float64"}}, "final": {"numpy.ndarray": {"values": [0.5, 0.16666666666666666], "dtype": "float64"}}, "transitions": [{"numpy.ndarray": {"values": [[0.16666666666666666, 0.08333333333333333], [0.16666666666666666, 0.16666666666666666]], "dtype": "float64"}}, {"numpy.ndarray": {"values": [[0.0, 0.25], [0.16666666666666666, 0.3333333333333333]], "dtype": "float64"}}], "type": "classic"}} \ No newline at end of file diff --git a/splearn/tests/datasets/simple_example.yaml b/splearn/tests/datasets/simple_example.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6e2a7ec6f9138cc441f796bd596b7636b19a419a --- /dev/null +++ b/splearn/tests/datasets/simple_example.yaml @@ -0,0 +1,23 @@ +automaton: + final: + numpy.ndarray: + dtype: float64 + values: [0.5, 0.16666666666666666] + initial: + numpy.ndarray: + dtype: float64 + values: [0.5, 0.5] + nbL: 2 + nbS: 2 + transitions: + - numpy.ndarray: + dtype: float64 + values: + - [0.16666666666666666, 0.08333333333333333] + - [0.16666666666666666, 0.16666666666666666] + - numpy.ndarray: + dtype: float64 + values: + - [0.0, 0.25] + - [0.16666666666666666, 0.3333333333333333] + type: classic diff --git a/splearn/tests/test_serializer.py b/splearn/tests/test_serializer.py new file mode 100644 index 0000000000000000000000000000000000000000..524a7c9536ff2b1411e417ebf7c4e2e2c3a2516f --- /dev/null +++ b/splearn/tests/test_serializer.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +import unittest +import numpy as np +import filecmp +import os + +from splearn.automaton import Automaton +from splearn.hankel import Hankel +from splearn.tests.datasets.get_dataset_path import get_dataset_path + +class UnitaryTest(unittest.TestCase): + + + def setUp(self): + self.A = Automaton.SimpleExample() + self.title = "Simple Example" + self.input_file = 'simple_example' + self.formats = ['json', 'yaml'] + self.words = [[], [0], [0, 0], [1], [1, 1], [0, 1, 0], [1, 0, 1]] + self.d_str = "Value for word \"{:s}\" = {:.2f}" + + def testReadAutomaton(self): + for f in self.formats: + B = Automaton.read(get_dataset_path(self.input_file + '.' + f), format=f) + for w in self.words: + np.testing.assert_almost_equal(self.A.val(w), B.val(w)) + + def testWriteAutomata(self): + for f in self.formats: + Automaton.write(self.A, get_dataset_path(self.input_file + '_2.' + f), format=f) + self.assertTrue(filecmp.cmp(get_dataset_path(self.input_file + '_2.' + f), + get_dataset_path(self.input_file + '.' + f))) + for f in self.formats: + os.remove(get_dataset_path(self.input_file + '_2.' + f)) + + def testReadHankel(self): + for f in self.formats: + H = self.A.to_hankel([(), (0,), (1,)], [(), (0,), (1,)]) + Hankel.write(H, get_dataset_path(self.input_file + "_hankel" + "." + f), format=f) + Hb = Hankel.read(get_dataset_path(self.input_file + "_hankel" + "." + f), format = f) + B = Hb.to_automaton(2) + for w in self.words: + np.testing.assert_almost_equal(self.A.val(w), B.val(w)) + for f in self.formats: + os.remove(get_dataset_path(self.input_file + "_hankel" + "." + f)) + + +if __name__ == "__main__": + #import sys;sys.argv = ['', 'Test.testName'] + unittest.main() \ No newline at end of file