diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cda6a4fe543650d59833a0424085d436a843c59a..27aed8b9fa4d4249225607f77befe62badcc0d48 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -14,6 +14,7 @@ unbutu:17.10: when: always untracked: true paths: - - build_python + - public + - build expire_in: 8d diff --git a/splearn/hankel.py b/splearn/hankel.py index e9f0d844a0a09ed3a149c24ea090afe36785ac0f..6e629a265785726d476e2749a40a55088704ddf7 100644 --- a/splearn/hankel.py +++ b/splearn/hankel.py @@ -112,24 +112,35 @@ class Hankel(object): raise ValueError("At least sample_instance or lhankel has to be not None.") def __eq__(self, other): - print("Hankel equality check") + #print("Hankel equality check") if self.version != other.version: - print("different versions") + #print("version is different") return False if self.partial != other.partial: + #print("partial is different") return False if self.sparse != other.sparse: + #print("sparse is different") return False if self.build_from_sample != other.build_from_sample: + #print("build_from_sample is different") return False if self.nbL != other.nbL: + #print("nbL is different") return False if self.nbEx != other.nbEx: + #print("nbEx is different") return False if len(self.lhankel) != len(other.lhankel): + #print("lhankel length is different") return False for lh1, lh2 in zip(self.lhankel, other.lhankel): - if (lh1 != lh2).nnz > 0: + if self.sparse: + if (lh1 != lh2).nnz > 0: + #print("{:d} elements oh lhandel are different".format((lh1 != lh2).nnz)) + return False + elif not np.array_equal(lh1, lh2): + #print("Different Array") return False return True diff --git a/splearn/serializer.py b/splearn/serializer.py index f7da0e5769ce24e24c1480099b5a3ea97fca149c..cc22be7ceb428465b79888433c94a46aea8e4b74 100644 --- a/splearn/serializer.py +++ b/splearn/serializer.py @@ -153,10 +153,12 @@ class Serializer(object): if "scipy.dok_matrix" in data_str: data = data_str["scipy.dok_matrix"] keys = {"shape", "dtype", "values"} + errorMsg = "The input data string (" + str(data_str) + errorMsg += ") should contain the following keys : \"" + '\", \"'.join(keys) + "\"" + if data is None: + raise ValueError(errorMsg) if not keys.issubset(set(data.keys())): - raise ValueError("The input data string (" + str(data_str) + - ") should contain the following keys : \"" + - '\", \"'.join(keys) + "\"") + raise ValueError(errorMsg) values = Serializer.__restore_json(data["values"]) shape = Serializer.__restore_json(data["shape"]) dok = sps.dok_matrix(shape, dtype=data["dtype"]) @@ -168,33 +170,35 @@ class Serializer(object): if "numpy.ndarray" in data_str: data = data_str["numpy.ndarray"] keys = {"values", "dtype"} + errorMsg = "The input data string (" + str(data_str) + errorMsg += ") should contain the following keys : \"" + '\", \"'.join(keys) + "\"" if data is None: - raise ValueError("The input data string (" + str(data_str) + - ") should contain the following keys : \"" + - '\", \"'.join(keys) + "\"") + raise ValueError(errorMsg) if not keys.issubset(set(data.keys())): - raise ValueError("The input data string (" + str(data_str) + - ") should contain the following keys : \"" + - '\", \"'.join(keys) + "\"") + raise ValueError(errorMsg) 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"} + errorMsg = "The input data string (" + str(data_str) + errorMsg += ") should contain the following keys : \"" + '\", \"'.join(keys) + "\"" + if data is None: + raise ValueError(errorMsg) if not keys.issubset(set(data.keys())): - raise ValueError("The input data string (" + str(data_str) + - ") should contain the following keys : \"" + - '\", \"'.join(keys) + "\"") + raise ValueError(errorMsg) 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"]) if "hankel" in data_str: - data = Serializer.__restore_json(data_str["hankel"]) + data = Serializer.__restore_yaml(data_str["hankel"]) keys = {"nbL", "lhankel", "version", "partial", "sparse", "build_from_sample", "nbEx"} + errorMsg = "The input data string (" + str(data_str) + errorMsg += ") should contain the following keys : \"" + '\", \"'.join(keys) + "\"" + if data is None: + raise ValueError(errorMsg) if not keys.issubset(set(data.keys())): - raise ValueError("The input data string (" + str(data_str) + - ") should contain the following keys : \"" + - '\", \"'.join(keys) + "\"") + raise ValueError(errorMsg) 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"]: diff --git a/splearn/tests/test_hankel.py b/splearn/tests/test_hankel.py index 970f16a1a6417c97e51d896b40e66c7f6d8e3fe1..2f2a4bc1cb6d43aa8faf681db67347f50ab6eded 100644 --- a/splearn/tests/test_hankel.py +++ b/splearn/tests/test_hankel.py @@ -36,16 +36,17 @@ from __future__ import division, print_function import unittest +import numpy as np +import scipy.sparse as sps + from splearn.datasets.base import load_data_sample from splearn.automaton import Automaton from splearn.spectral import Spectral from splearn.hankel import Hankel from splearn.tests.datasets.get_dataset_path import get_dataset_path -import numpy as np -import scipy.sparse as sps -class UnitaryTest(unittest.TestCase): +class HankelTest(unittest.TestCase): def test_CreateHankel_classic(self): adr = get_dataset_path("essai") @@ -344,5 +345,48 @@ class UnitaryTest(unittest.TestCase): with self.assertRaises(TypeError): h.nbEx = 6.8 + def testEqualityOperator(self): + adr = get_dataset_path("essai") + data = load_data_sample(adr=adr) + cl = Spectral() + cl._polulate_dictionnaries(data.data) + h1 = Hankel(sample_instance=data.data, lrows=1, lcolumns=1, + version="classic", partial=False, sparse=False) + h2 = Hankel(sample_instance=data.data, lrows=1, lcolumns=1, + version="prefix", partial=False, sparse=False) + self.assertTrue(h1 != h2) + h2 = Hankel(sample_instance=data.data, lrows=1, lcolumns=1, + version="classic", partial=True, sparse=False) + self.assertTrue(h1 != h2) + h2 = Hankel(sample_instance=data.data, lrows=1, lcolumns=1, + version="classic", partial=False, sparse=True) + self.assertTrue(h1 != h2) + h2 = Hankel(sample_instance=data.data, lrows=1, lcolumns=1, + version="classic", partial=False, sparse=False) + h2.lhankel[0][0,0] -= 1.0 + self.assertTrue(h1 != h2) + h2.lhankel = h2.lhankel[1:] + self.assertTrue(h1 != h2) + h2.nbEx -= 1 + self.assertTrue(h1 != h2) + h2.nbL -= 1 + self.assertTrue(h1 != h2) + l = [(0,), (1,), (0,0), (0,1), (1,0), (1,1)] + h2 = h1.to_automaton(2).to_hankel(lrows=l, lcolumns=l) + self.assertTrue(h1 != h2) + + adr = get_dataset_path("3.pautomac.train") + data = load_data_sample(adr=adr) + X = data.data + sp1 = Spectral() + sp1 = sp1.fit(X) + H1 = sp1.hankel + sp2 = Spectral() + sp2 = sp2.fit(X) + H2 = sp2.hankel + H2.lhankel[0][(1, 1610)] = 0 + self.assertTrue(H1 != H2) + + if __name__ == '__main__': unittest.main() diff --git a/splearn/tests/test_serializer.py b/splearn/tests/test_serializer.py index aa80ad1bd0c6b5b8de4d4a7ac589298265d49764..fddafd8002d6e27b7d0716adb2963748b2c7daac 100644 --- a/splearn/tests/test_serializer.py +++ b/splearn/tests/test_serializer.py @@ -46,7 +46,7 @@ from splearn.spectral import Spectral from splearn.tests.datasets.get_dataset_path import get_dataset_path from splearn.datasets.base import load_data_sample -class UnitaryTest(unittest.TestCase): +class SerializerTest(unittest.TestCase): def setUp(self): @@ -119,21 +119,24 @@ class UnitaryTest(unittest.TestCase): Serializer.data_to_json(deque('ghi')) def testBadDataException(self): - yamlstr = "- scipy.dok_matrix:\n shape:\n tuple: [1, 1]\n values: {'(0,0)': 1.0}" - with self.assertRaises(ValueError): - Serializer.yaml_to_data(yamlstr) - jsonstr = "{\"scipy.dok_matrix\":{}}" - with self.assertRaises(ValueError): - Serializer.json_to_data(jsonstr) - yamlstr = "- numpy.ndarray:" - with self.assertRaises(ValueError): - Serializer.yaml_to_data(yamlstr) - yamlstr = "- numpy.ndarray:\n dtype: float64" - with self.assertRaises(ValueError): - Serializer.yaml_to_data(yamlstr) - jsonstr = "{\"numpy.ndarray\":{}}" - with self.assertRaises(ValueError): - Serializer.json_to_data(jsonstr) + yamlstrList = ["- scipy.dok_matrix:", + "- scipy.dok_matrix:\n shape:\n tuple: [1, 1]\n values: {'(0,0)': 1.0}", + "- numpy.ndarray:", + "- numpy.ndarray:\n dtype: float64", + "- automaton:", + "- automaton:\n nbL: 1", + "- hankel:", + "- hankel:\n nbL: 1"] + jsonstrList = ["{\"scipy.dok_matrix\":{}}", + "{\"numpy.ndarray\":{}}", + "{\"automaton\":{}}", + "{\"hankel\":{}}"] + for yamlstr in yamlstrList: + with self.assertRaises(ValueError): + Serializer.yaml_to_data(yamlstr) + for jsonstr in jsonstrList: + with self.assertRaises(ValueError): + Serializer.json_to_data(jsonstr) if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName']