This is a driver script created to measure the error of my own Timer library.
This is a driver tool for calculating a certain python
script under different conditions. I didn't know how to express it, but I imagined WebDriver
and expressed it as a driver.
It's like a driver script to drive a Python
script.
python
from timer import Timer
def main():
seconds = 5
interval_set = [0.01,0.025,0.05,0.1,0.15,0.2,0.25,0.5,0.75,1,2,2.5,3,3.5,4,4.5]
num = len(interval_set) + 1
for case in range(1,num):
interval = interval_set[case-1]
timer = Timer(seconds, interval)
timer.test(seconds, interval, case)
if __name__ == '__main__':
main()
When this is executed, test
in timer.py
is executed for each value specified in ʻinterval_setand the result is recorded. The first half of
timer.py` is reprinted, but it is as follows.
python
import time
import csv
import platform
class Timer:
def __init__(self, seconds, interval):
self.interval = interval
self.seconds = seconds
self.n = int((seconds - (seconds % interval))/interval)
def timer(self, seconds,interval):
time_start = time.perf_counter()
time.sleep(self.interval*self.n)
time_stop = time.perf_counter()
self.remain = seconds - (time_stop - time_start)
if self.remain > 0: #Is it here if you add correction?
time.sleep(self.remain)
else:
pass
self.real = time.perf_counter() - time_start
self.error = self.real - seconds
return False
def test(self,seconds, interval, case):
filename = f"./error_data{case}"+"_"+str(platform.system())+".csv" #Formatted string literal
with open(filename, 'w') as f: #w is new
f.write('seconds(sec),interval(sec),real(sec),error(msec)\n')
f.close()
for i in range(1000): #Setting of measurement times
while self.timer(seconds, interval):
pass
else:
print('loop'+str(case)+' '+str(i+1)+'Time'+'interval=' + str(interval) + 'sec Error:'+str(self.error*1000)+'msec')
save_data = [self.seconds,self.interval,self.real, self.error*1000]
with open(filename, 'a', newline='') as f:
writer = csv.writer(f, lineterminator='\r\n')
writer.writerow(save_data)
In this example, the error is measured about 1000 times for every 16 interval values. It's quite a waiting time, so it's almost like training to execute it once.
This kind of work is where computers come into play.
By executing main.py, the result will be recorded in ʻerror_data1_Darwin.csv`. I'm running it on a Mac, so it's Darwin. If you run it on Raspberry Pi, it will be Linux.
$ python main.py
If you display a large number of graphs one by one, your heart will be broken, so the following script is used to graph them.
python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
num_figure = 9 #Number of csv files you want to draw
fig = plt.figure(figsize=(10.0, 15.0))
for i in range(1, num_figure+1):
df = pd.read_csv('./data/error_data{}_Darwin.csv'.format(i)) #read csv file
df_error = df['error(sec)'] #Read Error column
df_param = df['interval(sec)'] #Read Interval column
df_sec = df['seconds(sec)'] #Read Seconds column
ax = fig.add_subplot(5, 2, i) #It draws in order with a 5 row 2 column arrangement.
sns.distplot(df_error, kde=False, rug=False, bins=100)
# plt.hist(df_error,bins=100, alpha=0.4, histtype='stepfilled', color='g')
ax.set_title("Case{}".format(i)+' ('+str(df_sec[1])+'sec Timer, Int.='+str(df_param[1])+'sec)') #Give the graph a title
ax.set_ylim(0,25) #Set y-axis range
ax.set_xlim(-0.001,0.011) #Set x-axis range
plt.tight_layout() #Adjust graph spacing
plt.savefig('Error.png')
plt.show()
The graph will be drawn in no time. It will be reprinted just by changing the arrangement of the graph.
I would be grateful if you could comment, such as "This is better."
Recommended Posts