Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.17 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Nov 14 13:10:14 2019
  4.  
  5. @author: bekky
  6. """
  7. import numpy as np
  8. import os
  9. #---------------------------------------------------------------------------
  10. #ITEMS
  11. menuItems=["Load data.","Filter Data.","Display Statistics.","Generate Plots.","Quit."]
  12. statisticItems=["Mean Temperature","Mean Growth rate","Std Temperature","Rows", "Mean Cold Growth rate","Mean Hot Growth rate"]
  13. filterItems=["Growth Rate Range","Temp Range","Salmonella enterica","Bacillus cereus","Listeria","Brochothrix thermosphacta"]
  14. bacteriatypes=["Salmonella enterica","Bacillus cereus","Listeria","Brochothrix thermosphacta"]
  15. #---------------------------------------------------------------------------
  16. #Auxiliary  functions
  17.  
  18.  
  19. def count(a,b):
  20.     count = 0
  21.     for i in range(len(a)):
  22.         if a[i] == b:
  23.             count=count+1
  24.     return(count)
  25.    
  26. def inputString(prompt):                        #Prompt user to input a string
  27.     while True:
  28.         try:
  29.             string=str(input(prompt))
  30.             break
  31.         except ValueError:
  32.             pass
  33.     return string
  34.            
  35. def inputNumber(prompt):                    #Prompt user to input a number(int)
  36.     while True:
  37.         try:
  38.             num = float(input(prompt))
  39.             break
  40.         except ValueError:
  41.             pass
  42.     return num
  43.  
  44.  
  45.  
  46. def displayMenu():                                          #Main Menu
  47.     print("""Bacteria Data\n\nMENU-------------------- """)
  48.     for i in range(5):
  49.         print("{:d}.  {:s}".format(i+1,menuItems[i]))
  50.     choice=0
  51.     while not(np.any(choice == np.arange(5)+1)):
  52.         choice = inputNumber("\n\nPlease choose a menu item: ")
  53.     return choice
  54.  
  55.  
  56. def displaySubMenu():                                       #Sub Menu
  57.     for i in range(len(statisticItems)):
  58.         print("{:d}. {:s}".format(i+1, statisticItems[i]))
  59.     choice = 0
  60.     while not (np.any(choice == np.arange(6)+1)):
  61.         choice = inputNumber("Please choose a statistic: ")
  62.     return choice-1
  63.  
  64. def displaySubMenu1():                                       #Sub Menu
  65.     for i in range(len(filterItems)):
  66.         print("{:d}. {:s}".format(i+1, filterItems[i]))
  67.     choice = 0
  68.     while not (np.any(choice == np.arange(7)+1)):
  69.         choice = inputNumber("Please choose a filter: ")
  70.     return choice-1
  71.  
  72. def displaySubMenu2():                                       #Sub Menu
  73.     for i in range(len(bacteriatypes)):
  74.         print("{:d}. {:s}".format(i+1, bacteriatypes[i]))
  75.     choice = 0
  76.     while not (np.any(choice == np.arange(4)+1)):
  77.         choice = inputNumber("Please choose a bacteria: ")
  78.     return choice-1
  79.  
  80. #DataLoad-----------------------------------------------------------
  81.  
  82. def dataLoad(filename):
  83.     if os.path.isfile(filename)==  False:
  84.         data="error, not a valid directory"
  85.     else:
  86.         data = np.loadtxt(filename)
  87.         data1= data
  88.         i = 0
  89.         while i in range(len(data)):
  90.             if data[i, 0] < 10 or data[i, 0] > 60:
  91.                 print("error in line", i, "temperature is out of range")
  92.                 data = np.delete(data, i, 0)
  93.             i+=1
  94.         i=0
  95.         while i in range(len(data)):
  96.             if data[i, 1] < 0:
  97.                 print("error in line", i, "growth rate is not a negative number")
  98.                 data = np.delete(data, i, 0)
  99.             i+=1            
  100.         i=0
  101.         while i in range(len(data)):
  102.             if data[i, 2] > 4 or data[i, 2] < 1:
  103.                 print("error in line", i, "unrecognized bacteria ID number")
  104.                 data = np.delete(data, i, 0)
  105.             i+=1
  106.         print (data)
  107.         return data
  108.  
  109.  
  110.  
  111. #DataStatisics-------------------------------------------------------
  112. def dataStatistics(data, statistic):
  113.     if statistic=="Mean Temperature":
  114.         result=np.mean(data[:,0])
  115.     elif statistic=="Mean Growth rate":
  116.         result=np.mean(data[:,1])
  117.     elif statistic=="Std Temperature":
  118.         T=data[:,0]
  119.         result=T.std(0)
  120.     elif statistic=="Std Growth rate":
  121.         G=data[:,1]
  122.         result=G.std(0)
  123.     elif statistic=="Rows":
  124.         numrows=len(data)
  125.         result=numrows
  126.     elif statistic=="Mean Cold Growth rate":
  127.         (x,y,z)=np.mean(data[data[:,0]<20],axis=0)
  128.         result=y
  129.     elif statistic=="Mean Hot Growth rate":
  130.         (x,y,z)=np.mean(data[data[:,0]>50],axis=0)
  131.         result=y
  132.     return result
  133.  
  134. def dataPlot(data):
  135.     data=np.loadtxt("Project 1.txt")
  136.     def plot1():
  137.         objects = ("1","2","3","4")
  138.         y_pos = np.arange(len(objects))
  139.     #count how many times each number(type of bacteria) appears in the sub-matrix
  140.     #shoul i create a new function to do this and then call it to plot the bar graph?
  141.         Numbacteria=data[:,2]
  142.         performance = [count(np.where(Numbacteria==objects))] #?
  143.    
  144.         plt.bar(y_pos, performance, align="center", alpha=0.5)
  145.         plt.xticks(y_pos, objects)
  146.         plt.ylabel("Number per type")
  147.         plt.title("Number of bacteria")
  148.         plt.show()
  149.    
  150.     def plot2():
  151.         i=0
  152.         while (data[:,2])[i]==1:
  153.             x = (data[:,0])[i]
  154.             y = (data[:,1])[i]
  155.             plt.plot(x, y,"-r", label="Salmonela enterica") # Plot line graph of x and y
  156.             plt.title("Growth rate by temperature") # Set the title of the graph
  157.             plt.xlabel("Temperature") # Set the x-axis label
  158.             plt.ylabel("Growth rate") # Set the y-axis label
  159.             plt.xlim([10, 60]) # Set the limits of the x-axis
  160.             plt.ylim([0, 1]) # Set the limits of the y-axis
  161.             i=i+1
  162.         while (data[:,2])[i]==2:
  163.             x = (data[:,0])[i]
  164.             y = (data[:,1])[i]
  165.             plt.plot(x, y,"-g", label="Bacillus cereus") # Plot line graph of x and y
  166.             plt.title("Growth rate by temperature") # Set the title of the graph
  167.             plt.xlabel("Temperature") # Set the x-axis label
  168.             plt.ylabel("Growth rate") # Set the y-axis label
  169.             plt.xlim([10, 60]) # Set the limits of the x-axis
  170.             plt.ylim([0, 1]) # Set the limits of the y-axis
  171.             i=i+1
  172.         while (data[:,2])[i]==3:
  173.             x = (data[:,0])[i]
  174.             y = (data[:,1])[i]
  175.             plt.plot(x, y,"-b", label="Listeria") # Plot line graph of x and y
  176.             plt.title("Growth rate by temperature") # Set the title of the graph
  177.             plt.xlabel("Temperature") # Set the x-axis label
  178.             plt.ylabel("Growth rate") # Set the y-axis label
  179.             plt.xlim([10, 60]) # Set the limits of the x-axis
  180.             plt.ylim([0, 1]) # Set the limits of the y-axis
  181.             i=i+1
  182.         while (data[:,2])[i]==4:
  183.             x = (data[:,0])[i]
  184.             y = (data[:,1])[i]
  185.             plt.plot(x, y,"-y", label="Brochothrix thermosphacta") # Plot line graph of x and y
  186.             plt.title("Growth rate by temperature") # Set the title of the graph
  187.             plt.xlabel("Temperature") # Set the x-axis label
  188.             plt.ylabel("Growth rate") # Set the y-axis label
  189.             plt.xlim([10, 60]) # Set the limits of the x-axis
  190.             plt.ylim([0, 1]) # Set the limits of the y-axis
  191.             i=i+1
  192.         plt.show() #write plt.show() once in the end and it will plot both graphs at the same time
  193.  
  194.  
  195.  
  196.  
  197. #Main Script-----------------------------------------------------------
  198. while True:
  199.     choice=displayMenu()
  200.     if choice==1:       #Load Data
  201.         print("\nLoad Data")
  202.         data=dataLoad(inputString("Enter file name:\n>"))
  203.    
  204.     elif choice==2:      #End it later
  205.         print("\nFilter")
  206.         Filteropt=displaySubMenu1()
  207.         print(Filteropt)
  208.         if Filteropt+1==1:
  209.             Min=inputNumber("Minimum Value")
  210.             Max=inputNumber("Maximum Value")
  211.             data=data[data[:,1]>Min]
  212.            
  213.            
  214.            
  215.    
  216.     elif choice==3:    #Draw Plots
  217.         print("\nStatistics")
  218.         print("Which statistic do you want to calcultate?\n")
  219.         statistic=statisticItems[displaySubMenu()]
  220.         dataStat=dataStatistics(data,statistic)
  221.         print(dataStat)
  222.         print("\n")
  223.        
  224.        
  225.     elif choice==4:
  226.         pass
  227.     elif choice==5:
  228.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement