In Previous article, I created an exe that generates .png from the non-Python execution environment Windows for the graph of Matplotlib + Seaborn in Python. However, since the execution time is long (4 seconds), I tried to create it with the policy of launching .exe in advance and accepting it by interprocess communication.
The expected improvement is 1.Overload time is reduced because import is omitted. 2. Since it is interprocess communication, it can be directly exchanged with BYTE data instead of as a file. → But first, make only the one in 1.
Create interprocess communication by referring to this article. Ultimately, I would like to send and receive data transmission and data reply directly with BYTE, but first I will do only the trigger from another process.
The policy is to stop execution with arguments and instruct from the Socket communication Client. (Simply separate comma passing with .split (","))
import socket
#Define the host and port
host = 'localhost'
port = 64123 #Appropriate port
#socket connect
serversock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serversock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
serversock.bind((host,port))
serversock.listen(1)
if __name__ == '__main__':
client_socket, client_address = serversock.accept()
while True:
#Waiting for Message
rcvMsg = client_socket.recv(2048)
#Message reception
print('Receive data : %s' % (rcvMsg))
rcvMsg = rcvMsg.decode('utf-8').split(',')
#Check if the format is correct
Args1 = rcvMsg[0]
Args2 = rcvMsg[1]
Args3 = rcvMsg[2]
###Processing section description###
s_msg = 'Finish'
s_msg = s_msg.encode('utf-8')
client_socket.sendall(s_msg)
print('Send finish')
client_socket.close()
import socket
host = 'localhost'
port = 64123 #Arbitrary Local Port. Match with Server.
#Socket communication started
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port))
while True:
print('input command...')
massage = input().encode('utf-8')
client.send(massage)
response = client.recv(4096)
#Please enter the Break condition of While
client.close()
Make a Server and Client like this, Send a message from Client separated by commas → Execute processing on Server I made it like this.
Since the overhead has been eliminated, [Measure the time] until the graph is generated (https://qiita.com/fantm21/items/3dc7fbf4e935311488bc). First, make Server + Client with Python and check the operation. About 0.2-0.4 seconds. This works fine, but I'd like to make it a little faster if I want to.
Where is it slow ??
def Heatmap(data):
ax = sns.heatmap(data)
return ax
~ 0.1 seconds 2. filesave
plt.savefig(os.path.join(save_path,save_name),facecolor="blue")
~ 0.15 seconds
First of all, 1. Seaborn is slow, so I decided to do it with imshow of Matplotlib.
def HeatMapFast(data):
plt.figure()
plt.imshow(data,interpolation='nearest',vmin=0,vmax=15,cmap='rainbow')
plt.colorbar()
#bx = plt.show()
return plt
~0.05 With this, the heatmap creation part is OK once.
Next is about 2. [Investigating how to expand in memory without actually saving savefig] (https://qiita.com/kaito__/items/9aa63cccc99261814065)
This method did not change the execution process at all. It seems that the process to be performed before saving the graph is taking a long time. This part is a future issue.
import time
import socket
import os
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
#data is a two-dimensional array.csv file
data = np.loadtxt(load_path,delimiter=",")
#seaborn heatmap
hoge = Heatmap(data)
hoge.plot()
plt.savefig(os.path.join(save_path,save_name),facecolor="blue")
#matplotlib heatmap
hogehoge = HeatMapFast(data)
hogehoge.savefig(os.path.join(save_path,save_name2))
loadCSV:0.000997781753540039[sec]
HeatMap():0.09075665473937988[sec]
SaveFig:0.115692138671875[sec]
HeatMapFast():0.05684828758239746[sec]
SaveFig2:0.13364338874816895[sec]
That's it. HeatMap () is Seaborn and HeatMapFast () is imshow. Both of them have an image of 0.2 seconds or less when combined with savefig. I want to make it a little faster.