Plot in python without matplotlib - python

I'm writing a program for TopSpin, an NMR processing software from Bruker. TopSpin runs the python scripts in it's own environment using jython. The python version it returns is 2.5.* (I don't remember the last one).
import sys
print(sys.version)
>> 2.5.*
As the code is executed in it's own environment, I cannot update the version of python/jython and I also cannot install missing packages.
For my current project I would need a functionality to plot the values of two lists. The first list on the x-axis and the second one on the y-axis.
For this task I wanted to use matplotlib's function plot(xlist, ylist). Unfortunately TopSpin throws an error at me, saying that the package matplotlib does not exist. As I can't install the package (or at least I have no idea how I should), I now need a workaround.
Is there a way I could call a python script that runs in the normal environment of my computer (like idle) where python 3.6.3 and matplotlib are installed? Ideally this could generate the plot and ship it back to the TopSpin python program.
Or is there an other workaround that enables me to plot my data and show it to the user/save it on the disk, that does not rely on fancy or new packages?

Related

How do to use a python script in Unity?

I'm trying to run a face detection model in Unity. It gets input from the webcam, then spits out a face. But trying to make this work with C# has been an absolute nightmare. And despite all my suffering, I still haven't been able to make it work!
If I could use python, I'd be able to get it done easily. So, obviously, I want to find a way to get a python script working in Unity. But IronPython is the only thing I've been able to find, and it's outdated.
I need either knowledge of how to make IronPython work in spite of being outdated, or some other method. Please.
Unfortunately, Unity at this time does not support Python. Although, there is an asset that you can use a bit of Python with. I am not sure what you can do with this asset but I know it could help a minimal amount:https://assetstore.unity.com/packages/tools/integration/python-interpreter-645
Quick Note: Most programming languages work about the same way. If you figure out the documentation and grammar/punctuation for C#/UnityC#, you should be off just fine.
I try to use python once on Unity and I found a few ways:
There is a package call "IronPython" where you can add a python file to your unity project and then call a function from C# to your python code, to do that you should follow this:
We already know that we can use python to use .net internal calls.
Now we may use the same to start a console that can accept a scripting language in Unity engine.
To do this we have to include certain dll files.
These dll files must be present in Assets>plugins
IronPython.dll
IronPython.Modules.dll
Microsoft.Scripting.Core.dll
Microsoft.Scripting.dll
Microsoft.Scripting.Debugging.dll
Microsoft.Scripting.ExtensionAttribute.dll
Microsoft.Dynamic.dll
Once the Plugins are in place.
Initiate the Cs code
PythonEngine engine = new PythonEngine();
engine.LoadAssembly(Assembly.GetAssembly(typeof(GameObject)));
engine.ExecuteFile("Test.py");
Where test.py is the python code.
Initiate python side:
import UnityEngine from UnityEngine
import *
Debug.Log("Hello world from IronPython!")
References:
https://github.com/cesardeazevedo/Unity3D-Python-Editor
http://techartsurvival.blogspot.in/2013/12/embedding-ironpython-in-unity-tech-art.html
IronPython in Unity3D
the issue with this way is that most of the python module are not supported.
2.the second way is to create a file like json that contain the data you want to send to the json and then create an output json that send the output from the python script, this way is very limited with what you can send because the data must be contain in your json.
the last way that work for me is to install the Nuget package and copy the script from python to c# line by line with the relevent module installed in Unity and it's work for me, but copy a long code can take time.
this is a reference to the package:
https://github.com/GlitchEnzo/NuGetForUnity
and then to install the relevent package you should press on NuGet → Manage NuGet Packages and the choose the relevent package(for me it was Numpy and it work grate).
hope it will help you
I don't know how recent it is but there is a Unity package for python available on unity 2019.3 and further versions.
Warning the first versions of this package can't use Python3.
You can see more for yourself by the following link.
https://docs.unity3d.com/Packages/com.unity.scripting.python#2.0/manual/index.html
I hope this may help you.
We are thrilled to announce that Python for Unity 4.0.0-exp.5 is now available!
4.0.0-exp.5 is a major upgrade from our last public release, and incorporates a large number of changes. In summary:
Based on Python 3.7; scripts based on Python 2.7 will need to be ported.
Users no longer need to install Python on their system.
In-process Python is no longer reinitialized when the Unity domain reloads.
Removed the out-of-process API. The PySide example now runs in-process and is much simpler.
Limited support for a virtual environment workflow via the ProjectSettings/requirements.txt file.
Many bug fixes.
Documentation for the Python for Unity package is available here, and the full changelog can be found here.
This is an experimental release, and thus is not visible in Package Manager. To install this package, open Package Manager, click the + at the top left and select Add package by name.... Enter com.unity.scripting.python as the name and and 4.0.0-exp.5 as the version and click Add. Alternatively, you may edit Packages/manifest.json and add "com.unity.scripting.python": "4.0.0-exp.5", to the list of dependencies, or edit the existing entry for Python for Unity to update the version.
Soursce: https://forum.unity.com/threads/python-for-unity-release-announcements.1084688/
Documentation: https://docs.unity3d.com/Packages/com.unity.scripting.python#4.0/manual/index.html
Unity not supported python, But you Can write Python Code and run it by Socket programing, Create Server with python and send data,in C# Connect to server and use data sended with python.

how to make my own copy of a python package

I have a python script that I use to analyze data. I rely on number crunching packages like numpy and others to work with my data. However, the packages constantly evolve and some functions depreciate, etc. This forces me to go through the script several times per year to fix errors and make it work again.
One of the solutions is to keep an older version of numpy. However, there are other packages that require a new version of numpy.
So the question I have is: Is there a way to 1) keep multiple versions of a package installed or 2) have a local copy the package located in the directory of my script so I am in control what I am importing. For example, I can have my own package where I will have all the different packages and versions I need.
Later, I can simply import libraries I want
from my_package.numpy_1_15 as np115
from my_package.numpy_1_16_4 as np1164
and later in my code, I can decide which function to use from which numpy version. For example:
index = np115.argwhere(x == 0)
This is my vision of the solution to my problem where I want to keep using old functions from previous versions of numpy (or other libraries). In addition, in this way, I can always have all the libraries needed with me in my script directory. So, if I need to run the script on a different machine I don't need to spend hours figuring out if everything is compatible.
Here are possible proposed solutions and why they do not solve my problem.
Virtual Environments in Python or Anaconda.
There are a bunch of introductions (for example) available that explain how to use them. However, virtual environments require maintenance and initial setup. Imagine, if I can just have a python code that performs well a specific computational task independent on what year it is and what packages are installed on any machine. The code can be shared among different research groups and will always work.
python create standalone executable linux
I can create standalone executable (example). However, it will be compiled​ and cannot be dynamically changed the really nice feature of Python

Using Pandas with FreeCAD

I am brand new to FreeCAD and fairly new to python in general.
I have a project going on in which I have a list of several hundred (x,y) coordinates stored in two columns in an Excel doc. These points define a slice of an elevation profile. What I'm needing to do is draw a line/shape in FreeCAD using these points that can then be extruded into an extended version of this shape. Since the data is currently in excel, I'm leaning towards using pandas to access it, but I'm open to other ideas.
The problem I'm having with this line of thinking so far is that I can't figure out a way to access pandas from inside FreeCAD, so I've been attempting to use Winpython(2.7.10) through the Spyder ide to import FreeCAD(0.16) and pandas into the same script. I've tried adding both the Freecad/bin and Freecad/lib folders to my path (using this recommended method https://www.freecadweb.org/wiki/Embedding_FreeCAD/en), but have yet to have any success importing.
The error is: "DLL load failed: The specified procedure could not be found." .... Searching around, I can't seem to find FreeCad.dll or FreeCAD.so anywhere in my install, yet FreeCAD runs normally when used independently.
To summarize: Wanting to run FreeCAD through an external python console in order to access excel data through pandas (which doesn't seem to be available within FreeCAD itself). In doing so, I get an error and FreeCAD isn't accessible. It might be a version compatibility issue, or something more complex.
If you made it here, thank you for taking the time to read through my fairly complex/niche problem. Please feel free to suggest any more elegant alternatives, as I said, I'm new to this and likely haven't come across all possible solutions.
Thank you
I'm not exactly certain what "pandas" is - a quick search didn't reveal anything except the game engine Panda3D and the animal itself...
As for accessing data external to FreeCAD, this is something I've been exploring myself lately. At the moment, some success has been achieved by starting FreeCAD in server mode and having it watch an excel file, then running python script when it changes.
This approach would allow you to parse an Excel file using commonly available python libraries for that purpose.
EDIT:
Having been informed on just what pandas is, it seems odd you can't make it work with FreeCAD. However, there could be a number of causes:
Old version. FreeCAD is undergoing really significant change right now. Ensure you're using the latest stable release (0.17 - Roland), but I'd recommend getting your hands on the latest 0.18-dev release
Incorrect python version - FreeCAD currently is built on Py2, though Py3 builds are possible (I run FreeCAD / Py3 on my linux machine). Ensure you've installed pandas appropriately to match the python version FreeCAD depends upon (Py2, most likely).
Invalid install - If you know pandas imports and works in the python interpreter itself, try importing and running pandas code from the FreeCAD python console.

Distributing an interactive matplotlib plot

I have a Python script that generates an interactive matplotlib plot with several sliders and radio buttons using the matplotlib.widgets submodule. I'd like to let others play with the resulting plot without having to install python, scipy, numpy, and matplotlib.
I first tried converting my python script to a stand-alone executable that I could distribute. This turned out to be a nightmare - every package I tried (pyinstaller, py2exe, cx_Freeze) failed for one reason or another. The main issue I had was with integrating various scipy and matplotlib libraries, and I'm now very pessimistic about successfully "freezing" my interactive plot.
My next idea was to see if I could get some interactivity through a web browser. Using an IPython notebook with the third-party package JSAnimation initially seemed very promising (http://nbviewer.ipython.org/github/jakevdp/JSAnimation/blob/master/animation_example.ipynb), but it seems that the package only supports the matplotlib "animate" function. This doesn't quite fit the bill for what I'd like to do, as I have multiple sliders which 1) doesn't seem to be supported by this package, and 2) even if supported, would likely result in too many static figures to pre-render effectively, since any such arrangement would require snapshots for every possible combination of the three variables.
Any ideas for how to get this interactive matplotlib plot to others without requiring them to install Python?

Handling multiple python distributions

I guess I am partly responsible for the mess, but I can imagine that several users might have similar problems. I installed several python versions/distributions on my new Ubuntu-Gnome 13.04, which comes with python3.3 (0), including
python2.7.5
PyLab (python2.7.3)
EPD-python2.7.3
Canopy-1.1.0 (??)
Anaconda 1.6.1 (python2.7.5)
My main purpose for doing so was to get a python environment with the functionality of the Qt-console, which previously worked perfectly with the EPD-python distribution.
I now encountered several problems with basically all of the above. "Plain" pythons, such as 0, 1 and 2 do not offer the functionality I desire, while the others have their problems by their own, and I hope somebody knows to help here:
3: When starting the Qt-console shipped with the EPD-python distribution (installation worked perfectly fine), it cannot load the graphics package Matplotlib anymore, telling me
ImportError: Matplotlib backend_wx and backend_wxagg require wxPython >=2.8
Error initializing pylab, pylab mode will not be active
Manually importing does also not work. Why doesn't it work by itself as a stand-alone version, but requires a python version which is higher than the one EPD installed.
4: Cannot figure out how to use Canopy within a Qt-console or use the GUI for multi-tasking, i.e. multiple kernels. I previously addressed this issue here: Multiple kernels in Enthought Canopy.
5: Anaconda should also have an Qt-console. But after installing it, I cannot find any application relating to it. The only thing I can do is to run it in the terminal, either as normal python or ipython (the same I can do with EPD-python as well). It seems that this is not the full functionality of Anaconda, but I might be wrong here.
Any help is appreciated!!
BTW, can one make all these python communicate with each other and make them somehow "harmonise"?

Categories