diff --git a/COPYING b/COPYING index 001fbed973710f618c7037c192cf333881dad365..947f926a70f71e318f6f8d7ede626ed028a745fd 100644 --- a/COPYING +++ b/COPYING @@ -1,6 +1,6 @@ New BSD License -Copyright (c) DATE, The SCIKIT-SPLEARN developers. +Copyright (c) 2016-DATE, The SCIKIT-SPLEARN developers. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/copyrightstamp.txt b/copyrightstamp.txt index 05ecaeef72f9ee611ff216ccb0104ff5418901eb..a87735f6f0d5181d641ccbe2ce32aca960fe5cfc 100644 --- a/copyrightstamp.txt +++ b/copyrightstamp.txt @@ -1,17 +1,17 @@ ######### COPYRIGHT ######### -Copyright(c) DATE +Copyright(c) 2016-DATE ----------------- * LabEx Archimède: http://labex-archimede.univ-amu.fr/ -* Laboratoire d'Informatique Fondamentale : http://www.lif.univ-mrs.fr/ +* Laboratoire d'Informatique et Systèmes : http://www.lis-lab.fr/ Contributors: ------------ -* François Denis <francois.denis_AT_lif.univ-mrs.fr> -* Rémi Eyraud <remy.eyraud_AT_lif.univ-mrs.fr> -* Denis Arrivault <contact.dev_AT_lif.univ-mrs.fr> +* François Denis <francois.denis_AT_lis-lab.fr> +* Rémi Eyraud <remi.eyraud_AT_lis-lab.fr> +* Denis Arrivault <contact.dev_AT_lis-lab.fr> * Dominique Benielli <dominique.benielli_AT_univ-amu.fr> Description: diff --git a/examples/3.pautomac_light.train.json b/examples/3.pautomac_light.train.json new file mode 100644 index 0000000000000000000000000000000000000000..e77f61549383993bfd88d1693caf1b8a49cd23f1 --- /dev/null +++ b/examples/3.pautomac_light.train.json @@ -0,0 +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 diff --git a/examples/3.pautomac_light.train.yaml b/examples/3.pautomac_light.train.yaml new file mode 100644 index 0000000000000000000000000000000000000000..125acbb420b9067bb9ee4097e4ed5312dc268f64 --- /dev/null +++ b/examples/3.pautomac_light.train.yaml @@ -0,0 +1,67 @@ +automaton: + final: + numpy.ndarray: + dtype: float64 + values: [0.07757136847945034, -0.02422029400314446, -0.4468125366321277, 0.6277320840897538, + -0.554674433356226] + initial: + numpy.ndarray: + dtype: float64 + values: [-0.0004934419970497477, 0.003063469710791532, -0.0440739320155803, + -0.10777702616547354, -0.0866391379316936] + 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] + - 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] + - 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] + - 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] + type: classic diff --git a/examples/automaton_io.py b/examples/automaton_io.py new file mode 100644 index 0000000000000000000000000000000000000000..59559e3970797bf427256a106a6a6fbfcf859998 --- /dev/null +++ b/examples/automaton_io.py @@ -0,0 +1,11 @@ +from splearn.automaton import Automaton +from graphviz import Source + +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") + src = Source(dot) + src.render(input_file + '.' + f + '.gv', view=True) + diff --git a/examples/json_save.ipynb b/examples/json_save.ipynb index 5acc82882948dcfa4fbf7f2abd7666ed6ce714c5..1af50f640ead04aa0456773561b9b06f5926ce06 100644 --- a/examples/json_save.ipynb +++ b/examples/json_save.ipynb @@ -30,7 +30,7 @@ "source": [ "from splearn.datasets.base import load_data_sample\n", "from splearn.tests.datasets.get_dataset_path import get_dataset_path\n", - "from splearn import Spectral\n", + "from splearn import Spectral, Automaton, Serializer\n", "train_file = '3.pautomac_light.train'\n", "data = load_data_sample(adr=get_dataset_path(train_file))\n", "sp = Spectral()\n", @@ -39,26 +39,296 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "Automaton.write(sp.automaton, train_file + \".json\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "str1 = Serializer.data_to_json(sp.automaton)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "A = Serializer.json_to_data(str1) " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "str2 = Serializer.data_to_json(A)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str1 == str2" + ] + }, + { + "cell_type": "code", + "execution_count": 8, "metadata": {}, "outputs": [ { - "ename": "AttributeError", - "evalue": "'dict' object has no attribute 'iteritems'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m<ipython-input-2-2625a21006a9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mautomaton\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite_json\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"test.json\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m~/Codes/scikit-splearn/splearn/automaton.py\u001b[0m in \u001b[0;36mwrite_json\u001b[0;34m(self, jsonFileName)\u001b[0m\n\u001b[1;32m 695\u001b[0m \"transitions\":self.transitions, \"type\":self.type}\n\u001b[1;32m 696\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjsonFileName\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'w'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0moutfile\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 697\u001b[0;31m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdump\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mSerializer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata_to_json\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutfile\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 698\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 699\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mread_json\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mjsonFileName\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m~/Codes/scikit-splearn/splearn/serializer.py\u001b[0m in \u001b[0;36mdata_to_json\u001b[0;34m(self, data)\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 46\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdata_to_json\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 47\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdumps\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__serialize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 48\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mjson_to_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m~/Codes/scikit-splearn/splearn/serializer.py\u001b[0m in \u001b[0;36m__serialize\u001b[0;34m(self, data)\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdict\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__serialize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miteritems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 23\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m\"py/dict\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__serialize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__serialize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miteritems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mAttributeError\u001b[0m: 'dict' object has no attribute 'iteritems'" + "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\"}}}}'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str1" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "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\"}}}}'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str2" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "str3 = Serializer.data_to_yaml(sp.automaton)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "automaton:\n", + " final:\n", + " numpy.ndarray:\n", + " dtype: float64\n", + " values: [0.07757136847945034, -0.02422029400314446, -0.4468125366321277, 0.6277320840897538,\n", + " -0.554674433356226]\n", + " initial:\n", + " numpy.ndarray:\n", + " dtype: float64\n", + " values: [-0.0004934419970497477, 0.003063469710791532, -0.0440739320155803,\n", + " -0.10777702616547354, -0.0866391379316936]\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", + " - 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", + " - 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", + " - 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", + " type: classic\n", + "\n" ] } ], "source": [ - "sp.automaton.write_json(\"test.json\")" + "print(str3)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "Ajs = Automaton.read(train_file + \".json\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[array([[ 0.04512121, -0.2403897 , 0.34945 , -0.28116807, -0.21402523],\n", + " [ 0.06925801, -0.30062293, 0.20641375, -0.14960814, -0.55805732],\n", + " [ 0.02980115, -0.13866481, 0.18362213, -0.20969545, -0.14481622],\n", + " [ 0.00569934, -0.02338583, -0.06600665, 0.10749935, -0.15103655],\n", + " [-0.02008655, 0.09026348, -0.00552559, -0.03135532, 0.24329022]]),\n", + " array([[ 0.07744772, 0.09007074, -0.30472201, 0.27676245, 0.20289396],\n", + " [-0.0990298 , -0.08061847, 0.25853171, -0.1208633 , -0.11085208],\n", + " [-0.06171079, -0.06244152, 0.12007655, 0.00250637, -0.15679675],\n", + " [-0.00273697, -0.00900572, -0.00046003, -0.00855043, -0.05375465],\n", + " [ 0.03098733, 0.0397268 , -0.04997113, 0.00357694, 0.14182576]]),\n", + " array([[-0.06791915, -0.11357938, 0.37955393, -0.2178498 , -0.22977695],\n", + " [ 0.11596642, 0.14914957, -0.13357508, -0.00891606, 0.34841537],\n", + " [ 0.01173082, 0.0192738 , 0.04142658, -0.03534659, 0.02316491],\n", + " [ 0.00732891, 0.00553651, -0.02245608, 0.03611543, -0.03851434],\n", + " [-0.01058989, -0.01062662, -0.00054311, -0.02556748, 0.04984889]]),\n", + " array([[ 0.07276211, -0.01571956, 0.07428593, -0.10369862, 0.02475347],\n", + " [-0.05607105, -0.08896207, 0.27638225, -0.23711256, 0.07372294],\n", + " [-0.00739129, -0.0487418 , -0.62912397, 0.46816277, 0.09251699],\n", + " [-0.00711022, -0.05623318, -0.36606659, -0.0132978 , 0.64910332],\n", + " [ 0.00233552, -0.02156115, 0.09096243, -0.38438823, 0.66164772]])]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Ajs.transitions" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "Automaton.write(sp.automaton, train_file + \".yaml\", \"yaml\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "Ayl = Automaton.read(train_file + \".yaml\", \"yaml\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[array([[ 0.04512121, -0.2403897 , 0.34945 , -0.28116807, -0.21402523],\n", + " [ 0.06925801, -0.30062293, 0.20641375, -0.14960814, -0.55805732],\n", + " [ 0.02980115, -0.13866481, 0.18362213, -0.20969545, -0.14481622],\n", + " [ 0.00569934, -0.02338583, -0.06600665, 0.10749935, -0.15103655],\n", + " [-0.02008655, 0.09026348, -0.00552559, -0.03135532, 0.24329022]]),\n", + " array([[ 0.07744772, 0.09007074, -0.30472201, 0.27676245, 0.20289396],\n", + " [-0.0990298 , -0.08061847, 0.25853171, -0.1208633 , -0.11085208],\n", + " [-0.06171079, -0.06244152, 0.12007655, 0.00250637, -0.15679675],\n", + " [-0.00273697, -0.00900572, -0.00046003, -0.00855043, -0.05375465],\n", + " [ 0.03098733, 0.0397268 , -0.04997113, 0.00357694, 0.14182576]]),\n", + " array([[-0.06791915, -0.11357938, 0.37955393, -0.2178498 , -0.22977695],\n", + " [ 0.11596642, 0.14914957, -0.13357508, -0.00891606, 0.34841537],\n", + " [ 0.01173082, 0.0192738 , 0.04142658, -0.03534659, 0.02316491],\n", + " [ 0.00732891, 0.00553651, -0.02245608, 0.03611543, -0.03851434],\n", + " [-0.01058989, -0.01062662, -0.00054311, -0.02556748, 0.04984889]]),\n", + " array([[ 0.07276211, -0.01571956, 0.07428593, -0.10369862, 0.02475347],\n", + " [-0.05607105, -0.08896207, 0.27638225, -0.23711256, 0.07372294],\n", + " [-0.00739129, -0.0487418 , -0.62912397, 0.46816277, 0.09251699],\n", + " [-0.00711022, -0.05623318, -0.36606659, -0.0132978 , 0.64910332],\n", + " [ 0.00233552, -0.02156115, 0.09096243, -0.38438823, 0.66164772]])]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Ayl.transitions" ] }, { @@ -85,7 +355,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.5.2" } }, "nbformat": 4, diff --git a/examples/json_save.py b/examples/json_save.py deleted file mode 100644 index ae9eea10edca3e41801866d7405a8829c198497f..0000000000000000000000000000000000000000 --- a/examples/json_save.py +++ /dev/null @@ -1,13 +0,0 @@ -from splearn.datasets.base import load_data_sample -from splearn.tests.datasets.get_dataset_path import get_dataset_path -from splearn import Spectral -from splearn.automaton import Automaton -train_file = '3.pautomac_light.train' -data = load_data_sample(adr=get_dataset_path(train_file)) -sp = Spectral() -sp.fit(X=data.data) - -sp.automaton.write_json("test.json") -A = Automaton(1,1,[]).read_json("test.json") -print(A) - diff --git a/examples/simple_automata.json b/examples/simple_automata.json new file mode 100644 index 0000000000000000000000000000000000000000..ab271a879ea057c6064636ba019001158b41a863 --- /dev/null +++ b/examples/simple_automata.json @@ -0,0 +1 @@ +{"automaton": {"final": {"numpy.ndarray": {"values": [0.15, 0.9], "dtype": "float64"}}, "initial": {"numpy.ndarray": {"values": [0.5, 0.8], "dtype": "float64"}}, "transitions": [{"numpy.ndarray": {"values": [[0.5, 0.15], [0.9, 0]], "dtype": "float64"}}, {"numpy.ndarray": {"values": [[0, 0], [0, -0.15]], "dtype": "float64"}}], "type": "classic", "nbS": 2, "nbL": 2}} diff --git a/examples/simple_automata.json.gv b/examples/simple_automata.json.gv new file mode 100644 index 0000000000000000000000000000000000000000..0fd62e2d59b54c471ed4736cd2b9456c714a1897 --- /dev/null +++ b/examples/simple_automata.json.gv @@ -0,0 +1,15 @@ +//Simple Automata +digraph { + 0 [label="0 +______ +> 0.50 +0.15 >"] + 1 [label="1 +______ +> 0.80 +0.90 >"] + 0 -> 0 [label="0:0.50"] + 0 -> 1 [label="0:0.15"] + 1 -> 0 [label="0:0.90"] + 1 -> 1 [label="1:-0.15"] +} diff --git a/examples/simple_automata.json.gv.pdf b/examples/simple_automata.json.gv.pdf new file mode 100644 index 0000000000000000000000000000000000000000..bcedcde8006bcfa94c68c2c82199bd53b985503d Binary files /dev/null and b/examples/simple_automata.json.gv.pdf differ diff --git a/examples/simple_automata.yaml b/examples/simple_automata.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b85b6aec2fdc2ffe36ac8799948651b8c8c456ef --- /dev/null +++ b/examples/simple_automata.yaml @@ -0,0 +1,23 @@ +automaton: + final: + numpy.ndarray: + dtype: float64 + values: [0.15, 0.9] + initial: + numpy.ndarray: + dtype: float64 + values: [0.5, 0.8] + nbL: 2 + nbS: 2 + transitions: + - numpy.ndarray: + dtype: float64 + values: + - [0.5, 0.15] + - [0.9, 0] + - numpy.ndarray: + dtype: float64 + values: + - [0, 0] + - [0, -0.15] + type: classic diff --git a/examples/simple_automata.yaml.gv b/examples/simple_automata.yaml.gv new file mode 100644 index 0000000000000000000000000000000000000000..0fd62e2d59b54c471ed4736cd2b9456c714a1897 --- /dev/null +++ b/examples/simple_automata.yaml.gv @@ -0,0 +1,15 @@ +//Simple Automata +digraph { + 0 [label="0 +______ +> 0.50 +0.15 >"] + 1 [label="1 +______ +> 0.80 +0.90 >"] + 0 -> 0 [label="0:0.50"] + 0 -> 1 [label="0:0.15"] + 1 -> 0 [label="0:0.90"] + 1 -> 1 [label="1:-0.15"] +} diff --git a/examples/simple_automata.yaml.gv.pdf b/examples/simple_automata.yaml.gv.pdf new file mode 100644 index 0000000000000000000000000000000000000000..bcedcde8006bcfa94c68c2c82199bd53b985503d Binary files /dev/null and b/examples/simple_automata.yaml.gv.pdf differ diff --git a/examples/test.json b/examples/test.json deleted file mode 100644 index e4d8477590e688c7f9a2ece37a8c5331f3d3d963..0000000000000000000000000000000000000000 --- a/examples/test.json +++ /dev/null @@ -1 +0,0 @@ -{"nbL": 4, "nbS": 5, "initial": {"py/numpy.ndarray": {"values": [-0.0004934419970497512, 0.0030634697107912346, -0.044073932015580415, -0.1077770261654714, -0.0866391379316952], "dtype": "float64"}}, "final": {"py/numpy.ndarray": {"values": [0.07757136847945045, -0.024220294003132026, -0.4468125366321221, 0.627732084089759, -0.554674433356224], "dtype": "float64"}}, "transitions": [{"py/numpy.ndarray": {"values": [[0.04512120959511772, -0.24038969827844062, 0.34944999592135334, -0.2811680730534579, -0.21402523377497645], [0.0692580056243761, -0.30062293462829204, 0.20641375368520157, -0.14960814319756124, -0.5580573163749153], [0.02980115192176571, -0.13866480809160409, 0.18362212572805459, -0.20969545230657607, -0.14481622025561292], [0.005699344003198349, -0.023385825120201414, -0.06600665373981851, 0.10749935271466007, -0.15103654604159977], [-0.02008655193147911, 0.09026347555230492, -0.005525585655539262, -0.031355317090308935, 0.2432902242047721]], "dtype": "float64"}}, {"py/numpy.ndarray": {"values": [[0.0774477207917058, 0.09007073705762021, -0.3047220063293013, 0.2767624549859105, 0.20289396030628148], [-0.09902980483670844, -0.08061846818727973, 0.25853170692250554, -0.12086330214608881, -0.11085207725068251], [-0.061710792028537534, -0.06244151779954751, 0.12007654564862075, 0.0025063746277943564, -0.1567967473145572], [-0.002736973749965403, -0.009005721984277787, -0.00046003295909181354, -0.008550426472005344, -0.053754646789681754], [0.030987327588710728, 0.03972680066723246, -0.04997113350910248, 0.0035769411874962344, 0.1418257620585633]], "dtype": "float64"}}, {"py/numpy.ndarray": {"values": [[-0.06791915236220235, -0.11357937659088102, 0.37955392604054394, -0.21784979894046635, -0.22977695089938127], [0.11596642335411328, 0.14914956804629287, -0.13357508376686902, -0.008916063072034974, 0.3484153673774836], [0.011730817547426673, 0.019273800531955612, 0.0414265834586712, -0.035346588560982, 0.02316491010895583], [0.007328911075541707, 0.005536509132796312, -0.022456082950666856, 0.03611543477693187, -0.038514339001406585], [-0.010589894686551544, -0.010626616553723532, -0.000543105645661794, -0.025567476700160314, 0.04984888818929034]], "dtype": "float64"}}, {"py/numpy.ndarray": {"values": [[0.07276211427780357, -0.0157195576855797, 0.07428592814590385, -0.10369861539249735, 0.024753473688328077], [-0.05607105449779142, -0.08896207276035666, 0.27638225397521243, -0.2371125582838589, 0.07372294122306285], [-0.007391294007753122, -0.048741797963875705, -0.6291239733858526, 0.46816276521577677, 0.09251699239093385], [-0.007110224931878467, -0.05623317735898056, -0.36606658567620365, -0.013297798115225407, 0.6491033177492604], [0.002335515008556511, -0.021561151264484414, 0.09096243479437888, -0.38438823493062646, 0.6616477207948602]], "dtype": "float64"}}], "type": "classic"} \ No newline at end of file diff --git a/splearn/__init__.py b/splearn/__init__.py index 88d3dc0f47225850a99aff6e86da8fb109219213..ea4eddc4722c984de23538a502d30a24a91b5eb4 100644 --- a/splearn/__init__.py +++ b/splearn/__init__.py @@ -1,5 +1,5 @@ from splearn.automaton import Automaton -#from splearn.spectral import Learning +from splearn.serializer import Serializer from splearn.spectral import Spectral from splearn.hankel import Hankel __version__ = "1.1.0" diff --git a/splearn/automaton.py b/splearn/automaton.py index ad971aa8ce7ad36544fce40f99f9890eb5aba542..35e48e36c68df2d4a63298b13c9502ad47bf5431 100644 --- a/splearn/automaton.py +++ b/splearn/automaton.py @@ -3,7 +3,7 @@ """ import numpy as np -from splearn.serializer import Serializer + class Automaton(object): """ Define an automaton with parameters @@ -643,9 +643,9 @@ class Automaton(object): >>> from graphviz import Source >>> src = Source(dot) >>> src.render(dotfile + '.gv', view=True) - + - Input: - + :param Automaton self :param float threshold for the value to keep. If |weight| < threshold, the corresponding transition is not kept as an edge in the final dot string. @@ -688,25 +688,48 @@ class Automaton(object): out += label + "\"]\n" out += "}\n" return out - - def write_json(self, jsonFileName): - data = {"nbL":self.nbL, "nbS":self.nbS, "initial":self.initial, "final":self.final, - "transitions":self.transitions, "type":self.type} - data_str = Serializer().data_to_json(data) - with open(jsonFileName, 'w') as outfile: + + @staticmethod + def write(automaton_in, filename, format='json'): + """ write input automaton into a file with the given format. + + - Input: + + :param Automaton automaton_in: automaton 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(automaton_in) + elif format == 'yaml': + data_str = Serializer.data_to_yaml(automaton_in) + else: + raise ValueError("Invalid input format. Should be \"json\" or \"yaml\"") + with open(filename, 'w') as outfile: outfile.write(data_str) - - def read_json(self, jsonFileName): - with open(jsonFileName) as infile: + + @staticmethod + def read(filename, format='json'): + """ return an Automaton 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 automaton + :rtype: Automaton + """ + from splearn.serializer import Serializer + with open(filename) as infile: datastr = infile.read() - data = Serializer().json_to_data(datastr) - print(data) - keys = {"nbL", "nbS", "initial", "final", "transitions", "type"} - if not keys.issubset(set(data.keys())): - raise ValueError("The input json file should contain the following keys : \"" + '\", \"'.join(keys) + "\"") - self = Automaton(nbL=data["nbL"], nbS=data["nbS"], initial=data["initial"], final=data["final"], - transitions=data["transitions"], type=data["type"]) - - - - + 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\"") + \ No newline at end of file diff --git a/splearn/serializer.py b/splearn/serializer.py index b8645acad9c26cf161bf865096cec76157284681..2ba5e90d7a6a2d201fe4ec2ad7b72af00ae54b2e 100644 --- a/splearn/serializer.py +++ b/splearn/serializer.py @@ -1,50 +1,157 @@ -''' -Created on 6 févr. 2018 - -@author: arrivault -''' +# -*- coding: utf-8 -*- +"""This module contains the Serializer class +""" import numpy as np -import json + +from splearn.automaton import Automaton class Serializer(object): - ''' - classdocs - ''' - - def __serialize(self, data): + """ Serializer is an helping object for data serialization + """ + + @staticmethod + def __serialize(data): if data is None or isinstance(data, (bool, int, float, str)): return data if isinstance(data, list): - return [self.__serialize(val) for val in data] + return [Serializer.__serialize(val) for val in data] if isinstance(data, dict): if all(isinstance(k, str) for k in data): - return {k: self.__serialize(v) for k, v in data.items()} - return {"py/dict": [[self.__serialize(k), self.__serialize(v)] for k, v in data.items()]} + return {k: Serializer.__serialize(v) for k, v in data.items()} + return {"dict": [[Serializer.__serialize(k), Serializer.__serialize(v)] for k, v in data.items()]} if isinstance(data, tuple): - return {"py/tuple": [self.__serialize(val) for val in data]} + return {"tuple": [Serializer.__serialize(val) for val in data]} if isinstance(data, set): - return {"py/set": [self.__serialize(val) for val in data]} + return {"set": [Serializer.__serialize(val) for val in data]} if isinstance(data, np.ndarray): - return {"py/numpy.ndarray": { + return {"numpy.ndarray": { "values": data.tolist(), "dtype": str(data.dtype)}} - raise TypeError("Type %s not data-serializable" % type(data)) + if isinstance(data, Automaton): + 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)} + raise TypeError("Type %s is not serializabled" % type(data)) - def __restore(self, dct): - if "py/dict" in dct: - return dict(dct["py/dict"]) - if "py/tuple" in dct: - return tuple(dct["py/tuple"]) - if "py/set" in dct: - return set(dct["py/set"]) - if "py/numpy.ndarray" in dct: - data = dct["py/numpy.ndarray"] + @staticmethod + def __restore_json(data_str): + if "dict" in data_str: + return dict(data_str["dict"]) + if "tuple" in data_str: + return tuple(data_str["tuple"]) + if "set" in data_str: + return set(data_str["set"]) + if "numpy.ndarray" in data_str: + data = data_str["numpy.ndarray"] + keys = {"values", "dtype"} + if not keys.issubset(set(data.keys())): + raise ValueError("The input data string (" + data_str + + ") should contain the following keys : \"" + + '\", \"'.join(keys) + "\"") return np.array(data["values"], dtype=data["dtype"]) - return dct + if "automaton" in data_str: + data = Serializer.__restore_json(data_str["automaton"]) + keys = {"nbL", "nbS", "initial", "final", "transitions", "type"} + if not keys.issubset(set(data.keys())): + raise ValueError("The input data string (" + data_str + + ") should contain the following keys : \"" + + '\", \"'.join(keys) + "\"") + return Automaton(nbL=data["nbL"], nbS=data["nbS"], initial=data["initial"], final=data["final"], + transitions=data["transitions"], type=data["type"]) + return data_str + + @staticmethod + def __restore_yaml(data_str): + if "dict" in data_str: + return dict(data_str["dict"]) + if "tuple" in data_str: + return tuple(data_str["tuple"]) + if "set" in data_str: + return set(data_str["set"]) + if "numpy.ndarray" in data_str: + data = data_str["numpy.ndarray"] + keys = {"values", "dtype"} + if not keys.issubset(set(data.keys())): + raise ValueError("The input data string (" + data_str + + ") should contain the following keys : \"" + + '\", \"'.join(keys) + "\"") + return np.array(data["values"], dtype=data["dtype"]) + if "automaton" in data_str: + data = Serializer.__restore_yaml(data_str["automaton"]) + keys = {"nbL", "nbS", "initial", "final", "transitions", "type"} + if not keys.issubset(set(data.keys())): + raise ValueError("The input data string (" + data_str + + ") should contain the following keys : \"" + + '\", \"'.join(keys) + "\"") + return Automaton(nbL=data["nbL"], nbS=data["nbS"], initial=Serializer.__restore_yaml(data["initial"]), + final=Serializer.__restore_yaml(data["final"]), + transitions=[Serializer.__restore_yaml(k) for k in data["transitions"]], + type=data["type"]) + return data_str + + @staticmethod + def data_to_json(data): + """ return a string into json format that does contains the input data. + + - Input: + + :param data: data composed by any types that is serializabled + + - Output: + + :returns: the output string + :rtype: str + """ + + import json + return json.dumps(Serializer.__serialize(data)) + + @staticmethod + def json_to_data(json_data_str): + """ return a data from input json string. + + - Input: + + :param json_data_str: the json input string + + - Output: + + :returns: the data + :rtype: deduced form the json input string + """ + + import json + return json.loads(json_data_str, object_hook=Serializer.__restore_json) - def data_to_json(self, data): - return json.dumps(self.__serialize(data)) + @staticmethod + def data_to_yaml(data): + """ return a string into yaml format that does contains the input data. + + - Input: + + :param data: data composed by any types that is serializabled + + - Output: + + :returns: the output string + :rtype: str + """ + import yaml + return yaml.dump(Serializer.__serialize(data)) - def json_to_data(self, s): - return json.loads(s, object_hook=self.__restore) + @staticmethod + def yaml_to_data(yaml_data_str): + """ return a data from input yaml string. + + - Input: + + :param yaml_data_str: the yaml input string + + - Output: + + :returns: the data + :rtype: deduced form the yaml input string + """ + import yaml + return Serializer.__restore_yaml(yaml.load(yaml_data_str)) diff --git a/test.json b/test.json deleted file mode 100644 index e4d8477590e688c7f9a2ece37a8c5331f3d3d963..0000000000000000000000000000000000000000 --- a/test.json +++ /dev/null @@ -1 +0,0 @@ -{"nbL": 4, "nbS": 5, "initial": {"py/numpy.ndarray": {"values": [-0.0004934419970497512, 0.0030634697107912346, -0.044073932015580415, -0.1077770261654714, -0.0866391379316952], "dtype": "float64"}}, "final": {"py/numpy.ndarray": {"values": [0.07757136847945045, -0.024220294003132026, -0.4468125366321221, 0.627732084089759, -0.554674433356224], "dtype": "float64"}}, "transitions": [{"py/numpy.ndarray": {"values": [[0.04512120959511772, -0.24038969827844062, 0.34944999592135334, -0.2811680730534579, -0.21402523377497645], [0.0692580056243761, -0.30062293462829204, 0.20641375368520157, -0.14960814319756124, -0.5580573163749153], [0.02980115192176571, -0.13866480809160409, 0.18362212572805459, -0.20969545230657607, -0.14481622025561292], [0.005699344003198349, -0.023385825120201414, -0.06600665373981851, 0.10749935271466007, -0.15103654604159977], [-0.02008655193147911, 0.09026347555230492, -0.005525585655539262, -0.031355317090308935, 0.2432902242047721]], "dtype": "float64"}}, {"py/numpy.ndarray": {"values": [[0.0774477207917058, 0.09007073705762021, -0.3047220063293013, 0.2767624549859105, 0.20289396030628148], [-0.09902980483670844, -0.08061846818727973, 0.25853170692250554, -0.12086330214608881, -0.11085207725068251], [-0.061710792028537534, -0.06244151779954751, 0.12007654564862075, 0.0025063746277943564, -0.1567967473145572], [-0.002736973749965403, -0.009005721984277787, -0.00046003295909181354, -0.008550426472005344, -0.053754646789681754], [0.030987327588710728, 0.03972680066723246, -0.04997113350910248, 0.0035769411874962344, 0.1418257620585633]], "dtype": "float64"}}, {"py/numpy.ndarray": {"values": [[-0.06791915236220235, -0.11357937659088102, 0.37955392604054394, -0.21784979894046635, -0.22977695089938127], [0.11596642335411328, 0.14914956804629287, -0.13357508376686902, -0.008916063072034974, 0.3484153673774836], [0.011730817547426673, 0.019273800531955612, 0.0414265834586712, -0.035346588560982, 0.02316491010895583], [0.007328911075541707, 0.005536509132796312, -0.022456082950666856, 0.03611543477693187, -0.038514339001406585], [-0.010589894686551544, -0.010626616553723532, -0.000543105645661794, -0.025567476700160314, 0.04984888818929034]], "dtype": "float64"}}, {"py/numpy.ndarray": {"values": [[0.07276211427780357, -0.0157195576855797, 0.07428592814590385, -0.10369861539249735, 0.024753473688328077], [-0.05607105449779142, -0.08896207276035666, 0.27638225397521243, -0.2371125582838589, 0.07372294122306285], [-0.007391294007753122, -0.048741797963875705, -0.6291239733858526, 0.46816276521577677, 0.09251699239093385], [-0.007110224931878467, -0.05623317735898056, -0.36606658567620365, -0.013297798115225407, 0.6491033177492604], [0.002335515008556511, -0.021561151264484414, 0.09096243479437888, -0.38438823493062646, 0.6616477207948602]], "dtype": "float64"}}], "type": "classic"} \ No newline at end of file