Using graph drawing using Python's Matplotlib + Seaborn for interprocess communication on Windows, a non-Python execution environment

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.

Interprocess communication (Socket communication)

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 (","))

Server side

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()

Client side

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.

seaborn is slow (-_-;), so change to imshow

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 ??

1. Heatmap generation with seaborn


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.

3. Generate heatmap 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.

Packages and libraries used

import time
import socket
import os
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

Server actual processing part reference


#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)) 

result

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.

Recommended Posts

Using graph drawing using Python's Matplotlib + Seaborn for interprocess communication on Windows, a non-Python execution environment
Using graph drawing using Python's Matplotlib + Seaborn on Windows, a non-Python execution environment
Simply build a Python 3 execution environment on Windows
Graph drawing using matplotlib
Until drawing a 3D graph in Python on windows10
Procedure for building a CDK environment on Windows (Python)
Create a Python environment for professionals in VS Code on Windows
Create a Python execution environment for Windows with VScode + Remote WSL
Create a Linux environment on Windows 10
[Definitive Edition] Building an environment for learning "machine learning" using Python on Windows
Notes for using OpenCV on Windows10 Python 3.8.3.
Programming environment for beginners made on Windows
How to draw a graph using Matplotlib
Creating a python virtual environment on Windows
I built a TensorFlow environment on windows10
Run matplotlib on a Windows Docker container
Building an environment for "Tello_Video" on Windows
A tool for creating symbolic links on Windows
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Building an environment for matplotlib + cartopy on Mac
Build a Kubernetes environment for development on Ubuntu
Notes for using TensorFlow on Bash on Ubuntu on Windows
Create a Python execution environment on IBM i
Create a Python virtual development environment on Windows
How to build a Python virtual execution environment using Visual Studio Code and pipenv on a Windows machine (also Jupyter notebook)