This tutorial shows how to implements a new feature to the ImmVis framework on the server and Unity client library.
The feature to be implemented is the correlation matrix, using the function pandas.DataFrame.corr to find the correlation between the dimensions of a given dataset.
- Operating system: Windows, Linux, or macOS
- Download the ImmVisServerPython source code and follow its setup instructions
- Download the ImmVisClientLibraryUnity source code and follow its setup instructions
Adding a new feature on ImmVis consists of adding a new GRPC service on the server-side (Python) and then updating the client library (Unity) with the newly implemented function.
For more references about how to use GRPC, please refer to the GRPC documentation for Python and C#.
If you want to implement a client in any other language, please refer to the GRPC Supported Languages documentation.
- On the
ImmVisServerPythonfolder, add the code below to the DataManager class (immvis/data/data_manager.py). Please follow the exact indentation of the other functions.
def get_correlation_matrix(self):
return self.data_frame.corr()- Create the following funtion on the
ImmVisPandasservice (proto\immvis.proto)
syntax = "proto3";
service ImmVisPandas {
…
rpc GetCorrelationMatrix(Empty) returns (CorrelationMatrix) {}
}We will call our function GetCorrelationMatrix, making it receive an Empty parameter and return a CorrelationMatrix. If you need to add other parameters than Empty, please refer to the other existing functions to create custom parameters for your case. We also recommend visiting the gRPC documentation mentioned earlier.
- Create the
CorrelationMatrixandCorrelationMatrixRowmessages at the end of the proto file (proto\immvis.proto):
message CorrelationMatrix {
repeated CorrelationMatrixRow rows = 1;
}
message CorrelationMatrixRow {
string column1 = 1;
string column2 = 2;
float correlationCoeficient = 3;
}-
Run the command
.\utilities\generate_python_libs.bat(Windows) or./utilities/generate_python_libs.sh(Linux/Mac) in order to generate the GRPC python libraries onimmvis\grpc\proto -
Open the file
immvis\grpc\proto\immvis_pb2_grpc.pyand change the lineimport immvis_pb2 as immvis__pb2tofrom . import immvis_pb2 as immvis__pb2. Ignore the other errors and save the file. We are currently working to make this step automatic in the future. -
Create the file
immvis\grpc\mappers\correlation_matrix.pywith the following content:
from ..proto.immvis_pb2 import CorrelationMatrix, CorrelationMatrixRow
from pandas import DataFrame, Series
def map_correlation_matrix(data_frame: DataFrame) -> CorrelationMatrix:
rows = []
for index in data_frame.index.values:
row = data_frame.loc[index]
for column_index in row.index.values:
coeficient = row.loc[column_index]
rows.append(CorrelationMatrixRow(column1=str(index), column2=str(column_index), correlationCoeficient=coeficient))
return CorrelationMatrix(rows = rows)
- Add the following line to
immvis\grpc\mappers\__init__.py:
from .correlation_matrix import map_correlation_matrix- Open
immvis\grpc\immvis_grpc_servicer.pyand update the mappers import line withmap_correlation_matrixcreated on the previous step:
from .mappers import get_dataset_metadata, map_correlation_matrix- Still on
immvis\grpc\immvis_grpc_servicer.py, add the following method to theImmvisGrpcServicerclass:
class ImmvisGrpcServicer(immvis_pb2_grpc.ImmVisPandasServicer):
…
def GetCorrelationMatrix(self, request, context):
correlation_matrix = self._data_manager.get_correlation_matrix()
return map_correlation_matrix(correlation_matrix)-
Still inside the server project, run the command
.\utilities\generate_csharp_libs.bat(Windows) or./utilities/generate_python_libs.sh(Linux/Mac) in order to generate the ImmVis GRPC C# files (csharp\Immvis.csandcsharp\ImmvisGrpc.cs) -
Open the project using Unity3D and navigate to the
Assets\ImmVisClientGrpcUnity\Scripts\ImmVis\Grpc\Generatedfolder -
Replace the files
Assets\ImmVisClientGrpcUnity\Scripts\ImmVis\Grpc\Generated\Immvis.csandAssets\ImmVisClientGrpcUnity\Scripts\ImmVis\Grpc\Generated\ImmvisGrpc.csby the ones that were generated on the step 1. -
To test if everything worked, open the
Assets\ImmVisClientGrpcUnity\Examples\BasicUsage\ExampleSceneBehaviour.csfile, add the linevar correlationMatrix = await grpcClient.GetCorrelationMatrix(new Empty());after the dataset is loaded.
// Add the code below this line:
Debug.Log($"Loaded a dataset:\n{datasetInfoStringBuilder.ToString()}");
// Code to be added:
var correlationMatrix = await grpcClient.GetCorrelationMatrix(new Empty());
// Using the correlation matrix
Debug.Log($"Using:\n{correlationMatrix}");-
Run the server and then run the Unity3D project to check the project will build correctly.
-
If everything worked correctly, the project will run, with logs available on the Unity console.