def main(): with open ('flowstudy.csv', newline='') as csvfile: flowreader = csv.reader(csvfile, delimiter=',') for row in flowreader: flowrates.append({"date": datetime.strptime(row[0], '%m/%d/%Y %H:%M'), "flowrate": float(row[1])}) for d in flowrates: hour = d["date"].hour Hourly[hour].append(d["flowrate"]) Length[hour] = Length[hour] + 1 for i in range(24): Average[i] = statistics.mean(Hourly[i]) Deviation[i] = statistics.stdev(Hourly[i]) Hourly[i] = sorted(Hourly[i]) # Years of predicted data predicted_data = [] for i in range(YEARS * 365 * 24): predicted_data.extend(stats.norm.rvs(Average[i % 24], Deviation[i % 24], size=1)) # Initalize two pumps A = Pump() B = Pump() hourly_data = [] #with open ('pumps.json', 'r') as output: # json.read((A.__dict__, B.__dict__), output) for i in predicted_data: gpm = convert_to_gpm(i) temp_data = [] temp_data.append(int(gpm)) # This code checks the pumps max flowrate and splits the flow between the two pumps # and adds a small overhead. if A.get_max() > gpm: temp_data.append(A.hours_cost(gpm)) B.idle_wear() else: temp_data.append(A.hours_cost(gpm)) temp_data.append(B.hours_cost(gpm)) hourly_data.append(temp_data) # Print the final numbers with open ('output/data.csv', 'w') as output: writer = csv.writer(output, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL) for z in hourly_data: writer.writerow(z) with open ('output/average.csv', 'w') as output: writer = csv.writer(output, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) writer.writerow(("Pump A Energy Total Cost", int(A.total_energy_cost))) writer.writerow(("Pump A Repair Total Cost", int(A.total_repair_cost))) writer.writerow(("Pump B Energy Total Cost", int(B.total_energy_cost))) writer.writerow(("Pump B Repair Total Cost", int(B.total_repair_cost))) writer.writerow(("Pump A Energy Yearly Cost", int(A.total_energy_cost / YEARS))) writer.writerow(("Pump A Repair Yearly Cost", int(A.total_repair_cost / YEARS))) writer.writerow(("Pump B Energy Yearly Cost", int(B.total_energy_cost / YEARS))) writer.writerow(("Pump B Repair Yearly Cost", int(B.total_repair_cost / YEARS)))
def setUp(self): """ setup """ self.pump = Pump('127.0.0.1', 8000) self.decider = Decider(100, 0.05) self.sensor = Sensor('127.0.0.1', '8001') self.controller = Controller(self.sensor, self.pump, self.decider) self.actions = self.controller.actions
def setUp(self): """setup to use in integration testing""" self.sensor = Sensor('127.0.0.1', '8000') self.pump = Pump('127.0.0.1', '8001') self.decider = Decider(30, 0.05) self.controller = Controller(self.sensor, self.pump, self.decider) self.pump.set_state = MagicMock(return_value=True)
def __init__(self): print('CultureFlow_Model object created') self.pump = Pump() self.mswitch = Mswitch() self.devices = {"pump":self.pump,"mswitch":self.mswitch}
def setUp(self): self.sensor = Sensor('http://localhost', '8080') self.pump = Pump('http://localhost', '8080') self.decider = Decider(10, .1) self.controller = Controller(self.sensor, self.pump, self.decider) self.pump.set_state = MagicMock(return_value=True) self.decider.decide = MagicMock(return_value='PUMP_IN')
def test_module(self): """ Test the waterregulation module """ sensor = Sensor('127.0.0.1', '3080') pump = Pump('127.0.0.1', '4080') decider = Decider(100, .10) controller = Controller(sensor, pump, decider) controller.pump.set_state = MagicMock(return_value=True) levels = range(89, 112) for action in controller.actions.values(): for liquid_height in levels: controller.sensor.measure = MagicMock( return_value=liquid_height ) controller.pump.get_state = MagicMock( return_value=decider.decide( liquid_height, action, controller.actions ) ) controller.tick()
def test_decide(self): """ Method to test decide function """ pump = Pump('127.0.0.1', 8000) sensor = Sensor('127.0.0.1', 8000) pump.set_state = MagicMock(return_value=True) decider = Decider(10., 0.05) controller = Controller(sensor, pump, decider) self.assertEqual(decider.decide(1.0, int(0), controller.actions), controller.actions['PUMP_IN']) self.assertEqual(decider.decide(20.0, int(0), controller.actions), controller.actions['PUMP_OUT']) self.assertEqual(decider.decide(10.0, int(0), controller.actions), controller.actions['PUMP_OFF']) self.assertEqual(decider.decide(20.0, int(1), controller.actions), controller.actions['PUMP_OFF']) self.assertEqual(decider.decide(1.0, int(1), controller.actions), controller.actions['PUMP_IN']) self.assertEqual(decider.decide(1.0, int(-1), controller.actions), controller.actions['PUMP_OFF']) self.assertEqual(decider.decide(20.0, int(-1), controller.actions), controller.actions['PUMP_OUT']) with self.assertRaises(ValueError): decider.decide(10., 2.5, controller.actions)
def setUp(self): self.sensor = Sensor(address="0.0.0.0", port=514) self.pump = Pump(address="0.0.0.0", port=514) self.decider = Decider(target_height=100, margin=.10) self.controller = Controller(sensor=self.sensor, pump=self.pump, decider=self.decider)
def __init__(self, refPath, dataPath, dbFilename): GPIO.cleanup() GPIO.setmode(GPIO.BCM) GPIO.setup(self.AUTO_START_GPIO_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) self.__i2c = I2C(2) self.__analog = Analog(sel.__i2c.getLock(), 0x49) self.default = Default() self.database = Database(dataPath, dbFilename) self.waterlevel = Waterlevel(debug, self.database) self.light = Light(self.database) self.curtain = Curtain(self.database) self.pressure = Pressure(self.database, self.default, self.__analog) self.temperature = Temperature("28-0417716a37ff", self.database) self.redox = Redox(debug, self.database, self.default, self.__analog) self.ph = PH(debug, self.database, self.default, self.__analog, self.temperature) self.robot = Robot(debug, self.database) self.pump = Pump(debug, self.database, self.default, self.robot, self.redox, self.ph, self.temperature) self.panel = Panel(debug, self.database, self.default, self.pump, self.redox, self.ph, self.__i2c) self.statistic = Statistic(debug, self.pump, self.robot, self.redox, self.ph, self.temperature, self.pressure, self.waterlevel) self.refPath = refPath self.__autoSaveTick = 0 self.__today = date.today().day - 1 debug.TRACE(debug.DEBUG, "Initialisation done (Verbosity level: %s)\n", debug)
def test_integration(self): ''' I can see how you could use mocks to provide data for classes that aren't actually implemented yet. However, since both the set_state and get_state methods of pump are overridden the code doesn't seem to really test anything except that it doesn't crash. The important part, the decide method, is covered in the unit tests so I guess this mock method is as good as we can test until the pump and sensor are actually hooked up to live objects. I can see how the test can be converted to test real objects by just taking out the mock method assignments. ''' decider = Decider(4, 3) pump = Pump('127.0.0.1', 8000) pump.set_state = MagicMock(return_value=False) pump.get_state = MagicMock(return_value=Pump.PUMP_OFF) sensor = Sensor('127.0.0.1', 8000) sensor.measure = MagicMock(return_value=3) controller = Controller(sensor, pump, decider) self.assertEqual(controller.tick(), False)
def setUp(self): """ Create the sensor, pump, decider, and controller. The sensor is not really needed, and the pump isn't either except for its constants. The decider specifies the target height and the margin, while the controller is mainly needed for its constants (captured in a dict) as well. """ self.sensor = Sensor('127.0.0.1', 8000) self.pump = Pump('127.0.0.1', 8000) self.pump.set_state = MagicMock(return_value=True) self.decider = Decider(100, 0.05) self.controller = Controller(self.sensor, self.pump, self.decider) # Dict of correct future pump actions based on the current pump # action and the current height (using a target height of 100) self.outcomes_high_or_low = { # (current action, height): next action (self.pump.PUMP_IN, 90): self.pump.PUMP_IN, (self.pump.PUMP_IN, 110): self.pump.PUMP_OFF, (self.pump.PUMP_OUT, 90): self.pump.PUMP_OFF, (self.pump.PUMP_OUT, 110): self.pump.PUMP_OUT, (self.pump.PUMP_OFF, 90): self.pump.PUMP_IN, (self.pump.PUMP_OFF, 110): self.pump.PUMP_OUT }
def setup(cls): """ Create sensor, pump, decider and controller """ cls.pump = Pump('127.0.0.1', 8000) cls.decider = Decider(100, 0.05) cls.actions = {'PUMP_IN': 1, 'PUMP_OFF': 0, 'PUMP_OUT': -1}
def test_module(self): """ Go through various sensor.measure and pump.get_state values to verify the compatibility of decider and controller. """ dec = Decider(100, .05) control = Controller( Sensor("127.0.0.1", "8000"), Pump("127.0.0.1", "8000"), dec ) control.pump.set_state = MagicMock(return_value=True) levels = [110, 90, 99] for pump_act in control.actions.values(): control.pump.get_state = MagicMock(return_value=pump_act) for water_h in levels: control.sensor.measure = MagicMock(return_value=water_h) control.tick() control.pump.get_state = MagicMock( return_value=dec.decide( control.sensor.measure(), control.pump.get_state(), control.actions ) )
def test_controller_tick(self): """test the tick function in the controller""" sensor = Sensor('127.0.0.1', 8000) pump = Pump('127.0.0.1', 8000) dec = Decider(100, .05) con = Controller(sensor, pump, dec) # liquid height sensor.measure = MagicMock(return_value=94) # state of pump pump.get_state = MagicMock(return_value=pump.PUMP_IN) # decider next state for pump dec.decide = MagicMock(return_value=pump.PUMP_IN) # this line was added to fix my error pump.set_state = MagicMock(return_value=True) con.tick() sensor.measure.assert_called_with() pump.get_state.assert_called_with() dec.decide.assert_called_with(94, pump.PUMP_IN, actions)
def test_setup(self): """ Set values for tests that follow """ self.sensor = Sensor('127.0.0.1', 8000) self.pump = Pump('127.0.0.1', 8000) self.decider = Decider(100, .05) self.controller = Controller(self.sensor, self.pump, self.decider)
def setUp(self): """ Just some example syntax that you might use """ self.pump = Pump('127.0.0.1', 1) self.sensor = Sensor('127.0.0.2', 2) self.decider = Decider(300, 0.10) self.controller = Controller(self.sensor, self.pump, self.decider)
def setUp(self): ''' setup ''' self.decider = Decider(5, 2) self.pump = Pump('127.0.0.1', 8000) self.sensor = Sensor('127.0.0.1', 8000) self.controller = Controller(self.sensor, self.pump, self.decider)
def setUp(self): """Setup docstring.""" address = "127.0.0.1" port = "8000" self.sensor = Sensor(address, port) self.pump = Pump(address, port) self.decider = Decider(100, 0.05) self.controller = Controller(self.sensor, self.pump, self.decider)
def setUp(self): """ Create instance for testing """ self.decider = Decider(100, 0.05) self.pump = Pump('127.0.0.1', 8000) self.sensor = Sensor('127.0.0.1', 8000) self.controller = Controller(self.sensor, self.pump, self.decider)
def setUp(self): """ Setup for all 3 tests """ self.pump = Pump('127.0.0.1', '8080') self.sensor = Sensor('127.0.0.1', '8081') self.decider = Decider(100, .10) self.controller = Controller(self.sensor, self.pump, self.decider)
def test_unique_two_delete_not_found(self): # Delete the zip code from an existing address that doesn't have a zip code. p = Pump("data/org_def.json") p.update_data = {1: {u'uri': u'http://vivo.ufl.edu/individual/n765319', u'zip': u'None'}} [add, sub] = p.update() self.assertTrue(len(add) == 0 and len(sub) == 0)
def setUp(self): self.sensor = Sensor('127.0.0.1', 8000) self.pump = Pump('127.0.0.1', 8000) self.decider = Decider('127.0.0.1', 8000) self.controller = Controller(sensor=self.sensor, pump=self.pump, decider=self.decider)
def setUp(self): """ Sets up controller. """ self.sensor = Sensor("127.0.0.1", 8080) self.pump = Pump("127.0.0.1", 8050) self.decider = Decider(100, .05) self.controller = Controller(self.sensor, self.pump, self.decider)
def test_multiple_one_delete(self): # Empty the set of values p = Pump("data/person_def.json", verbose=True) p.update_data = {1: {u'uri': u'http://vivo.ufl.edu/individual/n25674', u'research_areas': u'None'}} [add, sub] = p.update() self.assertTrue(len(add) == 0 and len(sub) == 4)
def setUp(self): """ Sets up pump, sensor, decider and controller for use with unit tests. """ self.pump = Pump('127.0.0.1', 8000) self.sensor = Sensor('127.0.0.1', 8000) self.decider = Decider(target_height=100, margin=0.05) self.controller = Controller(self.sensor, self.pump, self.decider)
def test_unique_one_delete(self): from rdflib import URIRef, Literal, XSD p = Pump() p.update_data = {1: {u'uri': u'http://vivo.ufl.edu/individual/n111669', u'abbreviation': u'None'}} [add, sub] = p.update() self.assertTrue( len(add) == 0 and len(sub) == 1 and (URIRef("http://vivo.ufl.edu/individual/n111669"), URIRef("http://vivoweb.org/ontology/core#abbreviation"), Literal("JWRU", datatype=XSD.string)) in sub)
def setUpClass(self): """setting up for controller test""" self.pump = Pump('127.0.0.1', 8000) self.sensor = Sensor('127.0.0.1', 8000) self.decider = Decider(100, 0.05) self.actions = {'PUMP_IN': 1, 'PUMP_OFF': 0, 'PUMP_OUT': -1} self.controller = Controller(self.sensor, self.pump, self.decider)
def setUp(self): """ This method does a setup for unit testing Controller """ self.pump = Pump('127.0.0.1', 1000) self.sensor = Sensor('127.0.0.2', 2000) self.decider = Decider(100, 0.05) self.controller = Controller(self.sensor, self.pump, self.decider)
def test_multiple_one_change_nothing(self): # Do nothing if the multiple values specified match those in VIVO p = Pump("data/person_def.json", verbose=True) p.update_data = {1: {u'uri': u'http://vivo.ufl.edu/individual/n1723097935', u'types': u'person;thing;agent;fac;uf;ufc'}} [add, sub] = p.update() self.assertTrue(len(add) == 0 and len(sub) == 0)
def setUp(self): # setup controller self.sensor = Sensor('127.0.0.1', 8000) self.pump = Pump('127.0.0.1', 8000) self.decider = Decider(target_height=100, margin=.1) self.controller = Controller(sensor=self.sensor, pump=self.pump, decider=self.decider)