Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- import os.path
- import matplotlib.pyplot as plt
- class Interface:
- def __init__(self):
- print("Welcome to Bacteria'R'us™\nTo return to main menu enter 'quit'")
- self.data = pd.DataFrame([])
- self.filename = None
- self.filterchosen = False
- self.bactchosen = False
- self.growchosen = False
- self.bactfilterList = []
- self.growfilterlist = []
- self.l = pd.DataFrame([])
- def mainprompt(self):
- while True:
- print('Main menu\nYou have the following options:')
- print('1. Load data')
- print('2. Filter data')
- print('3. Display statistics')
- print('4. Generate plots')
- print('5. Quit')
- choice = input(">> Choose option\n")
- if choice in ['5', '5.', 'Quit', 'quit', 'q', 'Q']:
- print("This instance of Bacteria'R'us™ has terminated.")
- print('The team behind wishes you a nice day')
- return (0)
- elif os.path.exists(choice) == True:
- self.data = self.dataLoad(choice)
- elif choice in ['1', '1.', 'Load data', 'Load Data', 'load data']:
- global filename
- filename = input(">>Enter datafile\n")
- if os.path.exists(filename) == True:
- self.data = self.dataLoad(filename)
- else:
- print('Invalid file path\n')
- elif choice in ['2', '2.', 'Filter data', 'Filter Data', 'filter data']:
- if self.data.empty == True:
- print('no data')
- else:
- self.filterchosen = True
- self.dataFilter()
- elif choice in ['3', '3.', 'Display statistics', 'Display Statistics', 'display statistics']:
- if self.data.empty == True:
- print('no data')
- else:
- self.dataStatistics()
- elif choice in ['4', '4.', 'Generate plots', 'Generate Plots', 'generate plots']:
- if self.data.empty == True:
- print('no data')
- else:
- self.plots()
- # make plot function
- else:
- print('Invalid option')
- def dataLoad(self, filename):
- self.data = pd.read_csv(filename, sep=' ', names=['Temperature', 'Growth rate', 'Bacteria'])
- # the columns are named to evaluate conditions below
- Temp = np.array(self.data.iloc[:, 0])
- Growthrate = np.array(self.data.iloc[:, 1])
- Bact = np.array(self.data.iloc[:, 2])
- # a new matrix
- self.l = pd.DataFrame([])
- for i in range(len(Temp)):
- if Temp[i] < 10 or Temp[i] > 60:
- print('Data on line ' + str(
- i) + ' of file was omitted \nReason: temperature must be between 10 and 60 degrees')
- elif Growthrate[i] < 0:
- print('Data on line ' + str(i) + ' of file was omitted \nReason: Growth rate must be positive')
- elif Bact[i] not in [1, 2, 3, 4]:
- print('Data on line ' + str(i) + ' of file was omitted \nReason: Bacteria must be one of no. 1-4')
- else:
- self.l = self.l.append(self.data.iloc[i, :])
- self.data = self.l
- print(self.data)
- print('\nThese are the data\n')
- return self.data
- #return self.l
- def dataFilter(self):
- while True:
- if self.bactchosen:
- self.bactFilter()
- if self.growchosen:
- self.growFilter()
- if not self.filterchosen:
- return
- afilter = input('Filter bacteria type or growth rate? ')
- if afilter in ['Bacteria', 'B', 'b']:
- self.bactchosen = True
- elif afilter in ['Growth', 'G', 'g']:
- self.growchosen = True
- elif afilter in ['Q', 'q', 'Quit', 'quit', 'r', 'R', 'Return', 'return']:
- self.bactchosen = False
- return
- else:
- print('incorrect option')
- def bactFilter(self):
- while True:
- print("Bacteria filtered:", str(self.bactfilterList),
- "\nenter 'Return' for other filters or 'Quit' to exit to main")
- n = input('toggle bacterium number:(1-4)')
- if n in ['Q', 'q', 'Quit', 'quit', 'r', 'R', 'Return', 'return']:
- self.bactchosen = False
- if n in ['Q', 'q', 'Quit', 'quit']:
- self.filterchosen = False
- return
- elif n in []:
- self.bactchosen = False
- return
- elif n in ['1', '2', '3', '4', '1.0', '2.0', '3.0', '4.0']:
- if float(n) in self.bactfilterList:
- self.bactfilterList.remove(float(n))
- else:
- self.bactfilterList += [float(n)]
- self.l = pd.DataFrame([])
- length = len(np.array(self.data.iloc[:, 0]))
- for i in range(length):
- if self.data.iloc[i, 0] in self.bactfilterList:
- pass
- else:
- self.l = self.l.append(self.data.iloc[i, :])
- print(self.l)
- else:
- print('invalid number')
- def growFilter(self):
- while True:
- print('growth rate filters:', str(self.growfilterlist))
- length = len(np.array(self.data.iloc[:, 0]))
- lower = input('enter lower bound(float)')
- if lower in ['Q', 'q', 'Quit', 'quit', 'r', 'R', 'Return', 'return']:
- self.growchosen = False
- if lower in ['Q', 'q', 'Quit', 'quit']:
- self.filterchosen = False
- return
- upper = input('enter upper bound(float)')
- if upper in ['Q', 'q', 'Quit', 'quit', 'r', 'R', 'Return', 'return']:
- self.growchosen = False
- if upper in ['Q', 'q', 'Quit', 'quit']:
- self.filterchosen = False
- print('quitted')
- return
- elif (lower == float) and (upper == float):
- # elif float(u) not float: #or (float(l) is not float):
- # print('range must be entered as float values')
- self.growfilterlist.append(lower)
- self.l=pd.DataFrame([])
- for i in range(length):
- if float(l) > float(self.data.iloc[i, 0]) or float(self.data.iloc[i, 1]) > float(u):
- pass
- else:
- self.l = self.l.append(self.data.iloc[i, :])
- print(self.l)
- def dataStatistics(self):
- while True:
- print('Options:')
- print('1.’Mean Temperature’:\nMean (average) Temperature.')
- print('\n2.’Std Temperature’:\nStandard deviation of Temperature.')
- print('\n3.’Std Growth rate’:\nStandard deviation of Growth rate.')
- print('\n4.’Rows’:\nThe total number of rows in the data.')
- print('\n5.’Mean Cold Growth rate’:\nAverage Growth rate for Temperature below 20 degrees.')
- print('\n6.’Mean Hot Growth rate’:\nAverage Growth rate for Temperature above 50 degrees.')
- statistics = input("choose statistic:")
- result = 0
- if self.l.empty == True:
- print('Statistics can not be computed when all data is filtered')
- return
- if statistics in ['1', 'Mean Temperature', 'mean temperature', 'mean Temperature', 'mt', 'MT']:
- result = np.mean(np.array(self.l.iloc[:, 2]))
- print(result)
- elif statistics in ['2', 'Mean Growth rate', 'MGR', 'mgr']:
- result = np.mean(np.array(self.l.iloc[:, 1]))
- print(result)
- elif statistics in ['3', 'Std Temperature', 'ST', 'st']:
- result = np.std(np.array(self.l.iloc[:, 2]))
- print(result)
- elif statistics in ['4', 'Rows', 'rows', 'rs']:
- result = len(np.array(self.l.iloc[:, 0]))
- print(result)
- elif statistics in ['5', 'Mean Cold Growth Rate', 'mean cold growth rate', 'mcgr', 'MCGR']:
- Sum = 0
- n = 0
- for i in range(len(self.l.iloc[:, 1])):
- if self.l.iloc[i, 2] < 20:
- n += 1
- Sum += self.l.iloc[i, 1]
- if n <= 0:
- print('no data below 20 degrees found')
- else:
- result = Sum / n
- print(result)
- elif statistics in ['6', 'Mean Hot Growth Rate', 'mean hot growth rate', 'mhgr', 'MHGR']:
- Sum = 0
- n = 0
- for i in range(len(self.l.iloc[:, 1])):
- if self.l.iloc[i, 2] > 50:
- n += 1
- Sum += self.l.iloc[i, 1]
- if n <= 0:
- print('no data above 50 degrees found')
- else:
- result = Sum / n
- print(result)
- elif statistics in ['Q', 'q', 'Quit', 'quit', 'r', 'R', 'Return', 'return']:
- return
- # enter rest
- else:
- print("not valid, retry or quit?")
- def plots(self):
- plotchoice=input("What plots do you want to see? \nYou have the following options: \n1.Frequency of the Bacteria \n2.Growth Rate by Temperature")
- if plotchoice in ['1', 'Bacteria', 'bacteria','b','B']:
- measurements = [0,0,0,0]
- for i in range (len(self.l.iloc[:,0])):
- if self.l.iloc[i,0] == 1.0:
- measurements[0]+=1
- elif self.l.iloc[i,0] == 2.0:
- measurements[1]+=1
- elif self.l.iloc[i,0] == 3.0:
- measurements[2]+=1
- elif self.l.iloc[i,0] == 4.0:
- measurements[3]+=1
- objects = ('Salmonella \nenterica', 'Bacillus \ncereus', 'Listeria', 'Brochothrix \nthermosphacta')
- y_pos=np.arange(len(objects))
- plt.bar(y_pos, measurements, width=0.7, align='center', alpha=0.7)
- plt.xticks(y_pos, objects)
- plt.ylabel('Amount')
- plt.xlabel('Bacteria',fontdict=None, labelpad=None)
- plt.title('Frequency of the Bacteria')
- plt.show()
- elif plotchoice in ['2','GT','gt','Gt','gT','G','g','Growth','growth','Temperature','temperature','Growth Temperature','Growth temperature','growth temperature']:
- bacttemp1=[]
- bacttemp2=[]
- bacttemp3=[]
- bacttemp4=[]
- bactgrowth1=[]
- bactgrowth2=[]
- bactgrowth3=[]
- bactgrowth4=[]
- length=len(self.l.iloc[:,0])
- for i in range(length):
- if self.l.iloc[i,0] == [1.0]:
- bacttemp1.append(self.l.iloc[i,2])
- bactgrowth1.append(self.l.iloc[i,1])
- elif self.l.iloc[i,0] == 2.0:
- bacttemp2.append(int(self.l.iloc[i,2]))
- bactgrowth2.append(self.l.iloc[i,1])
- elif self.l.iloc[i,0] == 3.0:
- bacttemp3.append(int(self.l.iloc[i,2]))
- bactgrowth3.append(self.l.iloc[i,1])
- elif self.l.iloc[i,0] == 4.0:
- bacttemp4.append(self.l.iloc[i,2])
- bactgrowth4.append(self.l.iloc[i,1])
- z1 = sorted(zip(bacttemp1,bactgrowth1))
- bacttemp1=[i[0] for i in z1]
- bactgrowth1=[i[1] for i in z1]
- z2 = sorted(zip(bacttemp2,bactgrowth2))
- bacttemp2=[i[0] for i in z2]
- bactgrowth2=[i[1] for i in z2]
- z3 = sorted(zip(bacttemp3,bactgrowth3))
- bacttemp3=[i[0] for i in z3]
- bactgrowth3=[i[1] for i in z3]
- z4 = sorted(zip(bacttemp4,bactgrowth4))
- bacttemp4=[i[0] for i in z4]
- bactgrowth4=[i[1] for i in z4]
- # setting up a graph showing the relation of the growth rate to the temperature for each type of bacteria
- plt.plot(bacttemp1, bactgrowth1, 'b-', label='Salmonella enterica')
- plt.plot(bacttemp2, bactgrowth2, 'r-', label='Bacillus cereus')
- plt.plot(bacttemp3, bactgrowth3, 'g-', label='Listeria')
- plt.plot(bacttemp4, bactgrowth4, 'm-', label='Brochothrix thermosphacta')
- plt.legend(loc='best') #'best' localisation makes sure that the legend won't cover the data
- plt.ylabel('Growth rate')
- plt.xlabel('Temperature')
- plt.title('Growth Rate by Temperature for different bacteria')
- plt.xlim(xmin=10.0, xmax=60.0)
- plt.ylim(ymin=0.0, ymax=1.0)
- plt.grid(True)
- plt.show()
- if __name__ == '__main__':
- interface = Interface()
- interface.mainprompt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement