Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Thu Nov 14 13:10:14 2019
- @author: s194734, s194729, s193743
- """
- import numpy as np
- import os
- import matplotlib.pyplot as plt
- #---------------------------------------------------------------------------
- #ITEMS
- xfilter=1
- filterOpt=["Enable", "Disable and reset"]
- menuItems=["Load data.","Filter Data.","Display Statistics.","Generate Plots.","Quit."]
- statisticItems=["Mean Temperature","Mean Growth rate","Std Temperature","Rows", "Mean Cold Growth rate","Mean Hot Growth rate"]
- filterItems=["Growth Rate Range","Temperature Range","Salmonella enterica","Bacillus cereus","Listeria","Brochothrix thermosphacta"]
- bacteriatypes=["Salmonella enterica","Bacillus cereus","Listeria","Brochothrix thermosphacta"]
- #---------------------------------------------------------------------------
- #Auxiliary functions
- def count(a,b):
- count = 0
- for i in range(len(a)):
- if a[i] == b:
- count=count+1
- return(count)
- def inputString(prompt): #Prompt user to input a string
- while True:
- try:
- string=str(input(prompt))
- break
- except ValueError:
- pass
- return string
- def inputNumber(prompt): #Prompt user to input a number(int)
- while True:
- try:
- num = int(input(prompt))
- break
- except ValueError:
- pass
- return num
- def inputNumberFloat(prompt): #Prompt user to input a number(int)
- while True:
- try:
- num = float(input(prompt))
- break
- except ValueError:
- pass
- return num
- def displayMenu(): #Main Menu
- print("""Bacteria Data\n\nMENU-------------------- """)
- if xfilter==1:
- print("Filter disabled")
- else: print("Filter enabled")
- for i in range(5):
- print("{:d}. {:s}".format(i+1,menuItems[i]))
- choice=0
- while not(np.any(choice == np.arange(5)+1)):
- choice = inputNumber("\n\nPlease choose a menu item: ")
- return choice
- def displaySubMenu(): #Sub Menu
- for i in range(len(statisticItems)):
- print("{:d}. {:s}".format(i+1, statisticItems[i]))
- choice = 0
- while not (np.any(choice == np.arange(6)+1)):
- choice = inputNumber("Please choose a statistic: ")
- return choice-1
- def displaySubMenu1(): #Sub Menu
- for i in range(len(filterItems)):
- print("{:d}. {:s}".format(i+1, filterItems[i]))
- choice = 0
- while not (np.any(choice == np.arange(6)+1)):
- choice = inputNumber("Please choose a filter: ")
- return choice-1
- def displaySubMenu2(): #Sub Menu
- for i in range(len(bacteriatypes)):
- print("{:d}. {:s}".format(i+1, bacteriatypes[i]))
- choice = 0
- while not (np.any(choice == np.arange(4)+1)):
- choice = inputNumber("Please choose a bacteria: ")
- return choice-1
- def displaySubMenu3(): #Sub Menu
- for i in range(len(filterOpt)):
- print("{:d}. {:s}".format(i+1, filterOpt[i]))
- choice = 0
- while not (np.any(choice == np.arange(2)+1)):
- choice = inputNumber("Filter: ")
- return choice-1
- #DataLoad-----------------------------------------------------------
- def dataLoad(filename):
- if os.path.isfile(filename) == False:
- print("Invalid path/filename")
- return dataLoad(inputString("Enter file name or directory:\n>"))
- else:
- data = np.loadtxt(filename)
- i = 0
- while i in range(len(data)):
- if data[i, 0] < 10 or data[i, 0] > 60:
- print("error in line", i, "temperature is out of range")
- i += 1
- i = 0
- while i in range(len(data)):
- if data[i, 1] < 0:
- print("error in line", i, "growth rate is not a negative number")
- i += 1
- i = 0
- while i in range(len(data)):
- if data[i, 2] > 4 or data[i, 2] < 1:
- print("error in line", i, "unrecognized bacteria ID number")
- i += 1
- i = 0
- while i in range(len(data)):
- if data[i, 0] < 10 or data[i, 0] > 60:
- data = np.delete(data, i, 0)
- i += 1
- i = 0
- while i in range(len(data)):
- if data[i, 1] < 0:
- data = np.delete(data, i, 0)
- i += 1
- i = 0
- while i in range(len(data)):
- if data[i, 2] > 4 or data[i, 2] < 1:
- data = np.delete(data, i, 0)
- i += 1
- return data
- #DataStatisics-------------------------------------------------------
- def dataStatistics(dataf, statistic):
- if statistic=="Mean Temperature":
- result=np.mean(dataf[:,0])
- elif statistic=="Mean Growth rate":
- result=np.mean(dataf[:,1])
- elif statistic=="Std Temperature":
- T=dataf[:,0]
- result=T.std(0)
- elif statistic=="Std Growth rate":
- G=dataf[:,1]
- result=G.std(0)
- elif statistic=="Rows":
- numrows=len(dataf)
- result=numrows
- elif statistic=="Mean Cold Growth rate":
- (x,y,z)=np.mean(dataf[dataf[:,0]<20],axis=0)
- result=y
- elif statistic=="Mean Hot Growth rate":
- (x,y,z)=np.mean(dataf[dataf[:,0]>50],axis=0)
- result=y
- return result
- #DataPlot---------------------------------------------------------------
- def dataPlot(dataf):
- objects = ("Salmonella enterica","Bacillus cereus","Listeria","Brochothrix thermosphacta")
- x = np.arange(len(objects))
- Numbacteria=dataf[:,2]
- y=np.array([])
- for i in range(1,5):
- y = np.hstack((y,count(Numbacteria,i))) #?
- plt.bar(x, y, align="center", alpha=0.5)
- plt.xticks(x, objects)
- plt.xlabel("Type of bacteria")
- plt.ylabel("Number per type")
- plt.title("Number of bacteria")
- plt.show()
- data1 = dataf[dataf[:, 2] == 1]
- x = data1[:,0]
- y = data1[:,1]
- data1 = dataf[dataf[:, 2] == 2]
- x2 = data1[:,0]
- y2 = data1[:,1]
- data1 = dataf[dataf[:, 2] == 3]
- x3 = data1[:,0]
- y3 = data1[:,1]
- data1 = dataf[dataf[:, 2] == 4]
- x4 = data1[:,0]
- y4 = data1[:,1]
- plt.plot(x, y,"or", label="Salmonela enterica") # Plot line graph of x and y
- plt.plot(x2, y2,"*g", label="Bacillus cereus") # Plot line graph of x and y
- plt.plot(x3, y3,"^y", label="Listeria") # Plot line graph of x and y
- plt.plot(x4, y4,"sb", label="Brochothrix thermosphacta") # Plot line graph of x and y
- plt.title("Growth rate by temperature") # Set the title of the graph
- plt.xlabel("Temperature") # Set the x-axis label
- plt.ylabel("Growth rate") # Set the y-axis label
- plt.xlim([10, 60]) # Set the limits of the x-axis
- plt.ylim([0, 1]) # Set the limits of the y-axis
- plt.legend(bbox_to_anchor=(1,1.05))
- plt.grid()
- plt.show()
- #Main Script-----------------------------------------------------------
- data=dataLoad(inputString("Enter file name or directory:\n>"))
- while True:
- choice=displayMenu()
- if choice==1: #Load Data
- print("\nLoad Data")
- data=dataLoad(inputString("Enter file name or directory:\n>"))
- elif choice==2: #End it later
- print("\nFilter")
- print("Filter activation")
- xfilter=displaySubMenu3()
- if xfilter==1:
- dataf=data
- elif xfilter==0:
- Filteropt=displaySubMenu1()
- dataf=data
- if Filteropt + 1 == 1:
- Min = inputNumberFloat("Minimum Value:\n")
- Max = inputNumberFloat("Maximum Value:\n")
- dataf = dataf[dataf[:, 1] > Min]
- dataf= dataf[dataf[:, 1] < Max]
- elif Filteropt + 1 == 2:
- Min = inputNumberFloat("Minimum temperature:\n")
- Max = inputNumberFloat("Maximum temperature:\n")
- dataf = dataf[dataf[:, 0] > Min]
- dataf = dataf[dataf[:, 0] < Max]
- elif Filteropt + 1 == 3:
- dataf = dataf[dataf[:, 2] == 1]
- elif Filteropt + 1 == 4:
- dataf = dataf[dataf[:, 2] == 2]
- elif Filteropt + 1 == 5:
- dataf = dataf[dataf[:, 2] == 3]
- elif Filteropt + 1 == 6:
- dataf = dataf[dataf[:, 2] == 4]
- elif choice==3: #Draw Plots
- if xfilter==1:
- print("\nStatistics")
- print("Which statistic do you want to calcultate?\n")
- statistic=statisticItems[displaySubMenu()]
- dataStat=dataStatistics(data,statistic)
- print(dataStat)
- print("\n")
- elif xfilter==0:
- print("\nStatistics")
- print("Which statistic do you want to calcultate?\n")
- statistic=statisticItems[displaySubMenu()]
- dataStat=dataStatistics(dataf,statistic)
- print(dataStat)
- print("\n")
- elif choice==4:
- if xfilter==1:
- dataPlot(data)
- elif xfilter==0:
- dataPlot(dataf)
- elif choice==5:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement