Examples of PythonGateway Usage. Python Gateway for InterSystems Data Platforms. Execute Python code and more from InterSystems IRIS brings you the power of Python right into your InterSystems IRIS environment:
- Execute arbitrary Python code
- Seamlessly transfer data from InterSystems IRIS into Python
- Build intelligent Interoperability business processes with Python Interoperability Adapter
- Save, examine, modify and restore Python context from InterSystems IRIS
- Download the repo:
git clone https://github.com/intersystems-community/PythonGatewaySamples.git
cd PythonGatewaySamples
- Get the images:
docker-compose build --pull
- Start docker:
docker-compose up -d
- Install PythonGateway.
- Load ObjectScript code (i.e.
do $system.OBJ.ImportDir("C:\InterSystems\Repos\PythongatewaySamples\","*.cls","c",,1)
) into Production (Ensemble-enabled) namespace. In case you want to Production-enable namespace call:write ##class(%EnsembleMgr).EnableNamespace($Namespace, 1)
. - Load restaurant data with:
do ##class(ml.match.Restaurant).Import()
. - Load match data with:
do $system.OBJ.ImportDir("C:\InterSystems\Repos\PythongatewaySamples\","*.xml","c")
- Install required libraries:
pip install -r requirements.txt
- Open and start
ml.Production
.
To choose a specific example filter by production category.
Using Dedupe or RLTK to deduplicate restaurant data.
We start with the working self-correcting model used for predictive maintenance. First, we see how production elements work together and how our transactional processes can benefit from AI/ML models. After that, we’d improve the model and see how this change propagates through production. Finally, we’ll explore different applications of this architecture.
We want to do predictive maintenance on engines. Dataset: we store information about one specific engine. We get a large array of sensor data every second. Historic data is already in the dataset. Check Engine.md for a step-by-step walkthrough. Note that GitHub does not render base64 embedded images, so you'll need a separate markdown viewer.
py.ens.Operation
– executes Python code and sends back the results.ml.engine.TrainProcess
– trains a new prediction model.ml.engine.PredictService
– a service that receives information from engine sensors and sends it to ml.engine.PredictProcess to predict engine state. At the moment it is disabled (grey) and does not transfer data.ml.engine.PredictProcess
- uses the ML model to predict engine state.ml.engine.CheckService
– regularly checks the accuracy of the ML model. If the prediction error rate is above the threshold, the service sends a request to theml.engine.TrainProcess
to update the model.ml.engine.InitService
– sends a request to theml.engine.TrainProcess
to train the initial model at production start.
Start Predict Service
and Check Service
to see model be automatically retrained when prediction quality drops.
- Import - load Python libraries
- Load Data - load the data for model training
- Extract Y - split the data into X - the independent variables and Y - the dependent variable we are predicting
- PCA - The principal components calculation for X
- CV - cross-validation of potential models
- Fit - pipeline creation and training
This example shows an image similarity search, by default we are using the footwear dataset, but any type of image can be used, as long as the target object is the main object on the images in the dataset. Additionally, this example shows how an AI model can be exposed via REST API in InterSystems IRIS. This example can be used as a feature in retail a mobile app to allow users to take product photos from a camera and find the corresponding product(s) from the retailer shop.
- Execute steps 1 and 2 from
Docker Installation
above. Do not run the containers. - To preserve the index mount volume to the
/usr/irissys/mgr/Temp
directory.docker-compose.yml
contains an example. Uncomment the lines and point docker to the empty host directory. If you're running DD on Windows with HV backend you'll additionally need to share the host directory with Docker. - Start docker:
docker-compose up -d
- (OPTIONAL BUT RECOMMENDED) To use the prebuilt index download the index file and corresponding globals here. Unpack the index into the root of the mounted directory. Import globals into the PYTHON namespace.
- Enable
ml.cam.ens.bs.InitService
- Open
http://localhost:52773/csp/user/recommend.html
and upload your image (you can use images fromimg
directory for testing)
ml.cam.data.Photo
class contains our dataset of photos. UseLoadDir
method to add more photos.ml.cam.ens.bp.LoaderProcess
receives ids ofml.cam.data.Photo
and indexes them:- Images are resized and rescaled
- Images are passed to the chosen NN to get feature vectors (note that we use NN without the last classification layer to get better results for an image as a whole)
- Feature Vectors are indexed using cosine index
- The index is saved (mmaped) on disk for later reuse
- Corresponding variables are saved as an
isc.py.data.Context
to Intersystems IRIS
- On subsequent runs
ml.cam.ens.bp.LoaderProcess
first checks if saved context exists and if it does loads the index/variables instead of building it again ml.cam.ens.bs.InitService
sends initial trainig request on production startupml.cam.ens.bp.MatcherProcess
receives one target image and uses the index to find the best matchescam.rest.Main
exposesml.cam.ens.bp.MatcherProcess
as a REST API web application/cam
recommend.html
calls/cam
to get the best matches for an uploaded image (you can use images fromimg
directory for testing)
Execute all steps from Host Installation
first.
- Download and unpack any image dataset, for example, this.
- Load dataset into InterSystems IRIS:
zw ##class(ml.cam.data.Photo).LoadDir(<dir>)
. - Enable
ml.cam.ens.bs.InitService
. - Training will take 1-5 hours depending on your CPU/GPU.
- The training process saves the search index automatically so subsequent runs will take <1 minute.
- Copy
recommend.html
to any web application. - Create new unauthenticated REST app named
/cam
withml.cam.rest.Main
dispatch class. - Open
recommend.html
in a browser and send your test image
- To train a new index (after you already have one) execute:
TRUNCATE TABLE isc_py_data.Context
TRUNCATE TABLE isc_py_data.Variable
- Sometimes after training the index might perform poorly. In that case, restart the production.
- Prebuilt index is available here, corresponding globals here. Save index as
/usr/irissys/mgr/Temp/index.ann
(this is Default for Docker, in general use checkWorkDirectory
value forml.cam.ens.bp.LoaderProcess
and save the file there asindex.ann
).