Advertisement
Guest User

I hate coding

a guest
May 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. """
  2. #this code opens up a dialog box, makes you pick a file, asks you if you want to graph the file, the directory or the directory and all subdirectories, and graphs it. This can be changed for various data forms.
  3. #this code also gives you max and mins of any plot
  4. #this code also makes and saves excel files
  5.  
  6. #you can use askdirectory to get the full directory
  7. #the try and export are there so that this code works on Python 3.7 or 2.7
  8.  
  9. import pandas as pd
  10. import matplotlib.pyplot as plt
  11. import xlsxwriter
  12. import numpy as np
  13. import re
  14.  
  15.  
  16. try:
  17. import Tkinter as tk
  18. from tkFileDialog import askopenfilename
  19. except:
  20. import tkinter as tk
  21. from tkinter.filedialog import askopenfilename
  22. file_path=askopenfilename() #says that file path is the same as file name, opens up a dialog box to open a file
  23.  
  24.  
  25.  
  26. import os
  27.  
  28. mode = input('Root is '+os.path.dirname(file_path)+'\n'+'process one file (1), all files in this directory (2), all files in this and all subdirectories (3)') #opens a dialog in the console and asks us to input 1,2 or 3
  29.  
  30. startpath = os.path.dirname(file_path) #a shorter way of putting the filepath of the directory to shorten future code
  31.  
  32. filestoprocess=[] #creates a space to store files
  33.  
  34. if mode == '1': #gives us the option of only processing one file
  35. filestoprocess.append(file_path)
  36.  
  37. if mode == '3': # gives us the option of processing the entire directory and subdirectory
  38. for (dirpath, dirnames, filenames) in os.walk(startpath): #uses os.walk to open up the directory, subdirectory and filenames
  39. print(dirpath)
  40. print(dirnames)
  41. print(filenames)
  42. for filename in filenames:
  43. if filename[-3:]=="txt": #if the last three characters in the filename is txt, add it to the list
  44. filestoprocess.append(os.path.join(os.path.normpath(dirpath), filename)) #adds filenames to the list
  45.  
  46. if mode == '2': #gives us the option of just processing the entire directory
  47. for filename in os.listdir(os.path.dirname(file_path)):
  48. if filename[-3:]=="txt":
  49. filestoprocess.append(os.path.join(startpath, filename))
  50.  
  51. DataSummary = xlsxwriter.Workbook('CV DataSummary.xlsx')
  52. row = 1
  53. column = 0
  54. RawData = DataSummary.add_worksheet()
  55. BGData = DataSummary.add_worksheet()
  56. BGSubtractedData = DataSummary.add_worksheet()
  57.  
  58. for filename in filestoprocess:
  59. dfspectra=pd.read_csv(filename, delimiter = '\t+|\s+', header = None, names = ['Potential (V)','Current (mA)'], engine='python', skipfooter=1)
  60. fig, ax = plt.subplots()
  61. ax.plot(dfspectra['Potential (V)'],dfspectra['Current (mA)'], "ok")
  62. ax.set(xlabel='Potential (V)', ylabel='Current (mA)', title='CV Data')
  63. if column == 0:
  64. RawData.write_column(row,column,dfspectra['Potential (V)'])
  65. BGData.write_column(row,column,dfspectra['Potential (V)'])
  66. BGSubtractedData.write_column(row,column,dfspectra['Potential (V)'])
  67. column = column +1
  68. RawData.write_column(row,column,dfspectra['Current (mA)'])
  69. BGData.write_column(row,column,dfspectra['Current (mA)'])
  70. BGSubtractedData.write_column(row,column,dfspectra['Current (mA)'])
  71.  
  72. DataSummary.close()
  73.  
  74.  
  75. ymax = np.max(dfspectra['Current (mA)'])
  76. ymax2 = np.min(dfspectra['Current (mA)'])
  77.  
  78. maxVal = dfspectra.loc[dfspectra['Current (mA)'].idxmax()]
  79. minVal = dfspectra.loc[dfspectra['Current (mA)'].idxmin()]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement