def add_sensor_data(self, name, amplitude, noise_amp, delta_t, max_t, run_ave, trigger_value): """ Simulate a sensor and collect data with various parameters amplitude, noise_amp, delta_t, max_t, run_ave, trigger_value sets the trigger level for time, data not saved untill trigger value is reached """ # make a simulated sensor sensor = ss.SensorSim(amplitude, noise_amp, delta_t=delta_t) data0 = [] data1 = [] x0 = 0. if trigger_value > 0: trigger = dt.DataTrigger(trigger_value) if run_ave > 0: averager = moving_average.MovingAverage(run_ave) while x0 < max_t: x0, x1 = sensor.get_next_sample() if run_ave > 0: x1 = averager.nextVal(x1) add_flag = True if trigger_value > 0: add_flag = trigger.need_save(x0) if add_flag: data0.append(x0) data1.append(x1) self.add_line(name, data0, data1)
def test_validation(self): # check illegal characters in list with self.assertRaises(TypeError): data = [1, 2, 3, 'a'] ma.MovingAverage(data) # check negative values with self.assertRaises(ValueError): data = [1, 2, -3, 0] ma.MovingAverage(data, allow_negative_values=False) try: data = [1, 2, -3, 0] ma.MovingAverage(data, allow_negative_values=True) except ValueError: self.fail() # check empty values in list with self.assertRaises(Exception): data = [1, 2, 3, ''] ma.MovingAverage(data) data = [1, 2, 3, None] ma.MovingAverage(data) # check enough elements to calculate moving average with self.assertRaises(Exception): data = [] ma.MovingAverage(data) data = [1, 2, 3, 4] ma.MovingAverage(data, window=5)
def test_calculation(self): test_data = [10, 10, 10, 10] moving_average = ma.MovingAverage(test_data).get_data() ma_list = moving_average.replace(np.nan, '').tolist() result = ['', 10.0, 10.0, 10.0] self.assertListEqual(ma_list, result) test_data = [0, 10, 5, -4, 8, 16] moving_average = ma.MovingAverage( test_data, window=3, allow_negative_values=True).get_data() ma_list = moving_average.replace(np.nan, '').tolist() result = ['', '', 5.0, 11 / 3, 3.0, 20 / 3] self.assertListEqual(ma_list, result) test_data = [0, 12, 40, 671, 4, -531] moving_average = ma.MovingAverage( test_data, window=6, allow_negative_values=True).get_data() ma_list = moving_average.replace(np.nan, '').tolist() result = ['', '', '', '', '', sum(test_data) / len(test_data)] self.assertListEqual(ma_list, result)
import csv import japanize_matplotlib import pandas as pd import matplotlib.pyplot as plt import moving_average as ma #Analitics Weather data #Tokyo titile = 'TOKYO' data1990 = pd.read_csv('data/Tokyo_1990 - Tokyo1990.csv') temp_rolling1 = ma.MovingAverage(data1990['temp']) vap_pressure_rolling1 = ma.MovingAverage(data1990['vap_pressure']) humidity_rolling1 = ma.MovingAverage(data1990['humidity']) data1995 = pd.read_csv('data/Tokyo1995.csv') temp_rolling1 = ma.MovingAverage(data1995['temp']) vap_pressure_rolling1 = ma.MovingAverage(data1995['vap_pressure']) humidity_rolling1 = ma.MovingAverage(data1995['humidity']) data2000 = pd.read_csv('data/Tokyo_2000 - Tokyo2000.csv') temp_rolling2 = ma.MovingAverage(data2000['temp']) vap_pressure_rolling2 = ma.MovingAverage(data2000['vap_pressure']) humidity_rolling2 = ma.MovingAverage(data2000['humidity']) data2005 = pd.read_csv('data/Tokyo_2005.csv') temp_rolling2 = ma.MovingAverage(data2005['temp']) vap_pressure_rolling2 = ma.MovingAverage(data2005['vap_pressure']) humidity_rolling2 = ma.MovingAverage(data2005['humidity'])