I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas

Motivation

-See (1)

environment

Target

――The automation method is shown by taking the automation of manual operation in my environment as an example.

Method

There is a package called pandas that can be used in Python and is suitable for table operations like Excel. We will present an example of "Graphing the automatically acquired voltage value" using this and a package called matplotlib that can easily and neatly draw a graph.

Example

Suppose that when a current is flowing in a wide range in space in one direction, the current is measured using a mat smaller than the space. When the mate is gradually crossed perpendicular to the current and the current is recorded step by step, the "movement control" and "current measurement" are controlled from pyserial. Sigma Kouki SHOT102 is used for movement control, and ADVANTEST's R6441 series is used for current measurement. I will. 2019-12-09_19.57.13.png

Cell1

import serial
import time
import pandas as pd
import matplotlib.pyplot as plt

serial uses pyserial, time creates the interval time for serial communication, pandas uses dataframe, and matplotlib.pyplot is used for drawing graphs.

Cell2

##Constant specification
MAX = 42000
COMpulse = "COM9"
COMampere = "COM10"
bitRate = 9600

Specify the constant used in pyserial. Cell3

##Variable initialization
pulse = 0
ampere_list = []
pulse_list = []
ampere_average_list =[]

Initialize the variables to use.

Cell4 (optional)

##Move to the back origin(Initialization)
ser = serial.Serial(COMpulse, bitRate, timeout=0.1)
ser.write(b"H:2-\r\n")
time.sleep(0.1)
print(ser.read_all())
ser.close()

Move to the operation check and measurement origin.

Cell5 pulse takes a value of 0-42000 and moves 3 μm per pulse. (Actually it moves 6 μm per 2 pulses, but the command seems to be written like this). The current value at each position is sampled 5 times and averaged to the value at that point. The average number of times is arbitrary. After moving the optical table to the end, attach the list of current and position values as a dataframe, convert the pulse value to the actual length, and draw with matplotlib. It has become a flow. (By the way, I put the dataframe in csv.)

##Start measurement
while 1:
    if pulse >= MAX:
        ##End the while statement when the position reaches MAX
        break
    ##Record current location information
    pulse_list.append(pulse/2) #For some reason, pulse is specified by 2 and commanded, so it is divided by 2.
    ##Measure the current(Take 5 times and average it as the value at that position)
    for i in range(5):
        ser = serial.Serial(COMampere,bitRate,timeout=0.1)
        time.sleep(0.5)
        ser.write(b"MD?\r\n")
        time.sleep(0.5)
        tmp = ser.read_all()
        ampere = float(tmp.split()[1])
        ampere_average_list.append(ampere)
        ser.close()
    ##Current and pulse(position)To list
    ampere_list.append(sum(ampere_average_list)/len(ampere_average_list))
    ampere_average_list = [] #Initialize list
    
    ##Move the optical table
    pulse += 1000
    position = "A:2+P"+str(pulse)+"\r\n" #Create a command by combining strings
    ser = serial.Serial(COMpulse,bitRate,timeout=0.1)
    ser.write(bytes(position, 'UTF-8'))
    time.sleep(0.5)
    ser.write(b"G:\r\n")
    ser.close()
    

##Turn the list into a dataframe
print(ampere_list)
print(pulse_list)
df = pd.DataFrame({'ampere(A)':ampere_list,'pulse':pulse_list})
##Change pulse value to length
def pulseToMilliMeter(pulse):
    return pulse*0.006
df["position(mm)"] = df["pulse"].map(pulseToMilliMeter) #You can do things like apply batch functions in Excel
df.to_csv('./csv/hoge/result.csv',index=False)
##Draw a graph using matplotlib
plt.figure()
df.plot(x='position(mm)',y='ampere(A)')
plt.savefig('./img/sample.png')
plt.close('all')

in conclusion

Suppose that the efficiency has been improved. In fact, all you have to do is press Enter, which makes it much simpler and more reproducible. If there are other sources that can be made more efficient, I will write more.

Recommended Posts

I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas
I want to improve efficiency with Python even in an experimental system (2) RS232C and pySerial
I want to improve efficiency with Python even in an experimental system (1) Install Anaconda with Chocolatey
I want to improve efficiency with Python even in an experimental system (4) Use ser.close () when an error is thrown using try syntax
I want to do something like sort uniq in Python
I want to improve efficiency with Python even in the experimental system (5) I want to send a notification at the end of the experiment with the slack API
I wanted to do something like an Elixir pipe in Python
I want to do something in Python when I finish
I want to do ○○ with Pandas
I want to color a part of an Excel string in Python
I want to do Dunnett's test in Python
I want to work with a robot in python.
[ML Ops] I want to do multi-project with Python
I want to manipulate strings in Kotlin like Python!
How to read an Excel file (.xlsx) with Pandas [Python]
I want to debug with Python
Expand a Python nested dictionary to do something like Pandas' MultiIndex
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to run Rails with rails s even in vagrant environment
I want to do a full text search with elasticsearch + python
[Machine learning] I tried to do something like passing an image
I tried to improve the efficiency of daily work with Python
I want to play with aws with python
Do something like Redis transactions in Python
#I tried something like Vlookup with Python # 2
I want to do it with Python lambda Django, but I will stop
Environment maintenance made with Docker (I want to post-process GrADS in Python
I want to do a monkey patch only partially safely in Python
[Python] I want to know the variables in the function when an error occurs!
I want to use MATLAB feval with python
[Improve work efficiency with Python] Request: Make all Excel PDFs in this folder.
I want to create a window in Python
I want to send Gmail with Python, but I can't because of an error
I want to mock datetime.datetime.now () even with pytest!
I want to make a game with Python
[Python] How to read excel file with pandas
I want to be an OREMO with setParam!
I want to merge nested dicts in Python
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to realize something like AutoHotkey with AutoKey on Ubuntu (Kali Linux)
I want to write to a file with Python
Send an email with Excel attached in Python
I want to display the progress in Python!
[Python] I want to add a static directory with Flask [I want to use something other than static]
I want to save a file with "Do not compress images in file" set in OpenPyXL
I want to get an error message in Japanese with django Password Change Form
What to do if you run python in IntelliJ and end with an error
I want to write in Python! (1) Code format check
I want to convert an image to WebP with lollipop
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to transition with a button in flask
How to do hash calculation with salt in Python
I want to handle optimization with python and cplex
[Python] I want to merge Excel files anyway (pandas.merge)
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I was addicted to scraping with Selenium (+ Python) in 2020
I tried to implement an artificial perceptron with python
I want to write in Python! (3) Utilize the mock