Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- #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.
- #this code also gives you max and mins of any plot
- #this code also makes and saves excel files
- #you can use askdirectory to get the full directory
- #the try and export are there so that this code works on Python 3.7 or 2.7
- import pandas as pd
- import matplotlib.pyplot as plt
- import xlsxwriter
- import numpy as np
- import re
- try:
- import Tkinter as tk
- from tkFileDialog import askopenfilename
- except:
- import tkinter as tk
- from tkinter.filedialog import askopenfilename
- file_path=askopenfilename() #says that file path is the same as file name, opens up a dialog box to open a file
- import os
- 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
- startpath = os.path.dirname(file_path) #a shorter way of putting the filepath of the directory to shorten future code
- filestoprocess=[] #creates a space to store files
- if mode == '1': #gives us the option of only processing one file
- filestoprocess.append(file_path)
- if mode == '3': # gives us the option of processing the entire directory and subdirectory
- for (dirpath, dirnames, filenames) in os.walk(startpath): #uses os.walk to open up the directory, subdirectory and filenames
- print(dirpath)
- print(dirnames)
- print(filenames)
- for filename in filenames:
- if filename[-3:]=="txt": #if the last three characters in the filename is txt, add it to the list
- filestoprocess.append(os.path.join(os.path.normpath(dirpath), filename)) #adds filenames to the list
- if mode == '2': #gives us the option of just processing the entire directory
- for filename in os.listdir(os.path.dirname(file_path)):
- if filename[-3:]=="txt":
- filestoprocess.append(os.path.join(startpath, filename))
- DataSummary = xlsxwriter.Workbook('CV DataSummary.xlsx')
- row = 1
- column = 0
- RawData = DataSummary.add_worksheet()
- BGData = DataSummary.add_worksheet()
- BGSubtractedData = DataSummary.add_worksheet()
- for filename in filestoprocess:
- dfspectra=pd.read_csv(filename, delimiter = '\t+|\s+', header = None, names = ['Potential (V)','Current (mA)'], engine='python', skipfooter=1)
- fig, ax = plt.subplots()
- ax.plot(dfspectra['Potential (V)'],dfspectra['Current (mA)'], "ok")
- ax.set(xlabel='Potential (V)', ylabel='Current (mA)', title='CV Data')
- if column == 0:
- RawData.write_column(row,column,dfspectra['Potential (V)'])
- BGData.write_column(row,column,dfspectra['Potential (V)'])
- BGSubtractedData.write_column(row,column,dfspectra['Potential (V)'])
- column = column +1
- RawData.write_column(row,column,dfspectra['Current (mA)'])
- BGData.write_column(row,column,dfspectra['Current (mA)'])
- BGSubtractedData.write_column(row,column,dfspectra['Current (mA)'])
- DataSummary.close()
- ymax = np.max(dfspectra['Current (mA)'])
- ymax2 = np.min(dfspectra['Current (mA)'])
- maxVal = dfspectra.loc[dfspectra['Current (mA)'].idxmax()]
- minVal = dfspectra.loc[dfspectra['Current (mA)'].idxmin()]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement