Exemple #1
0
def test_iteritems():
    data = [0, 1, 2, 3, 4]
    time = [5, 6, 7, 8, 9]
    ts = ArrayTimeSeries(time, data)
    l = ts.iteritems()
    next(l)
    assert next(l) == (6, 1)
Exemple #2
0
def test_itertimes():
    data = [0, 1, 2, 3, 4]  #range(0,5)
    time = [5, 6, 7, 8, 9]  #range(5,10)
    ts = ArrayTimeSeries(time, data)
    l = ts.itertimes()
    next(l)
    assert next(l) == 6
def test_invalid_input_eq():
    ts_1 = ArrayTimeSeries([1, 2], [3, 4])
    ts_2 = ArrayTimeSeries([1, 2, 3], [3, 4, 5])
    with raises(TypeError):
        ts_1 == ([3, 1], [4, 2])
    with raises(ValueError):
        ts_1 == ts_2
Exemple #4
0
def test_setitem():
    data = [4, 5, 6]
    time = [1, 2, 3]
    ts = ArrayTimeSeries(time, data)
    index = 0
    val = 0
    ts[index] = val
    assert ts == ArrayTimeSeries([1, 2, 3], [0, 5, 6])
def test_invalid_input():
    with raises(ValueError):
        ArrayTimeSeries([1, 2], [3, 4, 5])
    with raises(TypeError):
        ArrayTimeSeries(1, 2)
    with raises(TypeError):
        ArrayTimeSeries(1, [1])
    with raises(TypeError):
        ArrayTimeSeries([1, 2], 3)
    with raises(ValueError):
        ArrayTimeSeries([1, 2], [2, 2])
def test_invalid_input_add():
    ts_1 = ArrayTimeSeries([1, 2], [3, 4])
    ts_2 = ArrayTimeSeries([1, 2, 3], [3, 4, 5])
    ts_3 = ArrayTimeSeries([2, 3], [5, 6])
    arry = [1, 2]
    with raises(ValueError):
        ts_sum = ts_1 + ts_2
    with raises(TypeError):
        ts_sum = ts_1 + arry
    with raises(ValueError):
        ts_sum = ts_1 + ts_3
Exemple #7
0
 def __init__(self, times, values, fsm, id=None):
     """
     Constructor for SMTimeSeries
     :param fsm: File manager
     :type fsm: FileStorageManager
     :param times: a sequence or a numpy array storing time stamps
     :param values: a sequence or a numpy array storing values
     :param id: ID of the timeseries
     """
     if not isinstance(values, collections.Sequence) and not \
             isinstance(values, np.ndarray):
         raise TypeError("Input values must be Sequence")
     if not isinstance(times, collections.Sequence) and not \
             isinstance(values, np.ndarray):
         raise TypeError("Input times must be Sequence")
     if len(times) != len(values):
         raise ValueError("Input values and times sequence must have "
                          "the same length")
     if len(times) != len(set(times)):
         raise ValueError("Input times sequence must be unique")
     self._fsm = fsm
     if id == None:
         id = self._fsm.gen_id()
     self._id = id
     self._fsm.store(self._id, ArrayTimeSeries(values, times))
Exemple #8
0
def clientThread(conn, sm):

    while True:
        #    conn.send("Server waiting for input:".encode())
        rec = conn.recv(8192)

        if not rec:
            break

        receivedData = pickle.loads(rec)
        if receivedData['cmd'] == "BYID":
            # Get ts storage using id received
            ts = sm.get(receivedData['id'])
            toSend = {
                "time": list(ts.times()),
                "value": list(ts.values()),
                "id": receivedData['id']
            }
            conn.send(pickle.dumps(toSend))

        elif receivedData['cmd'] == 'ADDTS':
            # Save to storage manager
            id, time, value = receivedData['id'], receivedData[
                'time'], receivedData['value']
            sm.store(id, ArrayTimeSeries(input_time=time, input_value=value))
            conn.send(pickle.dumps("Saved to DB!"))

    conn.close()
Exemple #9
0
def test_repr_greater_than_five():
    data = [4, 5, 6, 7, 8, 9]
    time = [1, 2, 3, 4, 5, 6]
    ts = ArrayTimeSeries(time, data)
    assert repr(
        ts
    ) == 'ArrayTimeSeries([(1, 4), (2, 5), (3, 6), (4, 7), (5, 8), (6, 9)])'
def test_empty_ts():
    ts = ArrayTimeSeries([], [])
    for t in ts._value == np.array([]):
        assert t
    for t in ts._time == np.array([]):
        assert t
    for t in ts._timeseries == np.array([]):
        assert t
Exemple #11
0
 def test_valid_init(self):
     """ All the sized timeseries __init__ constructors should take
         any sequence-like thing. Unsized should take a generator.
     """
     # NOTE: Depending on intepretation, this might be safe
     ts = TimeSeries([0], [0])
     ats = ArrayTimeSeries([0], [0])
     sts = SimulatedTimeSeries(zero_generator)
     scores.append(('#ts', 'init valid ts, ats, sts', 3))
Exemple #12
0
def test_setitem_index_error():
    data = [4, 5, 6]
    time = [1, 2, 3]
    ts = ArrayTimeSeries(time, data)
    index = 5
    val = 0

    with raises(IndexError):
        ts[index] = val
Exemple #13
0
    def __init__(self, input_time, input_value, id=None):
        """
		id is optional and could be None
		auto generate id is handled by FileStorageManager if id is None
		"""
        if id == None:
            id = StorageManager.generateId()
        StorageManager.store(str(id), ArrayTimeSeries(input_time, input_value))
        self._id = str(id)
Exemple #14
0
    def process(self, dat, conn):
        """
        process message from flask
        """
        fsm = FileStorageManager()

        query = dat['type']
        #return all metadata
        if query == "all":
            result = self._postgres.query_all()
        #filter by mean range
        elif query == "mean_in":
            result = self._postgres.query_mean_range(dat['mean_in'])
        #filtr by level
        elif query == "level_in":
            result = self._postgres.query_level(dat['level_in'])
        #filter
        elif query == "ts_data":
            msg = dat['ts_data']
            # add new timeseries given with key in jason format into database
            result = fsm.store(msg[0], ArrayTimeSeries(msg[1], msg[2]))
        elif query == "id":
            msg = dat['id']
            result = {}
            try:
                result['tsdata'] = fsm.get(msg)
                result['exist'] = 1
                result['metadata'] = self._postgres.query_id(msg)
            except ValueError:
                result['exist'] = 0
        #return nth closest ts data to ts with sim_id
        elif query == "ss_id":
            msg_id = dat['id']
            msg_k = dat['k']
            result = []
            try:
                ts = fsm.get(msg_id)
                min_dis, min_db_name, min_ts_file_name = max_similarity_search(
                    ts)
                result = kth_similarity_search(ts, min_dis, min_db_name, msg_k)
            except ValueError:
                pass
        #return nth closest ts data to ts dataset
        elif query == "ss_tsdata":
            ts = dat['tsdata']
            msg_k = dat['k']
            min_dis, min_db_name, min_ts_file_name = max_similarity_search(ts)
            result = kth_similarity_search(ts, min_dis, min_db_name, msg_k)
        else:
            raise NotImplementedError

        return result
 def get(self, id):
     """
     Get the timeseries using an given ID
     :param id: ID of timeseries
     :type id: int or string
     :return: timeseries of the given ID
     :rtype: SizedContainerTimeSeriesInterface
     """
     if not os.path.exists('data/ts_data_' + str(id) + '.txt'):
         raise ValueError(
             "Timeseries with ID={0} does not exist.".format(id))
     ts = np.loadtxt('data/ts_data_' + str(id) + '.txt')
     return ArrayTimeSeries(ts[:, 1], ts[:, 0])
Exemple #16
0
 def test_valid_nonempty_str_and_repr(self):
     """ All concrete classes should have a __str__() and __repr__(). Repeats are acceptable. """
     ts = TimeSeries([0], [0])
     ats = ArrayTimeSeries([0], [0])
     sts = SimulatedTimeSeries(zero_generator)
     for ts_class in [ts, ats, sts]:
         dunder_str = str(ts_class)
         dunder_repr = repr(ts_class)
         self.assertNotEqual(dunder_str, '')
         self.assertNotEqual(dunder_repr, '')
     scores.append(
         ('#ts', 'all concrete classes should have non-empty str and repr',
          1))
Exemple #17
0
    def get(self, id):
        '''The function to get the TimeSeries Object from the database.
		   Param:
		     id: the id of the TimeSeries object that taken out from 
		         database.
           Return: 
             The TimeSeries object got from the database.
        '''
        filename = self.dir_name + "/ts_" + str(id) + ".dat"
        try:
            f = open(filename, "r")
        except FileNotFoundError:
            raise Exception("The id is not in the database")
        else:
            array = json.loads(f.read())[str(id)]
            f.close()
            return ArrayTimeSeries(array[0], array[1])
Exemple #18
0
 def test_valid_nondefault_str_and_repr(self):
     """ All concrete classes should have non-default __str__() and __repr__(). """
     ts = TimeSeries([0], [0])
     ats = ArrayTimeSeries([0], [0])
     sts = SimulatedTimeSeries(zero_generator)
     self.assertNotRegexpMatches('^<.*.TimeSeries object at .*>$', str(ts))
     self.assertNotRegexpMatches('^<.*.TimeSeries object at .*>$', repr(ts))
     self.assertNotRegexpMatches('^<.*.ArrayTimeSeries object at .*>$',
                                 str(ats))
     self.assertNotRegexpMatches('^<.*.ArrayTimeSeries object at .*>$',
                                 repr(ats))
     self.assertNotRegexpMatches('^<.*.SimulatedTimeSeries object at .*>$',
                                 str(sts))
     self.assertNotRegexpMatches('^<.*.SimulatedTimeSeries object at .*>$',
                                 repr(sts))
     scores.append(
         ('#ts',
          'all concrete classes should have non-default str and repr', 1))
Exemple #19
0
    def get(self, tid):
        """
		The function gets the stored timeseries value on disk with the given id 

		Args
		------
		- tid: id for the time series values to be looked up from disk

		Return
		------
		values returned in the form of ArrayTimeSeries object. 
		will return non if no file existed with the given tid

		"""
        # Returns SizedContainerTimeSeriesInterface object
        if not isinstance(tid, str):
            tid = str(tid)
        if tid in self._id:
            timeseries = np.load(tid + ".npy")
            return ArrayTimeSeries(timeseries[0], timeseries[1])
        else:
            return None
Exemple #20
0
def clientThread(conn, storage_manager):
    while True:
    	rec = conn.recv(1024*4)
    	if not rec:
    		break
    	print("Server has been received:", rec)
    	
    	recv_data = pickle.loads(rec)

        if recv_data['cmd'] == "GET_BY_ID":
        	ts = storage_manager.get(recv_data['id'])
        	ts_dict = {"time": list(ts.times()), "value": list(ts.values()), "id": recv_data['id']}
        	conn.send(pickle.dumps(toSend))

        elif recv_data['cmd'] == 'ADD_TS':
        	id = recv_data['id']
        	time = recv_data['time']
        	value = recv_data['value']
        	storage_manager.store(id, ArrayTimeSeries(list(time.values)), list(value.values()))
        	conn.send(pickle.dumps("Saved to DataBase."))
        	
    conn.close()
def test_std():
    ts = ArrayTimeSeries([1, 2], [3, 4])
    assert ts.std() == sqrt(0.25)
def test_valid_input_sub():
    ts_1 = ArrayTimeSeries([1, 2], [3, 4])
    ts_2 = ArrayTimeSeries([1, 2], [3, 4])
    ts_sub = ts_1 - ts_2
    assert ts_sub == ArrayTimeSeries([0, 0], [3, 4])
def test_valid_input_add():
    ts_1 = ArrayTimeSeries([1, 2], [3, 4])
    ts_2 = ArrayTimeSeries([1, 2], [3, 4])
    ts_sum = ts_1 + ts_2
    assert ts_sum == ArrayTimeSeries([2, 4], [3, 4])
def test_contains():
    ts = ArrayTimeSeries(list(range(10)), list(range(1, 11)))
    assert (0 in ts) == True
    assert (11 in ts) == False
def test_invalid_input_setitem():
    ts = ArrayTimeSeries([1, 2], [3, 4])
    with raises(ValueError):
        ts[3] = 10
    with raises(TypeError):
        ts[{1}] = 10
def test_valid_input_setitem():
    ts = ArrayTimeSeries([1, 2], [3, 4])
    ts[0] = 10
    assert np.all(ts[0] == (10, 3)) == True
def test_getitem():
    ts = ArrayTimeSeries([3, 4], [1, 2])
    assert np.all(ts[1] == (4, 2)) == True
def test_len():
    ts = ArrayTimeSeries([3, 4], [1, 2])
    assert len(ts) == 2
def test_mean():
    ts = ArrayTimeSeries([1, 2], [3, 4])
    assert ts.mean() == 1.5
def test_lazy_check_length():
    l1 = ArrayTimeSeries(range(1, 4), range(0, 3))
    l2 = ArrayTimeSeries(range(2, 5), range(1, 4))
    thunk = check_length(l1, l2)
    assert isinstance(thunk, LazyOperation) == True
    assert thunk.eval() == True