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: bekky
- """
- import numpy as np
- import os
- #---------------------------------------------------------------------------
- #ITEMS
- 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","Temp 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 = float(input(prompt))
- break
- except ValueError:
- pass
- return num
- def displayMenu(): #Main Menu
- print("""Bacteria Data\n\nMENU-------------------- """)
- 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(7)+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
- #DataLoad-----------------------------------------------------------
- def dataLoad(filename):
- if os.path.isfile(filename)== False:
- data="error, not a valid directory"
- else:
- data = np.loadtxt(filename)
- data1= data
- 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")
- data = np.delete(data, i, 0)
- 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")
- 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:
- print("error in line", i, "unrecognized bacteria ID number")
- data = np.delete(data, i, 0)
- i+=1
- print (data)
- return data
- #DataStatisics-------------------------------------------------------
- def dataStatistics(data, statistic):
- if statistic=="Mean Temperature":
- result=np.mean(data[:,0])
- elif statistic=="Mean Growth rate":
- result=np.mean(data[:,1])
- elif statistic=="Std Temperature":
- T=data[:,0]
- result=T.std(0)
- elif statistic=="Std Growth rate":
- G=data[:,1]
- result=G.std(0)
- elif statistic=="Rows":
- numrows=len(data)
- result=numrows
- elif statistic=="Mean Cold Growth rate":
- (x,y,z)=np.mean(data[data[:,0]<20],axis=0)
- result=y
- elif statistic=="Mean Hot Growth rate":
- (x,y,z)=np.mean(data[data[:,0]>50],axis=0)
- result=y
- return result
- def dataPlot(data):
- data=np.loadtxt("Project 1.txt")
- def plot1():
- objects = ("1","2","3","4")
- y_pos = np.arange(len(objects))
- #count how many times each number(type of bacteria) appears in the sub-matrix
- #shoul i create a new function to do this and then call it to plot the bar graph?
- Numbacteria=data[:,2]
- performance = [count(np.where(Numbacteria==objects))] #?
- plt.bar(y_pos, performance, align="center", alpha=0.5)
- plt.xticks(y_pos, objects)
- plt.ylabel("Number per type")
- plt.title("Number of bacteria")
- plt.show()
- def plot2():
- i=0
- while (data[:,2])[i]==1:
- x = (data[:,0])[i]
- y = (data[:,1])[i]
- plt.plot(x, y,"-r", label="Salmonela enterica") # 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
- i=i+1
- while (data[:,2])[i]==2:
- x = (data[:,0])[i]
- y = (data[:,1])[i]
- plt.plot(x, y,"-g", label="Bacillus cereus") # 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
- i=i+1
- while (data[:,2])[i]==3:
- x = (data[:,0])[i]
- y = (data[:,1])[i]
- plt.plot(x, y,"-b", label="Listeria") # 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
- i=i+1
- while (data[:,2])[i]==4:
- x = (data[:,0])[i]
- y = (data[:,1])[i]
- plt.plot(x, y,"-y", 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
- i=i+1
- plt.show() #write plt.show() once in the end and it will plot both graphs at the same time
- #Main Script-----------------------------------------------------------
- while True:
- choice=displayMenu()
- if choice==1: #Load Data
- print("\nLoad Data")
- data=dataLoad(inputString("Enter file name:\n>"))
- elif choice==2: #End it later
- print("\nFilter")
- Filteropt=displaySubMenu1()
- print(Filteropt)
- if Filteropt+1==1:
- Min=inputNumber("Minimum Value")
- Max=inputNumber("Maximum Value")
- data=data[data[:,1]>Min]
- elif choice==3: #Draw Plots
- print("\nStatistics")
- print("Which statistic do you want to calcultate?\n")
- statistic=statisticItems[displaySubMenu()]
- dataStat=dataStatistics(data,statistic)
- print(dataStat)
- print("\n")
- elif choice==4:
- pass
- elif choice==5:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement