How to Experiment with NumPy in Flux?
Introduction
Numpy is a python module widely used in scientific computing area and several customer have expressed they would like to use in their python script for Flux.
However, NumPy is a native C-Python module whereas PyFlux scripts are executed by Jython, a Java Python interpreter, which is not able to execute C Python modules thus preventing NumPy usage with Flux.
Nowadays with the experimental JyNI (Jython Native Interface) project it becomes possible to run native C-Python extensions with Jython 2.7.2. In particular it becomes possible to use Numpy modules with Jython 2.7.2
Since Flux 2018.0 release, Flux has been upgraded to use Jython 2.7.2 and it becomes possible to use the JyNI library to experiment with native C Python modules like NumPy.
Besides usage of NumPy with Flux has not been qualified by Altair and no particular effort has been made to allow a smooth integration of Flux with NumPy. In particular there are known limitations / issues when trying to use NumPy in Flux: See the Troubleshooting section for more details.
Step-by-step
The steps below describe how to install Numpy for Flux on Windows. Numpy was successfully tested with Flux by applying the exact steps described below.
It may also work by choosing different options, for example when installing Python 2.7.2, but we did not test all the possible choices.
Uninstallation existing Python
This step may not be necessary but Numpy has not been tested with Flux on a machine where Python 2.7.2 was coexisting with another version of Python.
Install Python 2.7.2
- Choose single-user installation: "Just for me"
- Installation Path: C:\Python27
- Keep default installation
Install the required Python module
- Setup a Cmd.exe window to install the Python modules
- Open a Cmd.exe windows and enter the following commands
mkdir C:\Numpy4Flux
cd C:\Numpy4Flux
set PATH=C:\Python27;%PATH%
- Keep the Cmd.exe window open to execute the next steps
- Open a Cmd.exe windows and enter the following commands
- Install SetupTools 44.1.1
- Download: setuptools-44.1.1.zip
- From the downloaded zip file extract setuptools-44.1.1 to your working directory
- In the Cmd.exe window enter the following commands:
cd setuptools-44.1.1
python.exe setup.py install
- Install PIP 20.3.4
- Download: pip-20.3.4.tar.gz
- From the downloaded zip file extract pip-20.3.4 to your working
directory (
C:\Numpy4Flux
) - In the Cmd.exe window enter the following commands:
cd pip-20.3.4
python.exe setup.py install
- Install Numpy 1.13.3
- Download the Numpy package from https://pypi.org/project/numpy/1.13.3/
- For Windows 64-bits: numpy-1.13.3-cp27-none-win_amd64.wh
- Copy the downloaded file to your working directory
- In the Cmd.exe window enter the following commands:
-
C:\Python27\Scripts\pip.exe install numpy-1.13.3-cp27-none-win_amd64.whl
-
- Numpy is now installed in:
C:\Python27\Lib\site-packages
- Test that Numpy is correctly installed by opening the interactive Python
interpreter in the Cmd.exe windows:
python.exe
- Then try to import the numpy module:
import numpy
- If no error message is displayed the module has been successfully installed for Python
- Exit the Python interpreter:
exit()
- Download the Numpy package from https://pypi.org/project/numpy/1.13.3/
Install JyNI 2.7 alpha 5 in Flux
- In your web browser navigate to: https://github.com/Stewori/JyNI/releases/tag/v2.7-alpha.5
- Download both:
- The cross-platform JAR file: JyNI.jar
- The platform-specific archive for Windows 64-bits: JyNI-2.7-alpha.5-bin-win-amd64.zip
- Install the JyNI.jar into Flux
- Copy JyNI.jar to:
%FLUX_INSTALL_DIR%\Third_party\jython-2.7.2\javalib\JyNI.jar
- Copy JyNI.jar to:
- Install the JyNI.dll folder to Flux
- Create a
userdll
directory in Flux installation directory:%FLUX_INSTALL_DIR%\Third_party\jython-2.7.2\userdll
- From
JyNI-2.7-alpha.5-bin-win-amd64.zip
extract theJyNI.dll
folder to:%FLUX_INSTALL_DIR%\Third_party\jython-2.7.2\userdll\JyNI.dll
- Create a
Configure Flux custom directories
Configure the Flux Supervisor options to use your UserDLL and UserLib folders for all Flux modules (2D, 3D, Skew, PEEC):
- Starts Flux Supervisor
- Open the Options dialog by clicking on the Options button in the bottom left
- Navigate to the options
- For every Flux application (2D, 3D, Skew and PEEC) enther the following
paths:
- User Python scripts directory :
C:\Python27\Lib\site-packages
- User libraries directory :
%FLUX_INSTALL_DIR%\Third_party\jython-2.7.2\userdll
- User Python scripts directory :
- Click Apply and close with OK
Test NumPy in Flux
- From FluxSupervisor open Flux by clicking on Start a new project
- In the PyFlux Command window enter the following Python command
- You should obtain the following output in the Output
console
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Troubleshooting
- Flux does not start any more using module Y (2D,3D,Skew,PEEC) after I
configured NumPy for module X
- What to do?
Check you have configured the
userdll
directory for the module Y you want to start. - ExplanationOnce you have copied the
JyNI.jar
in%INSTALLFLUX%\Third_party\jython-2.7.2\javalib
, Flux requires access to theJyNI.dll
in order to start. This is the reason why, in the Flux Supervisor, theuserdll
directory must be configured for all modules (2D, 3D, Skew, PEEC) you want to use.It is understood this behaviour is not satisfying and should be improved in the future.
- What to do?
- Flux Server does not work any more after I configured NumPy
- What to do?
In order to fix the FluxServer you need to remove the
JyNI.jar
from the directory:%INSTALLFLUX%\Third_party\jython-2.7.1\javalib
Obviously, doing this will "break" the usage of NumPy in Flux.
This is a known limitation: it means that currently the user has to choose between using NumPy in Flux, or use the Flux Server but he cannot do both at the same time.
- Explanation
The reason is the same as the previous issue: once
JyNI.jar
is added to the directory it will be automatically loaded by Flux Server as well and Flux Server would need access to theJyNI.dll
in order to start.However for Flux Server there is currently no way for the user to customize the directory where the FluxServer should look for the additional required DLL.
- What to do?